blob: 89357665ee35bf48a8ce266eded29bcf942a925a [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;
Santos Cordon0fbe6322014-08-14 04:04:25 -070024import android.telecomm.ParcelableConference;
Evan Charlton89176372014-07-19 18:23:09 -070025import android.telecomm.PhoneAccountHandle;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
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
Jay Shraunera82c8f72014-08-14 15:49:16 -070031import java.util.Collections;
Santos Cordon7cdb9092014-07-24 21:33:33 -070032import java.util.List;
Santos Cordona56f2762014-03-24 15:55:53 -070033import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070034import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070039 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080040 * 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.
Jay Shraunera82c8f72014-08-14 15:49:16 -070069 *
70 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
71 * load factor before resizing, 1 means we only expect a single thread to
72 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -080073 */
Jay Shraunera82c8f72014-08-14 15:49:16 -070074 private final Set<Call> mCalls = Collections.newSetFromMap(
75 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -080076
Sailesh Nepal664837f2014-07-14 16:31:51 -070077 private final ConnectionServiceRepository mConnectionServiceRepository =
78 new ConnectionServiceRepository();
Santos Cordonf3671a62014-05-29 21:51:53 -070079 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
80 private final InCallController mInCallController = new InCallController();
81 private final CallAudioManager mCallAudioManager;
82 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -070083 // For this set initial table size to 16 because we add 13 listeners in
84 // the CallsManager constructor.
85 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
86 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -070087 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070088 private final WiredHeadsetManager mWiredHeadsetManager;
89 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -070090 private final ProximitySensorManager mProximitySensorManager;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070091
92 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070093 * The call the user is currently interacting with. This is the call that should have audio
94 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080095 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070096 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080097
Santos Cordon766d04f2014-05-06 10:28:25 -070098 /** Singleton accessor. */
99 static CallsManager getInstance() {
100 return INSTANCE;
101 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800102
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800103 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800104 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800105 */
106 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700107 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700108
Santos Cordondeb8c892014-05-30 01:38:03 -0700109 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700110 mWiredHeadsetManager = new WiredHeadsetManager(app);
111 mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700112 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700113 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700114 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700115 mTtyManager = new TtyManager(app, mWiredHeadsetManager);
Yorke Leed1346872014-07-28 14:38:45 -0700116 mProximitySensorManager = new ProximitySensorManager(app);
Santos Cordonae193062014-05-21 21:21:49 -0700117
Santos Cordondeb8c892014-05-30 01:38:03 -0700118 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700119 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700120 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700121 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700122 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700123 mListeners.add(new RingbackPlayer(this, playerFactory));
124 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700125 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700126 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700127 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700128 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700129 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700130 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800131 }
132
Santos Cordon766d04f2014-05-06 10:28:25 -0700133 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700134 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700135 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700136
Tyler Gunncde2e502014-08-12 14:35:58 -0700137 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700138 if (!mCalls.contains(call)) {
139 // Call was not added previously in startOutgoingCall due to it being a potential MMI
140 // code, so add it now.
141 addCall(call);
142 }
143
144 // The call's ConnectionService has been updated.
145 for (CallsManagerListener listener : mListeners) {
146 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700147 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700148
149 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700150 }
151
152 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700153 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
154 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700155
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700156 // TODO: Replace disconnect cause with more specific disconnect causes.
157 markCallAsDisconnected(call, errorCode, errorMsg);
158 }
159
160 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700161 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700162 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700163 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700164 addCall(call);
165 }
166
167 @Override
168 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700169 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700170 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800171 }
172
Ihab Awadcb387ac2014-05-28 16:49:38 -0700173 @Override
174 public void onRequestingRingback(Call call, boolean ringback) {
175 for (CallsManagerListener listener : mListeners) {
176 listener.onRequestingRingback(call, ringback);
177 }
178 }
179
Evan Charlton352105c2014-06-03 14:10:54 -0700180 @Override
181 public void onPostDialWait(Call call, String remaining) {
182 mInCallController.onPostDialWait(call, remaining);
183 }
184
Santos Cordona1610702014-06-04 20:22:56 -0700185 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700186 public void onParentChanged(Call call) {
187 for (CallsManagerListener listener : mListeners) {
188 listener.onIsConferencedChanged(call);
189 }
190 }
191
192 @Override
193 public void onChildrenChanged(Call call) {
194 for (CallsManagerListener listener : mListeners) {
195 listener.onIsConferencedChanged(call);
196 }
197 }
198
Ihab Awadff7493a2014-06-10 13:47:44 -0700199 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700200 public void onAudioModeIsVoipChanged(Call call) {
201 for (CallsManagerListener listener : mListeners) {
202 listener.onAudioModeIsVoipChanged(call);
203 }
204 }
205
Andrew Lee4a796602014-07-11 17:23:03 -0700206 @Override
207 public void onVideoStateChanged(Call call) {
208 for (CallsManagerListener listener : mListeners) {
209 listener.onVideoStateChanged(call);
210 }
211 }
212
Sailesh Nepal810735e2014-03-18 18:15:46 -0700213 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700214 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700215 }
216
217 Call getForegroundCall() {
218 return mForegroundCall;
219 }
220
Santos Cordonae193062014-05-21 21:21:49 -0700221 Ringer getRinger() {
222 return mRinger;
223 }
224
Santos Cordonf3671a62014-05-29 21:51:53 -0700225 InCallController getInCallController() {
226 return mInCallController;
227 }
228
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700229 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700230 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700231 if (call.isEmergencyCall()) {
232 return true;
233 }
234 }
235 return false;
236 }
237
Ihab Awad6fb37c82014-08-07 19:48:57 -0700238 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700239 return mCallAudioManager.getAudioState();
240 }
241
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700242 boolean isTtySupported() {
243 return mTtyManager.isTtySupported();
244 }
245
246 int getCurrentTtyMode() {
247 return mTtyManager.getCurrentTtyMode();
248 }
249
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800250 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700251 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800252 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700253 * @param phoneAccountHandle The phone account which contains the component name of the
254 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800255 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800256 */
Evan Charlton89176372014-07-19 18:23:09 -0700257 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800258 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700259 // Create a call with no handle. The handle is eventually set when the call is attached
260 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700261 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700262 mConnectionServiceRepository,
263 null /* handle */,
264 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700265 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700266 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700267 true /* isIncoming */,
268 false /* isConference */);
269
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700270 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700271 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700272 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700273 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800274 }
275
Nancy Chen0d3076c2014-07-30 14:45:44 -0700276 /**
277 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
278 *
279 * NOTE: emergency calls will never pass through this because they call
280 * placeOutgoingCall directly.
281 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700282 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700283 * @param phoneAccountHandle The phone account which contains the component name of the
284 * connection service to use for this call.
285 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700286 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700287 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700288 // We only allow a single outgoing call at any given time. Before placing a call, make sure
289 // there doesn't already exist another outgoing call.
290 Call call = getFirstCallWithState(CallState.NEW, CallState.DIALING);
291
292 if (call != null) {
293 Log.i(this, "Canceling simultaneous outgoing call.");
294 return null;
295 }
296
Nancy Chenbc9ef172014-08-15 17:11:57 -0700297 TelecommApp app = TelecommApp.getInstance();
298
299 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
300 // as if a phoneAccount was not specified (does the default behavior instead).
301 if (phoneAccountHandle != null) {
302 List<PhoneAccountHandle> enabledAccounts =
303 app.getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
304 if (!enabledAccounts.contains(phoneAccountHandle)) {
305 phoneAccountHandle = null;
306 }
307 }
308
309 if (phoneAccountHandle == null) {
310 // No preset account, check if default exists
311 PhoneAccountHandle defaultAccountHandle =
312 app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
313 if (defaultAccountHandle != null) {
314 phoneAccountHandle = defaultAccountHandle;
315 }
316 }
317
Nancy Chen0d3076c2014-07-30 14:45:44 -0700318 // Create a call with original handle. The handle may be changed when the call is attached
319 // to a connection service, but in most cases will remain the same.
320 call = new Call(
321 mConnectionServiceRepository,
322 handle,
323 null /* gatewayInfo */,
324 null /* connectionManagerPhoneAccount */,
325 phoneAccountHandle,
326 false /* isIncoming */,
327 false /* isConference */);
Nancy Chena9d91da2014-08-12 14:31:06 -0700328 call.setExtras(extras);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700329
330 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
331 if (phoneAccountHandle == null && !emergencyCall) {
332 // This is the state where the user is expected to select an account
333 call.setState(CallState.PRE_DIAL_WAIT);
334 } else {
335 call.setState(CallState.CONNECTING);
336 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700337
Yorke Leeb701f6d2014-08-12 14:04:19 -0700338 if (!isPotentialMMICode(handle)) {
339 addCall(call);
340 } else {
341 call.addListener(this);
342 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700343
344 return call;
345 }
346
Ben Gilada0d9f752014-02-26 11:49:03 -0800347 /**
Yorke Lee33501632014-03-17 19:24:12 -0700348 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800349 *
Yorke Lee33501632014-03-17 19:24:12 -0700350 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700351 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700352 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700353 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700354 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800355 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700356 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
357 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700358 if (call == null) {
359 // don't do anything if the call no longer exists
360 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700361 return;
362 }
363
Yorke Lee33501632014-03-17 19:24:12 -0700364 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
365
366 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700367 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700368 } else {
369 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
370 Log.pii(uriHandle), Log.pii(handle));
371 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700372
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 Chenbc9ef172014-08-15 17:11:57 -0700378 TelecommApp app = TelecommApp.getInstance();
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 Chen53ceedc2014-07-08 18:56:51 -0700383 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700384
Ihab Awadb78b2762014-07-25 15:16:23 -0700385 if (call.getTargetPhoneAccount() != null || emergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700386 // If the account has been set, proceed to place the outgoing call.
387 // Otherwise the connection will be initiated when the account is set by the user.
Nancy Chend9de92c2014-07-21 16:09:13 -0700388 call.startCreateConnection();
Nancy Chend9de92c2014-07-21 16:09:13 -0700389 }
Yorke Leef98fb572014-03-05 10:56:55 -0800390 }
391
392 /**
Santos Cordona1610702014-06-04 20:22:56 -0700393 * Attempts to start a conference call for the specified call.
394 *
Santos Cordon12d61822014-07-29 16:02:20 -0700395 * @param call The call to conference.
396 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700397 */
Santos Cordon12d61822014-07-29 16:02:20 -0700398 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700399 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700400 }
401
402 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800403 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
404 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
405 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700406 *
407 * @param call The call to answer.
408 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800409 */
Andrew Lee38931d02014-07-16 10:17:36 -0700410 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700411 if (!mCalls.contains(call)) {
412 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800413 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700414 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700415 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700416 if (mForegroundCall != null && mForegroundCall != call &&
417 (mForegroundCall.isActive() ||
418 mForegroundCall.getState() == CallState.DIALING)) {
419 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
420 mForegroundCall, call);
421 mForegroundCall.hold();
Santos Cordondf399862014-08-06 04:39:15 -0700422 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700423 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700424 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700425 }
426
Santos Cordona56f2762014-03-24 15:55:53 -0700427 for (CallsManagerListener listener : mListeners) {
428 listener.onIncomingCallAnswered(call);
429 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800430
Santos Cordon61d0f702014-02-19 02:52:23 -0800431 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700432 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700433 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800434 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800435 }
436
437 /**
438 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
439 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
440 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800441 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700442 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700443 if (!mCalls.contains(call)) {
444 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800445 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700446 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700447 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700448 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700449 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800450 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800451 }
452
453 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700454 * Instructs Telecomm to play the specified DTMF tone within the specified call.
455 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700456 * @param digit The DTMF digit to play.
457 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700458 void playDtmfTone(Call call, char digit) {
459 if (!mCalls.contains(call)) {
460 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700461 } else {
462 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700463 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700464 }
465 }
466
467 /**
468 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700469 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700470 void stopDtmfTone(Call call) {
471 if (!mCalls.contains(call)) {
472 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700473 } else {
474 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700475 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700476 }
477 }
478
479 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700480 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700481 */
Evan Charlton352105c2014-06-03 14:10:54 -0700482 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700483 if (!mCalls.contains(call)) {
484 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700485 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700486 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700487 }
488 }
489
490 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800491 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
492 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
493 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800494 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700495 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700496 Log.v(this, "disconnectCall %s", call);
497
Sailesh Nepale59bb192014-04-01 18:33:59 -0700498 if (!mCalls.contains(call)) {
499 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800500 } else {
501 call.disconnect();
502 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800503 }
Santos Cordon681663d2014-01-30 04:32:15 -0800504
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700505 /**
Tyler Gunn61b92102014-08-19 07:42:20 -0700506 * Instructs Telecomm to disconnect all calls.
507 */
508 void disconnectAllCalls() {
509 Log.v(this, "disconnectAllCalls");
510
511 for (Call call : mCalls) {
512 disconnectCall(call);
513 }
514 }
515
Nancy Chenbc9ef172014-08-15 17:11:57 -0700516
Tyler Gunn61b92102014-08-19 07:42:20 -0700517 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700518 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
519 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
520 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700521 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700522 void holdCall(Call call) {
523 if (!mCalls.contains(call)) {
524 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700525 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700526 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700527 call.hold();
528 }
529 }
530
531 /**
532 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
533 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
534 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700535 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700536 void unholdCall(Call call) {
537 if (!mCalls.contains(call)) {
538 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700539 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700540 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700541 for (Call c : mCalls) {
542 if (c != null && c.isAlive() && c != call) {
543 c.hold();
544 }
545 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700546 call.unhold();
547 }
548 }
549
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700550 /** Called by the in-call UI to change the mute state. */
551 void mute(boolean shouldMute) {
552 mCallAudioManager.mute(shouldMute);
553 }
554
555 /**
556 * Called by the in-call UI to change the audio route, for example to change from earpiece to
557 * speaker phone.
558 */
559 void setAudioRoute(int route) {
560 mCallAudioManager.setAudioRoute(route);
561 }
562
Yorke Leed1346872014-07-28 14:38:45 -0700563 /** Called by the in-call UI to turn the proximity sensor on. */
564 void turnOnProximitySensor() {
565 mProximitySensorManager.turnOn();
566 }
567
568 /**
569 * Called by the in-call UI to turn the proximity sensor off.
570 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
571 * the screen will be kept off until the proximity sensor goes negative.
572 */
573 void turnOffProximitySensor(boolean screenOnImmediately) {
574 mProximitySensorManager.turnOff(screenOnImmediately);
575 }
576
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700577 void phoneAccountClicked(Call call) {
578 if (!mCalls.contains(call)) {
579 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
580 } else {
581 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700582 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700583 }
584
Evan Charlton89176372014-07-19 18:23:09 -0700585 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700586 if (!mCalls.contains(call)) {
587 Log.i(this, "Attemped to add account to unknown call %s", call);
588 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700589 call.setTargetPhoneAccount(account);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700590 call.startCreateConnection();
591 }
592 }
593
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700594 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700595 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700596 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700597 for (CallsManagerListener listener : mListeners) {
598 listener.onAudioStateChanged(oldAudioState, newAudioState);
599 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700600 }
601
Sailesh Nepale59bb192014-04-01 18:33:59 -0700602 void markCallAsRinging(Call call) {
603 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800604 }
605
Sailesh Nepale59bb192014-04-01 18:33:59 -0700606 void markCallAsDialing(Call call) {
607 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800608 }
609
Sailesh Nepale59bb192014-04-01 18:33:59 -0700610 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700611 if (call.getConnectTimeMillis() == 0) {
612 call.setConnectTimeMillis(System.currentTimeMillis());
613 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700614 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700615
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700616 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700617 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700618 }
Santos Cordon681663d2014-01-30 04:32:15 -0800619 }
620
Sailesh Nepale59bb192014-04-01 18:33:59 -0700621 void markCallAsOnHold(Call call) {
622 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700623 }
624
Santos Cordon049b7b62014-01-30 05:34:26 -0800625 /**
Ihab Awad6fb37c82014-08-07 19:48:57 -0700626 * 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 -0800627 * live call, then also disconnect from the in-call controller.
628 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700629 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700630 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800631 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700632 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
633 call.setDisconnectCause(disconnectCause, disconnectMessage);
634 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700635 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700636 }
637
Ben Gilada0d9f752014-02-26 11:49:03 -0800638 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700639 * Removes an existing disconnected call, and notifies the in-call app.
640 */
641 void markCallAsRemoved(Call call) {
642 removeCall(call);
643 }
644
645 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700646 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700647 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700648 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700649 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700650 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700651 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700652 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700653 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700654 if (call.getConnectionService() == service) {
655 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
656 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700657 }
658 }
659 }
660
Santos Cordondeb8c892014-05-30 01:38:03 -0700661 boolean hasAnyCalls() {
662 return !mCalls.isEmpty();
663 }
664
Santos Cordonc7e85d42014-05-22 02:51:48 -0700665 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700666 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700667 }
668
669 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700670 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700671 }
672
Santos Cordondeb8c892014-05-30 01:38:03 -0700673 boolean onMediaButton(int type) {
674 if (hasAnyCalls()) {
675 if (HeadsetMediaButton.SHORT_PRESS == type) {
676 Call ringingCall = getFirstCallWithState(CallState.RINGING);
677 if (ringingCall == null) {
678 mCallAudioManager.toggleMute();
679 return true;
680 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700681 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700682 return true;
683 }
684 } else if (HeadsetMediaButton.LONG_PRESS == type) {
685 Log.d(this, "handleHeadsetHook: longpress -> hangup");
686 Call callToHangup = getFirstCallWithState(
687 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
688 if (callToHangup != null) {
689 callToHangup.disconnect();
690 return true;
691 }
692 }
693 }
694 return false;
695 }
696
697 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700698 * Checks to see if the specified call is the only high-level call and if so, enable the
699 * "Add-call" button. We allow you to add a second call but not a third or beyond.
700 *
701 * @param call The call to test for add-call.
702 * @return Whether the add-call feature should be enabled for the call.
703 */
704 protected boolean isAddCallCapable(Call call) {
705 if (call.getParentCall() != null) {
706 // Never true for child calls.
707 return false;
708 }
709
710 // Loop through all the other calls and there exists a top level (has no parent) call
711 // that is not the specified call, return false.
712 for (Call otherCall : mCalls) {
713 if (call != otherCall && otherCall.getParentCall() == null) {
714 return false;
715 }
716 }
717 return true;
718 }
719
720 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700721 * Returns the first call that it finds with the given states. The states are treated as having
722 * priority order so that any call with the first state will be returned before any call with
723 * states listed later in the parameter list.
724 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700725 Call getFirstCallWithState(int... states) {
726 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700727 // check the foreground first
728 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
729 return mForegroundCall;
730 }
731
Santos Cordondeb8c892014-05-30 01:38:03 -0700732 for (Call call : mCalls) {
733 if (currentState == call.getState()) {
734 return call;
735 }
736 }
737 }
738 return null;
739 }
740
Santos Cordon0fbe6322014-08-14 04:04:25 -0700741 Call createConferenceCall(
742 PhoneAccountHandle phoneAccount,
743 ParcelableConference parcelableConference) {
744 Call call = new Call(
745 mConnectionServiceRepository,
746 null /* handle */,
747 null /* gatewayInfo */,
748 null /* connectionManagerPhoneAccount */,
749 phoneAccount,
750 false /* isIncoming */,
751 true /* isConference */);
752
753 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
754 call.setCallCapabilities(parcelableConference.getCapabilities());
755
756 // TODO: Move this to be a part of addCall()
757 call.addListener(this);
758 addCall(call);
759 return call;
760 }
761
Nancy Chenbc9ef172014-08-15 17:11:57 -0700762
Santos Cordon4b2c1192014-03-19 18:15:38 -0700763 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800764 * Adds the specified call to the main list of live calls.
765 *
766 * @param call The call to add.
767 */
768 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700769 Log.v(this, "addCall(%s)", call);
770
771 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700772 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700773
Santos Cordondf399862014-08-06 04:39:15 -0700774 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700775 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700776 for (CallsManagerListener listener : mListeners) {
777 listener.onCallAdded(call);
778 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700779 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800780 }
781
Sailesh Nepal810735e2014-03-18 18:15:46 -0700782 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700783 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700784
Santos Cordon672321e2014-08-21 14:38:58 -0700785 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700786 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700787 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700788
789 boolean shouldNotify = false;
790 if (mCalls.contains(call)) {
791 mCalls.remove(call);
792 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700793 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800794
Sailesh Nepale59bb192014-04-01 18:33:59 -0700795 // Only broadcast changes for calls that are being tracked.
796 if (shouldNotify) {
797 for (CallsManagerListener listener : mListeners) {
798 listener.onCallRemoved(call);
799 }
800 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700801 }
802 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800803
Sailesh Nepal810735e2014-03-18 18:15:46 -0700804 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700805 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700806 *
807 * @param call The call.
808 * @param newState The new state of the call.
809 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700810 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700811 if (call == null) {
812 return;
813 }
Ihab Awad6fb37c82014-08-07 19:48:57 -0700814 int oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700815 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700816 if (newState != oldState) {
817 // Unfortunately, in the telephony world the radio is king. So if the call notifies
818 // 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 -0700819 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700820 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700821 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700822 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700823 // unexpected transition occurs.
824 call.setState(newState);
825
826 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700827 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700828 for (CallsManagerListener listener : mListeners) {
829 listener.onCallStateChanged(call, oldState, newState);
830 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700831 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800832 }
833 }
834 }
835
836 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700837 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800838 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700839 private void updateForegroundCall() {
840 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700841 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700842 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700843 // of its state will be foreground by default and instead the connection service should
844 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700845 // the call should play audio and listen to microphone if it wants.
846
847 // Active calls have priority.
848 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700849 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800850 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700851 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700852
853 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700854 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700855 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800856 }
857 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800858
Sailesh Nepal810735e2014-03-18 18:15:46 -0700859 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700860 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700861 Call oldForegroundCall = mForegroundCall;
862 mForegroundCall = newForegroundCall;
863
Santos Cordona56f2762014-03-24 15:55:53 -0700864 for (CallsManagerListener listener : mListeners) {
865 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
866 }
Santos Cordon681663d2014-01-30 04:32:15 -0800867 }
868 }
Yorke Leeb701f6d2014-08-12 14:04:19 -0700869
870 private boolean isPotentialMMICode(Uri handle) {
871 return (handle != null && handle.getSchemeSpecificPart() != null
872 && handle.getSchemeSpecificPart().contains("#"));
873 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800874}