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