blob: bb32cc3753455810551cb3b6b9ecd1a37ab82324 [file] [log] [blame]
Sailesh Nepal6aca10a2014-03-24 16:11:02 -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
23import com.google.common.collect.Lists;
24
25import java.util.List;
26
27/**
28 * Controls the ringtone player.
29 */
30final 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 Nepale59bb192014-04-01 18:33:59 -070037 private final List<Call> mUnansweredCalls = Lists.newLinkedList();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070038
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 Nepale59bb192014-04-01 18:33:59 -070048 if (mUnansweredCalls.contains(call)) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070049 Log.wtf(this, "New ringing call is already in list of unanswered calls");
50 }
Sailesh Nepale59bb192014-04-01 18:33:59 -070051 mUnansweredCalls.add(call);
52 if (mUnansweredCalls.size() == 1) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070053 // 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 Nepale59bb192014-04-01 18:33:59 -070062 removeFromUnansweredCall(call);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070063 }
64
65 @Override
66 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
67 if (newState != CallState.RINGING) {
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 removeFromUnansweredCall(call);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070069 }
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 Nepale59bb192014-04-01 18:33:59 -070084 if (!mUnansweredCalls.isEmpty() && mUnansweredCalls.get(0) == call) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070085 stopRinging();
86 }
87 }
88
89 /**
90 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
Sailesh Nepale59bb192014-04-01 18:33:59 -070091 * 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 Nepal6aca10a2014-03-24 16:11:02 -070093 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070094 private void removeFromUnansweredCall(Call call) {
95 if (mUnansweredCalls.remove(call)) {
96 if (mUnansweredCalls.isEmpty()) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070097 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}