Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioManager; |
| 21 | import android.telecomm.CallState; |
| 22 | |
| 23 | /** |
| 24 | * This class manages audio modes, streams and other properties. |
| 25 | */ |
| 26 | final 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 | } |