blob: 5f01cb87e4a218812e336a4cb7ac7b8f8ef4d9de [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;
Ihab Awad6fb37c82014-08-07 19:48:57 -070021import android.telecomm.AudioState;
Santos Cordon681663d2014-01-30 04:32:15 -080022import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070023import android.telecomm.GatewayInfo;
Evan Charlton89176372014-07-19 18:23:09 -070024import android.telecomm.PhoneAccountHandle;
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 Cordon7cdb9092014-07-24 21:33:33 -070032import java.util.List;
Santos Cordona56f2762014-03-24 15:55:53 -070033import java.util.Set;
Santos Cordon626697a2014-07-10 22:36:37 -070034import java.util.concurrent.CopyOnWriteArraySet;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
39 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
40 * access from other packages specifically refraining from passing the CallsManager instance
41 * beyond the com.android.telecomm package boundary.
42 */
Santos Cordon64c7e962014-07-02 15:15:27 -070043public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080044
Santos Cordondf399862014-08-06 04:39:15 -070045 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070049 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070050 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070051 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070052 ConnectionServiceWrapper oldService,
53 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070054 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070055 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070056 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070057 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070058 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070059 void onIsConferencedChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070060 void onAudioModeIsVoipChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070061 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070062 }
63
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080065
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070067 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
68 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080069 */
Santos Cordon626697a2014-07-10 22:36:37 -070070 private final Set<Call> mCalls = new CopyOnWriteArraySet<Call>();
Santos Cordon681663d2014-01-30 04:32:15 -080071
Sailesh Nepal664837f2014-07-14 16:31:51 -070072 private final ConnectionServiceRepository mConnectionServiceRepository =
73 new ConnectionServiceRepository();
Santos Cordonf3671a62014-05-29 21:51:53 -070074 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
75 private final InCallController mInCallController = new InCallController();
76 private final CallAudioManager mCallAudioManager;
77 private final Ringer mRinger;
78 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070079 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070080 private final WiredHeadsetManager mWiredHeadsetManager;
81 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -070082 private final ProximitySensorManager mProximitySensorManager;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070083
84 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070085 * The call the user is currently interacting with. This is the call that should have audio
86 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080087 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070088 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080089
Santos Cordon766d04f2014-05-06 10:28:25 -070090 /** Singleton accessor. */
91 static CallsManager getInstance() {
92 return INSTANCE;
93 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080094
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080096 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080097 */
98 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -070099 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700100
Santos Cordondeb8c892014-05-30 01:38:03 -0700101 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700102 mWiredHeadsetManager = new WiredHeadsetManager(app);
103 mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700104 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700105 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700106 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700107 mTtyManager = new TtyManager(app, mWiredHeadsetManager);
Yorke Leed1346872014-07-28 14:38:45 -0700108 mProximitySensorManager = new ProximitySensorManager(app);
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());
Yorke Leed1346872014-07-28 14:38:45 -0700122 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800123 }
124
Santos Cordon766d04f2014-05-06 10:28:25 -0700125 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700126 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700127 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Tyler Gunncde2e502014-08-12 14:35:58 -0700128 setCallState(call, callState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700129 if (mCalls.contains(call)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700130 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700131 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700132 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700133 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700134 } else {
135 Log.wtf(this, "unexpected successful call notification: %s", call);
136 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700137 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700138
139 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700140 }
141
142 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700143 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
144 Log.v(this, "onFailedOutgoingCall, call: %s", call);
145 // TODO: Replace disconnect cause with more specific disconnect causes.
146 markCallAsDisconnected(call, errorCode, errorMsg);
147 }
148
149 @Override
150 public void onCancelledOutgoingCall(Call call) {
151 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
152 setCallState(call, CallState.ABORTED);
153 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700154 }
155
156 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700157 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700158 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700159 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700160 addCall(call);
161 }
162
163 @Override
164 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700165 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700166 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800167 }
168
Ihab Awadcb387ac2014-05-28 16:49:38 -0700169 @Override
170 public void onRequestingRingback(Call call, boolean ringback) {
171 for (CallsManagerListener listener : mListeners) {
172 listener.onRequestingRingback(call, ringback);
173 }
174 }
175
Evan Charlton352105c2014-06-03 14:10:54 -0700176 @Override
177 public void onPostDialWait(Call call, String remaining) {
178 mInCallController.onPostDialWait(call, remaining);
179 }
180
Santos Cordona1610702014-06-04 20:22:56 -0700181 @Override
182 public void onExpiredConferenceCall(Call call) {
183 call.removeListener(this);
184 }
185
186 @Override
187 public void onConfirmedConferenceCall(Call call) {
188 addCall(call);
189 Log.v(this, "confirming Conf call %s", call);
190 for (CallsManagerListener listener : mListeners) {
191 listener.onIsConferencedChanged(call);
192 }
193 }
194
195 @Override
196 public void onParentChanged(Call call) {
197 for (CallsManagerListener listener : mListeners) {
198 listener.onIsConferencedChanged(call);
199 }
200 }
201
202 @Override
203 public void onChildrenChanged(Call call) {
204 for (CallsManagerListener listener : mListeners) {
205 listener.onIsConferencedChanged(call);
206 }
207 }
208
Ihab Awadff7493a2014-06-10 13:47:44 -0700209 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700210 public void onAudioModeIsVoipChanged(Call call) {
211 for (CallsManagerListener listener : mListeners) {
212 listener.onAudioModeIsVoipChanged(call);
213 }
214 }
215
Andrew Lee4a796602014-07-11 17:23:03 -0700216 @Override
217 public void onVideoStateChanged(Call call) {
218 for (CallsManagerListener listener : mListeners) {
219 listener.onVideoStateChanged(call);
220 }
221 }
222
Sailesh Nepal810735e2014-03-18 18:15:46 -0700223 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700224 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700225 }
226
227 Call getForegroundCall() {
228 return mForegroundCall;
229 }
230
Santos Cordonae193062014-05-21 21:21:49 -0700231 Ringer getRinger() {
232 return mRinger;
233 }
234
Santos Cordonf3671a62014-05-29 21:51:53 -0700235 InCallController getInCallController() {
236 return mInCallController;
237 }
238
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700239 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700240 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700241 if (call.isEmergencyCall()) {
242 return true;
243 }
244 }
245 return false;
246 }
247
Ihab Awad6fb37c82014-08-07 19:48:57 -0700248 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700249 return mCallAudioManager.getAudioState();
250 }
251
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700252 boolean isTtySupported() {
253 return mTtyManager.isTtySupported();
254 }
255
256 int getCurrentTtyMode() {
257 return mTtyManager.getCurrentTtyMode();
258 }
259
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800260 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700261 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800262 *
Evan Charlton89176372014-07-19 18:23:09 -0700263 * @param phoneAccountHandle The phone account which contains the component name of the connection
Nancy Chen0d3076c2014-07-30 14:45:44 -0700264 * service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800265 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800266 */
Evan Charlton89176372014-07-19 18:23:09 -0700267 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800268 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700269 // Create a call with no handle. The handle is eventually set when the call is attached
270 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700271 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700272 mConnectionServiceRepository,
273 null /* handle */,
274 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700275 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700276 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700277 true /* isIncoming */,
278 false /* isConference */);
279
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700280 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700281 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700282 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700283 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800284 }
285
Nancy Chen0d3076c2014-07-30 14:45:44 -0700286
287 /**
288 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
289 *
290 * NOTE: emergency calls will never pass through this because they call
291 * placeOutgoingCall directly.
292 *
293 */
294 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle) {
295 // We only allow a single outgoing call at any given time. Before placing a call, make sure
296 // there doesn't already exist another outgoing call.
297 Call call = getFirstCallWithState(CallState.NEW, CallState.DIALING);
298
299 if (call != null) {
300 Log.i(this, "Canceling simultaneous outgoing call.");
301 return null;
302 }
303
304 // Create a call with original handle. The handle may be changed when the call is attached
305 // to a connection service, but in most cases will remain the same.
306 call = new Call(
307 mConnectionServiceRepository,
308 handle,
309 null /* gatewayInfo */,
310 null /* connectionManagerPhoneAccount */,
311 phoneAccountHandle,
312 false /* isIncoming */,
313 false /* isConference */);
314 call.setState(CallState.CONNECTING);
315
316 // TODO: Move this to be a part of addCall()
317 call.addListener(this);
318 addCall(call);
319
320 return call;
321 }
322
Ben Gilada0d9f752014-02-26 11:49:03 -0800323 /**
Yorke Lee33501632014-03-17 19:24:12 -0700324 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800325 *
Yorke Lee33501632014-03-17 19:24:12 -0700326 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700327 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700328 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700329 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700330 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800331 */
Nancy Chen0d3076c2014-07-30 14:45:44 -0700332 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo,
333 PhoneAccountHandle accountHandle, boolean speakerphoneOn, int videoState) {
334 if (call == null) {
335 // don't do anything if the call no longer exists
336 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700337 return;
338 }
339
Santos Cordon7cdb9092014-07-24 21:33:33 -0700340 TelecommApp app = TelecommApp.getInstance();
Yorke Lee33501632014-03-17 19:24:12 -0700341 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
342
343 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700344 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700345 } else {
346 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
347 Log.pii(uriHandle), Log.pii(handle));
348 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700349
Nancy Chen0d3076c2014-07-30 14:45:44 -0700350 //TODO: phone account is already set in {@link #startOutgoingCall}, refactor so this is
351 // not redundant.
352 //
Santos Cordon7cdb9092014-07-24 21:33:33 -0700353 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
354 // as if a phoneAccount was not specified (does the default behavior instead).
355 if (accountHandle != null) {
356 List<PhoneAccountHandle> enabledAccounts =
Ihab Awad6fb37c82014-08-07 19:48:57 -0700357 app.getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Santos Cordon7cdb9092014-07-24 21:33:33 -0700358 if (!enabledAccounts.contains(accountHandle)) {
359 accountHandle = null;
360 }
361 }
362
Nancy Chen0d3076c2014-07-30 14:45:44 -0700363 call.setHandle(uriHandle);
364 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700365 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700366 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700367
Nancy Chend9de92c2014-07-21 16:09:13 -0700368 // This block of code will attempt to pre-determine a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700369 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
Nancy Chend9de92c2014-07-21 16:09:13 -0700370 if (emergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700371 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700372 call.setTargetPhoneAccount(null);
Nancy Chend9de92c2014-07-21 16:09:13 -0700373 } else if (accountHandle != null) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700374 // NOTE: this is currently not an option because no feature currently exists to
375 // preset a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700376 Log.d(this, "CALL with phone account: " + accountHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -0700377 call.setTargetPhoneAccount(accountHandle);
Nancy Chend9de92c2014-07-21 16:09:13 -0700378 } else {
379 // No preset account, check if default exists
Santos Cordon7cdb9092014-07-24 21:33:33 -0700380 PhoneAccountHandle defaultAccountHandle =
381 app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
Evan Charlton89176372014-07-19 18:23:09 -0700382 if (defaultAccountHandle != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700383 call.setTargetPhoneAccount(defaultAccountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700384 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700385 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700386
Ihab Awadb78b2762014-07-25 15:16:23 -0700387 if (call.getTargetPhoneAccount() != null || emergencyCall) {
Nancy Chend9de92c2014-07-21 16:09:13 -0700388 // If the account is selected, proceed to place the outgoing call
389 call.startCreateConnection();
390 } else {
391 // This is the state where the user is expected to select an account
392 call.setState(CallState.PRE_DIAL_WAIT);
393 }
Yorke Leef98fb572014-03-05 10:56:55 -0800394 }
395
396 /**
Santos Cordona1610702014-06-04 20:22:56 -0700397 * Attempts to start a conference call for the specified call.
398 *
Santos Cordon12d61822014-07-29 16:02:20 -0700399 * @param call The call to conference.
400 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700401 */
Santos Cordon12d61822014-07-29 16:02:20 -0700402 void conference(Call call, Call otherCall) {
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700403 Call conferenceCall = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700404 mConnectionServiceRepository,
405 null /* handle */,
406 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700407 null /* connectionManagerPhoneAccount */,
408 null /* targetPhoneAccount */,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700409 false /* isIncoming */,
410 true /* isConference */);
Santos Cordona1610702014-06-04 20:22:56 -0700411 conferenceCall.addListener(this);
412 call.conferenceInto(conferenceCall);
413 }
414
415 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800416 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
417 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
418 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700419 *
420 * @param call The call to answer.
421 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800422 */
Andrew Lee38931d02014-07-16 10:17:36 -0700423 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700424 if (!mCalls.contains(call)) {
425 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800426 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700427 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700428 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700429 if (mForegroundCall != null && mForegroundCall != call &&
430 (mForegroundCall.isActive() ||
431 mForegroundCall.getState() == CallState.DIALING)) {
432 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
433 mForegroundCall, call);
434 mForegroundCall.hold();
Santos Cordondf399862014-08-06 04:39:15 -0700435 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700436 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700437 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700438 }
439
Santos Cordona56f2762014-03-24 15:55:53 -0700440 for (CallsManagerListener listener : mListeners) {
441 listener.onIncomingCallAnswered(call);
442 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800443
Santos Cordon61d0f702014-02-19 02:52:23 -0800444 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700445 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700446 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800447 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800448 }
449
450 /**
451 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
452 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
453 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800454 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700455 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700456 if (!mCalls.contains(call)) {
457 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800458 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700459 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700460 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700461 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700462 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800463 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800464 }
465
466 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700467 * Instructs Telecomm to play the specified DTMF tone within the specified call.
468 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700469 * @param digit The DTMF digit to play.
470 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700471 void playDtmfTone(Call call, char digit) {
472 if (!mCalls.contains(call)) {
473 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700474 } else {
475 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700476 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700477 }
478 }
479
480 /**
481 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700482 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700483 void stopDtmfTone(Call call) {
484 if (!mCalls.contains(call)) {
485 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700486 } else {
487 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700488 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700489 }
490 }
491
492 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700493 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700494 */
Evan Charlton352105c2014-06-03 14:10:54 -0700495 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700496 if (!mCalls.contains(call)) {
497 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700498 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700499 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700500 }
501 }
502
503 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800504 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
505 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
506 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800507 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700508 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700509 Log.v(this, "disconnectCall %s", call);
510
Sailesh Nepale59bb192014-04-01 18:33:59 -0700511 if (!mCalls.contains(call)) {
512 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800513 } else {
514 call.disconnect();
515 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800516 }
Santos Cordon681663d2014-01-30 04:32:15 -0800517
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700518 /**
519 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
520 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
521 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700522 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700523 void holdCall(Call call) {
524 if (!mCalls.contains(call)) {
525 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700526 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700527 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700528 call.hold();
529 }
530 }
531
532 /**
533 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
534 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
535 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700536 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700537 void unholdCall(Call call) {
538 if (!mCalls.contains(call)) {
539 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700540 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700541 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700542 call.unhold();
543 }
544 }
545
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700546 /** Called by the in-call UI to change the mute state. */
547 void mute(boolean shouldMute) {
548 mCallAudioManager.mute(shouldMute);
549 }
550
551 /**
552 * Called by the in-call UI to change the audio route, for example to change from earpiece to
553 * speaker phone.
554 */
555 void setAudioRoute(int route) {
556 mCallAudioManager.setAudioRoute(route);
557 }
558
Yorke Leed1346872014-07-28 14:38:45 -0700559 /** Called by the in-call UI to turn the proximity sensor on. */
560 void turnOnProximitySensor() {
561 mProximitySensorManager.turnOn();
562 }
563
564 /**
565 * Called by the in-call UI to turn the proximity sensor off.
566 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
567 * the screen will be kept off until the proximity sensor goes negative.
568 */
569 void turnOffProximitySensor(boolean screenOnImmediately) {
570 mProximitySensorManager.turnOff(screenOnImmediately);
571 }
572
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700573 void phoneAccountClicked(Call call) {
574 if (!mCalls.contains(call)) {
575 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
576 } else {
577 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700578 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700579 }
580
Evan Charlton89176372014-07-19 18:23:09 -0700581 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700582 if (!mCalls.contains(call)) {
583 Log.i(this, "Attemped to add account to unknown call %s", call);
584 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700585 call.setTargetPhoneAccount(account);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700586 call.startCreateConnection();
587 }
588 }
589
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700590 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700591 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700592 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700593 for (CallsManagerListener listener : mListeners) {
594 listener.onAudioStateChanged(oldAudioState, newAudioState);
595 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700596 }
597
Sailesh Nepale59bb192014-04-01 18:33:59 -0700598 void markCallAsRinging(Call call) {
599 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800600 }
601
Sailesh Nepale59bb192014-04-01 18:33:59 -0700602 void markCallAsDialing(Call call) {
603 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800604 }
605
Sailesh Nepale59bb192014-04-01 18:33:59 -0700606 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700607 if (call.getConnectTimeMillis() == 0) {
608 call.setConnectTimeMillis(System.currentTimeMillis());
609 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700610 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700611
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700612 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700613 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700614 }
Santos Cordon681663d2014-01-30 04:32:15 -0800615 }
616
Sailesh Nepale59bb192014-04-01 18:33:59 -0700617 void markCallAsOnHold(Call call) {
618 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700619 }
620
Santos Cordon049b7b62014-01-30 05:34:26 -0800621 /**
Ihab Awad6fb37c82014-08-07 19:48:57 -0700622 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the last
Santos Cordon049b7b62014-01-30 05:34:26 -0800623 * live call, then also disconnect from the in-call controller.
624 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700625 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700626 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800627 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700628 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
629 call.setDisconnectCause(disconnectCause, disconnectMessage);
630 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700631 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700632 }
633
Ben Gilada0d9f752014-02-26 11:49:03 -0800634 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700635 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700636 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700637 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700638 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700639 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700640 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
641 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700642 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700643 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700644 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700645 }
646 }
647 }
648
Santos Cordondeb8c892014-05-30 01:38:03 -0700649 boolean hasAnyCalls() {
650 return !mCalls.isEmpty();
651 }
652
Santos Cordonc7e85d42014-05-22 02:51:48 -0700653 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700654 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700655 }
656
657 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700658 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700659 }
660
Santos Cordondeb8c892014-05-30 01:38:03 -0700661 boolean onMediaButton(int type) {
662 if (hasAnyCalls()) {
663 if (HeadsetMediaButton.SHORT_PRESS == type) {
664 Call ringingCall = getFirstCallWithState(CallState.RINGING);
665 if (ringingCall == null) {
666 mCallAudioManager.toggleMute();
667 return true;
668 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700669 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700670 return true;
671 }
672 } else if (HeadsetMediaButton.LONG_PRESS == type) {
673 Log.d(this, "handleHeadsetHook: longpress -> hangup");
674 Call callToHangup = getFirstCallWithState(
675 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
676 if (callToHangup != null) {
677 callToHangup.disconnect();
678 return true;
679 }
680 }
681 }
682 return false;
683 }
684
685 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700686 * Checks to see if the specified call is the only high-level call and if so, enable the
687 * "Add-call" button. We allow you to add a second call but not a third or beyond.
688 *
689 * @param call The call to test for add-call.
690 * @return Whether the add-call feature should be enabled for the call.
691 */
692 protected boolean isAddCallCapable(Call call) {
693 if (call.getParentCall() != null) {
694 // Never true for child calls.
695 return false;
696 }
697
698 // Loop through all the other calls and there exists a top level (has no parent) call
699 // that is not the specified call, return false.
700 for (Call otherCall : mCalls) {
701 if (call != otherCall && otherCall.getParentCall() == null) {
702 return false;
703 }
704 }
705 return true;
706 }
707
708 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700709 * Returns the first call that it finds with the given states. The states are treated as having
710 * priority order so that any call with the first state will be returned before any call with
711 * states listed later in the parameter list.
712 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700713 Call getFirstCallWithState(int... states) {
714 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700715 // check the foreground first
716 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
717 return mForegroundCall;
718 }
719
Santos Cordondeb8c892014-05-30 01:38:03 -0700720 for (Call call : mCalls) {
721 if (currentState == call.getState()) {
722 return call;
723 }
724 }
725 }
726 return null;
727 }
728
Santos Cordon4b2c1192014-03-19 18:15:38 -0700729 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800730 * Adds the specified call to the main list of live calls.
731 *
732 * @param call The call to add.
733 */
734 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700735 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700736
Santos Cordondf399862014-08-06 04:39:15 -0700737 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700738 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700739 for (CallsManagerListener listener : mListeners) {
740 listener.onCallAdded(call);
741 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700742 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800743 }
744
Sailesh Nepal810735e2014-03-18 18:15:46 -0700745 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700746 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700747
Santos Cordon766d04f2014-05-06 10:28:25 -0700748 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700749 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700750
751 boolean shouldNotify = false;
752 if (mCalls.contains(call)) {
753 mCalls.remove(call);
754 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700755 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800756
Sailesh Nepale59bb192014-04-01 18:33:59 -0700757 // Only broadcast changes for calls that are being tracked.
758 if (shouldNotify) {
759 for (CallsManagerListener listener : mListeners) {
760 listener.onCallRemoved(call);
761 }
762 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700763 }
764 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800765
Sailesh Nepal810735e2014-03-18 18:15:46 -0700766 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700767 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700768 *
769 * @param call The call.
770 * @param newState The new state of the call.
771 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700772 private void setCallState(Call call, int newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700773 Preconditions.checkNotNull(newState);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700774 int oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700775 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700776 if (newState != oldState) {
777 // Unfortunately, in the telephony world the radio is king. So if the call notifies
778 // us that the call is in a particular state, we allow it even if it doesn't make
Ihab Awad6fb37c82014-08-07 19:48:57 -0700779 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700780 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700781 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700782 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700783 // unexpected transition occurs.
784 call.setState(newState);
785
786 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700787 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700788 for (CallsManagerListener listener : mListeners) {
789 listener.onCallStateChanged(call, oldState, newState);
790 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700791 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800792 }
793 }
794 }
795
796 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700797 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800798 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700799 private void updateForegroundCall() {
800 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700801 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700802 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700803 // of its state will be foreground by default and instead the connection service should
804 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700805 // the call should play audio and listen to microphone if it wants.
806
807 // Active calls have priority.
808 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700809 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800810 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700811 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700812
813 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700814 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700815 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800816 }
817 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800818
Sailesh Nepal810735e2014-03-18 18:15:46 -0700819 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700820 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700821 Call oldForegroundCall = mForegroundCall;
822 mForegroundCall = newForegroundCall;
823
Santos Cordona56f2762014-03-24 15:55:53 -0700824 for (CallsManagerListener listener : mListeners) {
825 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
826 }
Santos Cordon681663d2014-01-30 04:32:15 -0800827 }
828 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800829}