blob: 22961d7f9f428b4aca8664acbc0da14b5d5ff251 [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;
20import android.hardware.usb.UsbManager;
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 /**
47 * Opens the MTP or PTP device and return an {@link android.mtp.MtpDevice} for it.
48 *
Mike Lockwoodc4308f02011-03-01 08:04:54 -080049 * @param manager reference to {@link android.hardware.usb.UsbManager}
Mike Lockwood540380f2011-02-09 21:48:53 -050050 * @return true if the device was successfully opened.
51 */
Mike Lockwood8182e722010-12-30 15:38:45 -050052 public boolean open(UsbManager manager) {
53 if (manager.openDevice(mDevice)) {
54 return native_open(mDevice.getDeviceName(), mDevice.getFileDescriptor());
55 } else {
56 return false;
57 }
58 }
59
Mike Lockwood540380f2011-02-09 21:48:53 -050060 /**
61 * Closes all resources related to the MtpDevice object
62 */
Mike Lockwood8182e722010-12-30 15:38:45 -050063 public void close() {
Mike Lockwood8182e722010-12-30 15:38:45 -050064 native_close();
65 }
66
67 @Override
68 protected void finalize() throws Throwable {
Mike Lockwood8182e722010-12-30 15:38:45 -050069 try {
70 native_close();
71 } finally {
72 super.finalize();
73 }
74 }
75
Mike Lockwood540380f2011-02-09 21:48:53 -050076 /**
77 * Returns the name of the USB device
78 *
79 * @return the device name
80 */
Mike Lockwood8182e722010-12-30 15:38:45 -050081 public String getDeviceName() {
82 return mDevice.getDeviceName();
83 }
84
Mike Lockwood540380f2011-02-09 21:48:53 -050085 /**
86 * Returns the ID of the USB device
87 *
88 * @return the device ID
89 */
Mike Lockwood8182e722010-12-30 15:38:45 -050090 public int getDeviceId() {
91 return mDevice.getDeviceId();
92 }
93
94 @Override
95 public String toString() {
96 return mDevice.getDeviceName();
97 }
98
Mike Lockwood540380f2011-02-09 21:48:53 -050099 /**
100 * Returns the {@link android.mtp.MtpDeviceInfo} for this device
101 *
102 * @return the device info
103 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500104 public MtpDeviceInfo getDeviceInfo() {
105 return native_get_device_info();
106 }
107
Mike Lockwood540380f2011-02-09 21:48:53 -0500108 /**
109 * Returns the list of IDs for all storage units on this device
110 *
111 * @return the storage IDs
112 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500113 public int[] getStorageIds() {
114 return native_get_storage_ids();
115 }
116
Mike Lockwood540380f2011-02-09 21:48:53 -0500117 /**
118 * Returns the list of object handles for all objects on the given storage unit,
119 * with the given format and parent.
120 *
121 * @param storageId the storage unit to query
122 * @param format the format of the object to return, or zero for all formats
123 * @param objectHandle the parent object to query, or zero for the storage root
124 * @return the object handles
125 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500126 public int[] getObjectHandles(int storageId, int format, int objectHandle) {
127 return native_get_object_handles(storageId, format, objectHandle);
128 }
129
Mike Lockwood540380f2011-02-09 21:48:53 -0500130 /**
131 * Returns the data for an object as a byte array.
132 *
133 * @param objectHandle handle of the object to read
134 * @param objectSize the size of the object (this should match
135 * {@link android.mtp.MtpObjectInfo#getCompressedSize}
136 * @return the object's data, or null if reading fails
137 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500138 public byte[] getObject(int objectHandle, int objectSize) {
139 return native_get_object(objectHandle, objectSize);
140 }
141
Mike Lockwood540380f2011-02-09 21:48:53 -0500142 /**
143 * Returns the thumbnail data for an object as a byte array.
144 *
145 * @param objectHandle handle of the object to read
146 * @return the object's thumbnail, or null if reading fails
147 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500148 public byte[] getThumbnail(int objectHandle) {
149 return native_get_thumbnail(objectHandle);
150 }
151
Mike Lockwood540380f2011-02-09 21:48:53 -0500152 /**
153 * Retrieves the {@link android.mtp.MtpStorageInfo} for a storage unit.
154 *
155 * @param storageId the ID of the storage unit
156 * @return the MtpStorageInfo
157 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500158 public MtpStorageInfo getStorageInfo(int storageId) {
159 return native_get_storage_info(storageId);
160 }
161
Mike Lockwood540380f2011-02-09 21:48:53 -0500162 /**
163 * Retrieves the {@link android.mtp.MtpObjectInfo} for an object.
164 *
165 * @param objectHandle the handle of the object
166 * @return the MtpObjectInfo
167 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500168 public MtpObjectInfo getObjectInfo(int objectHandle) {
169 return native_get_object_info(objectHandle);
170 }
171
Mike Lockwood540380f2011-02-09 21:48:53 -0500172 /**
173 * Deletes an object on the device.
174 *
175 * @param objectHandle handle of the object to delete
176 * @return true if the deletion succeeds
177 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500178 public boolean deleteObject(int objectHandle) {
179 return native_delete_object(objectHandle);
180 }
181
Mike Lockwood540380f2011-02-09 21:48:53 -0500182 /**
183 * Retrieves the object handle for the parent of an object on the device.
184 *
185 * @param objectHandle handle of the object to query
186 * @return the parent's handle, or zero if it is in the root of the storage
187 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500188 public long getParent(int objectHandle) {
189 return native_get_parent(objectHandle);
190 }
191
Mike Lockwood540380f2011-02-09 21:48:53 -0500192 /**
193 * Retrieves the ID of the storage unit containing the given object on the device.
194 *
195 * @param objectHandle handle of the object to query
196 * @return the object's storage unit ID
197 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500198 public long getStorageID(int objectHandle) {
199 return native_get_storage_id(objectHandle);
200 }
201
Mike Lockwood540380f2011-02-09 21:48:53 -0500202 /**
203 * Copies the data for an object to a file in external storage.
204 *
205 * @param objectHandle handle of the object to read
206 * @param destPath path to destination for the file transfer.
207 * This path should be in the external storage as defined by
208 * {@link android.os.Environment#getExternalStorageDirectory}
209 * @return true if the file transfer succeeds
210 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500211 public boolean importFile(int objectHandle, String destPath) {
212 return native_import_file(objectHandle, destPath);
213 }
214
215 // used by the JNI code
216 private int mNativeContext;
217
218 private native boolean native_open(String deviceName, int fd);
219 private native void native_close();
220 private native MtpDeviceInfo native_get_device_info();
221 private native int[] native_get_storage_ids();
222 private native MtpStorageInfo native_get_storage_info(int storageId);
223 private native int[] native_get_object_handles(int storageId, int format, int objectHandle);
224 private native MtpObjectInfo native_get_object_info(int objectHandle);
225 private native byte[] native_get_object(int objectHandle, int objectSize);
226 private native byte[] native_get_thumbnail(int objectHandle);
227 private native boolean native_delete_object(int objectHandle);
228 private native long native_get_parent(int objectHandle);
229 private native long native_get_storage_id(int objectHandle);
230 private native boolean native_import_file(int objectHandle, String destPath);
231}