Home | History | Annotate | Download | only in usb
      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.google.android.car.kitchensink.setting.usb;
     17 
     18 import android.hardware.usb.UsbConstants;
     19 import android.hardware.usb.UsbDevice;
     20 import android.hardware.usb.UsbInterface;
     21 import android.hardware.usb.UsbManager;
     22 import android.text.TextUtils;
     23 
     24 import java.util.HashMap;
     25 import java.util.LinkedList;
     26 
     27 /**
     28  * Util methods to work with USB devices.
     29  */
     30 class UsbUtil {
     31     public static final String ADB_INTERFACE_NAME = "ADB Interface";
     32     public static final String AOAP_INTERFACE_NAME = "Android Accessory Interface";
     33     public static final String MTP_INTERFACE_NAME = "MTP";
     34 
     35     public static LinkedList<UsbDevice> findAllPossibleAndroidDevices(UsbManager usbManager) {
     36         HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
     37         LinkedList<UsbDevice> androidDevices = null;
     38         for (UsbDevice device : devices.values()) {
     39             if (possiblyAndroid(device)) {
     40                 if (androidDevices == null) {
     41                     androidDevices = new LinkedList<>();
     42                 }
     43                 androidDevices.add(device);
     44             }
     45         }
     46         return androidDevices;
     47     }
     48 
     49     public static boolean possiblyAndroid(UsbDevice device) {
     50         int numInterfaces = device.getInterfaceCount();
     51         for (int i = 0; i < numInterfaces; i++) {
     52             UsbInterface usbInterface = device.getInterface(i);
     53             String interfaceName = usbInterface.getName();
     54             int interfaceClass = usbInterface.getInterfaceClass();
     55             // more thorough check can be added, later
     56             if (AOAP_INTERFACE_NAME.equals(interfaceName)
     57                     || ADB_INTERFACE_NAME.equals(interfaceName)
     58                     || MTP_INTERFACE_NAME.equals(interfaceName)
     59                     || interfaceClass == UsbConstants.USB_CLASS_MASS_STORAGE) {
     60                 return true;
     61             }
     62         }
     63         return false;
     64     }
     65 
     66     public static boolean isTheSameDevice(UsbDevice l, UsbDevice r) {
     67         if (TextUtils.equals(l.getManufacturerName(), r.getManufacturerName())
     68                 && TextUtils.equals(l.getProductName(), r.getProductName())
     69                 && TextUtils.equals(l.getSerialNumber(), r.getSerialNumber())) {
     70             return true;
     71         }
     72         return false;
     73     }
     74 
     75     public static boolean isDevicesMatching(UsbDevice l, UsbDevice r) {
     76         if (l.getVendorId() == r.getVendorId() && l.getProductId() == r.getProductId()
     77                 && TextUtils.equals(l.getSerialNumber(), r.getSerialNumber())) {
     78             return true;
     79         }
     80         return false;
     81     }
     82 
     83     public static boolean isDeviceConnected(UsbManager usbManager, UsbDevice device) {
     84         HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
     85         for (UsbDevice dev : devices.values()) {
     86             if (isDevicesMatching(dev, device)) {
     87                 return true;
     88             }
     89         }
     90         return false;
     91     }
     92 }
     93