blob: 2f871f627ee4f1cef0634fad07f05cfa4144ab23 [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;
Tyler Gunn504eb832014-10-13 14:15:27 -070020import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Sailesh Nepalce704b92014-03-17 18:31:43 -070023import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080024import android.os.Bundle;
Santos Cordonf193ba42014-09-12 06:37:39 -070025import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070026import android.telecom.AudioState;
27import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070028import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.GatewayInfo;
30import android.telecom.ParcelableConference;
Tyler Gunn5b452df2014-10-01 15:02:58 -070031import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070032import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070033import android.telecom.PhoneCapabilities;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.TelecomManager;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070035import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080036
Tyler Gunn91d43cf2014-09-17 12:19:39 -070037import com.android.internal.util.IndentingPrintWriter;
Yorke Lee9250e5f2014-10-01 13:39:09 -070038
Sailesh Nepal810735e2014-03-18 18:15:46 -070039import com.google.common.collect.ImmutableCollection;
40import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080041
Jay Shraunera82c8f72014-08-14 15:49:16 -070042import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070043import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070044import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070045import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070046import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070047import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080048
Ben Giladdd8c6082013-12-30 14:44:08 -080049/**
50 * Singleton.
51 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070052 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080053 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070054 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080055 */
Santos Cordon64c7e962014-07-02 15:15:27 -070056public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080057
Santos Cordondf399862014-08-06 04:39:15 -070058 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 interface CallsManagerListener {
60 void onCallAdded(Call call);
61 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070062 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070063 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070064 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 ConnectionServiceWrapper oldService,
66 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070068 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070069 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070070 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070071 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070072 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070073 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070074 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 }
76
Tyler Gunn91d43cf2014-09-17 12:19:39 -070077 /**
78 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
79 */
80 private static CallsManager INSTANCE = null;
81
82 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Santos Cordonf193ba42014-09-12 06:37:39 -070084 private static final int MAXIMUM_LIVE_CALLS = 1;
85 private static final int MAXIMUM_HOLD_CALLS = 1;
86 private static final int MAXIMUM_RINGING_CALLS = 1;
87 private static final int MAXIMUM_OUTGOING_CALLS = 1;
88
89 private static final int[] LIVE_CALL_STATES =
90 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
91
92 private static final int[] OUTGOING_CALL_STATES =
93 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
94
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070096 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
97 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070098 *
99 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
100 * load factor before resizing, 1 means we only expect a single thread to
101 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800102 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700103 private final Set<Call> mCalls = Collections.newSetFromMap(
104 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800105
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700106 private final ConnectionServiceRepository mConnectionServiceRepository;
107 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
108 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700109 private final CallAudioManager mCallAudioManager;
110 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700111 // For this set initial table size to 16 because we add 13 listeners in
112 // the CallsManager constructor.
113 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
114 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700115 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700116 private final WiredHeadsetManager mWiredHeadsetManager;
117 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700118 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700119 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700120 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700121 private final Context mContext;
122 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
123 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700124 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700125
126 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700127 * The call the user is currently interacting with. This is the call that should have audio
128 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800129 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700130 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800131
Santos Cordon766d04f2014-05-06 10:28:25 -0700132 /** Singleton accessor. */
133 static CallsManager getInstance() {
134 return INSTANCE;
135 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800136
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800137 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700138 * Sets the static singleton instance.
139 *
140 * @param instance The instance to set.
141 */
142 static void initialize(CallsManager instance) {
143 INSTANCE = instance;
144 }
145
146 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700147 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800148 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700149 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
150 PhoneAccountRegistrar phoneAccountRegistrar) {
151 mContext = context;
152 mPhoneAccountRegistrar = phoneAccountRegistrar;
153 mMissedCallNotifier = missedCallNotifier;
154 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
155 mWiredHeadsetManager = new WiredHeadsetManager(context);
156 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700157 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700158 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
159 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
160 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
161 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700162 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700163 mCallLogManager = new CallLogManager(context);
164 mInCallController = new InCallController(context);
165 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
166 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
167 context);
Santos Cordonae193062014-05-21 21:21:49 -0700168
Santos Cordondeb8c892014-05-30 01:38:03 -0700169 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700170 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700171 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700172 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700173 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700174 mListeners.add(new RingbackPlayer(this, playerFactory));
175 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700176 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700177 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700178 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700179 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700180 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700181 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800182 }
183
Santos Cordon766d04f2014-05-06 10:28:25 -0700184 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700185 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700186 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700187
Tyler Gunncde2e502014-08-12 14:35:58 -0700188 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700189 if (!mCalls.contains(call)) {
190 // Call was not added previously in startOutgoingCall due to it being a potential MMI
191 // code, so add it now.
192 addCall(call);
193 }
194
195 // The call's ConnectionService has been updated.
196 for (CallsManagerListener listener : mListeners) {
197 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700198 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700199
200 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700201 }
202
203 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700204 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700205 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700206
Andrew Leee6288a72014-10-01 13:50:02 -0700207 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700208 }
209
210 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700211 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700212 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700213 setCallState(incomingCall, CallState.RINGING);
214
215 if (hasMaximumRingingCalls()) {
216 incomingCall.reject(false, null);
217 // since the call was not added to the list of calls, we have to call the missed
218 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700219 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700220 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
221 } else {
222 addCall(incomingCall);
223 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700224 }
225
226 @Override
227 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700228 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700229 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800230 }
231
Ihab Awadcb387ac2014-05-28 16:49:38 -0700232 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700233 public void onSuccessfulUnknownCall(Call call, int callState) {
234 setCallState(call, callState);
235 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
236 addCall(call);
237 }
238
239 @Override
240 public void onFailedUnknownCall(Call call) {
241 Log.i(this, "onFailedUnknownCall for call %s", call);
242 setCallState(call, CallState.DISCONNECTED);
243 call.removeListener(this);
244 }
245
246 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700247 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700248 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700249 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700250 }
251 }
252
Evan Charlton352105c2014-06-03 14:10:54 -0700253 @Override
254 public void onPostDialWait(Call call, String remaining) {
255 mInCallController.onPostDialWait(call, remaining);
256 }
257
Santos Cordona1610702014-06-04 20:22:56 -0700258 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700259 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700260 // parent-child relationship affects which call should be foreground, so do an update.
261 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700262 for (CallsManagerListener listener : mListeners) {
263 listener.onIsConferencedChanged(call);
264 }
265 }
266
267 @Override
268 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700269 // parent-child relationship affects which call should be foreground, so do an update.
270 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700271 for (CallsManagerListener listener : mListeners) {
272 listener.onIsConferencedChanged(call);
273 }
274 }
275
Ihab Awadff7493a2014-06-10 13:47:44 -0700276 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700277 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700278 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700279 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700280 }
281 }
282
Andrew Lee4a796602014-07-11 17:23:03 -0700283 @Override
284 public void onVideoStateChanged(Call call) {
285 for (CallsManagerListener listener : mListeners) {
286 listener.onVideoStateChanged(call);
287 }
288 }
289
Sailesh Nepal810735e2014-03-18 18:15:46 -0700290 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700291 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700292 }
293
294 Call getForegroundCall() {
295 return mForegroundCall;
296 }
297
Santos Cordonae193062014-05-21 21:21:49 -0700298 Ringer getRinger() {
299 return mRinger;
300 }
301
Santos Cordonf3671a62014-05-29 21:51:53 -0700302 InCallController getInCallController() {
303 return mInCallController;
304 }
305
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700306 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700307 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700308 if (call.isEmergencyCall()) {
309 return true;
310 }
311 }
312 return false;
313 }
314
Ihab Awad6fb37c82014-08-07 19:48:57 -0700315 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700316 return mCallAudioManager.getAudioState();
317 }
318
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700319 boolean isTtySupported() {
320 return mTtyManager.isTtySupported();
321 }
322
323 int getCurrentTtyMode() {
324 return mTtyManager.getCurrentTtyMode();
325 }
326
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700327 void addListener(CallsManagerListener listener) {
328 mListeners.add(listener);
329 }
330
331 void removeListener(CallsManagerListener listener) {
332 mListeners.remove(listener);
333 }
334
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800335 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700336 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800337 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700338 * @param phoneAccountHandle The phone account which contains the component name of the
339 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800340 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800341 */
Evan Charlton89176372014-07-19 18:23:09 -0700342 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800343 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700344 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700345 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700346 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700347 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700348 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700349 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700350 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700351 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700352 true /* isIncoming */,
353 false /* isConference */);
354
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700355 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700356 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700357 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700358 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800359 }
360
Yorke Lee9250e5f2014-10-01 13:39:09 -0700361 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
362 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
363 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
364 Call call = new Call(
365 mContext,
366 mConnectionServiceRepository,
367 handle,
368 null /* gatewayInfo */,
369 null /* connectionManagerPhoneAccount */,
370 phoneAccountHandle,
371 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
372 // to the existing connection instead of trying to create a new one.
373 true /* isIncoming */,
374 false /* isConference */);
375 call.setConnectTimeMillis(System.currentTimeMillis());
376 call.setIsUnknown(true);
377 call.setExtras(extras);
378 call.addListener(this);
379 call.startCreateConnection(mPhoneAccountRegistrar);
380 }
381
Nancy Chen0d3076c2014-07-30 14:45:44 -0700382 /**
383 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
384 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700385 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700386 * @param phoneAccountHandle The phone account which contains the component name of the
387 * connection service to use for this call.
388 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700389 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700390 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700391 // Create a call with original handle. The handle may be changed when the call is attached
392 // to a connection service, but in most cases will remain the same.
393 Call call = new Call(
394 mContext,
395 mConnectionServiceRepository,
396 handle,
397 null /* gatewayInfo */,
398 null /* connectionManagerPhoneAccount */,
399 null /* phoneAccountHandle */,
400 false /* isIncoming */,
401 false /* isConference */);
402
403 List<PhoneAccountHandle> accounts =
404 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
405
Nancy Chenbc9ef172014-08-15 17:11:57 -0700406 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
407 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700408 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700409 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700410 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700411 phoneAccountHandle = null;
412 }
413 }
414
415 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700416 // No preset account, check if default exists that supports the URI scheme for the
417 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700418 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700419 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700420 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700421 if (defaultAccountHandle != null) {
422 phoneAccountHandle = defaultAccountHandle;
423 }
424 }
425
Nancy Chen1c5926f2014-09-17 14:44:14 -0700426 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700427
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700428 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
429 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700430 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700431
432 // Do not support any more live calls. Our options are to move a call to hold, disconnect
433 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700434 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700435 // just cancel at this point.
436 return null;
437 }
438
Nancy Chen1ccab202014-09-18 15:28:36 -0700439 if (phoneAccountHandle == null && accounts.size() > 1 && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700440 // This is the state where the user is expected to select an account
441 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700442 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700443 } else {
444 call.setState(CallState.CONNECTING);
445 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700446
Nancy Chen1c5926f2014-09-17 14:44:14 -0700447 call.setExtras(extras);
448
Tyler Gunn504eb832014-10-13 14:15:27 -0700449 // Do not add the call if it is a potential MMI code.
Yorke Leea3484382014-10-16 11:47:21 -0700450 if (isPotentialMMICode(handle) || isPotentialInCallMMICode) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700451 call.addListener(this);
Tyler Gunn5b452df2014-10-01 15:02:58 -0700452 } else {
453 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700454 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700455
456 return call;
457 }
458
Ben Gilada0d9f752014-02-26 11:49:03 -0800459 /**
Yorke Lee33501632014-03-17 19:24:12 -0700460 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800461 *
Yorke Lee33501632014-03-17 19:24:12 -0700462 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700463 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700464 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700465 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700466 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800467 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700468 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
469 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700470 if (call == null) {
471 // don't do anything if the call no longer exists
472 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700473 return;
474 }
475
Nancy Chen201b4372014-09-08 14:18:24 -0700476 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700477
478 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700479 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700480 } else {
481 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
482 Log.pii(uriHandle), Log.pii(handle));
483 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700484
Nancy Chen0d3076c2014-07-30 14:45:44 -0700485 call.setHandle(uriHandle);
486 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700487 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700488 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700489
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700490 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
491 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700492 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700493 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700494 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700495 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700496
Santos Cordonf193ba42014-09-12 06:37:39 -0700497 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700498 // If the account has been set, proceed to place the outgoing call.
499 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700500 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700501 }
Yorke Leef98fb572014-03-05 10:56:55 -0800502 }
503
504 /**
Santos Cordona1610702014-06-04 20:22:56 -0700505 * Attempts to start a conference call for the specified call.
506 *
Santos Cordon12d61822014-07-29 16:02:20 -0700507 * @param call The call to conference.
508 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700509 */
Santos Cordon12d61822014-07-29 16:02:20 -0700510 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700511 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700512 }
513
514 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700515 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
516 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800517 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700518 *
519 * @param call The call to answer.
520 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800521 */
Andrew Lee38931d02014-07-16 10:17:36 -0700522 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700523 if (!mCalls.contains(call)) {
524 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800525 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700526 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700527 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700528 if (mForegroundCall != null && mForegroundCall != call &&
529 (mForegroundCall.isActive() ||
530 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700531 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
532 // This call does not support hold. If it is from a different connection
533 // service, then disconnect it, otherwise allow the connection service to
534 // figure out the right states.
535 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
536 mForegroundCall.disconnect();
537 }
538 } else {
539 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
540 mForegroundCall, call);
541 mForegroundCall.hold();
542 }
Santos Cordondf399862014-08-06 04:39:15 -0700543 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700544 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700545 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700546 }
547
Santos Cordona56f2762014-03-24 15:55:53 -0700548 for (CallsManagerListener listener : mListeners) {
549 listener.onIncomingCallAnswered(call);
550 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800551
Santos Cordon61d0f702014-02-19 02:52:23 -0800552 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700553 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700554 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800555 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800556 }
557
558 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700559 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
560 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800561 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800562 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700563 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700564 if (!mCalls.contains(call)) {
565 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800566 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700567 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700568 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700569 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700570 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800571 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800572 }
573
574 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700575 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700576 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700577 * @param digit The DTMF digit to play.
578 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700579 void playDtmfTone(Call call, char digit) {
580 if (!mCalls.contains(call)) {
581 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700582 } else {
583 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700584 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700585 }
586 }
587
588 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700589 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700590 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700591 void stopDtmfTone(Call call) {
592 if (!mCalls.contains(call)) {
593 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700594 } else {
595 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700596 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700597 }
598 }
599
600 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700601 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700602 */
Evan Charlton352105c2014-06-03 14:10:54 -0700603 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700604 if (!mCalls.contains(call)) {
605 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700606 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700607 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700608 }
609 }
610
611 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700612 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800613 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
614 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800615 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700616 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700617 Log.v(this, "disconnectCall %s", call);
618
Sailesh Nepale59bb192014-04-01 18:33:59 -0700619 if (!mCalls.contains(call)) {
620 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800621 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700622 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800623 call.disconnect();
624 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800625 }
Santos Cordon681663d2014-01-30 04:32:15 -0800626
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700627 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700628 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700629 */
630 void disconnectAllCalls() {
631 Log.v(this, "disconnectAllCalls");
632
633 for (Call call : mCalls) {
634 disconnectCall(call);
635 }
636 }
637
Nancy Chenbc9ef172014-08-15 17:11:57 -0700638
Tyler Gunn61b92102014-08-19 07:42:20 -0700639 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700640 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700641 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
642 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700643 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700644 void holdCall(Call call) {
645 if (!mCalls.contains(call)) {
646 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700647 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700648 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700649 call.hold();
650 }
651 }
652
653 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700654 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700655 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
656 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700657 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700658 void unholdCall(Call call) {
659 if (!mCalls.contains(call)) {
660 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700661 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700662 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700663 for (Call c : mCalls) {
664 if (c != null && c.isAlive() && c != call) {
665 c.hold();
666 }
667 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700668 call.unhold();
669 }
670 }
671
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700672 /** Called by the in-call UI to change the mute state. */
673 void mute(boolean shouldMute) {
674 mCallAudioManager.mute(shouldMute);
675 }
676
677 /**
678 * Called by the in-call UI to change the audio route, for example to change from earpiece to
679 * speaker phone.
680 */
681 void setAudioRoute(int route) {
682 mCallAudioManager.setAudioRoute(route);
683 }
684
Yorke Leed1346872014-07-28 14:38:45 -0700685 /** Called by the in-call UI to turn the proximity sensor on. */
686 void turnOnProximitySensor() {
687 mProximitySensorManager.turnOn();
688 }
689
690 /**
691 * Called by the in-call UI to turn the proximity sensor off.
692 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
693 * the screen will be kept off until the proximity sensor goes negative.
694 */
695 void turnOffProximitySensor(boolean screenOnImmediately) {
696 mProximitySensorManager.turnOff(screenOnImmediately);
697 }
698
Evan Charlton89176372014-07-19 18:23:09 -0700699 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700700 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700701 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700702 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700703 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
704 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
705 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
706 // respecting a rewritten number or a canceled number. This is unlikely since
707 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
708 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700709 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700710
711 // Note: emergency calls never go through account selection dialog so they never
712 // arrive here.
713 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700714 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700715 } else {
716 call.disconnect();
717 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700718 }
719 }
720
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700721 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700722 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700723 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700724 for (CallsManagerListener listener : mListeners) {
725 listener.onAudioStateChanged(oldAudioState, newAudioState);
726 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700727 }
728
Sailesh Nepale59bb192014-04-01 18:33:59 -0700729 void markCallAsRinging(Call call) {
730 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800731 }
732
Sailesh Nepale59bb192014-04-01 18:33:59 -0700733 void markCallAsDialing(Call call) {
734 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800735 }
736
Sailesh Nepale59bb192014-04-01 18:33:59 -0700737 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700738 if (call.getConnectTimeMillis() == 0) {
739 call.setConnectTimeMillis(System.currentTimeMillis());
740 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700741 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700742
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700743 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700744 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700745 }
Santos Cordon681663d2014-01-30 04:32:15 -0800746 }
747
Sailesh Nepale59bb192014-04-01 18:33:59 -0700748 void markCallAsOnHold(Call call) {
749 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700750 }
751
Santos Cordon049b7b62014-01-30 05:34:26 -0800752 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700753 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
754 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800755 *
Andrew Lee701dc002014-09-11 21:29:12 -0700756 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800757 */
Andrew Lee701dc002014-09-11 21:29:12 -0700758 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
759 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700760 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700761 }
762
Ben Gilada0d9f752014-02-26 11:49:03 -0800763 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700764 * Removes an existing disconnected call, and notifies the in-call app.
765 */
766 void markCallAsRemoved(Call call) {
767 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700768 if (mLocallyDisconnectingCalls.contains(call)) {
769 mLocallyDisconnectingCalls.remove(call);
770 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
771 mForegroundCall.unhold();
772 }
773 }
Ihab Awada02bef52014-08-13 18:18:42 -0700774 }
775
776 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700777 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700778 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700779 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700780 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700781 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700782 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700783 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700784 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700785 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700786 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700787 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700788 }
789 }
790 }
791
Santos Cordondeb8c892014-05-30 01:38:03 -0700792 boolean hasAnyCalls() {
793 return !mCalls.isEmpty();
794 }
795
Santos Cordonc7e85d42014-05-22 02:51:48 -0700796 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700797 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700798 }
799
800 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700801 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700802 }
803
Santos Cordondeb8c892014-05-30 01:38:03 -0700804 boolean onMediaButton(int type) {
805 if (hasAnyCalls()) {
806 if (HeadsetMediaButton.SHORT_PRESS == type) {
807 Call ringingCall = getFirstCallWithState(CallState.RINGING);
808 if (ringingCall == null) {
809 mCallAudioManager.toggleMute();
810 return true;
811 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700812 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700813 return true;
814 }
815 } else if (HeadsetMediaButton.LONG_PRESS == type) {
816 Log.d(this, "handleHeadsetHook: longpress -> hangup");
817 Call callToHangup = getFirstCallWithState(
818 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
819 if (callToHangup != null) {
820 callToHangup.disconnect();
821 return true;
822 }
823 }
824 }
825 return false;
826 }
827
828 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700829 * Checks to see if the specified call is the only high-level call and if so, enable the
830 * "Add-call" button. We allow you to add a second call but not a third or beyond.
831 *
832 * @param call The call to test for add-call.
833 * @return Whether the add-call feature should be enabled for the call.
834 */
835 protected boolean isAddCallCapable(Call call) {
836 if (call.getParentCall() != null) {
837 // Never true for child calls.
838 return false;
839 }
840
Andrew Leec11fcd92014-09-19 18:31:46 -0700841 // Use canManageConference as a mechanism to check if the call is CDMA.
842 // Disable "Add Call" for CDMA calls which are conference calls.
843 boolean canManageConference = PhoneCapabilities.MANAGE_CONFERENCE
844 == (call.getCallCapabilities() & PhoneCapabilities.MANAGE_CONFERENCE);
845 if (call.isConference() && !canManageConference) {
846 return false;
847 }
848
Santos Cordon10838c22014-06-11 17:36:04 -0700849 // Loop through all the other calls and there exists a top level (has no parent) call
850 // that is not the specified call, return false.
851 for (Call otherCall : mCalls) {
852 if (call != otherCall && otherCall.getParentCall() == null) {
853 return false;
854 }
855 }
856 return true;
857 }
858
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700859 Call getRingingCall() {
860 return getFirstCallWithState(CallState.RINGING);
861 }
862
863 Call getActiveCall() {
864 return getFirstCallWithState(CallState.ACTIVE);
865 }
866
Nancy Chen05a9e402014-09-26 14:14:32 -0700867 Call getDialingCall() {
868 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700869 }
870
871 Call getHeldCall() {
872 return getFirstCallWithState(CallState.ON_HOLD);
873 }
874
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700875 int getNumHeldCalls() {
876 int count = 0;
877 for (Call call : mCalls) {
878 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
879 count++;
880 }
881 }
882 return count;
883 }
884
Santos Cordonf193ba42014-09-12 06:37:39 -0700885 Call getFirstCallWithState(int... states) {
886 return getFirstCallWithState(null, states);
887 }
888
Santos Cordon10838c22014-06-11 17:36:04 -0700889 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700890 * Returns the first call that it finds with the given states. The states are treated as having
891 * priority order so that any call with the first state will be returned before any call with
892 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700893 *
894 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700895 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700896 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700897 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700898 // check the foreground first
899 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
900 return mForegroundCall;
901 }
902
Santos Cordondeb8c892014-05-30 01:38:03 -0700903 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700904 if (Objects.equals(callToSkip, call)) {
905 continue;
906 }
907
908 // Only operate on top-level calls
909 if (call.getParentCall() != null) {
910 continue;
911 }
912
Santos Cordondeb8c892014-05-30 01:38:03 -0700913 if (currentState == call.getState()) {
914 return call;
915 }
916 }
917 }
918 return null;
919 }
920
Santos Cordon0fbe6322014-08-14 04:04:25 -0700921 Call createConferenceCall(
922 PhoneAccountHandle phoneAccount,
923 ParcelableConference parcelableConference) {
924 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700925 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700926 mConnectionServiceRepository,
927 null /* handle */,
928 null /* gatewayInfo */,
929 null /* connectionManagerPhoneAccount */,
930 phoneAccount,
931 false /* isIncoming */,
932 true /* isConference */);
933
934 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700935 if (call.getState() == CallState.ACTIVE) {
936 call.setConnectTimeMillis(System.currentTimeMillis());
937 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700938 call.setCallCapabilities(parcelableConference.getCapabilities());
939
940 // TODO: Move this to be a part of addCall()
941 call.addListener(this);
942 addCall(call);
943 return call;
944 }
945
Yorke Leef86db2e2014-09-12 17:56:48 -0700946 /**
947 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
948 */
949 int getCallState() {
950 return mPhoneStateBroadcaster.getCallState();
951 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700952
Santos Cordon4b2c1192014-03-19 18:15:38 -0700953 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700954 * Retrieves the {@link PhoneAccountRegistrar}.
955 *
956 * @return The {@link PhoneAccountRegistrar}.
957 */
958 PhoneAccountRegistrar getPhoneAccountRegistrar() {
959 return mPhoneAccountRegistrar;
960 }
961
962 /**
963 * Retrieves the {@link MissedCallNotifier}
964 * @return The {@link MissedCallNotifier}.
965 */
966 MissedCallNotifier getMissedCallNotifier() {
967 return mMissedCallNotifier;
968 }
969
970 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800971 * Adds the specified call to the main list of live calls.
972 *
973 * @param call The call to add.
974 */
975 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700976 Log.v(this, "addCall(%s)", call);
977
978 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700979 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700980
Santos Cordondf399862014-08-06 04:39:15 -0700981 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700982 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700983 for (CallsManagerListener listener : mListeners) {
984 listener.onCallAdded(call);
985 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700986 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800987 }
988
Sailesh Nepal810735e2014-03-18 18:15:46 -0700989 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700990 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700991
Santos Cordon672321e2014-08-21 14:38:58 -0700992 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700993 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700994 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700995
996 boolean shouldNotify = false;
997 if (mCalls.contains(call)) {
998 mCalls.remove(call);
999 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001000 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001001
Sailesh Nepale59bb192014-04-01 18:33:59 -07001002 // Only broadcast changes for calls that are being tracked.
1003 if (shouldNotify) {
1004 for (CallsManagerListener listener : mListeners) {
1005 listener.onCallRemoved(call);
1006 }
1007 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001008 }
1009 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001010
Sailesh Nepal810735e2014-03-18 18:15:46 -07001011 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001012 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001013 *
1014 * @param call The call.
1015 * @param newState The new state of the call.
1016 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001017 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001018 if (call == null) {
1019 return;
1020 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001021 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001022 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1023 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001024 if (newState != oldState) {
1025 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1026 // 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 -07001027 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001028 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001029 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001030 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001031 // unexpected transition occurs.
1032 call.setState(newState);
1033
1034 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001035 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001036 for (CallsManagerListener listener : mListeners) {
1037 listener.onCallStateChanged(call, oldState, newState);
1038 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001039 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001040 }
1041 }
1042 }
1043
1044 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001045 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001046 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001047 private void updateForegroundCall() {
1048 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001049 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001050 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001051 // of its state will be foreground by default and instead the connection service should
1052 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001053 // the call should play audio and listen to microphone if it wants.
1054
Santos Cordonf193ba42014-09-12 06:37:39 -07001055 // Only top-level calls can be in foreground
1056 if (call.getParentCall() != null) {
1057 continue;
1058 }
1059
Santos Cordon40f78c22014-04-07 02:11:42 -07001060 // Active calls have priority.
1061 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001062 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001063 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001064 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001065
1066 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001067 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001068 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001069 }
1070 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001071
Sailesh Nepal810735e2014-03-18 18:15:46 -07001072 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001073 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001074 Call oldForegroundCall = mForegroundCall;
1075 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001076
Santos Cordona56f2762014-03-24 15:55:53 -07001077 for (CallsManagerListener listener : mListeners) {
1078 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1079 }
Santos Cordon681663d2014-01-30 04:32:15 -08001080 }
1081 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001082
1083 private boolean isPotentialMMICode(Uri handle) {
1084 return (handle != null && handle.getSchemeSpecificPart() != null
1085 && handle.getSchemeSpecificPart().contains("#"));
1086 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001087
Tyler Gunn5b452df2014-10-01 15:02:58 -07001088 /**
1089 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1090 * MMI codes which can be dialed when one or more calls are in progress.
1091 * <P>
1092 * Checks for numbers formatted similar to the MMI codes defined in:
1093 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1094 * and
1095 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1096 *
1097 * @param handle The URI to call.
1098 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1099 */
1100 private boolean isPotentialInCallMMICode(Uri handle) {
1101 if (handle != null && handle.getSchemeSpecificPart() != null &&
1102 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1103
1104 String dialedNumber = handle.getSchemeSpecificPart();
1105 return (dialedNumber.equals("0") ||
1106 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1107 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1108 dialedNumber.equals("3") ||
1109 dialedNumber.equals("4") ||
1110 dialedNumber.equals("5"));
1111 }
1112 return false;
1113 }
1114
Santos Cordonf193ba42014-09-12 06:37:39 -07001115 private int getNumCallsWithState(int... states) {
1116 int count = 0;
1117 for (int state : states) {
1118 for (Call call : mCalls) {
1119 if (call.getState() == state) {
1120 count++;
1121 }
1122 }
1123 }
1124 return count;
1125 }
1126
1127 private boolean hasMaximumLiveCalls() {
1128 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1129 }
1130
1131 private boolean hasMaximumHoldingCalls() {
1132 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1133 }
1134
1135 private boolean hasMaximumRingingCalls() {
1136 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1137 }
1138
1139 private boolean hasMaximumOutgoingCalls() {
1140 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1141 }
1142
1143 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1144 if (hasMaximumLiveCalls()) {
1145 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1146 // have to change.
1147 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1148
Santos Cordond1766502014-09-26 15:45:39 -07001149 if (call == liveCall) {
1150 // If the call is already the foreground call, then we are golden.
1151 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1152 // state since the call was already populated into the list.
1153 return true;
1154 }
1155
Andrew Leee6595182014-09-23 18:43:05 -07001156 if (hasMaximumOutgoingCalls()) {
1157 // Disconnect the current outgoing call if it's not an emergency call. If the user
1158 // tries to make two outgoing calls to different emergency call numbers, we will try
1159 // to connect the first outgoing call.
1160 if (isEmergency) {
1161 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1162 if (!outgoingCall.isEmergencyCall()) {
1163 outgoingCall.disconnect();
1164 return true;
1165 }
1166 }
1167 return false;
1168 }
1169
Santos Cordonf193ba42014-09-12 06:37:39 -07001170 if (hasMaximumHoldingCalls()) {
1171 // There is no more room for any more calls, unless it's an emergency.
1172 if (isEmergency) {
1173 // Kill the current active call, this is easier then trying to disconnect a
1174 // holding call and hold an active call.
1175 liveCall.disconnect();
1176 return true;
1177 }
1178 return false; // No more room!
1179 }
1180
1181 // We have room for at least one more holding call at this point.
1182
1183 // First thing, if we are trying to make a call with the same phone account as the live
1184 // call, then allow it so that the connection service can make its own decision about
1185 // how to handle the new call relative to the current one.
1186 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1187 return true;
1188 } else if (call.getTargetPhoneAccount() == null) {
1189 // Without a phone account, we can't say reliably that the call will fail.
1190 // If the user chooses the same phone account as the live call, then it's
1191 // still possible that the call can be made (like with CDMA calls not supporting
1192 // hold but they still support adding a call by going immediately into conference
1193 // mode). Return true here and we'll run this code again after user chooses an
1194 // account.
1195 return true;
1196 }
1197
1198 // Try to hold the live call before attempting the new outgoing call.
1199 if (liveCall.can(PhoneCapabilities.HOLD)) {
1200 liveCall.hold();
1201 return true;
1202 }
1203
1204 // The live call cannot be held so we're out of luck here. There's no room.
1205 return false;
1206 }
1207 return true;
1208 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001209
1210 /**
1211 * Dumps the state of the {@link CallsManager}.
1212 *
1213 * @param pw The {@code IndentingPrintWriter} to write the state to.
1214 */
1215 public void dump(IndentingPrintWriter pw) {
1216 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1217 pw.increaseIndent();
1218 if (mCalls != null) {
1219 pw.println("mCalls: ");
1220 pw.increaseIndent();
1221 for (Call call : mCalls) {
1222 pw.println(call);
1223 }
1224 pw.decreaseIndent();
1225 }
1226 pw.decreaseIndent();
1227 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001228}