• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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.hdmicec.cts;
18 
19 import com.android.tradefed.device.ITestDevice;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import java.util.stream.IntStream;
25 
26 /** Helper class to get DUT audio status using Audio manager app */
27 public final class AudioManagerHelper {
28 
29     /** The package name of the APK. */
30     private static final String PACKAGE = "android.hdmicec.app";
31 
32     /** The class name of the main activity in the APK. */
33     private static final String CLASS = "HdmiCecAudioManager";
34 
35     /** The command to launch the main activity. */
36     private static final String START_COMMAND =
37             String.format("am start -n %s/%s.%s -a ", PACKAGE, PACKAGE, CLASS);
38 
39     /** The command to clear the main activity. */
40     private static final String CLEAR_COMMAND = String.format("pm clear %s", PACKAGE);
41 
42     public static final int MAX_AUDIO_FORMATS = 4;
43     public static final int MAX_VALID_AUDIO_FORMATS = 2;
44 
45     public static List<Integer> mSupportedAudioFormats = null;
46 
muteDevice(ITestDevice device, HdmiCecClientWrapper hdmiCecClient)47     public static void muteDevice(ITestDevice device, HdmiCecClientWrapper hdmiCecClient)
48             throws Exception {
49         // Clear activity
50         device.executeShellCommand(CLEAR_COMMAND);
51         // Clear logcat.
52         device.executeAdbCommand("logcat", "-c");
53         // Start the APK and wait for it to complete.
54         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.MUTE");
55         // The audio device should send <Report Audio Status> message after mute.
56         hdmiCecClient.checkExpectedOutput(LogicalAddress.TV, CecOperand.REPORT_AUDIO_STATUS);
57     }
58 
unmuteDevice(ITestDevice device, HdmiCecClientWrapper hdmiCecClient)59     public static void unmuteDevice(ITestDevice device, HdmiCecClientWrapper hdmiCecClient)
60             throws Exception {
61         // Clear activity
62         device.executeShellCommand(CLEAR_COMMAND);
63         // Start the APK and wait for it to complete.
64         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.UNMUTE");
65         // The audio device should send <Report Audio Status> message after unmute.
66         hdmiCecClient.checkExpectedOutput(LogicalAddress.TV, CecOperand.REPORT_AUDIO_STATUS);
67     }
68 
unmuteDevice(ITestDevice device)69     public static void unmuteDevice(ITestDevice device)
70             throws Exception {
71         // Clear activity
72         device.executeShellCommand(CLEAR_COMMAND);
73         // Start the APK and wait for it to complete.
74         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.UNMUTE");
75     }
76 
isDeviceMuted(ITestDevice device)77     public static boolean isDeviceMuted(ITestDevice device) throws Exception {
78         // Clear activity
79         device.executeShellCommand(CLEAR_COMMAND);
80         // Clear logcat.
81         device.executeAdbCommand("logcat", "-c");
82         // Start the APK and wait for it to complete.
83         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.REPORT_VOLUME");
84         return (LogHelper.parseDutVolume(device, CLASS) >= 128);
85     }
86 
lowerVolume(ITestDevice device)87     public static void lowerVolume(ITestDevice device) throws Exception {
88         // Clear activity
89         device.executeShellCommand(CLEAR_COMMAND);
90         // Start the APK and wait for it to complete
91         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.LOWER_VOLUME");
92     }
93 
raiseVolume(ITestDevice device)94     public static void raiseVolume(ITestDevice device) throws Exception {
95         // Clear activity
96         device.executeShellCommand(CLEAR_COMMAND);
97         // Start the APK and wait for it to complete
98         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.RAISE_VOLUME");
99     }
100 
setDeviceVolume(ITestDevice device, int percentVolume)101     public static void setDeviceVolume(ITestDevice device, int percentVolume) throws Exception {
102         // Clear activity
103         device.executeShellCommand(CLEAR_COMMAND);
104         // Start the APK and wait for it to complete.
105         device.executeShellCommand(
106                 START_COMMAND
107                         + "android.hdmicec.app.SET_VOLUME --ei "
108                         + "\"volumePercent\" "
109                         + percentVolume);
110     }
111 
getDutAudioVolume(ITestDevice device)112     public static int getDutAudioVolume(ITestDevice device) throws Exception {
113         // Clear activity
114         device.executeShellCommand(CLEAR_COMMAND);
115         // Clear logcat.
116         device.executeAdbCommand("logcat", "-c");
117         // Start the APK and wait for it to complete.
118         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.REPORT_VOLUME");
119         return LogHelper.parseDutVolume(device, CLASS);
120     }
121 
getRequestSadFormatsParams(ITestDevice device, boolean sendValidFormats)122     public static String getRequestSadFormatsParams(ITestDevice device, boolean sendValidFormats)
123             throws Exception {
124         // Clear activity
125         device.executeShellCommand(CLEAR_COMMAND);
126         // Clear logcat.
127         device.executeAdbCommand("logcat", "-c");
128         // Start the APK and wait for it to complete.
129         device.executeShellCommand(START_COMMAND + "android.hdmicec.app.GET_SUPPORTED_SAD_FORMATS");
130         mSupportedAudioFormats = LogHelper.getSupportedAudioFormats(device);
131 
132         // Create a list of all the audio format codes according to CEA-861-D. Remove the supported
133         // audio format codes from it, to get the unsupported audio format codes.
134         List<Integer> mAllCodecFormats =
135                 IntStream.range(1, 15).boxed().collect(Collectors.toList());
136         List<Integer> unsupportedAudioFormats = new ArrayList<>();
137         unsupportedAudioFormats.addAll(mAllCodecFormats);
138         unsupportedAudioFormats.removeAll(mSupportedAudioFormats);
139         // Create params message for REQUEST_SHORT_AUDIO_DESCRIPTOR
140         String messageParams = "";
141         int i = 0;
142         int listIndex = 0;
143         if (sendValidFormats) {
144             while (i < Math.min(MAX_VALID_AUDIO_FORMATS, mSupportedAudioFormats.size())) {
145                 messageParams += CecMessage.formatParams(mSupportedAudioFormats.get(listIndex), 2);
146                 i++;
147                 listIndex++;
148             }
149             listIndex = 0;
150         }
151         while (i < Math.min(MAX_AUDIO_FORMATS, unsupportedAudioFormats.size())) {
152             messageParams += CecMessage.formatParams(unsupportedAudioFormats.get(listIndex), 2);
153             i++;
154             listIndex++;
155         }
156         return messageParams;
157     }
158 }
159