blob: e7ea62182437e15ecac432f725117b6b9599b6a1 [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
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070092 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070093
Santos Cordona56f2762014-03-24 15:55:53 -070094 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070095
Evan Charltonf02e9882014-03-06 12:54:52 -080096 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080097
Evan Charltonf02e9882014-03-06 12:54:52 -080098 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080099
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800101 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 */
103 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700104 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700105
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700106 mSwitchboard = new Switchboard(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700107 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -0700108
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700109 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700110 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700111 mListeners.add(new PhoneStateBroadcaster());
112 mListeners.add(new InCallController());
113 mListeners.add(new Ringer(mCallAudioManager));
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700114 mListeners.add(new RingbackPlayer(this, playerFactory));
115 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700116 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700117 mListeners.add(app.getMissedCallNotifier());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800118 }
119
Ben Gilada0d9f752014-02-26 11:49:03 -0800120 static CallsManager getInstance() {
121 return INSTANCE;
122 }
123
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700125 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700126 }
127
128 Call getForegroundCall() {
129 return mForegroundCall;
130 }
131
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700132 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700133 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700134 if (call.isEmergencyCall()) {
135 return true;
136 }
137 }
138 return false;
139 }
140
141 CallAudioState getAudioState() {
142 return mCallAudioManager.getAudioState();
143 }
144
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800146 * Starts the incoming call sequence by having switchboard gather more information about the
147 * specified call; using the specified call service descriptor. Upon success, execution returns
148 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800149 *
Ben Giladc5b22692014-02-18 20:03:22 -0800150 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800151 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800152 */
Evan Charltona05805b2014-03-05 08:21:46 -0800153 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800154 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800155 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700156 // additional information from the call service, but for now we just need one to pass
157 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700158 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800159
Evan Charltona05805b2014-03-05 08:21:46 -0800160 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800161 }
162
163 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800164 * Validates the specified call and, upon no objection to connect it, adds the new call to the
165 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
166 *
167 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700168 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800169 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700170 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800171 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700172 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800173
Sailesh Nepalce704b92014-03-17 18:31:43 -0700174 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800175 ContactInfo contactInfo = call.getContactInfo();
176 for (IncomingCallValidator validator : mIncomingCallValidators) {
177 if (!validator.isValid(handle, contactInfo)) {
178 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800179 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800180 return;
181 }
182 }
183
184 // No objection to accept the incoming call, proceed with potentially connecting it (based
185 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700186 call.setHandle(callInfo.getHandle());
187 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800188 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700189 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800190
Sailesh Nepal810735e2014-03-18 18:15:46 -0700191 /**
192 * Called when an incoming call was not connected.
193 *
194 * @param call The incoming call.
195 */
196 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700197 // Incoming calls are not added unless they are successful. We set the state and disconnect
198 // cause just as a matter of good bookkeeping. We do not use the specific methods for
199 // setting those values so that we do not trigger CallsManagerListener events.
200 // TODO: Needs more specific disconnect error for this case.
201 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
202 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800203 }
204
205 /**
Yorke Lee33501632014-03-17 19:24:12 -0700206 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800207 *
Yorke Lee33501632014-03-17 19:24:12 -0700208 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800209 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700210 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700211 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800212 */
Yorke Lee33501632014-03-17 19:24:12 -0700213 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800214 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800215 if (!validator.isValid(handle, contactInfo)) {
216 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800217 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800218 return;
219 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800220 }
221
Yorke Lee33501632014-03-17 19:24:12 -0700222 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
223
224 if (gatewayInfo == null) {
225 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
226 } else {
227 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
228 Log.pii(uriHandle), Log.pii(handle));
229 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700230 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700231 setCallState(call, CallState.DIALING);
232 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800233 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800234 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800235
236 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700237 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800238 *
239 * @param call The new outgoing call.
240 */
241 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700242 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700243 if (mCalls.contains(call)) {
244 // The call's CallService has been updated.
245 for (CallsManagerListener listener : mListeners) {
246 listener.onCallServiceChanged(call, null, call.getCallService());
247 }
248 } else if (mPendingHandoffCalls.contains(call)) {
249 updateHandoffCallServiceDescriptor(call.getOriginalCall(), call);
250 }
Santos Cordon681663d2014-01-30 04:32:15 -0800251 }
252
253 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700254 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800255 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700256 * @param call The outgoing call.
257 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800258 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700259 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
260 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
261 if (isAborted) {
262 call.abort();
263 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700264 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700265 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700266 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700267 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700268 }
Yorke Leef98fb572014-03-05 10:56:55 -0800269 }
270
271 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800272 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
273 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
274 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800275 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700276 void answerCall(Call call) {
277 if (!mCalls.contains(call)) {
278 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800279 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700280 for (CallsManagerListener listener : mListeners) {
281 listener.onIncomingCallAnswered(call);
282 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800283
Santos Cordon61d0f702014-02-19 02:52:23 -0800284 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700285 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800286 call.answer();
287 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800288 }
289
290 /**
291 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
292 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
293 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800294 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700295 void rejectCall(Call call) {
296 if (!mCalls.contains(call)) {
297 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800298 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700299 for (CallsManagerListener listener : mListeners) {
300 listener.onIncomingCallRejected(call);
301 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800302 call.reject();
303 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800304 }
305
306 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700307 * Instructs Telecomm to play the specified DTMF tone within the specified call.
308 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700309 * @param digit The DTMF digit to play.
310 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700311 void playDtmfTone(Call call, char digit) {
312 if (!mCalls.contains(call)) {
313 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700314 } else {
315 call.playDtmfTone(digit);
316 }
317 }
318
319 /**
320 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700321 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700322 void stopDtmfTone(Call call) {
323 if (!mCalls.contains(call)) {
324 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700325 } else {
326 call.stopDtmfTone();
327 }
328 }
329
330 /**
331 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700332 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700333 void postDialContinue(Call call) {
334 if (!mCalls.contains(call)) {
335 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700336 } else {
337 // TODO(ihab): Implement this from this level on downwards
338 // call.postDialContinue();
339 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
340 }
341 }
342
343 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800344 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
345 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
346 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800347 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700348 void disconnectCall(Call call) {
349 if (!mCalls.contains(call)) {
350 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800351 } else {
352 call.disconnect();
353 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800354 }
Santos Cordon681663d2014-01-30 04:32:15 -0800355
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700356 /**
357 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
358 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
359 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700360 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700361 void holdCall(Call call) {
362 if (!mCalls.contains(call)) {
363 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700364 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700365 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700366 call.hold();
367 }
368 }
369
370 /**
371 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
372 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
373 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700374 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700375 void unholdCall(Call call) {
376 if (!mCalls.contains(call)) {
377 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700378 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700379 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700380 call.unhold();
381 }
382 }
383
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700384 /** Called by the in-call UI to change the mute state. */
385 void mute(boolean shouldMute) {
386 mCallAudioManager.mute(shouldMute);
387 }
388
389 /**
390 * Called by the in-call UI to change the audio route, for example to change from earpiece to
391 * speaker phone.
392 */
393 void setAudioRoute(int route) {
394 mCallAudioManager.setAudioRoute(route);
395 }
396
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700397 void startHandoffForCall(Call originalCall) {
398 if (!mCalls.contains(originalCall)) {
399 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
400 return;
401 }
402
403 for (Call handoffCall : mPendingHandoffCalls) {
404 if (handoffCall.getOriginalCall() == originalCall) {
405 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
406 return;
407 }
408 }
409
410 // Create a new call to be placed in the background. If handoff is successful then the
411 // original call will live on but its state will be updated to the new call's state. In
412 // particular the original call's call service will be updated to the new call's call
413 // service.
414 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
415 originalCall.getGatewayInfo(), false);
416 tempCall.setOriginalCall(originalCall);
417 tempCall.setExtras(originalCall.getExtras());
418 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
419 mPendingHandoffCalls.add(tempCall);
420 Log.d(this, "Placing handoff call");
421 mSwitchboard.placeOutgoingCall(tempCall);
422 }
423
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700424 /** Called when the audio state changes. */
425 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
426 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700427 for (CallsManagerListener listener : mListeners) {
428 listener.onAudioStateChanged(oldAudioState, newAudioState);
429 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700430 }
431
Sailesh Nepale59bb192014-04-01 18:33:59 -0700432 void markCallAsRinging(Call call) {
433 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800434 }
435
Sailesh Nepale59bb192014-04-01 18:33:59 -0700436 void markCallAsDialing(Call call) {
437 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800438 }
439
Sailesh Nepale59bb192014-04-01 18:33:59 -0700440 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700441 if (call.getConnectTimeMillis() == 0) {
442 call.setConnectTimeMillis(System.currentTimeMillis());
443 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700444 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700445
446 if (mPendingHandoffCalls.contains(call)) {
447 completeHandoff(call);
448 }
Santos Cordon681663d2014-01-30 04:32:15 -0800449 }
450
Sailesh Nepale59bb192014-04-01 18:33:59 -0700451 void markCallAsOnHold(Call call) {
452 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700453 }
454
Santos Cordon049b7b62014-01-30 05:34:26 -0800455 /**
456 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
457 * live call, then also disconnect from the in-call controller.
458 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700459 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
460 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800461 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700462 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
463 call.setDisconnectCause(disconnectCause, disconnectMessage);
464 setCallState(call, CallState.DISCONNECTED);
465 removeCall(call);
Ben Gilada0d9f752014-02-26 11:49:03 -0800466 }
467
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700468 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700469 if (!mCalls.contains(call)) {
470 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
471 return;
472 }
473
474 if (extras == null) {
475 call.setExtras(Bundle.EMPTY);
476 } else {
477 call.setExtras(extras);
478 }
479
480 Uri oldHandle = call.getHandoffHandle();
481 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
482 if (!areUriEqual(oldHandle, handle)) {
483 call.setHandoffHandle(handle);
484 for (CallsManagerListener listener : mListeners) {
485 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
486 }
487 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700488 }
489
Ben Gilada0d9f752014-02-26 11:49:03 -0800490 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700491 * Cleans up any calls currently associated with the specified call service when the
492 * call-service binder disconnects unexpectedly.
493 *
494 * @param callService The call service that disconnected.
495 */
496 void handleCallServiceDeath(CallServiceWrapper callService) {
497 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700498 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700499 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700500 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700501 }
502 }
503 }
504
505 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800506 * Adds the specified call to the main list of live calls.
507 *
508 * @param call The call to add.
509 */
510 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700511 mCalls.add(call);
Santos Cordona56f2762014-03-24 15:55:53 -0700512 for (CallsManagerListener listener : mListeners) {
513 listener.onCallAdded(call);
514 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700515 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800516 }
517
Sailesh Nepal810735e2014-03-18 18:15:46 -0700518 private void removeCall(Call call) {
519 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700520 call.clearCallServiceSelector();
521
522 boolean shouldNotify = false;
523 if (mCalls.contains(call)) {
524 mCalls.remove(call);
525 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700526 cleanUpHandoffCallsForOriginalCall(call);
527 } else if (mPendingHandoffCalls.contains(call)) {
528 Log.v(this, "silently removing handoff call %s", call);
529 mPendingHandoffCalls.remove(call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700530 updateHandoffCallServiceDescriptor(call.getOriginalCall(), null);
Santos Cordona56f2762014-03-24 15:55:53 -0700531 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800532
Sailesh Nepale59bb192014-04-01 18:33:59 -0700533 // Only broadcast changes for calls that are being tracked.
534 if (shouldNotify) {
535 for (CallsManagerListener listener : mListeners) {
536 listener.onCallRemoved(call);
537 }
538 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700539 }
540 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800541
Sailesh Nepal810735e2014-03-18 18:15:46 -0700542 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700543 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700544 *
545 * @param call The call.
546 * @param newState The new state of the call.
547 */
548 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700549 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700550 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700551 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700552 if (newState != oldState) {
553 // Unfortunately, in the telephony world the radio is king. So if the call notifies
554 // us that the call is in a particular state, we allow it even if it doesn't make
555 // sense (e.g., ACTIVE -> RINGING).
556 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
557 // into a well-defined state machine.
558 // TODO(santoscordon): Define expected state transitions here, and log when an
559 // unexpected transition occurs.
560 call.setState(newState);
561
562 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700563 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700564 for (CallsManagerListener listener : mListeners) {
565 listener.onCallStateChanged(call, oldState, newState);
566 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700567 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800568 }
569 }
570 }
571
572 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700573 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800574 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700575 private void updateForegroundCall() {
576 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700577 for (Call call : mCalls) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700578 // Incoming ringing calls have priority.
579 if (call.getState() == CallState.RINGING) {
580 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800581 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700582 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700583 if (call.isAlive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700584 newForegroundCall = call;
585 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800586 }
587 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800588
Sailesh Nepal810735e2014-03-18 18:15:46 -0700589 if (newForegroundCall != mForegroundCall) {
590 Call oldForegroundCall = mForegroundCall;
591 mForegroundCall = newForegroundCall;
592
Santos Cordona56f2762014-03-24 15:55:53 -0700593 for (CallsManagerListener listener : mListeners) {
594 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
595 }
Santos Cordon681663d2014-01-30 04:32:15 -0800596 }
597 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700598
599 private void completeHandoff(Call handoffCall) {
600 Call originalCall = handoffCall.getOriginalCall();
601 Log.v(this, "complete handoff, %s -> %s", handoffCall, originalCall);
602
603 // Disconnect.
604 originalCall.disconnect();
605
606 // Synchronize.
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700607 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700608 setCallState(originalCall, handoffCall.getState());
609
610 // Remove the transient handoff call object (don't disconnect because the call is still
611 // live).
612 removeCall(handoffCall);
613
614 // Force the foreground call changed notification to be sent.
615 for (CallsManagerListener listener : mListeners) {
616 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
617 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700618
619 updateHandoffCallServiceDescriptor(originalCall, null);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700620 }
621
622 /** Makes sure there are no dangling handoff calls. */
623 private void cleanUpHandoffCallsForOriginalCall(Call originalCall) {
624 for (Call handoffCall : ImmutableList.copyOf((mPendingHandoffCalls))) {
625 if (handoffCall.getOriginalCall() == originalCall) {
626 Log.d(this, "cancelling handoff call %s for originalCall: %s", handoffCall,
627 originalCall);
628 if (handoffCall.getState() == CallState.NEW) {
629 handoffCall.abort();
630 handoffCall.setState(CallState.ABORTED);
631 } else {
632 handoffCall.disconnect();
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700633 handoffCall.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700634 handoffCall.setState(CallState.DISCONNECTED);
635 }
636 removeCall(handoffCall);
637 }
638 }
639 }
640
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700641 private void updateHandoffCallServiceDescriptor(Call originalCall, Call handoffCall) {
642 Log.v(this, "updateHandoffCallServiceDescriptor, originalCall: %s, handoffCall: %s",
643 originalCall, handoffCall);
644 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
645 CallServiceDescriptor newDescriptor = null;
646 if (handoffCall != null && handoffCall.getCallService() != null) {
647 newDescriptor = handoffCall.getCallService().getDescriptor();
648 }
649 Log.v(this, "updateHandoffCallServiceDescriptor, pending descriptor: %s -> %s",
650 oldDescriptor, newDescriptor);
651
652 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
653 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
654 for (CallsManagerListener listener : mListeners) {
655 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
656 newDescriptor);
657 }
658 }
659 }
660
661 private static boolean areDescriptorsEqual(
662 CallServiceDescriptor descriptor1,
663 CallServiceDescriptor descriptor2) {
664 if (descriptor1 == null) {
665 return descriptor2 == null;
666 }
667 return descriptor1.equals(descriptor2);
668 }
669
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700670 private static boolean areUriEqual(Uri handle1, Uri handle2) {
671 if (handle1 == null) {
672 return handle2 == null;
673 }
674 return handle1.equals(handle2);
675 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800676}