blob: bb130d14f424abdfc4d651f468b2e13e5ae40deb [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 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
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Sailesh Nepalce704b92014-03-17 18:31:43 -070019import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070021import android.telecomm.CallAudioState;
Sailesh Nepal810735e2014-03-18 18:15:46 -070022import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080023import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080024import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.telecomm.GatewayInfo;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Santos Cordon681663d2014-01-30 04:32:15 -080028import com.google.common.base.Preconditions;
29import com.google.common.base.Strings;
Sailesh Nepal810735e2014-03-18 18:15:46 -070030import com.google.common.collect.ImmutableCollection;
31import com.google.common.collect.ImmutableList;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080032import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080033import com.google.common.collect.Maps;
Santos Cordona56f2762014-03-24 15:55:53 -070034import com.google.common.collect.Sets;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Gilad9f2bed32013-12-12 17:43:26 -080036import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080037import java.util.Map;
Santos Cordona56f2762014-03-24 15:55:53 -070038import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080039
Ben Giladdd8c6082013-12-30 14:44:08 -080040/**
41 * Singleton.
42 *
43 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
44 * access from other packages specifically refraining from passing the CallsManager instance
45 * beyond the com.android.telecomm package boundary.
46 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080047public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080048
Santos Cordona56f2762014-03-24 15:55:53 -070049 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070050 interface CallsManagerListener {
51 void onCallAdded(Call call);
52 void onCallRemoved(Call call);
53 void onCallStateChanged(Call call, CallState oldState, CallState newState);
54 void onIncomingCallAnswered(Call call);
55 void onIncomingCallRejected(Call call);
56 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070057 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 }
59
Santos Cordon8e8b8d22013-12-19 14:14:05 -080060 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080061
Santos Cordone3d76ab2014-01-28 17:25:20 -080062 private final Switchboard mSwitchboard;
63
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070065 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
66 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080067 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 private final Set<Call> mCalls = Sets.newLinkedHashSet();
Santos Cordon681663d2014-01-30 04:32:15 -080069
70 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070071 * The call the user is currently interacting with. This is the call that should have audio
72 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080073 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070074 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080075
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070076 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070077
Santos Cordona56f2762014-03-24 15:55:53 -070078 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070079
Evan Charltonf02e9882014-03-06 12:54:52 -080080 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080081
Evan Charltonf02e9882014-03-06 12:54:52 -080082 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Santos Cordon8e8b8d22013-12-19 14:14:05 -080084 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080085 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 */
87 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080088 mSwitchboard = new Switchboard(this);
Santos Cordona56f2762014-03-24 15:55:53 -070089
Sailesh Nepal810735e2014-03-18 18:15:46 -070090 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -070091
Santos Cordonc7b8eba2014-04-01 15:26:28 -070092 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
93
Santos Cordona56f2762014-03-24 15:55:53 -070094 mListeners.add(new CallLogManager(TelecommApp.getInstance()));
95 mListeners.add(new PhoneStateBroadcaster());
96 mListeners.add(new InCallController());
97 mListeners.add(new Ringer(mCallAudioManager));
Santos Cordonc7b8eba2014-04-01 15:26:28 -070098 mListeners.add(new RingbackPlayer(this, playerFactory));
99 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700100 mListeners.add(mCallAudioManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 }
102
Ben Gilada0d9f752014-02-26 11:49:03 -0800103 static CallsManager getInstance() {
104 return INSTANCE;
105 }
106
Sailesh Nepal810735e2014-03-18 18:15:46 -0700107 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700108 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700109 }
110
111 Call getForegroundCall() {
112 return mForegroundCall;
113 }
114
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700115 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700116 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700117 if (call.isEmergencyCall()) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124 CallAudioState getAudioState() {
125 return mCallAudioManager.getAudioState();
126 }
127
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800128 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800129 * Starts the incoming call sequence by having switchboard gather more information about the
130 * specified call; using the specified call service descriptor. Upon success, execution returns
131 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800132 *
Ben Giladc5b22692014-02-18 20:03:22 -0800133 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800134 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800135 */
Evan Charltona05805b2014-03-05 08:21:46 -0800136 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800137 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800138 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700139 // additional information from the call service, but for now we just need one to pass
140 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700141 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800142
Evan Charltona05805b2014-03-05 08:21:46 -0800143 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800144 }
145
146 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800147 * Validates the specified call and, upon no objection to connect it, adds the new call to the
148 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
149 *
150 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700151 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800152 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700153 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800154 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700155 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800156
Sailesh Nepalce704b92014-03-17 18:31:43 -0700157 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800158 ContactInfo contactInfo = call.getContactInfo();
159 for (IncomingCallValidator validator : mIncomingCallValidators) {
160 if (!validator.isValid(handle, contactInfo)) {
161 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800162 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800163 return;
164 }
165 }
166
167 // No objection to accept the incoming call, proceed with potentially connecting it (based
168 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700169 call.setHandle(callInfo.getHandle());
170 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800171 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700172 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800173
Sailesh Nepal810735e2014-03-18 18:15:46 -0700174 /**
175 * Called when an incoming call was not connected.
176 *
177 * @param call The incoming call.
178 */
179 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700180 // Incoming calls are not added unless they are successful. We set the state and disconnect
181 // cause just as a matter of good bookkeeping. We do not use the specific methods for
182 // setting those values so that we do not trigger CallsManagerListener events.
183 // TODO: Needs more specific disconnect error for this case.
184 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
185 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800186 }
187
188 /**
Yorke Lee33501632014-03-17 19:24:12 -0700189 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800190 *
Yorke Lee33501632014-03-17 19:24:12 -0700191 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800192 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700193 * @param gatewayInfo Optional gateway information that can be used to route the call to the
194 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800195 */
Yorke Lee33501632014-03-17 19:24:12 -0700196 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800197 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800198 if (!validator.isValid(handle, contactInfo)) {
199 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800200 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800201 return;
202 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800203 }
204
Yorke Lee33501632014-03-17 19:24:12 -0700205 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
206
207 if (gatewayInfo == null) {
208 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
209 } else {
210 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
211 Log.pii(uriHandle), Log.pii(handle));
212 }
213 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700214 setCallState(call, CallState.DIALING);
215 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800216 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800217 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800218
219 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700220 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800221 *
222 * @param call The new outgoing call.
223 */
224 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700225 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800226 }
227
228 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700229 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800230 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700231 * @param call The outgoing call.
232 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800233 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700234 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
235 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
236 if (isAborted) {
237 call.abort();
238 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700239 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700240 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700241 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700242 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700243 }
Yorke Leef98fb572014-03-05 10:56:55 -0800244 }
245
246 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800247 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
248 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
249 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800250 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700251 void answerCall(Call call) {
252 if (!mCalls.contains(call)) {
253 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800254 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700255 for (CallsManagerListener listener : mListeners) {
256 listener.onIncomingCallAnswered(call);
257 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800258
Santos Cordon61d0f702014-02-19 02:52:23 -0800259 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700260 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800261 call.answer();
262 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800263 }
264
265 /**
266 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
267 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
268 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800269 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700270 void rejectCall(Call call) {
271 if (!mCalls.contains(call)) {
272 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800273 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700274 for (CallsManagerListener listener : mListeners) {
275 listener.onIncomingCallRejected(call);
276 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800277 call.reject();
278 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800279 }
280
281 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700282 * Instructs Telecomm to play the specified DTMF tone within the specified call.
283 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700284 * @param digit The DTMF digit to play.
285 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700286 void playDtmfTone(Call call, char digit) {
287 if (!mCalls.contains(call)) {
288 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700289 } else {
290 call.playDtmfTone(digit);
291 }
292 }
293
294 /**
295 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700296 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700297 void stopDtmfTone(Call call) {
298 if (!mCalls.contains(call)) {
299 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700300 } else {
301 call.stopDtmfTone();
302 }
303 }
304
305 /**
306 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700307 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700308 void postDialContinue(Call call) {
309 if (!mCalls.contains(call)) {
310 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700311 } else {
312 // TODO(ihab): Implement this from this level on downwards
313 // call.postDialContinue();
314 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
315 }
316 }
317
318 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800319 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
320 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
321 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800322 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700323 void disconnectCall(Call call) {
324 if (!mCalls.contains(call)) {
325 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800326 } else {
327 call.disconnect();
328 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800329 }
Santos Cordon681663d2014-01-30 04:32:15 -0800330
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700331 /**
332 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
333 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
334 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700335 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700336 void holdCall(Call call) {
337 if (!mCalls.contains(call)) {
338 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700339 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700340 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700341 call.hold();
342 }
343 }
344
345 /**
346 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
347 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
348 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700349 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700350 void unholdCall(Call call) {
351 if (!mCalls.contains(call)) {
352 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700353 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700354 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700355 call.unhold();
356 }
357 }
358
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700359 /** Called by the in-call UI to change the mute state. */
360 void mute(boolean shouldMute) {
361 mCallAudioManager.mute(shouldMute);
362 }
363
364 /**
365 * Called by the in-call UI to change the audio route, for example to change from earpiece to
366 * speaker phone.
367 */
368 void setAudioRoute(int route) {
369 mCallAudioManager.setAudioRoute(route);
370 }
371
372 /** Called when the audio state changes. */
373 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
374 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700375 for (CallsManagerListener listener : mListeners) {
376 listener.onAudioStateChanged(oldAudioState, newAudioState);
377 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700378 }
379
Sailesh Nepale59bb192014-04-01 18:33:59 -0700380 void markCallAsRinging(Call call) {
381 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800382 }
383
Sailesh Nepale59bb192014-04-01 18:33:59 -0700384 void markCallAsDialing(Call call) {
385 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800386 }
387
Sailesh Nepale59bb192014-04-01 18:33:59 -0700388 void markCallAsActive(Call call) {
389 setCallState(call, CallState.ACTIVE);
Santos Cordon681663d2014-01-30 04:32:15 -0800390 }
391
Sailesh Nepale59bb192014-04-01 18:33:59 -0700392 void markCallAsOnHold(Call call) {
393 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700394 }
395
Santos Cordon049b7b62014-01-30 05:34:26 -0800396 /**
397 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
398 * live call, then also disconnect from the in-call controller.
399 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700400 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
401 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800402 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700403 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
404 call.setDisconnectCause(disconnectCause, disconnectMessage);
405 setCallState(call, CallState.DISCONNECTED);
406 removeCall(call);
Ben Gilada0d9f752014-02-26 11:49:03 -0800407 }
408
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700409 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
410 // TODO(sail): Implement this.
411 }
412
Ben Gilada0d9f752014-02-26 11:49:03 -0800413 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700414 * Cleans up any calls currently associated with the specified call service when the
415 * call-service binder disconnects unexpectedly.
416 *
417 * @param callService The call service that disconnected.
418 */
419 void handleCallServiceDeath(CallServiceWrapper callService) {
420 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700421 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700422 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700423 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700424 }
425 }
426 }
427
428 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800429 * Adds the specified call to the main list of live calls.
430 *
431 * @param call The call to add.
432 */
433 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700434 mCalls.add(call);
Santos Cordona56f2762014-03-24 15:55:53 -0700435 for (CallsManagerListener listener : mListeners) {
436 listener.onCallAdded(call);
437 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700438 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800439 }
440
Sailesh Nepal810735e2014-03-18 18:15:46 -0700441 private void removeCall(Call call) {
442 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700443 call.clearCallServiceSelector();
444
445 boolean shouldNotify = false;
446 if (mCalls.contains(call)) {
447 mCalls.remove(call);
448 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700449 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800450
Sailesh Nepale59bb192014-04-01 18:33:59 -0700451 // Only broadcast changes for calls that are being tracked.
452 if (shouldNotify) {
453 for (CallsManagerListener listener : mListeners) {
454 listener.onCallRemoved(call);
455 }
456 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700457 }
458 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800459
Sailesh Nepal810735e2014-03-18 18:15:46 -0700460 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700461 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700462 *
463 * @param call The call.
464 * @param newState The new state of the call.
465 */
466 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700467 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700468 CallState oldState = call.getState();
469 if (newState != oldState) {
470 // Unfortunately, in the telephony world the radio is king. So if the call notifies
471 // us that the call is in a particular state, we allow it even if it doesn't make
472 // sense (e.g., ACTIVE -> RINGING).
473 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
474 // into a well-defined state machine.
475 // TODO(santoscordon): Define expected state transitions here, and log when an
476 // unexpected transition occurs.
477 call.setState(newState);
478
479 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700480 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700481 for (CallsManagerListener listener : mListeners) {
482 listener.onCallStateChanged(call, oldState, newState);
483 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700484 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800485 }
486 }
487 }
488
489 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700490 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800491 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700492 private void updateForegroundCall() {
493 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700494 for (Call call : mCalls) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700495 // Incoming ringing calls have priority.
496 if (call.getState() == CallState.RINGING) {
497 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800498 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700499 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700500 if (call.isAlive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700501 newForegroundCall = call;
502 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800503 }
504 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800505
Sailesh Nepal810735e2014-03-18 18:15:46 -0700506 if (newForegroundCall != mForegroundCall) {
507 Call oldForegroundCall = mForegroundCall;
508 mForegroundCall = newForegroundCall;
509
Santos Cordona56f2762014-03-24 15:55:53 -0700510 for (CallsManagerListener listener : mListeners) {
511 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
512 }
Santos Cordon681663d2014-01-30 04:32:15 -0800513 }
514 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800515}