blob: 85b9340c0f2f87528706d51ff83c2a7c14a97391 [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;
Sailesh Nepal810735e2014-03-18 18:15:46 -070029import com.google.common.collect.ImmutableCollection;
30import com.google.common.collect.ImmutableList;
Santos Cordona56f2762014-03-24 15:55:53 -070031import com.google.common.collect.Sets;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordona56f2762014-03-24 15:55:53 -070033import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Ben Giladdd8c6082013-12-30 14:44:08 -080035/**
36 * Singleton.
37 *
38 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
39 * access from other packages specifically refraining from passing the CallsManager instance
40 * beyond the com.android.telecomm package boundary.
41 */
Santos Cordon766d04f2014-05-06 10:28:25 -070042public final class CallsManager implements Call.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080043
Santos Cordon74d420b2014-05-07 14:38:47 -070044 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070045 interface CallsManagerListener {
46 void onCallAdded(Call call);
47 void onCallRemoved(Call call);
48 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070049 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
50 void onCallServiceChanged(
51 Call call,
52 CallServiceWrapper oldCallService,
53 CallServiceWrapper newCallService);
54 void onCallHandoffCallServiceDescriptorChanged(
55 Call call,
56 CallServiceDescriptor oldDescriptor,
57 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 void onIncomingCallAnswered(Call call);
59 void onIncomingCallRejected(Call call);
60 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070061 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070062 }
63
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080065
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070067 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
68 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080069 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070070 private final Set<Call> mCalls = Sets.newLinkedHashSet();
Santos Cordon681663d2014-01-30 04:32:15 -080071
72 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070073 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
74 * and removed when hadnoff is complete.
75 */
76 private final Set<Call> mPendingHandoffCalls = Sets.newLinkedHashSet();
77
78 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070079 * The call the user is currently interacting with. This is the call that should have audio
80 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080081 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070082 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080083
Santos Cordon92a2d812014-04-30 15:19:01 -070084 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
85
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070086 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070087
Santos Cordona56f2762014-03-24 15:55:53 -070088 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070089
Santos Cordon766d04f2014-05-06 10:28:25 -070090 /** Singleton accessor. */
91 static CallsManager getInstance() {
92 return INSTANCE;
93 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080094
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080096 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080097 */
98 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -070099 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700100
Sailesh Nepal810735e2014-03-18 18:15:46 -0700101 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -0700102
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700103 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700104 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700105 mListeners.add(new PhoneStateBroadcaster());
106 mListeners.add(new InCallController());
Evan Charlton893f9e32014-04-09 08:51:52 -0700107 mListeners.add(new Ringer(mCallAudioManager, this, playerFactory, app));
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700108 mListeners.add(new RingbackPlayer(this, playerFactory));
109 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700110 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700111 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700112 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800113 }
114
Santos Cordon766d04f2014-05-06 10:28:25 -0700115 @Override
116 public void onSuccessfulOutgoingCall(Call call) {
117 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
118 if (mCalls.contains(call)) {
119 // The call's CallService has been updated.
120 for (CallsManagerListener listener : mListeners) {
121 listener.onCallServiceChanged(call, null, call.getCallService());
122 }
123 } else if (mPendingHandoffCalls.contains(call)) {
124 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
125 call.getCallService().getDescriptor());
126 }
127 }
128
129 @Override
130 public void onFailedOutgoingCall(Call call, boolean isAborted) {
131 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
132 if (isAborted) {
133 setCallState(call, CallState.ABORTED);
134 removeCall(call);
135 } else {
136 // TODO: Replace disconnect cause with more specific disconnect causes.
137 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
138 }
139 }
140
141 @Override
142 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
143 Log.d(this, "onSuccessfulIncomingCall");
144 setCallState(call, callInfo.getState());
145 addCall(call);
146 }
147
148 @Override
149 public void onFailedIncomingCall(Call call) {
150 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800151 }
152
Sailesh Nepal810735e2014-03-18 18:15:46 -0700153 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700154 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700155 }
156
157 Call getForegroundCall() {
158 return mForegroundCall;
159 }
160
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700161 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700162 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700163 if (call.isEmergencyCall()) {
164 return true;
165 }
166 }
167 return false;
168 }
169
170 CallAudioState getAudioState() {
171 return mCallAudioManager.getAudioState();
172 }
173
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800174 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800175 * Starts the incoming call sequence by having switchboard gather more information about the
176 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700177 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800178 *
Ben Giladc5b22692014-02-18 20:03:22 -0800179 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800180 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800181 */
Evan Charltona05805b2014-03-05 08:21:46 -0800182 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800183 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800184 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700185 // additional information from the call service, but for now we just need one to pass
186 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700187 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700188 // TODO(santoscordon): Move this to be a part of addCall()
189 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800190
Santos Cordon766d04f2014-05-06 10:28:25 -0700191 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800192 }
193
194 /**
Yorke Lee33501632014-03-17 19:24:12 -0700195 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800196 *
Yorke Lee33501632014-03-17 19:24:12 -0700197 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800198 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700199 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700200 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800201 */
Yorke Lee33501632014-03-17 19:24:12 -0700202 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700203 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
204
205 if (gatewayInfo == null) {
206 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
207 } else {
208 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
209 Log.pii(uriHandle), Log.pii(handle));
210 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700211
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700212 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700213
214 // TODO(santoscordon): Move this to be a part of addCall()
215 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700216 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217
Santos Cordon766d04f2014-05-06 10:28:25 -0700218 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800219 }
220
221 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800222 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
223 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
224 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800225 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700226 void answerCall(Call call) {
227 if (!mCalls.contains(call)) {
228 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800229 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700230 // If the foreground call is not the ringing call and it is currently isActive() or
231 // DIALING, put it on hold before answering the call.
232 if (mForegroundCall != null && mForegroundCall != call &&
233 (mForegroundCall.isActive() ||
234 mForegroundCall.getState() == CallState.DIALING)) {
235 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
236 mForegroundCall, call);
237 mForegroundCall.hold();
238 // TODO(santoscordon): Wait until we get confirmation of the active call being
239 // on-hold before answering the new call.
240 // TODO(santoscordon): Import logic from CallManager.acceptCall()
241 }
242
Santos Cordona56f2762014-03-24 15:55:53 -0700243 for (CallsManagerListener listener : mListeners) {
244 listener.onIncomingCallAnswered(call);
245 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800246
Santos Cordon61d0f702014-02-19 02:52:23 -0800247 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700248 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800249 call.answer();
250 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800251 }
252
253 /**
254 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
255 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
256 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800257 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700258 void rejectCall(Call call) {
259 if (!mCalls.contains(call)) {
260 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800261 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700262 for (CallsManagerListener listener : mListeners) {
263 listener.onIncomingCallRejected(call);
264 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800265 call.reject();
266 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800267 }
268
269 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700270 * Instructs Telecomm to play the specified DTMF tone within the specified call.
271 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700272 * @param digit The DTMF digit to play.
273 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700274 void playDtmfTone(Call call, char digit) {
275 if (!mCalls.contains(call)) {
276 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700277 } else {
278 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700279 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700280 }
281 }
282
283 /**
284 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700285 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700286 void stopDtmfTone(Call call) {
287 if (!mCalls.contains(call)) {
288 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700289 } else {
290 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700291 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700292 }
293 }
294
295 /**
296 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700297 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700298 void postDialContinue(Call call) {
299 if (!mCalls.contains(call)) {
300 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700301 } else {
302 // TODO(ihab): Implement this from this level on downwards
303 // call.postDialContinue();
304 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
305 }
306 }
307
308 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800309 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
310 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
311 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800312 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700313 void disconnectCall(Call call) {
314 if (!mCalls.contains(call)) {
315 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800316 } else {
317 call.disconnect();
318 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800319 }
Santos Cordon681663d2014-01-30 04:32:15 -0800320
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700321 /**
322 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
323 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
324 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700325 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700326 void holdCall(Call call) {
327 if (!mCalls.contains(call)) {
328 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700329 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700330 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700331 call.hold();
332 }
333 }
334
335 /**
336 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
337 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
338 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700339 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700340 void unholdCall(Call call) {
341 if (!mCalls.contains(call)) {
342 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700343 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700344 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700345 call.unhold();
346 }
347 }
348
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700349 /** Called by the in-call UI to change the mute state. */
350 void mute(boolean shouldMute) {
351 mCallAudioManager.mute(shouldMute);
352 }
353
354 /**
355 * Called by the in-call UI to change the audio route, for example to change from earpiece to
356 * speaker phone.
357 */
358 void setAudioRoute(int route) {
359 mCallAudioManager.setAudioRoute(route);
360 }
361
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700362 void startHandoffForCall(Call originalCall) {
363 if (!mCalls.contains(originalCall)) {
364 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
365 return;
366 }
367
368 for (Call handoffCall : mPendingHandoffCalls) {
369 if (handoffCall.getOriginalCall() == originalCall) {
370 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
371 return;
372 }
373 }
374
375 // Create a new call to be placed in the background. If handoff is successful then the
376 // original call will live on but its state will be updated to the new call's state. In
377 // particular the original call's call service will be updated to the new call's call
378 // service.
379 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
380 originalCall.getGatewayInfo(), false);
381 tempCall.setOriginalCall(originalCall);
382 tempCall.setExtras(originalCall.getExtras());
383 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
384 mPendingHandoffCalls.add(tempCall);
385 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700386 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700387 }
388
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700389 /** Called when the audio state changes. */
390 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
391 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700392 for (CallsManagerListener listener : mListeners) {
393 listener.onAudioStateChanged(oldAudioState, newAudioState);
394 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700395 }
396
Sailesh Nepale59bb192014-04-01 18:33:59 -0700397 void markCallAsRinging(Call call) {
398 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800399 }
400
Sailesh Nepale59bb192014-04-01 18:33:59 -0700401 void markCallAsDialing(Call call) {
402 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800403 }
404
Sailesh Nepale59bb192014-04-01 18:33:59 -0700405 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700406 if (call.getConnectTimeMillis() == 0) {
407 call.setConnectTimeMillis(System.currentTimeMillis());
408 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700409 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700410
411 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700412 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700413 }
Santos Cordon681663d2014-01-30 04:32:15 -0800414 }
415
Sailesh Nepale59bb192014-04-01 18:33:59 -0700416 void markCallAsOnHold(Call call) {
417 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700418 }
419
Santos Cordon049b7b62014-01-30 05:34:26 -0800420 /**
421 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
422 * live call, then also disconnect from the in-call controller.
423 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700424 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
425 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800426 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700427 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
428 call.setDisconnectCause(disconnectCause, disconnectMessage);
429 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700430
431 // Only remove the call if handoff is not pending.
432 if (call.getHandoffCallServiceDescriptor() == null) {
433 removeCall(call);
434 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800435 }
436
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700437 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700438 if (!mCalls.contains(call)) {
439 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
440 return;
441 }
442
443 if (extras == null) {
444 call.setExtras(Bundle.EMPTY);
445 } else {
446 call.setExtras(extras);
447 }
448
449 Uri oldHandle = call.getHandoffHandle();
450 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
451 if (!areUriEqual(oldHandle, handle)) {
452 call.setHandoffHandle(handle);
453 for (CallsManagerListener listener : mListeners) {
454 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
455 }
456 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700457 }
458
Ben Gilada0d9f752014-02-26 11:49:03 -0800459 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700460 * Cleans up any calls currently associated with the specified call service when the
461 * call-service binder disconnects unexpectedly.
462 *
463 * @param callService The call service that disconnected.
464 */
465 void handleCallServiceDeath(CallServiceWrapper callService) {
466 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700467 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700468 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700469 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700470 }
471 }
472 }
473
474 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800475 * Adds the specified call to the main list of live calls.
476 *
477 * @param call The call to add.
478 */
479 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700480 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700481
482 // TODO(santoscordon): Update mForegroundCall prior to invoking
483 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700484 for (CallsManagerListener listener : mListeners) {
485 listener.onCallAdded(call);
486 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700487 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800488 }
489
Sailesh Nepal810735e2014-03-18 18:15:46 -0700490 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700491 // If a handoff is pending then the original call shouldn't be removed.
492 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700493 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700494
Santos Cordon766d04f2014-05-06 10:28:25 -0700495 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700496 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700497 call.clearCallServiceSelector();
498
499 boolean shouldNotify = false;
500 if (mCalls.contains(call)) {
501 mCalls.remove(call);
502 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700503 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700504 Log.v(this, "removeCall, marking handoff call as failed");
505 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700506 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800507
Sailesh Nepale59bb192014-04-01 18:33:59 -0700508 // Only broadcast changes for calls that are being tracked.
509 if (shouldNotify) {
510 for (CallsManagerListener listener : mListeners) {
511 listener.onCallRemoved(call);
512 }
513 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700514 }
515 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800516
Sailesh Nepal810735e2014-03-18 18:15:46 -0700517 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700518 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700519 *
520 * @param call The call.
521 * @param newState The new state of the call.
522 */
523 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700524 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700525 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700526 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700527 if (newState != oldState) {
528 // Unfortunately, in the telephony world the radio is king. So if the call notifies
529 // us that the call is in a particular state, we allow it even if it doesn't make
530 // sense (e.g., ACTIVE -> RINGING).
531 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
532 // into a well-defined state machine.
533 // TODO(santoscordon): Define expected state transitions here, and log when an
534 // unexpected transition occurs.
535 call.setState(newState);
536
537 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700538 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700539 for (CallsManagerListener listener : mListeners) {
540 listener.onCallStateChanged(call, oldState, newState);
541 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700542 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800543 }
544 }
545 }
546
547 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700548 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800549 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700550 private void updateForegroundCall() {
551 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700552 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700553 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
554 // of its state will be foreground by default and instead the call service should be
555 // notified when its calls enter and exit foreground state. Foreground will mean that
556 // the call should play audio and listen to microphone if it wants.
557
558 // Active calls have priority.
559 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700560 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800561 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700562 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700563
564 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700565 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700566 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800567 }
568 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800569
Sailesh Nepal810735e2014-03-18 18:15:46 -0700570 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700571 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700572 Call oldForegroundCall = mForegroundCall;
573 mForegroundCall = newForegroundCall;
574
Santos Cordona56f2762014-03-24 15:55:53 -0700575 for (CallsManagerListener listener : mListeners) {
576 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
577 }
Santos Cordon681663d2014-01-30 04:32:15 -0800578 }
579 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700580
Sailesh Nepal4857f472014-04-07 22:26:27 -0700581 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700582 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700583 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
584 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700585
Sailesh Nepal4857f472014-04-07 22:26:27 -0700586 // Remove the transient handoff call object (don't disconnect because the call could still
587 // be live).
588 mPendingHandoffCalls.remove(handoffCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700589
Sailesh Nepal4857f472014-04-07 22:26:27 -0700590 if (wasSuccessful) {
591 // Disconnect.
592 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700593
Sailesh Nepal4857f472014-04-07 22:26:27 -0700594 // Synchronize.
595 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
596 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700597
Sailesh Nepal4857f472014-04-07 22:26:27 -0700598 // Force the foreground call changed notification to be sent.
599 for (CallsManagerListener listener : mListeners) {
600 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
601 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700602
Sailesh Nepal4857f472014-04-07 22:26:27 -0700603 updateHandoffCallServiceDescriptor(originalCall, null);
604 } else {
605 updateHandoffCallServiceDescriptor(originalCall, null);
606 if (originalCall.getState() == CallState.DISCONNECTED ||
607 originalCall.getState() == CallState.ABORTED) {
608 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700609 }
610 }
611 }
612
Sailesh Nepal4857f472014-04-07 22:26:27 -0700613 private void updateHandoffCallServiceDescriptor(
614 Call originalCall,
615 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700616 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700617 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
618 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700619
620 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
621 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
622 for (CallsManagerListener listener : mListeners) {
623 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
624 newDescriptor);
625 }
626 }
627 }
628
629 private static boolean areDescriptorsEqual(
630 CallServiceDescriptor descriptor1,
631 CallServiceDescriptor descriptor2) {
632 if (descriptor1 == null) {
633 return descriptor2 == null;
634 }
635 return descriptor1.equals(descriptor2);
636 }
637
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700638 private static boolean areUriEqual(Uri handle1, Uri handle2) {
639 if (handle1 == null) {
640 return handle2 == null;
641 }
642 return handle1.equals(handle2);
643 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800644}