blob: 5e5f710e57c7d0d96c61c6a110b2ebce87920006 [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);
Ihab Awadff7493a2014-06-10 13:47:44 -070060 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070061 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);
Santos Cordona1610702014-06-04 20:22:56 -070064 void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable);
65 void onIsConferencedChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070066 void onCannedSmsResponsesLoaded(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 }
68
Santos Cordon8e8b8d22013-12-19 14:14:05 -080069 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080070
Santos Cordon8e8b8d22013-12-19 14:14:05 -080071 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070072 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
73 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080074 */
Santos Cordonf3671a62014-05-29 21:51:53 -070075 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080076
77 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070078 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
79 * and removed when hadnoff is complete.
80 */
Santos Cordonf3671a62014-05-29 21:51:53 -070081 private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>();
82
83
84 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
85 private final InCallController mInCallController = new InCallController();
86 private final CallAudioManager mCallAudioManager;
87 private final Ringer mRinger;
88 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070089 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070090
91 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070092 * The call the user is currently interacting with. This is the call that should have audio
93 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080094 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070095 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080096
Santos Cordon766d04f2014-05-06 10:28:25 -070097 /** Singleton accessor. */
98 static CallsManager getInstance() {
99 return INSTANCE;
100 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800101
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800103 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 */
105 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700106 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700107
Santos Cordondeb8c892014-05-30 01:38:03 -0700108 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
109 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700110 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700111 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700112 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonae193062014-05-21 21:21:49 -0700113
Santos Cordondeb8c892014-05-30 01:38:03 -0700114 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700115 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700116 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700117 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700118 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700119 mListeners.add(new RingbackPlayer(this, playerFactory));
120 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700121 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700122 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700123 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700124 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700125 mListeners.add(RespondViaSmsManager.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800126 }
127
Santos Cordon766d04f2014-05-06 10:28:25 -0700128 @Override
129 public void onSuccessfulOutgoingCall(Call call) {
130 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
131 if (mCalls.contains(call)) {
132 // The call's CallService has been updated.
133 for (CallsManagerListener listener : mListeners) {
134 listener.onCallServiceChanged(call, null, call.getCallService());
135 }
136 } else if (mPendingHandoffCalls.contains(call)) {
137 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
138 call.getCallService().getDescriptor());
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700139 } else {
140 Log.wtf(this, "unexpected successful call notification: %s", call);
141 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700142 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700143
144 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700145 }
146
147 @Override
Ihab Awad0fea3f22014-06-03 18:45:05 -0700148 public void onFailedOutgoingCall(Call call, boolean isAborted, int errorCode, String errorMsg) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700149 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
150 if (isAborted) {
151 setCallState(call, CallState.ABORTED);
152 removeCall(call);
153 } else {
154 // TODO: Replace disconnect cause with more specific disconnect causes.
Ihab Awad0fea3f22014-06-03 18:45:05 -0700155 markCallAsDisconnected(call, errorCode, errorMsg);
Santos Cordon766d04f2014-05-06 10:28:25 -0700156 }
157 }
158
159 @Override
160 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
161 Log.d(this, "onSuccessfulIncomingCall");
162 setCallState(call, callInfo.getState());
163 addCall(call);
164 }
165
166 @Override
167 public void onFailedIncomingCall(Call call) {
168 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800169 }
170
Ihab Awadcb387ac2014-05-28 16:49:38 -0700171 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700172 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
173 for (CallsManagerListener listener : mListeners) {
174 listener.onIsConferenceCapableChanged(call, isConferenceCapable);
175 }
176 }
177
178 @Override
Ihab Awadcb387ac2014-05-28 16:49:38 -0700179 public void onRequestingRingback(Call call, boolean ringback) {
180 for (CallsManagerListener listener : mListeners) {
181 listener.onRequestingRingback(call, ringback);
182 }
183 }
184
Evan Charlton352105c2014-06-03 14:10:54 -0700185 @Override
186 public void onPostDialWait(Call call, String remaining) {
187 mInCallController.onPostDialWait(call, remaining);
188 }
189
Santos Cordona1610702014-06-04 20:22:56 -0700190 @Override
191 public void onExpiredConferenceCall(Call call) {
192 call.removeListener(this);
193 }
194
195 @Override
196 public void onConfirmedConferenceCall(Call call) {
197 addCall(call);
198 Log.v(this, "confirming Conf call %s", call);
199 for (CallsManagerListener listener : mListeners) {
200 listener.onIsConferencedChanged(call);
201 }
202 }
203
204 @Override
205 public void onParentChanged(Call call) {
206 for (CallsManagerListener listener : mListeners) {
207 listener.onIsConferencedChanged(call);
208 }
209 }
210
211 @Override
212 public void onChildrenChanged(Call call) {
213 for (CallsManagerListener listener : mListeners) {
214 listener.onIsConferencedChanged(call);
215 }
216 }
217
Ihab Awadff7493a2014-06-10 13:47:44 -0700218 @Override
219 public void onCannedSmsResponsesLoaded(Call call) {
220 for (CallsManagerListener listener : mListeners) {
221 listener.onCannedSmsResponsesLoaded(call);
222 }
223 }
224
Sailesh Nepal810735e2014-03-18 18:15:46 -0700225 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700226 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700227 }
228
229 Call getForegroundCall() {
230 return mForegroundCall;
231 }
232
Santos Cordonae193062014-05-21 21:21:49 -0700233 Ringer getRinger() {
234 return mRinger;
235 }
236
Santos Cordonf3671a62014-05-29 21:51:53 -0700237 InCallController getInCallController() {
238 return mInCallController;
239 }
240
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700241 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700242 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700243 if (call.isEmergencyCall()) {
244 return true;
245 }
246 }
247 return false;
248 }
249
250 CallAudioState getAudioState() {
251 return mCallAudioManager.getAudioState();
252 }
253
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800254 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800255 * Starts the incoming call sequence by having switchboard gather more information about the
256 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700257 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800258 *
Ben Giladc5b22692014-02-18 20:03:22 -0800259 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800260 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800261 */
Evan Charltona05805b2014-03-05 08:21:46 -0800262 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800263 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800264 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700265 // additional information from the call service, but for now we just need one to pass
266 // around.
Santos Cordona1610702014-06-04 20:22:56 -0700267 Call call = new Call(true /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700268 // TODO(santoscordon): Move this to be a part of addCall()
269 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800270
Santos Cordon766d04f2014-05-06 10:28:25 -0700271 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800272 }
273
274 /**
Yorke Lee33501632014-03-17 19:24:12 -0700275 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800276 *
Yorke Lee33501632014-03-17 19:24:12 -0700277 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800278 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700279 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700280 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800281 */
Yorke Lee33501632014-03-17 19:24:12 -0700282 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700283 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
284
285 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700286 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700287 } else {
288 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
289 Log.pii(uriHandle), Log.pii(handle));
290 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700291
Santos Cordona1610702014-06-04 20:22:56 -0700292 Call call = new Call(
293 uriHandle, gatewayInfo, false /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700294
295 // TODO(santoscordon): Move this to be a part of addCall()
296 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700297 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800298
Santos Cordon766d04f2014-05-06 10:28:25 -0700299 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800300 }
301
302 /**
Santos Cordona1610702014-06-04 20:22:56 -0700303 * Attempts to start a conference call for the specified call.
304 *
305 * @param call The call to conference with.
306 */
307 void conference(Call call) {
308 Call conferenceCall = new Call(false, true);
309 conferenceCall.addListener(this);
310 call.conferenceInto(conferenceCall);
311 }
312
313 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800314 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
315 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
316 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800317 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700318 void answerCall(Call call) {
319 if (!mCalls.contains(call)) {
320 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800321 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700322 // If the foreground call is not the ringing call and it is currently isActive() or
323 // DIALING, put it on hold before answering the call.
324 if (mForegroundCall != null && mForegroundCall != call &&
325 (mForegroundCall.isActive() ||
326 mForegroundCall.getState() == CallState.DIALING)) {
327 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
328 mForegroundCall, call);
329 mForegroundCall.hold();
330 // TODO(santoscordon): Wait until we get confirmation of the active call being
331 // on-hold before answering the new call.
332 // TODO(santoscordon): Import logic from CallManager.acceptCall()
333 }
334
Santos Cordona56f2762014-03-24 15:55:53 -0700335 for (CallsManagerListener listener : mListeners) {
336 listener.onIncomingCallAnswered(call);
337 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800338
Santos Cordon61d0f702014-02-19 02:52:23 -0800339 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700340 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800341 call.answer();
342 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800343 }
344
345 /**
346 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
347 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
348 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800349 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700350 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700351 if (!mCalls.contains(call)) {
352 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800353 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700354 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700355 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700356 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700357 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800358 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800359 }
360
361 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700362 * Instructs Telecomm to play the specified DTMF tone within the specified call.
363 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700364 * @param digit The DTMF digit to play.
365 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700366 void playDtmfTone(Call call, char digit) {
367 if (!mCalls.contains(call)) {
368 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700369 } else {
370 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700371 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700372 }
373 }
374
375 /**
376 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700377 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700378 void stopDtmfTone(Call call) {
379 if (!mCalls.contains(call)) {
380 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700381 } else {
382 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700383 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700384 }
385 }
386
387 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700388 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700389 */
Evan Charlton352105c2014-06-03 14:10:54 -0700390 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700391 if (!mCalls.contains(call)) {
392 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700393 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700394 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700395 }
396 }
397
398 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800399 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
400 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
401 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800402 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700403 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700404 Log.v(this, "disconnectCall %s", call);
405
Sailesh Nepale59bb192014-04-01 18:33:59 -0700406 if (!mCalls.contains(call)) {
407 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800408 } else {
409 call.disconnect();
410 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800411 }
Santos Cordon681663d2014-01-30 04:32:15 -0800412
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700413 /**
414 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
415 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
416 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700417 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 void holdCall(Call call) {
419 if (!mCalls.contains(call)) {
420 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700421 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700422 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700423 call.hold();
424 }
425 }
426
427 /**
428 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
429 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
430 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700431 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700432 void unholdCall(Call call) {
433 if (!mCalls.contains(call)) {
434 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700435 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700436 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700437 call.unhold();
438 }
439 }
440
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700441 /** Called by the in-call UI to change the mute state. */
442 void mute(boolean shouldMute) {
443 mCallAudioManager.mute(shouldMute);
444 }
445
446 /**
447 * Called by the in-call UI to change the audio route, for example to change from earpiece to
448 * speaker phone.
449 */
450 void setAudioRoute(int route) {
451 mCallAudioManager.setAudioRoute(route);
452 }
453
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700454 void startHandoffForCall(Call originalCall) {
455 if (!mCalls.contains(originalCall)) {
456 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
457 return;
458 }
459
460 for (Call handoffCall : mPendingHandoffCalls) {
461 if (handoffCall.getOriginalCall() == originalCall) {
462 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
463 return;
464 }
465 }
466
467 // Create a new call to be placed in the background. If handoff is successful then the
468 // original call will live on but its state will be updated to the new call's state. In
469 // particular the original call's call service will be updated to the new call's call
470 // service.
Santos Cordona1610702014-06-04 20:22:56 -0700471 Call tempCall = new Call(
472 originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false, false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700473 tempCall.setOriginalCall(originalCall);
474 tempCall.setExtras(originalCall.getExtras());
475 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
476 mPendingHandoffCalls.add(tempCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700477 tempCall.addListener(this);
478
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700479 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700480 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700481 }
482
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700483 /** Called when the audio state changes. */
484 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
485 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700486 for (CallsManagerListener listener : mListeners) {
487 listener.onAudioStateChanged(oldAudioState, newAudioState);
488 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700489 }
490
Sailesh Nepale59bb192014-04-01 18:33:59 -0700491 void markCallAsRinging(Call call) {
492 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800493 }
494
Sailesh Nepale59bb192014-04-01 18:33:59 -0700495 void markCallAsDialing(Call call) {
496 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800497 }
498
Sailesh Nepale59bb192014-04-01 18:33:59 -0700499 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700500 if (call.getConnectTimeMillis() == 0) {
501 call.setConnectTimeMillis(System.currentTimeMillis());
502 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700503 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700504
505 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700506 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700507 }
Santos Cordon681663d2014-01-30 04:32:15 -0800508 }
509
Sailesh Nepale59bb192014-04-01 18:33:59 -0700510 void markCallAsOnHold(Call call) {
511 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700512 }
513
Santos Cordon049b7b62014-01-30 05:34:26 -0800514 /**
515 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
516 * live call, then also disconnect from the in-call controller.
517 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700518 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
519 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800520 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700521 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
522 call.setDisconnectCause(disconnectCause, disconnectMessage);
523 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700524
525 // Only remove the call if handoff is not pending.
526 if (call.getHandoffCallServiceDescriptor() == null) {
527 removeCall(call);
528 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800529 }
530
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700531 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700532 if (!mCalls.contains(call)) {
533 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
534 return;
535 }
536
537 if (extras == null) {
538 call.setExtras(Bundle.EMPTY);
539 } else {
540 call.setExtras(extras);
541 }
542
543 Uri oldHandle = call.getHandoffHandle();
544 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
545 if (!areUriEqual(oldHandle, handle)) {
546 call.setHandoffHandle(handle);
547 for (CallsManagerListener listener : mListeners) {
548 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
549 }
550 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700551 }
552
Ben Gilada0d9f752014-02-26 11:49:03 -0800553 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700554 * Cleans up any calls currently associated with the specified call service when the
555 * call-service binder disconnects unexpectedly.
556 *
557 * @param callService The call service that disconnected.
558 */
559 void handleCallServiceDeath(CallServiceWrapper callService) {
560 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700561 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700562 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700563 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700564 }
565 }
566 }
567
Santos Cordondeb8c892014-05-30 01:38:03 -0700568 boolean hasAnyCalls() {
569 return !mCalls.isEmpty();
570 }
571
Santos Cordonc7e85d42014-05-22 02:51:48 -0700572 boolean hasActiveOrHoldingCall() {
573 for (Call call : mCalls) {
574 CallState state = call.getState();
575 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
576 return true;
577 }
578 }
579 return false;
580 }
581
582 boolean hasRingingCall() {
583 for (Call call : mCalls) {
584 if (call.getState() == CallState.RINGING) {
585 return true;
586 }
587 }
588 return false;
589 }
590
Santos Cordondeb8c892014-05-30 01:38:03 -0700591 boolean onMediaButton(int type) {
592 if (hasAnyCalls()) {
593 if (HeadsetMediaButton.SHORT_PRESS == type) {
594 Call ringingCall = getFirstCallWithState(CallState.RINGING);
595 if (ringingCall == null) {
596 mCallAudioManager.toggleMute();
597 return true;
598 } else {
599 ringingCall.answer();
600 return true;
601 }
602 } else if (HeadsetMediaButton.LONG_PRESS == type) {
603 Log.d(this, "handleHeadsetHook: longpress -> hangup");
604 Call callToHangup = getFirstCallWithState(
605 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
606 if (callToHangup != null) {
607 callToHangup.disconnect();
608 return true;
609 }
610 }
611 }
612 return false;
613 }
614
615 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700616 * Checks to see if the specified call is the only high-level call and if so, enable the
617 * "Add-call" button. We allow you to add a second call but not a third or beyond.
618 *
619 * @param call The call to test for add-call.
620 * @return Whether the add-call feature should be enabled for the call.
621 */
622 protected boolean isAddCallCapable(Call call) {
623 if (call.getParentCall() != null) {
624 // Never true for child calls.
625 return false;
626 }
627
628 // Loop through all the other calls and there exists a top level (has no parent) call
629 // that is not the specified call, return false.
630 for (Call otherCall : mCalls) {
631 if (call != otherCall && otherCall.getParentCall() == null) {
632 return false;
633 }
634 }
635 return true;
636 }
637
638 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700639 * Returns the first call that it finds with the given states. The states are treated as having
640 * priority order so that any call with the first state will be returned before any call with
641 * states listed later in the parameter list.
642 */
643 private Call getFirstCallWithState(CallState... states) {
644 for (CallState currentState : states) {
645 for (Call call : mCalls) {
646 if (currentState == call.getState()) {
647 return call;
648 }
649 }
650 }
651 return null;
652 }
653
Santos Cordon4b2c1192014-03-19 18:15:38 -0700654 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800655 * Adds the specified call to the main list of live calls.
656 *
657 * @param call The call to add.
658 */
659 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700660 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700661
662 // TODO(santoscordon): Update mForegroundCall prior to invoking
663 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700664 for (CallsManagerListener listener : mListeners) {
665 listener.onCallAdded(call);
666 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700667 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800668 }
669
Sailesh Nepal810735e2014-03-18 18:15:46 -0700670 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700671 // If a handoff is pending then the original call shouldn't be removed.
672 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700673 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700674
Santos Cordon766d04f2014-05-06 10:28:25 -0700675 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700676 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700677 call.clearCallServiceSelector();
678
679 boolean shouldNotify = false;
680 if (mCalls.contains(call)) {
681 mCalls.remove(call);
682 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700683 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700684 Log.v(this, "removeCall, marking handoff call as failed");
685 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700686 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800687
Sailesh Nepale59bb192014-04-01 18:33:59 -0700688 // Only broadcast changes for calls that are being tracked.
689 if (shouldNotify) {
690 for (CallsManagerListener listener : mListeners) {
691 listener.onCallRemoved(call);
692 }
693 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700694 }
695 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800696
Sailesh Nepal810735e2014-03-18 18:15:46 -0700697 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700698 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700699 *
700 * @param call The call.
701 * @param newState The new state of the call.
702 */
703 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700704 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700705 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700706 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700707 if (newState != oldState) {
708 // Unfortunately, in the telephony world the radio is king. So if the call notifies
709 // us that the call is in a particular state, we allow it even if it doesn't make
710 // sense (e.g., ACTIVE -> RINGING).
711 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
712 // into a well-defined state machine.
713 // TODO(santoscordon): Define expected state transitions here, and log when an
714 // unexpected transition occurs.
715 call.setState(newState);
716
717 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700718 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700719 for (CallsManagerListener listener : mListeners) {
720 listener.onCallStateChanged(call, oldState, newState);
721 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700722 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800723 }
724 }
725 }
726
727 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700728 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800729 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700730 private void updateForegroundCall() {
731 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700732 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700733 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
734 // of its state will be foreground by default and instead the call service should be
735 // notified when its calls enter and exit foreground state. Foreground will mean that
736 // the call should play audio and listen to microphone if it wants.
737
738 // Active calls have priority.
739 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700740 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800741 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700742 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700743
744 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700745 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700746 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800747 }
748 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800749
Sailesh Nepal810735e2014-03-18 18:15:46 -0700750 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700751 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700752 Call oldForegroundCall = mForegroundCall;
753 mForegroundCall = newForegroundCall;
754
Santos Cordona56f2762014-03-24 15:55:53 -0700755 for (CallsManagerListener listener : mListeners) {
756 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
757 }
Santos Cordon681663d2014-01-30 04:32:15 -0800758 }
759 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700760
Sailesh Nepal4857f472014-04-07 22:26:27 -0700761 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700762 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700763 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
764 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700765
Sailesh Nepal4857f472014-04-07 22:26:27 -0700766 // Remove the transient handoff call object (don't disconnect because the call could still
767 // be live).
768 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700769 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700770
Sailesh Nepal4857f472014-04-07 22:26:27 -0700771 if (wasSuccessful) {
Sailesh Nepaldcd64262014-05-23 17:03:28 -0700772 if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) {
773 originalCall.disconnect();
774 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700775
Sailesh Nepal4857f472014-04-07 22:26:27 -0700776 // Synchronize.
777 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
778 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700779
Sailesh Nepal4857f472014-04-07 22:26:27 -0700780 // Force the foreground call changed notification to be sent.
781 for (CallsManagerListener listener : mListeners) {
782 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
783 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700784
Sailesh Nepal4857f472014-04-07 22:26:27 -0700785 updateHandoffCallServiceDescriptor(originalCall, null);
786 } else {
787 updateHandoffCallServiceDescriptor(originalCall, null);
788 if (originalCall.getState() == CallState.DISCONNECTED ||
789 originalCall.getState() == CallState.ABORTED) {
790 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700791 }
792 }
793 }
794
Sailesh Nepal4857f472014-04-07 22:26:27 -0700795 private void updateHandoffCallServiceDescriptor(
796 Call originalCall,
797 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700798 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700799 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
800 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700801
802 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
803 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
804 for (CallsManagerListener listener : mListeners) {
805 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
806 newDescriptor);
807 }
808 }
809 }
810
811 private static boolean areDescriptorsEqual(
812 CallServiceDescriptor descriptor1,
813 CallServiceDescriptor descriptor2) {
814 if (descriptor1 == null) {
815 return descriptor2 == null;
816 }
817 return descriptor1.equals(descriptor2);
818 }
819
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700820 private static boolean areUriEqual(Uri handle1, Uri handle2) {
821 if (handle1 == null) {
822 return handle2 == null;
823 }
824 return handle1.equals(handle2);
825 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800826}