blob: 932084b93e660baa159b05f380d64690ec70fef1 [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Tyler Gunn504eb832014-10-13 14:15:27 -070020import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Sailesh Nepalce704b92014-03-17 18:31:43 -070023import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080024import android.os.Bundle;
Santos Cordonf193ba42014-09-12 06:37:39 -070025import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070026import android.telecom.AudioState;
27import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070028import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.GatewayInfo;
30import android.telecom.ParcelableConference;
Tyler Gunn5b452df2014-10-01 15:02:58 -070031import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070032import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070033import android.telecom.PhoneCapabilities;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.TelecomManager;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070035import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080036
Tyler Gunn91d43cf2014-09-17 12:19:39 -070037import com.android.internal.util.IndentingPrintWriter;
Yorke Lee9250e5f2014-10-01 13:39:09 -070038
Sailesh Nepal810735e2014-03-18 18:15:46 -070039import com.google.common.collect.ImmutableCollection;
40import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080041
Jay Shraunera82c8f72014-08-14 15:49:16 -070042import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070043import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070044import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070045import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070046import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070047import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080048
Ben Giladdd8c6082013-12-30 14:44:08 -080049/**
50 * Singleton.
51 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070052 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080053 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070054 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080055 */
Santos Cordon64c7e962014-07-02 15:15:27 -070056public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080057
Santos Cordondf399862014-08-06 04:39:15 -070058 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 interface CallsManagerListener {
60 void onCallAdded(Call call);
61 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070062 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070063 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070064 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 ConnectionServiceWrapper oldService,
66 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070068 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070069 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070070 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070071 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070072 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070073 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070074 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 }
76
Tyler Gunn91d43cf2014-09-17 12:19:39 -070077 /**
78 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
79 */
80 private static CallsManager INSTANCE = null;
81
82 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Santos Cordonf193ba42014-09-12 06:37:39 -070084 private static final int MAXIMUM_LIVE_CALLS = 1;
85 private static final int MAXIMUM_HOLD_CALLS = 1;
86 private static final int MAXIMUM_RINGING_CALLS = 1;
87 private static final int MAXIMUM_OUTGOING_CALLS = 1;
88
89 private static final int[] LIVE_CALL_STATES =
90 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
91
92 private static final int[] OUTGOING_CALL_STATES =
93 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
94
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070096 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
97 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070098 *
99 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
100 * load factor before resizing, 1 means we only expect a single thread to
101 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800102 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700103 private final Set<Call> mCalls = Collections.newSetFromMap(
104 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800105
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700106 private final ConnectionServiceRepository mConnectionServiceRepository;
107 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
108 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700109 private final CallAudioManager mCallAudioManager;
110 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700111 // For this set initial table size to 16 because we add 13 listeners in
112 // the CallsManager constructor.
113 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
114 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700115 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700116 private final WiredHeadsetManager mWiredHeadsetManager;
117 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700118 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700119 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700120 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700121 private final Context mContext;
122 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
123 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700124 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700125
126 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700127 * The call the user is currently interacting with. This is the call that should have audio
128 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800129 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700130 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800131
Santos Cordon766d04f2014-05-06 10:28:25 -0700132 /** Singleton accessor. */
133 static CallsManager getInstance() {
134 return INSTANCE;
135 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800136
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800137 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700138 * Sets the static singleton instance.
139 *
140 * @param instance The instance to set.
141 */
142 static void initialize(CallsManager instance) {
143 INSTANCE = instance;
144 }
145
146 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700147 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800148 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700149 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
150 PhoneAccountRegistrar phoneAccountRegistrar) {
151 mContext = context;
152 mPhoneAccountRegistrar = phoneAccountRegistrar;
153 mMissedCallNotifier = missedCallNotifier;
154 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
155 mWiredHeadsetManager = new WiredHeadsetManager(context);
156 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700157 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700158 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
159 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
160 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
161 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700162 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700163 mCallLogManager = new CallLogManager(context);
164 mInCallController = new InCallController(context);
165 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
166 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
167 context);
Santos Cordonae193062014-05-21 21:21:49 -0700168
Santos Cordondeb8c892014-05-30 01:38:03 -0700169 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700170 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700171 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700172 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700173 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700174 mListeners.add(new RingbackPlayer(this, playerFactory));
175 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700176 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700177 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700178 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700179 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700180 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700181 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800182 }
183
Santos Cordon766d04f2014-05-06 10:28:25 -0700184 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700185 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700186 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700187
Tyler Gunncde2e502014-08-12 14:35:58 -0700188 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700189 if (!mCalls.contains(call)) {
190 // Call was not added previously in startOutgoingCall due to it being a potential MMI
191 // code, so add it now.
192 addCall(call);
193 }
194
195 // The call's ConnectionService has been updated.
196 for (CallsManagerListener listener : mListeners) {
197 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700198 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700199
200 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700201 }
202
203 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700204 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700205 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700206
Andrew Leee6288a72014-10-01 13:50:02 -0700207 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700208 }
209
210 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700211 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700212 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700213 setCallState(incomingCall, CallState.RINGING);
214
215 if (hasMaximumRingingCalls()) {
216 incomingCall.reject(false, null);
217 // since the call was not added to the list of calls, we have to call the missed
218 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700219 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700220 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
221 } else {
222 addCall(incomingCall);
223 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700224 }
225
226 @Override
227 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700228 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700229 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800230 }
231
Ihab Awadcb387ac2014-05-28 16:49:38 -0700232 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700233 public void onSuccessfulUnknownCall(Call call, int callState) {
234 setCallState(call, callState);
235 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
236 addCall(call);
237 }
238
239 @Override
240 public void onFailedUnknownCall(Call call) {
241 Log.i(this, "onFailedUnknownCall for call %s", call);
242 setCallState(call, CallState.DISCONNECTED);
243 call.removeListener(this);
244 }
245
246 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700247 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700248 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700249 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700250 }
251 }
252
Evan Charlton352105c2014-06-03 14:10:54 -0700253 @Override
254 public void onPostDialWait(Call call, String remaining) {
255 mInCallController.onPostDialWait(call, remaining);
256 }
257
Santos Cordona1610702014-06-04 20:22:56 -0700258 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700259 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700260 // parent-child relationship affects which call should be foreground, so do an update.
261 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700262 for (CallsManagerListener listener : mListeners) {
263 listener.onIsConferencedChanged(call);
264 }
265 }
266
267 @Override
268 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700269 // parent-child relationship affects which call should be foreground, so do an update.
270 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700271 for (CallsManagerListener listener : mListeners) {
272 listener.onIsConferencedChanged(call);
273 }
274 }
275
Ihab Awadff7493a2014-06-10 13:47:44 -0700276 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700277 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700278 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700279 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700280 }
281 }
282
Andrew Lee4a796602014-07-11 17:23:03 -0700283 @Override
284 public void onVideoStateChanged(Call call) {
285 for (CallsManagerListener listener : mListeners) {
286 listener.onVideoStateChanged(call);
287 }
288 }
289
Sailesh Nepal810735e2014-03-18 18:15:46 -0700290 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700291 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700292 }
293
294 Call getForegroundCall() {
295 return mForegroundCall;
296 }
297
Santos Cordonae193062014-05-21 21:21:49 -0700298 Ringer getRinger() {
299 return mRinger;
300 }
301
Santos Cordonf3671a62014-05-29 21:51:53 -0700302 InCallController getInCallController() {
303 return mInCallController;
304 }
305
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700306 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700307 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700308 if (call.isEmergencyCall()) {
309 return true;
310 }
311 }
312 return false;
313 }
314
Ihab Awad6fb37c82014-08-07 19:48:57 -0700315 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700316 return mCallAudioManager.getAudioState();
317 }
318
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700319 boolean isTtySupported() {
320 return mTtyManager.isTtySupported();
321 }
322
323 int getCurrentTtyMode() {
324 return mTtyManager.getCurrentTtyMode();
325 }
326
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700327 void addListener(CallsManagerListener listener) {
328 mListeners.add(listener);
329 }
330
331 void removeListener(CallsManagerListener listener) {
332 mListeners.remove(listener);
333 }
334
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800335 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700336 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800337 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700338 * @param phoneAccountHandle The phone account which contains the component name of the
339 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800340 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800341 */
Evan Charlton89176372014-07-19 18:23:09 -0700342 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800343 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700344 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700345 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700346 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700347 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700348 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700349 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700350 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700351 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700352 true /* isIncoming */,
353 false /* isConference */);
354
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700355 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700356 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700357 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700358 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800359 }
360
Yorke Lee9250e5f2014-10-01 13:39:09 -0700361 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
362 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
363 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
364 Call call = new Call(
365 mContext,
366 mConnectionServiceRepository,
367 handle,
368 null /* gatewayInfo */,
369 null /* connectionManagerPhoneAccount */,
370 phoneAccountHandle,
371 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
372 // to the existing connection instead of trying to create a new one.
373 true /* isIncoming */,
374 false /* isConference */);
375 call.setConnectTimeMillis(System.currentTimeMillis());
376 call.setIsUnknown(true);
377 call.setExtras(extras);
378 call.addListener(this);
379 call.startCreateConnection(mPhoneAccountRegistrar);
380 }
381
Nancy Chen0d3076c2014-07-30 14:45:44 -0700382 /**
383 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
384 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700385 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700386 * @param phoneAccountHandle The phone account which contains the component name of the
387 * connection service to use for this call.
388 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700389 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700390 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700391 // Create a call with original handle. The handle may be changed when the call is attached
392 // to a connection service, but in most cases will remain the same.
393 Call call = new Call(
394 mContext,
395 mConnectionServiceRepository,
396 handle,
397 null /* gatewayInfo */,
398 null /* connectionManagerPhoneAccount */,
399 null /* phoneAccountHandle */,
400 false /* isIncoming */,
401 false /* isConference */);
402
403 List<PhoneAccountHandle> accounts =
404 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
405
Nancy Chenbc9ef172014-08-15 17:11:57 -0700406 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
407 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700408 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700409 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700410 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700411 phoneAccountHandle = null;
412 }
413 }
414
415 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700416 // No preset account, check if default exists that supports the URI scheme for the
417 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700418 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700419 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700420 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700421 if (defaultAccountHandle != null) {
422 phoneAccountHandle = defaultAccountHandle;
423 }
424 }
425
Nancy Chen1c5926f2014-09-17 14:44:14 -0700426 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700427
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700428 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
429 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700430 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700431
432 // Do not support any more live calls. Our options are to move a call to hold, disconnect
433 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700434 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700435 // just cancel at this point.
436 return null;
437 }
438
Nancy Chen1ccab202014-09-18 15:28:36 -0700439 if (phoneAccountHandle == null && accounts.size() > 1 && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700440 // This is the state where the user is expected to select an account
441 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700442 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700443 } else {
444 call.setState(CallState.CONNECTING);
445 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700446
Nancy Chen1c5926f2014-09-17 14:44:14 -0700447 call.setExtras(extras);
448
Tyler Gunn504eb832014-10-13 14:15:27 -0700449 // Do not add the call if it is a potential MMI code.
450 // We also want to skip adding the call if there is a broadcast receiver which could
451 // intercept the outgoing call and cancel it. We do this to ensure that we do not show the
452 // InCall UI for the cancelled call. If the call is not intercepted, it will be added in
453 // {@link CallsManager#onSuccessfulOutgoingCall}.
454 if (isPotentialMMICode(handle) || isPotentialInCallMMICode ||
455 (!isEmergencyCall && canOutgoingCallBroadcastsBeIntercepted())) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700456 call.addListener(this);
Tyler Gunn5b452df2014-10-01 15:02:58 -0700457 } else {
458 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700459 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700460
461 return call;
462 }
463
Ben Gilada0d9f752014-02-26 11:49:03 -0800464 /**
Yorke Lee33501632014-03-17 19:24:12 -0700465 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800466 *
Yorke Lee33501632014-03-17 19:24:12 -0700467 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700468 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700469 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700470 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700471 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800472 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700473 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
474 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700475 if (call == null) {
476 // don't do anything if the call no longer exists
477 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700478 return;
479 }
480
Nancy Chen201b4372014-09-08 14:18:24 -0700481 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700482
483 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700484 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700485 } else {
486 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
487 Log.pii(uriHandle), Log.pii(handle));
488 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700489
Nancy Chen0d3076c2014-07-30 14:45:44 -0700490 call.setHandle(uriHandle);
491 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700492 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700493 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700494
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700495 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
496 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700497 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700498 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700499 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700500 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700501
Santos Cordonf193ba42014-09-12 06:37:39 -0700502 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700503 // If the account has been set, proceed to place the outgoing call.
504 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700505 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700506 }
Yorke Leef98fb572014-03-05 10:56:55 -0800507 }
508
509 /**
Santos Cordona1610702014-06-04 20:22:56 -0700510 * Attempts to start a conference call for the specified call.
511 *
Santos Cordon12d61822014-07-29 16:02:20 -0700512 * @param call The call to conference.
513 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700514 */
Santos Cordon12d61822014-07-29 16:02:20 -0700515 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700516 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700517 }
518
519 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700520 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
521 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800522 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700523 *
524 * @param call The call to answer.
525 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800526 */
Andrew Lee38931d02014-07-16 10:17:36 -0700527 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700528 if (!mCalls.contains(call)) {
529 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800530 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700531 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700532 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700533 if (mForegroundCall != null && mForegroundCall != call &&
534 (mForegroundCall.isActive() ||
535 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700536 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
537 // This call does not support hold. If it is from a different connection
538 // service, then disconnect it, otherwise allow the connection service to
539 // figure out the right states.
540 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
541 mForegroundCall.disconnect();
542 }
543 } else {
544 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
545 mForegroundCall, call);
546 mForegroundCall.hold();
547 }
Santos Cordondf399862014-08-06 04:39:15 -0700548 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700549 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700550 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700551 }
552
Santos Cordona56f2762014-03-24 15:55:53 -0700553 for (CallsManagerListener listener : mListeners) {
554 listener.onIncomingCallAnswered(call);
555 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800556
Santos Cordon61d0f702014-02-19 02:52:23 -0800557 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700558 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700559 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800560 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800561 }
562
563 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700564 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
565 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800566 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800567 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700568 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700569 if (!mCalls.contains(call)) {
570 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800571 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700572 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700573 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700574 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700575 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800576 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800577 }
578
579 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700580 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700581 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700582 * @param digit The DTMF digit to play.
583 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700584 void playDtmfTone(Call call, char digit) {
585 if (!mCalls.contains(call)) {
586 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700587 } else {
588 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700589 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700590 }
591 }
592
593 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700594 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700595 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700596 void stopDtmfTone(Call call) {
597 if (!mCalls.contains(call)) {
598 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700599 } else {
600 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700601 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700602 }
603 }
604
605 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700606 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700607 */
Evan Charlton352105c2014-06-03 14:10:54 -0700608 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700609 if (!mCalls.contains(call)) {
610 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700611 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700612 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700613 }
614 }
615
616 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700617 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800618 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
619 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800620 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700621 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700622 Log.v(this, "disconnectCall %s", call);
623
Sailesh Nepale59bb192014-04-01 18:33:59 -0700624 if (!mCalls.contains(call)) {
625 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800626 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700627 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800628 call.disconnect();
629 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800630 }
Santos Cordon681663d2014-01-30 04:32:15 -0800631
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700632 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700633 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700634 */
635 void disconnectAllCalls() {
636 Log.v(this, "disconnectAllCalls");
637
638 for (Call call : mCalls) {
639 disconnectCall(call);
640 }
641 }
642
Nancy Chenbc9ef172014-08-15 17:11:57 -0700643
Tyler Gunn61b92102014-08-19 07:42:20 -0700644 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700645 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700646 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
647 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700648 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700649 void holdCall(Call call) {
650 if (!mCalls.contains(call)) {
651 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700652 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700653 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700654 call.hold();
655 }
656 }
657
658 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700659 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700660 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
661 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700662 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700663 void unholdCall(Call call) {
664 if (!mCalls.contains(call)) {
665 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700666 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700667 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700668 for (Call c : mCalls) {
669 if (c != null && c.isAlive() && c != call) {
670 c.hold();
671 }
672 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700673 call.unhold();
674 }
675 }
676
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700677 /** Called by the in-call UI to change the mute state. */
678 void mute(boolean shouldMute) {
679 mCallAudioManager.mute(shouldMute);
680 }
681
682 /**
683 * Called by the in-call UI to change the audio route, for example to change from earpiece to
684 * speaker phone.
685 */
686 void setAudioRoute(int route) {
687 mCallAudioManager.setAudioRoute(route);
688 }
689
Yorke Leed1346872014-07-28 14:38:45 -0700690 /** Called by the in-call UI to turn the proximity sensor on. */
691 void turnOnProximitySensor() {
692 mProximitySensorManager.turnOn();
693 }
694
695 /**
696 * Called by the in-call UI to turn the proximity sensor off.
697 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
698 * the screen will be kept off until the proximity sensor goes negative.
699 */
700 void turnOffProximitySensor(boolean screenOnImmediately) {
701 mProximitySensorManager.turnOff(screenOnImmediately);
702 }
703
Evan Charlton89176372014-07-19 18:23:09 -0700704 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700705 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700706 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700707 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700708 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
709 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
710 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
711 // respecting a rewritten number or a canceled number. This is unlikely since
712 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
713 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700714 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700715
716 // Note: emergency calls never go through account selection dialog so they never
717 // arrive here.
718 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700719 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700720 } else {
721 call.disconnect();
722 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700723 }
724 }
725
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700726 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700727 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700728 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700729 for (CallsManagerListener listener : mListeners) {
730 listener.onAudioStateChanged(oldAudioState, newAudioState);
731 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700732 }
733
Sailesh Nepale59bb192014-04-01 18:33:59 -0700734 void markCallAsRinging(Call call) {
735 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800736 }
737
Sailesh Nepale59bb192014-04-01 18:33:59 -0700738 void markCallAsDialing(Call call) {
739 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800740 }
741
Sailesh Nepale59bb192014-04-01 18:33:59 -0700742 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700743 if (call.getConnectTimeMillis() == 0) {
744 call.setConnectTimeMillis(System.currentTimeMillis());
745 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700746 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700747
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700748 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700749 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700750 }
Santos Cordon681663d2014-01-30 04:32:15 -0800751 }
752
Sailesh Nepale59bb192014-04-01 18:33:59 -0700753 void markCallAsOnHold(Call call) {
754 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700755 }
756
Santos Cordon049b7b62014-01-30 05:34:26 -0800757 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700758 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
759 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800760 *
Andrew Lee701dc002014-09-11 21:29:12 -0700761 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800762 */
Andrew Lee701dc002014-09-11 21:29:12 -0700763 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
764 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700765 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700766 }
767
Ben Gilada0d9f752014-02-26 11:49:03 -0800768 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700769 * Removes an existing disconnected call, and notifies the in-call app.
770 */
771 void markCallAsRemoved(Call call) {
772 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700773 if (mLocallyDisconnectingCalls.contains(call)) {
774 mLocallyDisconnectingCalls.remove(call);
775 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
776 mForegroundCall.unhold();
777 }
778 }
Ihab Awada02bef52014-08-13 18:18:42 -0700779 }
780
781 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700782 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700783 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700784 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700785 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700786 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700787 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700788 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700789 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700790 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700791 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700792 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700793 }
794 }
795 }
796
Santos Cordondeb8c892014-05-30 01:38:03 -0700797 boolean hasAnyCalls() {
798 return !mCalls.isEmpty();
799 }
800
Santos Cordonc7e85d42014-05-22 02:51:48 -0700801 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700802 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700803 }
804
805 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700806 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700807 }
808
Santos Cordondeb8c892014-05-30 01:38:03 -0700809 boolean onMediaButton(int type) {
810 if (hasAnyCalls()) {
811 if (HeadsetMediaButton.SHORT_PRESS == type) {
812 Call ringingCall = getFirstCallWithState(CallState.RINGING);
813 if (ringingCall == null) {
814 mCallAudioManager.toggleMute();
815 return true;
816 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700817 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700818 return true;
819 }
820 } else if (HeadsetMediaButton.LONG_PRESS == type) {
821 Log.d(this, "handleHeadsetHook: longpress -> hangup");
822 Call callToHangup = getFirstCallWithState(
823 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
824 if (callToHangup != null) {
825 callToHangup.disconnect();
826 return true;
827 }
828 }
829 }
830 return false;
831 }
832
833 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700834 * Checks to see if the specified call is the only high-level call and if so, enable the
835 * "Add-call" button. We allow you to add a second call but not a third or beyond.
836 *
837 * @param call The call to test for add-call.
838 * @return Whether the add-call feature should be enabled for the call.
839 */
840 protected boolean isAddCallCapable(Call call) {
841 if (call.getParentCall() != null) {
842 // Never true for child calls.
843 return false;
844 }
845
Andrew Leec11fcd92014-09-19 18:31:46 -0700846 // Use canManageConference as a mechanism to check if the call is CDMA.
847 // Disable "Add Call" for CDMA calls which are conference calls.
848 boolean canManageConference = PhoneCapabilities.MANAGE_CONFERENCE
849 == (call.getCallCapabilities() & PhoneCapabilities.MANAGE_CONFERENCE);
850 if (call.isConference() && !canManageConference) {
851 return false;
852 }
853
Santos Cordon10838c22014-06-11 17:36:04 -0700854 // Loop through all the other calls and there exists a top level (has no parent) call
855 // that is not the specified call, return false.
856 for (Call otherCall : mCalls) {
857 if (call != otherCall && otherCall.getParentCall() == null) {
858 return false;
859 }
860 }
861 return true;
862 }
863
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700864 Call getRingingCall() {
865 return getFirstCallWithState(CallState.RINGING);
866 }
867
868 Call getActiveCall() {
869 return getFirstCallWithState(CallState.ACTIVE);
870 }
871
Nancy Chen05a9e402014-09-26 14:14:32 -0700872 Call getDialingCall() {
873 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700874 }
875
876 Call getHeldCall() {
877 return getFirstCallWithState(CallState.ON_HOLD);
878 }
879
Santos Cordonf193ba42014-09-12 06:37:39 -0700880 Call getFirstCallWithState(int... states) {
881 return getFirstCallWithState(null, states);
882 }
883
Santos Cordon10838c22014-06-11 17:36:04 -0700884 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700885 * Returns the first call that it finds with the given states. The states are treated as having
886 * priority order so that any call with the first state will be returned before any call with
887 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700888 *
889 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700890 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700891 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700892 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700893 // check the foreground first
894 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
895 return mForegroundCall;
896 }
897
Santos Cordondeb8c892014-05-30 01:38:03 -0700898 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700899 if (Objects.equals(callToSkip, call)) {
900 continue;
901 }
902
903 // Only operate on top-level calls
904 if (call.getParentCall() != null) {
905 continue;
906 }
907
Santos Cordondeb8c892014-05-30 01:38:03 -0700908 if (currentState == call.getState()) {
909 return call;
910 }
911 }
912 }
913 return null;
914 }
915
Santos Cordon0fbe6322014-08-14 04:04:25 -0700916 Call createConferenceCall(
917 PhoneAccountHandle phoneAccount,
918 ParcelableConference parcelableConference) {
919 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700920 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700921 mConnectionServiceRepository,
922 null /* handle */,
923 null /* gatewayInfo */,
924 null /* connectionManagerPhoneAccount */,
925 phoneAccount,
926 false /* isIncoming */,
927 true /* isConference */);
928
929 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700930 if (call.getState() == CallState.ACTIVE) {
931 call.setConnectTimeMillis(System.currentTimeMillis());
932 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700933 call.setCallCapabilities(parcelableConference.getCapabilities());
934
935 // TODO: Move this to be a part of addCall()
936 call.addListener(this);
937 addCall(call);
938 return call;
939 }
940
Yorke Leef86db2e2014-09-12 17:56:48 -0700941 /**
942 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
943 */
944 int getCallState() {
945 return mPhoneStateBroadcaster.getCallState();
946 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700947
Santos Cordon4b2c1192014-03-19 18:15:38 -0700948 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700949 * Retrieves the {@link PhoneAccountRegistrar}.
950 *
951 * @return The {@link PhoneAccountRegistrar}.
952 */
953 PhoneAccountRegistrar getPhoneAccountRegistrar() {
954 return mPhoneAccountRegistrar;
955 }
956
957 /**
958 * Retrieves the {@link MissedCallNotifier}
959 * @return The {@link MissedCallNotifier}.
960 */
961 MissedCallNotifier getMissedCallNotifier() {
962 return mMissedCallNotifier;
963 }
964
965 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800966 * Adds the specified call to the main list of live calls.
967 *
968 * @param call The call to add.
969 */
970 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700971 Log.v(this, "addCall(%s)", call);
972
973 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700974 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700975
Santos Cordondf399862014-08-06 04:39:15 -0700976 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700977 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700978 for (CallsManagerListener listener : mListeners) {
979 listener.onCallAdded(call);
980 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700981 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800982 }
983
Sailesh Nepal810735e2014-03-18 18:15:46 -0700984 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700985 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700986
Santos Cordon672321e2014-08-21 14:38:58 -0700987 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700988 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700989 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700990
991 boolean shouldNotify = false;
992 if (mCalls.contains(call)) {
993 mCalls.remove(call);
994 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700995 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800996
Sailesh Nepale59bb192014-04-01 18:33:59 -0700997 // Only broadcast changes for calls that are being tracked.
998 if (shouldNotify) {
999 for (CallsManagerListener listener : mListeners) {
1000 listener.onCallRemoved(call);
1001 }
1002 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001003 }
1004 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001005
Sailesh Nepal810735e2014-03-18 18:15:46 -07001006 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001007 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001008 *
1009 * @param call The call.
1010 * @param newState The new state of the call.
1011 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001012 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001013 if (call == null) {
1014 return;
1015 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001016 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001017 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1018 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001019 if (newState != oldState) {
1020 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1021 // 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 -07001022 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001023 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001024 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001025 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001026 // unexpected transition occurs.
1027 call.setState(newState);
1028
1029 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001030 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001031 for (CallsManagerListener listener : mListeners) {
1032 listener.onCallStateChanged(call, oldState, newState);
1033 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001034 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001035 }
1036 }
1037 }
1038
1039 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001040 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001041 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001042 private void updateForegroundCall() {
1043 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001044 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001045 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001046 // of its state will be foreground by default and instead the connection service should
1047 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001048 // the call should play audio and listen to microphone if it wants.
1049
Santos Cordonf193ba42014-09-12 06:37:39 -07001050 // Only top-level calls can be in foreground
1051 if (call.getParentCall() != null) {
1052 continue;
1053 }
1054
Santos Cordon40f78c22014-04-07 02:11:42 -07001055 // Active calls have priority.
1056 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001057 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001058 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001059 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001060
1061 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001062 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001063 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001064 }
1065 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001066
Sailesh Nepal810735e2014-03-18 18:15:46 -07001067 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001068 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001069 Call oldForegroundCall = mForegroundCall;
1070 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001071
Santos Cordona56f2762014-03-24 15:55:53 -07001072 for (CallsManagerListener listener : mListeners) {
1073 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1074 }
Santos Cordon681663d2014-01-30 04:32:15 -08001075 }
1076 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001077
1078 private boolean isPotentialMMICode(Uri handle) {
1079 return (handle != null && handle.getSchemeSpecificPart() != null
1080 && handle.getSchemeSpecificPart().contains("#"));
1081 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001082
Tyler Gunn5b452df2014-10-01 15:02:58 -07001083 /**
1084 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1085 * MMI codes which can be dialed when one or more calls are in progress.
1086 * <P>
1087 * Checks for numbers formatted similar to the MMI codes defined in:
1088 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1089 * and
1090 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1091 *
1092 * @param handle The URI to call.
1093 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1094 */
1095 private boolean isPotentialInCallMMICode(Uri handle) {
1096 if (handle != null && handle.getSchemeSpecificPart() != null &&
1097 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1098
1099 String dialedNumber = handle.getSchemeSpecificPart();
1100 return (dialedNumber.equals("0") ||
1101 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1102 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1103 dialedNumber.equals("3") ||
1104 dialedNumber.equals("4") ||
1105 dialedNumber.equals("5"));
1106 }
1107 return false;
1108 }
1109
Santos Cordonf193ba42014-09-12 06:37:39 -07001110 private int getNumCallsWithState(int... states) {
1111 int count = 0;
1112 for (int state : states) {
1113 for (Call call : mCalls) {
1114 if (call.getState() == state) {
1115 count++;
1116 }
1117 }
1118 }
1119 return count;
1120 }
1121
1122 private boolean hasMaximumLiveCalls() {
1123 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1124 }
1125
1126 private boolean hasMaximumHoldingCalls() {
1127 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1128 }
1129
1130 private boolean hasMaximumRingingCalls() {
1131 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1132 }
1133
1134 private boolean hasMaximumOutgoingCalls() {
1135 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1136 }
1137
1138 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1139 if (hasMaximumLiveCalls()) {
1140 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1141 // have to change.
1142 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1143
Santos Cordond1766502014-09-26 15:45:39 -07001144 if (call == liveCall) {
1145 // If the call is already the foreground call, then we are golden.
1146 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1147 // state since the call was already populated into the list.
1148 return true;
1149 }
1150
Andrew Leee6595182014-09-23 18:43:05 -07001151 if (hasMaximumOutgoingCalls()) {
1152 // Disconnect the current outgoing call if it's not an emergency call. If the user
1153 // tries to make two outgoing calls to different emergency call numbers, we will try
1154 // to connect the first outgoing call.
1155 if (isEmergency) {
1156 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1157 if (!outgoingCall.isEmergencyCall()) {
1158 outgoingCall.disconnect();
1159 return true;
1160 }
1161 }
1162 return false;
1163 }
1164
Santos Cordonf193ba42014-09-12 06:37:39 -07001165 if (hasMaximumHoldingCalls()) {
1166 // There is no more room for any more calls, unless it's an emergency.
1167 if (isEmergency) {
1168 // Kill the current active call, this is easier then trying to disconnect a
1169 // holding call and hold an active call.
1170 liveCall.disconnect();
1171 return true;
1172 }
1173 return false; // No more room!
1174 }
1175
1176 // We have room for at least one more holding call at this point.
1177
1178 // First thing, if we are trying to make a call with the same phone account as the live
1179 // call, then allow it so that the connection service can make its own decision about
1180 // how to handle the new call relative to the current one.
1181 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1182 return true;
1183 } else if (call.getTargetPhoneAccount() == null) {
1184 // Without a phone account, we can't say reliably that the call will fail.
1185 // If the user chooses the same phone account as the live call, then it's
1186 // still possible that the call can be made (like with CDMA calls not supporting
1187 // hold but they still support adding a call by going immediately into conference
1188 // mode). Return true here and we'll run this code again after user chooses an
1189 // account.
1190 return true;
1191 }
1192
1193 // Try to hold the live call before attempting the new outgoing call.
1194 if (liveCall.can(PhoneCapabilities.HOLD)) {
1195 liveCall.hold();
1196 return true;
1197 }
1198
1199 // The live call cannot be held so we're out of luck here. There's no room.
1200 return false;
1201 }
1202 return true;
1203 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001204
1205 /**
Tyler Gunn504eb832014-10-13 14:15:27 -07001206 * Determines if the {@link Intent#ACTION_NEW_OUTGOING_CALL} intent can be received by another
1207 * package with priority 0, potentially providing the ability to cancel the intent before it
1208 * is received.
1209 *
1210 * @return {@code true} if the intent can be intercepted by another
1211 */
1212 private boolean canOutgoingCallBroadcastsBeIntercepted() {
1213 PackageManager packageManager = mContext.getPackageManager();
1214 Intent intent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
1215 List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
1216
1217 for (ResolveInfo info : receivers) {
1218 // Check for an interceptor with priority 0; this would potentially receive the
1219 // broadcast before Telecom and cancel it.
1220 if (info.priority == 0) {
1221 return true;
1222 }
1223 }
1224 return false;
1225 }
1226
1227 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001228 * Dumps the state of the {@link CallsManager}.
1229 *
1230 * @param pw The {@code IndentingPrintWriter} to write the state to.
1231 */
1232 public void dump(IndentingPrintWriter pw) {
1233 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1234 pw.increaseIndent();
1235 if (mCalls != null) {
1236 pw.println("mCalls: ");
1237 pw.increaseIndent();
1238 for (Call call : mCalls) {
1239 pw.println(call);
1240 }
1241 pw.decreaseIndent();
1242 }
1243 pw.decreaseIndent();
1244 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001245}