blob: 5a53525d61f0379289133a4cd1707976d266b937 [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;
Santos Cordon681663d2014-01-30 04:32:15 -080022import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070023import android.telecomm.GatewayInfo;
Ihab Awad98a55602014-06-30 21:27:28 -070024import android.telecomm.PhoneAccount;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070025import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080026
Santos Cordon681663d2014-01-30 04:32:15 -080027import com.google.common.base.Preconditions;
Sailesh Nepal810735e2014-03-18 18:15:46 -070028import com.google.common.collect.ImmutableCollection;
29import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080030
Santos Cordonf3671a62014-05-29 21:51:53 -070031import java.util.HashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070032import java.util.Set;
Santos Cordon626697a2014-07-10 22:36:37 -070033import java.util.concurrent.CopyOnWriteArraySet;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Ben Giladdd8c6082013-12-30 14:44:08 -080035/**
36 * Singleton.
37 *
38 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
39 * access from other packages specifically refraining from passing the CallsManager instance
40 * beyond the com.android.telecomm package boundary.
41 */
Santos Cordon64c7e962014-07-02 15:15:27 -070042public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080043
Santos Cordon74d420b2014-05-07 14:38:47 -070044 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070045 interface CallsManagerListener {
46 void onCallAdded(Call call);
47 void onCallRemoved(Call call);
48 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070049 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070050 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070051 ConnectionServiceWrapper oldService,
52 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070053 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070054 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070055 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070056 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070057 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070058 void onIsConferencedChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070059 void onAudioModeIsVoipChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070060 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070061 }
62
Santos Cordon8e8b8d22013-12-19 14:14:05 -080063 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080064
Santos Cordon8e8b8d22013-12-19 14:14:05 -080065 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070066 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
67 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080068 */
Santos Cordon626697a2014-07-10 22:36:37 -070069 private final Set<Call> mCalls = new CopyOnWriteArraySet<Call>();
Santos Cordon681663d2014-01-30 04:32:15 -080070
Sailesh Nepal664837f2014-07-14 16:31:51 -070071 private final ConnectionServiceRepository mConnectionServiceRepository =
72 new ConnectionServiceRepository();
Santos Cordonf3671a62014-05-29 21:51:53 -070073 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
74 private final InCallController mInCallController = new InCallController();
75 private final CallAudioManager mCallAudioManager;
76 private final Ringer mRinger;
77 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070078 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070079 private final WiredHeadsetManager mWiredHeadsetManager;
80 private final TtyManager mTtyManager;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070081
82 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070083 * The call the user is currently interacting with. This is the call that should have audio
84 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080085 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070086 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080087
Santos Cordon766d04f2014-05-06 10:28:25 -070088 /** Singleton accessor. */
89 static CallsManager getInstance() {
90 return INSTANCE;
91 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080092
Santos Cordon8e8b8d22013-12-19 14:14:05 -080093 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080094 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 */
96 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -070097 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -070098
Santos Cordondeb8c892014-05-30 01:38:03 -070099 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700100 mWiredHeadsetManager = new WiredHeadsetManager(app);
101 mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700102 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700103 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700104 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700105 mTtyManager = new TtyManager(app, mWiredHeadsetManager);
Santos Cordonae193062014-05-21 21:21:49 -0700106
Santos Cordondeb8c892014-05-30 01:38:03 -0700107 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700108 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700109 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700110 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700111 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700112 mListeners.add(new RingbackPlayer(this, playerFactory));
113 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700114 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700115 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700116 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700117 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700118 mListeners.add(RespondViaSmsManager.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800119 }
120
Santos Cordon766d04f2014-05-06 10:28:25 -0700121 @Override
122 public void onSuccessfulOutgoingCall(Call call) {
123 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
124 if (mCalls.contains(call)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700125 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700126 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700127 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700128 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700129 } else {
130 Log.wtf(this, "unexpected successful call notification: %s", call);
131 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700132 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700133
134 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700135 }
136
137 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700138 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
139 Log.v(this, "onFailedOutgoingCall, call: %s", call);
140 // TODO: Replace disconnect cause with more specific disconnect causes.
141 markCallAsDisconnected(call, errorCode, errorMsg);
142 }
143
144 @Override
145 public void onCancelledOutgoingCall(Call call) {
146 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
147 setCallState(call, CallState.ABORTED);
148 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700149 }
150
151 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700152 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700153 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700154 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700155 addCall(call);
156 }
157
158 @Override
159 public void onFailedIncomingCall(Call call) {
160 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800161 }
162
Ihab Awadcb387ac2014-05-28 16:49:38 -0700163 @Override
164 public void onRequestingRingback(Call call, boolean ringback) {
165 for (CallsManagerListener listener : mListeners) {
166 listener.onRequestingRingback(call, ringback);
167 }
168 }
169
Evan Charlton352105c2014-06-03 14:10:54 -0700170 @Override
171 public void onPostDialWait(Call call, String remaining) {
172 mInCallController.onPostDialWait(call, remaining);
173 }
174
Santos Cordona1610702014-06-04 20:22:56 -0700175 @Override
176 public void onExpiredConferenceCall(Call call) {
177 call.removeListener(this);
178 }
179
180 @Override
181 public void onConfirmedConferenceCall(Call call) {
182 addCall(call);
183 Log.v(this, "confirming Conf call %s", call);
184 for (CallsManagerListener listener : mListeners) {
185 listener.onIsConferencedChanged(call);
186 }
187 }
188
189 @Override
190 public void onParentChanged(Call call) {
191 for (CallsManagerListener listener : mListeners) {
192 listener.onIsConferencedChanged(call);
193 }
194 }
195
196 @Override
197 public void onChildrenChanged(Call call) {
198 for (CallsManagerListener listener : mListeners) {
199 listener.onIsConferencedChanged(call);
200 }
201 }
202
Ihab Awadff7493a2014-06-10 13:47:44 -0700203 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700204 public void onAudioModeIsVoipChanged(Call call) {
205 for (CallsManagerListener listener : mListeners) {
206 listener.onAudioModeIsVoipChanged(call);
207 }
208 }
209
Andrew Lee4a796602014-07-11 17:23:03 -0700210 @Override
211 public void onVideoStateChanged(Call call) {
212 for (CallsManagerListener listener : mListeners) {
213 listener.onVideoStateChanged(call);
214 }
215 }
216
Sailesh Nepal810735e2014-03-18 18:15:46 -0700217 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700218 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700219 }
220
221 Call getForegroundCall() {
222 return mForegroundCall;
223 }
224
Santos Cordonae193062014-05-21 21:21:49 -0700225 Ringer getRinger() {
226 return mRinger;
227 }
228
Santos Cordonf3671a62014-05-29 21:51:53 -0700229 InCallController getInCallController() {
230 return mInCallController;
231 }
232
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700233 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700234 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700235 if (call.isEmergencyCall()) {
236 return true;
237 }
238 }
239 return false;
240 }
241
242 CallAudioState getAudioState() {
243 return mCallAudioManager.getAudioState();
244 }
245
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700246 boolean isTtySupported() {
247 return mTtyManager.isTtySupported();
248 }
249
250 int getCurrentTtyMode() {
251 return mTtyManager.getCurrentTtyMode();
252 }
253
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800254 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700255 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800256 *
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700257 * @param phoneAccount The phone account which contains the component name of the connection
258 * serivce to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800259 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800260 */
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700261 void processIncomingCallIntent(PhoneAccount phoneAccount, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800262 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700263 // Create a call with no handle. The handle is eventually set when the call is attached
264 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700265 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700266 mConnectionServiceRepository,
267 null /* handle */,
268 null /* gatewayInfo */,
269 phoneAccount,
270 true /* isIncoming */,
271 false /* isConference */);
272
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700273 call.setExtras(extras);
Santos Cordon766d04f2014-05-06 10:28:25 -0700274 // TODO(santoscordon): Move this to be a part of addCall()
275 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700276 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800277 }
278
279 /**
Yorke Lee33501632014-03-17 19:24:12 -0700280 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800281 *
Yorke Lee33501632014-03-17 19:24:12 -0700282 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700283 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700284 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700285 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700286 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800287 */
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700288 void placeOutgoingCall(Uri handle, GatewayInfo gatewayInfo, PhoneAccount account,
289 boolean speakerphoneOn, int videoState) {
Tyler Gunnc4abd912014-07-08 14:22:10 -0700290
Yorke Lee33501632014-03-17 19:24:12 -0700291 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
292
293 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700294 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700295 } else {
296 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
297 Log.pii(uriHandle), Log.pii(handle));
298 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700299
Santos Cordona1610702014-06-04 20:22:56 -0700300 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700301 mConnectionServiceRepository,
302 uriHandle,
303 gatewayInfo,
304 account,
305 false /* isIncoming */,
306 false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700307 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700308 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700309
310 // TODO(santoscordon): Move this to be a part of addCall()
311 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700312 addCall(call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700313
Ihab Awad69eb0f52014-07-18 11:20:37 -0700314 if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), call.getHandle())) {
315 // Emergency -- CreateConnectionProcessor will choose accounts automatically
316 call.setPhoneAccount(null);
317 call.startCreateConnection();
318 } else if (account == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700319 PhoneAccount defaultAccount = TelecommApp.getInstance().getPhoneAccountRegistrar()
320 .getDefaultOutgoingPhoneAccount();
321 if (defaultAccount != null) {
322 call.setPhoneAccount(defaultAccount);
323 call.startCreateConnection();
324 } else {
325 call.setState(CallState.PRE_DIAL_WAIT);
326 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700327 }
Yorke Leef98fb572014-03-05 10:56:55 -0800328 }
329
330 /**
Santos Cordona1610702014-06-04 20:22:56 -0700331 * Attempts to start a conference call for the specified call.
332 *
333 * @param call The call to conference with.
334 */
335 void conference(Call call) {
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700336 Call conferenceCall = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700337 mConnectionServiceRepository,
338 null /* handle */,
339 null /* gatewayInfo */,
340 null /* phoneAccount */,
341 false /* isIncoming */,
342 true /* isConference */);
Santos Cordona1610702014-06-04 20:22:56 -0700343 conferenceCall.addListener(this);
344 call.conferenceInto(conferenceCall);
345 }
346
347 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800348 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
349 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
350 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700351 *
352 * @param call The call to answer.
353 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800354 */
Andrew Lee38931d02014-07-16 10:17:36 -0700355 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700356 if (!mCalls.contains(call)) {
357 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800358 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700359 // If the foreground call is not the ringing call and it is currently isActive() or
360 // DIALING, put it on hold before answering the call.
361 if (mForegroundCall != null && mForegroundCall != call &&
362 (mForegroundCall.isActive() ||
363 mForegroundCall.getState() == CallState.DIALING)) {
364 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
365 mForegroundCall, call);
366 mForegroundCall.hold();
367 // TODO(santoscordon): Wait until we get confirmation of the active call being
368 // on-hold before answering the new call.
369 // TODO(santoscordon): Import logic from CallManager.acceptCall()
370 }
371
Santos Cordona56f2762014-03-24 15:55:53 -0700372 for (CallsManagerListener listener : mListeners) {
373 listener.onIncomingCallAnswered(call);
374 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800375
Santos Cordon61d0f702014-02-19 02:52:23 -0800376 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700377 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700378 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800379 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800380 }
381
382 /**
383 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
384 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
385 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800386 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700387 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700388 if (!mCalls.contains(call)) {
389 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800390 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700391 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700392 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700393 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700394 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800395 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800396 }
397
398 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700399 * Instructs Telecomm to play the specified DTMF tone within the specified call.
400 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700401 * @param digit The DTMF digit to play.
402 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700403 void playDtmfTone(Call call, char digit) {
404 if (!mCalls.contains(call)) {
405 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700406 } else {
407 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700408 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700409 }
410 }
411
412 /**
413 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700414 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700415 void stopDtmfTone(Call call) {
416 if (!mCalls.contains(call)) {
417 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700418 } else {
419 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700420 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700421 }
422 }
423
424 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700425 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700426 */
Evan Charlton352105c2014-06-03 14:10:54 -0700427 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700428 if (!mCalls.contains(call)) {
429 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700430 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700431 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700432 }
433 }
434
435 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800436 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
437 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
438 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800439 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700440 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700441 Log.v(this, "disconnectCall %s", call);
442
Sailesh Nepale59bb192014-04-01 18:33:59 -0700443 if (!mCalls.contains(call)) {
444 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800445 } else {
446 call.disconnect();
447 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800448 }
Santos Cordon681663d2014-01-30 04:32:15 -0800449
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700450 /**
451 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
452 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
453 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700454 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700455 void holdCall(Call call) {
456 if (!mCalls.contains(call)) {
457 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700458 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700459 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700460 call.hold();
461 }
462 }
463
464 /**
465 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
466 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
467 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700468 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700469 void unholdCall(Call call) {
470 if (!mCalls.contains(call)) {
471 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700472 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700473 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700474 call.unhold();
475 }
476 }
477
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700478 /** Called by the in-call UI to change the mute state. */
479 void mute(boolean shouldMute) {
480 mCallAudioManager.mute(shouldMute);
481 }
482
483 /**
484 * Called by the in-call UI to change the audio route, for example to change from earpiece to
485 * speaker phone.
486 */
487 void setAudioRoute(int route) {
488 mCallAudioManager.setAudioRoute(route);
489 }
490
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700491 void phoneAccountClicked(Call call) {
492 if (!mCalls.contains(call)) {
493 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
494 } else {
495 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700496 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700497 }
498
Nancy Chen53ceedc2014-07-08 18:56:51 -0700499 void phoneAccountSelected(Call call, PhoneAccount account) {
500 if (!mCalls.contains(call)) {
501 Log.i(this, "Attemped to add account to unknown call %s", call);
502 } else {
503 call.setPhoneAccount(account);
504 call.startCreateConnection();
505 }
506 }
507
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700508 /** Called when the audio state changes. */
509 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
510 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700511 for (CallsManagerListener listener : mListeners) {
512 listener.onAudioStateChanged(oldAudioState, newAudioState);
513 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700514 }
515
Sailesh Nepale59bb192014-04-01 18:33:59 -0700516 void markCallAsRinging(Call call) {
517 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800518 }
519
Sailesh Nepale59bb192014-04-01 18:33:59 -0700520 void markCallAsDialing(Call call) {
521 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800522 }
523
Sailesh Nepale59bb192014-04-01 18:33:59 -0700524 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700525 if (call.getConnectTimeMillis() == 0) {
526 call.setConnectTimeMillis(System.currentTimeMillis());
527 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700528 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700529
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700530 if (call.getStartWithSpeakerphoneOn()) {
531 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
532 }
Santos Cordon681663d2014-01-30 04:32:15 -0800533 }
534
Sailesh Nepale59bb192014-04-01 18:33:59 -0700535 void markCallAsOnHold(Call call) {
536 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700537 }
538
Santos Cordon049b7b62014-01-30 05:34:26 -0800539 /**
540 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
541 * live call, then also disconnect from the in-call controller.
542 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700543 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700544 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800545 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700546 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
547 call.setDisconnectCause(disconnectCause, disconnectMessage);
548 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700549 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700550 }
551
Ben Gilada0d9f752014-02-26 11:49:03 -0800552 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700553 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700554 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700555 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700556 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700557 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700558 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
559 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700560 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700561 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700562 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700563 }
564 }
565 }
566
Santos Cordondeb8c892014-05-30 01:38:03 -0700567 boolean hasAnyCalls() {
568 return !mCalls.isEmpty();
569 }
570
Santos Cordonc7e85d42014-05-22 02:51:48 -0700571 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700572 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700573 }
574
575 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700576 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700577 }
578
Santos Cordondeb8c892014-05-30 01:38:03 -0700579 boolean onMediaButton(int type) {
580 if (hasAnyCalls()) {
581 if (HeadsetMediaButton.SHORT_PRESS == type) {
582 Call ringingCall = getFirstCallWithState(CallState.RINGING);
583 if (ringingCall == null) {
584 mCallAudioManager.toggleMute();
585 return true;
586 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700587 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700588 return true;
589 }
590 } else if (HeadsetMediaButton.LONG_PRESS == type) {
591 Log.d(this, "handleHeadsetHook: longpress -> hangup");
592 Call callToHangup = getFirstCallWithState(
593 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
594 if (callToHangup != null) {
595 callToHangup.disconnect();
596 return true;
597 }
598 }
599 }
600 return false;
601 }
602
603 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700604 * Checks to see if the specified call is the only high-level call and if so, enable the
605 * "Add-call" button. We allow you to add a second call but not a third or beyond.
606 *
607 * @param call The call to test for add-call.
608 * @return Whether the add-call feature should be enabled for the call.
609 */
610 protected boolean isAddCallCapable(Call call) {
611 if (call.getParentCall() != null) {
612 // Never true for child calls.
613 return false;
614 }
615
616 // Loop through all the other calls and there exists a top level (has no parent) call
617 // that is not the specified call, return false.
618 for (Call otherCall : mCalls) {
619 if (call != otherCall && otherCall.getParentCall() == null) {
620 return false;
621 }
622 }
623 return true;
624 }
625
626 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700627 * Returns the first call that it finds with the given states. The states are treated as having
628 * priority order so that any call with the first state will be returned before any call with
629 * states listed later in the parameter list.
630 */
Santos Cordon23baed32014-06-27 14:45:39 -0700631 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700632 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700633 // check the foreground first
634 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
635 return mForegroundCall;
636 }
637
Santos Cordondeb8c892014-05-30 01:38:03 -0700638 for (Call call : mCalls) {
639 if (currentState == call.getState()) {
640 return call;
641 }
642 }
643 }
644 return null;
645 }
646
Santos Cordon4b2c1192014-03-19 18:15:38 -0700647 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800648 * Adds the specified call to the main list of live calls.
649 *
650 * @param call The call to add.
651 */
652 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700653 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700654
655 // TODO(santoscordon): Update mForegroundCall prior to invoking
656 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700657 for (CallsManagerListener listener : mListeners) {
658 listener.onCallAdded(call);
659 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700660 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800661 }
662
Sailesh Nepal810735e2014-03-18 18:15:46 -0700663 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700664 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700665
Santos Cordon766d04f2014-05-06 10:28:25 -0700666 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700667 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700668
669 boolean shouldNotify = false;
670 if (mCalls.contains(call)) {
671 mCalls.remove(call);
672 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700673 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800674
Sailesh Nepale59bb192014-04-01 18:33:59 -0700675 // Only broadcast changes for calls that are being tracked.
676 if (shouldNotify) {
677 for (CallsManagerListener listener : mListeners) {
678 listener.onCallRemoved(call);
679 }
680 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700681 }
682 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800683
Sailesh Nepal810735e2014-03-18 18:15:46 -0700684 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700685 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700686 *
687 * @param call The call.
688 * @param newState The new state of the call.
689 */
690 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700691 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700692 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700693 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700694 if (newState != oldState) {
695 // Unfortunately, in the telephony world the radio is king. So if the call notifies
696 // us that the call is in a particular state, we allow it even if it doesn't make
697 // sense (e.g., ACTIVE -> RINGING).
698 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
699 // into a well-defined state machine.
700 // TODO(santoscordon): Define expected state transitions here, and log when an
701 // unexpected transition occurs.
702 call.setState(newState);
703
704 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700705 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700706 for (CallsManagerListener listener : mListeners) {
707 listener.onCallStateChanged(call, oldState, newState);
708 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700709 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800710 }
711 }
712 }
713
714 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700715 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800716 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700717 private void updateForegroundCall() {
718 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700719 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700720 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700721 // of its state will be foreground by default and instead the connection service should
722 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700723 // the call should play audio and listen to microphone if it wants.
724
725 // Active calls have priority.
726 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700727 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800728 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700729 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700730
731 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700732 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700733 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800734 }
735 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800736
Sailesh Nepal810735e2014-03-18 18:15:46 -0700737 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700738 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700739 Call oldForegroundCall = mForegroundCall;
740 mForegroundCall = newForegroundCall;
741
Santos Cordona56f2762014-03-24 15:55:53 -0700742 for (CallsManagerListener listener : mListeners) {
743 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
744 }
Santos Cordon681663d2014-01-30 04:32:15 -0800745 }
746 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800747}