blob: f1fdfdea4b3c1d5c7b51cca682a6fd7808f55699 [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 Chen8d92f452014-10-20 21:43:44 -0700439 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
440 !isEmergencyCall;
441
442 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700443 // This is the state where the user is expected to select an account
444 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700445 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700446 } else {
447 call.setState(CallState.CONNECTING);
448 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700449
Nancy Chen1c5926f2014-09-17 14:44:14 -0700450 call.setExtras(extras);
451
Tyler Gunn504eb832014-10-13 14:15:27 -0700452 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700453 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700454 call.addListener(this);
Tyler Gunn5b452df2014-10-01 15:02:58 -0700455 } else {
456 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700457 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700458
459 return call;
460 }
461
Ben Gilada0d9f752014-02-26 11:49:03 -0800462 /**
Yorke Lee33501632014-03-17 19:24:12 -0700463 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800464 *
Yorke Lee33501632014-03-17 19:24:12 -0700465 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700466 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700467 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700468 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700469 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800470 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700471 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
472 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700473 if (call == null) {
474 // don't do anything if the call no longer exists
475 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700476 return;
477 }
478
Nancy Chen201b4372014-09-08 14:18:24 -0700479 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700480
481 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700482 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700483 } else {
484 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
485 Log.pii(uriHandle), Log.pii(handle));
486 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700487
Nancy Chen0d3076c2014-07-30 14:45:44 -0700488 call.setHandle(uriHandle);
489 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700490 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700491 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700492
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700493 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
494 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700495 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700496 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700497 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700498 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700499
Santos Cordonf193ba42014-09-12 06:37:39 -0700500 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700501 // If the account has been set, proceed to place the outgoing call.
502 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700503 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700504 }
Yorke Leef98fb572014-03-05 10:56:55 -0800505 }
506
507 /**
Santos Cordona1610702014-06-04 20:22:56 -0700508 * Attempts to start a conference call for the specified call.
509 *
Santos Cordon12d61822014-07-29 16:02:20 -0700510 * @param call The call to conference.
511 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700512 */
Santos Cordon12d61822014-07-29 16:02:20 -0700513 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700514 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700515 }
516
517 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700518 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
519 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800520 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700521 *
522 * @param call The call to answer.
523 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800524 */
Andrew Lee38931d02014-07-16 10:17:36 -0700525 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700526 if (!mCalls.contains(call)) {
527 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800528 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700529 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700530 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700531 if (mForegroundCall != null && mForegroundCall != call &&
532 (mForegroundCall.isActive() ||
533 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700534 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
535 // This call does not support hold. If it is from a different connection
536 // service, then disconnect it, otherwise allow the connection service to
537 // figure out the right states.
538 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
539 mForegroundCall.disconnect();
540 }
541 } else {
542 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
543 mForegroundCall, call);
544 mForegroundCall.hold();
545 }
Santos Cordondf399862014-08-06 04:39:15 -0700546 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700547 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700548 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700549 }
550
Santos Cordona56f2762014-03-24 15:55:53 -0700551 for (CallsManagerListener listener : mListeners) {
552 listener.onIncomingCallAnswered(call);
553 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800554
Santos Cordon61d0f702014-02-19 02:52:23 -0800555 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700556 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700557 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800558 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800559 }
560
561 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700562 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
563 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800564 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800565 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700566 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700567 if (!mCalls.contains(call)) {
568 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800569 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700570 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700571 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700572 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700573 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800574 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800575 }
576
577 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700578 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700579 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700580 * @param digit The DTMF digit to play.
581 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700582 void playDtmfTone(Call call, char digit) {
583 if (!mCalls.contains(call)) {
584 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700585 } else {
586 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700587 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700588 }
589 }
590
591 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700592 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700593 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700594 void stopDtmfTone(Call call) {
595 if (!mCalls.contains(call)) {
596 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700597 } else {
598 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700599 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700600 }
601 }
602
603 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700604 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700605 */
Evan Charlton352105c2014-06-03 14:10:54 -0700606 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700607 if (!mCalls.contains(call)) {
608 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700609 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700610 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700611 }
612 }
613
614 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700615 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800616 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
617 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800618 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700619 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700620 Log.v(this, "disconnectCall %s", call);
621
Sailesh Nepale59bb192014-04-01 18:33:59 -0700622 if (!mCalls.contains(call)) {
623 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800624 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700625 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800626 call.disconnect();
627 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800628 }
Santos Cordon681663d2014-01-30 04:32:15 -0800629
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700630 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700631 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700632 */
633 void disconnectAllCalls() {
634 Log.v(this, "disconnectAllCalls");
635
636 for (Call call : mCalls) {
637 disconnectCall(call);
638 }
639 }
640
Nancy Chenbc9ef172014-08-15 17:11:57 -0700641
Tyler Gunn61b92102014-08-19 07:42:20 -0700642 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700643 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700644 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
645 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700646 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700647 void holdCall(Call call) {
648 if (!mCalls.contains(call)) {
649 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700650 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700651 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700652 call.hold();
653 }
654 }
655
656 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700657 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700658 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
659 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700660 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700661 void unholdCall(Call call) {
662 if (!mCalls.contains(call)) {
663 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700664 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700665 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700666 for (Call c : mCalls) {
667 if (c != null && c.isAlive() && c != call) {
668 c.hold();
669 }
670 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700671 call.unhold();
672 }
673 }
674
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700675 /** Called by the in-call UI to change the mute state. */
676 void mute(boolean shouldMute) {
677 mCallAudioManager.mute(shouldMute);
678 }
679
680 /**
681 * Called by the in-call UI to change the audio route, for example to change from earpiece to
682 * speaker phone.
683 */
684 void setAudioRoute(int route) {
685 mCallAudioManager.setAudioRoute(route);
686 }
687
Yorke Leed1346872014-07-28 14:38:45 -0700688 /** Called by the in-call UI to turn the proximity sensor on. */
689 void turnOnProximitySensor() {
690 mProximitySensorManager.turnOn();
691 }
692
693 /**
694 * Called by the in-call UI to turn the proximity sensor off.
695 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
696 * the screen will be kept off until the proximity sensor goes negative.
697 */
698 void turnOffProximitySensor(boolean screenOnImmediately) {
699 mProximitySensorManager.turnOff(screenOnImmediately);
700 }
701
Evan Charlton89176372014-07-19 18:23:09 -0700702 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700703 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700704 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700705 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700706 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
707 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
708 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
709 // respecting a rewritten number or a canceled number. This is unlikely since
710 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
711 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700712 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700713
714 // Note: emergency calls never go through account selection dialog so they never
715 // arrive here.
716 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700717 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700718 } else {
719 call.disconnect();
720 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700721 }
722 }
723
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700724 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700725 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700726 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700727 for (CallsManagerListener listener : mListeners) {
728 listener.onAudioStateChanged(oldAudioState, newAudioState);
729 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700730 }
731
Sailesh Nepale59bb192014-04-01 18:33:59 -0700732 void markCallAsRinging(Call call) {
733 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800734 }
735
Sailesh Nepale59bb192014-04-01 18:33:59 -0700736 void markCallAsDialing(Call call) {
737 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800738 }
739
Sailesh Nepale59bb192014-04-01 18:33:59 -0700740 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700741 if (call.getConnectTimeMillis() == 0) {
742 call.setConnectTimeMillis(System.currentTimeMillis());
743 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700744 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700745
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700746 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700747 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700748 }
Santos Cordon681663d2014-01-30 04:32:15 -0800749 }
750
Sailesh Nepale59bb192014-04-01 18:33:59 -0700751 void markCallAsOnHold(Call call) {
752 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700753 }
754
Santos Cordon049b7b62014-01-30 05:34:26 -0800755 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700756 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
757 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800758 *
Andrew Lee701dc002014-09-11 21:29:12 -0700759 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800760 */
Andrew Lee701dc002014-09-11 21:29:12 -0700761 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
762 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700763 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700764 }
765
Ben Gilada0d9f752014-02-26 11:49:03 -0800766 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700767 * Removes an existing disconnected call, and notifies the in-call app.
768 */
769 void markCallAsRemoved(Call call) {
770 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700771 if (mLocallyDisconnectingCalls.contains(call)) {
772 mLocallyDisconnectingCalls.remove(call);
773 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
774 mForegroundCall.unhold();
775 }
776 }
Ihab Awada02bef52014-08-13 18:18:42 -0700777 }
778
779 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700780 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700781 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700782 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700783 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700784 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700785 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700786 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700787 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700788 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700789 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700790 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700791 }
792 }
793 }
794
Santos Cordondeb8c892014-05-30 01:38:03 -0700795 boolean hasAnyCalls() {
796 return !mCalls.isEmpty();
797 }
798
Santos Cordonc7e85d42014-05-22 02:51:48 -0700799 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700800 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700801 }
802
803 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700804 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700805 }
806
Santos Cordondeb8c892014-05-30 01:38:03 -0700807 boolean onMediaButton(int type) {
808 if (hasAnyCalls()) {
809 if (HeadsetMediaButton.SHORT_PRESS == type) {
810 Call ringingCall = getFirstCallWithState(CallState.RINGING);
811 if (ringingCall == null) {
812 mCallAudioManager.toggleMute();
813 return true;
814 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700815 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700816 return true;
817 }
818 } else if (HeadsetMediaButton.LONG_PRESS == type) {
819 Log.d(this, "handleHeadsetHook: longpress -> hangup");
820 Call callToHangup = getFirstCallWithState(
821 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
822 if (callToHangup != null) {
823 callToHangup.disconnect();
824 return true;
825 }
826 }
827 }
828 return false;
829 }
830
831 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700832 * Checks to see if the specified call is the only high-level call and if so, enable the
833 * "Add-call" button. We allow you to add a second call but not a third or beyond.
834 *
835 * @param call The call to test for add-call.
836 * @return Whether the add-call feature should be enabled for the call.
837 */
838 protected boolean isAddCallCapable(Call call) {
839 if (call.getParentCall() != null) {
840 // Never true for child calls.
841 return false;
842 }
843
Andrew Leec11fcd92014-09-19 18:31:46 -0700844 // Use canManageConference as a mechanism to check if the call is CDMA.
845 // Disable "Add Call" for CDMA calls which are conference calls.
846 boolean canManageConference = PhoneCapabilities.MANAGE_CONFERENCE
847 == (call.getCallCapabilities() & PhoneCapabilities.MANAGE_CONFERENCE);
848 if (call.isConference() && !canManageConference) {
849 return false;
850 }
851
Santos Cordon10838c22014-06-11 17:36:04 -0700852 // Loop through all the other calls and there exists a top level (has no parent) call
853 // that is not the specified call, return false.
854 for (Call otherCall : mCalls) {
855 if (call != otherCall && otherCall.getParentCall() == null) {
856 return false;
857 }
858 }
859 return true;
860 }
861
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700862 Call getRingingCall() {
863 return getFirstCallWithState(CallState.RINGING);
864 }
865
866 Call getActiveCall() {
867 return getFirstCallWithState(CallState.ACTIVE);
868 }
869
Nancy Chen05a9e402014-09-26 14:14:32 -0700870 Call getDialingCall() {
871 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700872 }
873
874 Call getHeldCall() {
875 return getFirstCallWithState(CallState.ON_HOLD);
876 }
877
Santos Cordonf193ba42014-09-12 06:37:39 -0700878 Call getFirstCallWithState(int... states) {
879 return getFirstCallWithState(null, states);
880 }
881
Santos Cordon10838c22014-06-11 17:36:04 -0700882 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700883 * Returns the first call that it finds with the given states. The states are treated as having
884 * priority order so that any call with the first state will be returned before any call with
885 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700886 *
887 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700888 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700889 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700890 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700891 // check the foreground first
892 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
893 return mForegroundCall;
894 }
895
Santos Cordondeb8c892014-05-30 01:38:03 -0700896 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700897 if (Objects.equals(callToSkip, call)) {
898 continue;
899 }
900
901 // Only operate on top-level calls
902 if (call.getParentCall() != null) {
903 continue;
904 }
905
Santos Cordondeb8c892014-05-30 01:38:03 -0700906 if (currentState == call.getState()) {
907 return call;
908 }
909 }
910 }
911 return null;
912 }
913
Santos Cordon0fbe6322014-08-14 04:04:25 -0700914 Call createConferenceCall(
915 PhoneAccountHandle phoneAccount,
916 ParcelableConference parcelableConference) {
917 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700918 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700919 mConnectionServiceRepository,
920 null /* handle */,
921 null /* gatewayInfo */,
922 null /* connectionManagerPhoneAccount */,
923 phoneAccount,
924 false /* isIncoming */,
925 true /* isConference */);
926
927 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700928 if (call.getState() == CallState.ACTIVE) {
929 call.setConnectTimeMillis(System.currentTimeMillis());
930 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700931 call.setCallCapabilities(parcelableConference.getCapabilities());
932
933 // TODO: Move this to be a part of addCall()
934 call.addListener(this);
935 addCall(call);
936 return call;
937 }
938
Yorke Leef86db2e2014-09-12 17:56:48 -0700939 /**
940 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
941 */
942 int getCallState() {
943 return mPhoneStateBroadcaster.getCallState();
944 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700945
Santos Cordon4b2c1192014-03-19 18:15:38 -0700946 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700947 * Retrieves the {@link PhoneAccountRegistrar}.
948 *
949 * @return The {@link PhoneAccountRegistrar}.
950 */
951 PhoneAccountRegistrar getPhoneAccountRegistrar() {
952 return mPhoneAccountRegistrar;
953 }
954
955 /**
956 * Retrieves the {@link MissedCallNotifier}
957 * @return The {@link MissedCallNotifier}.
958 */
959 MissedCallNotifier getMissedCallNotifier() {
960 return mMissedCallNotifier;
961 }
962
963 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800964 * Adds the specified call to the main list of live calls.
965 *
966 * @param call The call to add.
967 */
968 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700969 Log.v(this, "addCall(%s)", call);
970
971 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700972 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700973
Santos Cordondf399862014-08-06 04:39:15 -0700974 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700975 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700976 for (CallsManagerListener listener : mListeners) {
977 listener.onCallAdded(call);
978 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700979 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800980 }
981
Sailesh Nepal810735e2014-03-18 18:15:46 -0700982 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700983 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700984
Santos Cordon672321e2014-08-21 14:38:58 -0700985 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700986 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700987 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700988
989 boolean shouldNotify = false;
990 if (mCalls.contains(call)) {
991 mCalls.remove(call);
992 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700993 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800994
Sailesh Nepale59bb192014-04-01 18:33:59 -0700995 // Only broadcast changes for calls that are being tracked.
996 if (shouldNotify) {
997 for (CallsManagerListener listener : mListeners) {
998 listener.onCallRemoved(call);
999 }
1000 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001001 }
1002 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001003
Sailesh Nepal810735e2014-03-18 18:15:46 -07001004 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001005 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001006 *
1007 * @param call The call.
1008 * @param newState The new state of the call.
1009 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001010 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001011 if (call == null) {
1012 return;
1013 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001014 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001015 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1016 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001017 if (newState != oldState) {
1018 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1019 // 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 -07001020 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001021 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001022 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001023 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001024 // unexpected transition occurs.
1025 call.setState(newState);
1026
1027 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001028 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001029 for (CallsManagerListener listener : mListeners) {
1030 listener.onCallStateChanged(call, oldState, newState);
1031 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001032 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001033 }
1034 }
1035 }
1036
1037 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001038 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001039 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001040 private void updateForegroundCall() {
1041 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001042 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001043 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001044 // of its state will be foreground by default and instead the connection service should
1045 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001046 // the call should play audio and listen to microphone if it wants.
1047
Santos Cordonf193ba42014-09-12 06:37:39 -07001048 // Only top-level calls can be in foreground
1049 if (call.getParentCall() != null) {
1050 continue;
1051 }
1052
Santos Cordon40f78c22014-04-07 02:11:42 -07001053 // Active calls have priority.
1054 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001055 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001056 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001057 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001058
1059 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001060 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001061 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001062 }
1063 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001064
Sailesh Nepal810735e2014-03-18 18:15:46 -07001065 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001066 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001067 Call oldForegroundCall = mForegroundCall;
1068 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001069
Santos Cordona56f2762014-03-24 15:55:53 -07001070 for (CallsManagerListener listener : mListeners) {
1071 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1072 }
Santos Cordon681663d2014-01-30 04:32:15 -08001073 }
1074 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001075
1076 private boolean isPotentialMMICode(Uri handle) {
1077 return (handle != null && handle.getSchemeSpecificPart() != null
1078 && handle.getSchemeSpecificPart().contains("#"));
1079 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001080
Tyler Gunn5b452df2014-10-01 15:02:58 -07001081 /**
1082 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1083 * MMI codes which can be dialed when one or more calls are in progress.
1084 * <P>
1085 * Checks for numbers formatted similar to the MMI codes defined in:
1086 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1087 * and
1088 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1089 *
1090 * @param handle The URI to call.
1091 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1092 */
1093 private boolean isPotentialInCallMMICode(Uri handle) {
1094 if (handle != null && handle.getSchemeSpecificPart() != null &&
1095 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1096
1097 String dialedNumber = handle.getSchemeSpecificPart();
1098 return (dialedNumber.equals("0") ||
1099 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1100 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1101 dialedNumber.equals("3") ||
1102 dialedNumber.equals("4") ||
1103 dialedNumber.equals("5"));
1104 }
1105 return false;
1106 }
1107
Santos Cordonf193ba42014-09-12 06:37:39 -07001108 private int getNumCallsWithState(int... states) {
1109 int count = 0;
1110 for (int state : states) {
1111 for (Call call : mCalls) {
1112 if (call.getState() == state) {
1113 count++;
1114 }
1115 }
1116 }
1117 return count;
1118 }
1119
1120 private boolean hasMaximumLiveCalls() {
1121 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1122 }
1123
1124 private boolean hasMaximumHoldingCalls() {
1125 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1126 }
1127
1128 private boolean hasMaximumRingingCalls() {
1129 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1130 }
1131
1132 private boolean hasMaximumOutgoingCalls() {
1133 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1134 }
1135
1136 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1137 if (hasMaximumLiveCalls()) {
1138 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1139 // have to change.
1140 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1141
Santos Cordond1766502014-09-26 15:45:39 -07001142 if (call == liveCall) {
1143 // If the call is already the foreground call, then we are golden.
1144 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1145 // state since the call was already populated into the list.
1146 return true;
1147 }
1148
Andrew Leee6595182014-09-23 18:43:05 -07001149 if (hasMaximumOutgoingCalls()) {
1150 // Disconnect the current outgoing call if it's not an emergency call. If the user
1151 // tries to make two outgoing calls to different emergency call numbers, we will try
1152 // to connect the first outgoing call.
1153 if (isEmergency) {
1154 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1155 if (!outgoingCall.isEmergencyCall()) {
1156 outgoingCall.disconnect();
1157 return true;
1158 }
1159 }
1160 return false;
1161 }
1162
Santos Cordonf193ba42014-09-12 06:37:39 -07001163 if (hasMaximumHoldingCalls()) {
1164 // There is no more room for any more calls, unless it's an emergency.
1165 if (isEmergency) {
1166 // Kill the current active call, this is easier then trying to disconnect a
1167 // holding call and hold an active call.
1168 liveCall.disconnect();
1169 return true;
1170 }
1171 return false; // No more room!
1172 }
1173
1174 // We have room for at least one more holding call at this point.
1175
1176 // First thing, if we are trying to make a call with the same phone account as the live
1177 // call, then allow it so that the connection service can make its own decision about
1178 // how to handle the new call relative to the current one.
1179 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1180 return true;
1181 } else if (call.getTargetPhoneAccount() == null) {
1182 // Without a phone account, we can't say reliably that the call will fail.
1183 // If the user chooses the same phone account as the live call, then it's
1184 // still possible that the call can be made (like with CDMA calls not supporting
1185 // hold but they still support adding a call by going immediately into conference
1186 // mode). Return true here and we'll run this code again after user chooses an
1187 // account.
1188 return true;
1189 }
1190
1191 // Try to hold the live call before attempting the new outgoing call.
1192 if (liveCall.can(PhoneCapabilities.HOLD)) {
1193 liveCall.hold();
1194 return true;
1195 }
1196
1197 // The live call cannot be held so we're out of luck here. There's no room.
1198 return false;
1199 }
1200 return true;
1201 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001202
1203 /**
1204 * Dumps the state of the {@link CallsManager}.
1205 *
1206 * @param pw The {@code IndentingPrintWriter} to write the state to.
1207 */
1208 public void dump(IndentingPrintWriter pw) {
1209 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1210 pw.increaseIndent();
1211 if (mCalls != null) {
1212 pw.println("mCalls: ");
1213 pw.increaseIndent();
1214 for (Call call : mCalls) {
1215 pw.println(call);
1216 }
1217 pw.decreaseIndent();
1218 }
1219 pw.decreaseIndent();
1220 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001221}