blob: 727e44d9b032f0f117328d0bf289457f8891acef [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 Cordon81289982014-06-03 16:03:08 -0700121 mListeners.add(mHeadsetMediaButton);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800122 }
123
Santos Cordon766d04f2014-05-06 10:28:25 -0700124 @Override
125 public void onSuccessfulOutgoingCall(Call call) {
126 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
127 if (mCalls.contains(call)) {
128 // The call's CallService has been updated.
129 for (CallsManagerListener listener : mListeners) {
130 listener.onCallServiceChanged(call, null, call.getCallService());
131 }
132 } else if (mPendingHandoffCalls.contains(call)) {
133 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
134 call.getCallService().getDescriptor());
Santos Cordon355083c2014-05-23 14:09:32 -0700135 } else {
136 Log.wtf(this, "unexpected successful call notification: %s", call);
137 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700138 }
Santos Cordon355083c2014-05-23 14:09:32 -0700139
140 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700141 }
142
143 @Override
144 public void onFailedOutgoingCall(Call call, boolean isAborted) {
145 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
146 if (isAborted) {
147 setCallState(call, CallState.ABORTED);
148 removeCall(call);
149 } else {
150 // TODO: Replace disconnect cause with more specific disconnect causes.
151 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
152 }
153 }
154
155 @Override
156 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
157 Log.d(this, "onSuccessfulIncomingCall");
158 setCallState(call, callInfo.getState());
159 addCall(call);
160 }
161
162 @Override
163 public void onFailedIncomingCall(Call call) {
164 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800165 }
166
Ihab Awad50a57132014-05-28 16:49:38 -0700167 @Override
168 public void onRequestingRingback(Call call, boolean ringback) {
169 for (CallsManagerListener listener : mListeners) {
170 listener.onRequestingRingback(call, ringback);
171 }
172 }
173
Sailesh Nepal810735e2014-03-18 18:15:46 -0700174 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700175 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700176 }
177
178 Call getForegroundCall() {
179 return mForegroundCall;
180 }
181
Santos Cordonb64c1502014-05-21 21:21:49 -0700182 Ringer getRinger() {
183 return mRinger;
184 }
185
Santos Cordonf3671a62014-05-29 21:51:53 -0700186 InCallController getInCallController() {
187 return mInCallController;
188 }
189
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700190 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700191 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700192 if (call.isEmergencyCall()) {
193 return true;
194 }
195 }
196 return false;
197 }
198
199 CallAudioState getAudioState() {
200 return mCallAudioManager.getAudioState();
201 }
202
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800203 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800204 * Starts the incoming call sequence by having switchboard gather more information about the
205 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700206 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800207 *
Ben Giladc5b22692014-02-18 20:03:22 -0800208 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800209 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800210 */
Evan Charltona05805b2014-03-05 08:21:46 -0800211 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800212 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800213 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700214 // additional information from the call service, but for now we just need one to pass
215 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700216 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700217 // TODO(santoscordon): Move this to be a part of addCall()
218 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800219
Santos Cordon766d04f2014-05-06 10:28:25 -0700220 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800221 }
222
223 /**
Yorke Lee33501632014-03-17 19:24:12 -0700224 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800225 *
Yorke Lee33501632014-03-17 19:24:12 -0700226 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800227 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700228 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700229 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800230 */
Yorke Lee33501632014-03-17 19:24:12 -0700231 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700232 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
233
234 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700235 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700236 } else {
237 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
238 Log.pii(uriHandle), Log.pii(handle));
239 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700240
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700241 Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700242
243 // TODO(santoscordon): Move this to be a part of addCall()
244 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700245 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800246
Santos Cordon766d04f2014-05-06 10:28:25 -0700247 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800248 }
249
250 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800251 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
252 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
253 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800254 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700255 void answerCall(Call call) {
256 if (!mCalls.contains(call)) {
257 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800258 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700259 // If the foreground call is not the ringing call and it is currently isActive() or
260 // DIALING, put it on hold before answering the call.
261 if (mForegroundCall != null && mForegroundCall != call &&
262 (mForegroundCall.isActive() ||
263 mForegroundCall.getState() == CallState.DIALING)) {
264 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
265 mForegroundCall, call);
266 mForegroundCall.hold();
267 // TODO(santoscordon): Wait until we get confirmation of the active call being
268 // on-hold before answering the new call.
269 // TODO(santoscordon): Import logic from CallManager.acceptCall()
270 }
271
Santos Cordona56f2762014-03-24 15:55:53 -0700272 for (CallsManagerListener listener : mListeners) {
273 listener.onIncomingCallAnswered(call);
274 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800275
Santos Cordon61d0f702014-02-19 02:52:23 -0800276 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700277 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800278 call.answer();
279 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800280 }
281
282 /**
283 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
284 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
285 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800286 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700287 void rejectCall(Call call) {
288 if (!mCalls.contains(call)) {
289 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800290 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700291 for (CallsManagerListener listener : mListeners) {
292 listener.onIncomingCallRejected(call);
293 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800294 call.reject();
295 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800296 }
297
298 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700299 * Instructs Telecomm to play the specified DTMF tone within the specified call.
300 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700301 * @param digit The DTMF digit to play.
302 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700303 void playDtmfTone(Call call, char digit) {
304 if (!mCalls.contains(call)) {
305 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700306 } else {
307 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700308 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700309 }
310 }
311
312 /**
313 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700314 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700315 void stopDtmfTone(Call call) {
316 if (!mCalls.contains(call)) {
317 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700318 } else {
319 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700320 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700321 }
322 }
323
324 /**
325 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700326 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700327 void postDialContinue(Call call) {
328 if (!mCalls.contains(call)) {
329 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700330 } else {
331 // TODO(ihab): Implement this from this level on downwards
332 // call.postDialContinue();
333 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
334 }
335 }
336
337 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800338 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
339 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
340 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800341 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700342 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700343 Log.v(this, "disconnectCall %s", call);
344
Sailesh Nepale59bb192014-04-01 18:33:59 -0700345 if (!mCalls.contains(call)) {
346 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800347 } else {
348 call.disconnect();
349 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800350 }
Santos Cordon681663d2014-01-30 04:32:15 -0800351
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700352 /**
353 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
354 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
355 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700356 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700357 void holdCall(Call call) {
358 if (!mCalls.contains(call)) {
359 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700360 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700361 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700362 call.hold();
363 }
364 }
365
366 /**
367 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
368 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
369 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700370 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700371 void unholdCall(Call call) {
372 if (!mCalls.contains(call)) {
373 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700374 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700375 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700376 call.unhold();
377 }
378 }
379
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700380 /** Called by the in-call UI to change the mute state. */
381 void mute(boolean shouldMute) {
382 mCallAudioManager.mute(shouldMute);
383 }
384
385 /**
386 * Called by the in-call UI to change the audio route, for example to change from earpiece to
387 * speaker phone.
388 */
389 void setAudioRoute(int route) {
390 mCallAudioManager.setAudioRoute(route);
391 }
392
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700393 void startHandoffForCall(Call originalCall) {
394 if (!mCalls.contains(originalCall)) {
395 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
396 return;
397 }
398
399 for (Call handoffCall : mPendingHandoffCalls) {
400 if (handoffCall.getOriginalCall() == originalCall) {
401 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
402 return;
403 }
404 }
405
406 // Create a new call to be placed in the background. If handoff is successful then the
407 // original call will live on but its state will be updated to the new call's state. In
408 // particular the original call's call service will be updated to the new call's call
409 // service.
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700410 Call tempCall =
411 new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700412 tempCall.setOriginalCall(originalCall);
413 tempCall.setExtras(originalCall.getExtras());
414 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
415 mPendingHandoffCalls.add(tempCall);
Santos Cordon355083c2014-05-23 14:09:32 -0700416 tempCall.addListener(this);
417
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700418 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700419 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700420 }
421
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700422 /** Called when the audio state changes. */
423 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
424 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700425 for (CallsManagerListener listener : mListeners) {
426 listener.onAudioStateChanged(oldAudioState, newAudioState);
427 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700428 }
429
Sailesh Nepale59bb192014-04-01 18:33:59 -0700430 void markCallAsRinging(Call call) {
431 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800432 }
433
Sailesh Nepale59bb192014-04-01 18:33:59 -0700434 void markCallAsDialing(Call call) {
435 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800436 }
437
Sailesh Nepale59bb192014-04-01 18:33:59 -0700438 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700439 if (call.getConnectTimeMillis() == 0) {
440 call.setConnectTimeMillis(System.currentTimeMillis());
441 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700442 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700443
444 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700445 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700446 }
Santos Cordon681663d2014-01-30 04:32:15 -0800447 }
448
Sailesh Nepale59bb192014-04-01 18:33:59 -0700449 void markCallAsOnHold(Call call) {
450 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700451 }
452
Santos Cordon049b7b62014-01-30 05:34:26 -0800453 /**
454 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
455 * live call, then also disconnect from the in-call controller.
456 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700457 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
458 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800459 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700460 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
461 call.setDisconnectCause(disconnectCause, disconnectMessage);
462 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700463
464 // Only remove the call if handoff is not pending.
465 if (call.getHandoffCallServiceDescriptor() == null) {
466 removeCall(call);
467 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800468 }
469
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700470 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700471 if (!mCalls.contains(call)) {
472 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
473 return;
474 }
475
476 if (extras == null) {
477 call.setExtras(Bundle.EMPTY);
478 } else {
479 call.setExtras(extras);
480 }
481
482 Uri oldHandle = call.getHandoffHandle();
483 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
484 if (!areUriEqual(oldHandle, handle)) {
485 call.setHandoffHandle(handle);
486 for (CallsManagerListener listener : mListeners) {
487 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
488 }
489 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700490 }
491
Ben Gilada0d9f752014-02-26 11:49:03 -0800492 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700493 * Cleans up any calls currently associated with the specified call service when the
494 * call-service binder disconnects unexpectedly.
495 *
496 * @param callService The call service that disconnected.
497 */
498 void handleCallServiceDeath(CallServiceWrapper callService) {
499 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700500 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700501 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700502 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700503 }
504 }
505 }
506
Santos Cordondeb8c892014-05-30 01:38:03 -0700507 boolean hasAnyCalls() {
508 return !mCalls.isEmpty();
509 }
510
Santos Cordonc7e85d42014-05-22 02:51:48 -0700511 boolean hasActiveOrHoldingCall() {
512 for (Call call : mCalls) {
513 CallState state = call.getState();
514 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
515 return true;
516 }
517 }
518 return false;
519 }
520
521 boolean hasRingingCall() {
522 for (Call call : mCalls) {
523 if (call.getState() == CallState.RINGING) {
524 return true;
525 }
526 }
527 return false;
528 }
529
Santos Cordondeb8c892014-05-30 01:38:03 -0700530 boolean onMediaButton(int type) {
531 if (hasAnyCalls()) {
532 if (HeadsetMediaButton.SHORT_PRESS == type) {
533 Call ringingCall = getFirstCallWithState(CallState.RINGING);
534 if (ringingCall == null) {
535 mCallAudioManager.toggleMute();
536 return true;
537 } else {
538 ringingCall.answer();
539 return true;
540 }
541 } else if (HeadsetMediaButton.LONG_PRESS == type) {
542 Log.d(this, "handleHeadsetHook: longpress -> hangup");
543 Call callToHangup = getFirstCallWithState(
544 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
545 if (callToHangup != null) {
546 callToHangup.disconnect();
547 return true;
548 }
549 }
550 }
551 return false;
552 }
553
554 /**
555 * Returns the first call that it finds with the given states. The states are treated as having
556 * priority order so that any call with the first state will be returned before any call with
557 * states listed later in the parameter list.
558 */
559 private Call getFirstCallWithState(CallState... states) {
560 for (CallState currentState : states) {
561 for (Call call : mCalls) {
562 if (currentState == call.getState()) {
563 return call;
564 }
565 }
566 }
567 return null;
568 }
569
Santos Cordon4b2c1192014-03-19 18:15:38 -0700570 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800571 * Adds the specified call to the main list of live calls.
572 *
573 * @param call The call to add.
574 */
575 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700576 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700577
578 // TODO(santoscordon): Update mForegroundCall prior to invoking
579 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700580 for (CallsManagerListener listener : mListeners) {
581 listener.onCallAdded(call);
582 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700583 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800584 }
585
Sailesh Nepal810735e2014-03-18 18:15:46 -0700586 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700587 // If a handoff is pending then the original call shouldn't be removed.
588 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700589 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700590
Santos Cordon766d04f2014-05-06 10:28:25 -0700591 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700592 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700593 call.clearCallServiceSelector();
594
595 boolean shouldNotify = false;
596 if (mCalls.contains(call)) {
597 mCalls.remove(call);
598 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700599 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700600 Log.v(this, "removeCall, marking handoff call as failed");
601 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700602 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800603
Sailesh Nepale59bb192014-04-01 18:33:59 -0700604 // Only broadcast changes for calls that are being tracked.
605 if (shouldNotify) {
606 for (CallsManagerListener listener : mListeners) {
607 listener.onCallRemoved(call);
608 }
609 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700610 }
611 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800612
Sailesh Nepal810735e2014-03-18 18:15:46 -0700613 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700614 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700615 *
616 * @param call The call.
617 * @param newState The new state of the call.
618 */
619 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700620 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700621 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700622 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700623 if (newState != oldState) {
624 // Unfortunately, in the telephony world the radio is king. So if the call notifies
625 // us that the call is in a particular state, we allow it even if it doesn't make
626 // sense (e.g., ACTIVE -> RINGING).
627 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
628 // into a well-defined state machine.
629 // TODO(santoscordon): Define expected state transitions here, and log when an
630 // unexpected transition occurs.
631 call.setState(newState);
632
633 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700634 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700635 for (CallsManagerListener listener : mListeners) {
636 listener.onCallStateChanged(call, oldState, newState);
637 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700638 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800639 }
640 }
641 }
642
643 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700644 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800645 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700646 private void updateForegroundCall() {
647 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700648 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700649 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
650 // of its state will be foreground by default and instead the call service should be
651 // notified when its calls enter and exit foreground state. Foreground will mean that
652 // the call should play audio and listen to microphone if it wants.
653
654 // Active calls have priority.
655 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700656 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800657 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700658 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700659
660 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700661 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700662 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800663 }
664 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800665
Sailesh Nepal810735e2014-03-18 18:15:46 -0700666 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700667 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700668 Call oldForegroundCall = mForegroundCall;
669 mForegroundCall = newForegroundCall;
670
Santos Cordona56f2762014-03-24 15:55:53 -0700671 for (CallsManagerListener listener : mListeners) {
672 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
673 }
Santos Cordon681663d2014-01-30 04:32:15 -0800674 }
675 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700676
Sailesh Nepal4857f472014-04-07 22:26:27 -0700677 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700678 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700679 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
680 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700681
Sailesh Nepal4857f472014-04-07 22:26:27 -0700682 // Remove the transient handoff call object (don't disconnect because the call could still
683 // be live).
684 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon355083c2014-05-23 14:09:32 -0700685 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700686
Sailesh Nepal4857f472014-04-07 22:26:27 -0700687 if (wasSuccessful) {
688 // Disconnect.
689 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700690
Sailesh Nepal4857f472014-04-07 22:26:27 -0700691 // Synchronize.
692 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
693 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700694
Sailesh Nepal4857f472014-04-07 22:26:27 -0700695 // Force the foreground call changed notification to be sent.
696 for (CallsManagerListener listener : mListeners) {
697 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
698 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700699
Sailesh Nepal4857f472014-04-07 22:26:27 -0700700 updateHandoffCallServiceDescriptor(originalCall, null);
701 } else {
702 updateHandoffCallServiceDescriptor(originalCall, null);
703 if (originalCall.getState() == CallState.DISCONNECTED ||
704 originalCall.getState() == CallState.ABORTED) {
705 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700706 }
707 }
708 }
709
Sailesh Nepal4857f472014-04-07 22:26:27 -0700710 private void updateHandoffCallServiceDescriptor(
711 Call originalCall,
712 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700713 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700714 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
715 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700716
717 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
718 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
719 for (CallsManagerListener listener : mListeners) {
720 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
721 newDescriptor);
722 }
723 }
724 }
725
726 private static boolean areDescriptorsEqual(
727 CallServiceDescriptor descriptor1,
728 CallServiceDescriptor descriptor2) {
729 if (descriptor1 == null) {
730 return descriptor2 == null;
731 }
732 return descriptor1.equals(descriptor2);
733 }
734
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700735 private static boolean areUriEqual(Uri handle1, Uri handle2) {
736 if (handle1 == null) {
737 return handle2 == null;
738 }
739 return handle1.equals(handle2);
740 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800741}