summaryrefslogtreecommitdiff
path: root/apps/CtsVerifier/src/com/android/cts/verifier/audio/USBAudioPeripheralNotificationsTest.java
blob: bc23048c28ee08fd99d398c56b3aa5aafd4317b5 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2020 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 android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.media.AudioDeviceCallback;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;

import android.os.Bundle;
import android.os.Handler;

import android.util.Log;

import android.widget.TextView;

import com.android.compatibility.common.util.CddTest;
import com.android.compatibility.common.util.ReportLog;
import com.android.compatibility.common.util.ResultType;
import com.android.compatibility.common.util.ResultUnit;

import com.android.cts.verifier.PassFailButtons;
import com.android.cts.verifier.R;  // needed to access resource in CTSVerifier project namespace.

@CddTest(requirement = "7.8.2.2/H-2-1,H-3-1,H-4-2,H-4-3,H-4-4,H-4-5")
public class USBAudioPeripheralNotificationsTest extends PassFailButtons.Activity {
    private static final String
            TAG = USBAudioPeripheralNotificationsTest.class.getSimpleName();

    private AudioManager    mAudioManager;

    private TextView mHeadsetInName;
    private TextView mHeadsetOutName;
    private TextView mUsbDeviceInName;
    private TextView mUsbDeviceOutName;

    // private TextView mHeadsetPlugText;
    private TextView mHeadsetPlugMessage;

    // Intents
    private HeadsetPlugReceiver mHeadsetPlugReceiver;
    private boolean mPlugIntentReceived;

    // Device
    private AudioDeviceInfo mUsbHeadsetInInfo;
    private AudioDeviceInfo mUsbHeadsetOutInfo;
    private AudioDeviceInfo mUsbDeviceInInfo;
    private AudioDeviceInfo mUsbDeviceOutInfo;

    private boolean mUsbHeadsetInReceived;
    private boolean mUsbHeadsetOutReceived;
    private boolean mUsbDeviceInReceived;
    private boolean mUsbDeviceOutReceived;

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

        mHeadsetInName = (TextView)findViewById(R.id.uap_messages_headset_in_name);
        mHeadsetOutName = (TextView)findViewById(R.id.uap_messages_headset_out_name);

        mUsbDeviceInName = (TextView)findViewById(R.id.uap_messages_usb_device_in_name);
        mUsbDeviceOutName = (TextView)findViewById(R.id.uap_messages_usb_device__out_name);

        mHeadsetPlugMessage = (TextView)findViewById(R.id.uap_messages_plug_message);

        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        mAudioManager.registerAudioDeviceCallback(new ConnectListener(), new Handler());

        mHeadsetPlugReceiver = new HeadsetPlugReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(mHeadsetPlugReceiver, filter);

        setInfoResources(R.string.audio_uap_notifications_test, R.string.uapNotificationsTestInfo,
                -1);

        setPassFailButtonClickListeners();
        getPassButton().setEnabled(false);
    }

    //
    // UI
    //
    private void showConnectedDevices() {
        if (mUsbHeadsetInInfo != null) {
            mHeadsetInName.setText(
                    "Headset INPUT Connected " + mUsbHeadsetInInfo.getProductName());
        } else {
            mHeadsetInName.setText("");
        }

        if (mUsbHeadsetOutInfo != null) {
            mHeadsetOutName.setText(
                    "Headset OUTPUT Connected " + mUsbHeadsetOutInfo.getProductName());
        } else {
            mHeadsetOutName.setText("");
        }

        if (mUsbDeviceInInfo != null) {
            mUsbDeviceInName.setText(
                    "USB DEVICE INPUT Connected " + mUsbDeviceInInfo.getProductName());
        } else {
            mUsbDeviceInName.setText("");
        }

        if (mUsbDeviceOutInfo != null) {
            mUsbDeviceOutName.setText(
                    "USB DEVICE OUTPUT Connected " + mUsbDeviceOutInfo.getProductName());
        } else {
            mUsbDeviceOutName.setText("");
        }
    }

    private void reportPlugIntent(Intent intent) {
        // [ 7.8 .2.2/H-2-1] MUST broadcast Intent ACTION_HEADSET_PLUG with "microphone" extra
        // set to 0 when the USB audio terminal types 0x0302 is detected.
        // [ 7.8 .2.2/H-3-1] MUST broadcast Intent ACTION_HEADSET_PLUG with "microphone" extra
        // set to 1 when the USB audio terminal types 0x0402 is detected, they:
        mPlugIntentReceived = true;

        // state - 0 for unplugged, 1 for plugged.
        // name - Headset type, human readable string
        // microphone - 1 if headset has a microphone, 0 otherwise
        int state = intent.getIntExtra("state", -1);
        if (state != -1) {

            StringBuilder sb = new StringBuilder();
            sb.append("ACTION_HEADSET_PLUG received - " + (state == 0 ? "Unplugged" : "Plugged"));

            String name = intent.getStringExtra("name");
            if (name != null) {
                sb.append(" - " + name);
            }

            int hasMic = intent.getIntExtra("microphone", 0);
            if (hasMic == 1) {
                sb.append(" [mic]");
            }

            mHeadsetPlugMessage.setText(sb.toString());
        }

        getReportLog().addValue(
                "ACTION_HEADSET_PLUG Intent Received. State: ",
                state,
                ResultType.NEUTRAL,
                ResultUnit.NONE);

        getPassButton().setEnabled(calculatePass());
    }

    //
    // Test Status
    //
    private boolean calculatePass() {
        return mUsbHeadsetInReceived && mUsbHeadsetOutReceived &&
                mUsbDeviceInReceived && mUsbDeviceOutReceived &&
                mPlugIntentReceived;
    }

    //
    // Devices
    //
    private void scanDevices(AudioDeviceInfo[] devices) {
        mUsbHeadsetInInfo = mUsbHeadsetOutInfo =
                mUsbDeviceInInfo = mUsbDeviceOutInfo = null;

        for (AudioDeviceInfo devInfo : devices) {
            if (devInfo.getType() == AudioDeviceInfo.TYPE_USB_HEADSET) {
                if (devInfo.isSource()) {
                    // [ 7.8 .2.2/H-4-3] MUST list a device of type AudioDeviceInfo.TYPE_USB_HEADSET
                    // and role isSource() if the USB audio terminal type field is 0x0402.
                    mUsbHeadsetInReceived = true;
                    mUsbHeadsetInInfo = devInfo;
                    getReportLog().addValue(
                            "USB Headset connected - INPUT",
                            0,
                            ResultType.NEUTRAL,
                            ResultUnit.NONE);
                } else if (devInfo.isSink()) {
                    // [ 7.8 .2.2/H-4-2] MUST list a device of type AudioDeviceInfo.TYPE_USB_HEADSET
                    // and role isSink() if the USB audio terminal type field is 0x0402.
                    mUsbHeadsetOutReceived = true;
                    mUsbHeadsetOutInfo = devInfo;
                    getReportLog().addValue(
                            "USB Headset connected - OUTPUT",
                            0,
                            ResultType.NEUTRAL,
                            ResultUnit.NONE);
                }
            } else if (devInfo.getType() == AudioDeviceInfo.TYPE_USB_DEVICE) {
                if (devInfo.isSource()) {
                    // [ 7.8 .2.2/H-4-5] MUST list a device of type AudioDeviceInfo.TYPE_USB_DEVICE
                    // and role isSource() if the USB audio terminal type field is 0x604.
                    mUsbDeviceInReceived = true;
                    mUsbDeviceInInfo = devInfo;
                    getReportLog().addValue(
                            "USB Device connected - INPUT",
                            0,
                            ResultType.NEUTRAL,
                            ResultUnit.NONE);
                } else if (devInfo.isSink()) {
                    // [ 7.8 .2.2/H-4-4] MUST list a device of type AudioDeviceInfo.TYPE_USB_DEVICE
                    // and role isSink() if the USB audio terminal type field is 0x603.
                    mUsbDeviceOutReceived = true;
                    mUsbDeviceOutInfo = devInfo;
                    getReportLog().addValue(
                            "USB Headset connected - OUTPUT",
                            0,
                            ResultType.NEUTRAL,
                            ResultUnit.NONE);
                }
            }

            if (mUsbHeadsetInInfo != null &&
                    mUsbHeadsetOutInfo != null &&
                    mUsbDeviceInInfo != null &&
                    mUsbDeviceOutInfo != null) {
                break;
            }
        }


        showConnectedDevices();
        getPassButton().setEnabled(calculatePass());
    }

    private class ConnectListener extends AudioDeviceCallback {
        /*package*/ ConnectListener() {}

        //
        // AudioDevicesManager.OnDeviceConnectionListener
        //
        @Override
        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
            Log.i(TAG, "onAudioDevicesAdded() num:" + addedDevices.length);

            scanDevices(mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL));
        }

        @Override
        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
            Log.i(TAG, "onAudioDevicesRemoved() num:" + removedDevices.length);

            scanDevices(mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL));
        }
    }

    // Intents
    private class HeadsetPlugReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            reportPlugIntent(intent);
        }
    }

}