1 /* 2 * Copyright (C) 2012 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 com.android.cts.audiotest; 18 19 import android.app.Activity; 20 import android.app.KeyguardManager; 21 import android.content.Context; 22 import android.media.AudioManager; 23 import android.os.Bundle; 24 25 26 public class CtsAudioClientActivity extends Activity { 27 private AudioProtocol mProtocol = null; 28 int mVolumeMusic; 29 int mVolumeVoice; 30 @Override onPause()31 protected void onPause() { 32 try { 33 mProtocol.stop(); 34 } catch (InterruptedException e) { 35 e.printStackTrace(); 36 } 37 mProtocol = null; 38 setVolume(AudioManager.STREAM_MUSIC, mVolumeMusic); 39 setVolume(AudioManager.STREAM_VOICE_CALL, mVolumeVoice); 40 super.onPause(); 41 } 42 43 @Override onResume()44 protected void onResume() { 45 // set volume to max 46 mVolumeMusic = setVolume(AudioManager.STREAM_MUSIC, -1); 47 mVolumeVoice = setVolume(AudioManager.STREAM_VOICE_CALL, -1); 48 mProtocol = new AudioProtocol(); 49 mProtocol.start(); 50 super.onResume(); 51 } 52 53 /** Called when the activity is first created. */ 54 @Override onCreate(Bundle savedInstanceState)55 public void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 setContentView(R.layout.main); 58 KeyguardManager keyguardManager = 59 (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 60 keyguardManager.newKeyguardLock("cts-audio").disableKeyguard(); 61 } 62 63 /** 64 * set volume to desired level 65 * @param level target level, if -1, set to max 66 * @return the original volume level 67 */ setVolume(int stream, int level)68 int setVolume(int stream, int level) { 69 AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 70 mgr.setStreamMute(stream, false); 71 int original = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); 72 int targetLevel = level; 73 if (level == -1) { 74 targetLevel = mgr.getStreamMaxVolume(stream); 75 } 76 mgr.setStreamVolume(stream, targetLevel, 0); 77 return original; 78 } 79 }