blob: 3aee1fb08fc92099da272fae84d7d4c0ed107e05 [file] [log] [blame]
Hakan Lindh8853c6d2017-04-06 10:20:36 -07001/*
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 */
16package com.android.media.tests;
17
18import com.android.ddmlib.CollectingOutputReceiver;
19import com.android.tradefed.device.DeviceNotAvailableException;
20import com.android.tradefed.device.ITestDevice;
21import com.android.tradefed.log.LogUtil.CLog;
22
23import java.util.concurrent.TimeUnit;
24
25/** Class to provide audio level utility functions for a test device */
26public class AudioLevelUtility {
27
28 public static int extractDeviceAudioLevelFromAdbShell(ITestDevice device)
29 throws DeviceNotAvailableException {
30
31 final String ADB_SHELL_DUMPSYS_AUDIO = "dumpsys audio";
32 final String STREAM_MUSIC = "- STREAM_MUSIC:";
33 final String HEADSET = "(headset): ";
34
35 final CollectingOutputReceiver receiver = new CollectingOutputReceiver();
36
37 device.executeShellCommand(
38 ADB_SHELL_DUMPSYS_AUDIO, receiver, 300, TimeUnit.MILLISECONDS, 1);
39 final String shellOutput = receiver.getOutput();
40 if (shellOutput == null || shellOutput.isEmpty()) {
41 return -1;
42 }
43
44 int audioLevel = -1;
45 int pos = shellOutput.indexOf(STREAM_MUSIC);
46 if (pos != -1) {
47 pos = shellOutput.indexOf(HEADSET, pos);
48 if (pos != -1) {
49 final int start = pos + HEADSET.length();
50 final int stop = shellOutput.indexOf(",", start);
51 if (stop != -1) {
52 final String audioLevelStr = shellOutput.substring(start, stop);
53 try {
54 audioLevel = Integer.parseInt(audioLevelStr);
55 } catch (final NumberFormatException e) {
56 CLog.e(e.getMessage());
57 audioLevel = 1;
58 }
59 }
60 }
61 }
62
63 return audioLevel;
64 }
65}