1 /* 2 * Copyright (C) 2017 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.media.AudioDeviceCallback; 20 import android.media.AudioDeviceInfo; 21 import android.media.AudioManager; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.util.Log; 25 import android.widget.TextView; 26 27 import com.android.cts.verifier.audio.peripheralprofile.PeripheralProfile; 28 import com.android.cts.verifier.audio.peripheralprofile.ProfileManager; 29 30 import com.android.cts.verifier.PassFailButtons; 31 import com.android.cts.verifier.R; // needed to access resource in CTSVerifier project namespace. 32 33 public abstract class USBAudioPeripheralActivity extends PassFailButtons.Activity { 34 private static final String TAG = "USBAudioPeripheralActivity"; 35 private static final boolean DEBUG = false; 36 37 // Profile 38 protected ProfileManager mProfileManager = new ProfileManager(); 39 protected PeripheralProfile mSelectedProfile; 40 41 // Peripheral 42 AudioManager mAudioManager; 43 protected boolean mIsPeripheralAttached; 44 protected AudioDeviceInfo mOutputDevInfo; 45 protected AudioDeviceInfo mInputDevInfo; 46 47 // This will be overriden... 48 protected int mSystemSampleRate = 48000; 49 50 // Widgets 51 private TextView mProfileNameTx; 52 private TextView mProfileDescriptionTx; 53 54 private TextView mPeripheralNameTx; 55 56 public USBAudioPeripheralActivity() { 57 super(); 58 59 mProfileManager.loadProfiles(); 60 } 61 62 @Override 63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 66 mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE); 67 mAudioManager.registerAudioDeviceCallback(new ConnectListener(), new Handler()); 68 69 mSystemSampleRate = Integer.parseInt( 70 mAudioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)); 71 } 72 73 protected void connectPeripheralStatusWidgets() { 74 mProfileNameTx = (TextView)findViewById(R.id.uap_profileNameTx); 75 mProfileDescriptionTx = 76 (TextView)findViewById(R.id.uap_profileDescriptionTx); 77 mPeripheralNameTx = (TextView)findViewById(R.id.uap_peripheralNameTx); 78 } 79 80 private void showProfileStatus() { 81 if (DEBUG) { 82 Log.d(TAG, "showProfileStatus()" + (mSelectedProfile != null)); 83 } 84 if (mSelectedProfile != null) { 85 mProfileNameTx.setText(mSelectedProfile.getName()); 86 mProfileDescriptionTx.setText(mSelectedProfile.getDescription()); 87 } else { 88 mProfileNameTx.setText(""); 89 mProfileDescriptionTx.setText(""); 90 } 91 } 92 93 private void showPeripheralStatus() { 94 if (mIsPeripheralAttached) { 95 String productName = ""; 96 if (mOutputDevInfo != null) { 97 productName = mOutputDevInfo.getProductName().toString(); 98 } else if (mInputDevInfo != null) { 99 productName = mInputDevInfo.getProductName().toString(); 100 } 101 String ctrlText; 102 if (mSelectedProfile == null) { 103 ctrlText = productName + " - UNSUPPORTED"; 104 } else { 105 ctrlText = productName; 106 } 107 mPeripheralNameTx.setText(ctrlText); 108 } else { 109 mPeripheralNameTx.setText("Disconnected"); 110 } 111 } 112 113 private void scanPeripheralList(AudioDeviceInfo[] devices) { 114 // Can't just use the first record because then we will only get 115 // Source OR sink, not both even on devices that are both. 116 mOutputDevInfo = null; 117 mInputDevInfo = null; 118 119 // Any valid peripherals 120 for(AudioDeviceInfo devInfo : devices) { 121 if (devInfo.getType() == AudioDeviceInfo.TYPE_USB_DEVICE || 122 devInfo.getType() == AudioDeviceInfo.TYPE_USB_HEADSET) { 123 if (devInfo.isSink()) { 124 mOutputDevInfo = devInfo; 125 } 126 if (devInfo.isSource()) { 127 mInputDevInfo = devInfo; 128 } 129 } 130 } 131 mIsPeripheralAttached = mOutputDevInfo != null || mInputDevInfo != null; 132 if (DEBUG) { 133 Log.d(TAG, "mIsPeripheralAttached: " + mIsPeripheralAttached); 134 } 135 136 // any associated profiles? 137 if (mIsPeripheralAttached) { 138 if (mOutputDevInfo != null) { 139 mSelectedProfile = 140 mProfileManager.getProfile(mOutputDevInfo.getProductName().toString()); 141 } else if (mInputDevInfo != null) { 142 mSelectedProfile = 143 mProfileManager.getProfile(mInputDevInfo.getProductName().toString()); 144 } 145 } else { 146 mSelectedProfile = null; 147 } 148 149 } 150 151 private class ConnectListener extends AudioDeviceCallback { 152 /*package*/ ConnectListener() {} 153 154 // 155 // AudioDevicesManager.OnDeviceConnectionListener 156 // 157 @Override 158 public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) { 159 // Log.i(TAG, "onAudioDevicesAdded() num:" + addedDevices.length); 160 161 scanPeripheralList(mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL)); 162 163 showProfileStatus(); 164 showPeripheralStatus(); 165 updateConnectStatus(); 166 } 167 168 @Override 169 public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) { 170 // Log.i(TAG, "onAudioDevicesRemoved() num:" + removedDevices.length); 171 172 scanPeripheralList(mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL)); 173 174 showProfileStatus(); 175 showPeripheralStatus(); 176 updateConnectStatus(); 177 } 178 } 179 180 abstract public void updateConnectStatus(); 181 } 182 183