blob: 5ce9fe87bceee2e764f776969f3b7ca455abf0ee [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;
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 Cordon74d420b2014-05-07 14:38:47 -070045 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
49 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh 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);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070057 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState 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
126 public void onSuccessfulOutgoingCall(Call call) {
127 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
128 if (mCalls.contains(call)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700129 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700130 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700131 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700132 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700133 } else {
134 Log.wtf(this, "unexpected successful call notification: %s", call);
135 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700136 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700137
138 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700139 }
140
141 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700142 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
143 Log.v(this, "onFailedOutgoingCall, call: %s", call);
144 // TODO: Replace disconnect cause with more specific disconnect causes.
145 markCallAsDisconnected(call, errorCode, errorMsg);
146 }
147
148 @Override
149 public void onCancelledOutgoingCall(Call call) {
150 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
151 setCallState(call, CallState.ABORTED);
152 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700153 }
154
155 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700156 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700157 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700158 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700159 addCall(call);
160 }
161
162 @Override
163 public void onFailedIncomingCall(Call call) {
164 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800165 }
166
Ihab Awadcb387ac2014-05-28 16:49:38 -0700167 @Override
168 public void onRequestingRingback(Call call, boolean ringback) {
169 for (CallsManagerListener listener : mListeners) {
170 listener.onRequestingRingback(call, ringback);
171 }
172 }
173
Evan Charlton352105c2014-06-03 14:10:54 -0700174 @Override
175 public void onPostDialWait(Call call, String remaining) {
176 mInCallController.onPostDialWait(call, remaining);
177 }
178
Santos Cordona1610702014-06-04 20:22:56 -0700179 @Override
180 public void onExpiredConferenceCall(Call call) {
181 call.removeListener(this);
182 }
183
184 @Override
185 public void onConfirmedConferenceCall(Call call) {
186 addCall(call);
187 Log.v(this, "confirming Conf call %s", call);
188 for (CallsManagerListener listener : mListeners) {
189 listener.onIsConferencedChanged(call);
190 }
191 }
192
193 @Override
194 public void onParentChanged(Call call) {
195 for (CallsManagerListener listener : mListeners) {
196 listener.onIsConferencedChanged(call);
197 }
198 }
199
200 @Override
201 public void onChildrenChanged(Call call) {
202 for (CallsManagerListener listener : mListeners) {
203 listener.onIsConferencedChanged(call);
204 }
205 }
206
Ihab Awadff7493a2014-06-10 13:47:44 -0700207 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700208 public void onAudioModeIsVoipChanged(Call call) {
209 for (CallsManagerListener listener : mListeners) {
210 listener.onAudioModeIsVoipChanged(call);
211 }
212 }
213
Andrew Lee4a796602014-07-11 17:23:03 -0700214 @Override
215 public void onVideoStateChanged(Call call) {
216 for (CallsManagerListener listener : mListeners) {
217 listener.onVideoStateChanged(call);
218 }
219 }
220
Sailesh Nepal810735e2014-03-18 18:15:46 -0700221 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700222 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700223 }
224
225 Call getForegroundCall() {
226 return mForegroundCall;
227 }
228
Santos Cordonae193062014-05-21 21:21:49 -0700229 Ringer getRinger() {
230 return mRinger;
231 }
232
Santos Cordonf3671a62014-05-29 21:51:53 -0700233 InCallController getInCallController() {
234 return mInCallController;
235 }
236
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700237 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700238 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700239 if (call.isEmergencyCall()) {
240 return true;
241 }
242 }
243 return false;
244 }
245
246 CallAudioState getAudioState() {
247 return mCallAudioManager.getAudioState();
248 }
249
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700250 boolean isTtySupported() {
251 return mTtyManager.isTtySupported();
252 }
253
254 int getCurrentTtyMode() {
255 return mTtyManager.getCurrentTtyMode();
256 }
257
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800258 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700259 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800260 *
Evan Charlton89176372014-07-19 18:23:09 -0700261 * @param phoneAccountHandle The phone account which contains the component name of the connection
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700262 * serivce to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800263 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800264 */
Evan Charlton89176372014-07-19 18:23:09 -0700265 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800266 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700267 // Create a call with no handle. The handle is eventually set when the call is attached
268 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700269 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700270 mConnectionServiceRepository,
271 null /* handle */,
272 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700273 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700274 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700275 true /* isIncoming */,
276 false /* isConference */);
277
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700278 call.setExtras(extras);
Santos Cordon766d04f2014-05-06 10:28:25 -0700279 // TODO(santoscordon): Move this to be a part of addCall()
280 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700281 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800282 }
283
284 /**
Yorke Lee33501632014-03-17 19:24:12 -0700285 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800286 *
Yorke Lee33501632014-03-17 19:24:12 -0700287 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700288 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700289 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700290 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700291 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800292 */
Evan Charlton89176372014-07-19 18:23:09 -0700293 void placeOutgoingCall(Uri handle, GatewayInfo gatewayInfo, PhoneAccountHandle accountHandle,
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700294 boolean speakerphoneOn, int videoState) {
Tyler Gunnc4abd912014-07-08 14:22:10 -0700295
Santos Cordon7cdb9092014-07-24 21:33:33 -0700296 TelecommApp app = TelecommApp.getInstance();
Yorke Lee33501632014-03-17 19:24:12 -0700297 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
298
299 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700300 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700301 } else {
302 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
303 Log.pii(uriHandle), Log.pii(handle));
304 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700305
Santos Cordon7cdb9092014-07-24 21:33:33 -0700306 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
307 // as if a phoneAccount was not specified (does the default behavior instead).
308 if (accountHandle != null) {
309 List<PhoneAccountHandle> enabledAccounts =
310 app.getPhoneAccountRegistrar().getEnabledPhoneAccounts();
311 if (!enabledAccounts.contains(accountHandle)) {
312 accountHandle = null;
313 }
314 }
315
Santos Cordona1610702014-06-04 20:22:56 -0700316 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700317 mConnectionServiceRepository,
318 uriHandle,
319 gatewayInfo,
Ihab Awadb78b2762014-07-25 15:16:23 -0700320 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700321 accountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700322 false /* isIncoming */,
323 false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700324 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700325 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700326
327 // TODO(santoscordon): Move this to be a part of addCall()
328 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700329 addCall(call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700330
Nancy Chend9de92c2014-07-21 16:09:13 -0700331 // This block of code will attempt to pre-determine a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700332 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
Nancy Chend9de92c2014-07-21 16:09:13 -0700333 if (emergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700334 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700335 call.setTargetPhoneAccount(null);
Nancy Chend9de92c2014-07-21 16:09:13 -0700336 } else if (accountHandle != null) {
Santos Cordon7cdb9092014-07-24 21:33:33 -0700337 Log.d(this, "CALL with phone account: " + accountHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -0700338 call.setTargetPhoneAccount(accountHandle);
Nancy Chend9de92c2014-07-21 16:09:13 -0700339 } else {
340 // No preset account, check if default exists
Santos Cordon7cdb9092014-07-24 21:33:33 -0700341 PhoneAccountHandle defaultAccountHandle =
342 app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
Evan Charlton89176372014-07-19 18:23:09 -0700343 if (defaultAccountHandle != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700344 call.setTargetPhoneAccount(defaultAccountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700345 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700346 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700347
Ihab Awadb78b2762014-07-25 15:16:23 -0700348 if (call.getTargetPhoneAccount() != null || emergencyCall) {
Nancy Chend9de92c2014-07-21 16:09:13 -0700349 // If the account is selected, proceed to place the outgoing call
350 call.startCreateConnection();
351 } else {
352 // This is the state where the user is expected to select an account
353 call.setState(CallState.PRE_DIAL_WAIT);
354 }
Yorke Leef98fb572014-03-05 10:56:55 -0800355 }
356
357 /**
Santos Cordona1610702014-06-04 20:22:56 -0700358 * Attempts to start a conference call for the specified call.
359 *
360 * @param call The call to conference with.
361 */
362 void conference(Call call) {
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700363 Call conferenceCall = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700364 mConnectionServiceRepository,
365 null /* handle */,
366 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700367 null /* connectionManagerPhoneAccount */,
368 null /* targetPhoneAccount */,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700369 false /* isIncoming */,
370 true /* isConference */);
Santos Cordona1610702014-06-04 20:22:56 -0700371 conferenceCall.addListener(this);
372 call.conferenceInto(conferenceCall);
373 }
374
375 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800376 * Instructs Telecomm to answer 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 answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700379 *
380 * @param call The call to answer.
381 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800382 */
Andrew Lee38931d02014-07-16 10:17:36 -0700383 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700384 if (!mCalls.contains(call)) {
385 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800386 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700387 // If the foreground call is not the ringing call and it is currently isActive() or
388 // DIALING, put it on hold before answering the call.
389 if (mForegroundCall != null && mForegroundCall != call &&
390 (mForegroundCall.isActive() ||
391 mForegroundCall.getState() == CallState.DIALING)) {
392 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
393 mForegroundCall, call);
394 mForegroundCall.hold();
395 // TODO(santoscordon): Wait until we get confirmation of the active call being
396 // on-hold before answering the new call.
397 // TODO(santoscordon): Import logic from CallManager.acceptCall()
398 }
399
Santos Cordona56f2762014-03-24 15:55:53 -0700400 for (CallsManagerListener listener : mListeners) {
401 listener.onIncomingCallAnswered(call);
402 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800403
Santos Cordon61d0f702014-02-19 02:52:23 -0800404 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700405 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700406 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800407 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800408 }
409
410 /**
411 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
412 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
413 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800414 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700415 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700416 if (!mCalls.contains(call)) {
417 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800418 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700419 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700420 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700421 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700422 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800423 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800424 }
425
426 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700427 * Instructs Telecomm to play the specified DTMF tone within the specified call.
428 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700429 * @param digit The DTMF digit to play.
430 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700431 void playDtmfTone(Call call, char digit) {
432 if (!mCalls.contains(call)) {
433 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700434 } else {
435 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700436 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700437 }
438 }
439
440 /**
441 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700442 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700443 void stopDtmfTone(Call call) {
444 if (!mCalls.contains(call)) {
445 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700446 } else {
447 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700448 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700449 }
450 }
451
452 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700453 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700454 */
Evan Charlton352105c2014-06-03 14:10:54 -0700455 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700456 if (!mCalls.contains(call)) {
457 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700458 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700459 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700460 }
461 }
462
463 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800464 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
465 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
466 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800467 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700468 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700469 Log.v(this, "disconnectCall %s", call);
470
Sailesh Nepale59bb192014-04-01 18:33:59 -0700471 if (!mCalls.contains(call)) {
472 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800473 } else {
474 call.disconnect();
475 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800476 }
Santos Cordon681663d2014-01-30 04:32:15 -0800477
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700478 /**
479 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
480 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
481 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700482 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700483 void holdCall(Call call) {
484 if (!mCalls.contains(call)) {
485 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700486 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700487 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700488 call.hold();
489 }
490 }
491
492 /**
493 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
494 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
495 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700496 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700497 void unholdCall(Call call) {
498 if (!mCalls.contains(call)) {
499 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700500 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700501 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700502 call.unhold();
503 }
504 }
505
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700506 /** Called by the in-call UI to change the mute state. */
507 void mute(boolean shouldMute) {
508 mCallAudioManager.mute(shouldMute);
509 }
510
511 /**
512 * Called by the in-call UI to change the audio route, for example to change from earpiece to
513 * speaker phone.
514 */
515 void setAudioRoute(int route) {
516 mCallAudioManager.setAudioRoute(route);
517 }
518
Yorke Leed1346872014-07-28 14:38:45 -0700519 /** Called by the in-call UI to turn the proximity sensor on. */
520 void turnOnProximitySensor() {
521 mProximitySensorManager.turnOn();
522 }
523
524 /**
525 * Called by the in-call UI to turn the proximity sensor off.
526 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
527 * the screen will be kept off until the proximity sensor goes negative.
528 */
529 void turnOffProximitySensor(boolean screenOnImmediately) {
530 mProximitySensorManager.turnOff(screenOnImmediately);
531 }
532
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700533 void phoneAccountClicked(Call call) {
534 if (!mCalls.contains(call)) {
535 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
536 } else {
537 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700538 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700539 }
540
Evan Charlton89176372014-07-19 18:23:09 -0700541 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700542 if (!mCalls.contains(call)) {
543 Log.i(this, "Attemped to add account to unknown call %s", call);
544 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700545 call.setTargetPhoneAccount(account);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700546 call.startCreateConnection();
547 }
548 }
549
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700550 /** Called when the audio state changes. */
551 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
552 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700553 for (CallsManagerListener listener : mListeners) {
554 listener.onAudioStateChanged(oldAudioState, newAudioState);
555 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700556 }
557
Sailesh Nepale59bb192014-04-01 18:33:59 -0700558 void markCallAsRinging(Call call) {
559 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800560 }
561
Sailesh Nepale59bb192014-04-01 18:33:59 -0700562 void markCallAsDialing(Call call) {
563 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800564 }
565
Sailesh Nepale59bb192014-04-01 18:33:59 -0700566 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700567 if (call.getConnectTimeMillis() == 0) {
568 call.setConnectTimeMillis(System.currentTimeMillis());
569 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700570 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700571
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700572 if (call.getStartWithSpeakerphoneOn()) {
573 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
574 }
Santos Cordon681663d2014-01-30 04:32:15 -0800575 }
576
Sailesh Nepale59bb192014-04-01 18:33:59 -0700577 void markCallAsOnHold(Call call) {
578 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700579 }
580
Santos Cordon049b7b62014-01-30 05:34:26 -0800581 /**
582 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
583 * live call, then also disconnect from the in-call controller.
584 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700585 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700586 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800587 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700588 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
589 call.setDisconnectCause(disconnectCause, disconnectMessage);
590 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700591 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700592 }
593
Ben Gilada0d9f752014-02-26 11:49:03 -0800594 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700595 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700596 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700597 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700598 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700599 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700600 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
601 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700602 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700603 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700604 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700605 }
606 }
607 }
608
Santos Cordondeb8c892014-05-30 01:38:03 -0700609 boolean hasAnyCalls() {
610 return !mCalls.isEmpty();
611 }
612
Santos Cordonc7e85d42014-05-22 02:51:48 -0700613 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700614 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700615 }
616
617 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700618 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700619 }
620
Santos Cordondeb8c892014-05-30 01:38:03 -0700621 boolean onMediaButton(int type) {
622 if (hasAnyCalls()) {
623 if (HeadsetMediaButton.SHORT_PRESS == type) {
624 Call ringingCall = getFirstCallWithState(CallState.RINGING);
625 if (ringingCall == null) {
626 mCallAudioManager.toggleMute();
627 return true;
628 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700629 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700630 return true;
631 }
632 } else if (HeadsetMediaButton.LONG_PRESS == type) {
633 Log.d(this, "handleHeadsetHook: longpress -> hangup");
634 Call callToHangup = getFirstCallWithState(
635 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
636 if (callToHangup != null) {
637 callToHangup.disconnect();
638 return true;
639 }
640 }
641 }
642 return false;
643 }
644
645 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700646 * Checks to see if the specified call is the only high-level call and if so, enable the
647 * "Add-call" button. We allow you to add a second call but not a third or beyond.
648 *
649 * @param call The call to test for add-call.
650 * @return Whether the add-call feature should be enabled for the call.
651 */
652 protected boolean isAddCallCapable(Call call) {
653 if (call.getParentCall() != null) {
654 // Never true for child calls.
655 return false;
656 }
657
658 // Loop through all the other calls and there exists a top level (has no parent) call
659 // that is not the specified call, return false.
660 for (Call otherCall : mCalls) {
661 if (call != otherCall && otherCall.getParentCall() == null) {
662 return false;
663 }
664 }
665 return true;
666 }
667
668 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700669 * Returns the first call that it finds with the given states. The states are treated as having
670 * priority order so that any call with the first state will be returned before any call with
671 * states listed later in the parameter list.
672 */
Santos Cordon23baed32014-06-27 14:45:39 -0700673 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700674 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700675 // check the foreground first
676 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
677 return mForegroundCall;
678 }
679
Santos Cordondeb8c892014-05-30 01:38:03 -0700680 for (Call call : mCalls) {
681 if (currentState == call.getState()) {
682 return call;
683 }
684 }
685 }
686 return null;
687 }
688
Santos Cordon4b2c1192014-03-19 18:15:38 -0700689 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800690 * Adds the specified call to the main list of live calls.
691 *
692 * @param call The call to add.
693 */
694 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700695 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700696
697 // TODO(santoscordon): Update mForegroundCall prior to invoking
698 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700699 for (CallsManagerListener listener : mListeners) {
700 listener.onCallAdded(call);
701 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700702 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800703 }
704
Sailesh Nepal810735e2014-03-18 18:15:46 -0700705 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700706 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700707
Santos Cordon766d04f2014-05-06 10:28:25 -0700708 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700709 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700710
711 boolean shouldNotify = false;
712 if (mCalls.contains(call)) {
713 mCalls.remove(call);
714 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700715 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800716
Sailesh Nepale59bb192014-04-01 18:33:59 -0700717 // Only broadcast changes for calls that are being tracked.
718 if (shouldNotify) {
719 for (CallsManagerListener listener : mListeners) {
720 listener.onCallRemoved(call);
721 }
722 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700723 }
724 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800725
Sailesh Nepal810735e2014-03-18 18:15:46 -0700726 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700727 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700728 *
729 * @param call The call.
730 * @param newState The new state of the call.
731 */
732 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700733 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700734 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700735 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700736 if (newState != oldState) {
737 // Unfortunately, in the telephony world the radio is king. So if the call notifies
738 // us that the call is in a particular state, we allow it even if it doesn't make
739 // sense (e.g., ACTIVE -> RINGING).
740 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
741 // into a well-defined state machine.
742 // TODO(santoscordon): Define expected state transitions here, and log when an
743 // unexpected transition occurs.
744 call.setState(newState);
745
746 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700747 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700748 for (CallsManagerListener listener : mListeners) {
749 listener.onCallStateChanged(call, oldState, newState);
750 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700751 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800752 }
753 }
754 }
755
756 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700757 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800758 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700759 private void updateForegroundCall() {
760 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700761 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700762 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700763 // of its state will be foreground by default and instead the connection service should
764 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700765 // the call should play audio and listen to microphone if it wants.
766
767 // Active calls have priority.
768 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700769 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800770 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700771 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700772
773 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700774 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700775 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800776 }
777 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800778
Sailesh Nepal810735e2014-03-18 18:15:46 -0700779 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700780 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700781 Call oldForegroundCall = mForegroundCall;
782 mForegroundCall = newForegroundCall;
783
Santos Cordona56f2762014-03-24 15:55:53 -0700784 for (CallsManagerListener listener : mListeners) {
785 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
786 }
Santos Cordon681663d2014-01-30 04:32:15 -0800787 }
788 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800789}