Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -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 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 17 | package android.hardware.usb; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 18 | |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 21 | import android.app.ActivityThread; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 27 | import com.android.internal.util.Preconditions; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 28 | |
Daulet Zhanguzin | 0eb0a77 | 2019-12-30 17:13:24 +0000 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 31 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 32 | * This class represents a USB device attached to the android device with the android device |
| 33 | * acting as the USB host. |
| 34 | * Each device contains one or more {@link UsbInterface}s, each of which contains a number of |
| 35 | * {@link UsbEndpoint}s (the channels via which data is transmitted over USB). |
| 36 | * |
| 37 | * <p> This class contains information (along with {@link UsbInterface} and {@link UsbEndpoint}) |
| 38 | * that describes the capabilities of the USB device. |
| 39 | * To communicate with the device, you open a {@link UsbDeviceConnection} for the device |
| 40 | * and use {@link UsbRequest} to send and receive data on an endpoint. |
| 41 | * {@link UsbDeviceConnection#controlTransfer} is used for control requests on endpoint zero. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 42 | * |
| 43 | * <div class="special reference"> |
| 44 | * <h3>Developer Guides</h3> |
| 45 | * <p>For more information about communicating with USB hardware, read the |
Dave Friedman | df0fd34d | 2017-04-10 14:50:24 -0700 | [diff] [blame] | 46 | * <a href="{@docRoot}guide/topics/connectivity/usb/index.html">USB</a> developer guide.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 47 | * </div> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 48 | */ |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 49 | public class UsbDevice implements Parcelable { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 50 | |
| 51 | private static final String TAG = "UsbDevice"; |
Paul McLean | 0a8f069 | 2014-12-22 14:57:45 -0700 | [diff] [blame] | 52 | private static final boolean DEBUG = false; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 53 | |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 54 | private final @NonNull String mName; |
| 55 | private final @Nullable String mManufacturerName; |
| 56 | private final @Nullable String mProductName; |
| 57 | private final @NonNull String mVersion; |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 58 | private final @NonNull UsbConfiguration[] mConfigurations; |
| 59 | private final @NonNull IUsbSerialReader mSerialNumberReader; |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 60 | private final int mVendorId; |
| 61 | private final int mProductId; |
| 62 | private final int mClass; |
| 63 | private final int mSubclass; |
| 64 | private final int mProtocol; |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 65 | private final boolean mHasAudioPlayback; |
| 66 | private final boolean mHasAudioCapture; |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 67 | private final boolean mHasMidi; |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 68 | private final boolean mHasVideoPlayback; |
| 69 | private final boolean mHasVideoCapture; |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 70 | |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 71 | /** All interfaces on the device. Initialized on first call to getInterfaceList */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 72 | @UnsupportedAppUsage |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 73 | private @Nullable UsbInterface[] mInterfaces; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 74 | |
| 75 | /** |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 76 | * Create a new UsbDevice object. Only called by {@link Builder#build(IUsbSerialReader)} |
| 77 | * |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 78 | * @hide |
| 79 | */ |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 80 | private UsbDevice(@NonNull String name, int vendorId, int productId, int Class, int subClass, |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 81 | int protocol, @Nullable String manufacturerName, @Nullable String productName, |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 82 | @NonNull String version, @NonNull UsbConfiguration[] configurations, |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 83 | @NonNull IUsbSerialReader serialNumberReader, |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 84 | boolean hasAudioPlayback, boolean hasAudioCapture, boolean hasMidi, |
| 85 | boolean hasVideoPlayback, boolean hasVideoCapture) { |
Daulet Zhanguzin | fcc8d86 | 2019-12-18 14:42:04 +0000 | [diff] [blame] | 86 | mName = Objects.requireNonNull(name); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 87 | mVendorId = vendorId; |
| 88 | mProductId = productId; |
| 89 | mClass = Class; |
| 90 | mSubclass = subClass; |
| 91 | mProtocol = protocol; |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 92 | mManufacturerName = manufacturerName; |
| 93 | mProductName = productName; |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 94 | mVersion = Preconditions.checkStringNotEmpty(version); |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 95 | mConfigurations = Preconditions.checkArrayElementsNotNull(configurations, "configurations"); |
Daulet Zhanguzin | fcc8d86 | 2019-12-18 14:42:04 +0000 | [diff] [blame] | 96 | mSerialNumberReader = Objects.requireNonNull(serialNumberReader); |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 97 | mHasAudioPlayback = hasAudioPlayback; |
| 98 | mHasAudioCapture = hasAudioCapture; |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 99 | mHasMidi = hasMidi; |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 100 | mHasVideoPlayback = hasVideoPlayback; |
| 101 | mHasVideoCapture = hasVideoCapture; |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 102 | |
| 103 | // Make sure the binder belongs to the system |
| 104 | if (ActivityThread.isSystem()) { |
| 105 | Preconditions.checkArgument(mSerialNumberReader instanceof IUsbSerialReader.Stub); |
| 106 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the name of the device. |
| 111 | * In the standard implementation, this is the path of the device file |
| 112 | * for the device in the usbfs file system. |
| 113 | * |
| 114 | * @return the device name |
| 115 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 116 | public @NonNull String getDeviceName() { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 117 | return mName; |
| 118 | } |
| 119 | |
| 120 | /** |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 121 | * Returns the manufacturer name of the device. |
| 122 | * |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 123 | * @return the manufacturer name, or {@code null} if the property could not be read |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 124 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 125 | public @Nullable String getManufacturerName() { |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 126 | return mManufacturerName; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the product name of the device. |
| 131 | * |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 132 | * @return the product name, or {@code null} if the property could not be read |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 133 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 134 | public @Nullable String getProductName() { |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 135 | return mProductName; |
| 136 | } |
| 137 | |
| 138 | /** |
Mike Lockwood | c9bb40e | 2015-04-29 13:05:55 -0700 | [diff] [blame] | 139 | * Returns the version number of the device. |
| 140 | * |
| 141 | * @return the device version |
| 142 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 143 | public @NonNull String getVersion() { |
Mike Lockwood | c9bb40e | 2015-04-29 13:05:55 -0700 | [diff] [blame] | 144 | return mVersion; |
| 145 | } |
| 146 | |
| 147 | /** |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 148 | * Returns the serial number of the device. |
| 149 | * |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 150 | * @return the serial number name, or {@code null} if the property could not be read |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 151 | * |
| 152 | * @throws SecurityException if the app targets SDK >= {@value android.os.Build.VERSION_CODES#Q} |
| 153 | * and the app does not have permission to read from the device. |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 154 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 155 | public @Nullable String getSerialNumber() { |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 156 | try { |
| 157 | return mSerialNumberReader.getSerial(ActivityThread.currentPackageName()); |
| 158 | } catch (RemoteException e) { |
| 159 | e.rethrowFromSystemServer(); |
| 160 | return null; |
| 161 | } |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 165 | * Returns a unique integer ID for the device. |
| 166 | * This is a convenience for clients that want to use an integer to represent |
| 167 | * the device, rather than the device name. |
| 168 | * IDs are not persistent across USB disconnects. |
| 169 | * |
| 170 | * @return the device ID |
| 171 | */ |
| 172 | public int getDeviceId() { |
| 173 | return getDeviceId(mName); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns a vendor ID for the device. |
| 178 | * |
| 179 | * @return the device vendor ID |
| 180 | */ |
| 181 | public int getVendorId() { |
| 182 | return mVendorId; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns a product ID for the device. |
| 187 | * |
| 188 | * @return the device product ID |
| 189 | */ |
| 190 | public int getProductId() { |
| 191 | return mProductId; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the devices's class field. |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 196 | * Some useful constants for USB device classes can be found in {@link UsbConstants}. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 197 | * |
| 198 | * @return the devices's class |
| 199 | */ |
| 200 | public int getDeviceClass() { |
| 201 | return mClass; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns the device's subclass field. |
| 206 | * |
| 207 | * @return the device's subclass |
| 208 | */ |
| 209 | public int getDeviceSubclass() { |
| 210 | return mSubclass; |
| 211 | } |
| 212 | |
| 213 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 214 | * Returns the device's protocol field. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 215 | * |
| 216 | * @return the device's protocol |
| 217 | */ |
| 218 | public int getDeviceProtocol() { |
| 219 | return mProtocol; |
| 220 | } |
| 221 | |
| 222 | /** |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 223 | * Returns the number of {@link UsbConfiguration}s this device contains. |
| 224 | * |
| 225 | * @return the number of configurations |
| 226 | */ |
| 227 | public int getConfigurationCount() { |
| 228 | return mConfigurations.length; |
| 229 | } |
| 230 | |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 231 | /** @hide */ |
| 232 | public boolean getHasAudioPlayback() { |
| 233 | return mHasAudioPlayback; |
| 234 | } |
| 235 | |
| 236 | /** @hide */ |
| 237 | public boolean getHasAudioCapture() { |
| 238 | return mHasAudioCapture; |
| 239 | } |
| 240 | |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 241 | /** @hide */ |
| 242 | public boolean getHasMidi() { |
| 243 | return mHasMidi; |
| 244 | } |
| 245 | |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 246 | /** @hide */ |
| 247 | public boolean getHasVideoPlayback() { |
| 248 | return mHasVideoPlayback; |
| 249 | } |
| 250 | |
| 251 | /** @hide */ |
| 252 | public boolean getHasVideoCapture() { |
| 253 | return mHasVideoCapture; |
| 254 | } |
| 255 | |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Returns the {@link UsbConfiguration} at the given index. |
| 258 | * |
| 259 | * @return the configuration |
| 260 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 261 | public @NonNull UsbConfiguration getConfiguration(int index) { |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 262 | return mConfigurations[index]; |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 265 | private @Nullable UsbInterface[] getInterfaceList() { |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 266 | if (mInterfaces == null) { |
| 267 | int configurationCount = mConfigurations.length; |
| 268 | int interfaceCount = 0; |
| 269 | for (int i = 0; i < configurationCount; i++) { |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 270 | UsbConfiguration configuration = mConfigurations[i]; |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 271 | interfaceCount += configuration.getInterfaceCount(); |
| 272 | } |
| 273 | |
| 274 | mInterfaces = new UsbInterface[interfaceCount]; |
| 275 | int offset = 0; |
| 276 | for (int i = 0; i < configurationCount; i++) { |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 277 | UsbConfiguration configuration = mConfigurations[i]; |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 278 | interfaceCount = configuration.getInterfaceCount(); |
| 279 | for (int j = 0; j < interfaceCount; j++) { |
| 280 | mInterfaces[offset++] = configuration.getInterface(j); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return mInterfaces; |
| 286 | } |
| 287 | |
| 288 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 289 | * Returns the number of {@link UsbInterface}s this device contains. |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 290 | * For devices with multiple configurations, you will probably want to use |
| 291 | * {@link UsbConfiguration#getInterfaceCount} instead. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 292 | * |
| 293 | * @return the number of interfaces |
| 294 | */ |
| 295 | public int getInterfaceCount() { |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 296 | return getInterfaceList().length; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 300 | * Returns the {@link UsbInterface} at the given index. |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 301 | * For devices with multiple configurations, you will probably want to use |
| 302 | * {@link UsbConfiguration#getInterface} instead. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 303 | * |
| 304 | * @return the interface |
| 305 | */ |
Philip P. Moltmann | f2d83ed | 2016-10-17 12:14:41 -0700 | [diff] [blame] | 306 | public @NonNull UsbInterface getInterface(int index) { |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 307 | return getInterfaceList()[index]; |
| 308 | } |
| 309 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 310 | @Override |
| 311 | public boolean equals(Object o) { |
| 312 | if (o instanceof UsbDevice) { |
| 313 | return ((UsbDevice)o).mName.equals(mName); |
| 314 | } else if (o instanceof String) { |
| 315 | return ((String)o).equals(mName); |
| 316 | } else { |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | @Override |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 322 | public int hashCode() { |
| 323 | return mName.hashCode(); |
| 324 | } |
| 325 | |
| 326 | @Override |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 327 | public String toString() { |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 328 | StringBuilder builder = new StringBuilder("UsbDevice[mName=" + mName |
| 329 | + ",mVendorId=" + mVendorId + ",mProductId=" + mProductId |
| 330 | + ",mClass=" + mClass + ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol |
| 331 | + ",mManufacturerName=" + mManufacturerName + ",mProductName=" + mProductName |
| 332 | + ",mVersion=" + mVersion + ",mSerialNumberReader=" + mSerialNumberReader |
| 333 | + ", mHasAudioPlayback=" + mHasAudioPlayback |
| 334 | + ", mHasAudioCapture=" + mHasAudioCapture |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 335 | + ", mHasMidi=" + mHasMidi |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 336 | + ", mHasVideoCapture=" + mHasVideoCapture |
| 337 | + ", mHasVideoPlayback=" + mHasVideoPlayback |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 338 | + ", mConfigurations=["); |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 339 | for (int i = 0; i < mConfigurations.length; i++) { |
| 340 | builder.append("\n"); |
| 341 | builder.append(mConfigurations[i].toString()); |
| 342 | } |
| 343 | builder.append("]"); |
| 344 | return builder.toString(); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 347 | public static final @android.annotation.NonNull Parcelable.Creator<UsbDevice> CREATOR = |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 348 | new Parcelable.Creator<UsbDevice>() { |
| 349 | public UsbDevice createFromParcel(Parcel in) { |
| 350 | String name = in.readString(); |
| 351 | int vendorId = in.readInt(); |
| 352 | int productId = in.readInt(); |
| 353 | int clasz = in.readInt(); |
| 354 | int subClass = in.readInt(); |
| 355 | int protocol = in.readInt(); |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 356 | String manufacturerName = in.readString(); |
| 357 | String productName = in.readString(); |
Mike Lockwood | c9bb40e | 2015-04-29 13:05:55 -0700 | [diff] [blame] | 358 | String version = in.readString(); |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 359 | IUsbSerialReader serialNumberReader = |
| 360 | IUsbSerialReader.Stub.asInterface(in.readStrongBinder()); |
| 361 | UsbConfiguration[] configurations = in.readParcelableArray( |
| 362 | UsbConfiguration.class.getClassLoader(), UsbConfiguration.class); |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 363 | // Capabilities |
| 364 | boolean hasAudioPlayback = in.readInt() == 1; |
| 365 | boolean hasAudioCapture = in.readInt() == 1; |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 366 | boolean hasMidi = in.readInt() == 1; |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 367 | boolean hasVideoPlayback = in.readInt() == 1; |
| 368 | boolean hasVideoCapture = in.readInt() == 1; |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 369 | UsbDevice device = new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 370 | manufacturerName, productName, version, configurations, serialNumberReader, |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 371 | hasAudioPlayback, hasAudioCapture, hasMidi, |
| 372 | hasVideoPlayback, hasVideoCapture); |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 373 | |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 374 | return device; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | public UsbDevice[] newArray(int size) { |
| 378 | return new UsbDevice[size]; |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | public int describeContents() { |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | public void writeToParcel(Parcel parcel, int flags) { |
| 387 | parcel.writeString(mName); |
| 388 | parcel.writeInt(mVendorId); |
| 389 | parcel.writeInt(mProductId); |
| 390 | parcel.writeInt(mClass); |
| 391 | parcel.writeInt(mSubclass); |
| 392 | parcel.writeInt(mProtocol); |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 393 | parcel.writeString(mManufacturerName); |
| 394 | parcel.writeString(mProductName); |
Mike Lockwood | c9bb40e | 2015-04-29 13:05:55 -0700 | [diff] [blame] | 395 | parcel.writeString(mVersion); |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 396 | parcel.writeStrongBinder(mSerialNumberReader.asBinder()); |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 397 | parcel.writeParcelableArray(mConfigurations, 0); |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 398 | parcel.writeInt(mHasAudioPlayback ? 1 : 0); |
| 399 | parcel.writeInt(mHasAudioCapture ? 1 : 0); |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 400 | parcel.writeInt(mHasMidi ? 1 : 0); |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 401 | parcel.writeInt(mHasVideoPlayback ? 1 : 0); |
| 402 | parcel.writeInt(mHasVideoCapture ? 1 : 0); |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 403 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 404 | |
| 405 | public static int getDeviceId(String name) { |
| 406 | return native_get_device_id(name); |
| 407 | } |
| 408 | |
| 409 | public static String getDeviceName(int id) { |
| 410 | return native_get_device_name(id); |
| 411 | } |
| 412 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 413 | private static native int native_get_device_id(String name); |
| 414 | private static native String native_get_device_name(int id); |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 415 | |
| 416 | /** |
| 417 | * @hide |
| 418 | */ |
| 419 | public static class Builder { |
| 420 | private final @NonNull String mName; |
| 421 | private final int mVendorId; |
| 422 | private final int mProductId; |
| 423 | private final int mClass; |
| 424 | private final int mSubclass; |
| 425 | private final int mProtocol; |
| 426 | private final @Nullable String mManufacturerName; |
| 427 | private final @Nullable String mProductName; |
| 428 | private final @NonNull String mVersion; |
| 429 | private final @NonNull UsbConfiguration[] mConfigurations; |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 430 | private final boolean mHasAudioPlayback; |
| 431 | private final boolean mHasAudioCapture; |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 432 | private final boolean mHasMidi; |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 433 | private final boolean mHasVideoPlayback; |
| 434 | private final boolean mHasVideoCapture; |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 435 | |
| 436 | // Temporary storage for serial number. Serial number reader need to be wrapped in a |
| 437 | // IUsbSerialReader as they might be used as PII. |
| 438 | public final @Nullable String serialNumber; |
| 439 | |
| 440 | public Builder(@NonNull String name, int vendorId, int productId, int Class, int subClass, |
| 441 | int protocol, @Nullable String manufacturerName, @Nullable String productName, |
| 442 | @NonNull String version, @NonNull UsbConfiguration[] configurations, |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 443 | @Nullable String serialNumber, |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 444 | boolean hasAudioPlayback, boolean hasAudioCapture, boolean hasMidi, |
| 445 | boolean hasVideoPlayback, boolean hasVideoCapture) { |
Daulet Zhanguzin | fcc8d86 | 2019-12-18 14:42:04 +0000 | [diff] [blame] | 446 | mName = Objects.requireNonNull(name); |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 447 | mVendorId = vendorId; |
| 448 | mProductId = productId; |
| 449 | mClass = Class; |
| 450 | mSubclass = subClass; |
| 451 | mProtocol = protocol; |
| 452 | mManufacturerName = manufacturerName; |
| 453 | mProductName = productName; |
| 454 | mVersion = Preconditions.checkStringNotEmpty(version); |
| 455 | mConfigurations = configurations; |
| 456 | this.serialNumber = serialNumber; |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 457 | mHasAudioPlayback = hasAudioPlayback; |
| 458 | mHasAudioCapture = hasAudioCapture; |
Paul McLean | 9bd2f89 | 2019-10-04 20:15:33 -0600 | [diff] [blame] | 459 | mHasMidi = hasMidi; |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 460 | mHasVideoPlayback = hasVideoPlayback; |
| 461 | mHasVideoCapture = hasVideoCapture; |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Create a new {@link UsbDevice} |
| 466 | * |
| 467 | * @param serialReader The method to read the serial number. |
| 468 | * |
| 469 | * @return The usb device |
| 470 | */ |
| 471 | public UsbDevice build(@NonNull IUsbSerialReader serialReader) { |
| 472 | return new UsbDevice(mName, mVendorId, mProductId, mClass, mSubclass, mProtocol, |
Paul McLean | d336453 | 2019-09-23 08:28:41 -0600 | [diff] [blame] | 473 | mManufacturerName, mProductName, mVersion, mConfigurations, serialReader, |
Paul McLean | a7e7f94f | 2019-10-08 11:07:29 -0600 | [diff] [blame] | 474 | mHasAudioPlayback, mHasAudioCapture, mHasMidi, |
| 475 | mHasVideoPlayback, mHasVideoCapture); |
Philip P. Moltmann | a2c6eea | 2018-08-24 09:50:40 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 478 | } |