blob: 52c0b44a97b5028b8197c7e0fb789aaa6fa63a4c [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
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Sailesh Nepalce704b92014-03-17 18:31:43 -070020import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
Santos Cordonf193ba42014-09-12 06:37:39 -070022import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070023import android.telecom.AudioState;
24import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070025import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070026import android.telecom.GatewayInfo;
27import android.telecom.ParcelableConference;
Tyler Gunn5b452df2014-10-01 15:02:58 -070028import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070030import android.telecom.PhoneCapabilities;
Tyler Gunn5b452df2014-10-01 15:02:58 -070031import android.telecom.TelecomManager;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070032import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033
Tyler Gunn91d43cf2014-09-17 12:19:39 -070034import com.android.internal.util.IndentingPrintWriter;
Yorke Lee9250e5f2014-10-01 13:39:09 -070035
Sailesh Nepal810735e2014-03-18 18:15:46 -070036import com.google.common.collect.ImmutableCollection;
37import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080038
Jay Shraunera82c8f72014-08-14 15:49:16 -070039import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070040import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070041import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070042import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070043import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070044import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080045
Ben Giladdd8c6082013-12-30 14:44:08 -080046/**
47 * Singleton.
48 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070049 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080050 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070051 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080052 */
Santos Cordon64c7e962014-07-02 15:15:27 -070053public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080054
Santos Cordondf399862014-08-06 04:39:15 -070055 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070056 interface CallsManagerListener {
57 void onCallAdded(Call call);
58 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070059 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070060 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070061 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070062 ConnectionServiceWrapper oldService,
63 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070064 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070065 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070066 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070067 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070068 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070069 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070070 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070071 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070072 }
73
Tyler Gunn91d43cf2014-09-17 12:19:39 -070074 /**
75 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
76 */
77 private static CallsManager INSTANCE = null;
78
79 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080080
Santos Cordonf193ba42014-09-12 06:37:39 -070081 private static final int MAXIMUM_LIVE_CALLS = 1;
82 private static final int MAXIMUM_HOLD_CALLS = 1;
83 private static final int MAXIMUM_RINGING_CALLS = 1;
84 private static final int MAXIMUM_OUTGOING_CALLS = 1;
85
86 private static final int[] LIVE_CALL_STATES =
87 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
88
89 private static final int[] OUTGOING_CALL_STATES =
90 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
91
Santos Cordon8e8b8d22013-12-19 14:14:05 -080092 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070093 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
94 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070095 *
96 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
97 * load factor before resizing, 1 means we only expect a single thread to
98 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -080099 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700100 private final Set<Call> mCalls = Collections.newSetFromMap(
101 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800102
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700103 private final ConnectionServiceRepository mConnectionServiceRepository;
104 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
105 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700106 private final CallAudioManager mCallAudioManager;
107 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700108 // For this set initial table size to 16 because we add 13 listeners in
109 // the CallsManager constructor.
110 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
111 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700112 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700113 private final WiredHeadsetManager mWiredHeadsetManager;
114 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700115 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700116 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700117 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700118 private final Context mContext;
119 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
120 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700121 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700122
123 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 * The call the user is currently interacting with. This is the call that should have audio
125 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800126 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700127 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800128
Santos Cordon766d04f2014-05-06 10:28:25 -0700129 /** Singleton accessor. */
130 static CallsManager getInstance() {
131 return INSTANCE;
132 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800133
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800134 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700135 * Sets the static singleton instance.
136 *
137 * @param instance The instance to set.
138 */
139 static void initialize(CallsManager instance) {
140 INSTANCE = instance;
141 }
142
143 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700144 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700146 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
147 PhoneAccountRegistrar phoneAccountRegistrar) {
148 mContext = context;
149 mPhoneAccountRegistrar = phoneAccountRegistrar;
150 mMissedCallNotifier = missedCallNotifier;
151 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
152 mWiredHeadsetManager = new WiredHeadsetManager(context);
153 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700154 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700155 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
156 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
157 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
158 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700159 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700160 mCallLogManager = new CallLogManager(context);
161 mInCallController = new InCallController(context);
162 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
163 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
164 context);
Santos Cordonae193062014-05-21 21:21:49 -0700165
Santos Cordondeb8c892014-05-30 01:38:03 -0700166 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700167 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700168 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700169 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700170 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700171 mListeners.add(new RingbackPlayer(this, playerFactory));
172 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700173 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700174 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700175 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700176 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700177 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700178 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800179 }
180
Santos Cordon766d04f2014-05-06 10:28:25 -0700181 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700182 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700183 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700184
Tyler Gunncde2e502014-08-12 14:35:58 -0700185 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700186 if (!mCalls.contains(call)) {
187 // Call was not added previously in startOutgoingCall due to it being a potential MMI
188 // code, so add it now.
189 addCall(call);
190 }
191
192 // The call's ConnectionService has been updated.
193 for (CallsManagerListener listener : mListeners) {
194 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700195 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700196
197 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700198 }
199
200 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700201 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700202 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700203
Andrew Leee6288a72014-10-01 13:50:02 -0700204 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700205 }
206
207 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700208 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700209 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700210 setCallState(incomingCall, CallState.RINGING);
211
212 if (hasMaximumRingingCalls()) {
213 incomingCall.reject(false, null);
214 // since the call was not added to the list of calls, we have to call the missed
215 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700216 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700217 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
218 } else {
219 addCall(incomingCall);
220 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700221 }
222
223 @Override
224 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700225 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700226 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800227 }
228
Ihab Awadcb387ac2014-05-28 16:49:38 -0700229 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700230 public void onSuccessfulUnknownCall(Call call, int callState) {
231 setCallState(call, callState);
232 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
233 addCall(call);
234 }
235
236 @Override
237 public void onFailedUnknownCall(Call call) {
238 Log.i(this, "onFailedUnknownCall for call %s", call);
239 setCallState(call, CallState.DISCONNECTED);
240 call.removeListener(this);
241 }
242
243 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700244 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700245 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700246 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700247 }
248 }
249
Evan Charlton352105c2014-06-03 14:10:54 -0700250 @Override
251 public void onPostDialWait(Call call, String remaining) {
252 mInCallController.onPostDialWait(call, remaining);
253 }
254
Santos Cordona1610702014-06-04 20:22:56 -0700255 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700256 public void onParentChanged(Call call) {
257 for (CallsManagerListener listener : mListeners) {
258 listener.onIsConferencedChanged(call);
259 }
260 }
261
262 @Override
263 public void onChildrenChanged(Call call) {
264 for (CallsManagerListener listener : mListeners) {
265 listener.onIsConferencedChanged(call);
266 }
267 }
268
Ihab Awadff7493a2014-06-10 13:47:44 -0700269 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700270 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700271 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700272 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700273 }
274 }
275
Andrew Lee4a796602014-07-11 17:23:03 -0700276 @Override
277 public void onVideoStateChanged(Call call) {
278 for (CallsManagerListener listener : mListeners) {
279 listener.onVideoStateChanged(call);
280 }
281 }
282
Sailesh Nepal810735e2014-03-18 18:15:46 -0700283 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700284 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700285 }
286
287 Call getForegroundCall() {
288 return mForegroundCall;
289 }
290
Santos Cordonae193062014-05-21 21:21:49 -0700291 Ringer getRinger() {
292 return mRinger;
293 }
294
Santos Cordonf3671a62014-05-29 21:51:53 -0700295 InCallController getInCallController() {
296 return mInCallController;
297 }
298
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700299 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700300 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700301 if (call.isEmergencyCall()) {
302 return true;
303 }
304 }
305 return false;
306 }
307
Ihab Awad6fb37c82014-08-07 19:48:57 -0700308 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700309 return mCallAudioManager.getAudioState();
310 }
311
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700312 boolean isTtySupported() {
313 return mTtyManager.isTtySupported();
314 }
315
316 int getCurrentTtyMode() {
317 return mTtyManager.getCurrentTtyMode();
318 }
319
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700320 void addListener(CallsManagerListener listener) {
321 mListeners.add(listener);
322 }
323
324 void removeListener(CallsManagerListener listener) {
325 mListeners.remove(listener);
326 }
327
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800328 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700329 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800330 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700331 * @param phoneAccountHandle The phone account which contains the component name of the
332 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800333 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800334 */
Evan Charlton89176372014-07-19 18:23:09 -0700335 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800336 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700337 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700338 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700339 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700340 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700341 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700342 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700343 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700344 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700345 true /* isIncoming */,
346 false /* isConference */);
347
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700348 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700349 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700350 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700351 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800352 }
353
Yorke Lee9250e5f2014-10-01 13:39:09 -0700354 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
355 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
356 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
357 Call call = new Call(
358 mContext,
359 mConnectionServiceRepository,
360 handle,
361 null /* gatewayInfo */,
362 null /* connectionManagerPhoneAccount */,
363 phoneAccountHandle,
364 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
365 // to the existing connection instead of trying to create a new one.
366 true /* isIncoming */,
367 false /* isConference */);
368 call.setConnectTimeMillis(System.currentTimeMillis());
369 call.setIsUnknown(true);
370 call.setExtras(extras);
371 call.addListener(this);
372 call.startCreateConnection(mPhoneAccountRegistrar);
373 }
374
Nancy Chen0d3076c2014-07-30 14:45:44 -0700375 /**
376 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
377 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700378 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700379 * @param phoneAccountHandle The phone account which contains the component name of the
380 * connection service to use for this call.
381 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700382 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700383 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700384 // Create a call with original handle. The handle may be changed when the call is attached
385 // to a connection service, but in most cases will remain the same.
386 Call call = new Call(
387 mContext,
388 mConnectionServiceRepository,
389 handle,
390 null /* gatewayInfo */,
391 null /* connectionManagerPhoneAccount */,
392 null /* phoneAccountHandle */,
393 false /* isIncoming */,
394 false /* isConference */);
395
396 List<PhoneAccountHandle> accounts =
397 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
398
Nancy Chenbc9ef172014-08-15 17:11:57 -0700399 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
400 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700401 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700402 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700403 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700404 phoneAccountHandle = null;
405 }
406 }
407
408 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700409 // No preset account, check if default exists that supports the URI scheme for the
410 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700411 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700412 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700413 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700414 if (defaultAccountHandle != null) {
415 phoneAccountHandle = defaultAccountHandle;
416 }
417 }
418
Nancy Chen1c5926f2014-09-17 14:44:14 -0700419 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700420
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700421 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
422 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700423 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700424
425 // Do not support any more live calls. Our options are to move a call to hold, disconnect
426 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700427 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700428 // just cancel at this point.
429 return null;
430 }
431
Nancy Chen1ccab202014-09-18 15:28:36 -0700432 if (phoneAccountHandle == null && accounts.size() > 1 && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700433 // This is the state where the user is expected to select an account
434 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700435 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700436 } else {
437 call.setState(CallState.CONNECTING);
438 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700439
Nancy Chen1c5926f2014-09-17 14:44:14 -0700440 call.setExtras(extras);
441
Tyler Gunn5b452df2014-10-01 15:02:58 -0700442 if (isPotentialMMICode(handle) || isPotentialInCallMMICode) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700443 call.addListener(this);
Tyler Gunn5b452df2014-10-01 15:02:58 -0700444 } else {
445 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700446 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700447
448 return call;
449 }
450
Ben Gilada0d9f752014-02-26 11:49:03 -0800451 /**
Yorke Lee33501632014-03-17 19:24:12 -0700452 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800453 *
Yorke Lee33501632014-03-17 19:24:12 -0700454 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700455 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700456 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700457 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700458 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800459 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700460 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
461 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700462 if (call == null) {
463 // don't do anything if the call no longer exists
464 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700465 return;
466 }
467
Nancy Chen201b4372014-09-08 14:18:24 -0700468 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700469
470 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700471 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700472 } else {
473 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
474 Log.pii(uriHandle), Log.pii(handle));
475 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700476
Nancy Chen0d3076c2014-07-30 14:45:44 -0700477 call.setHandle(uriHandle);
478 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700479 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700480 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700481
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700482 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
483 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700484 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700485 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700486 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700487 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700488
Santos Cordonf193ba42014-09-12 06:37:39 -0700489 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700490 // If the account has been set, proceed to place the outgoing call.
491 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700492 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700493 }
Yorke Leef98fb572014-03-05 10:56:55 -0800494 }
495
496 /**
Santos Cordona1610702014-06-04 20:22:56 -0700497 * Attempts to start a conference call for the specified call.
498 *
Santos Cordon12d61822014-07-29 16:02:20 -0700499 * @param call The call to conference.
500 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700501 */
Santos Cordon12d61822014-07-29 16:02:20 -0700502 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700503 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700504 }
505
506 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700507 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
508 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800509 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700510 *
511 * @param call The call to answer.
512 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800513 */
Andrew Lee38931d02014-07-16 10:17:36 -0700514 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700515 if (!mCalls.contains(call)) {
516 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800517 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700518 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700519 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700520 if (mForegroundCall != null && mForegroundCall != call &&
521 (mForegroundCall.isActive() ||
522 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700523 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
524 // This call does not support hold. If it is from a different connection
525 // service, then disconnect it, otherwise allow the connection service to
526 // figure out the right states.
527 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
528 mForegroundCall.disconnect();
529 }
530 } else {
531 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
532 mForegroundCall, call);
533 mForegroundCall.hold();
534 }
Santos Cordondf399862014-08-06 04:39:15 -0700535 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700536 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700537 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700538 }
539
Santos Cordona56f2762014-03-24 15:55:53 -0700540 for (CallsManagerListener listener : mListeners) {
541 listener.onIncomingCallAnswered(call);
542 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800543
Santos Cordon61d0f702014-02-19 02:52:23 -0800544 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700545 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700546 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800547 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800548 }
549
550 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700551 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
552 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800553 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800554 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700555 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700556 if (!mCalls.contains(call)) {
557 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800558 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700559 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700560 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700561 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700562 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800563 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800564 }
565
566 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700567 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700568 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700569 * @param digit The DTMF digit to play.
570 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700571 void playDtmfTone(Call call, char digit) {
572 if (!mCalls.contains(call)) {
573 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700574 } else {
575 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700576 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700577 }
578 }
579
580 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700581 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700582 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700583 void stopDtmfTone(Call call) {
584 if (!mCalls.contains(call)) {
585 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700586 } else {
587 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700588 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700589 }
590 }
591
592 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700593 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700594 */
Evan Charlton352105c2014-06-03 14:10:54 -0700595 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700596 if (!mCalls.contains(call)) {
597 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700598 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700599 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700600 }
601 }
602
603 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700604 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800605 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
606 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800607 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700608 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700609 Log.v(this, "disconnectCall %s", call);
610
Sailesh Nepale59bb192014-04-01 18:33:59 -0700611 if (!mCalls.contains(call)) {
612 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800613 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700614 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800615 call.disconnect();
616 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800617 }
Santos Cordon681663d2014-01-30 04:32:15 -0800618
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700619 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700620 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700621 */
622 void disconnectAllCalls() {
623 Log.v(this, "disconnectAllCalls");
624
625 for (Call call : mCalls) {
626 disconnectCall(call);
627 }
628 }
629
Nancy Chenbc9ef172014-08-15 17:11:57 -0700630
Tyler Gunn61b92102014-08-19 07:42:20 -0700631 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700632 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700633 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
634 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700635 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700636 void holdCall(Call call) {
637 if (!mCalls.contains(call)) {
638 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700639 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700640 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700641 call.hold();
642 }
643 }
644
645 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700646 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700647 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
648 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700649 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700650 void unholdCall(Call call) {
651 if (!mCalls.contains(call)) {
652 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700653 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700654 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700655 for (Call c : mCalls) {
656 if (c != null && c.isAlive() && c != call) {
657 c.hold();
658 }
659 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700660 call.unhold();
661 }
662 }
663
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700664 /** Called by the in-call UI to change the mute state. */
665 void mute(boolean shouldMute) {
666 mCallAudioManager.mute(shouldMute);
667 }
668
669 /**
670 * Called by the in-call UI to change the audio route, for example to change from earpiece to
671 * speaker phone.
672 */
673 void setAudioRoute(int route) {
674 mCallAudioManager.setAudioRoute(route);
675 }
676
Yorke Leed1346872014-07-28 14:38:45 -0700677 /** Called by the in-call UI to turn the proximity sensor on. */
678 void turnOnProximitySensor() {
679 mProximitySensorManager.turnOn();
680 }
681
682 /**
683 * Called by the in-call UI to turn the proximity sensor off.
684 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
685 * the screen will be kept off until the proximity sensor goes negative.
686 */
687 void turnOffProximitySensor(boolean screenOnImmediately) {
688 mProximitySensorManager.turnOff(screenOnImmediately);
689 }
690
Evan Charlton89176372014-07-19 18:23:09 -0700691 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700692 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700693 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700694 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700695 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
696 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
697 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
698 // respecting a rewritten number or a canceled number. This is unlikely since
699 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
700 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700701 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700702
703 // Note: emergency calls never go through account selection dialog so they never
704 // arrive here.
705 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700706 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700707 } else {
708 call.disconnect();
709 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700710 }
711 }
712
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700713 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700714 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700715 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700716 for (CallsManagerListener listener : mListeners) {
717 listener.onAudioStateChanged(oldAudioState, newAudioState);
718 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700719 }
720
Sailesh Nepale59bb192014-04-01 18:33:59 -0700721 void markCallAsRinging(Call call) {
722 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800723 }
724
Sailesh Nepale59bb192014-04-01 18:33:59 -0700725 void markCallAsDialing(Call call) {
726 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800727 }
728
Sailesh Nepale59bb192014-04-01 18:33:59 -0700729 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700730 if (call.getConnectTimeMillis() == 0) {
731 call.setConnectTimeMillis(System.currentTimeMillis());
732 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700733 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700734
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700735 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700736 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700737 }
Santos Cordon681663d2014-01-30 04:32:15 -0800738 }
739
Sailesh Nepale59bb192014-04-01 18:33:59 -0700740 void markCallAsOnHold(Call call) {
741 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700742 }
743
Santos Cordon049b7b62014-01-30 05:34:26 -0800744 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700745 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
746 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800747 *
Andrew Lee701dc002014-09-11 21:29:12 -0700748 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800749 */
Andrew Lee701dc002014-09-11 21:29:12 -0700750 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
751 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700752 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700753 }
754
Ben Gilada0d9f752014-02-26 11:49:03 -0800755 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700756 * Removes an existing disconnected call, and notifies the in-call app.
757 */
758 void markCallAsRemoved(Call call) {
759 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700760 if (mLocallyDisconnectingCalls.contains(call)) {
761 mLocallyDisconnectingCalls.remove(call);
762 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
763 mForegroundCall.unhold();
764 }
765 }
Ihab Awada02bef52014-08-13 18:18:42 -0700766 }
767
768 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700769 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700770 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700771 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700772 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700773 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700774 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700775 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700776 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700777 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700778 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700779 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700780 }
781 }
782 }
783
Santos Cordondeb8c892014-05-30 01:38:03 -0700784 boolean hasAnyCalls() {
785 return !mCalls.isEmpty();
786 }
787
Santos Cordonc7e85d42014-05-22 02:51:48 -0700788 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700789 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700790 }
791
792 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700793 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700794 }
795
Santos Cordondeb8c892014-05-30 01:38:03 -0700796 boolean onMediaButton(int type) {
797 if (hasAnyCalls()) {
798 if (HeadsetMediaButton.SHORT_PRESS == type) {
799 Call ringingCall = getFirstCallWithState(CallState.RINGING);
800 if (ringingCall == null) {
801 mCallAudioManager.toggleMute();
802 return true;
803 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700804 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700805 return true;
806 }
807 } else if (HeadsetMediaButton.LONG_PRESS == type) {
808 Log.d(this, "handleHeadsetHook: longpress -> hangup");
809 Call callToHangup = getFirstCallWithState(
810 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
811 if (callToHangup != null) {
812 callToHangup.disconnect();
813 return true;
814 }
815 }
816 }
817 return false;
818 }
819
820 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700821 * Checks to see if the specified call is the only high-level call and if so, enable the
822 * "Add-call" button. We allow you to add a second call but not a third or beyond.
823 *
824 * @param call The call to test for add-call.
825 * @return Whether the add-call feature should be enabled for the call.
826 */
827 protected boolean isAddCallCapable(Call call) {
828 if (call.getParentCall() != null) {
829 // Never true for child calls.
830 return false;
831 }
832
Andrew Leec11fcd92014-09-19 18:31:46 -0700833 // Use canManageConference as a mechanism to check if the call is CDMA.
834 // Disable "Add Call" for CDMA calls which are conference calls.
835 boolean canManageConference = PhoneCapabilities.MANAGE_CONFERENCE
836 == (call.getCallCapabilities() & PhoneCapabilities.MANAGE_CONFERENCE);
837 if (call.isConference() && !canManageConference) {
838 return false;
839 }
840
Santos Cordon10838c22014-06-11 17:36:04 -0700841 // Loop through all the other calls and there exists a top level (has no parent) call
842 // that is not the specified call, return false.
843 for (Call otherCall : mCalls) {
844 if (call != otherCall && otherCall.getParentCall() == null) {
845 return false;
846 }
847 }
848 return true;
849 }
850
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700851 Call getRingingCall() {
852 return getFirstCallWithState(CallState.RINGING);
853 }
854
855 Call getActiveCall() {
856 return getFirstCallWithState(CallState.ACTIVE);
857 }
858
Nancy Chen05a9e402014-09-26 14:14:32 -0700859 Call getDialingCall() {
860 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700861 }
862
863 Call getHeldCall() {
864 return getFirstCallWithState(CallState.ON_HOLD);
865 }
866
Santos Cordonf193ba42014-09-12 06:37:39 -0700867 Call getFirstCallWithState(int... states) {
868 return getFirstCallWithState(null, states);
869 }
870
Santos Cordon10838c22014-06-11 17:36:04 -0700871 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700872 * Returns the first call that it finds with the given states. The states are treated as having
873 * priority order so that any call with the first state will be returned before any call with
874 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700875 *
876 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700877 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700878 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700879 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700880 // check the foreground first
881 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
882 return mForegroundCall;
883 }
884
Santos Cordondeb8c892014-05-30 01:38:03 -0700885 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700886 if (Objects.equals(callToSkip, call)) {
887 continue;
888 }
889
890 // Only operate on top-level calls
891 if (call.getParentCall() != null) {
892 continue;
893 }
894
Santos Cordondeb8c892014-05-30 01:38:03 -0700895 if (currentState == call.getState()) {
896 return call;
897 }
898 }
899 }
900 return null;
901 }
902
Santos Cordon0fbe6322014-08-14 04:04:25 -0700903 Call createConferenceCall(
904 PhoneAccountHandle phoneAccount,
905 ParcelableConference parcelableConference) {
906 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700907 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700908 mConnectionServiceRepository,
909 null /* handle */,
910 null /* gatewayInfo */,
911 null /* connectionManagerPhoneAccount */,
912 phoneAccount,
913 false /* isIncoming */,
914 true /* isConference */);
915
916 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700917 if (call.getState() == CallState.ACTIVE) {
918 call.setConnectTimeMillis(System.currentTimeMillis());
919 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700920 call.setCallCapabilities(parcelableConference.getCapabilities());
921
922 // TODO: Move this to be a part of addCall()
923 call.addListener(this);
924 addCall(call);
925 return call;
926 }
927
Yorke Leef86db2e2014-09-12 17:56:48 -0700928 /**
929 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
930 */
931 int getCallState() {
932 return mPhoneStateBroadcaster.getCallState();
933 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700934
Santos Cordon4b2c1192014-03-19 18:15:38 -0700935 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700936 * Retrieves the {@link PhoneAccountRegistrar}.
937 *
938 * @return The {@link PhoneAccountRegistrar}.
939 */
940 PhoneAccountRegistrar getPhoneAccountRegistrar() {
941 return mPhoneAccountRegistrar;
942 }
943
944 /**
945 * Retrieves the {@link MissedCallNotifier}
946 * @return The {@link MissedCallNotifier}.
947 */
948 MissedCallNotifier getMissedCallNotifier() {
949 return mMissedCallNotifier;
950 }
951
952 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800953 * Adds the specified call to the main list of live calls.
954 *
955 * @param call The call to add.
956 */
957 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700958 Log.v(this, "addCall(%s)", call);
959
960 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700961 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700962
Santos Cordondf399862014-08-06 04:39:15 -0700963 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700964 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700965 for (CallsManagerListener listener : mListeners) {
966 listener.onCallAdded(call);
967 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700968 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800969 }
970
Sailesh Nepal810735e2014-03-18 18:15:46 -0700971 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700972 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700973
Santos Cordon672321e2014-08-21 14:38:58 -0700974 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700975 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700976 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700977
978 boolean shouldNotify = false;
979 if (mCalls.contains(call)) {
980 mCalls.remove(call);
981 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700982 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800983
Sailesh Nepale59bb192014-04-01 18:33:59 -0700984 // Only broadcast changes for calls that are being tracked.
985 if (shouldNotify) {
986 for (CallsManagerListener listener : mListeners) {
987 listener.onCallRemoved(call);
988 }
989 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700990 }
991 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800992
Sailesh Nepal810735e2014-03-18 18:15:46 -0700993 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700994 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700995 *
996 * @param call The call.
997 * @param newState The new state of the call.
998 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700999 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001000 if (call == null) {
1001 return;
1002 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001003 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001004 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1005 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001006 if (newState != oldState) {
1007 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1008 // 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 -07001009 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001010 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001011 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001012 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001013 // unexpected transition occurs.
1014 call.setState(newState);
1015
1016 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001017 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001018 for (CallsManagerListener listener : mListeners) {
1019 listener.onCallStateChanged(call, oldState, newState);
1020 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001021 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001022 }
1023 }
1024 }
1025
1026 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001027 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001028 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001029 private void updateForegroundCall() {
1030 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001031 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001032 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001033 // of its state will be foreground by default and instead the connection service should
1034 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001035 // the call should play audio and listen to microphone if it wants.
1036
Santos Cordonf193ba42014-09-12 06:37:39 -07001037 // Only top-level calls can be in foreground
1038 if (call.getParentCall() != null) {
1039 continue;
1040 }
1041
Santos Cordon40f78c22014-04-07 02:11:42 -07001042 // Active calls have priority.
1043 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001044 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001045 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001046 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001047
1048 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001049 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001050 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001051 }
1052 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001053
Sailesh Nepal810735e2014-03-18 18:15:46 -07001054 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001055 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001056 Call oldForegroundCall = mForegroundCall;
1057 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001058
Santos Cordona56f2762014-03-24 15:55:53 -07001059 for (CallsManagerListener listener : mListeners) {
1060 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1061 }
Santos Cordon681663d2014-01-30 04:32:15 -08001062 }
1063 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001064
1065 private boolean isPotentialMMICode(Uri handle) {
1066 return (handle != null && handle.getSchemeSpecificPart() != null
1067 && handle.getSchemeSpecificPart().contains("#"));
1068 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001069
Tyler Gunn5b452df2014-10-01 15:02:58 -07001070 /**
1071 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1072 * MMI codes which can be dialed when one or more calls are in progress.
1073 * <P>
1074 * Checks for numbers formatted similar to the MMI codes defined in:
1075 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1076 * and
1077 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1078 *
1079 * @param handle The URI to call.
1080 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1081 */
1082 private boolean isPotentialInCallMMICode(Uri handle) {
1083 if (handle != null && handle.getSchemeSpecificPart() != null &&
1084 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1085
1086 String dialedNumber = handle.getSchemeSpecificPart();
1087 return (dialedNumber.equals("0") ||
1088 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1089 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1090 dialedNumber.equals("3") ||
1091 dialedNumber.equals("4") ||
1092 dialedNumber.equals("5"));
1093 }
1094 return false;
1095 }
1096
Santos Cordonf193ba42014-09-12 06:37:39 -07001097 private int getNumCallsWithState(int... states) {
1098 int count = 0;
1099 for (int state : states) {
1100 for (Call call : mCalls) {
1101 if (call.getState() == state) {
1102 count++;
1103 }
1104 }
1105 }
1106 return count;
1107 }
1108
1109 private boolean hasMaximumLiveCalls() {
1110 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1111 }
1112
1113 private boolean hasMaximumHoldingCalls() {
1114 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1115 }
1116
1117 private boolean hasMaximumRingingCalls() {
1118 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1119 }
1120
1121 private boolean hasMaximumOutgoingCalls() {
1122 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1123 }
1124
1125 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1126 if (hasMaximumLiveCalls()) {
1127 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1128 // have to change.
1129 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1130
Santos Cordond1766502014-09-26 15:45:39 -07001131 if (call == liveCall) {
1132 // If the call is already the foreground call, then we are golden.
1133 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1134 // state since the call was already populated into the list.
1135 return true;
1136 }
1137
Andrew Leee6595182014-09-23 18:43:05 -07001138 if (hasMaximumOutgoingCalls()) {
1139 // Disconnect the current outgoing call if it's not an emergency call. If the user
1140 // tries to make two outgoing calls to different emergency call numbers, we will try
1141 // to connect the first outgoing call.
1142 if (isEmergency) {
1143 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1144 if (!outgoingCall.isEmergencyCall()) {
1145 outgoingCall.disconnect();
1146 return true;
1147 }
1148 }
1149 return false;
1150 }
1151
Santos Cordonf193ba42014-09-12 06:37:39 -07001152 if (hasMaximumHoldingCalls()) {
1153 // There is no more room for any more calls, unless it's an emergency.
1154 if (isEmergency) {
1155 // Kill the current active call, this is easier then trying to disconnect a
1156 // holding call and hold an active call.
1157 liveCall.disconnect();
1158 return true;
1159 }
1160 return false; // No more room!
1161 }
1162
1163 // We have room for at least one more holding call at this point.
1164
1165 // First thing, if we are trying to make a call with the same phone account as the live
1166 // call, then allow it so that the connection service can make its own decision about
1167 // how to handle the new call relative to the current one.
1168 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1169 return true;
1170 } else if (call.getTargetPhoneAccount() == null) {
1171 // Without a phone account, we can't say reliably that the call will fail.
1172 // If the user chooses the same phone account as the live call, then it's
1173 // still possible that the call can be made (like with CDMA calls not supporting
1174 // hold but they still support adding a call by going immediately into conference
1175 // mode). Return true here and we'll run this code again after user chooses an
1176 // account.
1177 return true;
1178 }
1179
1180 // Try to hold the live call before attempting the new outgoing call.
1181 if (liveCall.can(PhoneCapabilities.HOLD)) {
1182 liveCall.hold();
1183 return true;
1184 }
1185
1186 // The live call cannot be held so we're out of luck here. There's no room.
1187 return false;
1188 }
1189 return true;
1190 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001191
1192 /**
1193 * Dumps the state of the {@link CallsManager}.
1194 *
1195 * @param pw The {@code IndentingPrintWriter} to write the state to.
1196 */
1197 public void dump(IndentingPrintWriter pw) {
1198 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1199 pw.increaseIndent();
1200 if (mCalls != null) {
1201 pw.println("mCalls: ");
1202 pw.increaseIndent();
1203 for (Call call : mCalls) {
1204 pw.println(call);
1205 }
1206 pw.decreaseIndent();
1207 }
1208 pw.decreaseIndent();
1209 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001210}