summaryrefslogtreecommitdiff
path: root/apps/CtsVerifier/src/com/android/cts/verifier/audio/AudioOutputRoutingNotificationsActivity.java
blob: e23ee17f25ddc56a63a923eecbaee3266f1e3547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.cts.verifier.audio;

import static com.android.cts.verifier.TestListActivity.sCurrentDisplayMode;
import static com.android.cts.verifier.TestListAdapter.setTestNameSuffix;

import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.compatibility.common.util.ResultType;
import com.android.compatibility.common.util.ResultUnit;
import com.android.cts.verifier.CtsVerifierReportLog;
import com.android.cts.verifier.R;

import org.hyphonate.megaaudio.player.AudioSourceProvider;
import org.hyphonate.megaaudio.player.JavaPlayer;
import org.hyphonate.megaaudio.player.PlayerBuilder;
import org.hyphonate.megaaudio.player.sources.SinAudioSourceProvider;

/**
 * Tests AudioTrack and AudioRecord (re)Routing messages.
 */
public class AudioOutputRoutingNotificationsActivity extends AudioWiredDeviceBaseActivity {
    private static final String TAG = "AudioOutputRoutingNotificationsActivity";

    static final int NUM_CHANNELS = 2;
    static final int SAMPLE_RATE = 48000;

    Context mContext;

    Button playBtn;
    Button stopBtn;
    TextView mInfoView;

    int mNumRoutingNotifications;

    private OnBtnClickListener mBtnClickListener = new OnBtnClickListener();

    // ignore messages sent as a consequence of starting the player
    private static final int NUM_IGNORE_MESSAGES = 1;

    // Mega Player
    private JavaPlayer mAudioPlayer;
    private AudioTrackRoutingChangeListener mRoutingChangeListener;
    private boolean mIsPlaying;

    private boolean mInitialRoutingMessageHandled;

    boolean mRoutingNotificationReceived;

    // ReportLog schema
    private static final String SECTION_OUTPUT_ROUTING = "audio_out_routing_notifications";

    private class OnBtnClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (mAudioPlayer == null) {
                return; // failed to create the player
            }
            int id = v.getId();
            if (id == R.id.audio_routingnotification_playBtn) {
                startPlayback();
            } else if (id == R.id.audio_routingnotification_playStopBtn) {
                stopPlayback();
            }
        }
    }

    private void startPlayback() {
        if (!mIsPlaying) {
            mNumRoutingNotifications = 0;

            mAudioPlayer.startStream();

            AudioTrack audioTrack = mAudioPlayer.getAudioTrack();
            audioTrack.addOnRoutingChangedListener(mRoutingChangeListener,
                    new Handler());

            mIsPlaying = true;

            enableTestButtons(false);
        }
    }

    private void stopPlayback() {
        if (mIsPlaying) {
            mAudioPlayer.stopStream();

            AudioTrack audioTrack = mAudioPlayer.getAudioTrack();
            audioTrack.removeOnRoutingChangedListener(mRoutingChangeListener);

            mIsPlaying = false;

            enableTestButtons(true);
        }
    }

    private class AudioTrackRoutingChangeListener implements AudioTrack.OnRoutingChangedListener {
        public void onRoutingChanged(AudioTrack audioTrack) {
            // Starting playback triggers a messages, so ignore the first one.
            mNumRoutingNotifications++;
            if (mNumRoutingNotifications <= NUM_IGNORE_MESSAGES) {
                return;
            }

            TextView textView =
                (TextView)findViewById(R.id.audio_routingnotification_audioTrack_change);
            String msg = mContext.getResources().getString(
                    R.string.audio_routingnotification_trackRoutingMsg);
            AudioDeviceInfo routedDevice = audioTrack.getRoutedDevice();
            CharSequence deviceName = routedDevice != null ? routedDevice.getProductName() : "none";
            mConnectedPeripheralName = deviceName.toString();
            int deviceType = routedDevice != null ? routedDevice.getType() : -1;
            textView.setText(msg + " - " +
                             deviceName + " [0x" + Integer.toHexString(deviceType) + "]" +
                             " - " + mNumRoutingNotifications);

            mRoutingNotificationReceived = true;
            calculatePass();
        }
    }

    @Override
    protected void enableTestButtons(boolean enabled) {
        playBtn.setEnabled(enabled);
        stopBtn.setEnabled(!enabled);
    }

    @Override
    protected void calculatePass() {
        getPassButton().setEnabled(mRoutingNotificationReceived || !mSupportsWiredPeripheral);
        if (mRoutingNotificationReceived) {
            ((TextView) findViewById(R.id.audio_routingnotification_testresult)).setText(
                    "Test PASSES - Routing notification received");
        } else if (!mSupportsWiredPeripheral) {
            ((TextView) findViewById(
                    R.id.audio_routingnotification_testresult)).setText(
                    "Test PASSES - No peripheral support");
        }

        stopPlayback();
    }

    protected void storeTestResults() {
        super.storeTestResults();

        CtsVerifierReportLog reportLog = getReportLog();
        reportLog.addValue(
                KEY_ROUTING_RECEIVED,
                mRoutingNotificationReceived ? 1 : 0,
                ResultType.NEUTRAL,
                ResultUnit.NONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_output_routingnotifications_test);

        mContext = this;

        playBtn = (Button) findViewById(R.id.audio_routingnotification_playBtn);
        playBtn.setOnClickListener(mBtnClickListener);
        stopBtn = (Button) findViewById(R.id.audio_routingnotification_playStopBtn);
        stopBtn.setOnClickListener(mBtnClickListener);

        enableTestButtons(false);

        mInfoView = (TextView) findViewById(R.id.info_text);

        // Setup Player
        //
        // Allocate the source provider for the sort of signal we want to play
        //
        AudioSourceProvider sourceProvider = new SinAudioSourceProvider();
        try {
            PlayerBuilder builder = new PlayerBuilder();
            mAudioPlayer = (JavaPlayer)builder
                    // choose one or the other of these for a Java or an Oboe player
                    .setPlayerType(PlayerBuilder.TYPE_JAVA)
                    // .setPlayerType(PlayerBuilder.PLAYER_OBOE)
                    .setSourceProvider(sourceProvider)
                    .build();
            //TODO - explain the choice of 96 here.
            mAudioPlayer.setupStream(NUM_CHANNELS, SAMPLE_RATE, 96);
        } catch (PlayerBuilder.BadStateException ex) {
            Log.e(TAG, "Failed MegaPlayer build.");
        }

        mRoutingChangeListener = new AudioTrackRoutingChangeListener();

        // "Honor System" buttons
        super.setup();
        setInfoResources(R.string.audio_output_routingnotifications_test,
                R.string.audio_output_routingnotification_instructions, -1);
        setPassFailButtonClickListeners();
        getPassButton().setEnabled(false);
    }

    @Override
    public final String getReportSectionName() {
        return setTestNameSuffix(sCurrentDisplayMode, SECTION_OUTPUT_ROUTING);
    }

    @Override
    public void onBackPressed () {
        stopPlayback();
        super.onBackPressed();
    }
}