1 /* 2 * Copyright (C) 2016 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 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 // more thorough check can be added, later 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 } 88