blob: a4b2086308bff0b12fdb1e11647ae9efaa500a5e [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) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700314 Log.v(this, "disconnectCall %s", call);
315
Sailesh Nepale59bb192014-04-01 18:33:59 -0700316 if (!mCalls.contains(call)) {
317 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800318 } else {
319 call.disconnect();
320 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800321 }
Santos Cordon681663d2014-01-30 04:32:15 -0800322
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700323 /**
324 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
325 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
326 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700327 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700328 void holdCall(Call call) {
329 if (!mCalls.contains(call)) {
330 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700331 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700332 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700333 call.hold();
334 }
335 }
336
337 /**
338 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
339 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
340 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700341 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700342 void unholdCall(Call call) {
343 if (!mCalls.contains(call)) {
344 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700345 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700346 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700347 call.unhold();
348 }
349 }
350
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700351 /** Called by the in-call UI to change the mute state. */
352 void mute(boolean shouldMute) {
353 mCallAudioManager.mute(shouldMute);
354 }
355
356 /**
357 * Called by the in-call UI to change the audio route, for example to change from earpiece to
358 * speaker phone.
359 */
360 void setAudioRoute(int route) {
361 mCallAudioManager.setAudioRoute(route);
362 }
363
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700364 void startHandoffForCall(Call originalCall) {
365 if (!mCalls.contains(originalCall)) {
366 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
367 return;
368 }
369
370 for (Call handoffCall : mPendingHandoffCalls) {
371 if (handoffCall.getOriginalCall() == originalCall) {
372 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
373 return;
374 }
375 }
376
377 // Create a new call to be placed in the background. If handoff is successful then the
378 // original call will live on but its state will be updated to the new call's state. In
379 // particular the original call's call service will be updated to the new call's call
380 // service.
381 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
382 originalCall.getGatewayInfo(), false);
383 tempCall.setOriginalCall(originalCall);
384 tempCall.setExtras(originalCall.getExtras());
385 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
386 mPendingHandoffCalls.add(tempCall);
387 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700388 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700389 }
390
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700391 /** Called when the audio state changes. */
392 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
393 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700394 for (CallsManagerListener listener : mListeners) {
395 listener.onAudioStateChanged(oldAudioState, newAudioState);
396 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700397 }
398
Sailesh Nepale59bb192014-04-01 18:33:59 -0700399 void markCallAsRinging(Call call) {
400 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800401 }
402
Sailesh Nepale59bb192014-04-01 18:33:59 -0700403 void markCallAsDialing(Call call) {
404 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800405 }
406
Sailesh Nepale59bb192014-04-01 18:33:59 -0700407 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700408 if (call.getConnectTimeMillis() == 0) {
409 call.setConnectTimeMillis(System.currentTimeMillis());
410 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700411 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700412
413 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700414 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700415 }
Santos Cordon681663d2014-01-30 04:32:15 -0800416 }
417
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 void markCallAsOnHold(Call call) {
419 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700420 }
421
Santos Cordon049b7b62014-01-30 05:34:26 -0800422 /**
423 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
424 * live call, then also disconnect from the in-call controller.
425 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700426 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
427 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800428 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700429 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
430 call.setDisconnectCause(disconnectCause, disconnectMessage);
431 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700432
433 // Only remove the call if handoff is not pending.
434 if (call.getHandoffCallServiceDescriptor() == null) {
435 removeCall(call);
436 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800437 }
438
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700439 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700440 if (!mCalls.contains(call)) {
441 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
442 return;
443 }
444
445 if (extras == null) {
446 call.setExtras(Bundle.EMPTY);
447 } else {
448 call.setExtras(extras);
449 }
450
451 Uri oldHandle = call.getHandoffHandle();
452 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
453 if (!areUriEqual(oldHandle, handle)) {
454 call.setHandoffHandle(handle);
455 for (CallsManagerListener listener : mListeners) {
456 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
457 }
458 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700459 }
460
Ben Gilada0d9f752014-02-26 11:49:03 -0800461 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700462 * Cleans up any calls currently associated with the specified call service when the
463 * call-service binder disconnects unexpectedly.
464 *
465 * @param callService The call service that disconnected.
466 */
467 void handleCallServiceDeath(CallServiceWrapper callService) {
468 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700469 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700470 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700471 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700472 }
473 }
474 }
475
476 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800477 * Adds the specified call to the main list of live calls.
478 *
479 * @param call The call to add.
480 */
481 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700482 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700483
484 // TODO(santoscordon): Update mForegroundCall prior to invoking
485 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700486 for (CallsManagerListener listener : mListeners) {
487 listener.onCallAdded(call);
488 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700489 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800490 }
491
Sailesh Nepal810735e2014-03-18 18:15:46 -0700492 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700493 // If a handoff is pending then the original call shouldn't be removed.
494 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700495 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700496
Santos Cordon766d04f2014-05-06 10:28:25 -0700497 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700498 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700499 call.clearCallServiceSelector();
500
501 boolean shouldNotify = false;
502 if (mCalls.contains(call)) {
503 mCalls.remove(call);
504 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700505 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700506 Log.v(this, "removeCall, marking handoff call as failed");
507 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700508 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800509
Sailesh Nepale59bb192014-04-01 18:33:59 -0700510 // Only broadcast changes for calls that are being tracked.
511 if (shouldNotify) {
512 for (CallsManagerListener listener : mListeners) {
513 listener.onCallRemoved(call);
514 }
515 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700516 }
517 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800518
Sailesh Nepal810735e2014-03-18 18:15:46 -0700519 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700520 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700521 *
522 * @param call The call.
523 * @param newState The new state of the call.
524 */
525 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700526 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700527 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700528 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700529 if (newState != oldState) {
530 // Unfortunately, in the telephony world the radio is king. So if the call notifies
531 // us that the call is in a particular state, we allow it even if it doesn't make
532 // sense (e.g., ACTIVE -> RINGING).
533 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
534 // into a well-defined state machine.
535 // TODO(santoscordon): Define expected state transitions here, and log when an
536 // unexpected transition occurs.
537 call.setState(newState);
538
539 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700540 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700541 for (CallsManagerListener listener : mListeners) {
542 listener.onCallStateChanged(call, oldState, newState);
543 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700544 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800545 }
546 }
547 }
548
549 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700550 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800551 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700552 private void updateForegroundCall() {
553 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700554 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700555 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
556 // of its state will be foreground by default and instead the call service should be
557 // notified when its calls enter and exit foreground state. Foreground will mean that
558 // the call should play audio and listen to microphone if it wants.
559
560 // Active calls have priority.
561 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700562 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800563 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700564 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700565
566 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700567 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700568 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800569 }
570 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800571
Sailesh Nepal810735e2014-03-18 18:15:46 -0700572 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700573 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700574 Call oldForegroundCall = mForegroundCall;
575 mForegroundCall = newForegroundCall;
576
Santos Cordona56f2762014-03-24 15:55:53 -0700577 for (CallsManagerListener listener : mListeners) {
578 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
579 }
Santos Cordon681663d2014-01-30 04:32:15 -0800580 }
581 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700582
Sailesh Nepal4857f472014-04-07 22:26:27 -0700583 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700584 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700585 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
586 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700587
Sailesh Nepal4857f472014-04-07 22:26:27 -0700588 // Remove the transient handoff call object (don't disconnect because the call could still
589 // be live).
590 mPendingHandoffCalls.remove(handoffCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700591
Sailesh Nepal4857f472014-04-07 22:26:27 -0700592 if (wasSuccessful) {
593 // Disconnect.
594 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700595
Sailesh Nepal4857f472014-04-07 22:26:27 -0700596 // Synchronize.
597 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
598 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700599
Sailesh Nepal4857f472014-04-07 22:26:27 -0700600 // Force the foreground call changed notification to be sent.
601 for (CallsManagerListener listener : mListeners) {
602 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
603 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700604
Sailesh Nepal4857f472014-04-07 22:26:27 -0700605 updateHandoffCallServiceDescriptor(originalCall, null);
606 } else {
607 updateHandoffCallServiceDescriptor(originalCall, null);
608 if (originalCall.getState() == CallState.DISCONNECTED ||
609 originalCall.getState() == CallState.ABORTED) {
610 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700611 }
612 }
613 }
614
Sailesh Nepal4857f472014-04-07 22:26:27 -0700615 private void updateHandoffCallServiceDescriptor(
616 Call originalCall,
617 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700618 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700619 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
620 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700621
622 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
623 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
624 for (CallsManagerListener listener : mListeners) {
625 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
626 newDescriptor);
627 }
628 }
629 }
630
631 private static boolean areDescriptorsEqual(
632 CallServiceDescriptor descriptor1,
633 CallServiceDescriptor descriptor2) {
634 if (descriptor1 == null) {
635 return descriptor2 == null;
636 }
637 return descriptor1.equals(descriptor2);
638 }
639
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700640 private static boolean areUriEqual(Uri handle1, Uri handle2) {
641 if (handle1 == null) {
642 return handle2 == null;
643 }
644 return handle1.equals(handle2);
645 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800646}