Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 com.android.mtp; |
| 18 | |
| 19 | import android.content.Context; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 20 | import android.hardware.usb.UsbConstants; |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 21 | import android.hardware.usb.UsbDevice; |
| 22 | import android.hardware.usb.UsbDeviceConnection; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 23 | import android.hardware.usb.UsbInterface; |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 24 | import android.hardware.usb.UsbManager; |
Tomasz Mikolajewski | df54417 | 2015-08-31 10:59:43 +0900 | [diff] [blame] | 25 | import android.mtp.MtpConstants; |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 26 | import android.mtp.MtpDevice; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 27 | import android.mtp.MtpDeviceInfo; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 28 | import android.mtp.MtpEvent; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 29 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 30 | import android.os.CancellationSignal; |
Tomasz Mikolajewski | 52652ac | 2015-08-05 17:33:33 +0900 | [diff] [blame] | 31 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 32 | import android.util.Log; |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 33 | import android.util.SparseArray; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 34 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 35 | import com.android.internal.annotations.VisibleForTesting; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 36 | import com.android.mtp.exceptions.BusyDeviceException; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 37 | |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 38 | import java.io.FileNotFoundException; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 39 | import java.io.IOException; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 40 | import java.util.ArrayList; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * The model wrapping android.mtp API. |
| 44 | */ |
| 45 | class MtpManager { |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 46 | final static int OBJECT_HANDLE_ROOT_CHILDREN = -1; |
| 47 | |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 48 | /** |
| 49 | * Subclass for PTP. |
| 50 | */ |
| 51 | private static final int SUBCLASS_STILL_IMAGE_CAPTURE = 1; |
| 52 | |
| 53 | /** |
| 54 | * Subclass for Android style MTP. |
| 55 | */ |
| 56 | private static final int SUBCLASS_MTP = 0xff; |
| 57 | |
| 58 | /** |
| 59 | * Protocol for Picture Transfer Protocol (PIMA 15470). |
| 60 | */ |
| 61 | private static final int PROTOCOL_PICTURE_TRANSFER = 1; |
| 62 | |
| 63 | /** |
| 64 | * Protocol for Android style MTP. |
| 65 | */ |
| 66 | private static final int PROTOCOL_MTP = 0; |
| 67 | |
| 68 | |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 69 | private final UsbManager mManager; |
| 70 | // TODO: Save and restore the set of opened device. |
Daichi Hirono | 2ff024f | 2015-08-11 20:02:58 +0900 | [diff] [blame] | 71 | private final SparseArray<MtpDevice> mDevices = new SparseArray<>(); |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 72 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 73 | MtpManager(Context context) { |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 74 | mManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 77 | synchronized void openDevice(int deviceId) throws IOException { |
| 78 | UsbDevice rawDevice = null; |
| 79 | for (final UsbDevice candidate : mManager.getDeviceList().values()) { |
| 80 | if (candidate.getDeviceId() == deviceId) { |
| 81 | rawDevice = candidate; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (rawDevice == null) { |
| 87 | throw new IOException("Not found USB device: " + deviceId); |
| 88 | } |
| 89 | |
| 90 | if (!mManager.hasPermission(rawDevice)) { |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 91 | mManager.grantPermission(rawDevice); |
| 92 | if (!mManager.hasPermission(rawDevice)) { |
| 93 | throw new IOException("Failed to grant a device permission."); |
| 94 | } |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | final MtpDevice device = new MtpDevice(rawDevice); |
| 98 | |
| 99 | final UsbDeviceConnection connection = mManager.openDevice(rawDevice); |
| 100 | if (connection == null) { |
| 101 | throw new IOException("Failed to open a USB connection."); |
| 102 | } |
| 103 | |
| 104 | if (!device.open(connection)) { |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 105 | // We cannot open connection when another application use the device. |
| 106 | throw new BusyDeviceException(); |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Handle devices that fail to obtain storages just after opening a MTP session. |
| 110 | final int[] storageIds = device.getStorageIds(); |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 111 | if (storageIds == null) { |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 112 | throw new IOException("Not found MTP storages in the device."); |
| 113 | } |
| 114 | |
| 115 | mDevices.put(deviceId, device); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 118 | synchronized void closeDevice(int deviceId) throws IOException { |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 119 | getDevice(deviceId).close(); |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 120 | mDevices.remove(deviceId); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 121 | } |
| 122 | |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 123 | synchronized MtpDeviceRecord[] getDevices() { |
| 124 | final ArrayList<MtpDeviceRecord> devices = new ArrayList<>(); |
| 125 | for (UsbDevice device : mManager.getDeviceList().values()) { |
| 126 | if (!isMtpDevice(device)) { |
| 127 | continue; |
| 128 | } |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 129 | final MtpDevice mtpDevice = mDevices.get(device.getDeviceId()); |
| 130 | final boolean opened = mtpDevice != null; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 131 | final String name = device.getProductName(); |
| 132 | MtpRoot[] roots; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 133 | int[] operationsSupported = null; |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 134 | int[] eventsSupported = null; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 135 | if (opened) { |
| 136 | try { |
| 137 | roots = getRoots(device.getDeviceId()); |
| 138 | } catch (IOException exp) { |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 139 | Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exp); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 140 | // If we failed to fetch roots for the device, we still returns device model |
| 141 | // with an empty set of roots so that the device is shown DocumentsUI as long as |
| 142 | // the device is physically connected. |
| 143 | roots = new MtpRoot[0]; |
| 144 | } |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 145 | final MtpDeviceInfo info = mtpDevice.getDeviceInfo(); |
| 146 | if (info != null) { |
| 147 | operationsSupported = mtpDevice.getDeviceInfo().getOperationsSupported(); |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 148 | eventsSupported = mtpDevice.getDeviceInfo().getEventsSupported(); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 149 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 150 | } else { |
| 151 | roots = new MtpRoot[0]; |
| 152 | } |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 153 | devices.add(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 154 | device.getDeviceId(), name, device.getSerialNumber(), opened, roots, |
| 155 | operationsSupported, eventsSupported)); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 156 | } |
| 157 | return devices.toArray(new MtpDeviceRecord[devices.size()]); |
| 158 | } |
| 159 | |
Daichi Hirono | 21c20ad | 2015-07-23 16:56:19 +0900 | [diff] [blame] | 160 | synchronized int[] getOpenedDeviceIds() { |
| 161 | final int[] result = new int[mDevices.size()]; |
| 162 | for (int i = 0; i < result.length; i++) { |
| 163 | result[i] = mDevices.keyAt(i); |
| 164 | } |
| 165 | return result; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 166 | } |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 167 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 168 | MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 169 | throws IOException { |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 170 | final MtpDevice device = getDevice(deviceId); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 171 | synchronized (device) { |
| 172 | return device.getObjectInfo(objectHandle); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 176 | int[] getObjectHandles(int deviceId, int storageId, int parentObjectHandle) |
| 177 | throws IOException { |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 178 | final MtpDevice device = getDevice(deviceId); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 179 | synchronized (device) { |
| 180 | return device.getObjectHandles(storageId, 0 /* all format */, parentObjectHandle); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 181 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | byte[] getObject(int deviceId, int objectHandle, int expectedSize) |
| 185 | throws IOException { |
| 186 | final MtpDevice device = getDevice(deviceId); |
| 187 | synchronized (device) { |
| 188 | return device.getObject(objectHandle, expectedSize); |
| 189 | } |
| 190 | } |
| 191 | |
Daichi Hirono | d426ed2 | 2016-01-11 17:14:41 +0900 | [diff] [blame] | 192 | @VisibleForTesting |
| 193 | long getPartialObject(int deviceId, int objectHandle, long offset, long size, byte[] buffer) |
| 194 | throws IOException { |
| 195 | final MtpDevice device = getDevice(deviceId); |
| 196 | synchronized (device) { |
| 197 | return device.getPartialObject(objectHandle, offset, size, buffer); |
| 198 | } |
| 199 | } |
| 200 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 201 | byte[] getThumbnail(int deviceId, int objectHandle) throws IOException { |
| 202 | final MtpDevice device = getDevice(deviceId); |
| 203 | synchronized (device) { |
| 204 | return device.getThumbnail(objectHandle); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void deleteDocument(int deviceId, int objectHandle) throws IOException { |
| 209 | final MtpDevice device = getDevice(deviceId); |
| 210 | synchronized (device) { |
| 211 | if (!device.deleteObject(objectHandle)) { |
| 212 | throw new IOException("Failed to delete document"); |
Tomasz Mikolajewski | df54417 | 2015-08-31 10:59:43 +0900 | [diff] [blame] | 213 | } |
| 214 | } |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 217 | int createDocument(int deviceId, MtpObjectInfo objectInfo, |
| 218 | ParcelFileDescriptor source) throws IOException { |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 219 | final MtpDevice device = getDevice(deviceId); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 220 | synchronized (device) { |
| 221 | final MtpObjectInfo sendObjectInfoResult = device.sendObjectInfo(objectInfo); |
| 222 | if (sendObjectInfoResult == null) { |
| 223 | throw new IOException("Failed to create a document"); |
| 224 | } |
| 225 | if (objectInfo.getFormat() != MtpConstants.FORMAT_ASSOCIATION) { |
| 226 | if (!device.sendObject(sendObjectInfoResult.getObjectHandle(), |
| 227 | sendObjectInfoResult.getCompressedSize(), source)) { |
| 228 | throw new IOException("Failed to send contents of a document"); |
| 229 | } |
| 230 | } |
| 231 | return sendObjectInfoResult.getObjectHandle(); |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 232 | } |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 233 | } |
| 234 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 235 | int getParent(int deviceId, int objectHandle) throws IOException { |
| 236 | final MtpDevice device = getDevice(deviceId); |
| 237 | synchronized (device) { |
| 238 | final int result = (int) device.getParent(objectHandle); |
| 239 | if (result < 0) { |
| 240 | throw new FileNotFoundException("Not found parent object"); |
| 241 | } |
| 242 | return result; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void importFile(int deviceId, int objectHandle, ParcelFileDescriptor target) |
Tomasz Mikolajewski | 52652ac | 2015-08-05 17:33:33 +0900 | [diff] [blame] | 247 | throws IOException { |
Daichi Hirono | 2ff024f | 2015-08-11 20:02:58 +0900 | [diff] [blame] | 248 | final MtpDevice device = getDevice(deviceId); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 249 | synchronized (device) { |
| 250 | device.importFile(objectHandle, target); |
| 251 | } |
Tomasz Mikolajewski | 52652ac | 2015-08-05 17:33:33 +0900 | [diff] [blame] | 252 | } |
| 253 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 254 | @VisibleForTesting |
| 255 | MtpEvent readEvent(int deviceId, CancellationSignal signal) throws IOException { |
| 256 | final MtpDevice device = getDevice(deviceId); |
| 257 | return device.readEvent(signal); |
| 258 | } |
| 259 | |
| 260 | private synchronized MtpDevice getDevice(int deviceId) throws IOException { |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 261 | final MtpDevice device = mDevices.get(deviceId); |
| 262 | if (device == null) { |
| 263 | throw new IOException("USB device " + deviceId + " is not opened."); |
| 264 | } |
| 265 | return device; |
| 266 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 267 | |
| 268 | private MtpRoot[] getRoots(int deviceId) throws IOException { |
| 269 | final MtpDevice device = getDevice(deviceId); |
| 270 | synchronized (device) { |
| 271 | final int[] storageIds = device.getStorageIds(); |
| 272 | if (storageIds == null) { |
| 273 | throw new IOException("Failed to obtain storage IDs."); |
| 274 | } |
| 275 | final MtpRoot[] results = new MtpRoot[storageIds.length]; |
| 276 | for (int i = 0; i < storageIds.length; i++) { |
| 277 | results[i] = new MtpRoot( |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 278 | device.getDeviceId(), device.getStorageInfo(storageIds[i])); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 279 | } |
| 280 | return results; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static boolean isMtpDevice(UsbDevice device) { |
| 285 | for (int i = 0; i < device.getInterfaceCount(); i++) { |
| 286 | final UsbInterface usbInterface = device.getInterface(i); |
| 287 | if ((usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_STILL_IMAGE && |
| 288 | usbInterface.getInterfaceSubclass() == SUBCLASS_STILL_IMAGE_CAPTURE && |
| 289 | usbInterface.getInterfaceProtocol() == PROTOCOL_PICTURE_TRANSFER)) { |
| 290 | return true; |
| 291 | } |
| 292 | if (usbInterface.getInterfaceClass() == UsbConstants.USB_SUBCLASS_VENDOR_SPEC && |
| 293 | usbInterface.getInterfaceSubclass() == SUBCLASS_MTP && |
| 294 | usbInterface.getInterfaceProtocol() == PROTOCOL_MTP && |
| 295 | usbInterface.getName().equals("MTP")) { |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | return false; |
| 300 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 301 | } |