blob: d0aa49f7f265d72c9048fe4defc1b03ba744d157 [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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Sailesh Nepalce704b92014-03-17 18:31:43 -070019import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070021import android.telecom.AudioState;
22import android.telecom.CallState;
23import android.telecom.GatewayInfo;
24import android.telecom.ParcelableConference;
25import android.telecom.PhoneAccountHandle;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070027import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Sailesh Nepal810735e2014-03-18 18:15:46 -070029import com.google.common.collect.ImmutableCollection;
30import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Jay Shraunera82c8f72014-08-14 15:49:16 -070032import java.util.Collections;
Santos Cordon7cdb9092014-07-24 21:33:33 -070033import java.util.List;
Santos Cordona56f2762014-03-24 15:55:53 -070034import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070035import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Ben Giladdd8c6082013-12-30 14:44:08 -080037/**
38 * Singleton.
39 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070040 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080041 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070042 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080043 */
Santos Cordon64c7e962014-07-02 15:15:27 -070044public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080045
Santos Cordondf399862014-08-06 04:39:15 -070046 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070047 interface CallsManagerListener {
48 void onCallAdded(Call call);
49 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070050 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070051 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070052 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070053 ConnectionServiceWrapper oldService,
54 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070055 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070056 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070057 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070058 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070059 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070060 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070061 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070062 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070063 }
64
Santos Cordon8e8b8d22013-12-19 14:14:05 -080065 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080066
Santos Cordon8e8b8d22013-12-19 14:14:05 -080067 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
69 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070070 *
71 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
72 * load factor before resizing, 1 means we only expect a single thread to
73 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -080074 */
Jay Shraunera82c8f72014-08-14 15:49:16 -070075 private final Set<Call> mCalls = Collections.newSetFromMap(
76 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -080077
Sailesh Nepal664837f2014-07-14 16:31:51 -070078 private final ConnectionServiceRepository mConnectionServiceRepository =
79 new ConnectionServiceRepository();
Santos Cordonf3671a62014-05-29 21:51:53 -070080 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
81 private final InCallController mInCallController = new InCallController();
82 private final CallAudioManager mCallAudioManager;
83 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -070084 // For this set initial table size to 16 because we add 13 listeners in
85 // the CallsManager constructor.
86 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
87 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -070088 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070089 private final WiredHeadsetManager mWiredHeadsetManager;
90 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -070091 private final ProximitySensorManager mProximitySensorManager;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070092
93 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070094 * The call the user is currently interacting with. This is the call that should have audio
95 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080096 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070097 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080098
Santos Cordon766d04f2014-05-06 10:28:25 -070099 /** Singleton accessor. */
100 static CallsManager getInstance() {
101 return INSTANCE;
102 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800103
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700105 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800106 */
107 private CallsManager() {
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700108 TelecomApp app = TelecomApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700109
Santos Cordondeb8c892014-05-30 01:38:03 -0700110 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700111 mWiredHeadsetManager = new WiredHeadsetManager(app);
112 mCallAudioManager = new CallAudioManager(app, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700113 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700114 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700115 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700116 mTtyManager = new TtyManager(app, mWiredHeadsetManager);
Yorke Leed1346872014-07-28 14:38:45 -0700117 mProximitySensorManager = new ProximitySensorManager(app);
Santos Cordonae193062014-05-21 21:21:49 -0700118
Santos Cordondeb8c892014-05-30 01:38:03 -0700119 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700120 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700121 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700122 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700123 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700124 mListeners.add(new RingbackPlayer(this, playerFactory));
125 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700126 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700127 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700128 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700129 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700130 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700131 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800132 }
133
Santos Cordon766d04f2014-05-06 10:28:25 -0700134 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700135 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700136 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700137
Tyler Gunncde2e502014-08-12 14:35:58 -0700138 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700139 if (!mCalls.contains(call)) {
140 // Call was not added previously in startOutgoingCall due to it being a potential MMI
141 // code, so add it now.
142 addCall(call);
143 }
144
145 // The call's ConnectionService has been updated.
146 for (CallsManagerListener listener : mListeners) {
147 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700148 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700149
150 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700151 }
152
153 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700154 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
155 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700156
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700157 // TODO: Replace disconnect cause with more specific disconnect causes.
158 markCallAsDisconnected(call, errorCode, errorMsg);
159 }
160
161 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700162 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700163 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700164 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700165 addCall(call);
166 }
167
168 @Override
169 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700170 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700171 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800172 }
173
Ihab Awadcb387ac2014-05-28 16:49:38 -0700174 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700175 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700176 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700177 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700178 }
179 }
180
Evan Charlton352105c2014-06-03 14:10:54 -0700181 @Override
182 public void onPostDialWait(Call call, String remaining) {
183 mInCallController.onPostDialWait(call, remaining);
184 }
185
Santos Cordona1610702014-06-04 20:22:56 -0700186 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700187 public void onParentChanged(Call call) {
188 for (CallsManagerListener listener : mListeners) {
189 listener.onIsConferencedChanged(call);
190 }
191 }
192
193 @Override
194 public void onChildrenChanged(Call call) {
195 for (CallsManagerListener listener : mListeners) {
196 listener.onIsConferencedChanged(call);
197 }
198 }
199
Ihab Awadff7493a2014-06-10 13:47:44 -0700200 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700201 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700202 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700203 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700204 }
205 }
206
Andrew Lee4a796602014-07-11 17:23:03 -0700207 @Override
208 public void onVideoStateChanged(Call call) {
209 for (CallsManagerListener listener : mListeners) {
210 listener.onVideoStateChanged(call);
211 }
212 }
213
Sailesh Nepal810735e2014-03-18 18:15:46 -0700214 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700215 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700216 }
217
218 Call getForegroundCall() {
219 return mForegroundCall;
220 }
221
Santos Cordonae193062014-05-21 21:21:49 -0700222 Ringer getRinger() {
223 return mRinger;
224 }
225
Santos Cordonf3671a62014-05-29 21:51:53 -0700226 InCallController getInCallController() {
227 return mInCallController;
228 }
229
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700230 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700231 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700232 if (call.isEmergencyCall()) {
233 return true;
234 }
235 }
236 return false;
237 }
238
Ihab Awad6fb37c82014-08-07 19:48:57 -0700239 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700240 return mCallAudioManager.getAudioState();
241 }
242
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700243 boolean isTtySupported() {
244 return mTtyManager.isTtySupported();
245 }
246
247 int getCurrentTtyMode() {
248 return mTtyManager.getCurrentTtyMode();
249 }
250
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800251 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700252 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800253 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700254 * @param phoneAccountHandle The phone account which contains the component name of the
255 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800256 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800257 */
Evan Charlton89176372014-07-19 18:23:09 -0700258 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800259 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700260 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700261 Call call = new Call(
Sailesh Nepal664837f2014-07-14 16:31:51 -0700262 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700263 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700264 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 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700279 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700280 * @param phoneAccountHandle The phone account which contains the component name of the
281 * connection service to use for this call.
282 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700283 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700284 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700285 // We only allow a single outgoing call at any given time. Before placing a call, make sure
286 // there doesn't already exist another outgoing call.
Yorke Lee15a30c32014-09-02 17:58:13 -0700287 Call call = getFirstCallWithState(CallState.NEW, CallState.DIALING,
288 CallState.CONNECTING, CallState.PRE_DIAL_WAIT);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700289
290 if (call != null) {
291 Log.i(this, "Canceling simultaneous outgoing call.");
292 return null;
293 }
294
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700295 TelecomApp app = TelecomApp.getInstance();
Nancy Chenbc9ef172014-08-15 17:11:57 -0700296
297 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
298 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700299 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700300 if (phoneAccountHandle != null) {
301 List<PhoneAccountHandle> enabledAccounts =
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700302 app.getPhoneAccountRegistrar().getEnabledPhoneAccounts(handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700303 if (!enabledAccounts.contains(phoneAccountHandle)) {
304 phoneAccountHandle = null;
305 }
306 }
307
308 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700309 // No preset account, check if default exists that supports the URI scheme for the
310 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700311 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn84253572014-09-02 14:50:05 -0700312 app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount(
313 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700314 if (defaultAccountHandle != null) {
315 phoneAccountHandle = defaultAccountHandle;
316 }
317 }
318
Nancy Chen0d3076c2014-07-30 14:45:44 -0700319 // Create a call with original handle. The handle may be changed when the call is attached
320 // to a connection service, but in most cases will remain the same.
321 call = new Call(
322 mConnectionServiceRepository,
323 handle,
324 null /* gatewayInfo */,
325 null /* connectionManagerPhoneAccount */,
326 phoneAccountHandle,
327 false /* isIncoming */,
328 false /* isConference */);
Nancy Chena9d91da2014-08-12 14:31:06 -0700329 call.setExtras(extras);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700330
331 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
332 if (phoneAccountHandle == null && !emergencyCall) {
333 // This is the state where the user is expected to select an account
334 call.setState(CallState.PRE_DIAL_WAIT);
335 } else {
336 call.setState(CallState.CONNECTING);
337 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700338
Yorke Leeb701f6d2014-08-12 14:04:19 -0700339 if (!isPotentialMMICode(handle)) {
340 addCall(call);
341 } else {
342 call.addListener(this);
343 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700344
345 return call;
346 }
347
Ben Gilada0d9f752014-02-26 11:49:03 -0800348 /**
Yorke Lee33501632014-03-17 19:24:12 -0700349 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800350 *
Yorke Lee33501632014-03-17 19:24:12 -0700351 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700352 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700353 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700354 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700355 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800356 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700357 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
358 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700359 if (call == null) {
360 // don't do anything if the call no longer exists
361 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700362 return;
363 }
364
Nancy Chen201b4372014-09-08 14:18:24 -0700365 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700366
367 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700368 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700369 } else {
370 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
371 Log.pii(uriHandle), Log.pii(handle));
372 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700373
Nancy Chen0d3076c2014-07-30 14:45:44 -0700374 call.setHandle(uriHandle);
375 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700376 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700377 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700378
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700379 TelecomApp app = TelecomApp.getInstance();
Santos Cordon7cdb9092014-07-24 21:33:33 -0700380 final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
Nancy Chend9de92c2014-07-21 16:09:13 -0700381 if (emergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700382 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700383 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700384 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700385
Ihab Awadb78b2762014-07-25 15:16:23 -0700386 if (call.getTargetPhoneAccount() != null || emergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700387 // If the account has been set, proceed to place the outgoing call.
388 // Otherwise the connection will be initiated when the account is set by the user.
Nancy Chend9de92c2014-07-21 16:09:13 -0700389 call.startCreateConnection();
Nancy Chend9de92c2014-07-21 16:09:13 -0700390 }
Yorke Leef98fb572014-03-05 10:56:55 -0800391 }
392
393 /**
Santos Cordona1610702014-06-04 20:22:56 -0700394 * Attempts to start a conference call for the specified call.
395 *
Santos Cordon12d61822014-07-29 16:02:20 -0700396 * @param call The call to conference.
397 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700398 */
Santos Cordon12d61822014-07-29 16:02:20 -0700399 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700400 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700401 }
402
403 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700404 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
405 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800406 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700407 *
408 * @param call The call to answer.
409 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800410 */
Andrew Lee38931d02014-07-16 10:17:36 -0700411 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700412 if (!mCalls.contains(call)) {
413 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800414 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700415 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700416 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700417 if (mForegroundCall != null && mForegroundCall != call &&
418 (mForegroundCall.isActive() ||
419 mForegroundCall.getState() == CallState.DIALING)) {
420 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
421 mForegroundCall, call);
422 mForegroundCall.hold();
Santos Cordondf399862014-08-06 04:39:15 -0700423 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700424 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700425 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700426 }
427
Santos Cordona56f2762014-03-24 15:55:53 -0700428 for (CallsManagerListener listener : mListeners) {
429 listener.onIncomingCallAnswered(call);
430 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800431
Santos Cordon61d0f702014-02-19 02:52:23 -0800432 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700434 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800435 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800436 }
437
438 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700439 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
440 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800441 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800442 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700443 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700444 if (!mCalls.contains(call)) {
445 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800446 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700447 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700448 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700449 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700450 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800451 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800452 }
453
454 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700455 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700456 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700457 * @param digit The DTMF digit to play.
458 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700459 void playDtmfTone(Call call, char digit) {
460 if (!mCalls.contains(call)) {
461 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700462 } else {
463 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700464 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700465 }
466 }
467
468 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700469 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700470 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700471 void stopDtmfTone(Call call) {
472 if (!mCalls.contains(call)) {
473 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700474 } else {
475 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700476 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700477 }
478 }
479
480 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700481 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700482 */
Evan Charlton352105c2014-06-03 14:10:54 -0700483 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700484 if (!mCalls.contains(call)) {
485 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700486 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700487 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700488 }
489 }
490
491 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700492 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800493 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
494 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800495 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700496 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700497 Log.v(this, "disconnectCall %s", call);
498
Sailesh Nepale59bb192014-04-01 18:33:59 -0700499 if (!mCalls.contains(call)) {
500 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800501 } else {
502 call.disconnect();
503 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800504 }
Santos Cordon681663d2014-01-30 04:32:15 -0800505
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700506 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700507 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700508 */
509 void disconnectAllCalls() {
510 Log.v(this, "disconnectAllCalls");
511
512 for (Call call : mCalls) {
513 disconnectCall(call);
514 }
515 }
516
Nancy Chenbc9ef172014-08-15 17:11:57 -0700517
Tyler Gunn61b92102014-08-19 07:42:20 -0700518 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700519 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700520 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
521 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700522 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700523 void holdCall(Call call) {
524 if (!mCalls.contains(call)) {
525 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700526 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700527 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700528 call.hold();
529 }
530 }
531
532 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700533 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700534 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
535 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700536 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700537 void unholdCall(Call call) {
538 if (!mCalls.contains(call)) {
539 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700540 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700541 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700542 for (Call c : mCalls) {
543 if (c != null && c.isAlive() && c != call) {
544 c.hold();
545 }
546 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700547 call.unhold();
548 }
549 }
550
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700551 /** Called by the in-call UI to change the mute state. */
552 void mute(boolean shouldMute) {
553 mCallAudioManager.mute(shouldMute);
554 }
555
556 /**
557 * Called by the in-call UI to change the audio route, for example to change from earpiece to
558 * speaker phone.
559 */
560 void setAudioRoute(int route) {
561 mCallAudioManager.setAudioRoute(route);
562 }
563
Yorke Leed1346872014-07-28 14:38:45 -0700564 /** Called by the in-call UI to turn the proximity sensor on. */
565 void turnOnProximitySensor() {
566 mProximitySensorManager.turnOn();
567 }
568
569 /**
570 * Called by the in-call UI to turn the proximity sensor off.
571 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
572 * the screen will be kept off until the proximity sensor goes negative.
573 */
574 void turnOffProximitySensor(boolean screenOnImmediately) {
575 mProximitySensorManager.turnOff(screenOnImmediately);
576 }
577
Evan Charlton89176372014-07-19 18:23:09 -0700578 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700579 if (!mCalls.contains(call)) {
580 Log.i(this, "Attemped to add account to unknown call %s", call);
581 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700582 call.setTargetPhoneAccount(account);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700583 call.startCreateConnection();
584 }
585 }
586
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700587 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700588 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700589 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700590 for (CallsManagerListener listener : mListeners) {
591 listener.onAudioStateChanged(oldAudioState, newAudioState);
592 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700593 }
594
Sailesh Nepale59bb192014-04-01 18:33:59 -0700595 void markCallAsRinging(Call call) {
596 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800597 }
598
Sailesh Nepale59bb192014-04-01 18:33:59 -0700599 void markCallAsDialing(Call call) {
600 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800601 }
602
Sailesh Nepale59bb192014-04-01 18:33:59 -0700603 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700604 if (call.getConnectTimeMillis() == 0) {
605 call.setConnectTimeMillis(System.currentTimeMillis());
606 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700607 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700608
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700609 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700610 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700611 }
Santos Cordon681663d2014-01-30 04:32:15 -0800612 }
613
Sailesh Nepale59bb192014-04-01 18:33:59 -0700614 void markCallAsOnHold(Call call) {
615 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700616 }
617
Santos Cordon049b7b62014-01-30 05:34:26 -0800618 /**
Ihab Awad6fb37c82014-08-07 19:48:57 -0700619 * 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 -0800620 * live call, then also disconnect from the in-call controller.
621 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700622 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700623 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800624 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700625 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
626 call.setDisconnectCause(disconnectCause, disconnectMessage);
627 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700628 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700629 }
630
Ben Gilada0d9f752014-02-26 11:49:03 -0800631 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700632 * Removes an existing disconnected call, and notifies the in-call app.
633 */
634 void markCallAsRemoved(Call call) {
635 removeCall(call);
636 }
637
638 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700639 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700640 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700641 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700642 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700643 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700644 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700645 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700646 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700647 if (call.getConnectionService() == service) {
648 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
649 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700650 }
651 }
652 }
653
Santos Cordondeb8c892014-05-30 01:38:03 -0700654 boolean hasAnyCalls() {
655 return !mCalls.isEmpty();
656 }
657
Santos Cordonc7e85d42014-05-22 02:51:48 -0700658 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700659 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700660 }
661
662 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700663 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700664 }
665
Santos Cordondeb8c892014-05-30 01:38:03 -0700666 boolean onMediaButton(int type) {
667 if (hasAnyCalls()) {
668 if (HeadsetMediaButton.SHORT_PRESS == type) {
669 Call ringingCall = getFirstCallWithState(CallState.RINGING);
670 if (ringingCall == null) {
671 mCallAudioManager.toggleMute();
672 return true;
673 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700674 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700675 return true;
676 }
677 } else if (HeadsetMediaButton.LONG_PRESS == type) {
678 Log.d(this, "handleHeadsetHook: longpress -> hangup");
679 Call callToHangup = getFirstCallWithState(
680 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
681 if (callToHangup != null) {
682 callToHangup.disconnect();
683 return true;
684 }
685 }
686 }
687 return false;
688 }
689
690 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700691 * Checks to see if the specified call is the only high-level call and if so, enable the
692 * "Add-call" button. We allow you to add a second call but not a third or beyond.
693 *
694 * @param call The call to test for add-call.
695 * @return Whether the add-call feature should be enabled for the call.
696 */
697 protected boolean isAddCallCapable(Call call) {
698 if (call.getParentCall() != null) {
699 // Never true for child calls.
700 return false;
701 }
702
703 // Loop through all the other calls and there exists a top level (has no parent) call
704 // that is not the specified call, return false.
705 for (Call otherCall : mCalls) {
706 if (call != otherCall && otherCall.getParentCall() == null) {
707 return false;
708 }
709 }
710 return true;
711 }
712
713 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700714 * Returns the first call that it finds with the given states. The states are treated as having
715 * priority order so that any call with the first state will be returned before any call with
716 * states listed later in the parameter list.
717 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700718 Call getFirstCallWithState(int... states) {
719 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700720 // check the foreground first
721 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
722 return mForegroundCall;
723 }
724
Santos Cordondeb8c892014-05-30 01:38:03 -0700725 for (Call call : mCalls) {
726 if (currentState == call.getState()) {
727 return call;
728 }
729 }
730 }
731 return null;
732 }
733
Santos Cordon0fbe6322014-08-14 04:04:25 -0700734 Call createConferenceCall(
735 PhoneAccountHandle phoneAccount,
736 ParcelableConference parcelableConference) {
737 Call call = new Call(
738 mConnectionServiceRepository,
739 null /* handle */,
740 null /* gatewayInfo */,
741 null /* connectionManagerPhoneAccount */,
742 phoneAccount,
743 false /* isIncoming */,
744 true /* isConference */);
745
746 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700747 if (call.getState() == CallState.ACTIVE) {
748 call.setConnectTimeMillis(System.currentTimeMillis());
749 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700750 call.setCallCapabilities(parcelableConference.getCapabilities());
751
752 // TODO: Move this to be a part of addCall()
753 call.addListener(this);
754 addCall(call);
755 return call;
756 }
757
Nancy Chenbc9ef172014-08-15 17:11:57 -0700758
Santos Cordon4b2c1192014-03-19 18:15:38 -0700759 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800760 * Adds the specified call to the main list of live calls.
761 *
762 * @param call The call to add.
763 */
764 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700765 Log.v(this, "addCall(%s)", call);
766
767 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700768 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700769
Santos Cordondf399862014-08-06 04:39:15 -0700770 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700771 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700772 for (CallsManagerListener listener : mListeners) {
773 listener.onCallAdded(call);
774 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700775 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800776 }
777
Sailesh Nepal810735e2014-03-18 18:15:46 -0700778 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700779 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700780
Santos Cordon672321e2014-08-21 14:38:58 -0700781 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700782 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700783 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700784
785 boolean shouldNotify = false;
786 if (mCalls.contains(call)) {
787 mCalls.remove(call);
788 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700789 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800790
Sailesh Nepale59bb192014-04-01 18:33:59 -0700791 // Only broadcast changes for calls that are being tracked.
792 if (shouldNotify) {
793 for (CallsManagerListener listener : mListeners) {
794 listener.onCallRemoved(call);
795 }
796 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700797 }
798 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800799
Sailesh Nepal810735e2014-03-18 18:15:46 -0700800 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700801 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700802 *
803 * @param call The call.
804 * @param newState The new state of the call.
805 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700806 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700807 if (call == null) {
808 return;
809 }
Ihab Awad6fb37c82014-08-07 19:48:57 -0700810 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -0700811 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
812 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700813 if (newState != oldState) {
814 // Unfortunately, in the telephony world the radio is king. So if the call notifies
815 // 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 -0700816 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700817 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700818 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700819 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700820 // unexpected transition occurs.
821 call.setState(newState);
822
823 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700824 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700825 for (CallsManagerListener listener : mListeners) {
826 listener.onCallStateChanged(call, oldState, newState);
827 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700828 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800829 }
830 }
831 }
832
833 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700834 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800835 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700836 private void updateForegroundCall() {
837 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700838 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700839 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700840 // of its state will be foreground by default and instead the connection service should
841 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700842 // the call should play audio and listen to microphone if it wants.
843
844 // Active calls have priority.
845 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700846 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800847 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700848 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700849
850 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700851 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700852 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800853 }
854 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800855
Sailesh Nepal810735e2014-03-18 18:15:46 -0700856 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700857 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700858 Call oldForegroundCall = mForegroundCall;
859 mForegroundCall = newForegroundCall;
860
Santos Cordona56f2762014-03-24 15:55:53 -0700861 for (CallsManagerListener listener : mListeners) {
862 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
863 }
Santos Cordon681663d2014-01-30 04:32:15 -0800864 }
865 }
Yorke Leeb701f6d2014-08-12 14:04:19 -0700866
867 private boolean isPotentialMMICode(Uri handle) {
868 return (handle != null && handle.getSchemeSpecificPart() != null
869 && handle.getSchemeSpecificPart().contains("#"));
870 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800871}