Paul McLean | af80c87 | 2015-06-05 07:48:15 -0700 | [diff] [blame] | 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 com.android.cts.verifier.PassFailButtons; |
| 20 | import com.android.cts.verifier.R; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | |
| 24 | import android.media.AudioDeviceCallback; |
| 25 | import android.media.AudioDeviceInfo; |
| 26 | import android.media.AudioManager; |
| 27 | import android.media.AudioRecord; |
| 28 | import android.media.AudioTrack; |
| 29 | |
| 30 | import android.os.Bundle; |
| 31 | import android.os.Handler; |
| 32 | |
| 33 | import android.util.Log; |
| 34 | |
| 35 | import android.view.View; |
| 36 | import android.view.View.OnClickListener; |
| 37 | |
| 38 | import android.widget.Button; |
| 39 | import android.widget.TextView; |
| 40 | |
| 41 | /** |
| 42 | * Tests AudioTrack and AudioRecord (re)Routing messages. |
| 43 | */ |
| 44 | public class AudioRoutingNotificationsActivity extends PassFailButtons.Activity { |
| 45 | private static final String TAG = "AudioRoutingNotificationsActivity"; |
| 46 | |
| 47 | Context mContext; |
| 48 | |
| 49 | OnBtnClickListener mBtnClickListener = new OnBtnClickListener(); |
| 50 | |
| 51 | int mNumTrackNotifications = 0; |
| 52 | int mNumRecordNotifications = 0; |
| 53 | |
| 54 | TrivialPlayer mAudioPlayer = new TrivialPlayer(); |
| 55 | TrivialRecorder mAudioRecorder = new TrivialRecorder(); |
| 56 | |
| 57 | private class OnBtnClickListener implements OnClickListener { |
| 58 | @Override |
| 59 | public void onClick(View v) { |
| 60 | switch (v.getId()) { |
| 61 | case R.id.audio_routingnotification_playBtn: |
| 62 | Log.i(TAG, "audio_routingnotification_playBtn"); |
| 63 | mAudioPlayer.start(); |
| 64 | break; |
| 65 | |
| 66 | case R.id.audio_routingnotification_playStopBtn: |
| 67 | Log.i(TAG, "audio_routingnotification_playStopBtn"); |
| 68 | mAudioPlayer.stop(); |
| 69 | break; |
| 70 | |
| 71 | case R.id.audio_routingnotification_recordBtn: |
| 72 | break; |
| 73 | |
| 74 | case R.id.audio_routingnotification_recordStopBtn: |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private class AudioTrackRoutingChangeListener implements AudioTrack.OnRoutingChangedListener { |
| 81 | public void onRoutingChanged(AudioTrack audioTrack) { |
| 82 | mNumTrackNotifications++; |
| 83 | TextView textView = |
| 84 | (TextView)findViewById(R.id.audio_routingnotification_audioTrack_change); |
| 85 | String msg = mContext.getResources().getString( |
| 86 | R.string.audio_routingnotification_trackRoutingMsg); |
| 87 | AudioDeviceInfo routedDevice = audioTrack.getRoutedDevice(); |
| 88 | CharSequence deviceName = routedDevice != null ? routedDevice.getProductName() : "none"; |
| 89 | int deviceType = routedDevice != null ? routedDevice.getType() : -1; |
| 90 | textView.setText(msg + " - " + |
| 91 | deviceName + " [0x" + Integer.toHexString(deviceType) + "]" + |
| 92 | " - " + mNumTrackNotifications); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private class AudioRecordRoutingChangeListener implements AudioRecord.OnRoutingChangedListener { |
| 97 | public void onRoutingChanged(AudioRecord audioRecord) { |
| 98 | mNumRecordNotifications++; |
| 99 | TextView textView = |
| 100 | (TextView)findViewById(R.id.audio_routingnotification_audioRecord_change); |
| 101 | String msg = mContext.getResources().getString( |
| 102 | R.string.audio_routingnotification_recordRoutingMsg); |
| 103 | AudioDeviceInfo routedDevice = audioRecord.getRoutedDevice(); |
| 104 | CharSequence deviceName = routedDevice != null ? routedDevice.getProductName() : "none"; |
| 105 | int deviceType = routedDevice != null ? routedDevice.getType() : -1; |
| 106 | textView.setText(msg + " - " + |
| 107 | deviceName + " [0x" + Integer.toHexString(deviceType) + "]" + |
| 108 | " - " + mNumRecordNotifications); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected void onCreate(Bundle savedInstanceState) { |
| 114 | super.onCreate(savedInstanceState); |
| 115 | setContentView(R.layout.audio_routingnotifications_test); |
| 116 | |
| 117 | Button btn; |
| 118 | btn = (Button)findViewById(R.id.audio_routingnotification_playBtn); |
| 119 | btn.setOnClickListener(mBtnClickListener); |
| 120 | btn = (Button)findViewById(R.id.audio_routingnotification_playStopBtn); |
| 121 | btn.setOnClickListener(mBtnClickListener); |
| 122 | btn = (Button)findViewById(R.id.audio_routingnotification_recordBtn); |
| 123 | btn.setOnClickListener(mBtnClickListener); |
| 124 | btn = (Button)findViewById(R.id.audio_routingnotification_recordStopBtn); |
| 125 | btn.setOnClickListener(mBtnClickListener); |
| 126 | |
| 127 | mContext = this; |
| 128 | |
| 129 | AudioTrack audioTrack = mAudioPlayer.getAudioTrack(); |
| 130 | audioTrack.addOnRoutingChangedListener( |
| 131 | new AudioTrackRoutingChangeListener(), new Handler()); |
| 132 | |
| 133 | AudioRecord audioRecord = mAudioRecorder.getAudioRecord(); |
| 134 | audioRecord.addOnRoutingChangedListener( |
| 135 | new AudioRecordRoutingChangeListener(), new Handler()); |
| 136 | |
| 137 | setPassFailButtonClickListeners(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void onBackPressed () { |
| 142 | mAudioPlayer.shutDown(); |
| 143 | mAudioRecorder.shutDown(); |
| 144 | super.onBackPressed(); |
| 145 | } |
| 146 | } |