| 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 | |
| Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame^] | 19 | import android.hardware.usb.UsbDevice; |
| 20 | import android.hardware.usb.UsbManager; |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 21 | import android.os.ParcelFileDescriptor; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | /** |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 25 | * This class represents an MTP or PTP device connected on the USB host bus. |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 26 | */ |
| 27 | public 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 Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 37 | /** |
| 38 | * MtpClient constructor |
| 39 | * |
| Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame^] | 40 | * @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] | 41 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 42 | public MtpDevice(UsbDevice device) { |
| 43 | mDevice = device; |
| 44 | } |
| 45 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 46 | /** |
| 47 | * Opens the MTP or PTP device and return an {@link android.mtp.MtpDevice} for it. |
| 48 | * |
| Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame^] | 49 | * @param manager reference to {@link android.hardware.usb.UsbManager} |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 50 | * @return true if the device was successfully opened. |
| 51 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 52 | 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 Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 60 | /** |
| 61 | * Closes all resources related to the MtpDevice object |
| 62 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 63 | public void close() { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 64 | native_close(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void finalize() throws Throwable { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 69 | try { |
| 70 | native_close(); |
| 71 | } finally { |
| 72 | super.finalize(); |
| 73 | } |
| 74 | } |
| 75 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 76 | /** |
| 77 | * Returns the name of the USB device |
| 78 | * |
| 79 | * @return the device name |
| 80 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 81 | public String getDeviceName() { |
| 82 | return mDevice.getDeviceName(); |
| 83 | } |
| 84 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 85 | /** |
| 86 | * Returns the ID of the USB device |
| 87 | * |
| 88 | * @return the device ID |
| 89 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 90 | public int getDeviceId() { |
| 91 | return mDevice.getDeviceId(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
| 96 | return mDevice.getDeviceName(); |
| 97 | } |
| 98 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 99 | /** |
| 100 | * Returns the {@link android.mtp.MtpDeviceInfo} for this device |
| 101 | * |
| 102 | * @return the device info |
| 103 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 104 | public MtpDeviceInfo getDeviceInfo() { |
| 105 | return native_get_device_info(); |
| 106 | } |
| 107 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 108 | /** |
| 109 | * Returns the list of IDs for all storage units on this device |
| 110 | * |
| 111 | * @return the storage IDs |
| 112 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 113 | public int[] getStorageIds() { |
| 114 | return native_get_storage_ids(); |
| 115 | } |
| 116 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 117 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 126 | public int[] getObjectHandles(int storageId, int format, int objectHandle) { |
| 127 | return native_get_object_handles(storageId, format, objectHandle); |
| 128 | } |
| 129 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 130 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 138 | public byte[] getObject(int objectHandle, int objectSize) { |
| 139 | return native_get_object(objectHandle, objectSize); |
| 140 | } |
| 141 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 142 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 148 | public byte[] getThumbnail(int objectHandle) { |
| 149 | return native_get_thumbnail(objectHandle); |
| 150 | } |
| 151 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 152 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 158 | public MtpStorageInfo getStorageInfo(int storageId) { |
| 159 | return native_get_storage_info(storageId); |
| 160 | } |
| 161 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 162 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 168 | public MtpObjectInfo getObjectInfo(int objectHandle) { |
| 169 | return native_get_object_info(objectHandle); |
| 170 | } |
| 171 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 172 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 178 | public boolean deleteObject(int objectHandle) { |
| 179 | return native_delete_object(objectHandle); |
| 180 | } |
| 181 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 182 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 188 | public long getParent(int objectHandle) { |
| 189 | return native_get_parent(objectHandle); |
| 190 | } |
| 191 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 192 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 198 | public long getStorageID(int objectHandle) { |
| 199 | return native_get_storage_id(objectHandle); |
| 200 | } |
| 201 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 202 | /** |
| 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 Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 211 | 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 | } |