blob: 8265b20f79f7934a5ee907d99374a52a28d525e2 [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
Nancy Chen53ceedc2014-07-08 18:56:51 -070065 /** Temporary flag for disabling account selection menu */
66 public static final boolean ENABLE_ACCOUNT_SELECT = true;
67
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 Cordon626697a2014-07-10 22:36:37 -070072 private final Set<Call> mCalls = new CopyOnWriteArraySet<Call>();
Santos Cordon681663d2014-01-30 04:32:15 -080073
Sailesh Nepal664837f2014-07-14 16:31:51 -070074 private final ConnectionServiceRepository mConnectionServiceRepository =
75 new ConnectionServiceRepository();
Santos Cordonf3671a62014-05-29 21:51:53 -070076 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
77 private final InCallController mInCallController = new InCallController();
78 private final CallAudioManager mCallAudioManager;
79 private final Ringer mRinger;
80 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070081 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070082 private final WiredHeadsetManager mWiredHeadsetManager;
83 private final TtyManager mTtyManager;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070084
85 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070086 * The call the user is currently interacting with. This is the call that should have audio
87 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080088 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070089 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080090
Santos Cordon766d04f2014-05-06 10:28:25 -070091 /** Singleton accessor. */
92 static CallsManager getInstance() {
93 return INSTANCE;
94 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080095
Santos Cordon8e8b8d22013-12-19 14:14:05 -080096 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080097 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 */
99 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700100 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700101
Santos Cordondeb8c892014-05-30 01:38:03 -0700102 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700103 mWiredHeadsetManager = new WiredHeadsetManager(app);
104 mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700105 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700106 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700107 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700108 mTtyManager = new TtyManager(app, mWiredHeadsetManager);
Santos Cordonae193062014-05-21 21:21:49 -0700109
Santos Cordondeb8c892014-05-30 01:38:03 -0700110 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700111 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700112 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700113 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700114 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700115 mListeners.add(new RingbackPlayer(this, playerFactory));
116 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700117 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700118 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700119 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700120 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700121 mListeners.add(RespondViaSmsManager.getInstance());
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)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700128 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700129 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700130 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700131 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700132 } else {
133 Log.wtf(this, "unexpected successful call notification: %s", call);
134 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700135 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700136
137 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700138 }
139
140 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700141 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
142 Log.v(this, "onFailedOutgoingCall, call: %s", call);
143 // TODO: Replace disconnect cause with more specific disconnect causes.
144 markCallAsDisconnected(call, errorCode, errorMsg);
145 }
146
147 @Override
148 public void onCancelledOutgoingCall(Call call) {
149 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
150 setCallState(call, CallState.ABORTED);
151 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700152 }
153
154 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700155 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700156 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700157 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700158 addCall(call);
159 }
160
161 @Override
162 public void onFailedIncomingCall(Call call) {
163 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800164 }
165
Ihab Awadcb387ac2014-05-28 16:49:38 -0700166 @Override
167 public void onRequestingRingback(Call call, boolean ringback) {
168 for (CallsManagerListener listener : mListeners) {
169 listener.onRequestingRingback(call, ringback);
170 }
171 }
172
Evan Charlton352105c2014-06-03 14:10:54 -0700173 @Override
174 public void onPostDialWait(Call call, String remaining) {
175 mInCallController.onPostDialWait(call, remaining);
176 }
177
Santos Cordona1610702014-06-04 20:22:56 -0700178 @Override
179 public void onExpiredConferenceCall(Call call) {
180 call.removeListener(this);
181 }
182
183 @Override
184 public void onConfirmedConferenceCall(Call call) {
185 addCall(call);
186 Log.v(this, "confirming Conf call %s", call);
187 for (CallsManagerListener listener : mListeners) {
188 listener.onIsConferencedChanged(call);
189 }
190 }
191
192 @Override
193 public void onParentChanged(Call call) {
194 for (CallsManagerListener listener : mListeners) {
195 listener.onIsConferencedChanged(call);
196 }
197 }
198
199 @Override
200 public void onChildrenChanged(Call call) {
201 for (CallsManagerListener listener : mListeners) {
202 listener.onIsConferencedChanged(call);
203 }
204 }
205
Ihab Awadff7493a2014-06-10 13:47:44 -0700206 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700207 public void onAudioModeIsVoipChanged(Call call) {
208 for (CallsManagerListener listener : mListeners) {
209 listener.onAudioModeIsVoipChanged(call);
210 }
211 }
212
Andrew Lee4a796602014-07-11 17:23:03 -0700213 @Override
214 public void onVideoStateChanged(Call call) {
215 for (CallsManagerListener listener : mListeners) {
216 listener.onVideoStateChanged(call);
217 }
218 }
219
Sailesh Nepal810735e2014-03-18 18:15:46 -0700220 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700221 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700222 }
223
224 Call getForegroundCall() {
225 return mForegroundCall;
226 }
227
Santos Cordonae193062014-05-21 21:21:49 -0700228 Ringer getRinger() {
229 return mRinger;
230 }
231
Santos Cordonf3671a62014-05-29 21:51:53 -0700232 InCallController getInCallController() {
233 return mInCallController;
234 }
235
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700236 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700237 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700238 if (call.isEmergencyCall()) {
239 return true;
240 }
241 }
242 return false;
243 }
244
245 CallAudioState getAudioState() {
246 return mCallAudioManager.getAudioState();
247 }
248
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700249 boolean isTtySupported() {
250 return mTtyManager.isTtySupported();
251 }
252
253 int getCurrentTtyMode() {
254 return mTtyManager.getCurrentTtyMode();
255 }
256
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800257 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700258 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800259 *
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700260 * @param phoneAccount The phone account which contains the component name of the connection
261 * serivce to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800262 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800263 */
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700264 void processIncomingCallIntent(PhoneAccount phoneAccount, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800265 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700266 // Create a call with no handle. The handle is eventually set when the call is attached
267 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700268 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700269 mConnectionServiceRepository,
270 null /* handle */,
271 null /* gatewayInfo */,
272 phoneAccount,
273 true /* isIncoming */,
274 false /* isConference */);
275
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700276 call.setExtras(extras);
Santos Cordon766d04f2014-05-06 10:28:25 -0700277 // TODO(santoscordon): Move this to be a part of addCall()
278 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700279 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800280 }
281
282 /**
Yorke Lee33501632014-03-17 19:24:12 -0700283 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800284 *
Yorke Lee33501632014-03-17 19:24:12 -0700285 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700286 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700287 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700288 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700289 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800290 */
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700291 void placeOutgoingCall(Uri handle, GatewayInfo gatewayInfo, PhoneAccount account,
292 boolean speakerphoneOn, int videoState) {
Tyler Gunnc4abd912014-07-08 14:22:10 -0700293
Yorke Lee33501632014-03-17 19:24:12 -0700294 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
295
296 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700297 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700298 } else {
299 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
300 Log.pii(uriHandle), Log.pii(handle));
301 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700302
Santos Cordona1610702014-06-04 20:22:56 -0700303 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700304 mConnectionServiceRepository,
305 uriHandle,
306 gatewayInfo,
307 account,
308 false /* isIncoming */,
309 false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700310 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700311 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700312
313 // TODO(santoscordon): Move this to be a part of addCall()
314 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700315 addCall(call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700316
317 // TODO: check for default account
318 if (account == null && ENABLE_ACCOUNT_SELECT) {
319 call.setState(CallState.PRE_DIAL_WAIT);
320 return;
321 }
322
Sailesh Nepal664837f2014-07-14 16:31:51 -0700323 call.startCreateConnection();
Yorke Leef98fb572014-03-05 10:56:55 -0800324 }
325
326 /**
Santos Cordona1610702014-06-04 20:22:56 -0700327 * Attempts to start a conference call for the specified call.
328 *
329 * @param call The call to conference with.
330 */
331 void conference(Call call) {
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700332 Call conferenceCall = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700333 mConnectionServiceRepository,
334 null /* handle */,
335 null /* gatewayInfo */,
336 null /* phoneAccount */,
337 false /* isIncoming */,
338 true /* isConference */);
Santos Cordona1610702014-06-04 20:22:56 -0700339 conferenceCall.addListener(this);
340 call.conferenceInto(conferenceCall);
341 }
342
343 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800344 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
345 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
346 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800347 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700348 void answerCall(Call call) {
349 if (!mCalls.contains(call)) {
350 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800351 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700352 // If the foreground call is not the ringing call and it is currently isActive() or
353 // DIALING, put it on hold before answering the call.
354 if (mForegroundCall != null && mForegroundCall != call &&
355 (mForegroundCall.isActive() ||
356 mForegroundCall.getState() == CallState.DIALING)) {
357 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
358 mForegroundCall, call);
359 mForegroundCall.hold();
360 // TODO(santoscordon): Wait until we get confirmation of the active call being
361 // on-hold before answering the new call.
362 // TODO(santoscordon): Import logic from CallManager.acceptCall()
363 }
364
Santos Cordona56f2762014-03-24 15:55:53 -0700365 for (CallsManagerListener listener : mListeners) {
366 listener.onIncomingCallAnswered(call);
367 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800368
Santos Cordon61d0f702014-02-19 02:52:23 -0800369 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700370 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800371 call.answer();
372 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800373 }
374
375 /**
376 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
377 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
378 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800379 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700380 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700381 if (!mCalls.contains(call)) {
382 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800383 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700384 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700385 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700386 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700387 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800388 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800389 }
390
391 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700392 * Instructs Telecomm to play the specified DTMF tone within the specified call.
393 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700394 * @param digit The DTMF digit to play.
395 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700396 void playDtmfTone(Call call, char digit) {
397 if (!mCalls.contains(call)) {
398 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700399 } else {
400 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700401 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700402 }
403 }
404
405 /**
406 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700407 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700408 void stopDtmfTone(Call call) {
409 if (!mCalls.contains(call)) {
410 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700411 } else {
412 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700413 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700414 }
415 }
416
417 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700418 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700419 */
Evan Charlton352105c2014-06-03 14:10:54 -0700420 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700421 if (!mCalls.contains(call)) {
422 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700423 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700424 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700425 }
426 }
427
428 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800429 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
430 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
431 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800432 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700434 Log.v(this, "disconnectCall %s", call);
435
Sailesh Nepale59bb192014-04-01 18:33:59 -0700436 if (!mCalls.contains(call)) {
437 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800438 } else {
439 call.disconnect();
440 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800441 }
Santos Cordon681663d2014-01-30 04:32:15 -0800442
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700443 /**
444 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
445 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
446 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700447 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700448 void holdCall(Call call) {
449 if (!mCalls.contains(call)) {
450 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700451 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700452 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700453 call.hold();
454 }
455 }
456
457 /**
458 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
459 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
460 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700461 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700462 void unholdCall(Call call) {
463 if (!mCalls.contains(call)) {
464 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700465 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700466 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700467 call.unhold();
468 }
469 }
470
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700471 /** Called by the in-call UI to change the mute state. */
472 void mute(boolean shouldMute) {
473 mCallAudioManager.mute(shouldMute);
474 }
475
476 /**
477 * Called by the in-call UI to change the audio route, for example to change from earpiece to
478 * speaker phone.
479 */
480 void setAudioRoute(int route) {
481 mCallAudioManager.setAudioRoute(route);
482 }
483
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700484 void phoneAccountClicked(Call call) {
485 if (!mCalls.contains(call)) {
486 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
487 } else {
488 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700489 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700490 }
491
Nancy Chen53ceedc2014-07-08 18:56:51 -0700492 void phoneAccountSelected(Call call, PhoneAccount account) {
493 if (!mCalls.contains(call)) {
494 Log.i(this, "Attemped to add account to unknown call %s", call);
495 } else {
496 call.setPhoneAccount(account);
497 call.startCreateConnection();
498 }
499 }
500
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700501 /** Called when the audio state changes. */
502 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
503 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700504 for (CallsManagerListener listener : mListeners) {
505 listener.onAudioStateChanged(oldAudioState, newAudioState);
506 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700507 }
508
Sailesh Nepale59bb192014-04-01 18:33:59 -0700509 void markCallAsRinging(Call call) {
510 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800511 }
512
Sailesh Nepale59bb192014-04-01 18:33:59 -0700513 void markCallAsDialing(Call call) {
514 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800515 }
516
Sailesh Nepale59bb192014-04-01 18:33:59 -0700517 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700518 if (call.getConnectTimeMillis() == 0) {
519 call.setConnectTimeMillis(System.currentTimeMillis());
520 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700521 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700522
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700523 if (call.getStartWithSpeakerphoneOn()) {
524 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
525 }
Santos Cordon681663d2014-01-30 04:32:15 -0800526 }
527
Sailesh Nepale59bb192014-04-01 18:33:59 -0700528 void markCallAsOnHold(Call call) {
529 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700530 }
531
Santos Cordon049b7b62014-01-30 05:34:26 -0800532 /**
533 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
534 * live call, then also disconnect from the in-call controller.
535 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700536 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700537 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800538 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700539 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
540 call.setDisconnectCause(disconnectCause, disconnectMessage);
541 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700542 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700543 }
544
Ben Gilada0d9f752014-02-26 11:49:03 -0800545 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700546 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700547 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700548 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700549 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700550 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700551 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
552 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700553 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700554 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700555 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700556 }
557 }
558 }
559
Santos Cordondeb8c892014-05-30 01:38:03 -0700560 boolean hasAnyCalls() {
561 return !mCalls.isEmpty();
562 }
563
Santos Cordonc7e85d42014-05-22 02:51:48 -0700564 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700565 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700566 }
567
568 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700569 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700570 }
571
Santos Cordondeb8c892014-05-30 01:38:03 -0700572 boolean onMediaButton(int type) {
573 if (hasAnyCalls()) {
574 if (HeadsetMediaButton.SHORT_PRESS == type) {
575 Call ringingCall = getFirstCallWithState(CallState.RINGING);
576 if (ringingCall == null) {
577 mCallAudioManager.toggleMute();
578 return true;
579 } else {
580 ringingCall.answer();
581 return true;
582 }
583 } else if (HeadsetMediaButton.LONG_PRESS == type) {
584 Log.d(this, "handleHeadsetHook: longpress -> hangup");
585 Call callToHangup = getFirstCallWithState(
586 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
587 if (callToHangup != null) {
588 callToHangup.disconnect();
589 return true;
590 }
591 }
592 }
593 return false;
594 }
595
596 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700597 * Checks to see if the specified call is the only high-level call and if so, enable the
598 * "Add-call" button. We allow you to add a second call but not a third or beyond.
599 *
600 * @param call The call to test for add-call.
601 * @return Whether the add-call feature should be enabled for the call.
602 */
603 protected boolean isAddCallCapable(Call call) {
604 if (call.getParentCall() != null) {
605 // Never true for child calls.
606 return false;
607 }
608
609 // Loop through all the other calls and there exists a top level (has no parent) call
610 // that is not the specified call, return false.
611 for (Call otherCall : mCalls) {
612 if (call != otherCall && otherCall.getParentCall() == null) {
613 return false;
614 }
615 }
616 return true;
617 }
618
619 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700620 * Returns the first call that it finds with the given states. The states are treated as having
621 * priority order so that any call with the first state will be returned before any call with
622 * states listed later in the parameter list.
623 */
Santos Cordon23baed32014-06-27 14:45:39 -0700624 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700625 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700626 // check the foreground first
627 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
628 return mForegroundCall;
629 }
630
Santos Cordondeb8c892014-05-30 01:38:03 -0700631 for (Call call : mCalls) {
632 if (currentState == call.getState()) {
633 return call;
634 }
635 }
636 }
637 return null;
638 }
639
Santos Cordon4b2c1192014-03-19 18:15:38 -0700640 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800641 * Adds the specified call to the main list of live calls.
642 *
643 * @param call The call to add.
644 */
645 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700646 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700647
648 // TODO(santoscordon): Update mForegroundCall prior to invoking
649 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700650 for (CallsManagerListener listener : mListeners) {
651 listener.onCallAdded(call);
652 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700653 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800654 }
655
Sailesh Nepal810735e2014-03-18 18:15:46 -0700656 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700657 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700658
Santos Cordon766d04f2014-05-06 10:28:25 -0700659 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700660 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700661
662 boolean shouldNotify = false;
663 if (mCalls.contains(call)) {
664 mCalls.remove(call);
665 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700666 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800667
Sailesh Nepale59bb192014-04-01 18:33:59 -0700668 // Only broadcast changes for calls that are being tracked.
669 if (shouldNotify) {
670 for (CallsManagerListener listener : mListeners) {
671 listener.onCallRemoved(call);
672 }
673 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700674 }
675 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800676
Sailesh Nepal810735e2014-03-18 18:15:46 -0700677 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700678 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700679 *
680 * @param call The call.
681 * @param newState The new state of the call.
682 */
683 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700684 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700685 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700686 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700687 if (newState != oldState) {
688 // Unfortunately, in the telephony world the radio is king. So if the call notifies
689 // us that the call is in a particular state, we allow it even if it doesn't make
690 // sense (e.g., ACTIVE -> RINGING).
691 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
692 // into a well-defined state machine.
693 // TODO(santoscordon): Define expected state transitions here, and log when an
694 // unexpected transition occurs.
695 call.setState(newState);
696
697 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700698 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700699 for (CallsManagerListener listener : mListeners) {
700 listener.onCallStateChanged(call, oldState, newState);
701 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700702 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800703 }
704 }
705 }
706
707 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700708 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800709 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700710 private void updateForegroundCall() {
711 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700712 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700713 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700714 // of its state will be foreground by default and instead the connection service should
715 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700716 // the call should play audio and listen to microphone if it wants.
717
718 // Active calls have priority.
719 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700720 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800721 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700722 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700723
724 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700725 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700726 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800727 }
728 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800729
Sailesh Nepal810735e2014-03-18 18:15:46 -0700730 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700731 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700732 Call oldForegroundCall = mForegroundCall;
733 mForegroundCall = newForegroundCall;
734
Santos Cordona56f2762014-03-24 15:55:53 -0700735 for (CallsManagerListener listener : mListeners) {
736 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
737 }
Santos Cordon681663d2014-01-30 04:32:15 -0800738 }
739 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800740}