| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.hardware.usb.externalmanagementtest; |
| 17 |
|
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.LinkedList; |
| 20 |
|
| 21 |
import android.content.Context; |
| 22 |
import android.hardware.usb.UsbDevice; |
| 23 |
import android.hardware.usb.UsbInterface; |
| 24 |
import android.hardware.usb.UsbManager; |
| 25 |
import android.text.TextUtils; |
| 26 |
|
| 27 |
public class UsbUtil { |
| 28 |
public static final String ADB_INTERFACE_NAME = "ADB Interface"; |
| 29 |
public static final String AOAP_INTERFACE_NAME = "Android Accessory Interface"; |
| 30 |
public static final String MTP_INTERFACE_NAME = "MTP"; |
| 31 |
|
| 32 |
public static LinkedList<UsbDevice> findAllPossibleAndroidDevices(UsbManager usbManager) { |
| 33 |
HashMap<String, UsbDevice> devices = usbManager.getDeviceList(); |
| 34 |
LinkedList<UsbDevice> androidDevices = null; |
| 35 |
for (UsbDevice device : devices.values()) { |
| 36 |
if (possiblyAndroid(device)) { |
| 37 |
if (androidDevices == null) { |
| 38 |
androidDevices = new LinkedList<>(); |
| 39 |
} |
| 40 |
androidDevices.add(device); |
| 41 |
} |
| 42 |
} |
| 43 |
return androidDevices; |
| 44 |
} |
| 45 |
|
| 46 |
public static boolean possiblyAndroid(UsbDevice device) { |
| 47 |
int numInterfaces = device.getInterfaceCount(); |
| 48 |
for (int i = 0; i < numInterfaces; i++) { |
| 49 |
UsbInterface usbInterface = device.getInterface(i); |
| 50 |
String interfaceName = usbInterface.getName(); |
| 51 |
|
| 52 |
if (AOAP_INTERFACE_NAME.equals(interfaceName) || |
| 53 |
ADB_INTERFACE_NAME.equals(interfaceName) || |
| 54 |
MTP_INTERFACE_NAME.equals(interfaceName)) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
public static boolean isTheSameDevice(UsbDevice l, UsbDevice r) { |
| 62 |
if (TextUtils.equals(l.getManufacturerName(), r.getManufacturerName()) && |
| 63 |
TextUtils.equals(l.getProductName(), r.getProductName()) && |
| 64 |
TextUtils.equals(l.getSerialNumber(), r.getSerialNumber())) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
public static boolean isDevicesMatching(UsbDevice l, UsbDevice r) { |
| 71 |
if (l.getVendorId() == r.getVendorId() && l.getProductId() == r.getProductId() && |
| 72 |
TextUtils.equals(l.getSerialNumber(), r.getSerialNumber())) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
public static boolean isDeviceConnected(UsbManager usbManager, UsbDevice device) { |
| 79 |
HashMap<String, UsbDevice> devices = usbManager.getDeviceList(); |
| 80 |
for (UsbDevice dev : devices.values()) { |
| 81 |
if (isDevicesMatching(dev, device)) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
} |
| 85 |
return false; |
| 86 |
} |
| 87 |
} |