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 android.nativemedia.aaudio;
18 
19 import static android.Manifest.permission.CAPTURE_AUDIO_HOTWORD;
20 import static android.Manifest.permission.CAPTURE_AUDIO_OUTPUT;
21 
22 import android.app.UiAutomation;
23 import android.media.AudioDeviceInfo;
24 import android.media.AudioFormat;
25 import android.media.AudioManager;
26 import android.os.IBinder;
27 import android.os.ServiceManager;
28 
29 import androidx.test.platform.app.InstrumentationRegistry;
30 
31 import com.android.gtestrunner.GtestRunner;
32 import com.android.gtestrunner.TargetLibrary;
33 
34 import org.junit.runner.RunWith;
35 
36 import java.util.Arrays;
37 
38 @RunWith(GtestRunner.class)
39 @TargetLibrary("nativeaaudiotest")
40 public class AAudioTests {
getAudioFlinger()41     static IBinder getAudioFlinger() {
42         return ServiceManager.getService("media.audio_flinger");
43     }
44 
isIEC61937Supported()45     static boolean isIEC61937Supported() {
46         AudioDeviceInfo[] devices = AudioManager.getDevicesStatic(AudioManager.GET_DEVICES_OUTPUTS);
47         for (AudioDeviceInfo device : devices) {
48             if (Arrays.stream(device.getEncodings()).anyMatch(
49                     encoding -> encoding == AudioFormat.ENCODING_IEC61937)) {
50                 return true;
51             }
52         }
53         return false;
54     }
55 
isEchoReferenceSupported()56     static boolean isEchoReferenceSupported() {
57         AudioDeviceInfo[] devices = AudioManager.getDevicesStatic(AudioManager.GET_DEVICES_INPUTS);
58         for (AudioDeviceInfo device : devices) {
59             if (device.getType() == AudioDeviceInfo.TYPE_ECHO_REFERENCE) {
60                 return true;
61             }
62         }
63         return false;
64     }
65 
enableAudioOutputPermission()66     static void enableAudioOutputPermission() {
67         // Drop any identity adopted earlier.
68         UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
69         uiAutomation.dropShellPermissionIdentity();
70         // need to retain the identity until the callback is triggered
71         uiAutomation.adoptShellPermissionIdentity(CAPTURE_AUDIO_OUTPUT);
72     }
73 
enableAudioHotwordPermission()74     static void enableAudioHotwordPermission() {
75         // Drop any identity adopted earlier.
76         UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
77         uiAutomation.dropShellPermissionIdentity();
78         // need to retain the identity until the callback is triggered
79         uiAutomation.adoptShellPermissionIdentity(CAPTURE_AUDIO_HOTWORD);
80     }
81 
disablePermissions()82     static void disablePermissions() {
83         // Drop any identity adopted earlier.
84         UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
85         uiAutomation.dropShellPermissionIdentity();
86     }
87 }
88