1 /* 2 * Copyright (C) 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.app; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.media.AudioManager; 22 import android.os.Bundle; 23 import android.util.Log; 24 25 import com.android.server.hdmi.SadConfigurationReaderTest; 26 27 import org.junit.runner.JUnitCore; 28 29 /** 30 * A simple app that can be used to mute, unmute, set volume or get the volume status of a device. 31 * The actions supported are: 32 * 33 * <p>1. android.hdmicec.app.MUTE: Mutes the STREAM_MUSIC of the device, irrespective of the 34 * previous state. Usage: START_COMMAND -a android.hdmicec.app.MUTE 35 * 36 * <p>2. android.hdmicec.app.UNMUTE: Unmutes the STREAM_MUSIC of the device, irrespective of the 37 * previous state. Usage: START_COMMAND -a android.hdmicec.app.UNMUTE 38 * 39 * <p>3. android.hdmicec.app.REPORT_VOLUME: Reports current volume level in percent if not muted. 40 * Adds 128 to volume percent level if the device is muted. Usage: START_COMMAND -a 41 * android.hdmicec.app.REPORT_VOLUME 42 * 43 * <p>4. android.hdmicec.app.SET_VOLUME: Sets the volume of STREAM_MUSIC to a particular level. Has 44 * to be used with --ei "volumePercent" x. Usage: START_COMMAND -a android.hdmicec.app.SET_VOLUME 45 * --ei "volumePercent" x 46 * 47 * <p>where START_COMMAND is adb shell am start -n 48 * "android.hdmicec.app/android.hdmicec.app.HdmiCecAudioManager" 49 */ 50 public class HdmiCecAudioManager extends Activity { 51 52 private static final String TAG = HdmiCecAudioManager.class.getSimpleName(); 53 54 @Override onCreate(Bundle icicle)55 public void onCreate(Bundle icicle) { 56 super.onCreate(icicle); 57 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 58 int minVolume = audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC); 59 int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 60 switch(getIntent().getAction()) { 61 case "android.hdmicec.app.MUTE": 62 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 63 AudioManager.ADJUST_MUTE, 0); 64 break; 65 case "android.hdmicec.app.UNMUTE": 66 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 67 AudioManager.ADJUST_UNMUTE, 0); 68 break; 69 case "android.hdmicec.app.REPORT_VOLUME": 70 int volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 71 int percentageVolume = 100 * volumeLevel / (maxVolume - minVolume); 72 if (audioManager.isStreamMute(AudioManager.STREAM_MUSIC)) { 73 percentageVolume += 128; 74 } 75 Log.i(TAG, "Volume at " + percentageVolume + "%"); 76 break; 77 case "android.hdmicec.app.RAISE_VOLUME": 78 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 79 AudioManager.ADJUST_RAISE, 0); 80 break; 81 case "android.hdmicec.app.LOWER_VOLUME": 82 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 83 AudioManager.ADJUST_LOWER, 0); 84 break; 85 case "android.hdmicec.app.SET_VOLUME": 86 int percentVolume = getIntent().getIntExtra("volumePercent", 50); 87 int volume = minVolume + ((maxVolume - minVolume) * percentVolume / 100); 88 audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); 89 Log.i(TAG, "Set volume to " + volume + " (" + percentVolume + "%)"); 90 break; 91 case "android.hdmicec.app.GET_SUPPORTED_SAD_FORMATS": 92 JUnitCore junit = new JUnitCore(); 93 junit.run(SadConfigurationReaderTest.class); 94 break; 95 default: 96 Log.w(TAG, "Unknown intent!"); 97 } 98 finishAndRemoveTask(); 99 } 100 } 101 102