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 android.car.usb.handler; 17 18 import android.hardware.usb.UsbDevice; 19 import android.hardware.usb.UsbDeviceConnection; 20 import android.hardware.usb.UsbManager; 21 import android.text.TextUtils; 22 import java.io.IOException; 23 import java.util.ArrayList; 24 import java.util.HashMap; 25 import java.util.List; 26 27 /** 28 * Util methods to work with USB devices. 29 */ 30 class UsbUtil { 31 public static List<UsbDevice> findAllPossibleAndroidDevices(UsbManager usbManager) { 32 HashMap<String, UsbDevice> devices = usbManager.getDeviceList(); 33 ArrayList<UsbDevice> androidDevices = new ArrayList<>(devices.size()); 34 for (UsbDevice device : devices.values()) { 35 UsbDeviceConnection connection = openConnection(usbManager, device); 36 if (AoapInterface.isSupported(connection)) { 37 androidDevices.add(device); 38 } 39 connection.close(); 40 } 41 return androidDevices; 42 } 43 44 public static UsbDeviceConnection openConnection(UsbManager manager, UsbDevice device) { 45 manager.grantPermission(device); 46 return manager.openDevice(device); 47 } 48 49 public static void sendAoapAccessoryStart(UsbDeviceConnection connection, String manufacturer, 50 String model, String description, String version, String uri, String serial) 51 throws IOException { 52 AoapInterface.sendString(connection, AoapInterface.ACCESSORY_STRING_MANUFACTURER, 53 manufacturer); 54 AoapInterface.sendString(connection, AoapInterface.ACCESSORY_STRING_MODEL, 55 model); 56 AoapInterface.sendString(connection, AoapInterface.ACCESSORY_STRING_DESCRIPTION, 57 description); 58 AoapInterface.sendString(connection, AoapInterface.ACCESSORY_STRING_VERSION, 59 version); 60 AoapInterface.sendString(connection, AoapInterface.ACCESSORY_STRING_URI, uri); 61 AoapInterface.sendString(connection, AoapInterface.ACCESSORY_STRING_SERIAL,serial); 62 AoapInterface.sendAoapStart(connection); 63 } 64 65 public static boolean isTheSameDevice(UsbDevice l, UsbDevice r) { 66 if (TextUtils.equals(l.getManufacturerName(), r.getManufacturerName()) 67 && TextUtils.equals(l.getProductName(), r.getProductName()) 68 && TextUtils.equals(l.getSerialNumber(), r.getSerialNumber())) { 69 return true; 70 } 71 return false; 72 } 73 74 public static boolean isDevicesMatching(UsbDevice l, UsbDevice r) { 75 if (l.getVendorId() == r.getVendorId() && l.getProductId() == r.getProductId() 76 && TextUtils.equals(l.getSerialNumber(), r.getSerialNumber())) { 77 return true; 78 } 79 return false; 80 } 81 82 public static boolean isDeviceConnected(UsbManager usbManager, UsbDevice device) { 83 HashMap<String, UsbDevice> devices = usbManager.getDeviceList(); 84 for (UsbDevice dev : devices.values()) { 85 if (isDevicesMatching(dev, device)) { 86 return true; 87 } 88 } 89 return false; 90 } 91 } 92