blob: 07d5dbe111d138dbb21d8f5cc569bb77c6ee1b5a [file] [log] [blame]
Sailesh Nepal810735e2014-03-18 18:15:46 -07001/*
2 * Copyright (C) 2014 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
17package com.android.telecomm;
18
19import android.content.Context;
20import android.media.AudioManager;
21import android.telecomm.CallState;
22
23/**
24 * This class manages audio modes, streams and other properties.
25 */
26final class CallAudioManager extends CallsManagerListenerBase {
27 @Override
28 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
29 switch (newState) {
30 case ACTIVE:
31 updateAudioFocusForActiveCall(call);
32 break;
33 case DISCONNECTED:
34 updateAudioFocusForNoCall();
35 break;
36 default:
37 break;
38 }
39 }
40
41 private void updateAudioFocusForActiveCall(Call call) {
42 Log.v(this, "onForegroundCallChanged, requesting audio focus");
43 Context context = TelecommApp.getInstance();
44 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
45 audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
46 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
47 audioManager.setMode(AudioManager.MODE_IN_CALL);
48 audioManager.setMicrophoneMute(false);
49 audioManager.setSpeakerphoneOn(false);
50 }
51
52 private void updateAudioFocusForNoCall() {
53 Log.v(this, "updateAudioFocusForNoCall, abandoning audio focus");
54 Context context = TelecommApp.getInstance().getApplicationContext();
55 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
56 audioManager.setMode(AudioManager.MODE_NORMAL);
57 audioManager.abandonAudioFocusForCall();
58 }
59}