blob: dbf76d5069dd09c463c71541e0afc0ab7b2326e5 [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 */
37 private final List<String> mUnansweredCallIds = Lists.newLinkedList();
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) {
48 if (mUnansweredCallIds.contains(call.getId())) {
49 Log.wtf(this, "New ringing call is already in list of unanswered calls");
50 }
51 mUnansweredCallIds.add(call.getId());
52 if (mUnansweredCallIds.size() == 1) {
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) {
62 removeFromUnansweredCallIds(call.getId());
63 }
64
65 @Override
66 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
67 if (newState != CallState.RINGING) {
68 removeFromUnansweredCallIds(call.getId());
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.
84 if (!mUnansweredCallIds.isEmpty() && mUnansweredCallIds.get(0).equals(call.getId())) {
85 stopRinging();
86 }
87 }
88
89 /**
90 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
91 * based on the new state of {@link #mUnansweredCallIds}. Safe to call with a call ID that
92 * is not present in the list of incoming calls.
93 *
94 * @param callId The ID of the call.
95 */
96 private void removeFromUnansweredCallIds(String callId) {
97 if (mUnansweredCallIds.remove(callId)) {
98 if (mUnansweredCallIds.isEmpty()) {
99 stopRinging();
100 } else {
101 startRinging();
102 }
103 }
104 }
105
106 private void startRinging() {
107 AudioManager audioManager = (AudioManager) TelecommApp.getInstance().getSystemService(
108 Context.AUDIO_SERVICE);
109 if (audioManager.getStreamVolume(AudioManager.STREAM_RING) > 0) {
110 Log.v(this, "startRinging");
111 mCallAudioManager.setIsRinging(true);
112 mRingtonePlayer.play();
113 } else {
114 Log.v(this, "startRinging, skipping because volume is 0");
115 }
116 }
117
118 private void stopRinging() {
119 Log.v(this, "stopRinging");
120 mRingtonePlayer.stop();
121 // Even though stop is asynchronous it's ok to update the audio manager. Things like audio
122 // focus are voluntary so releasing focus too early is not detrimental.
123 mCallAudioManager.setIsRinging(false);
124 }
125}