Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +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 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 19 | import android.hardware.usb.UsbDevice; |
| 20 | import android.hardware.usb.UsbDeviceConnection; |
| 21 | import android.hardware.usb.UsbManager; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 22 | import android.mtp.MtpConstants; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 23 | import android.os.SystemClock; |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 24 | |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 25 | import java.io.FileNotFoundException; |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.HashMap; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 28 | import java.util.Objects; |
| 29 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 30 | /** |
| 31 | * Static utility methods for testing. |
| 32 | */ |
Daichi Hirono | 99b5805 | 2015-12-03 18:09:16 +0900 | [diff] [blame] | 33 | final class TestUtil { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 34 | private TestUtil() {} |
| 35 | |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 36 | static final int[] OPERATIONS_SUPPORTED = new int[] { |
| 37 | MtpConstants.OPERATION_GET_PARTIAL_OBJECT, |
| 38 | MtpConstants.OPERATION_SEND_OBJECT, |
| 39 | MtpConstants.OPERATION_SEND_OBJECT_INFO, |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 40 | MtpConstants.OPERATION_DELETE_OBJECT, |
Daichi Hirono | 64111e0 | 2016-03-24 21:07:38 +0900 | [diff] [blame] | 41 | MtpConstants.OPERATION_GET_OBJECT_PROP_DESC, |
| 42 | MtpConstants.OPERATION_GET_OBJECT_PROP_VALUE |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 43 | }; |
| 44 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 45 | /** |
| 46 | * Requests permission for a MTP device and returns the first MTP device that has at least one |
| 47 | * storage. |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 48 | */ |
| 49 | static UsbDevice setupMtpDevice( |
| 50 | TestResultInstrumentation instrumentation, |
| 51 | UsbManager usbManager, |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 52 | MtpManager manager) { |
| 53 | while (true) { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 54 | try { |
Daichi Hirono | ab03cb1 | 2016-01-11 15:47:21 +0900 | [diff] [blame] | 55 | final UsbDevice device = findMtpDevice(usbManager, manager); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 56 | waitForStorages(instrumentation, manager, device.getDeviceId()); |
| 57 | return device; |
| 58 | } catch (IOException exp) { |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 59 | instrumentation.show(Objects.toString(exp.getMessage())); |
| 60 | SystemClock.sleep(1000); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 61 | // When the MTP device is Android, and it changes the USB device type from |
| 62 | // "Charging" to "MTP", the device ID will be updated. We need to find a device |
| 63 | // again. |
| 64 | continue; |
| 65 | } |
| 66 | } |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 69 | static void addTestDevice(MtpDatabase database) throws FileNotFoundException { |
| 70 | database.getMapper().startAddingDocuments(null); |
| 71 | database.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 72 | 0, "Device", "device_key", /* opened is */ true, new MtpRoot[0], |
| 73 | OPERATIONS_SUPPORTED, null)); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 74 | database.getMapper().stopAddingDocuments(null); |
| 75 | } |
| 76 | |
| 77 | static void addTestStorage(MtpDatabase database, String parentId) throws FileNotFoundException { |
| 78 | database.getMapper().startAddingDocuments(parentId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 79 | database.getMapper().putStorageDocuments(parentId, OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 80 | new MtpRoot(0, 100, "Storage", 1024, 1024, ""), |
| 81 | }); |
| 82 | database.getMapper().stopAddingDocuments(parentId); |
| 83 | } |
| 84 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 85 | private static UsbDevice findMtpDevice( |
Daichi Hirono | af5ea38 | 2016-01-07 18:24:59 +0900 | [diff] [blame] | 86 | UsbManager usbManager, |
Daichi Hirono | ab03cb1 | 2016-01-11 15:47:21 +0900 | [diff] [blame] | 87 | MtpManager manager) throws IOException { |
| 88 | final HashMap<String,UsbDevice> devices = usbManager.getDeviceList(); |
| 89 | if (devices.size() == 0) { |
| 90 | throw new IOException("Device not found."); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 91 | } |
Daichi Hirono | ab03cb1 | 2016-01-11 15:47:21 +0900 | [diff] [blame] | 92 | final UsbDevice device = devices.values().iterator().next(); |
| 93 | // Tries to get ownership of the device in case that another application use it. |
| 94 | if (usbManager.hasPermission(device)) { |
| 95 | final UsbDeviceConnection connection = usbManager.openDevice(device); |
| 96 | for (int i = 0; i < device.getInterfaceCount(); i++) { |
| 97 | // Since the test runs real environment, we need to call claim interface with |
| 98 | // force = true to rob interfaces from other applications. |
| 99 | connection.claimInterface(device.getInterface(i), true); |
| 100 | connection.releaseInterface(device.getInterface(i)); |
| 101 | } |
| 102 | connection.close(); |
| 103 | } |
| 104 | manager.openDevice(device.getDeviceId()); |
| 105 | return device; |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 108 | private static void waitForStorages( |
| 109 | TestResultInstrumentation instrumentation, |
| 110 | MtpManager manager, |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 111 | int deviceId) throws IOException { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 112 | while (true) { |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 113 | MtpDeviceRecord device = null; |
| 114 | for (final MtpDeviceRecord deviceCandidate : manager.getDevices()) { |
| 115 | if (deviceCandidate.deviceId == deviceId) { |
| 116 | device = deviceCandidate; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | if (device == null) { |
| 121 | throw new IOException("Device was detached."); |
| 122 | } |
| 123 | if (device.roots.length == 0) { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 124 | instrumentation.show("Wait for storages."); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 125 | SystemClock.sleep(1000); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | } |