blob: 48aa5255ef28b3238ef99f23036d232c158b5834 [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 Cordonae193062014-05-21 21:21:49 -070088 private final Ringer mRinger;
89
Santos Cordona56f2762014-03-24 15:55:53 -070090 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070091
Santos Cordon766d04f2014-05-06 10:28:25 -070092 /** Singleton accessor. */
93 static CallsManager getInstance() {
94 return INSTANCE;
95 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080096
Santos Cordon8e8b8d22013-12-19 14:14:05 -080097 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080098 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 */
100 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700101 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700102
Sailesh Nepal810735e2014-03-18 18:15:46 -0700103 mCallAudioManager = new CallAudioManager();
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700104 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700105 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
106
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700107 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700108 mListeners.add(new PhoneStateBroadcaster());
109 mListeners.add(new InCallController());
Santos Cordonae193062014-05-21 21:21:49 -0700110 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700111 mListeners.add(new RingbackPlayer(this, playerFactory));
112 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700113 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700114 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700115 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800116 }
117
Santos Cordon766d04f2014-05-06 10:28:25 -0700118 @Override
119 public void onSuccessfulOutgoingCall(Call call) {
120 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
121 if (mCalls.contains(call)) {
122 // The call's CallService has been updated.
123 for (CallsManagerListener listener : mListeners) {
124 listener.onCallServiceChanged(call, null, call.getCallService());
125 }
126 } else if (mPendingHandoffCalls.contains(call)) {
127 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
128 call.getCallService().getDescriptor());
129 }
130 }
131
132 @Override
133 public void onFailedOutgoingCall(Call call, boolean isAborted) {
134 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
135 if (isAborted) {
136 setCallState(call, CallState.ABORTED);
137 removeCall(call);
138 } else {
139 // TODO: Replace disconnect cause with more specific disconnect causes.
140 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
141 }
142 }
143
144 @Override
145 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
146 Log.d(this, "onSuccessfulIncomingCall");
147 setCallState(call, callInfo.getState());
148 addCall(call);
149 }
150
151 @Override
152 public void onFailedIncomingCall(Call call) {
153 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800154 }
155
Sailesh Nepal810735e2014-03-18 18:15:46 -0700156 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700157 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700158 }
159
160 Call getForegroundCall() {
161 return mForegroundCall;
162 }
163
Santos Cordonae193062014-05-21 21:21:49 -0700164 Ringer getRinger() {
165 return mRinger;
166 }
167
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700168 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700169 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700170 if (call.isEmergencyCall()) {
171 return true;
172 }
173 }
174 return false;
175 }
176
177 CallAudioState getAudioState() {
178 return mCallAudioManager.getAudioState();
179 }
180
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800181 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800182 * Starts the incoming call sequence by having switchboard gather more information about the
183 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700184 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800185 *
Ben Giladc5b22692014-02-18 20:03:22 -0800186 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800187 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800188 */
Evan Charltona05805b2014-03-05 08:21:46 -0800189 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800190 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800191 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700192 // additional information from the call service, but for now we just need one to pass
193 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700194 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700195 // TODO(santoscordon): Move this to be a part of addCall()
196 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800197
Santos Cordon766d04f2014-05-06 10:28:25 -0700198 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800199 }
200
201 /**
Yorke Lee33501632014-03-17 19:24:12 -0700202 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800203 *
Yorke Lee33501632014-03-17 19:24:12 -0700204 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800205 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700206 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700207 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800208 */
Yorke Lee33501632014-03-17 19:24:12 -0700209 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700210 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
211
212 if (gatewayInfo == null) {
213 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
214 } else {
215 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
216 Log.pii(uriHandle), Log.pii(handle));
217 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700218
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700219 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700220
221 // TODO(santoscordon): Move this to be a part of addCall()
222 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700223 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800224
Santos Cordon766d04f2014-05-06 10:28:25 -0700225 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800226 }
227
228 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800229 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
230 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
231 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800232 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700233 void answerCall(Call call) {
234 if (!mCalls.contains(call)) {
235 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800236 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700237 // If the foreground call is not the ringing call and it is currently isActive() or
238 // DIALING, put it on hold before answering the call.
239 if (mForegroundCall != null && mForegroundCall != call &&
240 (mForegroundCall.isActive() ||
241 mForegroundCall.getState() == CallState.DIALING)) {
242 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
243 mForegroundCall, call);
244 mForegroundCall.hold();
245 // TODO(santoscordon): Wait until we get confirmation of the active call being
246 // on-hold before answering the new call.
247 // TODO(santoscordon): Import logic from CallManager.acceptCall()
248 }
249
Santos Cordona56f2762014-03-24 15:55:53 -0700250 for (CallsManagerListener listener : mListeners) {
251 listener.onIncomingCallAnswered(call);
252 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800253
Santos Cordon61d0f702014-02-19 02:52:23 -0800254 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700255 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800256 call.answer();
257 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800258 }
259
260 /**
261 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
262 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
263 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800264 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700265 void rejectCall(Call call) {
266 if (!mCalls.contains(call)) {
267 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800268 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700269 for (CallsManagerListener listener : mListeners) {
270 listener.onIncomingCallRejected(call);
271 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800272 call.reject();
273 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800274 }
275
276 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700277 * Instructs Telecomm to play the specified DTMF tone within the specified call.
278 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700279 * @param digit The DTMF digit to play.
280 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700281 void playDtmfTone(Call call, char digit) {
282 if (!mCalls.contains(call)) {
283 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700284 } else {
285 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700286 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700287 }
288 }
289
290 /**
291 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700292 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700293 void stopDtmfTone(Call call) {
294 if (!mCalls.contains(call)) {
295 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700296 } else {
297 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700298 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700299 }
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) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700321 Log.v(this, "disconnectCall %s", call);
322
Sailesh Nepale59bb192014-04-01 18:33:59 -0700323 if (!mCalls.contains(call)) {
324 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800325 } else {
326 call.disconnect();
327 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800328 }
Santos Cordon681663d2014-01-30 04:32:15 -0800329
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700330 /**
331 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
332 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
333 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700334 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700335 void holdCall(Call call) {
336 if (!mCalls.contains(call)) {
337 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700338 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700339 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700340 call.hold();
341 }
342 }
343
344 /**
345 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
346 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
347 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700348 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700349 void unholdCall(Call call) {
350 if (!mCalls.contains(call)) {
351 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700352 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700353 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700354 call.unhold();
355 }
356 }
357
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700358 /** Called by the in-call UI to change the mute state. */
359 void mute(boolean shouldMute) {
360 mCallAudioManager.mute(shouldMute);
361 }
362
363 /**
364 * Called by the in-call UI to change the audio route, for example to change from earpiece to
365 * speaker phone.
366 */
367 void setAudioRoute(int route) {
368 mCallAudioManager.setAudioRoute(route);
369 }
370
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700371 void startHandoffForCall(Call originalCall) {
372 if (!mCalls.contains(originalCall)) {
373 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
374 return;
375 }
376
377 for (Call handoffCall : mPendingHandoffCalls) {
378 if (handoffCall.getOriginalCall() == originalCall) {
379 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
380 return;
381 }
382 }
383
384 // Create a new call to be placed in the background. If handoff is successful then the
385 // original call will live on but its state will be updated to the new call's state. In
386 // particular the original call's call service will be updated to the new call's call
387 // service.
388 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
389 originalCall.getGatewayInfo(), false);
390 tempCall.setOriginalCall(originalCall);
391 tempCall.setExtras(originalCall.getExtras());
392 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
393 mPendingHandoffCalls.add(tempCall);
394 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700395 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700396 }
397
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700398 /** Called when the audio state changes. */
399 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
400 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700401 for (CallsManagerListener listener : mListeners) {
402 listener.onAudioStateChanged(oldAudioState, newAudioState);
403 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700404 }
405
Sailesh Nepale59bb192014-04-01 18:33:59 -0700406 void markCallAsRinging(Call call) {
407 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800408 }
409
Sailesh Nepale59bb192014-04-01 18:33:59 -0700410 void markCallAsDialing(Call call) {
411 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800412 }
413
Sailesh Nepale59bb192014-04-01 18:33:59 -0700414 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700415 if (call.getConnectTimeMillis() == 0) {
416 call.setConnectTimeMillis(System.currentTimeMillis());
417 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700419
420 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700421 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700422 }
Santos Cordon681663d2014-01-30 04:32:15 -0800423 }
424
Sailesh Nepale59bb192014-04-01 18:33:59 -0700425 void markCallAsOnHold(Call call) {
426 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700427 }
428
Santos Cordon049b7b62014-01-30 05:34:26 -0800429 /**
430 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
431 * live call, then also disconnect from the in-call controller.
432 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700433 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
434 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800435 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700436 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
437 call.setDisconnectCause(disconnectCause, disconnectMessage);
438 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700439
440 // Only remove the call if handoff is not pending.
441 if (call.getHandoffCallServiceDescriptor() == null) {
442 removeCall(call);
443 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800444 }
445
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700446 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700447 if (!mCalls.contains(call)) {
448 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
449 return;
450 }
451
452 if (extras == null) {
453 call.setExtras(Bundle.EMPTY);
454 } else {
455 call.setExtras(extras);
456 }
457
458 Uri oldHandle = call.getHandoffHandle();
459 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
460 if (!areUriEqual(oldHandle, handle)) {
461 call.setHandoffHandle(handle);
462 for (CallsManagerListener listener : mListeners) {
463 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
464 }
465 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700466 }
467
Ben Gilada0d9f752014-02-26 11:49:03 -0800468 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700469 * Cleans up any calls currently associated with the specified call service when the
470 * call-service binder disconnects unexpectedly.
471 *
472 * @param callService The call service that disconnected.
473 */
474 void handleCallServiceDeath(CallServiceWrapper callService) {
475 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700476 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700477 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700478 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700479 }
480 }
481 }
482
483 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800484 * Adds the specified call to the main list of live calls.
485 *
486 * @param call The call to add.
487 */
488 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700489 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700490
491 // TODO(santoscordon): Update mForegroundCall prior to invoking
492 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700493 for (CallsManagerListener listener : mListeners) {
494 listener.onCallAdded(call);
495 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700496 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800497 }
498
Sailesh Nepal810735e2014-03-18 18:15:46 -0700499 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700500 // If a handoff is pending then the original call shouldn't be removed.
501 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700502 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700503
Santos Cordon766d04f2014-05-06 10:28:25 -0700504 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700505 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700506 call.clearCallServiceSelector();
507
508 boolean shouldNotify = false;
509 if (mCalls.contains(call)) {
510 mCalls.remove(call);
511 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700512 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700513 Log.v(this, "removeCall, marking handoff call as failed");
514 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700515 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800516
Sailesh Nepale59bb192014-04-01 18:33:59 -0700517 // Only broadcast changes for calls that are being tracked.
518 if (shouldNotify) {
519 for (CallsManagerListener listener : mListeners) {
520 listener.onCallRemoved(call);
521 }
522 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700523 }
524 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800525
Sailesh Nepal810735e2014-03-18 18:15:46 -0700526 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700527 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700528 *
529 * @param call The call.
530 * @param newState The new state of the call.
531 */
532 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700533 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700534 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700535 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700536 if (newState != oldState) {
537 // Unfortunately, in the telephony world the radio is king. So if the call notifies
538 // us that the call is in a particular state, we allow it even if it doesn't make
539 // sense (e.g., ACTIVE -> RINGING).
540 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
541 // into a well-defined state machine.
542 // TODO(santoscordon): Define expected state transitions here, and log when an
543 // unexpected transition occurs.
544 call.setState(newState);
545
546 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700547 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700548 for (CallsManagerListener listener : mListeners) {
549 listener.onCallStateChanged(call, oldState, newState);
550 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700551 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800552 }
553 }
554 }
555
556 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700557 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800558 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700559 private void updateForegroundCall() {
560 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700561 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700562 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
563 // of its state will be foreground by default and instead the call service should be
564 // notified when its calls enter and exit foreground state. Foreground will mean that
565 // the call should play audio and listen to microphone if it wants.
566
567 // Active calls have priority.
568 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700569 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800570 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700571 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700572
573 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700574 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700575 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800576 }
577 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800578
Sailesh Nepal810735e2014-03-18 18:15:46 -0700579 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700580 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700581 Call oldForegroundCall = mForegroundCall;
582 mForegroundCall = newForegroundCall;
583
Santos Cordona56f2762014-03-24 15:55:53 -0700584 for (CallsManagerListener listener : mListeners) {
585 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
586 }
Santos Cordon681663d2014-01-30 04:32:15 -0800587 }
588 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700589
Sailesh Nepal4857f472014-04-07 22:26:27 -0700590 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700591 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700592 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
593 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700594
Sailesh Nepal4857f472014-04-07 22:26:27 -0700595 // Remove the transient handoff call object (don't disconnect because the call could still
596 // be live).
597 mPendingHandoffCalls.remove(handoffCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700598
Sailesh Nepal4857f472014-04-07 22:26:27 -0700599 if (wasSuccessful) {
600 // Disconnect.
601 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700602
Sailesh Nepal4857f472014-04-07 22:26:27 -0700603 // Synchronize.
604 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
605 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700606
Sailesh Nepal4857f472014-04-07 22:26:27 -0700607 // Force the foreground call changed notification to be sent.
608 for (CallsManagerListener listener : mListeners) {
609 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
610 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700611
Sailesh Nepal4857f472014-04-07 22:26:27 -0700612 updateHandoffCallServiceDescriptor(originalCall, null);
613 } else {
614 updateHandoffCallServiceDescriptor(originalCall, null);
615 if (originalCall.getState() == CallState.DISCONNECTED ||
616 originalCall.getState() == CallState.ABORTED) {
617 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700618 }
619 }
620 }
621
Sailesh Nepal4857f472014-04-07 22:26:27 -0700622 private void updateHandoffCallServiceDescriptor(
623 Call originalCall,
624 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700625 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700626 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
627 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700628
629 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
630 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
631 for (CallsManagerListener listener : mListeners) {
632 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
633 newDescriptor);
634 }
635 }
636 }
637
638 private static boolean areDescriptorsEqual(
639 CallServiceDescriptor descriptor1,
640 CallServiceDescriptor descriptor2) {
641 if (descriptor1 == null) {
642 return descriptor2 == null;
643 }
644 return descriptor1.equals(descriptor2);
645 }
646
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700647 private static boolean areUriEqual(Uri handle1, Uri handle2) {
648 if (handle1 == null) {
649 return handle2 == null;
650 }
651 return handle1.equals(handle2);
652 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800653}