Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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.media.cts;
     18 
     19 import android.media.cts.R;
     20 
     21 import android.app.Activity;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.media.AudioManager;
     25 import android.media.AudioSystem;
     26 import android.media.MediaPlayer;
     27 import android.os.Bundle;
     28 
     29 import com.android.compatibility.common.util.CTSResult;
     30 
     31 public class AudioManagerStub extends Activity {
     32     private final int MP3_TO_PLAY = R.raw.testmp3;
     33     private MediaPlayer mMediaPlayer;
     34     private AudioManager mAudioManager;
     35     private static CTSResult mCTSResult;
     36     private static final int[] STREAM_TYPES = {
     37             AudioManager.STREAM_VOICE_CALL, AudioManager.STREAM_SYSTEM,
     38             AudioManager.STREAM_RING, AudioManager.STREAM_MUSIC, AudioManager.STREAM_ALARM,
     39             AudioManager.STREAM_NOTIFICATION, AudioManager.STREAM_DTMF,
     40             AudioManager.STREAM_ACCESSIBILITY };
     41 
     42     public static void setCTSResult(CTSResult cr) {
     43         mCTSResult = cr;
     44     }
     45 
     46     @Override
     47     protected void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
     50         mMediaPlayer = MediaPlayer.create(this, MP3_TO_PLAY);
     51         mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
     52         mMediaPlayer.setLooping(false);
     53         mMediaPlayer.start();
     54     }
     55 
     56     @Override
     57     protected void onPause() {
     58         super.onPause();
     59         try {
     60             for (int i : STREAM_TYPES) {
     61                 mAudioManager.setStreamMute(i, false);
     62                 mAudioManager.setStreamSolo(i, false);
     63             }
     64         } catch (Exception e) {
     65             mCTSResult.setResult(CTSResult.RESULT_FAIL);
     66             finish();
     67         }
     68     }
     69 
     70     @Override
     71     protected void onResume() {
     72         super.onResume();
     73         try {
     74             for (int i : STREAM_TYPES) {
     75                 mAudioManager.setStreamMute(i, true);
     76                 mAudioManager.setStreamSolo(i, true);
     77             }
     78         } catch (Exception e) {
     79             mCTSResult.setResult(CTSResult.RESULT_FAIL);
     80             finish();
     81             return;
     82         }
     83 
     84         Intent intent = new Intent();
     85         intent.setClass(this, AudioManagerStubHelper.class);
     86         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     87         startActivityForResult(intent, 1);
     88     }
     89 
     90     @Override
     91     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     92         super.onActivityResult(requestCode, resultCode, data);
     93         mMediaPlayer.stop();
     94         mMediaPlayer.release();
     95         mCTSResult.setResult(CTSResult.RESULT_OK);
     96         finish();
     97     }
     98 }
     99