blob: 2d5d5c09a9b74e616c84541d0b4e6bf4439f87f4 [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);
Sailesh Nepal810735e2014-03-18 18:15:46 -070060 }
61
Santos Cordon8e8b8d22013-12-19 14:14:05 -080062 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080063
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070065 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
66 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080067 */
Santos Cordon626697a2014-07-10 22:36:37 -070068 private final Set<Call> mCalls = new CopyOnWriteArraySet<Call>();
Santos Cordon681663d2014-01-30 04:32:15 -080069
Santos Cordonf3671a62014-05-29 21:51:53 -070070 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
71 private final InCallController mInCallController = new InCallController();
72 private final CallAudioManager mCallAudioManager;
73 private final Ringer mRinger;
74 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070075 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070076
77 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070078 * The call the user is currently interacting with. This is the call that should have audio
79 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080080 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070081 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080082
Santos Cordon766d04f2014-05-06 10:28:25 -070083 /** Singleton accessor. */
84 static CallsManager getInstance() {
85 return INSTANCE;
86 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080087
Santos Cordon8e8b8d22013-12-19 14:14:05 -080088 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080089 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080090 */
91 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -070092 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -070093
Santos Cordondeb8c892014-05-30 01:38:03 -070094 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
95 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -070096 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -070097 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -070098 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonae193062014-05-21 21:21:49 -070099
Santos Cordondeb8c892014-05-30 01:38:03 -0700100 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700101 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700102 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700103 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700104 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700105 mListeners.add(new RingbackPlayer(this, playerFactory));
106 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700107 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700108 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700109 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700110 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700111 mListeners.add(RespondViaSmsManager.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800112 }
113
Santos Cordon766d04f2014-05-06 10:28:25 -0700114 @Override
115 public void onSuccessfulOutgoingCall(Call call) {
116 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
117 if (mCalls.contains(call)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700118 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700119 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700120 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700121 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700122 } else {
123 Log.wtf(this, "unexpected successful call notification: %s", call);
124 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700125 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700126
127 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700128 }
129
130 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700131 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
132 Log.v(this, "onFailedOutgoingCall, call: %s", call);
133 // TODO: Replace disconnect cause with more specific disconnect causes.
134 markCallAsDisconnected(call, errorCode, errorMsg);
135 }
136
137 @Override
138 public void onCancelledOutgoingCall(Call call) {
139 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
140 setCallState(call, CallState.ABORTED);
141 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700142 }
143
144 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700145 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700146 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700147 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700148 addCall(call);
149 }
150
151 @Override
152 public void onFailedIncomingCall(Call call) {
153 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800154 }
155
Ihab Awadcb387ac2014-05-28 16:49:38 -0700156 @Override
157 public void onRequestingRingback(Call call, boolean ringback) {
158 for (CallsManagerListener listener : mListeners) {
159 listener.onRequestingRingback(call, ringback);
160 }
161 }
162
Evan Charlton352105c2014-06-03 14:10:54 -0700163 @Override
164 public void onPostDialWait(Call call, String remaining) {
165 mInCallController.onPostDialWait(call, remaining);
166 }
167
Santos Cordona1610702014-06-04 20:22:56 -0700168 @Override
169 public void onExpiredConferenceCall(Call call) {
170 call.removeListener(this);
171 }
172
173 @Override
174 public void onConfirmedConferenceCall(Call call) {
175 addCall(call);
176 Log.v(this, "confirming Conf call %s", call);
177 for (CallsManagerListener listener : mListeners) {
178 listener.onIsConferencedChanged(call);
179 }
180 }
181
182 @Override
183 public void onParentChanged(Call call) {
184 for (CallsManagerListener listener : mListeners) {
185 listener.onIsConferencedChanged(call);
186 }
187 }
188
189 @Override
190 public void onChildrenChanged(Call call) {
191 for (CallsManagerListener listener : mListeners) {
192 listener.onIsConferencedChanged(call);
193 }
194 }
195
Ihab Awadff7493a2014-06-10 13:47:44 -0700196 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700197 public void onAudioModeIsVoipChanged(Call call) {
198 for (CallsManagerListener listener : mListeners) {
199 listener.onAudioModeIsVoipChanged(call);
200 }
201 }
202
Sailesh Nepal810735e2014-03-18 18:15:46 -0700203 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700204 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700205 }
206
207 Call getForegroundCall() {
208 return mForegroundCall;
209 }
210
Santos Cordonae193062014-05-21 21:21:49 -0700211 Ringer getRinger() {
212 return mRinger;
213 }
214
Santos Cordonf3671a62014-05-29 21:51:53 -0700215 InCallController getInCallController() {
216 return mInCallController;
217 }
218
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700219 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700220 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700221 if (call.isEmergencyCall()) {
222 return true;
223 }
224 }
225 return false;
226 }
227
228 CallAudioState getAudioState() {
229 return mCallAudioManager.getAudioState();
230 }
231
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800232 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800233 * Starts the incoming call sequence by having switchboard gather more information about the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700234 * specified call; using the specified connection service component name. Upon success,
235 * execution returns to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800236 *
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700237 * @param phoneAccount The phone account which contains the component name of the connection
238 * serivce to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800239 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800240 */
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700241 void processIncomingCallIntent(PhoneAccount phoneAccount, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800242 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800243 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700244 // additional information from the connection service, but for now we just need one to pass
Sailesh Nepale59bb192014-04-01 18:33:59 -0700245 // around.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700246 Call call = new Call(
247 null, null, phoneAccount, true /* isIncoming */, false /* isConference */);
248 call.setExtras(extras);
Santos Cordon766d04f2014-05-06 10:28:25 -0700249 // TODO(santoscordon): Move this to be a part of addCall()
250 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800251
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700252 call.startIncoming();
Ben Gilada0d9f752014-02-26 11:49:03 -0800253 }
254
255 /**
Yorke Lee33501632014-03-17 19:24:12 -0700256 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800257 *
Yorke Lee33501632014-03-17 19:24:12 -0700258 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800259 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700260 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700261 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700262 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700263 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800264 */
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700265 void placeOutgoingCall(Uri handle, GatewayInfo gatewayInfo, PhoneAccount account,
266 boolean speakerphoneOn, int videoState) {
Tyler Gunnc4abd912014-07-08 14:22:10 -0700267
Yorke Lee33501632014-03-17 19:24:12 -0700268 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
269
270 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700271 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700272 } else {
273 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
274 Log.pii(uriHandle), Log.pii(handle));
275 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700276
Santos Cordona1610702014-06-04 20:22:56 -0700277 Call call = new Call(
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700278 uriHandle, gatewayInfo, account, false /* isIncoming */, false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700279 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700280 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700281
282 // TODO(santoscordon): Move this to be a part of addCall()
283 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700284 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800285
Santos Cordon766d04f2014-05-06 10:28:25 -0700286 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800287 }
288
289 /**
Santos Cordona1610702014-06-04 20:22:56 -0700290 * Attempts to start a conference call for the specified call.
291 *
292 * @param call The call to conference with.
293 */
294 void conference(Call call) {
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700295 Call conferenceCall = new Call(
296 null, null, null, false /* isIncoming */, true /* isConference */);
Santos Cordona1610702014-06-04 20:22:56 -0700297 conferenceCall.addListener(this);
298 call.conferenceInto(conferenceCall);
299 }
300
301 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800302 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
303 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
304 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800305 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700306 void answerCall(Call call) {
307 if (!mCalls.contains(call)) {
308 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800309 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700310 // If the foreground call is not the ringing call and it is currently isActive() or
311 // DIALING, put it on hold before answering the call.
312 if (mForegroundCall != null && mForegroundCall != call &&
313 (mForegroundCall.isActive() ||
314 mForegroundCall.getState() == CallState.DIALING)) {
315 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
316 mForegroundCall, call);
317 mForegroundCall.hold();
318 // TODO(santoscordon): Wait until we get confirmation of the active call being
319 // on-hold before answering the new call.
320 // TODO(santoscordon): Import logic from CallManager.acceptCall()
321 }
322
Santos Cordona56f2762014-03-24 15:55:53 -0700323 for (CallsManagerListener listener : mListeners) {
324 listener.onIncomingCallAnswered(call);
325 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800326
Santos Cordon61d0f702014-02-19 02:52:23 -0800327 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700328 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800329 call.answer();
330 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800331 }
332
333 /**
334 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
335 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
336 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800337 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700338 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700339 if (!mCalls.contains(call)) {
340 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800341 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700342 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700343 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700344 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700345 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800346 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800347 }
348
349 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700350 * Instructs Telecomm to play the specified DTMF tone within the specified call.
351 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700352 * @param digit The DTMF digit to play.
353 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700354 void playDtmfTone(Call call, char digit) {
355 if (!mCalls.contains(call)) {
356 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700357 } else {
358 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700359 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700360 }
361 }
362
363 /**
364 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700365 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700366 void stopDtmfTone(Call call) {
367 if (!mCalls.contains(call)) {
368 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700369 } else {
370 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700371 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700372 }
373 }
374
375 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700376 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700377 */
Evan Charlton352105c2014-06-03 14:10:54 -0700378 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700379 if (!mCalls.contains(call)) {
380 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700381 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700382 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700383 }
384 }
385
386 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800387 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
388 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
389 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800390 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700391 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700392 Log.v(this, "disconnectCall %s", call);
393
Sailesh Nepale59bb192014-04-01 18:33:59 -0700394 if (!mCalls.contains(call)) {
395 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800396 } else {
397 call.disconnect();
398 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800399 }
Santos Cordon681663d2014-01-30 04:32:15 -0800400
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700401 /**
402 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
403 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
404 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700405 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700406 void holdCall(Call call) {
407 if (!mCalls.contains(call)) {
408 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700409 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700410 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700411 call.hold();
412 }
413 }
414
415 /**
416 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
417 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
418 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700419 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700420 void unholdCall(Call call) {
421 if (!mCalls.contains(call)) {
422 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700423 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700424 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700425 call.unhold();
426 }
427 }
428
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700429 /** Called by the in-call UI to change the mute state. */
430 void mute(boolean shouldMute) {
431 mCallAudioManager.mute(shouldMute);
432 }
433
434 /**
435 * Called by the in-call UI to change the audio route, for example to change from earpiece to
436 * speaker phone.
437 */
438 void setAudioRoute(int route) {
439 mCallAudioManager.setAudioRoute(route);
440 }
441
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700442 void phoneAccountClicked(Call call) {
443 if (!mCalls.contains(call)) {
444 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
445 } else {
446 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700447 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700448 }
449
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700450 /** Called when the audio state changes. */
451 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
452 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700453 for (CallsManagerListener listener : mListeners) {
454 listener.onAudioStateChanged(oldAudioState, newAudioState);
455 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700456 }
457
Sailesh Nepale59bb192014-04-01 18:33:59 -0700458 void markCallAsRinging(Call call) {
459 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800460 }
461
Sailesh Nepale59bb192014-04-01 18:33:59 -0700462 void markCallAsDialing(Call call) {
463 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800464 }
465
Sailesh Nepale59bb192014-04-01 18:33:59 -0700466 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700467 if (call.getConnectTimeMillis() == 0) {
468 call.setConnectTimeMillis(System.currentTimeMillis());
469 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700470 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700471
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700472 if (call.getStartWithSpeakerphoneOn()) {
473 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
474 }
Santos Cordon681663d2014-01-30 04:32:15 -0800475 }
476
Sailesh Nepale59bb192014-04-01 18:33:59 -0700477 void markCallAsOnHold(Call call) {
478 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700479 }
480
Santos Cordon049b7b62014-01-30 05:34:26 -0800481 /**
482 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
483 * live call, then also disconnect from the in-call controller.
484 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700485 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700486 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800487 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700488 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
489 call.setDisconnectCause(disconnectCause, disconnectMessage);
490 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700491 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700492 }
493
Ben Gilada0d9f752014-02-26 11:49:03 -0800494 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700495 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700496 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700497 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700498 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700499 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700500 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
501 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700502 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700503 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700504 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700505 }
506 }
507 }
508
Santos Cordondeb8c892014-05-30 01:38:03 -0700509 boolean hasAnyCalls() {
510 return !mCalls.isEmpty();
511 }
512
Santos Cordonc7e85d42014-05-22 02:51:48 -0700513 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700514 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700515 }
516
517 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700518 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700519 }
520
Santos Cordondeb8c892014-05-30 01:38:03 -0700521 boolean onMediaButton(int type) {
522 if (hasAnyCalls()) {
523 if (HeadsetMediaButton.SHORT_PRESS == type) {
524 Call ringingCall = getFirstCallWithState(CallState.RINGING);
525 if (ringingCall == null) {
526 mCallAudioManager.toggleMute();
527 return true;
528 } else {
529 ringingCall.answer();
530 return true;
531 }
532 } else if (HeadsetMediaButton.LONG_PRESS == type) {
533 Log.d(this, "handleHeadsetHook: longpress -> hangup");
534 Call callToHangup = getFirstCallWithState(
535 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
536 if (callToHangup != null) {
537 callToHangup.disconnect();
538 return true;
539 }
540 }
541 }
542 return false;
543 }
544
545 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700546 * Checks to see if the specified call is the only high-level call and if so, enable the
547 * "Add-call" button. We allow you to add a second call but not a third or beyond.
548 *
549 * @param call The call to test for add-call.
550 * @return Whether the add-call feature should be enabled for the call.
551 */
552 protected boolean isAddCallCapable(Call call) {
553 if (call.getParentCall() != null) {
554 // Never true for child calls.
555 return false;
556 }
557
558 // Loop through all the other calls and there exists a top level (has no parent) call
559 // that is not the specified call, return false.
560 for (Call otherCall : mCalls) {
561 if (call != otherCall && otherCall.getParentCall() == null) {
562 return false;
563 }
564 }
565 return true;
566 }
567
568 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700569 * Returns the first call that it finds with the given states. The states are treated as having
570 * priority order so that any call with the first state will be returned before any call with
571 * states listed later in the parameter list.
572 */
Santos Cordon23baed32014-06-27 14:45:39 -0700573 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700574 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700575 // check the foreground first
576 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
577 return mForegroundCall;
578 }
579
Santos Cordondeb8c892014-05-30 01:38:03 -0700580 for (Call call : mCalls) {
581 if (currentState == call.getState()) {
582 return call;
583 }
584 }
585 }
586 return null;
587 }
588
Santos Cordon4b2c1192014-03-19 18:15:38 -0700589 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800590 * Adds the specified call to the main list of live calls.
591 *
592 * @param call The call to add.
593 */
594 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700595 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700596
597 // TODO(santoscordon): Update mForegroundCall prior to invoking
598 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700599 for (CallsManagerListener listener : mListeners) {
600 listener.onCallAdded(call);
601 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700602 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800603 }
604
Sailesh Nepal810735e2014-03-18 18:15:46 -0700605 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700606 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700607
Santos Cordon766d04f2014-05-06 10:28:25 -0700608 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700609 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700610
611 boolean shouldNotify = false;
612 if (mCalls.contains(call)) {
613 mCalls.remove(call);
614 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700615 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800616
Sailesh Nepale59bb192014-04-01 18:33:59 -0700617 // Only broadcast changes for calls that are being tracked.
618 if (shouldNotify) {
619 for (CallsManagerListener listener : mListeners) {
620 listener.onCallRemoved(call);
621 }
622 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700623 }
624 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800625
Sailesh Nepal810735e2014-03-18 18:15:46 -0700626 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700627 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700628 *
629 * @param call The call.
630 * @param newState The new state of the call.
631 */
632 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700633 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700634 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700635 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700636 if (newState != oldState) {
637 // Unfortunately, in the telephony world the radio is king. So if the call notifies
638 // us that the call is in a particular state, we allow it even if it doesn't make
639 // sense (e.g., ACTIVE -> RINGING).
640 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
641 // into a well-defined state machine.
642 // TODO(santoscordon): Define expected state transitions here, and log when an
643 // unexpected transition occurs.
644 call.setState(newState);
645
646 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700647 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700648 for (CallsManagerListener listener : mListeners) {
649 listener.onCallStateChanged(call, oldState, newState);
650 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700651 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800652 }
653 }
654 }
655
656 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700657 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800658 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700659 private void updateForegroundCall() {
660 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700661 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700662 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700663 // of its state will be foreground by default and instead the connection service should
664 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700665 // the call should play audio and listen to microphone if it wants.
666
667 // Active calls have priority.
668 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700669 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800670 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700671 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700672
673 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700674 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700675 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800676 }
677 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800678
Sailesh Nepal810735e2014-03-18 18:15:46 -0700679 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700680 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700681 Call oldForegroundCall = mForegroundCall;
682 mForegroundCall = newForegroundCall;
683
Santos Cordona56f2762014-03-24 15:55:53 -0700684 for (CallsManagerListener listener : mListeners) {
685 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
686 }
Santos Cordon681663d2014-01-30 04:32:15 -0800687 }
688 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800689}