1 /*
2  * Copyright (C) 2015 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 
17 package com.android.cts.verifier.audio;
18 
19 import android.content.Context;
20 
21 import android.hardware.usb.UsbAccessory;
22 import android.hardware.usb.UsbConstants;
23 import android.hardware.usb.UsbDevice;
24 import android.hardware.usb.UsbInterface;
25 import android.hardware.usb.UsbManager;
26 
27 import java.util.HashMap;
28 import java.util.Iterator;
29 
30 public class UsbMicrophoneTester {
31 
getUSBDeviceListString(Context context)32     public static String getUSBDeviceListString(Context context) {
33         UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
34         StringBuilder sb = new StringBuilder();
35         HashMap<String, UsbDevice> devicelist = manager.getDeviceList();
36         for(UsbDevice usbDevice : devicelist.values()) {
37             sb.append("Model    : " + usbDevice.getDeviceName() + "\n");
38             sb.append(" Id      : " + usbDevice.getDeviceId() + "\n");
39             sb.append(" Class   : " + usbDevice.getDeviceClass() + "\n");
40             sb.append(" Prod.Id : " + usbDevice.getProductId() + "\n");
41             sb.append(" Vendor.Id : " + usbDevice.getVendorId() + "\n");
42             int iCount = usbDevice.getInterfaceCount();
43             for (int i = 0; i < iCount; i++) {
44                 UsbInterface usbInterface = usbDevice.getInterface(i);
45                 sb.append("    Interface " + i + " :\n");
46                 sb.append("     Class: " + usbInterface.getInterfaceClass() + "\n");
47             }
48         }
49         return sb.toString();
50     }
51 
getIsMicrophoneConnected(Context context)52     public static boolean getIsMicrophoneConnected(Context context) {
53         UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
54         StringBuilder sb = new StringBuilder();
55         HashMap<String, UsbDevice> devicelist = manager.getDeviceList();
56         for(UsbDevice usbDevice : devicelist.values()) {
57             int iCount = usbDevice.getInterfaceCount();
58             for (int i = 0; i < iCount; i++) {
59                 UsbInterface usbInterface = usbDevice.getInterface(i);
60                 if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) {
61                     return true;
62                 }
63             }
64         }
65         return false;
66     }
67 }