blob: 64c1d37337d420ec091223f0d67b8591275462c8 [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 Awad50a57132014-05-28 16:49:38 -070063 void onRequestingRingback(Call call, boolean ringback);
Sailesh Nepal810735e2014-03-18 18:15:46 -070064 }
65
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080067
Santos Cordon8e8b8d22013-12-19 14:14:05 -080068 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070069 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
70 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080071 */
Santos Cordonf3671a62014-05-29 21:51:53 -070072 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080073
74 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070075 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
76 * and removed when hadnoff is complete.
77 */
Santos Cordonf3671a62014-05-29 21:51:53 -070078 private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>();
79
80
81 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
82 private final InCallController mInCallController = new InCallController();
83 private final CallAudioManager mCallAudioManager;
84 private final Ringer mRinger;
85 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070086 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070087
88 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070089 * The call the user is currently interacting with. This is the call that should have audio
90 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080091 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070092 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080093
Santos Cordon766d04f2014-05-06 10:28:25 -070094 /** Singleton accessor. */
95 static CallsManager getInstance() {
96 return INSTANCE;
97 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080098
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800100 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 */
102 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700103 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700104
Santos Cordondeb8c892014-05-30 01:38:03 -0700105 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
106 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700107 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonb64c1502014-05-21 21:21:49 -0700108 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700109 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonb64c1502014-05-21 21:21:49 -0700110
Santos Cordondeb8c892014-05-30 01:38:03 -0700111 mListeners.add(statusBarNotifier);
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());
Santos Cordonf3671a62014-05-29 21:51:53 -0700114 mListeners.add(mInCallController);
Santos Cordonb64c1502014-05-21 21:21:49 -0700115 mListeners.add(mRinger);
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
Santos Cordon766d04f2014-05-06 10:28:25 -0700123 @Override
124 public void onSuccessfulOutgoingCall(Call call) {
125 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
126 if (mCalls.contains(call)) {
127 // The call's CallService has been updated.
128 for (CallsManagerListener listener : mListeners) {
129 listener.onCallServiceChanged(call, null, call.getCallService());
130 }
131 } else if (mPendingHandoffCalls.contains(call)) {
132 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
133 call.getCallService().getDescriptor());
Santos Cordon355083c2014-05-23 14:09:32 -0700134 } else {
135 Log.wtf(this, "unexpected successful call notification: %s", call);
136 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700137 }
Santos Cordon355083c2014-05-23 14:09:32 -0700138
139 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700140 }
141
142 @Override
143 public void onFailedOutgoingCall(Call call, boolean isAborted) {
144 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
145 if (isAborted) {
146 setCallState(call, CallState.ABORTED);
147 removeCall(call);
148 } else {
149 // TODO: Replace disconnect cause with more specific disconnect causes.
150 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
151 }
152 }
153
154 @Override
155 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
156 Log.d(this, "onSuccessfulIncomingCall");
157 setCallState(call, callInfo.getState());
158 addCall(call);
159 }
160
161 @Override
162 public void onFailedIncomingCall(Call call) {
163 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800164 }
165
Ihab Awad50a57132014-05-28 16:49:38 -0700166 @Override
167 public void onRequestingRingback(Call call, boolean ringback) {
168 for (CallsManagerListener listener : mListeners) {
169 listener.onRequestingRingback(call, ringback);
170 }
171 }
172
Sailesh Nepal810735e2014-03-18 18:15:46 -0700173 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700174 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700175 }
176
177 Call getForegroundCall() {
178 return mForegroundCall;
179 }
180
Santos Cordonb64c1502014-05-21 21:21:49 -0700181 Ringer getRinger() {
182 return mRinger;
183 }
184
Santos Cordonf3671a62014-05-29 21:51:53 -0700185 InCallController getInCallController() {
186 return mInCallController;
187 }
188
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700189 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700190 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700191 if (call.isEmergencyCall()) {
192 return true;
193 }
194 }
195 return false;
196 }
197
198 CallAudioState getAudioState() {
199 return mCallAudioManager.getAudioState();
200 }
201
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800202 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800203 * Starts the incoming call sequence by having switchboard gather more information about the
204 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700205 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800206 *
Ben Giladc5b22692014-02-18 20:03:22 -0800207 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800208 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800209 */
Evan Charltona05805b2014-03-05 08:21:46 -0800210 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800211 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800212 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700213 // additional information from the call service, but for now we just need one to pass
214 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700215 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700216 // TODO(santoscordon): Move this to be a part of addCall()
217 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800218
Santos Cordon766d04f2014-05-06 10:28:25 -0700219 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800220 }
221
222 /**
Yorke Lee33501632014-03-17 19:24:12 -0700223 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800224 *
Yorke Lee33501632014-03-17 19:24:12 -0700225 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800226 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700227 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700228 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800229 */
Yorke Lee33501632014-03-17 19:24:12 -0700230 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700231 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
232
233 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700234 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700235 } else {
236 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
237 Log.pii(uriHandle), Log.pii(handle));
238 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700239
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700240 Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700241
242 // TODO(santoscordon): Move this to be a part of addCall()
243 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700244 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800245
Santos Cordon766d04f2014-05-06 10:28:25 -0700246 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800247 }
248
249 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800250 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
251 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
252 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800253 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700254 void answerCall(Call call) {
255 if (!mCalls.contains(call)) {
256 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800257 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700258 // If the foreground call is not the ringing call and it is currently isActive() or
259 // DIALING, put it on hold before answering the call.
260 if (mForegroundCall != null && mForegroundCall != call &&
261 (mForegroundCall.isActive() ||
262 mForegroundCall.getState() == CallState.DIALING)) {
263 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
264 mForegroundCall, call);
265 mForegroundCall.hold();
266 // TODO(santoscordon): Wait until we get confirmation of the active call being
267 // on-hold before answering the new call.
268 // TODO(santoscordon): Import logic from CallManager.acceptCall()
269 }
270
Santos Cordona56f2762014-03-24 15:55:53 -0700271 for (CallsManagerListener listener : mListeners) {
272 listener.onIncomingCallAnswered(call);
273 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800274
Santos Cordon61d0f702014-02-19 02:52:23 -0800275 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700276 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800277 call.answer();
278 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800279 }
280
281 /**
282 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
283 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
284 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800285 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700286 void rejectCall(Call call) {
287 if (!mCalls.contains(call)) {
288 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800289 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700290 for (CallsManagerListener listener : mListeners) {
291 listener.onIncomingCallRejected(call);
292 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800293 call.reject();
294 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800295 }
296
297 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700298 * Instructs Telecomm to play the specified DTMF tone within the specified call.
299 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700300 * @param digit The DTMF digit to play.
301 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700302 void playDtmfTone(Call call, char digit) {
303 if (!mCalls.contains(call)) {
304 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700305 } else {
306 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700307 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700308 }
309 }
310
311 /**
312 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700313 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700314 void stopDtmfTone(Call call) {
315 if (!mCalls.contains(call)) {
316 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700317 } else {
318 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700319 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700320 }
321 }
322
323 /**
324 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700325 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700326 void postDialContinue(Call call) {
327 if (!mCalls.contains(call)) {
328 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700329 } else {
330 // TODO(ihab): Implement this from this level on downwards
331 // call.postDialContinue();
332 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
333 }
334 }
335
336 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800337 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
338 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
339 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800340 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700341 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700342 Log.v(this, "disconnectCall %s", call);
343
Sailesh Nepale59bb192014-04-01 18:33:59 -0700344 if (!mCalls.contains(call)) {
345 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800346 } else {
347 call.disconnect();
348 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800349 }
Santos Cordon681663d2014-01-30 04:32:15 -0800350
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700351 /**
352 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
353 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
354 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700355 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700356 void holdCall(Call call) {
357 if (!mCalls.contains(call)) {
358 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700359 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700360 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700361 call.hold();
362 }
363 }
364
365 /**
366 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
367 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
368 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700369 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700370 void unholdCall(Call call) {
371 if (!mCalls.contains(call)) {
372 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700373 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700374 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700375 call.unhold();
376 }
377 }
378
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700379 /** Called by the in-call UI to change the mute state. */
380 void mute(boolean shouldMute) {
381 mCallAudioManager.mute(shouldMute);
382 }
383
384 /**
385 * Called by the in-call UI to change the audio route, for example to change from earpiece to
386 * speaker phone.
387 */
388 void setAudioRoute(int route) {
389 mCallAudioManager.setAudioRoute(route);
390 }
391
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700392 void startHandoffForCall(Call originalCall) {
393 if (!mCalls.contains(originalCall)) {
394 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
395 return;
396 }
397
398 for (Call handoffCall : mPendingHandoffCalls) {
399 if (handoffCall.getOriginalCall() == originalCall) {
400 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
401 return;
402 }
403 }
404
405 // Create a new call to be placed in the background. If handoff is successful then the
406 // original call will live on but its state will be updated to the new call's state. In
407 // particular the original call's call service will be updated to the new call's call
408 // service.
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700409 Call tempCall =
410 new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700411 tempCall.setOriginalCall(originalCall);
412 tempCall.setExtras(originalCall.getExtras());
413 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
414 mPendingHandoffCalls.add(tempCall);
Santos Cordon355083c2014-05-23 14:09:32 -0700415 tempCall.addListener(this);
416
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700417 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700418 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700419 }
420
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700421 /** Called when the audio state changes. */
422 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
423 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700424 for (CallsManagerListener listener : mListeners) {
425 listener.onAudioStateChanged(oldAudioState, newAudioState);
426 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700427 }
428
Sailesh Nepale59bb192014-04-01 18:33:59 -0700429 void markCallAsRinging(Call call) {
430 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800431 }
432
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 void markCallAsDialing(Call call) {
434 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800435 }
436
Sailesh Nepale59bb192014-04-01 18:33:59 -0700437 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700438 if (call.getConnectTimeMillis() == 0) {
439 call.setConnectTimeMillis(System.currentTimeMillis());
440 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700441 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700442
443 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700444 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700445 }
Santos Cordon681663d2014-01-30 04:32:15 -0800446 }
447
Sailesh Nepale59bb192014-04-01 18:33:59 -0700448 void markCallAsOnHold(Call call) {
449 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700450 }
451
Santos Cordon049b7b62014-01-30 05:34:26 -0800452 /**
453 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
454 * live call, then also disconnect from the in-call controller.
455 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700456 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
457 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800458 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700459 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
460 call.setDisconnectCause(disconnectCause, disconnectMessage);
461 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700462
463 // Only remove the call if handoff is not pending.
464 if (call.getHandoffCallServiceDescriptor() == null) {
465 removeCall(call);
466 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800467 }
468
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700469 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700470 if (!mCalls.contains(call)) {
471 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
472 return;
473 }
474
475 if (extras == null) {
476 call.setExtras(Bundle.EMPTY);
477 } else {
478 call.setExtras(extras);
479 }
480
481 Uri oldHandle = call.getHandoffHandle();
482 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
483 if (!areUriEqual(oldHandle, handle)) {
484 call.setHandoffHandle(handle);
485 for (CallsManagerListener listener : mListeners) {
486 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
487 }
488 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700489 }
490
Ben Gilada0d9f752014-02-26 11:49:03 -0800491 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700492 * Cleans up any calls currently associated with the specified call service when the
493 * call-service binder disconnects unexpectedly.
494 *
495 * @param callService The call service that disconnected.
496 */
497 void handleCallServiceDeath(CallServiceWrapper callService) {
498 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700499 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700500 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700501 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700502 }
503 }
504 }
505
Santos Cordondeb8c892014-05-30 01:38:03 -0700506 boolean hasAnyCalls() {
507 return !mCalls.isEmpty();
508 }
509
Santos Cordonc7e85d42014-05-22 02:51:48 -0700510 boolean hasActiveOrHoldingCall() {
511 for (Call call : mCalls) {
512 CallState state = call.getState();
513 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
514 return true;
515 }
516 }
517 return false;
518 }
519
520 boolean hasRingingCall() {
521 for (Call call : mCalls) {
522 if (call.getState() == CallState.RINGING) {
523 return true;
524 }
525 }
526 return false;
527 }
528
Santos Cordondeb8c892014-05-30 01:38:03 -0700529 boolean onMediaButton(int type) {
530 if (hasAnyCalls()) {
531 if (HeadsetMediaButton.SHORT_PRESS == type) {
532 Call ringingCall = getFirstCallWithState(CallState.RINGING);
533 if (ringingCall == null) {
534 mCallAudioManager.toggleMute();
535 return true;
536 } else {
537 ringingCall.answer();
538 return true;
539 }
540 } else if (HeadsetMediaButton.LONG_PRESS == type) {
541 Log.d(this, "handleHeadsetHook: longpress -> hangup");
542 Call callToHangup = getFirstCallWithState(
543 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
544 if (callToHangup != null) {
545 callToHangup.disconnect();
546 return true;
547 }
548 }
549 }
550 return false;
551 }
552
553 /**
554 * Returns the first call that it finds with the given states. The states are treated as having
555 * priority order so that any call with the first state will be returned before any call with
556 * states listed later in the parameter list.
557 */
558 private Call getFirstCallWithState(CallState... states) {
559 for (CallState currentState : states) {
560 for (Call call : mCalls) {
561 if (currentState == call.getState()) {
562 return call;
563 }
564 }
565 }
566 return null;
567 }
568
Santos Cordon4b2c1192014-03-19 18:15:38 -0700569 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800570 * Adds the specified call to the main list of live calls.
571 *
572 * @param call The call to add.
573 */
574 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700575 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700576
577 // TODO(santoscordon): Update mForegroundCall prior to invoking
578 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700579 for (CallsManagerListener listener : mListeners) {
580 listener.onCallAdded(call);
581 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700582 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800583 }
584
Sailesh Nepal810735e2014-03-18 18:15:46 -0700585 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700586 // If a handoff is pending then the original call shouldn't be removed.
587 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700588 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700589
Santos Cordon766d04f2014-05-06 10:28:25 -0700590 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700591 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700592 call.clearCallServiceSelector();
593
594 boolean shouldNotify = false;
595 if (mCalls.contains(call)) {
596 mCalls.remove(call);
597 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700598 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700599 Log.v(this, "removeCall, marking handoff call as failed");
600 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700601 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800602
Sailesh Nepale59bb192014-04-01 18:33:59 -0700603 // Only broadcast changes for calls that are being tracked.
604 if (shouldNotify) {
605 for (CallsManagerListener listener : mListeners) {
606 listener.onCallRemoved(call);
607 }
608 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700609 }
610 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800611
Sailesh Nepal810735e2014-03-18 18:15:46 -0700612 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700613 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700614 *
615 * @param call The call.
616 * @param newState The new state of the call.
617 */
618 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700619 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700620 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700621 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700622 if (newState != oldState) {
623 // Unfortunately, in the telephony world the radio is king. So if the call notifies
624 // us that the call is in a particular state, we allow it even if it doesn't make
625 // sense (e.g., ACTIVE -> RINGING).
626 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
627 // into a well-defined state machine.
628 // TODO(santoscordon): Define expected state transitions here, and log when an
629 // unexpected transition occurs.
630 call.setState(newState);
631
632 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700633 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700634 for (CallsManagerListener listener : mListeners) {
635 listener.onCallStateChanged(call, oldState, newState);
636 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700637 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800638 }
639 }
640 }
641
642 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700643 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800644 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700645 private void updateForegroundCall() {
646 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700647 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700648 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
649 // of its state will be foreground by default and instead the call service should be
650 // notified when its calls enter and exit foreground state. Foreground will mean that
651 // the call should play audio and listen to microphone if it wants.
652
653 // Active calls have priority.
654 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700655 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800656 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700657 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700658
659 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700660 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700661 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800662 }
663 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800664
Sailesh Nepal810735e2014-03-18 18:15:46 -0700665 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700666 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700667 Call oldForegroundCall = mForegroundCall;
668 mForegroundCall = newForegroundCall;
669
Santos Cordona56f2762014-03-24 15:55:53 -0700670 for (CallsManagerListener listener : mListeners) {
671 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
672 }
Santos Cordon681663d2014-01-30 04:32:15 -0800673 }
674 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700675
Sailesh Nepal4857f472014-04-07 22:26:27 -0700676 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700677 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700678 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
679 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700680
Sailesh Nepal4857f472014-04-07 22:26:27 -0700681 // Remove the transient handoff call object (don't disconnect because the call could still
682 // be live).
683 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon355083c2014-05-23 14:09:32 -0700684 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700685
Sailesh Nepal4857f472014-04-07 22:26:27 -0700686 if (wasSuccessful) {
687 // Disconnect.
688 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700689
Sailesh Nepal4857f472014-04-07 22:26:27 -0700690 // Synchronize.
691 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
692 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700693
Sailesh Nepal4857f472014-04-07 22:26:27 -0700694 // Force the foreground call changed notification to be sent.
695 for (CallsManagerListener listener : mListeners) {
696 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
697 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700698
Sailesh Nepal4857f472014-04-07 22:26:27 -0700699 updateHandoffCallServiceDescriptor(originalCall, null);
700 } else {
701 updateHandoffCallServiceDescriptor(originalCall, null);
702 if (originalCall.getState() == CallState.DISCONNECTED ||
703 originalCall.getState() == CallState.ABORTED) {
704 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700705 }
706 }
707 }
708
Sailesh Nepal4857f472014-04-07 22:26:27 -0700709 private void updateHandoffCallServiceDescriptor(
710 Call originalCall,
711 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700712 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700713 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
714 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700715
716 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
717 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
718 for (CallsManagerListener listener : mListeners) {
719 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
720 newDescriptor);
721 }
722 }
723 }
724
725 private static boolean areDescriptorsEqual(
726 CallServiceDescriptor descriptor1,
727 CallServiceDescriptor descriptor2) {
728 if (descriptor1 == null) {
729 return descriptor2 == null;
730 }
731 return descriptor1.equals(descriptor2);
732 }
733
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700734 private static boolean areUriEqual(Uri handle1, Uri handle2) {
735 if (handle1 == null) {
736 return handle2 == null;
737 }
738 return handle1.equals(handle2);
739 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800740}