Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -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 | import com.google.common.collect.Lists; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Controls the ringtone player. |
| 29 | */ |
| 30 | final class Ringer extends CallsManagerListenerBase { |
| 31 | private final AsyncRingtonePlayer mRingtonePlayer = new AsyncRingtonePlayer(); |
| 32 | |
| 33 | /** |
| 34 | * Used to keep ordering of unanswered incoming calls. There can easily exist multiple incoming |
| 35 | * calls and explicit ordering is useful for maintaining the proper state of the ringer. |
| 36 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 37 | private final List<Call> mUnansweredCalls = Lists.newLinkedList(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 38 | |
| 39 | private final CallAudioManager mCallAudioManager; |
| 40 | |
| 41 | Ringer(CallAudioManager callAudioManager) { |
| 42 | mCallAudioManager = callAudioManager; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void onCallAdded(Call call) { |
| 47 | if (call.isIncoming() && call.getState() == CallState.RINGING) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 48 | if (mUnansweredCalls.contains(call)) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 49 | Log.wtf(this, "New ringing call is already in list of unanswered calls"); |
| 50 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 51 | mUnansweredCalls.add(call); |
| 52 | if (mUnansweredCalls.size() == 1) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 53 | // Start the ringer if we are the top-most incoming call (the only one in this |
| 54 | // case). |
| 55 | startRinging(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onCallRemoved(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 62 | removeFromUnansweredCall(call); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
| 67 | if (newState != CallState.RINGING) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 68 | removeFromUnansweredCall(call); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onIncomingCallAnswered(Call call) { |
| 74 | onRespondedToIncomingCall(call); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onIncomingCallRejected(Call call) { |
| 79 | onRespondedToIncomingCall(call); |
| 80 | } |
| 81 | |
| 82 | private void onRespondedToIncomingCall(Call call) { |
| 83 | // Only stop the ringer if this call is the top-most incoming call. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 84 | if (!mUnansweredCalls.isEmpty() && mUnansweredCalls.get(0) == call) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 85 | stopRinging(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Removes the specified call from the list of unanswered incoming calls and updates the ringer |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 91 | * based on the new state of {@link #mUnansweredCalls}. Safe to call with a call that is not |
| 92 | * present in the list of incoming calls. |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 93 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 94 | private void removeFromUnansweredCall(Call call) { |
| 95 | if (mUnansweredCalls.remove(call)) { |
| 96 | if (mUnansweredCalls.isEmpty()) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 97 | stopRinging(); |
| 98 | } else { |
| 99 | startRinging(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private void startRinging() { |
| 105 | AudioManager audioManager = (AudioManager) TelecommApp.getInstance().getSystemService( |
| 106 | Context.AUDIO_SERVICE); |
| 107 | if (audioManager.getStreamVolume(AudioManager.STREAM_RING) > 0) { |
| 108 | Log.v(this, "startRinging"); |
| 109 | mCallAudioManager.setIsRinging(true); |
| 110 | mRingtonePlayer.play(); |
| 111 | } else { |
| 112 | Log.v(this, "startRinging, skipping because volume is 0"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private void stopRinging() { |
| 117 | Log.v(this, "stopRinging"); |
| 118 | mRingtonePlayer.stop(); |
| 119 | // Even though stop is asynchronous it's ok to update the audio manager. Things like audio |
| 120 | // focus are voluntary so releasing focus too early is not detrimental. |
| 121 | mCallAudioManager.setIsRinging(false); |
| 122 | } |
| 123 | } |