blob: 7ab48db330271014ee424180659cf887c736042e [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
Sailesh Nepal810735e2014-03-18 18:15:46 -070027import com.google.common.collect.ImmutableCollection;
28import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080029
Santos Cordonf3671a62014-05-29 21:51:53 -070030import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070031import java.util.List;
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 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070038 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080039 * 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 Cordondf399862014-08-06 04:39:15 -070044 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070045 interface CallsManagerListener {
46 void onCallAdded(Call call);
47 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070048 void onCallStateChanged(Call call, int oldState, int 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);
Ihab Awad6fb37c82014-08-07 19:48:57 -070056 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070057 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070058 void onIsConferencedChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070059 void onAudioModeIsVoipChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070060 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070061 }
62
Santos Cordon8e8b8d22013-12-19 14:14:05 -080063 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080064
Santos Cordon8e8b8d22013-12-19 14:14:05 -080065 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070066 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
67 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080068 */
Santos Cordon626697a2014-07-10 22:36:37 -070069 private final Set<Call> mCalls = new CopyOnWriteArraySet<Call>();
Santos Cordon681663d2014-01-30 04:32:15 -080070
Sailesh Nepal664837f2014-07-14 16:31:51 -070071 private final ConnectionServiceRepository mConnectionServiceRepository =
72 new ConnectionServiceRepository();
Santos Cordonf3671a62014-05-29 21:51:53 -070073 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
74 private final InCallController mInCallController = new InCallController();
75 private final CallAudioManager mCallAudioManager;
76 private final Ringer mRinger;
77 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070078 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070079 private final WiredHeadsetManager mWiredHeadsetManager;
80 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -070081 private final ProximitySensorManager mProximitySensorManager;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070082
83 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070084 * The call the user is currently interacting with. This is the call that should have audio
85 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080086 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070087 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080088
Santos Cordon766d04f2014-05-06 10:28:25 -070089 /** Singleton accessor. */
90 static CallsManager getInstance() {
91 return INSTANCE;
92 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080093
Santos Cordon8e8b8d22013-12-19 14:14:05 -080094 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080095 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080096 */
97 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -070098 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -070099
Santos Cordondeb8c892014-05-30 01:38:03 -0700100 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700101 mWiredHeadsetManager = new WiredHeadsetManager(app);
102 mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700103 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700104 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700105 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700106 mTtyManager = new TtyManager(app, mWiredHeadsetManager);
Yorke Leed1346872014-07-28 14:38:45 -0700107 mProximitySensorManager = new ProximitySensorManager(app);
Santos Cordonae193062014-05-21 21:21:49 -0700108
Santos Cordondeb8c892014-05-30 01:38:03 -0700109 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700110 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700111 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700112 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700113 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700114 mListeners.add(new RingbackPlayer(this, playerFactory));
115 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700116 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700117 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700118 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700119 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700120 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700121 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800122 }
123
Santos Cordon766d04f2014-05-06 10:28:25 -0700124 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700125 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700126 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700127
Tyler Gunncde2e502014-08-12 14:35:58 -0700128 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700129 if (!mCalls.contains(call)) {
130 // Call was not added previously in startOutgoingCall due to it being a potential MMI
131 // code, so add it now.
132 addCall(call);
133 }
134
135 // The call's ConnectionService has been updated.
136 for (CallsManagerListener listener : mListeners) {
137 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700138 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700139
140 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700141 }
142
143 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700144 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
145 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700146
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700147 // TODO: Replace disconnect cause with more specific disconnect causes.
148 markCallAsDisconnected(call, errorCode, errorMsg);
149 }
150
151 @Override
152 public void onCancelledOutgoingCall(Call call) {
153 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
154 setCallState(call, CallState.ABORTED);
155 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700156 }
157
158 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700159 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700160 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700161 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700162 addCall(call);
163 }
164
165 @Override
166 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700167 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700168 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800169 }
170
Ihab Awadcb387ac2014-05-28 16:49:38 -0700171 @Override
172 public void onRequestingRingback(Call call, boolean ringback) {
173 for (CallsManagerListener listener : mListeners) {
174 listener.onRequestingRingback(call, ringback);
175 }
176 }
177
Evan Charlton352105c2014-06-03 14:10:54 -0700178 @Override
179 public void onPostDialWait(Call call, String remaining) {
180 mInCallController.onPostDialWait(call, remaining);
181 }
182
Santos Cordona1610702014-06-04 20:22:56 -0700183 @Override
184 public void onExpiredConferenceCall(Call call) {
185 call.removeListener(this);
186 }
187
188 @Override
189 public void onConfirmedConferenceCall(Call call) {
190 addCall(call);
191 Log.v(this, "confirming Conf call %s", call);
192 for (CallsManagerListener listener : mListeners) {
193 listener.onIsConferencedChanged(call);
194 }
195 }
196
197 @Override
198 public void onParentChanged(Call call) {
199 for (CallsManagerListener listener : mListeners) {
200 listener.onIsConferencedChanged(call);
201 }
202 }
203
204 @Override
205 public void onChildrenChanged(Call call) {
206 for (CallsManagerListener listener : mListeners) {
207 listener.onIsConferencedChanged(call);
208 }
209 }
210
Ihab Awadff7493a2014-06-10 13:47:44 -0700211 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700212 public void onAudioModeIsVoipChanged(Call call) {
213 for (CallsManagerListener listener : mListeners) {
214 listener.onAudioModeIsVoipChanged(call);
215 }
216 }
217
Andrew Lee4a796602014-07-11 17:23:03 -0700218 @Override
219 public void onVideoStateChanged(Call call) {
220 for (CallsManagerListener listener : mListeners) {
221 listener.onVideoStateChanged(call);
222 }
223 }
224
Sailesh Nepal810735e2014-03-18 18:15:46 -0700225 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700226 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700227 }
228
229 Call getForegroundCall() {
230 return mForegroundCall;
231 }
232
Santos Cordonae193062014-05-21 21:21:49 -0700233 Ringer getRinger() {
234 return mRinger;
235 }
236
Santos Cordonf3671a62014-05-29 21:51:53 -0700237 InCallController getInCallController() {
238 return mInCallController;
239 }
240
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700241 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700242 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700243 if (call.isEmergencyCall()) {
244 return true;
245 }
246 }
247 return false;
248 }
249
Ihab Awad6fb37c82014-08-07 19:48:57 -0700250 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700251 return mCallAudioManager.getAudioState();
252 }
253
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700254 boolean isTtySupported() {
255 return mTtyManager.isTtySupported();
256 }
257
258 int getCurrentTtyMode() {
259 return mTtyManager.getCurrentTtyMode();
260 }
261
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800262 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700263 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800264 *
Evan Charlton89176372014-07-19 18:23:09 -0700265 * @param phoneAccountHandle The phone account which contains the component name of the connection
Nancy Chen0d3076c2014-07-30 14:45:44 -0700266 * service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800267 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800268 */
Evan Charlton89176372014-07-19 18:23:09 -0700269 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800270 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700271 // Create a call with no handle. The handle is eventually set when the call is attached
272 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700273 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700274 mConnectionServiceRepository,
275 null /* handle */,
276 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700277 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700278 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700279 true /* isIncoming */,
280 false /* isConference */);
281
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700282 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700283 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700284 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700285 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800286 }
287
Nancy Chen0d3076c2014-07-30 14:45:44 -0700288
289 /**
290 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
291 *
292 * NOTE: emergency calls will never pass through this because they call
293 * placeOutgoingCall directly.
294 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700295 * @param handle Handle to connect the call with.
296 * @param phoneAccountHandle The phone account which contains the component name of the connection
297 * service to use for this call.
298 * @param extras The optional extras Bundle passed with the intent used for the outgoing call.
299 *
Nancy Chen0d3076c2014-07-30 14:45:44 -0700300 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700301 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700302 // We only allow a single outgoing call at any given time. Before placing a call, make sure
303 // there doesn't already exist another outgoing call.
304 Call call = getFirstCallWithState(CallState.NEW, CallState.DIALING);
305
306 if (call != null) {
307 Log.i(this, "Canceling simultaneous outgoing call.");
308 return null;
309 }
310
311 // Create a call with original handle. The handle may be changed when the call is attached
312 // to a connection service, but in most cases will remain the same.
313 call = new Call(
314 mConnectionServiceRepository,
315 handle,
316 null /* gatewayInfo */,
317 null /* connectionManagerPhoneAccount */,
318 phoneAccountHandle,
319 false /* isIncoming */,
320 false /* isConference */);
Nancy Chena9d91da2014-08-12 14:31:06 -0700321 call.setExtras(extras);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700322 call.setState(CallState.CONNECTING);
323
Yorke Leeb701f6d2014-08-12 14:04:19 -0700324 if (!isPotentialMMICode(handle)) {
325 addCall(call);
326 } else {
327 call.addListener(this);
328 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700329
330 return call;
331 }
332
Ben Gilada0d9f752014-02-26 11:49:03 -0800333 /**
Yorke Lee33501632014-03-17 19:24:12 -0700334 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800335 *
Yorke Lee33501632014-03-17 19:24:12 -0700336 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700337 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700338 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700339 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700340 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800341 */
Nancy Chen0d3076c2014-07-30 14:45:44 -0700342 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo,
343 PhoneAccountHandle accountHandle, boolean speakerphoneOn, int videoState) {
344 if (call == null) {
345 // don't do anything if the call no longer exists
346 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700347 return;
348 }
349
Santos Cordon7cdb9092014-07-24 21:33:33 -0700350 TelecommApp app = TelecommApp.getInstance();
Yorke Lee33501632014-03-17 19:24:12 -0700351 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
352
353 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700354 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700355 } else {
356 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
357 Log.pii(uriHandle), Log.pii(handle));
358 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700359
Nancy Chen0d3076c2014-07-30 14:45:44 -0700360 //TODO: phone account is already set in {@link #startOutgoingCall}, refactor so this is
361 // not redundant.
362 //
Santos Cordon7cdb9092014-07-24 21:33:33 -0700363 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
364 // as if a phoneAccount was not specified (does the default behavior instead).
365 if (accountHandle != null) {
366 List<PhoneAccountHandle> enabledAccounts =
Ihab Awad6fb37c82014-08-07 19:48:57 -0700367 app.getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Santos Cordon7cdb9092014-07-24 21:33:33 -0700368 if (!enabledAccounts.contains(accountHandle)) {
369 accountHandle = null;
370 }
371 }
372
Nancy Chen0d3076c2014-07-30 14:45:44 -0700373 call.setHandle(uriHandle);
374 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700375 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700376 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700377
Nancy Chend9de92c2014-07-21 16:09:13 -0700378 // This block of code will attempt to pre-determine a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700379 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
Nancy Chend9de92c2014-07-21 16:09:13 -0700380 if (emergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700381 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700382 call.setTargetPhoneAccount(null);
Nancy Chend9de92c2014-07-21 16:09:13 -0700383 } else if (accountHandle != null) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700384 // NOTE: this is currently not an option because no feature currently exists to
385 // preset a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700386 Log.d(this, "CALL with phone account: " + accountHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -0700387 call.setTargetPhoneAccount(accountHandle);
Nancy Chend9de92c2014-07-21 16:09:13 -0700388 } else {
389 // No preset account, check if default exists
Santos Cordon7cdb9092014-07-24 21:33:33 -0700390 PhoneAccountHandle defaultAccountHandle =
391 app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
Evan Charlton89176372014-07-19 18:23:09 -0700392 if (defaultAccountHandle != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700393 call.setTargetPhoneAccount(defaultAccountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700394 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700395 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700396
Ihab Awadb78b2762014-07-25 15:16:23 -0700397 if (call.getTargetPhoneAccount() != null || emergencyCall) {
Nancy Chend9de92c2014-07-21 16:09:13 -0700398 // If the account is selected, proceed to place the outgoing call
399 call.startCreateConnection();
400 } else {
401 // This is the state where the user is expected to select an account
402 call.setState(CallState.PRE_DIAL_WAIT);
403 }
Yorke Leef98fb572014-03-05 10:56:55 -0800404 }
405
406 /**
Santos Cordona1610702014-06-04 20:22:56 -0700407 * Attempts to start a conference call for the specified call.
408 *
Santos Cordon12d61822014-07-29 16:02:20 -0700409 * @param call The call to conference.
410 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700411 */
Santos Cordon12d61822014-07-29 16:02:20 -0700412 void conference(Call call, Call otherCall) {
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700413 Call conferenceCall = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700414 mConnectionServiceRepository,
415 null /* handle */,
416 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700417 null /* connectionManagerPhoneAccount */,
418 null /* targetPhoneAccount */,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700419 false /* isIncoming */,
420 true /* isConference */);
Santos Cordona1610702014-06-04 20:22:56 -0700421 conferenceCall.addListener(this);
422 call.conferenceInto(conferenceCall);
423 }
424
425 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800426 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
427 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
428 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700429 *
430 * @param call The call to answer.
431 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800432 */
Andrew Lee38931d02014-07-16 10:17:36 -0700433 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700434 if (!mCalls.contains(call)) {
435 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800436 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700437 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700438 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700439 if (mForegroundCall != null && mForegroundCall != call &&
440 (mForegroundCall.isActive() ||
441 mForegroundCall.getState() == CallState.DIALING)) {
442 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
443 mForegroundCall, call);
444 mForegroundCall.hold();
Santos Cordondf399862014-08-06 04:39:15 -0700445 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700446 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700447 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700448 }
449
Santos Cordona56f2762014-03-24 15:55:53 -0700450 for (CallsManagerListener listener : mListeners) {
451 listener.onIncomingCallAnswered(call);
452 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800453
Santos Cordon61d0f702014-02-19 02:52:23 -0800454 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700455 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700456 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800457 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800458 }
459
460 /**
461 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
462 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
463 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800464 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700465 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700466 if (!mCalls.contains(call)) {
467 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800468 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700469 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700470 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700471 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700472 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800473 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800474 }
475
476 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700477 * Instructs Telecomm to play the specified DTMF tone within the specified call.
478 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700479 * @param digit The DTMF digit to play.
480 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700481 void playDtmfTone(Call call, char digit) {
482 if (!mCalls.contains(call)) {
483 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700484 } else {
485 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700486 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700487 }
488 }
489
490 /**
491 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700492 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700493 void stopDtmfTone(Call call) {
494 if (!mCalls.contains(call)) {
495 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700496 } else {
497 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700498 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700499 }
500 }
501
502 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700503 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700504 */
Evan Charlton352105c2014-06-03 14:10:54 -0700505 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700506 if (!mCalls.contains(call)) {
507 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700508 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700509 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700510 }
511 }
512
513 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800514 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
515 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
516 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800517 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700518 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700519 Log.v(this, "disconnectCall %s", call);
520
Sailesh Nepale59bb192014-04-01 18:33:59 -0700521 if (!mCalls.contains(call)) {
522 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800523 } else {
524 call.disconnect();
525 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800526 }
Santos Cordon681663d2014-01-30 04:32:15 -0800527
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700528 /**
529 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
530 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
531 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700532 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700533 void holdCall(Call call) {
534 if (!mCalls.contains(call)) {
535 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700536 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700537 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700538 call.hold();
539 }
540 }
541
542 /**
543 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
544 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
545 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700546 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700547 void unholdCall(Call call) {
548 if (!mCalls.contains(call)) {
549 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700550 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700551 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700552 for (Call c : mCalls) {
553 if (c != null && c.isAlive() && c != call) {
554 c.hold();
555 }
556 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700557 call.unhold();
558 }
559 }
560
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700561 /** Called by the in-call UI to change the mute state. */
562 void mute(boolean shouldMute) {
563 mCallAudioManager.mute(shouldMute);
564 }
565
566 /**
567 * Called by the in-call UI to change the audio route, for example to change from earpiece to
568 * speaker phone.
569 */
570 void setAudioRoute(int route) {
571 mCallAudioManager.setAudioRoute(route);
572 }
573
Yorke Leed1346872014-07-28 14:38:45 -0700574 /** Called by the in-call UI to turn the proximity sensor on. */
575 void turnOnProximitySensor() {
576 mProximitySensorManager.turnOn();
577 }
578
579 /**
580 * Called by the in-call UI to turn the proximity sensor off.
581 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
582 * the screen will be kept off until the proximity sensor goes negative.
583 */
584 void turnOffProximitySensor(boolean screenOnImmediately) {
585 mProximitySensorManager.turnOff(screenOnImmediately);
586 }
587
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700588 void phoneAccountClicked(Call call) {
589 if (!mCalls.contains(call)) {
590 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
591 } else {
592 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700593 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700594 }
595
Evan Charlton89176372014-07-19 18:23:09 -0700596 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700597 if (!mCalls.contains(call)) {
598 Log.i(this, "Attemped to add account to unknown call %s", call);
599 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700600 call.setTargetPhoneAccount(account);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700601 call.startCreateConnection();
602 }
603 }
604
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700605 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700606 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700607 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700608 for (CallsManagerListener listener : mListeners) {
609 listener.onAudioStateChanged(oldAudioState, newAudioState);
610 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700611 }
612
Sailesh Nepale59bb192014-04-01 18:33:59 -0700613 void markCallAsRinging(Call call) {
614 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800615 }
616
Sailesh Nepale59bb192014-04-01 18:33:59 -0700617 void markCallAsDialing(Call call) {
618 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800619 }
620
Sailesh Nepale59bb192014-04-01 18:33:59 -0700621 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700622 if (call.getConnectTimeMillis() == 0) {
623 call.setConnectTimeMillis(System.currentTimeMillis());
624 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700625 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700626
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700627 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700628 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700629 }
Santos Cordon681663d2014-01-30 04:32:15 -0800630 }
631
Sailesh Nepale59bb192014-04-01 18:33:59 -0700632 void markCallAsOnHold(Call call) {
633 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700634 }
635
Santos Cordon049b7b62014-01-30 05:34:26 -0800636 /**
Ihab Awad6fb37c82014-08-07 19:48:57 -0700637 * 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 -0800638 * live call, then also disconnect from the in-call controller.
639 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700640 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700641 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800642 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700643 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
644 call.setDisconnectCause(disconnectCause, disconnectMessage);
645 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700646 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700647 }
648
Ben Gilada0d9f752014-02-26 11:49:03 -0800649 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700650 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700651 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700652 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700653 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700654 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700655 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700656 if (service != null) {
657 for (Call call : ImmutableList.copyOf(mCalls)) {
658 if (call.getConnectionService() == service) {
659 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
660 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700661 }
662 }
663 }
664
Santos Cordondeb8c892014-05-30 01:38:03 -0700665 boolean hasAnyCalls() {
666 return !mCalls.isEmpty();
667 }
668
Santos Cordonc7e85d42014-05-22 02:51:48 -0700669 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700670 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700671 }
672
673 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700674 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700675 }
676
Santos Cordondeb8c892014-05-30 01:38:03 -0700677 boolean onMediaButton(int type) {
678 if (hasAnyCalls()) {
679 if (HeadsetMediaButton.SHORT_PRESS == type) {
680 Call ringingCall = getFirstCallWithState(CallState.RINGING);
681 if (ringingCall == null) {
682 mCallAudioManager.toggleMute();
683 return true;
684 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700685 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700686 return true;
687 }
688 } else if (HeadsetMediaButton.LONG_PRESS == type) {
689 Log.d(this, "handleHeadsetHook: longpress -> hangup");
690 Call callToHangup = getFirstCallWithState(
691 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
692 if (callToHangup != null) {
693 callToHangup.disconnect();
694 return true;
695 }
696 }
697 }
698 return false;
699 }
700
701 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700702 * Checks to see if the specified call is the only high-level call and if so, enable the
703 * "Add-call" button. We allow you to add a second call but not a third or beyond.
704 *
705 * @param call The call to test for add-call.
706 * @return Whether the add-call feature should be enabled for the call.
707 */
708 protected boolean isAddCallCapable(Call call) {
709 if (call.getParentCall() != null) {
710 // Never true for child calls.
711 return false;
712 }
713
714 // Loop through all the other calls and there exists a top level (has no parent) call
715 // that is not the specified call, return false.
716 for (Call otherCall : mCalls) {
717 if (call != otherCall && otherCall.getParentCall() == null) {
718 return false;
719 }
720 }
721 return true;
722 }
723
724 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700725 * Returns the first call that it finds with the given states. The states are treated as having
726 * priority order so that any call with the first state will be returned before any call with
727 * states listed later in the parameter list.
728 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700729 Call getFirstCallWithState(int... states) {
730 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700731 // check the foreground first
732 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
733 return mForegroundCall;
734 }
735
Santos Cordondeb8c892014-05-30 01:38:03 -0700736 for (Call call : mCalls) {
737 if (currentState == call.getState()) {
738 return call;
739 }
740 }
741 }
742 return null;
743 }
744
Santos Cordon4b2c1192014-03-19 18:15:38 -0700745 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800746 * Adds the specified call to the main list of live calls.
747 *
748 * @param call The call to add.
749 */
750 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700751 Log.v(this, "addCall(%s)", call);
752
753 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700754 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700755
Santos Cordondf399862014-08-06 04:39:15 -0700756 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700757 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700758 for (CallsManagerListener listener : mListeners) {
759 listener.onCallAdded(call);
760 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700761 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800762 }
763
Sailesh Nepal810735e2014-03-18 18:15:46 -0700764 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700765 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700766
Santos Cordon766d04f2014-05-06 10:28:25 -0700767 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700768 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700769
770 boolean shouldNotify = false;
771 if (mCalls.contains(call)) {
772 mCalls.remove(call);
773 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700774 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800775
Sailesh Nepale59bb192014-04-01 18:33:59 -0700776 // Only broadcast changes for calls that are being tracked.
777 if (shouldNotify) {
778 for (CallsManagerListener listener : mListeners) {
779 listener.onCallRemoved(call);
780 }
781 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700782 }
783 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800784
Sailesh Nepal810735e2014-03-18 18:15:46 -0700785 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700786 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700787 *
788 * @param call The call.
789 * @param newState The new state of the call.
790 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700791 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700792 if (call == null) {
793 return;
794 }
Ihab Awad6fb37c82014-08-07 19:48:57 -0700795 int oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700796 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700797 if (newState != oldState) {
798 // Unfortunately, in the telephony world the radio is king. So if the call notifies
799 // 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 -0700800 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700801 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700802 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700803 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700804 // unexpected transition occurs.
805 call.setState(newState);
806
807 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700808 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700809 for (CallsManagerListener listener : mListeners) {
810 listener.onCallStateChanged(call, oldState, newState);
811 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700812 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800813 }
814 }
815 }
816
817 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700818 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800819 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700820 private void updateForegroundCall() {
821 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700822 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700823 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700824 // of its state will be foreground by default and instead the connection service should
825 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700826 // the call should play audio and listen to microphone if it wants.
827
828 // Active calls have priority.
829 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700830 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800831 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700832 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700833
834 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700835 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700836 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800837 }
838 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800839
Sailesh Nepal810735e2014-03-18 18:15:46 -0700840 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700841 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700842 Call oldForegroundCall = mForegroundCall;
843 mForegroundCall = newForegroundCall;
844
Santos Cordona56f2762014-03-24 15:55:53 -0700845 for (CallsManagerListener listener : mListeners) {
846 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
847 }
Santos Cordon681663d2014-01-30 04:32:15 -0800848 }
849 }
Yorke Leeb701f6d2014-08-12 14:04:19 -0700850
851 private boolean isPotentialMMICode(Uri handle) {
852 return (handle != null && handle.getSchemeSpecificPart() != null
853 && handle.getSchemeSpecificPart().contains("#"));
854 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800855}