• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.AlertDialog;
20 import android.content.Context;
21 import android.media.AudioDeviceCallback;
22 import android.media.AudioDeviceInfo;
23 import android.media.AudioManager;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.util.Log;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.view.ViewGroup;
30 import android.widget.LinearLayout;
31 
32 import com.android.compatibility.common.util.ResultType;
33 import com.android.compatibility.common.util.ResultUnit;
34 import com.android.cts.verifier.PassFailButtons;
35 import com.android.cts.verifier.R;
36 
37 /**
38  * Audio Frequency Test base activity
39  */
40 public class AudioFrequencyActivity extends PassFailButtons.Activity {
41     private static final String TAG = "AudioFrequencyActivity";
42     private static final boolean DEBUG = true;
43 
44     protected Context mContext;
45     protected AudioManager mAudioManager;
46 
47     protected AudioDeviceInfo mOutputDevInfo;
48     protected AudioDeviceInfo mInputDevInfo;
49 
50     public int mMaxLevel = 0;
51 
52     //
53     // TODO - These should be refactored into a RefMicActivity class
54     // i.e. AudioFrequencyActivity <- RefMicActivity
55     private OnBtnClickListener mBtnClickListener = new OnBtnClickListener();
56 
57     @Override
onCreate(Bundle savedInstanceState)58     protected void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60 
61         mContext = this;
62 
63         mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
64         mAudioManager.registerAudioDeviceCallback(new ConnectListener(), new Handler());
65     }
66 
67     //
68     // Common UI Handling
connectRefMicUI()69     protected void connectRefMicUI() {
70         findViewById(R.id.refmic_tests_yes_btn).setOnClickListener(mBtnClickListener);
71         findViewById(R.id.refmic_tests_no_btn).setOnClickListener(mBtnClickListener);
72         findViewById(R.id.refmic_test_info_btn).setOnClickListener(mBtnClickListener);
73 
74         enableTestUI(false);
75     }
76 
showRefMicInfoDialog()77     private void showRefMicInfoDialog() {
78         new AlertDialog.Builder(this)
79                 .setTitle(R.string.ref_mic_dlg_caption)
80                 .setMessage(R.string.ref_mic_dlg_text)
81                 .setPositiveButton(R.string.audio_general_ok, null)
82                 .show();
83     }
84 
85     private class OnBtnClickListener implements OnClickListener {
86         @Override
onClick(View v)87         public void onClick(View v) {
88             int id = v.getId();
89             if (id == R.id.refmic_tests_yes_btn) {
90                 recordRefMicStatus(true);
91                 enableTestUI(true);
92                 // disable test button so that they will now run the test(s)
93                 getPassButton().setEnabled(false);
94             } else if (id == R.id.refmic_tests_no_btn) {
95                 recordRefMicStatus(false);
96                 enableTestUI(false);
97                 // Allow the user to "pass" the test.
98                 getPassButton().setEnabled(true);
99             } else if (id == R.id.refmic_test_info_btn) {
100                 showRefMicInfoDialog();
101             }
102         }
103     }
104 
recordRefMicStatus(boolean has)105     private void recordRefMicStatus(boolean has) {
106         getReportLog().addValue(
107                 "User reported ref mic availability: ",
108                 has ? 1.0 : 0,
109                 ResultType.NEUTRAL,
110                 ResultUnit.NONE);
111     }
112 
113     //
114     // Overrides
115     //
enableTestUI(boolean enable)116     void enableTestUI(boolean enable) {
117 
118     }
119 
enableLayout(int layoutId, boolean enable)120     void enableLayout(int layoutId, boolean enable) {
121         ViewGroup group = (ViewGroup)findViewById(layoutId);
122         for (int i = 0; i < group.getChildCount(); i++) {
123             group.getChildAt(i).setEnabled(enable);
124         }
125     }
126 
setMaxLevel()127     public void setMaxLevel() {
128         mMaxLevel = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
129         mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(mMaxLevel), 0);
130     }
131 
setMinLevel()132     public void setMinLevel() {
133         mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
134     }
135 
testMaxLevel()136     public void testMaxLevel() {
137         int currentLevel = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
138         Log.i(TAG, String.format("Max level: %d curLevel: %d", mMaxLevel, currentLevel));
139         if (currentLevel != mMaxLevel) {
140             new AlertDialog.Builder(this)
141                 .setTitle(R.string.audio_general_warning)
142                 .setMessage(R.string.audio_general_level_not_max)
143                 .setPositiveButton(R.string.audio_general_ok, null)
144                 .show();
145         }
146     }
147 
getMaxLevelForStream(int streamType)148     public int getMaxLevelForStream(int streamType) {
149         return mAudioManager.getStreamMaxVolume(streamType);
150     }
151 
setLevelForStream(int streamType, int level)152     public void setLevelForStream(int streamType, int level) {
153         try {
154             mAudioManager.setStreamVolume(streamType, level, 0);
155         } catch (Exception e) {
156             Log.e(TAG, "Error setting stream volume: ", e);
157         }
158     }
159 
getLevelForStream(int streamType)160     public int getLevelForStream(int streamType) {
161         return mAudioManager.getStreamVolume(streamType);
162     }
163 
enableUILayout(LinearLayout layout, boolean enable)164     public void enableUILayout(LinearLayout layout, boolean enable) {
165         for (int i = 0; i < layout.getChildCount(); i++) {
166             View view = layout.getChildAt(i);
167             view.setEnabled(enable);
168         }
169     }
170 
scanPeripheralList(AudioDeviceInfo[] devices)171     private void scanPeripheralList(AudioDeviceInfo[] devices) {
172         // Can't just use the first record because then we will only get
173         // Source OR sink, not both even on devices that are both.
174         mOutputDevInfo = null;
175         mInputDevInfo = null;
176 
177         // Any valid peripherals
178         for(AudioDeviceInfo devInfo : devices) {
179             if (devInfo.getType() == AudioDeviceInfo.TYPE_USB_DEVICE ||
180                     devInfo.getType() == AudioDeviceInfo.TYPE_USB_HEADSET) {
181                 if (devInfo.isSink()) {
182                     mOutputDevInfo = devInfo;
183                 }
184                 if (devInfo.isSource()) {
185                     mInputDevInfo = devInfo;
186                 }
187             }
188         }
189 
190     }
191 
192     private class ConnectListener extends AudioDeviceCallback {
ConnectListener()193         /*package*/ ConnectListener() {}
194 
195         //
196         // AudioDevicesManager.OnDeviceConnectionListener
197         //
198         @Override
onAudioDevicesAdded(AudioDeviceInfo[] addedDevices)199         public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
200             // Log.i(TAG, "onAudioDevicesAdded() num:" + addedDevices.length);
201 
202             scanPeripheralList(mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL));
203         }
204 
205         @Override
onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices)206         public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
207             // Log.i(TAG, "onAudioDevicesRemoved() num:" + removedDevices.length);
208 
209             scanPeripheralList(mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL));
210         }
211     }
212 
213 //    abstract public void updateConnectStatus();
214 }
215