blob: d36e7ee9de9f5d068270d9b8b8f79ba23e13d7bf [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Sailesh Nepalce704b92014-03-17 18:31:43 -070020import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
Santos Cordonf193ba42014-09-12 06:37:39 -070022import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070023import android.telecom.AudioState;
24import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070025import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070026import android.telecom.GatewayInfo;
27import android.telecom.ParcelableConference;
Tyler Gunn5b452df2014-10-01 15:02:58 -070028import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070030import android.telecom.PhoneCapabilities;
Tyler Gunn5b452df2014-10-01 15:02:58 -070031import android.telecom.TelecomManager;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070032import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033
Tyler Gunn91d43cf2014-09-17 12:19:39 -070034import com.android.internal.util.IndentingPrintWriter;
Yorke Lee9250e5f2014-10-01 13:39:09 -070035
Sailesh Nepal810735e2014-03-18 18:15:46 -070036import com.google.common.collect.ImmutableCollection;
37import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080038
Jay Shraunera82c8f72014-08-14 15:49:16 -070039import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070040import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070041import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070042import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070043import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070044import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080045
Ben Giladdd8c6082013-12-30 14:44:08 -080046/**
47 * Singleton.
48 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070049 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080050 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070051 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080052 */
Santos Cordon64c7e962014-07-02 15:15:27 -070053public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080054
Santos Cordondf399862014-08-06 04:39:15 -070055 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070056 interface CallsManagerListener {
57 void onCallAdded(Call call);
58 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070059 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070060 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070061 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070062 ConnectionServiceWrapper oldService,
63 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070064 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070065 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070066 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070067 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070068 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070069 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070070 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070071 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070072 }
73
Tyler Gunn91d43cf2014-09-17 12:19:39 -070074 /**
75 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
76 */
77 private static CallsManager INSTANCE = null;
78
79 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080080
Santos Cordonf193ba42014-09-12 06:37:39 -070081 private static final int MAXIMUM_LIVE_CALLS = 1;
82 private static final int MAXIMUM_HOLD_CALLS = 1;
83 private static final int MAXIMUM_RINGING_CALLS = 1;
84 private static final int MAXIMUM_OUTGOING_CALLS = 1;
85
86 private static final int[] LIVE_CALL_STATES =
87 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
88
89 private static final int[] OUTGOING_CALL_STATES =
90 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
91
Santos Cordon8e8b8d22013-12-19 14:14:05 -080092 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070093 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
94 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070095 *
96 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
97 * load factor before resizing, 1 means we only expect a single thread to
98 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -080099 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700100 private final Set<Call> mCalls = Collections.newSetFromMap(
101 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800102
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700103 private final ConnectionServiceRepository mConnectionServiceRepository;
104 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
105 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700106 private final CallAudioManager mCallAudioManager;
107 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700108 // For this set initial table size to 16 because we add 13 listeners in
109 // the CallsManager constructor.
110 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
111 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700112 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700113 private final WiredHeadsetManager mWiredHeadsetManager;
114 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700115 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700116 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700117 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700118 private final Context mContext;
119 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
120 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700121 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700122
123 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 * The call the user is currently interacting with. This is the call that should have audio
125 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800126 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700127 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800128
Santos Cordon766d04f2014-05-06 10:28:25 -0700129 /** Singleton accessor. */
130 static CallsManager getInstance() {
131 return INSTANCE;
132 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800133
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800134 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700135 * Sets the static singleton instance.
136 *
137 * @param instance The instance to set.
138 */
139 static void initialize(CallsManager instance) {
140 INSTANCE = instance;
141 }
142
143 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700144 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700146 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
147 PhoneAccountRegistrar phoneAccountRegistrar) {
148 mContext = context;
149 mPhoneAccountRegistrar = phoneAccountRegistrar;
150 mMissedCallNotifier = missedCallNotifier;
151 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
152 mWiredHeadsetManager = new WiredHeadsetManager(context);
153 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700154 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700155 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
156 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
157 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
158 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700159 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700160 mCallLogManager = new CallLogManager(context);
161 mInCallController = new InCallController(context);
162 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
163 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
164 context);
Santos Cordonae193062014-05-21 21:21:49 -0700165
Santos Cordondeb8c892014-05-30 01:38:03 -0700166 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700167 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700168 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700169 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700170 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700171 mListeners.add(new RingbackPlayer(this, playerFactory));
172 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700173 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700174 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700175 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700176 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700177 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700178 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800179 }
180
Santos Cordon766d04f2014-05-06 10:28:25 -0700181 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700182 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700183 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700184
Tyler Gunncde2e502014-08-12 14:35:58 -0700185 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700186 if (!mCalls.contains(call)) {
187 // Call was not added previously in startOutgoingCall due to it being a potential MMI
188 // code, so add it now.
189 addCall(call);
190 }
191
192 // The call's ConnectionService has been updated.
193 for (CallsManagerListener listener : mListeners) {
194 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700195 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700196
197 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700198 }
199
200 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700201 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700202 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700203
Andrew Leee6288a72014-10-01 13:50:02 -0700204 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700205 }
206
207 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700208 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700209 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700210 setCallState(incomingCall, CallState.RINGING);
211
212 if (hasMaximumRingingCalls()) {
213 incomingCall.reject(false, null);
214 // since the call was not added to the list of calls, we have to call the missed
215 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700216 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700217 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
218 } else {
219 addCall(incomingCall);
220 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700221 }
222
223 @Override
224 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700225 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700226 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800227 }
228
Ihab Awadcb387ac2014-05-28 16:49:38 -0700229 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700230 public void onSuccessfulUnknownCall(Call call, int callState) {
231 setCallState(call, callState);
232 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
233 addCall(call);
234 }
235
236 @Override
237 public void onFailedUnknownCall(Call call) {
238 Log.i(this, "onFailedUnknownCall for call %s", call);
239 setCallState(call, CallState.DISCONNECTED);
240 call.removeListener(this);
241 }
242
243 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700244 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700245 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700246 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700247 }
248 }
249
Evan Charlton352105c2014-06-03 14:10:54 -0700250 @Override
251 public void onPostDialWait(Call call, String remaining) {
252 mInCallController.onPostDialWait(call, remaining);
253 }
254
Santos Cordona1610702014-06-04 20:22:56 -0700255 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700256 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700257 // parent-child relationship affects which call should be foreground, so do an update.
258 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700259 for (CallsManagerListener listener : mListeners) {
260 listener.onIsConferencedChanged(call);
261 }
262 }
263
264 @Override
265 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700266 // parent-child relationship affects which call should be foreground, so do an update.
267 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700268 for (CallsManagerListener listener : mListeners) {
269 listener.onIsConferencedChanged(call);
270 }
271 }
272
Ihab Awadff7493a2014-06-10 13:47:44 -0700273 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700274 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700275 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700276 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700277 }
278 }
279
Andrew Lee4a796602014-07-11 17:23:03 -0700280 @Override
281 public void onVideoStateChanged(Call call) {
282 for (CallsManagerListener listener : mListeners) {
283 listener.onVideoStateChanged(call);
284 }
285 }
286
Sailesh Nepal810735e2014-03-18 18:15:46 -0700287 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700288 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700289 }
290
291 Call getForegroundCall() {
292 return mForegroundCall;
293 }
294
Santos Cordonae193062014-05-21 21:21:49 -0700295 Ringer getRinger() {
296 return mRinger;
297 }
298
Santos Cordonf3671a62014-05-29 21:51:53 -0700299 InCallController getInCallController() {
300 return mInCallController;
301 }
302
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700303 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700304 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700305 if (call.isEmergencyCall()) {
306 return true;
307 }
308 }
309 return false;
310 }
311
Ihab Awad6fb37c82014-08-07 19:48:57 -0700312 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700313 return mCallAudioManager.getAudioState();
314 }
315
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700316 boolean isTtySupported() {
317 return mTtyManager.isTtySupported();
318 }
319
320 int getCurrentTtyMode() {
321 return mTtyManager.getCurrentTtyMode();
322 }
323
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700324 void addListener(CallsManagerListener listener) {
325 mListeners.add(listener);
326 }
327
328 void removeListener(CallsManagerListener listener) {
329 mListeners.remove(listener);
330 }
331
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800332 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700333 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800334 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700335 * @param phoneAccountHandle The phone account which contains the component name of the
336 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800337 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800338 */
Evan Charlton89176372014-07-19 18:23:09 -0700339 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800340 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700341 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700342 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700343 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700344 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700345 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700346 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700347 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700348 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700349 true /* isIncoming */,
350 false /* isConference */);
351
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700352 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700353 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700354 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700355 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800356 }
357
Yorke Lee9250e5f2014-10-01 13:39:09 -0700358 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
359 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
360 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
361 Call call = new Call(
362 mContext,
363 mConnectionServiceRepository,
364 handle,
365 null /* gatewayInfo */,
366 null /* connectionManagerPhoneAccount */,
367 phoneAccountHandle,
368 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
369 // to the existing connection instead of trying to create a new one.
370 true /* isIncoming */,
371 false /* isConference */);
372 call.setConnectTimeMillis(System.currentTimeMillis());
373 call.setIsUnknown(true);
374 call.setExtras(extras);
375 call.addListener(this);
376 call.startCreateConnection(mPhoneAccountRegistrar);
377 }
378
Nancy Chen0d3076c2014-07-30 14:45:44 -0700379 /**
380 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
381 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700382 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700383 * @param phoneAccountHandle The phone account which contains the component name of the
384 * connection service to use for this call.
385 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700386 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700387 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700388 // Create a call with original handle. The handle may be changed when the call is attached
389 // to a connection service, but in most cases will remain the same.
390 Call call = new Call(
391 mContext,
392 mConnectionServiceRepository,
393 handle,
394 null /* gatewayInfo */,
395 null /* connectionManagerPhoneAccount */,
396 null /* phoneAccountHandle */,
397 false /* isIncoming */,
398 false /* isConference */);
399
400 List<PhoneAccountHandle> accounts =
401 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
402
Nancy Chenbc9ef172014-08-15 17:11:57 -0700403 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
404 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700405 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700406 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700407 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700408 phoneAccountHandle = null;
409 }
410 }
411
412 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700413 // No preset account, check if default exists that supports the URI scheme for the
414 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700415 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700416 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700417 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700418 if (defaultAccountHandle != null) {
419 phoneAccountHandle = defaultAccountHandle;
420 }
421 }
422
Nancy Chen1c5926f2014-09-17 14:44:14 -0700423 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700424
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700425 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
426 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700427 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700428
429 // Do not support any more live calls. Our options are to move a call to hold, disconnect
430 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700431 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700432 // just cancel at this point.
433 return null;
434 }
435
Nancy Chen1ccab202014-09-18 15:28:36 -0700436 if (phoneAccountHandle == null && accounts.size() > 1 && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700437 // This is the state where the user is expected to select an account
438 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700439 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700440 } else {
441 call.setState(CallState.CONNECTING);
442 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700443
Nancy Chen1c5926f2014-09-17 14:44:14 -0700444 call.setExtras(extras);
445
Tyler Gunn5b452df2014-10-01 15:02:58 -0700446 if (isPotentialMMICode(handle) || isPotentialInCallMMICode) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700447 call.addListener(this);
Tyler Gunn5b452df2014-10-01 15:02:58 -0700448 } else {
449 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700450 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700451
452 return call;
453 }
454
Ben Gilada0d9f752014-02-26 11:49:03 -0800455 /**
Yorke Lee33501632014-03-17 19:24:12 -0700456 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800457 *
Yorke Lee33501632014-03-17 19:24:12 -0700458 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700459 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700460 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700461 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700462 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800463 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700464 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
465 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700466 if (call == null) {
467 // don't do anything if the call no longer exists
468 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700469 return;
470 }
471
Nancy Chen201b4372014-09-08 14:18:24 -0700472 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700473
474 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700475 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700476 } else {
477 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
478 Log.pii(uriHandle), Log.pii(handle));
479 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700480
Nancy Chen0d3076c2014-07-30 14:45:44 -0700481 call.setHandle(uriHandle);
482 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700483 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700484 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700485
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700486 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
487 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700488 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700489 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700490 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700491 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700492
Santos Cordonf193ba42014-09-12 06:37:39 -0700493 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700494 // If the account has been set, proceed to place the outgoing call.
495 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700496 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700497 }
Yorke Leef98fb572014-03-05 10:56:55 -0800498 }
499
500 /**
Santos Cordona1610702014-06-04 20:22:56 -0700501 * Attempts to start a conference call for the specified call.
502 *
Santos Cordon12d61822014-07-29 16:02:20 -0700503 * @param call The call to conference.
504 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700505 */
Santos Cordon12d61822014-07-29 16:02:20 -0700506 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700507 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700508 }
509
510 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700511 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
512 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800513 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700514 *
515 * @param call The call to answer.
516 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800517 */
Andrew Lee38931d02014-07-16 10:17:36 -0700518 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700519 if (!mCalls.contains(call)) {
520 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800521 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700522 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700523 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700524 if (mForegroundCall != null && mForegroundCall != call &&
525 (mForegroundCall.isActive() ||
526 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700527 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
528 // This call does not support hold. If it is from a different connection
529 // service, then disconnect it, otherwise allow the connection service to
530 // figure out the right states.
531 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
532 mForegroundCall.disconnect();
533 }
534 } else {
535 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
536 mForegroundCall, call);
537 mForegroundCall.hold();
538 }
Santos Cordondf399862014-08-06 04:39:15 -0700539 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700540 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700541 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700542 }
543
Santos Cordona56f2762014-03-24 15:55:53 -0700544 for (CallsManagerListener listener : mListeners) {
545 listener.onIncomingCallAnswered(call);
546 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800547
Santos Cordon61d0f702014-02-19 02:52:23 -0800548 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700549 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700550 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800551 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800552 }
553
554 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700555 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
556 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800557 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800558 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700559 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700560 if (!mCalls.contains(call)) {
561 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800562 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700563 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700564 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700565 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700566 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800567 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800568 }
569
570 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700571 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700572 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700573 * @param digit The DTMF digit to play.
574 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700575 void playDtmfTone(Call call, char digit) {
576 if (!mCalls.contains(call)) {
577 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700578 } else {
579 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700580 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700581 }
582 }
583
584 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700585 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700586 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700587 void stopDtmfTone(Call call) {
588 if (!mCalls.contains(call)) {
589 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700590 } else {
591 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700592 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700593 }
594 }
595
596 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700597 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700598 */
Evan Charlton352105c2014-06-03 14:10:54 -0700599 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700600 if (!mCalls.contains(call)) {
601 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700602 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700603 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700604 }
605 }
606
607 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700608 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800609 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
610 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800611 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700612 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700613 Log.v(this, "disconnectCall %s", call);
614
Sailesh Nepale59bb192014-04-01 18:33:59 -0700615 if (!mCalls.contains(call)) {
616 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800617 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700618 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800619 call.disconnect();
620 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800621 }
Santos Cordon681663d2014-01-30 04:32:15 -0800622
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700623 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700624 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700625 */
626 void disconnectAllCalls() {
627 Log.v(this, "disconnectAllCalls");
628
629 for (Call call : mCalls) {
630 disconnectCall(call);
631 }
632 }
633
Nancy Chenbc9ef172014-08-15 17:11:57 -0700634
Tyler Gunn61b92102014-08-19 07:42:20 -0700635 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700636 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700637 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
638 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700639 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700640 void holdCall(Call call) {
641 if (!mCalls.contains(call)) {
642 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700643 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700644 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700645 call.hold();
646 }
647 }
648
649 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700650 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700651 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
652 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700653 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700654 void unholdCall(Call call) {
655 if (!mCalls.contains(call)) {
656 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700657 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700658 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700659 for (Call c : mCalls) {
660 if (c != null && c.isAlive() && c != call) {
661 c.hold();
662 }
663 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700664 call.unhold();
665 }
666 }
667
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700668 /** Called by the in-call UI to change the mute state. */
669 void mute(boolean shouldMute) {
670 mCallAudioManager.mute(shouldMute);
671 }
672
673 /**
674 * Called by the in-call UI to change the audio route, for example to change from earpiece to
675 * speaker phone.
676 */
677 void setAudioRoute(int route) {
678 mCallAudioManager.setAudioRoute(route);
679 }
680
Yorke Leed1346872014-07-28 14:38:45 -0700681 /** Called by the in-call UI to turn the proximity sensor on. */
682 void turnOnProximitySensor() {
683 mProximitySensorManager.turnOn();
684 }
685
686 /**
687 * Called by the in-call UI to turn the proximity sensor off.
688 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
689 * the screen will be kept off until the proximity sensor goes negative.
690 */
691 void turnOffProximitySensor(boolean screenOnImmediately) {
692 mProximitySensorManager.turnOff(screenOnImmediately);
693 }
694
Evan Charlton89176372014-07-19 18:23:09 -0700695 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700696 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700697 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700698 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700699 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
700 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
701 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
702 // respecting a rewritten number or a canceled number. This is unlikely since
703 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
704 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700705 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700706
707 // Note: emergency calls never go through account selection dialog so they never
708 // arrive here.
709 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700710 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700711 } else {
712 call.disconnect();
713 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700714 }
715 }
716
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700717 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700718 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700719 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700720 for (CallsManagerListener listener : mListeners) {
721 listener.onAudioStateChanged(oldAudioState, newAudioState);
722 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700723 }
724
Sailesh Nepale59bb192014-04-01 18:33:59 -0700725 void markCallAsRinging(Call call) {
726 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800727 }
728
Sailesh Nepale59bb192014-04-01 18:33:59 -0700729 void markCallAsDialing(Call call) {
730 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800731 }
732
Sailesh Nepale59bb192014-04-01 18:33:59 -0700733 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700734 if (call.getConnectTimeMillis() == 0) {
735 call.setConnectTimeMillis(System.currentTimeMillis());
736 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700737 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700738
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700739 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700740 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700741 }
Santos Cordon681663d2014-01-30 04:32:15 -0800742 }
743
Sailesh Nepale59bb192014-04-01 18:33:59 -0700744 void markCallAsOnHold(Call call) {
745 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700746 }
747
Santos Cordon049b7b62014-01-30 05:34:26 -0800748 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700749 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
750 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800751 *
Andrew Lee701dc002014-09-11 21:29:12 -0700752 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800753 */
Andrew Lee701dc002014-09-11 21:29:12 -0700754 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
755 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700756 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700757 }
758
Ben Gilada0d9f752014-02-26 11:49:03 -0800759 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700760 * Removes an existing disconnected call, and notifies the in-call app.
761 */
762 void markCallAsRemoved(Call call) {
763 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700764 if (mLocallyDisconnectingCalls.contains(call)) {
765 mLocallyDisconnectingCalls.remove(call);
766 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
767 mForegroundCall.unhold();
768 }
769 }
Ihab Awada02bef52014-08-13 18:18:42 -0700770 }
771
772 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700773 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700774 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700775 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700776 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700777 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700778 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700779 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700780 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700781 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700782 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700783 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700784 }
785 }
786 }
787
Santos Cordondeb8c892014-05-30 01:38:03 -0700788 boolean hasAnyCalls() {
789 return !mCalls.isEmpty();
790 }
791
Santos Cordonc7e85d42014-05-22 02:51:48 -0700792 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700793 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700794 }
795
796 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700797 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700798 }
799
Santos Cordondeb8c892014-05-30 01:38:03 -0700800 boolean onMediaButton(int type) {
801 if (hasAnyCalls()) {
802 if (HeadsetMediaButton.SHORT_PRESS == type) {
803 Call ringingCall = getFirstCallWithState(CallState.RINGING);
804 if (ringingCall == null) {
805 mCallAudioManager.toggleMute();
806 return true;
807 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700808 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700809 return true;
810 }
811 } else if (HeadsetMediaButton.LONG_PRESS == type) {
812 Log.d(this, "handleHeadsetHook: longpress -> hangup");
813 Call callToHangup = getFirstCallWithState(
814 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
815 if (callToHangup != null) {
816 callToHangup.disconnect();
817 return true;
818 }
819 }
820 }
821 return false;
822 }
823
824 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700825 * Checks to see if the specified call is the only high-level call and if so, enable the
826 * "Add-call" button. We allow you to add a second call but not a third or beyond.
827 *
828 * @param call The call to test for add-call.
829 * @return Whether the add-call feature should be enabled for the call.
830 */
831 protected boolean isAddCallCapable(Call call) {
832 if (call.getParentCall() != null) {
833 // Never true for child calls.
834 return false;
835 }
836
Andrew Leec11fcd92014-09-19 18:31:46 -0700837 // Use canManageConference as a mechanism to check if the call is CDMA.
838 // Disable "Add Call" for CDMA calls which are conference calls.
839 boolean canManageConference = PhoneCapabilities.MANAGE_CONFERENCE
840 == (call.getCallCapabilities() & PhoneCapabilities.MANAGE_CONFERENCE);
841 if (call.isConference() && !canManageConference) {
842 return false;
843 }
844
Santos Cordon10838c22014-06-11 17:36:04 -0700845 // Loop through all the other calls and there exists a top level (has no parent) call
846 // that is not the specified call, return false.
847 for (Call otherCall : mCalls) {
848 if (call != otherCall && otherCall.getParentCall() == null) {
849 return false;
850 }
851 }
852 return true;
853 }
854
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700855 Call getRingingCall() {
856 return getFirstCallWithState(CallState.RINGING);
857 }
858
859 Call getActiveCall() {
860 return getFirstCallWithState(CallState.ACTIVE);
861 }
862
Nancy Chen05a9e402014-09-26 14:14:32 -0700863 Call getDialingCall() {
864 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700865 }
866
867 Call getHeldCall() {
868 return getFirstCallWithState(CallState.ON_HOLD);
869 }
870
Santos Cordonf193ba42014-09-12 06:37:39 -0700871 Call getFirstCallWithState(int... states) {
872 return getFirstCallWithState(null, states);
873 }
874
Santos Cordon10838c22014-06-11 17:36:04 -0700875 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700876 * Returns the first call that it finds with the given states. The states are treated as having
877 * priority order so that any call with the first state will be returned before any call with
878 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700879 *
880 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700881 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700882 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700883 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700884 // check the foreground first
885 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
886 return mForegroundCall;
887 }
888
Santos Cordondeb8c892014-05-30 01:38:03 -0700889 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700890 if (Objects.equals(callToSkip, call)) {
891 continue;
892 }
893
894 // Only operate on top-level calls
895 if (call.getParentCall() != null) {
896 continue;
897 }
898
Santos Cordondeb8c892014-05-30 01:38:03 -0700899 if (currentState == call.getState()) {
900 return call;
901 }
902 }
903 }
904 return null;
905 }
906
Santos Cordon0fbe6322014-08-14 04:04:25 -0700907 Call createConferenceCall(
908 PhoneAccountHandle phoneAccount,
909 ParcelableConference parcelableConference) {
910 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700911 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700912 mConnectionServiceRepository,
913 null /* handle */,
914 null /* gatewayInfo */,
915 null /* connectionManagerPhoneAccount */,
916 phoneAccount,
917 false /* isIncoming */,
918 true /* isConference */);
919
920 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700921 if (call.getState() == CallState.ACTIVE) {
922 call.setConnectTimeMillis(System.currentTimeMillis());
923 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700924 call.setCallCapabilities(parcelableConference.getCapabilities());
925
926 // TODO: Move this to be a part of addCall()
927 call.addListener(this);
928 addCall(call);
929 return call;
930 }
931
Yorke Leef86db2e2014-09-12 17:56:48 -0700932 /**
933 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
934 */
935 int getCallState() {
936 return mPhoneStateBroadcaster.getCallState();
937 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700938
Santos Cordon4b2c1192014-03-19 18:15:38 -0700939 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700940 * Retrieves the {@link PhoneAccountRegistrar}.
941 *
942 * @return The {@link PhoneAccountRegistrar}.
943 */
944 PhoneAccountRegistrar getPhoneAccountRegistrar() {
945 return mPhoneAccountRegistrar;
946 }
947
948 /**
949 * Retrieves the {@link MissedCallNotifier}
950 * @return The {@link MissedCallNotifier}.
951 */
952 MissedCallNotifier getMissedCallNotifier() {
953 return mMissedCallNotifier;
954 }
955
956 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800957 * Adds the specified call to the main list of live calls.
958 *
959 * @param call The call to add.
960 */
961 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700962 Log.v(this, "addCall(%s)", call);
963
964 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700965 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700966
Santos Cordondf399862014-08-06 04:39:15 -0700967 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700968 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700969 for (CallsManagerListener listener : mListeners) {
970 listener.onCallAdded(call);
971 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700972 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800973 }
974
Sailesh Nepal810735e2014-03-18 18:15:46 -0700975 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700976 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700977
Santos Cordon672321e2014-08-21 14:38:58 -0700978 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700979 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700980 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700981
982 boolean shouldNotify = false;
983 if (mCalls.contains(call)) {
984 mCalls.remove(call);
985 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700986 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800987
Sailesh Nepale59bb192014-04-01 18:33:59 -0700988 // Only broadcast changes for calls that are being tracked.
989 if (shouldNotify) {
990 for (CallsManagerListener listener : mListeners) {
991 listener.onCallRemoved(call);
992 }
993 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700994 }
995 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800996
Sailesh Nepal810735e2014-03-18 18:15:46 -0700997 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700998 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700999 *
1000 * @param call The call.
1001 * @param newState The new state of the call.
1002 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001003 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001004 if (call == null) {
1005 return;
1006 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001007 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001008 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1009 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001010 if (newState != oldState) {
1011 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1012 // 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 -07001013 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001014 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001015 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001016 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001017 // unexpected transition occurs.
1018 call.setState(newState);
1019
1020 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001021 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001022 for (CallsManagerListener listener : mListeners) {
1023 listener.onCallStateChanged(call, oldState, newState);
1024 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001025 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001026 }
1027 }
1028 }
1029
1030 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001031 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001032 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001033 private void updateForegroundCall() {
1034 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001035 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001036 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001037 // of its state will be foreground by default and instead the connection service should
1038 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001039 // the call should play audio and listen to microphone if it wants.
1040
Santos Cordonf193ba42014-09-12 06:37:39 -07001041 // Only top-level calls can be in foreground
1042 if (call.getParentCall() != null) {
1043 continue;
1044 }
1045
Santos Cordon40f78c22014-04-07 02:11:42 -07001046 // Active calls have priority.
1047 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001048 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001049 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001050 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001051
1052 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001053 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001054 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001055 }
1056 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001057
Sailesh Nepal810735e2014-03-18 18:15:46 -07001058 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001059 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001060 Call oldForegroundCall = mForegroundCall;
1061 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001062
Santos Cordona56f2762014-03-24 15:55:53 -07001063 for (CallsManagerListener listener : mListeners) {
1064 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1065 }
Santos Cordon681663d2014-01-30 04:32:15 -08001066 }
1067 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001068
1069 private boolean isPotentialMMICode(Uri handle) {
1070 return (handle != null && handle.getSchemeSpecificPart() != null
1071 && handle.getSchemeSpecificPart().contains("#"));
1072 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001073
Tyler Gunn5b452df2014-10-01 15:02:58 -07001074 /**
1075 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1076 * MMI codes which can be dialed when one or more calls are in progress.
1077 * <P>
1078 * Checks for numbers formatted similar to the MMI codes defined in:
1079 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1080 * and
1081 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1082 *
1083 * @param handle The URI to call.
1084 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1085 */
1086 private boolean isPotentialInCallMMICode(Uri handle) {
1087 if (handle != null && handle.getSchemeSpecificPart() != null &&
1088 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1089
1090 String dialedNumber = handle.getSchemeSpecificPart();
1091 return (dialedNumber.equals("0") ||
1092 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1093 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1094 dialedNumber.equals("3") ||
1095 dialedNumber.equals("4") ||
1096 dialedNumber.equals("5"));
1097 }
1098 return false;
1099 }
1100
Santos Cordonf193ba42014-09-12 06:37:39 -07001101 private int getNumCallsWithState(int... states) {
1102 int count = 0;
1103 for (int state : states) {
1104 for (Call call : mCalls) {
1105 if (call.getState() == state) {
1106 count++;
1107 }
1108 }
1109 }
1110 return count;
1111 }
1112
1113 private boolean hasMaximumLiveCalls() {
1114 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1115 }
1116
1117 private boolean hasMaximumHoldingCalls() {
1118 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1119 }
1120
1121 private boolean hasMaximumRingingCalls() {
1122 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1123 }
1124
1125 private boolean hasMaximumOutgoingCalls() {
1126 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1127 }
1128
1129 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1130 if (hasMaximumLiveCalls()) {
1131 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1132 // have to change.
1133 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1134
Santos Cordond1766502014-09-26 15:45:39 -07001135 if (call == liveCall) {
1136 // If the call is already the foreground call, then we are golden.
1137 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1138 // state since the call was already populated into the list.
1139 return true;
1140 }
1141
Andrew Leee6595182014-09-23 18:43:05 -07001142 if (hasMaximumOutgoingCalls()) {
1143 // Disconnect the current outgoing call if it's not an emergency call. If the user
1144 // tries to make two outgoing calls to different emergency call numbers, we will try
1145 // to connect the first outgoing call.
1146 if (isEmergency) {
1147 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1148 if (!outgoingCall.isEmergencyCall()) {
1149 outgoingCall.disconnect();
1150 return true;
1151 }
1152 }
1153 return false;
1154 }
1155
Santos Cordonf193ba42014-09-12 06:37:39 -07001156 if (hasMaximumHoldingCalls()) {
1157 // There is no more room for any more calls, unless it's an emergency.
1158 if (isEmergency) {
1159 // Kill the current active call, this is easier then trying to disconnect a
1160 // holding call and hold an active call.
1161 liveCall.disconnect();
1162 return true;
1163 }
1164 return false; // No more room!
1165 }
1166
1167 // We have room for at least one more holding call at this point.
1168
1169 // First thing, if we are trying to make a call with the same phone account as the live
1170 // call, then allow it so that the connection service can make its own decision about
1171 // how to handle the new call relative to the current one.
1172 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1173 return true;
1174 } else if (call.getTargetPhoneAccount() == null) {
1175 // Without a phone account, we can't say reliably that the call will fail.
1176 // If the user chooses the same phone account as the live call, then it's
1177 // still possible that the call can be made (like with CDMA calls not supporting
1178 // hold but they still support adding a call by going immediately into conference
1179 // mode). Return true here and we'll run this code again after user chooses an
1180 // account.
1181 return true;
1182 }
1183
1184 // Try to hold the live call before attempting the new outgoing call.
1185 if (liveCall.can(PhoneCapabilities.HOLD)) {
1186 liveCall.hold();
1187 return true;
1188 }
1189
1190 // The live call cannot be held so we're out of luck here. There's no room.
1191 return false;
1192 }
1193 return true;
1194 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001195
1196 /**
1197 * Dumps the state of the {@link CallsManager}.
1198 *
1199 * @param pw The {@code IndentingPrintWriter} to write the state to.
1200 */
1201 public void dump(IndentingPrintWriter pw) {
1202 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001203 if (mCalls != null) {
1204 pw.println("mCalls: ");
1205 pw.increaseIndent();
1206 for (Call call : mCalls) {
1207 pw.println(call);
1208 }
1209 pw.decreaseIndent();
1210 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001211 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1212
1213 if (mCallAudioManager != null) {
1214 pw.println("mCallAudioManager:");
1215 pw.increaseIndent();
1216 mCallAudioManager.dump(pw);
1217 pw.decreaseIndent();
1218 }
1219
1220 if (mTtyManager != null) {
1221 pw.println("mTtyManager:");
1222 pw.increaseIndent();
1223 mTtyManager.dump(pw);
1224 pw.decreaseIndent();
1225 }
1226
1227 if (mInCallController != null) {
1228 pw.println("mInCallController:");
1229 pw.increaseIndent();
1230 mInCallController.dump(pw);
1231 pw.decreaseIndent();
1232 }
1233
1234 if (mConnectionServiceRepository != null) {
1235 pw.println("mConnectionServiceRepository:");
1236 pw.increaseIndent();
1237 mConnectionServiceRepository.dump(pw);
1238 pw.decreaseIndent();
1239 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001240 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001241}