blob: e019349215a1d5faf596346084a5a5612a8fba04 [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;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070026import android.telecomm.InCallCall;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070027import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Santos Cordon681663d2014-01-30 04:32:15 -080029import com.google.common.base.Preconditions;
30import com.google.common.base.Strings;
Sailesh Nepal810735e2014-03-18 18:15:46 -070031import com.google.common.collect.ImmutableCollection;
32import com.google.common.collect.ImmutableList;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080034import com.google.common.collect.Maps;
Santos Cordona56f2762014-03-24 15:55:53 -070035import com.google.common.collect.Sets;
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Ben Gilad9f2bed32013-12-12 17:43:26 -080037import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080038import java.util.Map;
Santos Cordona56f2762014-03-24 15:55:53 -070039import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080040
Ben Giladdd8c6082013-12-30 14:44:08 -080041/**
42 * Singleton.
43 *
44 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
45 * access from other packages specifically refraining from passing the CallsManager instance
46 * beyond the com.android.telecomm package boundary.
47 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080048public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080049
Santos Cordona56f2762014-03-24 15:55:53 -070050 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070051 interface CallsManagerListener {
52 void onCallAdded(Call call);
53 void onCallRemoved(Call call);
54 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070055 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
56 void onCallServiceChanged(
57 Call call,
58 CallServiceWrapper oldCallService,
59 CallServiceWrapper newCallService);
60 void onCallHandoffCallServiceDescriptorChanged(
61 Call call,
62 CallServiceDescriptor oldDescriptor,
63 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070064 void onIncomingCallAnswered(Call call);
65 void onIncomingCallRejected(Call call);
66 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070067 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 }
69
Santos Cordon8e8b8d22013-12-19 14:14:05 -080070 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080071
Santos Cordone3d76ab2014-01-28 17:25:20 -080072 private final Switchboard mSwitchboard;
73
Santos Cordon8e8b8d22013-12-19 14:14:05 -080074 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070075 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
76 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080077 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 private final Set<Call> mCalls = Sets.newLinkedHashSet();
Santos Cordon681663d2014-01-30 04:32:15 -080079
80 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070081 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
82 * and removed when hadnoff is complete.
83 */
84 private final Set<Call> mPendingHandoffCalls = Sets.newLinkedHashSet();
85
86 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070087 * The call the user is currently interacting with. This is the call that should have audio
88 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080089 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070090 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080091
Santos Cordon92a2d812014-04-30 15:19:01 -070092 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
93
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070094 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070095
Santos Cordona56f2762014-03-24 15:55:53 -070096 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070097
Evan Charltonf02e9882014-03-06 12:54:52 -080098 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080099
Evan Charltonf02e9882014-03-06 12:54:52 -0800100 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -0800101
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800103 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 */
105 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700106 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700107
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700108 mSwitchboard = new Switchboard(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700109 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -0700110
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700111 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700112 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700113 mListeners.add(new PhoneStateBroadcaster());
114 mListeners.add(new InCallController());
Evan Charlton893f9e32014-04-09 08:51:52 -0700115 mListeners.add(new Ringer(mCallAudioManager, this, playerFactory, app));
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700116 mListeners.add(new RingbackPlayer(this, playerFactory));
117 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700118 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700119 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700120 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800121 }
122
Ben Gilada0d9f752014-02-26 11:49:03 -0800123 static CallsManager getInstance() {
124 return INSTANCE;
125 }
126
Sailesh Nepal810735e2014-03-18 18:15:46 -0700127 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700128 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700129 }
130
131 Call getForegroundCall() {
132 return mForegroundCall;
133 }
134
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700135 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700136 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700137 if (call.isEmergencyCall()) {
138 return true;
139 }
140 }
141 return false;
142 }
143
144 CallAudioState getAudioState() {
145 return mCallAudioManager.getAudioState();
146 }
147
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800148 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800149 * Starts the incoming call sequence by having switchboard gather more information about the
150 * specified call; using the specified call service descriptor. Upon success, execution returns
151 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800152 *
Ben Giladc5b22692014-02-18 20:03:22 -0800153 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800154 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800155 */
Evan Charltona05805b2014-03-05 08:21:46 -0800156 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800157 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800158 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700159 // additional information from the call service, but for now we just need one to pass
160 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700161 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800162
Evan Charltona05805b2014-03-05 08:21:46 -0800163 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800164 }
165
166 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800167 * Validates the specified call and, upon no objection to connect it, adds the new call to the
168 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
169 *
170 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700171 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800172 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700173 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800174 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700175 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800176
Sailesh Nepalce704b92014-03-17 18:31:43 -0700177 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800178 ContactInfo contactInfo = call.getContactInfo();
179 for (IncomingCallValidator validator : mIncomingCallValidators) {
180 if (!validator.isValid(handle, contactInfo)) {
181 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800182 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800183 return;
184 }
185 }
186
187 // No objection to accept the incoming call, proceed with potentially connecting it (based
188 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700189 call.setHandle(callInfo.getHandle());
190 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800191 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700192 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800193
Sailesh Nepal810735e2014-03-18 18:15:46 -0700194 /**
195 * Called when an incoming call was not connected.
196 *
197 * @param call The incoming call.
198 */
199 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700200 // Incoming calls are not added unless they are successful. We set the state and disconnect
201 // cause just as a matter of good bookkeeping. We do not use the specific methods for
202 // setting those values so that we do not trigger CallsManagerListener events.
203 // TODO: Needs more specific disconnect error for this case.
204 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
205 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800206 }
207
208 /**
Yorke Lee33501632014-03-17 19:24:12 -0700209 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800210 *
Yorke Lee33501632014-03-17 19:24:12 -0700211 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800212 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700213 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700214 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800215 */
Yorke Lee33501632014-03-17 19:24:12 -0700216 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800217 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800218 if (!validator.isValid(handle, contactInfo)) {
219 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800220 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800221 return;
222 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800223 }
224
Yorke Lee33501632014-03-17 19:24:12 -0700225 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
226
227 if (gatewayInfo == null) {
228 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
229 } else {
230 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
231 Log.pii(uriHandle), Log.pii(handle));
232 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700233 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700234 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800235 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800236 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800237
238 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700239 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800240 *
241 * @param call The new outgoing call.
242 */
243 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700244 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700245 if (mCalls.contains(call)) {
246 // The call's CallService has been updated.
247 for (CallsManagerListener listener : mListeners) {
248 listener.onCallServiceChanged(call, null, call.getCallService());
249 }
250 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700251 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
252 call.getCallService().getDescriptor());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700253 }
Santos Cordon681663d2014-01-30 04:32:15 -0800254 }
255
256 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700257 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800258 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700259 * @param call The outgoing call.
260 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800261 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700262 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
263 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
264 if (isAborted) {
265 call.abort();
266 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700267 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700268 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700269 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700270 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700271 }
Yorke Leef98fb572014-03-05 10:56:55 -0800272 }
273
274 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800275 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
276 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
277 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800278 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700279 void answerCall(Call call) {
280 if (!mCalls.contains(call)) {
281 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800282 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700283 // If the foreground call is not the ringing call and it is currently isActive() or
284 // DIALING, put it on hold before answering the call.
285 if (mForegroundCall != null && mForegroundCall != call &&
286 (mForegroundCall.isActive() ||
287 mForegroundCall.getState() == CallState.DIALING)) {
288 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
289 mForegroundCall, call);
290 mForegroundCall.hold();
291 // TODO(santoscordon): Wait until we get confirmation of the active call being
292 // on-hold before answering the new call.
293 // TODO(santoscordon): Import logic from CallManager.acceptCall()
294 }
295
Santos Cordona56f2762014-03-24 15:55:53 -0700296 for (CallsManagerListener listener : mListeners) {
297 listener.onIncomingCallAnswered(call);
298 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800299
Santos Cordon61d0f702014-02-19 02:52:23 -0800300 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700301 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800302 call.answer();
303 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800304 }
305
306 /**
307 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
308 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
309 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800310 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700311 void rejectCall(Call call) {
312 if (!mCalls.contains(call)) {
313 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800314 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700315 for (CallsManagerListener listener : mListeners) {
316 listener.onIncomingCallRejected(call);
317 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800318 call.reject();
319 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800320 }
321
322 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700323 * Instructs Telecomm to play the specified DTMF tone within the specified call.
324 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700325 * @param digit The DTMF digit to play.
326 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700327 void playDtmfTone(Call call, char digit) {
328 if (!mCalls.contains(call)) {
329 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700330 } else {
331 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700332 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700333 }
334 }
335
336 /**
337 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700338 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700339 void stopDtmfTone(Call call) {
340 if (!mCalls.contains(call)) {
341 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700342 } else {
343 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700344 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700345 }
346 }
347
348 /**
349 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700350 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700351 void postDialContinue(Call call) {
352 if (!mCalls.contains(call)) {
353 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700354 } else {
355 // TODO(ihab): Implement this from this level on downwards
356 // call.postDialContinue();
357 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
358 }
359 }
360
361 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800362 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
363 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
364 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800365 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700366 void disconnectCall(Call call) {
367 if (!mCalls.contains(call)) {
368 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800369 } else {
370 call.disconnect();
371 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800372 }
Santos Cordon681663d2014-01-30 04:32:15 -0800373
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700374 /**
375 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
376 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
377 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700378 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700379 void holdCall(Call call) {
380 if (!mCalls.contains(call)) {
381 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700382 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700383 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700384 call.hold();
385 }
386 }
387
388 /**
389 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
390 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
391 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700392 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700393 void unholdCall(Call call) {
394 if (!mCalls.contains(call)) {
395 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700396 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700397 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700398 call.unhold();
399 }
400 }
401
Ihab Awad4e0f1922014-04-14 17:35:38 -0700402 /**
403 * Instructs Telecomm to abort any outgoing state of the specified call.
404 */
405 void abortCall(Call call) {
406 if (!mCalls.contains(call)) {
407 Log.w(this, "Unknown call (%s) asked to be aborted", call);
408 } else {
409 Log.d(this, "Aborting call: (%s)", call);
410 mSwitchboard.abortCall(call);
411 }
412 }
413
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700414 /** Called by the in-call UI to change the mute state. */
415 void mute(boolean shouldMute) {
416 mCallAudioManager.mute(shouldMute);
417 }
418
419 /**
420 * Called by the in-call UI to change the audio route, for example to change from earpiece to
421 * speaker phone.
422 */
423 void setAudioRoute(int route) {
424 mCallAudioManager.setAudioRoute(route);
425 }
426
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700427 void startHandoffForCall(Call originalCall) {
428 if (!mCalls.contains(originalCall)) {
429 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
430 return;
431 }
432
433 for (Call handoffCall : mPendingHandoffCalls) {
434 if (handoffCall.getOriginalCall() == originalCall) {
435 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
436 return;
437 }
438 }
439
440 // Create a new call to be placed in the background. If handoff is successful then the
441 // original call will live on but its state will be updated to the new call's state. In
442 // particular the original call's call service will be updated to the new call's call
443 // service.
444 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
445 originalCall.getGatewayInfo(), false);
446 tempCall.setOriginalCall(originalCall);
447 tempCall.setExtras(originalCall.getExtras());
448 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
449 mPendingHandoffCalls.add(tempCall);
450 Log.d(this, "Placing handoff call");
451 mSwitchboard.placeOutgoingCall(tempCall);
452 }
453
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700454 /** Called when the audio state changes. */
455 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
456 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700457 for (CallsManagerListener listener : mListeners) {
458 listener.onAudioStateChanged(oldAudioState, newAudioState);
459 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700460 }
461
Sailesh Nepale59bb192014-04-01 18:33:59 -0700462 void markCallAsRinging(Call call) {
463 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800464 }
465
Sailesh Nepale59bb192014-04-01 18:33:59 -0700466 void markCallAsDialing(Call call) {
467 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800468 }
469
Sailesh Nepale59bb192014-04-01 18:33:59 -0700470 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700471 if (call.getConnectTimeMillis() == 0) {
472 call.setConnectTimeMillis(System.currentTimeMillis());
473 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700474 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700475
476 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700477 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700478 }
Santos Cordon681663d2014-01-30 04:32:15 -0800479 }
480
Sailesh Nepale59bb192014-04-01 18:33:59 -0700481 void markCallAsOnHold(Call call) {
482 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700483 }
484
Santos Cordon049b7b62014-01-30 05:34:26 -0800485 /**
486 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
487 * live call, then also disconnect from the in-call controller.
488 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700489 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
490 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800491 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700492 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
493 call.setDisconnectCause(disconnectCause, disconnectMessage);
494 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700495
496 // Only remove the call if handoff is not pending.
497 if (call.getHandoffCallServiceDescriptor() == null) {
498 removeCall(call);
499 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800500 }
501
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700502 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700503 if (!mCalls.contains(call)) {
504 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
505 return;
506 }
507
508 if (extras == null) {
509 call.setExtras(Bundle.EMPTY);
510 } else {
511 call.setExtras(extras);
512 }
513
514 Uri oldHandle = call.getHandoffHandle();
515 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
516 if (!areUriEqual(oldHandle, handle)) {
517 call.setHandoffHandle(handle);
518 for (CallsManagerListener listener : mListeners) {
519 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
520 }
521 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700522 }
523
Ben Gilada0d9f752014-02-26 11:49:03 -0800524 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700525 * Cleans up any calls currently associated with the specified call service when the
526 * call-service binder disconnects unexpectedly.
527 *
528 * @param callService The call service that disconnected.
529 */
530 void handleCallServiceDeath(CallServiceWrapper callService) {
531 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700532 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700533 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700534 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700535 }
536 }
537 }
538
539 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800540 * Adds the specified call to the main list of live calls.
541 *
542 * @param call The call to add.
543 */
544 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700545 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700546
547 // TODO(santoscordon): Update mForegroundCall prior to invoking
548 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700549 for (CallsManagerListener listener : mListeners) {
550 listener.onCallAdded(call);
551 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700552 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800553 }
554
Sailesh Nepal810735e2014-03-18 18:15:46 -0700555 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700556 // If a handoff is pending then the original call shouldn't be removed.
557 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700558 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700559
Sailesh Nepal810735e2014-03-18 18:15:46 -0700560 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700561 call.clearCallServiceSelector();
562
563 boolean shouldNotify = false;
564 if (mCalls.contains(call)) {
565 mCalls.remove(call);
566 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700567 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700568 Log.v(this, "removeCall, marking handoff call as failed");
569 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700570 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800571
Sailesh Nepale59bb192014-04-01 18:33:59 -0700572 // Only broadcast changes for calls that are being tracked.
573 if (shouldNotify) {
574 for (CallsManagerListener listener : mListeners) {
575 listener.onCallRemoved(call);
576 }
577 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700578 }
579 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800580
Sailesh Nepal810735e2014-03-18 18:15:46 -0700581 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700582 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700583 *
584 * @param call The call.
585 * @param newState The new state of the call.
586 */
587 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700588 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700589 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700590 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700591 if (newState != oldState) {
592 // Unfortunately, in the telephony world the radio is king. So if the call notifies
593 // us that the call is in a particular state, we allow it even if it doesn't make
594 // sense (e.g., ACTIVE -> RINGING).
595 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
596 // into a well-defined state machine.
597 // TODO(santoscordon): Define expected state transitions here, and log when an
598 // unexpected transition occurs.
599 call.setState(newState);
600
601 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700602 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700603 for (CallsManagerListener listener : mListeners) {
604 listener.onCallStateChanged(call, oldState, newState);
605 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700606 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800607 }
608 }
609 }
610
611 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700612 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800613 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700614 private void updateForegroundCall() {
615 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700616 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700617 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
618 // of its state will be foreground by default and instead the call service should be
619 // notified when its calls enter and exit foreground state. Foreground will mean that
620 // the call should play audio and listen to microphone if it wants.
621
622 // Active calls have priority.
623 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700624 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800625 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700626 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700627
628 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700629 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700630 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800631 }
632 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800633
Sailesh Nepal810735e2014-03-18 18:15:46 -0700634 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700635 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700636 Call oldForegroundCall = mForegroundCall;
637 mForegroundCall = newForegroundCall;
638
Santos Cordona56f2762014-03-24 15:55:53 -0700639 for (CallsManagerListener listener : mListeners) {
640 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
641 }
Santos Cordon681663d2014-01-30 04:32:15 -0800642 }
643 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700644
Sailesh Nepal4857f472014-04-07 22:26:27 -0700645 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700646 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700647 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
648 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700649
Sailesh Nepal4857f472014-04-07 22:26:27 -0700650 // Remove the transient handoff call object (don't disconnect because the call could still
651 // be live).
652 mPendingHandoffCalls.remove(handoffCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700653
Sailesh Nepal4857f472014-04-07 22:26:27 -0700654 if (wasSuccessful) {
655 // Disconnect.
656 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700657
Sailesh Nepal4857f472014-04-07 22:26:27 -0700658 // Synchronize.
659 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
660 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700661
Sailesh Nepal4857f472014-04-07 22:26:27 -0700662 // Force the foreground call changed notification to be sent.
663 for (CallsManagerListener listener : mListeners) {
664 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
665 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700666
Sailesh Nepal4857f472014-04-07 22:26:27 -0700667 updateHandoffCallServiceDescriptor(originalCall, null);
668 } else {
669 updateHandoffCallServiceDescriptor(originalCall, null);
670 if (originalCall.getState() == CallState.DISCONNECTED ||
671 originalCall.getState() == CallState.ABORTED) {
672 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700673 }
674 }
675 }
676
Sailesh Nepal4857f472014-04-07 22:26:27 -0700677 private void updateHandoffCallServiceDescriptor(
678 Call originalCall,
679 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700680 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700681 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
682 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700683
684 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
685 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
686 for (CallsManagerListener listener : mListeners) {
687 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
688 newDescriptor);
689 }
690 }
691 }
692
693 private static boolean areDescriptorsEqual(
694 CallServiceDescriptor descriptor1,
695 CallServiceDescriptor descriptor2) {
696 if (descriptor1 == null) {
697 return descriptor2 == null;
698 }
699 return descriptor1.equals(descriptor2);
700 }
701
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700702 private static boolean areUriEqual(Uri handle1, Uri handle2) {
703 if (handle1 == null) {
704 return handle2 == null;
705 }
706 return handle1.equals(handle2);
707 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800708}