blob: 60573504a37cf4cbbe760e77060d8e3b5c5f5fe5 [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;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Santos Cordonf3671a62014-05-29 21:51:53 -070032import java.util.HashSet;
33import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070034import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
39 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
40 * access from other packages specifically refraining from passing the CallsManager instance
41 * beyond the com.android.telecomm package boundary.
42 */
Santos Cordon766d04f2014-05-06 10:28:25 -070043public final class CallsManager implements Call.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080044
Santos Cordon74d420b2014-05-07 14:38:47 -070045 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
49 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070050 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
51 void onCallServiceChanged(
52 Call call,
53 CallServiceWrapper oldCallService,
54 CallServiceWrapper newCallService);
55 void onCallHandoffCallServiceDescriptorChanged(
56 Call call,
57 CallServiceDescriptor oldDescriptor,
58 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 void onIncomingCallAnswered(Call call);
60 void onIncomingCallRejected(Call call);
61 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070062 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070063 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070064 void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable);
65 void onIsConferencedChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070066 }
67
Santos Cordon8e8b8d22013-12-19 14:14:05 -080068 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080069
Santos Cordon8e8b8d22013-12-19 14:14:05 -080070 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070071 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
72 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080073 */
Santos Cordonf3671a62014-05-29 21:51:53 -070074 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080075
76 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070077 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
78 * and removed when hadnoff is complete.
79 */
Santos Cordonf3671a62014-05-29 21:51:53 -070080 private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>();
81
82
83 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
84 private final InCallController mInCallController = new InCallController();
85 private final CallAudioManager mCallAudioManager;
86 private final Ringer mRinger;
87 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070088 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070089
90 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070091 * The call the user is currently interacting with. This is the call that should have audio
92 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080093 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070094 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080095
Santos Cordon766d04f2014-05-06 10:28:25 -070096 /** Singleton accessor. */
97 static CallsManager getInstance() {
98 return INSTANCE;
99 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800100
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800102 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800103 */
104 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700105 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700106
Santos Cordondeb8c892014-05-30 01:38:03 -0700107 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
108 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700109 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700110 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700111 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonae193062014-05-21 21:21:49 -0700112
Santos Cordondeb8c892014-05-30 01:38:03 -0700113 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700114 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700115 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700116 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700117 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700118 mListeners.add(new RingbackPlayer(this, playerFactory));
119 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700120 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700121 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700122 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700123 mListeners.add(mHeadsetMediaButton);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800124 }
125
Santos Cordon766d04f2014-05-06 10:28:25 -0700126 @Override
127 public void onSuccessfulOutgoingCall(Call call) {
128 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
129 if (mCalls.contains(call)) {
130 // The call's CallService has been updated.
131 for (CallsManagerListener listener : mListeners) {
132 listener.onCallServiceChanged(call, null, call.getCallService());
133 }
134 } else if (mPendingHandoffCalls.contains(call)) {
135 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
136 call.getCallService().getDescriptor());
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700137 } else {
138 Log.wtf(this, "unexpected successful call notification: %s", call);
139 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700140 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700141
142 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700143 }
144
145 @Override
Ihab Awad0fea3f22014-06-03 18:45:05 -0700146 public void onFailedOutgoingCall(Call call, boolean isAborted, int errorCode, String errorMsg) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700147 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
148 if (isAborted) {
149 setCallState(call, CallState.ABORTED);
150 removeCall(call);
151 } else {
152 // TODO: Replace disconnect cause with more specific disconnect causes.
Ihab Awad0fea3f22014-06-03 18:45:05 -0700153 markCallAsDisconnected(call, errorCode, errorMsg);
Santos Cordon766d04f2014-05-06 10:28:25 -0700154 }
155 }
156
157 @Override
158 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
159 Log.d(this, "onSuccessfulIncomingCall");
160 setCallState(call, callInfo.getState());
161 addCall(call);
162 }
163
164 @Override
165 public void onFailedIncomingCall(Call call) {
166 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800167 }
168
Ihab Awadcb387ac2014-05-28 16:49:38 -0700169 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700170 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
171 for (CallsManagerListener listener : mListeners) {
172 listener.onIsConferenceCapableChanged(call, isConferenceCapable);
173 }
174 }
175
176 @Override
Ihab Awadcb387ac2014-05-28 16:49:38 -0700177 public void onRequestingRingback(Call call, boolean ringback) {
178 for (CallsManagerListener listener : mListeners) {
179 listener.onRequestingRingback(call, ringback);
180 }
181 }
182
Evan Charlton352105c2014-06-03 14:10:54 -0700183 @Override
184 public void onPostDialWait(Call call, String remaining) {
185 mInCallController.onPostDialWait(call, remaining);
186 }
187
Santos Cordona1610702014-06-04 20:22:56 -0700188 @Override
189 public void onExpiredConferenceCall(Call call) {
190 call.removeListener(this);
191 }
192
193 @Override
194 public void onConfirmedConferenceCall(Call call) {
195 addCall(call);
196 Log.v(this, "confirming Conf call %s", call);
197 for (CallsManagerListener listener : mListeners) {
198 listener.onIsConferencedChanged(call);
199 }
200 }
201
202 @Override
203 public void onParentChanged(Call call) {
204 for (CallsManagerListener listener : mListeners) {
205 listener.onIsConferencedChanged(call);
206 }
207 }
208
209 @Override
210 public void onChildrenChanged(Call call) {
211 for (CallsManagerListener listener : mListeners) {
212 listener.onIsConferencedChanged(call);
213 }
214 }
215
Sailesh Nepal810735e2014-03-18 18:15:46 -0700216 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700217 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700218 }
219
220 Call getForegroundCall() {
221 return mForegroundCall;
222 }
223
Santos Cordonae193062014-05-21 21:21:49 -0700224 Ringer getRinger() {
225 return mRinger;
226 }
227
Santos Cordonf3671a62014-05-29 21:51:53 -0700228 InCallController getInCallController() {
229 return mInCallController;
230 }
231
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700232 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700233 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700234 if (call.isEmergencyCall()) {
235 return true;
236 }
237 }
238 return false;
239 }
240
241 CallAudioState getAudioState() {
242 return mCallAudioManager.getAudioState();
243 }
244
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800245 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800246 * Starts the incoming call sequence by having switchboard gather more information about the
247 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700248 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800249 *
Ben Giladc5b22692014-02-18 20:03:22 -0800250 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800251 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800252 */
Evan Charltona05805b2014-03-05 08:21:46 -0800253 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800254 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800255 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700256 // additional information from the call service, but for now we just need one to pass
257 // around.
Santos Cordona1610702014-06-04 20:22:56 -0700258 Call call = new Call(true /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700259 // TODO(santoscordon): Move this to be a part of addCall()
260 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800261
Santos Cordon766d04f2014-05-06 10:28:25 -0700262 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800263 }
264
265 /**
Yorke Lee33501632014-03-17 19:24:12 -0700266 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800267 *
Yorke Lee33501632014-03-17 19:24:12 -0700268 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800269 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700270 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700271 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800272 */
Yorke Lee33501632014-03-17 19:24:12 -0700273 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700274 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
275
276 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700277 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700278 } else {
279 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
280 Log.pii(uriHandle), Log.pii(handle));
281 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700282
Santos Cordona1610702014-06-04 20:22:56 -0700283 Call call = new Call(
284 uriHandle, gatewayInfo, false /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700285
286 // TODO(santoscordon): Move this to be a part of addCall()
287 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700288 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800289
Santos Cordon766d04f2014-05-06 10:28:25 -0700290 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800291 }
292
293 /**
Santos Cordona1610702014-06-04 20:22:56 -0700294 * Attempts to start a conference call for the specified call.
295 *
296 * @param call The call to conference with.
297 */
298 void conference(Call call) {
299 Call conferenceCall = new Call(false, true);
300 conferenceCall.addListener(this);
301 call.conferenceInto(conferenceCall);
302 }
303
304 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800305 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
306 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
307 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800308 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700309 void answerCall(Call call) {
310 if (!mCalls.contains(call)) {
311 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800312 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700313 // If the foreground call is not the ringing call and it is currently isActive() or
314 // DIALING, put it on hold before answering the call.
315 if (mForegroundCall != null && mForegroundCall != call &&
316 (mForegroundCall.isActive() ||
317 mForegroundCall.getState() == CallState.DIALING)) {
318 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
319 mForegroundCall, call);
320 mForegroundCall.hold();
321 // TODO(santoscordon): Wait until we get confirmation of the active call being
322 // on-hold before answering the new call.
323 // TODO(santoscordon): Import logic from CallManager.acceptCall()
324 }
325
Santos Cordona56f2762014-03-24 15:55:53 -0700326 for (CallsManagerListener listener : mListeners) {
327 listener.onIncomingCallAnswered(call);
328 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800329
Santos Cordon61d0f702014-02-19 02:52:23 -0800330 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700331 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800332 call.answer();
333 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800334 }
335
336 /**
337 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
338 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
339 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800340 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700341 void rejectCall(Call call) {
342 if (!mCalls.contains(call)) {
343 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800344 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700345 for (CallsManagerListener listener : mListeners) {
346 listener.onIncomingCallRejected(call);
347 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800348 call.reject();
349 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800350 }
351
352 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700353 * Instructs Telecomm to play the specified DTMF tone within the specified call.
354 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700355 * @param digit The DTMF digit to play.
356 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700357 void playDtmfTone(Call call, char digit) {
358 if (!mCalls.contains(call)) {
359 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700360 } else {
361 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700362 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700363 }
364 }
365
366 /**
367 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700368 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700369 void stopDtmfTone(Call call) {
370 if (!mCalls.contains(call)) {
371 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700372 } else {
373 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700374 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700375 }
376 }
377
378 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700379 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700380 */
Evan Charlton352105c2014-06-03 14:10:54 -0700381 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700382 if (!mCalls.contains(call)) {
383 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700384 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700385 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700386 }
387 }
388
389 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800390 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
391 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
392 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800393 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700394 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700395 Log.v(this, "disconnectCall %s", call);
396
Sailesh Nepale59bb192014-04-01 18:33:59 -0700397 if (!mCalls.contains(call)) {
398 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800399 } else {
400 call.disconnect();
401 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800402 }
Santos Cordon681663d2014-01-30 04:32:15 -0800403
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700404 /**
405 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
406 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
407 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700408 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700409 void holdCall(Call call) {
410 if (!mCalls.contains(call)) {
411 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700412 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700413 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700414 call.hold();
415 }
416 }
417
418 /**
419 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
420 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
421 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700422 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700423 void unholdCall(Call call) {
424 if (!mCalls.contains(call)) {
425 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700426 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700427 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700428 call.unhold();
429 }
430 }
431
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700432 /** Called by the in-call UI to change the mute state. */
433 void mute(boolean shouldMute) {
434 mCallAudioManager.mute(shouldMute);
435 }
436
437 /**
438 * Called by the in-call UI to change the audio route, for example to change from earpiece to
439 * speaker phone.
440 */
441 void setAudioRoute(int route) {
442 mCallAudioManager.setAudioRoute(route);
443 }
444
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700445 void startHandoffForCall(Call originalCall) {
446 if (!mCalls.contains(originalCall)) {
447 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
448 return;
449 }
450
451 for (Call handoffCall : mPendingHandoffCalls) {
452 if (handoffCall.getOriginalCall() == originalCall) {
453 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
454 return;
455 }
456 }
457
458 // Create a new call to be placed in the background. If handoff is successful then the
459 // original call will live on but its state will be updated to the new call's state. In
460 // particular the original call's call service will be updated to the new call's call
461 // service.
Santos Cordona1610702014-06-04 20:22:56 -0700462 Call tempCall = new Call(
463 originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false, false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700464 tempCall.setOriginalCall(originalCall);
465 tempCall.setExtras(originalCall.getExtras());
466 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
467 mPendingHandoffCalls.add(tempCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700468 tempCall.addListener(this);
469
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700470 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700471 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700472 }
473
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700474 /** Called when the audio state changes. */
475 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
476 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700477 for (CallsManagerListener listener : mListeners) {
478 listener.onAudioStateChanged(oldAudioState, newAudioState);
479 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700480 }
481
Sailesh Nepale59bb192014-04-01 18:33:59 -0700482 void markCallAsRinging(Call call) {
483 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800484 }
485
Sailesh Nepale59bb192014-04-01 18:33:59 -0700486 void markCallAsDialing(Call call) {
487 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800488 }
489
Sailesh Nepale59bb192014-04-01 18:33:59 -0700490 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700491 if (call.getConnectTimeMillis() == 0) {
492 call.setConnectTimeMillis(System.currentTimeMillis());
493 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700494 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700495
496 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700497 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700498 }
Santos Cordon681663d2014-01-30 04:32:15 -0800499 }
500
Sailesh Nepale59bb192014-04-01 18:33:59 -0700501 void markCallAsOnHold(Call call) {
502 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700503 }
504
Santos Cordon049b7b62014-01-30 05:34:26 -0800505 /**
506 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
507 * live call, then also disconnect from the in-call controller.
508 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700509 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
510 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800511 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700512 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
513 call.setDisconnectCause(disconnectCause, disconnectMessage);
514 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700515
516 // Only remove the call if handoff is not pending.
517 if (call.getHandoffCallServiceDescriptor() == null) {
518 removeCall(call);
519 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800520 }
521
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700522 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700523 if (!mCalls.contains(call)) {
524 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
525 return;
526 }
527
528 if (extras == null) {
529 call.setExtras(Bundle.EMPTY);
530 } else {
531 call.setExtras(extras);
532 }
533
534 Uri oldHandle = call.getHandoffHandle();
535 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
536 if (!areUriEqual(oldHandle, handle)) {
537 call.setHandoffHandle(handle);
538 for (CallsManagerListener listener : mListeners) {
539 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
540 }
541 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700542 }
543
Ben Gilada0d9f752014-02-26 11:49:03 -0800544 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700545 * Cleans up any calls currently associated with the specified call service when the
546 * call-service binder disconnects unexpectedly.
547 *
548 * @param callService The call service that disconnected.
549 */
550 void handleCallServiceDeath(CallServiceWrapper callService) {
551 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700552 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700553 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700554 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700555 }
556 }
557 }
558
Santos Cordondeb8c892014-05-30 01:38:03 -0700559 boolean hasAnyCalls() {
560 return !mCalls.isEmpty();
561 }
562
Santos Cordonc7e85d42014-05-22 02:51:48 -0700563 boolean hasActiveOrHoldingCall() {
564 for (Call call : mCalls) {
565 CallState state = call.getState();
566 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
567 return true;
568 }
569 }
570 return false;
571 }
572
573 boolean hasRingingCall() {
574 for (Call call : mCalls) {
575 if (call.getState() == CallState.RINGING) {
576 return true;
577 }
578 }
579 return false;
580 }
581
Santos Cordondeb8c892014-05-30 01:38:03 -0700582 boolean onMediaButton(int type) {
583 if (hasAnyCalls()) {
584 if (HeadsetMediaButton.SHORT_PRESS == type) {
585 Call ringingCall = getFirstCallWithState(CallState.RINGING);
586 if (ringingCall == null) {
587 mCallAudioManager.toggleMute();
588 return true;
589 } else {
590 ringingCall.answer();
591 return true;
592 }
593 } else if (HeadsetMediaButton.LONG_PRESS == type) {
594 Log.d(this, "handleHeadsetHook: longpress -> hangup");
595 Call callToHangup = getFirstCallWithState(
596 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
597 if (callToHangup != null) {
598 callToHangup.disconnect();
599 return true;
600 }
601 }
602 }
603 return false;
604 }
605
606 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700607 * Checks to see if the specified call is the only high-level call and if so, enable the
608 * "Add-call" button. We allow you to add a second call but not a third or beyond.
609 *
610 * @param call The call to test for add-call.
611 * @return Whether the add-call feature should be enabled for the call.
612 */
613 protected boolean isAddCallCapable(Call call) {
614 if (call.getParentCall() != null) {
615 // Never true for child calls.
616 return false;
617 }
618
619 // Loop through all the other calls and there exists a top level (has no parent) call
620 // that is not the specified call, return false.
621 for (Call otherCall : mCalls) {
622 if (call != otherCall && otherCall.getParentCall() == null) {
623 return false;
624 }
625 }
626 return true;
627 }
628
629 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700630 * Returns the first call that it finds with the given states. The states are treated as having
631 * priority order so that any call with the first state will be returned before any call with
632 * states listed later in the parameter list.
633 */
634 private Call getFirstCallWithState(CallState... states) {
635 for (CallState currentState : states) {
636 for (Call call : mCalls) {
637 if (currentState == call.getState()) {
638 return call;
639 }
640 }
641 }
642 return null;
643 }
644
Santos Cordon4b2c1192014-03-19 18:15:38 -0700645 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800646 * Adds the specified call to the main list of live calls.
647 *
648 * @param call The call to add.
649 */
650 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700651 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700652
653 // TODO(santoscordon): Update mForegroundCall prior to invoking
654 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700655 for (CallsManagerListener listener : mListeners) {
656 listener.onCallAdded(call);
657 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700658 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800659 }
660
Sailesh Nepal810735e2014-03-18 18:15:46 -0700661 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700662 // If a handoff is pending then the original call shouldn't be removed.
663 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700664 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700665
Santos Cordon766d04f2014-05-06 10:28:25 -0700666 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700667 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700668 call.clearCallServiceSelector();
669
670 boolean shouldNotify = false;
671 if (mCalls.contains(call)) {
672 mCalls.remove(call);
673 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700674 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700675 Log.v(this, "removeCall, marking handoff call as failed");
676 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700677 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800678
Sailesh Nepale59bb192014-04-01 18:33:59 -0700679 // Only broadcast changes for calls that are being tracked.
680 if (shouldNotify) {
681 for (CallsManagerListener listener : mListeners) {
682 listener.onCallRemoved(call);
683 }
684 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700685 }
686 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800687
Sailesh Nepal810735e2014-03-18 18:15:46 -0700688 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700689 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700690 *
691 * @param call The call.
692 * @param newState The new state of the call.
693 */
694 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700695 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700696 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700697 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700698 if (newState != oldState) {
699 // Unfortunately, in the telephony world the radio is king. So if the call notifies
700 // us that the call is in a particular state, we allow it even if it doesn't make
701 // sense (e.g., ACTIVE -> RINGING).
702 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
703 // into a well-defined state machine.
704 // TODO(santoscordon): Define expected state transitions here, and log when an
705 // unexpected transition occurs.
706 call.setState(newState);
707
708 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700709 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700710 for (CallsManagerListener listener : mListeners) {
711 listener.onCallStateChanged(call, oldState, newState);
712 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700713 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800714 }
715 }
716 }
717
718 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700719 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800720 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700721 private void updateForegroundCall() {
722 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700723 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700724 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
725 // of its state will be foreground by default and instead the call service should be
726 // notified when its calls enter and exit foreground state. Foreground will mean that
727 // the call should play audio and listen to microphone if it wants.
728
729 // Active calls have priority.
730 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700731 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800732 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700733 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700734
735 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700736 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700737 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800738 }
739 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800740
Sailesh Nepal810735e2014-03-18 18:15:46 -0700741 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700742 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700743 Call oldForegroundCall = mForegroundCall;
744 mForegroundCall = newForegroundCall;
745
Santos Cordona56f2762014-03-24 15:55:53 -0700746 for (CallsManagerListener listener : mListeners) {
747 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
748 }
Santos Cordon681663d2014-01-30 04:32:15 -0800749 }
750 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700751
Sailesh Nepal4857f472014-04-07 22:26:27 -0700752 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700753 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700754 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
755 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700756
Sailesh Nepal4857f472014-04-07 22:26:27 -0700757 // Remove the transient handoff call object (don't disconnect because the call could still
758 // be live).
759 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700760 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700761
Sailesh Nepal4857f472014-04-07 22:26:27 -0700762 if (wasSuccessful) {
Sailesh Nepaldcd64262014-05-23 17:03:28 -0700763 if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) {
764 originalCall.disconnect();
765 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700766
Sailesh Nepal4857f472014-04-07 22:26:27 -0700767 // Synchronize.
768 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
769 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700770
Sailesh Nepal4857f472014-04-07 22:26:27 -0700771 // Force the foreground call changed notification to be sent.
772 for (CallsManagerListener listener : mListeners) {
773 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
774 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700775
Sailesh Nepal4857f472014-04-07 22:26:27 -0700776 updateHandoffCallServiceDescriptor(originalCall, null);
777 } else {
778 updateHandoffCallServiceDescriptor(originalCall, null);
779 if (originalCall.getState() == CallState.DISCONNECTED ||
780 originalCall.getState() == CallState.ABORTED) {
781 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700782 }
783 }
784 }
785
Sailesh Nepal4857f472014-04-07 22:26:27 -0700786 private void updateHandoffCallServiceDescriptor(
787 Call originalCall,
788 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700789 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700790 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
791 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700792
793 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
794 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
795 for (CallsManagerListener listener : mListeners) {
796 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
797 newDescriptor);
798 }
799 }
800 }
801
802 private static boolean areDescriptorsEqual(
803 CallServiceDescriptor descriptor1,
804 CallServiceDescriptor descriptor2) {
805 if (descriptor1 == null) {
806 return descriptor2 == null;
807 }
808 return descriptor1.equals(descriptor2);
809 }
810
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700811 private static boolean areUriEqual(Uri handle1, Uri handle2) {
812 if (handle1 == null) {
813 return handle2 == null;
814 }
815 return handle1.equals(handle2);
816 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800817}