blob: c82b04d6e25d264bb569b1088d0f16301229c268 [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
92 mListeners.add(new CallLogManager(TelecommApp.getInstance()));
93 mListeners.add(new PhoneStateBroadcaster());
94 mListeners.add(new InCallController());
95 mListeners.add(new Ringer(mCallAudioManager));
96 mListeners.add(new RingbackPlayer(this, new InCallTonePlayer.Factory(mCallAudioManager)));
97 mListeners.add(mCallAudioManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 }
99
Ben Gilada0d9f752014-02-26 11:49:03 -0800100 static CallsManager getInstance() {
101 return INSTANCE;
102 }
103
Sailesh Nepal810735e2014-03-18 18:15:46 -0700104 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700105 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700106 }
107
108 Call getForegroundCall() {
109 return mForegroundCall;
110 }
111
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700112 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700113 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700114 if (call.isEmergencyCall()) {
115 return true;
116 }
117 }
118 return false;
119 }
120
121 CallAudioState getAudioState() {
122 return mCallAudioManager.getAudioState();
123 }
124
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800125 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800126 * Starts the incoming call sequence by having switchboard gather more information about the
127 * specified call; using the specified call service descriptor. Upon success, execution returns
128 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800129 *
Ben Giladc5b22692014-02-18 20:03:22 -0800130 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800131 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800132 */
Evan Charltona05805b2014-03-05 08:21:46 -0800133 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800134 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800135 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700136 // additional information from the call service, but for now we just need one to pass
137 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800139
Evan Charltona05805b2014-03-05 08:21:46 -0800140 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800141 }
142
143 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800144 * Validates the specified call and, upon no objection to connect it, adds the new call to the
145 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
146 *
147 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700148 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800149 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700150 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800151 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700152 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800153
Sailesh Nepalce704b92014-03-17 18:31:43 -0700154 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800155 ContactInfo contactInfo = call.getContactInfo();
156 for (IncomingCallValidator validator : mIncomingCallValidators) {
157 if (!validator.isValid(handle, contactInfo)) {
158 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800159 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800160 return;
161 }
162 }
163
164 // No objection to accept the incoming call, proceed with potentially connecting it (based
165 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700166 call.setHandle(callInfo.getHandle());
167 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800168 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700169 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800170
Sailesh Nepal810735e2014-03-18 18:15:46 -0700171 /**
172 * Called when an incoming call was not connected.
173 *
174 * @param call The incoming call.
175 */
176 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700177 // Incoming calls are not added unless they are successful. We set the state and disconnect
178 // cause just as a matter of good bookkeeping. We do not use the specific methods for
179 // setting those values so that we do not trigger CallsManagerListener events.
180 // TODO: Needs more specific disconnect error for this case.
181 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
182 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800183 }
184
185 /**
Yorke Lee33501632014-03-17 19:24:12 -0700186 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800187 *
Yorke Lee33501632014-03-17 19:24:12 -0700188 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800189 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700190 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700191 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800192 */
Yorke Lee33501632014-03-17 19:24:12 -0700193 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800194 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800195 if (!validator.isValid(handle, contactInfo)) {
196 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800197 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800198 return;
199 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800200 }
201
Yorke Lee33501632014-03-17 19:24:12 -0700202 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
203
204 if (gatewayInfo == null) {
205 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
206 } else {
207 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
208 Log.pii(uriHandle), Log.pii(handle));
209 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700210 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700211 setCallState(call, CallState.DIALING);
212 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800213 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800214 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800215
216 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700217 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800218 *
219 * @param call The new outgoing call.
220 */
221 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700222 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800223 }
224
225 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700226 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800227 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700228 * @param call The outgoing call.
229 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800230 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700231 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
232 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
233 if (isAborted) {
234 call.abort();
235 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700236 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700237 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700238 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700239 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700240 }
Yorke Leef98fb572014-03-05 10:56:55 -0800241 }
242
243 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800244 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
245 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
246 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800247 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700248 void answerCall(Call call) {
249 if (!mCalls.contains(call)) {
250 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800251 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700252 for (CallsManagerListener listener : mListeners) {
253 listener.onIncomingCallAnswered(call);
254 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800255
Santos Cordon61d0f702014-02-19 02:52:23 -0800256 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700257 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800258 call.answer();
259 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800260 }
261
262 /**
263 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
264 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
265 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800266 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700267 void rejectCall(Call call) {
268 if (!mCalls.contains(call)) {
269 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800270 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700271 for (CallsManagerListener listener : mListeners) {
272 listener.onIncomingCallRejected(call);
273 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800274 call.reject();
275 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800276 }
277
278 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700279 * Instructs Telecomm to play the specified DTMF tone within the specified call.
280 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700281 * @param digit The DTMF digit to play.
282 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700283 void playDtmfTone(Call call, char digit) {
284 if (!mCalls.contains(call)) {
285 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700286 } else {
287 call.playDtmfTone(digit);
288 }
289 }
290
291 /**
292 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700293 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700294 void stopDtmfTone(Call call) {
295 if (!mCalls.contains(call)) {
296 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700297 } else {
298 call.stopDtmfTone();
299 }
300 }
301
302 /**
303 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700304 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700305 void postDialContinue(Call call) {
306 if (!mCalls.contains(call)) {
307 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700308 } else {
309 // TODO(ihab): Implement this from this level on downwards
310 // call.postDialContinue();
311 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
312 }
313 }
314
315 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800316 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
317 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
318 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800319 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700320 void disconnectCall(Call call) {
321 if (!mCalls.contains(call)) {
322 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800323 } else {
324 call.disconnect();
325 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800326 }
Santos Cordon681663d2014-01-30 04:32:15 -0800327
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700328 /**
329 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
330 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
331 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700332 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700333 void holdCall(Call call) {
334 if (!mCalls.contains(call)) {
335 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700336 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700337 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700338 call.hold();
339 }
340 }
341
342 /**
343 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
344 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
345 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700346 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700347 void unholdCall(Call call) {
348 if (!mCalls.contains(call)) {
349 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700350 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700351 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700352 call.unhold();
353 }
354 }
355
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700356 /** Called by the in-call UI to change the mute state. */
357 void mute(boolean shouldMute) {
358 mCallAudioManager.mute(shouldMute);
359 }
360
361 /**
362 * Called by the in-call UI to change the audio route, for example to change from earpiece to
363 * speaker phone.
364 */
365 void setAudioRoute(int route) {
366 mCallAudioManager.setAudioRoute(route);
367 }
368
369 /** Called when the audio state changes. */
370 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
371 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700372 for (CallsManagerListener listener : mListeners) {
373 listener.onAudioStateChanged(oldAudioState, newAudioState);
374 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700375 }
376
Sailesh Nepale59bb192014-04-01 18:33:59 -0700377 void markCallAsRinging(Call call) {
378 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800379 }
380
Sailesh Nepale59bb192014-04-01 18:33:59 -0700381 void markCallAsDialing(Call call) {
382 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800383 }
384
Sailesh Nepale59bb192014-04-01 18:33:59 -0700385 void markCallAsActive(Call call) {
386 setCallState(call, CallState.ACTIVE);
Santos Cordon681663d2014-01-30 04:32:15 -0800387 }
388
Sailesh Nepale59bb192014-04-01 18:33:59 -0700389 void markCallAsOnHold(Call call) {
390 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700391 }
392
Santos Cordon049b7b62014-01-30 05:34:26 -0800393 /**
394 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
395 * live call, then also disconnect from the in-call controller.
396 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700397 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
398 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800399 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700400 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
401 call.setDisconnectCause(disconnectCause, disconnectMessage);
402 setCallState(call, CallState.DISCONNECTED);
403 removeCall(call);
Ben Gilada0d9f752014-02-26 11:49:03 -0800404 }
405
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700406 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
407 // TODO(sail): Implement this.
408 }
409
Ben Gilada0d9f752014-02-26 11:49:03 -0800410 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700411 * Cleans up any calls currently associated with the specified call service when the
412 * call-service binder disconnects unexpectedly.
413 *
414 * @param callService The call service that disconnected.
415 */
416 void handleCallServiceDeath(CallServiceWrapper callService) {
417 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700419 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700420 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700421 }
422 }
423 }
424
425 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800426 * Adds the specified call to the main list of live calls.
427 *
428 * @param call The call to add.
429 */
430 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700431 mCalls.add(call);
Santos Cordona56f2762014-03-24 15:55:53 -0700432 for (CallsManagerListener listener : mListeners) {
433 listener.onCallAdded(call);
434 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700435 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800436 }
437
Sailesh Nepal810735e2014-03-18 18:15:46 -0700438 private void removeCall(Call call) {
439 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700440 call.clearCallServiceSelector();
441
442 boolean shouldNotify = false;
443 if (mCalls.contains(call)) {
444 mCalls.remove(call);
445 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700446 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800447
Sailesh Nepale59bb192014-04-01 18:33:59 -0700448 // Only broadcast changes for calls that are being tracked.
449 if (shouldNotify) {
450 for (CallsManagerListener listener : mListeners) {
451 listener.onCallRemoved(call);
452 }
453 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700454 }
455 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800456
Sailesh Nepal810735e2014-03-18 18:15:46 -0700457 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700458 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700459 *
460 * @param call The call.
461 * @param newState The new state of the call.
462 */
463 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700464 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700465 CallState oldState = call.getState();
466 if (newState != oldState) {
467 // Unfortunately, in the telephony world the radio is king. So if the call notifies
468 // us that the call is in a particular state, we allow it even if it doesn't make
469 // sense (e.g., ACTIVE -> RINGING).
470 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
471 // into a well-defined state machine.
472 // TODO(santoscordon): Define expected state transitions here, and log when an
473 // unexpected transition occurs.
474 call.setState(newState);
475
476 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700477 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700478 for (CallsManagerListener listener : mListeners) {
479 listener.onCallStateChanged(call, oldState, newState);
480 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700481 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800482 }
483 }
484 }
485
486 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700487 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800488 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700489 private void updateForegroundCall() {
490 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700491 for (Call call : mCalls) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700492 // Incoming ringing calls have priority.
493 if (call.getState() == CallState.RINGING) {
494 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800495 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700496 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700497 if (call.isAlive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700498 newForegroundCall = call;
499 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800500 }
501 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800502
Sailesh Nepal810735e2014-03-18 18:15:46 -0700503 if (newForegroundCall != mForegroundCall) {
504 Call oldForegroundCall = mForegroundCall;
505 mForegroundCall = newForegroundCall;
506
Santos Cordona56f2762014-03-24 15:55:53 -0700507 for (CallsManagerListener listener : mListeners) {
508 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
509 }
Santos Cordon681663d2014-01-30 04:32:15 -0800510 }
511 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800512}