blob: b4e892c1f99ea878a119078cf67c3ca9896acb2d [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
161 public void onCancelledOutgoingCall(Call call) {
162 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
Santos Cordon2d0b3312014-08-15 15:04:17 -0700163 markCallAsDisconnected(call, DisconnectCause.OUTGOING_CANCELED, null);
Santos Cordon766d04f2014-05-06 10:28:25 -0700164 }
165
166 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700167 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700168 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700169 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700170 addCall(call);
171 }
172
173 @Override
174 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700175 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700176 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800177 }
178
Ihab Awadcb387ac2014-05-28 16:49:38 -0700179 @Override
180 public void onRequestingRingback(Call call, boolean ringback) {
181 for (CallsManagerListener listener : mListeners) {
182 listener.onRequestingRingback(call, ringback);
183 }
184 }
185
Evan Charlton352105c2014-06-03 14:10:54 -0700186 @Override
187 public void onPostDialWait(Call call, String remaining) {
188 mInCallController.onPostDialWait(call, remaining);
189 }
190
Santos Cordona1610702014-06-04 20:22:56 -0700191 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700192 public void onParentChanged(Call call) {
193 for (CallsManagerListener listener : mListeners) {
194 listener.onIsConferencedChanged(call);
195 }
196 }
197
198 @Override
199 public void onChildrenChanged(Call call) {
200 for (CallsManagerListener listener : mListeners) {
201 listener.onIsConferencedChanged(call);
202 }
203 }
204
Ihab Awadff7493a2014-06-10 13:47:44 -0700205 @Override
Sailesh Nepal7e669572014-07-08 21:29:12 -0700206 public void onAudioModeIsVoipChanged(Call call) {
207 for (CallsManagerListener listener : mListeners) {
208 listener.onAudioModeIsVoipChanged(call);
209 }
210 }
211
Andrew Lee4a796602014-07-11 17:23:03 -0700212 @Override
213 public void onVideoStateChanged(Call call) {
214 for (CallsManagerListener listener : mListeners) {
215 listener.onVideoStateChanged(call);
216 }
217 }
218
Sailesh Nepal810735e2014-03-18 18:15:46 -0700219 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700220 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700221 }
222
223 Call getForegroundCall() {
224 return mForegroundCall;
225 }
226
Santos Cordonae193062014-05-21 21:21:49 -0700227 Ringer getRinger() {
228 return mRinger;
229 }
230
Santos Cordonf3671a62014-05-29 21:51:53 -0700231 InCallController getInCallController() {
232 return mInCallController;
233 }
234
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700235 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700236 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700237 if (call.isEmergencyCall()) {
238 return true;
239 }
240 }
241 return false;
242 }
243
Ihab Awad6fb37c82014-08-07 19:48:57 -0700244 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700245 return mCallAudioManager.getAudioState();
246 }
247
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700248 boolean isTtySupported() {
249 return mTtyManager.isTtySupported();
250 }
251
252 int getCurrentTtyMode() {
253 return mTtyManager.getCurrentTtyMode();
254 }
255
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800256 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700257 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800258 *
Evan Charlton89176372014-07-19 18:23:09 -0700259 * @param phoneAccountHandle The phone account which contains the component name of the connection
Nancy Chen0d3076c2014-07-30 14:45:44 -0700260 * service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800261 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800262 */
Evan Charlton89176372014-07-19 18:23:09 -0700263 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800264 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700265 // Create a call with no handle. The handle is eventually set when the call is attached
266 // to a connection service.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700267 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700268 mConnectionServiceRepository,
269 null /* handle */,
270 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700271 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700272 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700273 true /* isIncoming */,
274 false /* isConference */);
275
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700276 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700277 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700278 call.addListener(this);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700279 call.startCreateConnection();
Ben Gilada0d9f752014-02-26 11:49:03 -0800280 }
281
Nancy Chen0d3076c2014-07-30 14:45:44 -0700282
283 /**
284 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
285 *
286 * NOTE: emergency calls will never pass through this because they call
287 * placeOutgoingCall directly.
288 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700289 * @param handle Handle to connect the call with.
290 * @param phoneAccountHandle The phone account which contains the component name of the connection
291 * service to use for this call.
292 * @param extras The optional extras Bundle passed with the intent used for the outgoing call.
293 *
Nancy Chen0d3076c2014-07-30 14:45:44 -0700294 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700295 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700296 // We only allow a single outgoing call at any given time. Before placing a call, make sure
297 // there doesn't already exist another outgoing call.
298 Call call = getFirstCallWithState(CallState.NEW, CallState.DIALING);
299
300 if (call != null) {
301 Log.i(this, "Canceling simultaneous outgoing call.");
302 return null;
303 }
304
305 // Create a call with original handle. The handle may be changed when the call is attached
306 // to a connection service, but in most cases will remain the same.
307 call = new Call(
308 mConnectionServiceRepository,
309 handle,
310 null /* gatewayInfo */,
311 null /* connectionManagerPhoneAccount */,
312 phoneAccountHandle,
313 false /* isIncoming */,
314 false /* isConference */);
Nancy Chena9d91da2014-08-12 14:31:06 -0700315 call.setExtras(extras);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700316 call.setState(CallState.CONNECTING);
317
Yorke Leeb701f6d2014-08-12 14:04:19 -0700318 if (!isPotentialMMICode(handle)) {
319 addCall(call);
320 } else {
321 call.addListener(this);
322 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700323
324 return call;
325 }
326
Ben Gilada0d9f752014-02-26 11:49:03 -0800327 /**
Yorke Lee33501632014-03-17 19:24:12 -0700328 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800329 *
Yorke Lee33501632014-03-17 19:24:12 -0700330 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700331 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700332 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700333 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700334 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800335 */
Nancy Chen0d3076c2014-07-30 14:45:44 -0700336 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo,
337 PhoneAccountHandle accountHandle, boolean speakerphoneOn, int videoState) {
338 if (call == null) {
339 // don't do anything if the call no longer exists
340 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700341 return;
342 }
343
Santos Cordon7cdb9092014-07-24 21:33:33 -0700344 TelecommApp app = TelecommApp.getInstance();
Yorke Lee33501632014-03-17 19:24:12 -0700345 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
346
347 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700348 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700349 } else {
350 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
351 Log.pii(uriHandle), Log.pii(handle));
352 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700353
Nancy Chen0d3076c2014-07-30 14:45:44 -0700354 //TODO: phone account is already set in {@link #startOutgoingCall}, refactor so this is
355 // not redundant.
356 //
Santos Cordon7cdb9092014-07-24 21:33:33 -0700357 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
358 // as if a phoneAccount was not specified (does the default behavior instead).
359 if (accountHandle != null) {
360 List<PhoneAccountHandle> enabledAccounts =
Ihab Awad6fb37c82014-08-07 19:48:57 -0700361 app.getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Santos Cordon7cdb9092014-07-24 21:33:33 -0700362 if (!enabledAccounts.contains(accountHandle)) {
363 accountHandle = null;
364 }
365 }
366
Nancy Chen0d3076c2014-07-30 14:45:44 -0700367 call.setHandle(uriHandle);
368 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700369 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700370 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700371
Nancy Chend9de92c2014-07-21 16:09:13 -0700372 // This block of code will attempt to pre-determine a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700373 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
Nancy Chend9de92c2014-07-21 16:09:13 -0700374 if (emergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700375 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700376 call.setTargetPhoneAccount(null);
Nancy Chend9de92c2014-07-21 16:09:13 -0700377 } else if (accountHandle != null) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700378 // NOTE: this is currently not an option because no feature currently exists to
379 // preset a phone account
Santos Cordon7cdb9092014-07-24 21:33:33 -0700380 Log.d(this, "CALL with phone account: " + accountHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -0700381 call.setTargetPhoneAccount(accountHandle);
Nancy Chend9de92c2014-07-21 16:09:13 -0700382 } else {
383 // No preset account, check if default exists
Santos Cordon7cdb9092014-07-24 21:33:33 -0700384 PhoneAccountHandle defaultAccountHandle =
385 app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
Evan Charlton89176372014-07-19 18:23:09 -0700386 if (defaultAccountHandle != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700387 call.setTargetPhoneAccount(defaultAccountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700388 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700389 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700390
Ihab Awadb78b2762014-07-25 15:16:23 -0700391 if (call.getTargetPhoneAccount() != null || emergencyCall) {
Nancy Chend9de92c2014-07-21 16:09:13 -0700392 // If the account is selected, proceed to place the outgoing call
393 call.startCreateConnection();
394 } else {
395 // This is the state where the user is expected to select an account
Santos Cordon2d0b3312014-08-15 15:04:17 -0700396 setCallState(call, CallState.PRE_DIAL_WAIT);
Nancy Chend9de92c2014-07-21 16:09:13 -0700397 }
Yorke Leef98fb572014-03-05 10:56:55 -0800398 }
399
400 /**
Santos Cordona1610702014-06-04 20:22:56 -0700401 * Attempts to start a conference call for the specified call.
402 *
Santos Cordon12d61822014-07-29 16:02:20 -0700403 * @param call The call to conference.
404 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700405 */
Santos Cordon12d61822014-07-29 16:02:20 -0700406 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700407 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700408 }
409
410 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800411 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
412 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
413 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700414 *
415 * @param call The call to answer.
416 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800417 */
Andrew Lee38931d02014-07-16 10:17:36 -0700418 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700419 if (!mCalls.contains(call)) {
420 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800421 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700422 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700423 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700424 if (mForegroundCall != null && mForegroundCall != call &&
425 (mForegroundCall.isActive() ||
426 mForegroundCall.getState() == CallState.DIALING)) {
427 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
428 mForegroundCall, call);
429 mForegroundCall.hold();
Santos Cordondf399862014-08-06 04:39:15 -0700430 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700431 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700432 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700433 }
434
Santos Cordona56f2762014-03-24 15:55:53 -0700435 for (CallsManagerListener listener : mListeners) {
436 listener.onIncomingCallAnswered(call);
437 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800438
Santos Cordon61d0f702014-02-19 02:52:23 -0800439 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700440 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700441 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800442 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800443 }
444
445 /**
446 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
447 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
448 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800449 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700450 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700451 if (!mCalls.contains(call)) {
452 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800453 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700454 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700455 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700456 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700457 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800458 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800459 }
460
461 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700462 * Instructs Telecomm to play the specified DTMF tone within the specified call.
463 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700464 * @param digit The DTMF digit to play.
465 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700466 void playDtmfTone(Call call, char digit) {
467 if (!mCalls.contains(call)) {
468 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700469 } else {
470 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700471 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700472 }
473 }
474
475 /**
476 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700477 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700478 void stopDtmfTone(Call call) {
479 if (!mCalls.contains(call)) {
480 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700481 } else {
482 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700483 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700484 }
485 }
486
487 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700488 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700489 */
Evan Charlton352105c2014-06-03 14:10:54 -0700490 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700491 if (!mCalls.contains(call)) {
492 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700493 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700494 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700495 }
496 }
497
498 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800499 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
500 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
501 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800502 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700503 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700504 Log.v(this, "disconnectCall %s", call);
505
Sailesh Nepale59bb192014-04-01 18:33:59 -0700506 if (!mCalls.contains(call)) {
507 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800508 } else {
509 call.disconnect();
510 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800511 }
Santos Cordon681663d2014-01-30 04:32:15 -0800512
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700513 /**
Tyler Gunn61b92102014-08-19 07:42:20 -0700514 * Instructs Telecomm to disconnect all calls.
515 */
516 void disconnectAllCalls() {
517 Log.v(this, "disconnectAllCalls");
518
519 for (Call call : mCalls) {
520 disconnectCall(call);
521 }
522 }
523
524 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700525 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
526 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
527 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700528 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700529 void holdCall(Call call) {
530 if (!mCalls.contains(call)) {
531 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700532 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700533 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700534 call.hold();
535 }
536 }
537
538 /**
539 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
540 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
541 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700542 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700543 void unholdCall(Call call) {
544 if (!mCalls.contains(call)) {
545 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700546 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700547 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700548 call.unhold();
549 }
550 }
551
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700552 /** Called by the in-call UI to change the mute state. */
553 void mute(boolean shouldMute) {
554 mCallAudioManager.mute(shouldMute);
555 }
556
557 /**
558 * Called by the in-call UI to change the audio route, for example to change from earpiece to
559 * speaker phone.
560 */
561 void setAudioRoute(int route) {
562 mCallAudioManager.setAudioRoute(route);
563 }
564
Yorke Leed1346872014-07-28 14:38:45 -0700565 /** Called by the in-call UI to turn the proximity sensor on. */
566 void turnOnProximitySensor() {
567 mProximitySensorManager.turnOn();
568 }
569
570 /**
571 * Called by the in-call UI to turn the proximity sensor off.
572 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
573 * the screen will be kept off until the proximity sensor goes negative.
574 */
575 void turnOffProximitySensor(boolean screenOnImmediately) {
576 mProximitySensorManager.turnOff(screenOnImmediately);
577 }
578
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700579 void phoneAccountClicked(Call call) {
580 if (!mCalls.contains(call)) {
581 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
582 } else {
583 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700584 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700585 }
586
Evan Charlton89176372014-07-19 18:23:09 -0700587 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700588 if (!mCalls.contains(call)) {
589 Log.i(this, "Attemped to add account to unknown call %s", call);
590 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700591 call.setTargetPhoneAccount(account);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700592 call.startCreateConnection();
593 }
594 }
595
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700596 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700597 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700598 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700599 for (CallsManagerListener listener : mListeners) {
600 listener.onAudioStateChanged(oldAudioState, newAudioState);
601 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700602 }
603
Sailesh Nepale59bb192014-04-01 18:33:59 -0700604 void markCallAsRinging(Call call) {
605 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800606 }
607
Sailesh Nepale59bb192014-04-01 18:33:59 -0700608 void markCallAsDialing(Call call) {
609 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800610 }
611
Sailesh Nepale59bb192014-04-01 18:33:59 -0700612 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700613 if (call.getConnectTimeMillis() == 0) {
614 call.setConnectTimeMillis(System.currentTimeMillis());
615 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700616 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700617
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700618 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700619 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700620 }
Santos Cordon681663d2014-01-30 04:32:15 -0800621 }
622
Sailesh Nepale59bb192014-04-01 18:33:59 -0700623 void markCallAsOnHold(Call call) {
624 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700625 }
626
Santos Cordon049b7b62014-01-30 05:34:26 -0800627 /**
Ihab Awad6fb37c82014-08-07 19:48:57 -0700628 * 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 -0800629 * live call, then also disconnect from the in-call controller.
630 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700631 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700632 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800633 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700634 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
635 call.setDisconnectCause(disconnectCause, disconnectMessage);
636 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700637 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700638 }
639
Ben Gilada0d9f752014-02-26 11:49:03 -0800640 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700641 * Removes an existing disconnected call, and notifies the in-call app.
642 */
643 void markCallAsRemoved(Call call) {
644 removeCall(call);
645 }
646
647 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700648 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700649 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700650 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700651 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700652 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700653 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700654 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700655 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700656 if (call.getConnectionService() == service) {
657 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
658 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700659 }
660 }
661 }
662
Santos Cordondeb8c892014-05-30 01:38:03 -0700663 boolean hasAnyCalls() {
664 return !mCalls.isEmpty();
665 }
666
Santos Cordonc7e85d42014-05-22 02:51:48 -0700667 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700668 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700669 }
670
671 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700672 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700673 }
674
Santos Cordondeb8c892014-05-30 01:38:03 -0700675 boolean onMediaButton(int type) {
676 if (hasAnyCalls()) {
677 if (HeadsetMediaButton.SHORT_PRESS == type) {
678 Call ringingCall = getFirstCallWithState(CallState.RINGING);
679 if (ringingCall == null) {
680 mCallAudioManager.toggleMute();
681 return true;
682 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700683 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700684 return true;
685 }
686 } else if (HeadsetMediaButton.LONG_PRESS == type) {
687 Log.d(this, "handleHeadsetHook: longpress -> hangup");
688 Call callToHangup = getFirstCallWithState(
689 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
690 if (callToHangup != null) {
691 callToHangup.disconnect();
692 return true;
693 }
694 }
695 }
696 return false;
697 }
698
699 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700700 * Checks to see if the specified call is the only high-level call and if so, enable the
701 * "Add-call" button. We allow you to add a second call but not a third or beyond.
702 *
703 * @param call The call to test for add-call.
704 * @return Whether the add-call feature should be enabled for the call.
705 */
706 protected boolean isAddCallCapable(Call call) {
707 if (call.getParentCall() != null) {
708 // Never true for child calls.
709 return false;
710 }
711
712 // Loop through all the other calls and there exists a top level (has no parent) call
713 // that is not the specified call, return false.
714 for (Call otherCall : mCalls) {
715 if (call != otherCall && otherCall.getParentCall() == null) {
716 return false;
717 }
718 }
719 return true;
720 }
721
722 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700723 * Returns the first call that it finds with the given states. The states are treated as having
724 * priority order so that any call with the first state will be returned before any call with
725 * states listed later in the parameter list.
726 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700727 Call getFirstCallWithState(int... states) {
728 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700729 // check the foreground first
730 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
731 return mForegroundCall;
732 }
733
Santos Cordondeb8c892014-05-30 01:38:03 -0700734 for (Call call : mCalls) {
735 if (currentState == call.getState()) {
736 return call;
737 }
738 }
739 }
740 return null;
741 }
742
Santos Cordon0fbe6322014-08-14 04:04:25 -0700743 Call createConferenceCall(
744 PhoneAccountHandle phoneAccount,
745 ParcelableConference parcelableConference) {
746 Call call = new Call(
747 mConnectionServiceRepository,
748 null /* handle */,
749 null /* gatewayInfo */,
750 null /* connectionManagerPhoneAccount */,
751 phoneAccount,
752 false /* isIncoming */,
753 true /* isConference */);
754
755 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
756 call.setCallCapabilities(parcelableConference.getCapabilities());
757
758 // TODO: Move this to be a part of addCall()
759 call.addListener(this);
760 addCall(call);
761 return call;
762 }
763
Santos Cordon4b2c1192014-03-19 18:15:38 -0700764 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800765 * Adds the specified call to the main list of live calls.
766 *
767 * @param call The call to add.
768 */
769 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700770 Log.v(this, "addCall(%s)", call);
771
772 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700773 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700774
Santos Cordondf399862014-08-06 04:39:15 -0700775 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700776 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700777 for (CallsManagerListener listener : mListeners) {
778 listener.onCallAdded(call);
779 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700780 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800781 }
782
Sailesh Nepal810735e2014-03-18 18:15:46 -0700783 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700784 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700785
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}