blob: 9a9e235cc7eb070b2ae42c41e8fd23dd3959141f [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Sailesh Nepalce704b92014-03-17 18:31:43 -070019import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070021import android.telecomm.CallAudioState;
Sailesh Nepal810735e2014-03-18 18:15:46 -070022import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080023import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080024import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.telecomm.GatewayInfo;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Santos Cordon681663d2014-01-30 04:32:15 -080028import com.google.common.base.Preconditions;
Sailesh Nepal810735e2014-03-18 18:15:46 -070029import com.google.common.collect.ImmutableCollection;
30import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Santos Cordonf3671a62014-05-29 21:51:53 -070032import java.util.HashSet;
33import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070034import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
39 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
40 * access from other packages specifically refraining from passing the CallsManager instance
41 * beyond the com.android.telecomm package boundary.
42 */
Santos Cordon766d04f2014-05-06 10:28:25 -070043public final class CallsManager implements Call.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080044
Santos Cordon74d420b2014-05-07 14:38:47 -070045 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
49 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070050 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
51 void onCallServiceChanged(
52 Call call,
53 CallServiceWrapper oldCallService,
54 CallServiceWrapper newCallService);
55 void onCallHandoffCallServiceDescriptorChanged(
56 Call call,
57 CallServiceDescriptor oldDescriptor,
58 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 void onIncomingCallAnswered(Call call);
60 void onIncomingCallRejected(Call call);
61 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070062 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070063 void onRequestingRingback(Call call, boolean ringback);
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 Cordonae193062014-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 Cordonae193062014-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 Cordonae193062014-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 Cordon6cb7ba92014-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 Cordon6cb7ba92014-05-23 14:09:32 -0700139
140 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700141 }
142
143 @Override
Ihab Awad0fea3f22014-06-03 18:45:05 -0700144 public void onFailedOutgoingCall(Call call, boolean isAborted, int errorCode, String errorMsg) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700145 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.
Ihab Awad0fea3f22014-06-03 18:45:05 -0700151 markCallAsDisconnected(call, errorCode, errorMsg);
Santos Cordon766d04f2014-05-06 10:28:25 -0700152 }
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 Awadcb387ac2014-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
Evan Charlton352105c2014-06-03 14:10:54 -0700174 @Override
175 public void onPostDialWait(Call call, String remaining) {
176 mInCallController.onPostDialWait(call, remaining);
177 }
178
Sailesh Nepal810735e2014-03-18 18:15:46 -0700179 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700180 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700181 }
182
183 Call getForegroundCall() {
184 return mForegroundCall;
185 }
186
Santos Cordonae193062014-05-21 21:21:49 -0700187 Ringer getRinger() {
188 return mRinger;
189 }
190
Santos Cordonf3671a62014-05-29 21:51:53 -0700191 InCallController getInCallController() {
192 return mInCallController;
193 }
194
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700195 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700196 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700197 if (call.isEmergencyCall()) {
198 return true;
199 }
200 }
201 return false;
202 }
203
204 CallAudioState getAudioState() {
205 return mCallAudioManager.getAudioState();
206 }
207
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800208 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800209 * Starts the incoming call sequence by having switchboard gather more information about the
210 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700211 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800212 *
Ben Giladc5b22692014-02-18 20:03:22 -0800213 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800214 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800215 */
Evan Charltona05805b2014-03-05 08:21:46 -0800216 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800217 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800218 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700219 // additional information from the call service, but for now we just need one to pass
220 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700221 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700222 // TODO(santoscordon): Move this to be a part of addCall()
223 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800224
Santos Cordon766d04f2014-05-06 10:28:25 -0700225 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800226 }
227
228 /**
Yorke Lee33501632014-03-17 19:24:12 -0700229 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800230 *
Yorke Lee33501632014-03-17 19:24:12 -0700231 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800232 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700233 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700234 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800235 */
Yorke Lee33501632014-03-17 19:24:12 -0700236 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700237 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
238
239 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700240 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700241 } else {
242 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
243 Log.pii(uriHandle), Log.pii(handle));
244 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700245
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700246 Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700247
248 // TODO(santoscordon): Move this to be a part of addCall()
249 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700250 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800251
Santos Cordon766d04f2014-05-06 10:28:25 -0700252 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800253 }
254
255 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800256 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
257 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
258 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800259 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700260 void answerCall(Call call) {
261 if (!mCalls.contains(call)) {
262 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800263 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700264 // If the foreground call is not the ringing call and it is currently isActive() or
265 // DIALING, put it on hold before answering the call.
266 if (mForegroundCall != null && mForegroundCall != call &&
267 (mForegroundCall.isActive() ||
268 mForegroundCall.getState() == CallState.DIALING)) {
269 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
270 mForegroundCall, call);
271 mForegroundCall.hold();
272 // TODO(santoscordon): Wait until we get confirmation of the active call being
273 // on-hold before answering the new call.
274 // TODO(santoscordon): Import logic from CallManager.acceptCall()
275 }
276
Santos Cordona56f2762014-03-24 15:55:53 -0700277 for (CallsManagerListener listener : mListeners) {
278 listener.onIncomingCallAnswered(call);
279 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800280
Santos Cordon61d0f702014-02-19 02:52:23 -0800281 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700282 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800283 call.answer();
284 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800285 }
286
287 /**
288 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
289 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
290 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800291 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700292 void rejectCall(Call call) {
293 if (!mCalls.contains(call)) {
294 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800295 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700296 for (CallsManagerListener listener : mListeners) {
297 listener.onIncomingCallRejected(call);
298 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800299 call.reject();
300 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800301 }
302
303 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700304 * Instructs Telecomm to play the specified DTMF tone within the specified call.
305 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700306 * @param digit The DTMF digit to play.
307 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700308 void playDtmfTone(Call call, char digit) {
309 if (!mCalls.contains(call)) {
310 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700311 } else {
312 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700313 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700314 }
315 }
316
317 /**
318 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700319 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700320 void stopDtmfTone(Call call) {
321 if (!mCalls.contains(call)) {
322 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700323 } else {
324 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700325 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700326 }
327 }
328
329 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700330 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700331 */
Evan Charlton352105c2014-06-03 14:10:54 -0700332 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700333 if (!mCalls.contains(call)) {
334 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700335 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700336 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700337 }
338 }
339
340 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800341 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
342 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
343 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800344 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700345 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700346 Log.v(this, "disconnectCall %s", call);
347
Sailesh Nepale59bb192014-04-01 18:33:59 -0700348 if (!mCalls.contains(call)) {
349 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800350 } else {
351 call.disconnect();
352 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800353 }
Santos Cordon681663d2014-01-30 04:32:15 -0800354
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700355 /**
356 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
357 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
358 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700359 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700360 void holdCall(Call call) {
361 if (!mCalls.contains(call)) {
362 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700363 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700364 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700365 call.hold();
366 }
367 }
368
369 /**
370 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
371 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
372 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700373 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700374 void unholdCall(Call call) {
375 if (!mCalls.contains(call)) {
376 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700377 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700378 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700379 call.unhold();
380 }
381 }
382
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700383 /** Called by the in-call UI to change the mute state. */
384 void mute(boolean shouldMute) {
385 mCallAudioManager.mute(shouldMute);
386 }
387
388 /**
389 * Called by the in-call UI to change the audio route, for example to change from earpiece to
390 * speaker phone.
391 */
392 void setAudioRoute(int route) {
393 mCallAudioManager.setAudioRoute(route);
394 }
395
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700396 void startHandoffForCall(Call originalCall) {
397 if (!mCalls.contains(originalCall)) {
398 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
399 return;
400 }
401
402 for (Call handoffCall : mPendingHandoffCalls) {
403 if (handoffCall.getOriginalCall() == originalCall) {
404 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
405 return;
406 }
407 }
408
409 // Create a new call to be placed in the background. If handoff is successful then the
410 // original call will live on but its state will be updated to the new call's state. In
411 // particular the original call's call service will be updated to the new call's call
412 // service.
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700413 Call tempCall =
414 new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700415 tempCall.setOriginalCall(originalCall);
416 tempCall.setExtras(originalCall.getExtras());
417 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
418 mPendingHandoffCalls.add(tempCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700419 tempCall.addListener(this);
420
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700421 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700422 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700423 }
424
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700425 /** Called when the audio state changes. */
426 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
427 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700428 for (CallsManagerListener listener : mListeners) {
429 listener.onAudioStateChanged(oldAudioState, newAudioState);
430 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700431 }
432
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 void markCallAsRinging(Call call) {
434 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800435 }
436
Sailesh Nepale59bb192014-04-01 18:33:59 -0700437 void markCallAsDialing(Call call) {
438 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800439 }
440
Sailesh Nepale59bb192014-04-01 18:33:59 -0700441 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700442 if (call.getConnectTimeMillis() == 0) {
443 call.setConnectTimeMillis(System.currentTimeMillis());
444 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700445 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700446
447 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700448 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700449 }
Santos Cordon681663d2014-01-30 04:32:15 -0800450 }
451
Sailesh Nepale59bb192014-04-01 18:33:59 -0700452 void markCallAsOnHold(Call call) {
453 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700454 }
455
Santos Cordon049b7b62014-01-30 05:34:26 -0800456 /**
457 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
458 * live call, then also disconnect from the in-call controller.
459 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700460 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
461 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800462 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700463 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
464 call.setDisconnectCause(disconnectCause, disconnectMessage);
465 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700466
467 // Only remove the call if handoff is not pending.
468 if (call.getHandoffCallServiceDescriptor() == null) {
469 removeCall(call);
470 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800471 }
472
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700473 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700474 if (!mCalls.contains(call)) {
475 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
476 return;
477 }
478
479 if (extras == null) {
480 call.setExtras(Bundle.EMPTY);
481 } else {
482 call.setExtras(extras);
483 }
484
485 Uri oldHandle = call.getHandoffHandle();
486 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
487 if (!areUriEqual(oldHandle, handle)) {
488 call.setHandoffHandle(handle);
489 for (CallsManagerListener listener : mListeners) {
490 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
491 }
492 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700493 }
494
Ben Gilada0d9f752014-02-26 11:49:03 -0800495 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700496 * Cleans up any calls currently associated with the specified call service when the
497 * call-service binder disconnects unexpectedly.
498 *
499 * @param callService The call service that disconnected.
500 */
501 void handleCallServiceDeath(CallServiceWrapper callService) {
502 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700503 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700504 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700505 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700506 }
507 }
508 }
509
Santos Cordondeb8c892014-05-30 01:38:03 -0700510 boolean hasAnyCalls() {
511 return !mCalls.isEmpty();
512 }
513
Santos Cordonc7e85d42014-05-22 02:51:48 -0700514 boolean hasActiveOrHoldingCall() {
515 for (Call call : mCalls) {
516 CallState state = call.getState();
517 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
518 return true;
519 }
520 }
521 return false;
522 }
523
524 boolean hasRingingCall() {
525 for (Call call : mCalls) {
526 if (call.getState() == CallState.RINGING) {
527 return true;
528 }
529 }
530 return false;
531 }
532
Santos Cordondeb8c892014-05-30 01:38:03 -0700533 boolean onMediaButton(int type) {
534 if (hasAnyCalls()) {
535 if (HeadsetMediaButton.SHORT_PRESS == type) {
536 Call ringingCall = getFirstCallWithState(CallState.RINGING);
537 if (ringingCall == null) {
538 mCallAudioManager.toggleMute();
539 return true;
540 } else {
541 ringingCall.answer();
542 return true;
543 }
544 } else if (HeadsetMediaButton.LONG_PRESS == type) {
545 Log.d(this, "handleHeadsetHook: longpress -> hangup");
546 Call callToHangup = getFirstCallWithState(
547 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
548 if (callToHangup != null) {
549 callToHangup.disconnect();
550 return true;
551 }
552 }
553 }
554 return false;
555 }
556
557 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700558 * Checks to see if the specified call is the only high-level call and if so, enable the
559 * "Add-call" button. We allow you to add a second call but not a third or beyond.
560 *
561 * @param call The call to test for add-call.
562 * @return Whether the add-call feature should be enabled for the call.
563 */
564 protected boolean isAddCallCapable(Call call) {
565 if (call.getParentCall() != null) {
566 // Never true for child calls.
567 return false;
568 }
569
570 // Loop through all the other calls and there exists a top level (has no parent) call
571 // that is not the specified call, return false.
572 for (Call otherCall : mCalls) {
573 if (call != otherCall && otherCall.getParentCall() == null) {
574 return false;
575 }
576 }
577 return true;
578 }
579
580 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700581 * Returns the first call that it finds with the given states. The states are treated as having
582 * priority order so that any call with the first state will be returned before any call with
583 * states listed later in the parameter list.
584 */
585 private Call getFirstCallWithState(CallState... states) {
586 for (CallState currentState : states) {
587 for (Call call : mCalls) {
588 if (currentState == call.getState()) {
589 return call;
590 }
591 }
592 }
593 return null;
594 }
595
Santos Cordon4b2c1192014-03-19 18:15:38 -0700596 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800597 * Adds the specified call to the main list of live calls.
598 *
599 * @param call The call to add.
600 */
601 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700602 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700603
604 // TODO(santoscordon): Update mForegroundCall prior to invoking
605 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700606 for (CallsManagerListener listener : mListeners) {
607 listener.onCallAdded(call);
608 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700609 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800610 }
611
Sailesh Nepal810735e2014-03-18 18:15:46 -0700612 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700613 // If a handoff is pending then the original call shouldn't be removed.
614 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700615 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700616
Santos Cordon766d04f2014-05-06 10:28:25 -0700617 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700618 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700619 call.clearCallServiceSelector();
620
621 boolean shouldNotify = false;
622 if (mCalls.contains(call)) {
623 mCalls.remove(call);
624 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700625 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700626 Log.v(this, "removeCall, marking handoff call as failed");
627 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700628 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800629
Sailesh Nepale59bb192014-04-01 18:33:59 -0700630 // Only broadcast changes for calls that are being tracked.
631 if (shouldNotify) {
632 for (CallsManagerListener listener : mListeners) {
633 listener.onCallRemoved(call);
634 }
635 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700636 }
637 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800638
Sailesh Nepal810735e2014-03-18 18:15:46 -0700639 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700640 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700641 *
642 * @param call The call.
643 * @param newState The new state of the call.
644 */
645 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700646 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700647 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700648 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700649 if (newState != oldState) {
650 // Unfortunately, in the telephony world the radio is king. So if the call notifies
651 // us that the call is in a particular state, we allow it even if it doesn't make
652 // sense (e.g., ACTIVE -> RINGING).
653 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
654 // into a well-defined state machine.
655 // TODO(santoscordon): Define expected state transitions here, and log when an
656 // unexpected transition occurs.
657 call.setState(newState);
658
659 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700660 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700661 for (CallsManagerListener listener : mListeners) {
662 listener.onCallStateChanged(call, oldState, newState);
663 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700664 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800665 }
666 }
667 }
668
669 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700670 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800671 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700672 private void updateForegroundCall() {
673 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700674 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700675 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
676 // of its state will be foreground by default and instead the call service should be
677 // notified when its calls enter and exit foreground state. Foreground will mean that
678 // the call should play audio and listen to microphone if it wants.
679
680 // Active calls have priority.
681 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700682 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800683 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700684 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700685
686 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700687 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700688 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800689 }
690 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800691
Sailesh Nepal810735e2014-03-18 18:15:46 -0700692 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700693 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700694 Call oldForegroundCall = mForegroundCall;
695 mForegroundCall = newForegroundCall;
696
Santos Cordona56f2762014-03-24 15:55:53 -0700697 for (CallsManagerListener listener : mListeners) {
698 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
699 }
Santos Cordon681663d2014-01-30 04:32:15 -0800700 }
701 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700702
Sailesh Nepal4857f472014-04-07 22:26:27 -0700703 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700704 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700705 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
706 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700707
Sailesh Nepal4857f472014-04-07 22:26:27 -0700708 // Remove the transient handoff call object (don't disconnect because the call could still
709 // be live).
710 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700711 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700712
Sailesh Nepal4857f472014-04-07 22:26:27 -0700713 if (wasSuccessful) {
Sailesh Nepaldcd64262014-05-23 17:03:28 -0700714 if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) {
715 originalCall.disconnect();
716 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700717
Sailesh Nepal4857f472014-04-07 22:26:27 -0700718 // Synchronize.
719 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
720 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700721
Sailesh Nepal4857f472014-04-07 22:26:27 -0700722 // Force the foreground call changed notification to be sent.
723 for (CallsManagerListener listener : mListeners) {
724 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
725 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700726
Sailesh Nepal4857f472014-04-07 22:26:27 -0700727 updateHandoffCallServiceDescriptor(originalCall, null);
728 } else {
729 updateHandoffCallServiceDescriptor(originalCall, null);
730 if (originalCall.getState() == CallState.DISCONNECTED ||
731 originalCall.getState() == CallState.ABORTED) {
732 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700733 }
734 }
735 }
736
Sailesh Nepal4857f472014-04-07 22:26:27 -0700737 private void updateHandoffCallServiceDescriptor(
738 Call originalCall,
739 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700740 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700741 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
742 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700743
744 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
745 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
746 for (CallsManagerListener listener : mListeners) {
747 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
748 newDescriptor);
749 }
750 }
751 }
752
753 private static boolean areDescriptorsEqual(
754 CallServiceDescriptor descriptor1,
755 CallServiceDescriptor descriptor2) {
756 if (descriptor1 == null) {
757 return descriptor2 == null;
758 }
759 return descriptor1.equals(descriptor2);
760 }
761
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700762 private static boolean areUriEqual(Uri handle1, Uri handle2) {
763 if (handle1 == null) {
764 return handle2 == null;
765 }
766 return handle1.equals(handle2);
767 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800768}