blob: ed555b74e5ca3625fa076d3f836126c265e3ec1d [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;
28import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070029import android.telecom.PhoneCapabilities;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070030import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080031
Tyler Gunn91d43cf2014-09-17 12:19:39 -070032import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal810735e2014-03-18 18:15:46 -070033import com.google.common.collect.ImmutableCollection;
34import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Jay Shraunera82c8f72014-08-14 15:49:16 -070036import java.util.Collections;
Santos Cordon7cdb9092014-07-24 21:33:33 -070037import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070038import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070039import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070040import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080041
Ben Giladdd8c6082013-12-30 14:44:08 -080042/**
43 * Singleton.
44 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070045 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080046 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070047 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080048 */
Santos Cordon64c7e962014-07-02 15:15:27 -070049public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080050
Santos Cordondf399862014-08-06 04:39:15 -070051 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070052 interface CallsManagerListener {
53 void onCallAdded(Call call);
54 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070055 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070056 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070057 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070058 ConnectionServiceWrapper oldService,
59 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070060 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070061 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070062 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070063 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070064 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070065 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070066 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070067 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 }
69
Tyler Gunn91d43cf2014-09-17 12:19:39 -070070 /**
71 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
72 */
73 private static CallsManager INSTANCE = null;
74
75 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080076
Santos Cordonf193ba42014-09-12 06:37:39 -070077 private static final int MAXIMUM_LIVE_CALLS = 1;
78 private static final int MAXIMUM_HOLD_CALLS = 1;
79 private static final int MAXIMUM_RINGING_CALLS = 1;
80 private static final int MAXIMUM_OUTGOING_CALLS = 1;
81
82 private static final int[] LIVE_CALL_STATES =
83 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
84
85 private static final int[] OUTGOING_CALL_STATES =
86 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
87
Santos Cordon8e8b8d22013-12-19 14:14:05 -080088 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070089 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
90 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070091 *
92 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
93 * load factor before resizing, 1 means we only expect a single thread to
94 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -080095 */
Jay Shraunera82c8f72014-08-14 15:49:16 -070096 private final Set<Call> mCalls = Collections.newSetFromMap(
97 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -080098
Tyler Gunn91d43cf2014-09-17 12:19:39 -070099 private final ConnectionServiceRepository mConnectionServiceRepository;
100 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
101 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700102 private final CallAudioManager mCallAudioManager;
103 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700104 // For this set initial table size to 16 because we add 13 listeners in
105 // the CallsManager constructor.
106 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
107 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700108 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700109 private final WiredHeadsetManager mWiredHeadsetManager;
110 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700111 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700112 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700113 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700114 private final Context mContext;
115 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
116 private final MissedCallNotifier mMissedCallNotifier;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700117
118 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700119 * The call the user is currently interacting with. This is the call that should have audio
120 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800121 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700122 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800123
Santos Cordon766d04f2014-05-06 10:28:25 -0700124 /** Singleton accessor. */
125 static CallsManager getInstance() {
126 return INSTANCE;
127 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800128
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800129 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700130 * Sets the static singleton instance.
131 *
132 * @param instance The instance to set.
133 */
134 static void initialize(CallsManager instance) {
135 INSTANCE = instance;
136 }
137
138 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700139 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800140 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700141 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
142 PhoneAccountRegistrar phoneAccountRegistrar) {
143 mContext = context;
144 mPhoneAccountRegistrar = phoneAccountRegistrar;
145 mMissedCallNotifier = missedCallNotifier;
146 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
147 mWiredHeadsetManager = new WiredHeadsetManager(context);
148 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700149 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700150 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
151 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
152 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
153 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700154 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700155 mCallLogManager = new CallLogManager(context);
156 mInCallController = new InCallController(context);
157 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
158 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
159 context);
Santos Cordonae193062014-05-21 21:21:49 -0700160
Santos Cordondeb8c892014-05-30 01:38:03 -0700161 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700162 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700163 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700164 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700165 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700166 mListeners.add(new RingbackPlayer(this, playerFactory));
167 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700168 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700169 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700170 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700171 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700172 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700173 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800174 }
175
Santos Cordon766d04f2014-05-06 10:28:25 -0700176 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700177 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700178 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700179
Tyler Gunncde2e502014-08-12 14:35:58 -0700180 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700181 if (!mCalls.contains(call)) {
182 // Call was not added previously in startOutgoingCall due to it being a potential MMI
183 // code, so add it now.
184 addCall(call);
185 }
186
187 // The call's ConnectionService has been updated.
188 for (CallsManagerListener listener : mListeners) {
189 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700190 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700191
192 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700193 }
194
195 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700196 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700197 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700198
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700199 // TODO: Replace disconnect cause with more specific disconnect causes.
Andrew Lee701dc002014-09-11 21:29:12 -0700200 markCallAsDisconnected(call, disconnectCause);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700201 }
202
203 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700204 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700205 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700206 setCallState(incomingCall, CallState.RINGING);
207
208 if (hasMaximumRingingCalls()) {
209 incomingCall.reject(false, null);
210 // since the call was not added to the list of calls, we have to call the missed
211 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700212 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700213 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
214 } else {
215 addCall(incomingCall);
216 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700217 }
218
219 @Override
220 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700221 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700222 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800223 }
224
Ihab Awadcb387ac2014-05-28 16:49:38 -0700225 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700226 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700227 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700228 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700229 }
230 }
231
Evan Charlton352105c2014-06-03 14:10:54 -0700232 @Override
233 public void onPostDialWait(Call call, String remaining) {
234 mInCallController.onPostDialWait(call, remaining);
235 }
236
Santos Cordona1610702014-06-04 20:22:56 -0700237 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700238 public void onParentChanged(Call call) {
239 for (CallsManagerListener listener : mListeners) {
240 listener.onIsConferencedChanged(call);
241 }
242 }
243
244 @Override
245 public void onChildrenChanged(Call call) {
246 for (CallsManagerListener listener : mListeners) {
247 listener.onIsConferencedChanged(call);
248 }
249 }
250
Ihab Awadff7493a2014-06-10 13:47:44 -0700251 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700252 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700253 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700254 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700255 }
256 }
257
Andrew Lee4a796602014-07-11 17:23:03 -0700258 @Override
259 public void onVideoStateChanged(Call call) {
260 for (CallsManagerListener listener : mListeners) {
261 listener.onVideoStateChanged(call);
262 }
263 }
264
Sailesh Nepal810735e2014-03-18 18:15:46 -0700265 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700266 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700267 }
268
269 Call getForegroundCall() {
270 return mForegroundCall;
271 }
272
Santos Cordonae193062014-05-21 21:21:49 -0700273 Ringer getRinger() {
274 return mRinger;
275 }
276
Santos Cordonf3671a62014-05-29 21:51:53 -0700277 InCallController getInCallController() {
278 return mInCallController;
279 }
280
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700281 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700282 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700283 if (call.isEmergencyCall()) {
284 return true;
285 }
286 }
287 return false;
288 }
289
Ihab Awad6fb37c82014-08-07 19:48:57 -0700290 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700291 return mCallAudioManager.getAudioState();
292 }
293
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700294 boolean isTtySupported() {
295 return mTtyManager.isTtySupported();
296 }
297
298 int getCurrentTtyMode() {
299 return mTtyManager.getCurrentTtyMode();
300 }
301
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800302 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700303 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800304 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700305 * @param phoneAccountHandle The phone account which contains the component name of the
306 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800307 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800308 */
Evan Charlton89176372014-07-19 18:23:09 -0700309 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800310 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700311 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700312 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700313 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700314 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700315 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700316 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700317 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700318 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700319 true /* isIncoming */,
320 false /* isConference */);
321
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700322 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700323 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700324 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700325 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800326 }
327
Nancy Chen0d3076c2014-07-30 14:45:44 -0700328 /**
329 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
330 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700331 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700332 * @param phoneAccountHandle The phone account which contains the component name of the
333 * connection service to use for this call.
334 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700335 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700336 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700337 // Create a call with original handle. The handle may be changed when the call is attached
338 // to a connection service, but in most cases will remain the same.
339 Call call = new Call(
340 mContext,
341 mConnectionServiceRepository,
342 handle,
343 null /* gatewayInfo */,
344 null /* connectionManagerPhoneAccount */,
345 null /* phoneAccountHandle */,
346 false /* isIncoming */,
347 false /* isConference */);
348
349 List<PhoneAccountHandle> accounts =
350 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
351
Nancy Chenbc9ef172014-08-15 17:11:57 -0700352 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
353 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700354 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700355 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700356 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700357 phoneAccountHandle = null;
358 }
359 }
360
361 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700362 // No preset account, check if default exists that supports the URI scheme for the
363 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700364 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700365 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700366 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700367 if (defaultAccountHandle != null) {
368 phoneAccountHandle = defaultAccountHandle;
369 }
370 }
371
Nancy Chen1c5926f2014-09-17 14:44:14 -0700372 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700373
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700374 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
375 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700376
377 // Do not support any more live calls. Our options are to move a call to hold, disconnect
378 // a call, or cancel this call altogether.
379 if (!makeRoomForOutgoingCall(call, isEmergencyCall)) {
380 // just cancel at this point.
381 return null;
382 }
383
Nancy Chen1ccab202014-09-18 15:28:36 -0700384 if (phoneAccountHandle == null && accounts.size() > 1 && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700385 // This is the state where the user is expected to select an account
386 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700387 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700388 } else {
389 call.setState(CallState.CONNECTING);
390 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700391
Nancy Chen1c5926f2014-09-17 14:44:14 -0700392 call.setExtras(extras);
393
Yorke Leeb701f6d2014-08-12 14:04:19 -0700394 if (!isPotentialMMICode(handle)) {
395 addCall(call);
396 } else {
397 call.addListener(this);
398 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700399
400 return call;
401 }
402
Ben Gilada0d9f752014-02-26 11:49:03 -0800403 /**
Yorke Lee33501632014-03-17 19:24:12 -0700404 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800405 *
Yorke Lee33501632014-03-17 19:24:12 -0700406 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700407 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700408 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700409 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700410 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800411 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700412 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
413 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700414 if (call == null) {
415 // don't do anything if the call no longer exists
416 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700417 return;
418 }
419
Nancy Chen201b4372014-09-08 14:18:24 -0700420 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700421
422 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700423 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700424 } else {
425 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
426 Log.pii(uriHandle), Log.pii(handle));
427 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700428
Nancy Chen0d3076c2014-07-30 14:45:44 -0700429 call.setHandle(uriHandle);
430 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700431 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700432 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700433
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700434 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
435 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700436 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700437 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700438 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700439 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700440
Santos Cordonf193ba42014-09-12 06:37:39 -0700441 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700442 // If the account has been set, proceed to place the outgoing call.
443 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700444 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700445 }
Yorke Leef98fb572014-03-05 10:56:55 -0800446 }
447
448 /**
Santos Cordona1610702014-06-04 20:22:56 -0700449 * Attempts to start a conference call for the specified call.
450 *
Santos Cordon12d61822014-07-29 16:02:20 -0700451 * @param call The call to conference.
452 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700453 */
Santos Cordon12d61822014-07-29 16:02:20 -0700454 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700455 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700456 }
457
458 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700459 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
460 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800461 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700462 *
463 * @param call The call to answer.
464 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800465 */
Andrew Lee38931d02014-07-16 10:17:36 -0700466 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700467 if (!mCalls.contains(call)) {
468 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800469 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700470 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700471 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700472 if (mForegroundCall != null && mForegroundCall != call &&
473 (mForegroundCall.isActive() ||
474 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700475 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
476 // This call does not support hold. If it is from a different connection
477 // service, then disconnect it, otherwise allow the connection service to
478 // figure out the right states.
479 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
480 mForegroundCall.disconnect();
481 }
482 } else {
483 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
484 mForegroundCall, call);
485 mForegroundCall.hold();
486 }
Santos Cordondf399862014-08-06 04:39:15 -0700487 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700488 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700489 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700490 }
491
Santos Cordona56f2762014-03-24 15:55:53 -0700492 for (CallsManagerListener listener : mListeners) {
493 listener.onIncomingCallAnswered(call);
494 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800495
Santos Cordon61d0f702014-02-19 02:52:23 -0800496 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700497 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700498 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800499 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800500 }
501
502 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700503 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
504 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800505 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800506 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700507 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700508 if (!mCalls.contains(call)) {
509 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800510 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700511 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700512 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700513 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700514 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800515 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800516 }
517
518 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700519 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700520 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700521 * @param digit The DTMF digit to play.
522 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700523 void playDtmfTone(Call call, char digit) {
524 if (!mCalls.contains(call)) {
525 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700526 } else {
527 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700528 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700529 }
530 }
531
532 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700533 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700534 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700535 void stopDtmfTone(Call call) {
536 if (!mCalls.contains(call)) {
537 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700538 } else {
539 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700540 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700541 }
542 }
543
544 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700545 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700546 */
Evan Charlton352105c2014-06-03 14:10:54 -0700547 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700548 if (!mCalls.contains(call)) {
549 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700550 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700551 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700552 }
553 }
554
555 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700556 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800557 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
558 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800559 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700560 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700561 Log.v(this, "disconnectCall %s", call);
562
Sailesh Nepale59bb192014-04-01 18:33:59 -0700563 if (!mCalls.contains(call)) {
564 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800565 } else {
566 call.disconnect();
567 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800568 }
Santos Cordon681663d2014-01-30 04:32:15 -0800569
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700570 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700571 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700572 */
573 void disconnectAllCalls() {
574 Log.v(this, "disconnectAllCalls");
575
576 for (Call call : mCalls) {
577 disconnectCall(call);
578 }
579 }
580
Nancy Chenbc9ef172014-08-15 17:11:57 -0700581
Tyler Gunn61b92102014-08-19 07:42:20 -0700582 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700583 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700584 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
585 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700586 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700587 void holdCall(Call call) {
588 if (!mCalls.contains(call)) {
589 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700590 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700591 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700592 call.hold();
593 }
594 }
595
596 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700597 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700598 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
599 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700600 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700601 void unholdCall(Call call) {
602 if (!mCalls.contains(call)) {
603 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700604 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700605 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700606 for (Call c : mCalls) {
607 if (c != null && c.isAlive() && c != call) {
608 c.hold();
609 }
610 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700611 call.unhold();
612 }
613 }
614
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700615 /** Called by the in-call UI to change the mute state. */
616 void mute(boolean shouldMute) {
617 mCallAudioManager.mute(shouldMute);
618 }
619
620 /**
621 * Called by the in-call UI to change the audio route, for example to change from earpiece to
622 * speaker phone.
623 */
624 void setAudioRoute(int route) {
625 mCallAudioManager.setAudioRoute(route);
626 }
627
Yorke Leed1346872014-07-28 14:38:45 -0700628 /** Called by the in-call UI to turn the proximity sensor on. */
629 void turnOnProximitySensor() {
630 mProximitySensorManager.turnOn();
631 }
632
633 /**
634 * Called by the in-call UI to turn the proximity sensor off.
635 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
636 * the screen will be kept off until the proximity sensor goes negative.
637 */
638 void turnOffProximitySensor(boolean screenOnImmediately) {
639 mProximitySensorManager.turnOff(screenOnImmediately);
640 }
641
Evan Charlton89176372014-07-19 18:23:09 -0700642 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700643 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700644 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700645 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700646 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
647 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
648 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
649 // respecting a rewritten number or a canceled number. This is unlikely since
650 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
651 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700652 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700653
654 // Note: emergency calls never go through account selection dialog so they never
655 // arrive here.
656 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700657 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700658 } else {
659 call.disconnect();
660 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700661 }
662 }
663
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700664 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700665 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700666 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700667 for (CallsManagerListener listener : mListeners) {
668 listener.onAudioStateChanged(oldAudioState, newAudioState);
669 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700670 }
671
Sailesh Nepale59bb192014-04-01 18:33:59 -0700672 void markCallAsRinging(Call call) {
673 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800674 }
675
Sailesh Nepale59bb192014-04-01 18:33:59 -0700676 void markCallAsDialing(Call call) {
677 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800678 }
679
Sailesh Nepale59bb192014-04-01 18:33:59 -0700680 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700681 if (call.getConnectTimeMillis() == 0) {
682 call.setConnectTimeMillis(System.currentTimeMillis());
683 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700684 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700685
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700686 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700687 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700688 }
Santos Cordon681663d2014-01-30 04:32:15 -0800689 }
690
Sailesh Nepale59bb192014-04-01 18:33:59 -0700691 void markCallAsOnHold(Call call) {
692 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700693 }
694
Santos Cordon049b7b62014-01-30 05:34:26 -0800695 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700696 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
697 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800698 *
Andrew Lee701dc002014-09-11 21:29:12 -0700699 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800700 */
Andrew Lee701dc002014-09-11 21:29:12 -0700701 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
702 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700703 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700704 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700705 }
706
Ben Gilada0d9f752014-02-26 11:49:03 -0800707 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700708 * Removes an existing disconnected call, and notifies the in-call app.
709 */
710 void markCallAsRemoved(Call call) {
711 removeCall(call);
712 }
713
714 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700715 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700716 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700717 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700718 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700719 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700720 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700721 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700722 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700723 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700724 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700725 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700726 }
727 }
728 }
729
Santos Cordondeb8c892014-05-30 01:38:03 -0700730 boolean hasAnyCalls() {
731 return !mCalls.isEmpty();
732 }
733
Santos Cordonc7e85d42014-05-22 02:51:48 -0700734 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700735 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700736 }
737
738 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700739 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700740 }
741
Santos Cordondeb8c892014-05-30 01:38:03 -0700742 boolean onMediaButton(int type) {
743 if (hasAnyCalls()) {
744 if (HeadsetMediaButton.SHORT_PRESS == type) {
745 Call ringingCall = getFirstCallWithState(CallState.RINGING);
746 if (ringingCall == null) {
747 mCallAudioManager.toggleMute();
748 return true;
749 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700750 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700751 return true;
752 }
753 } else if (HeadsetMediaButton.LONG_PRESS == type) {
754 Log.d(this, "handleHeadsetHook: longpress -> hangup");
755 Call callToHangup = getFirstCallWithState(
756 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
757 if (callToHangup != null) {
758 callToHangup.disconnect();
759 return true;
760 }
761 }
762 }
763 return false;
764 }
765
766 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700767 * Checks to see if the specified call is the only high-level call and if so, enable the
768 * "Add-call" button. We allow you to add a second call but not a third or beyond.
769 *
770 * @param call The call to test for add-call.
771 * @return Whether the add-call feature should be enabled for the call.
772 */
773 protected boolean isAddCallCapable(Call call) {
774 if (call.getParentCall() != null) {
775 // Never true for child calls.
776 return false;
777 }
778
779 // Loop through all the other calls and there exists a top level (has no parent) call
780 // that is not the specified call, return false.
781 for (Call otherCall : mCalls) {
782 if (call != otherCall && otherCall.getParentCall() == null) {
783 return false;
784 }
785 }
786 return true;
787 }
788
Santos Cordonf193ba42014-09-12 06:37:39 -0700789 Call getFirstCallWithState(int... states) {
790 return getFirstCallWithState(null, states);
791 }
792
Santos Cordon10838c22014-06-11 17:36:04 -0700793 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700794 * Returns the first call that it finds with the given states. The states are treated as having
795 * priority order so that any call with the first state will be returned before any call with
796 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700797 *
798 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700799 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700800 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700801 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700802 // check the foreground first
803 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
804 return mForegroundCall;
805 }
806
Santos Cordondeb8c892014-05-30 01:38:03 -0700807 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700808 if (Objects.equals(callToSkip, call)) {
809 continue;
810 }
811
812 // Only operate on top-level calls
813 if (call.getParentCall() != null) {
814 continue;
815 }
816
Santos Cordondeb8c892014-05-30 01:38:03 -0700817 if (currentState == call.getState()) {
818 return call;
819 }
820 }
821 }
822 return null;
823 }
824
Santos Cordon0fbe6322014-08-14 04:04:25 -0700825 Call createConferenceCall(
826 PhoneAccountHandle phoneAccount,
827 ParcelableConference parcelableConference) {
828 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700829 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700830 mConnectionServiceRepository,
831 null /* handle */,
832 null /* gatewayInfo */,
833 null /* connectionManagerPhoneAccount */,
834 phoneAccount,
835 false /* isIncoming */,
836 true /* isConference */);
837
838 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700839 if (call.getState() == CallState.ACTIVE) {
840 call.setConnectTimeMillis(System.currentTimeMillis());
841 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700842 call.setCallCapabilities(parcelableConference.getCapabilities());
843
844 // TODO: Move this to be a part of addCall()
845 call.addListener(this);
846 addCall(call);
847 return call;
848 }
849
Yorke Leef86db2e2014-09-12 17:56:48 -0700850 /**
851 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
852 */
853 int getCallState() {
854 return mPhoneStateBroadcaster.getCallState();
855 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700856
Santos Cordon4b2c1192014-03-19 18:15:38 -0700857 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700858 * Retrieves the {@link PhoneAccountRegistrar}.
859 *
860 * @return The {@link PhoneAccountRegistrar}.
861 */
862 PhoneAccountRegistrar getPhoneAccountRegistrar() {
863 return mPhoneAccountRegistrar;
864 }
865
866 /**
867 * Retrieves the {@link MissedCallNotifier}
868 * @return The {@link MissedCallNotifier}.
869 */
870 MissedCallNotifier getMissedCallNotifier() {
871 return mMissedCallNotifier;
872 }
873
874 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800875 * Adds the specified call to the main list of live calls.
876 *
877 * @param call The call to add.
878 */
879 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700880 Log.v(this, "addCall(%s)", call);
881
882 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700883 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700884
Santos Cordondf399862014-08-06 04:39:15 -0700885 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700886 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700887 for (CallsManagerListener listener : mListeners) {
888 listener.onCallAdded(call);
889 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700890 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800891 }
892
Sailesh Nepal810735e2014-03-18 18:15:46 -0700893 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700894 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700895
Santos Cordon672321e2014-08-21 14:38:58 -0700896 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700897 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700898 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700899
900 boolean shouldNotify = false;
901 if (mCalls.contains(call)) {
902 mCalls.remove(call);
903 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700904 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800905
Sailesh Nepale59bb192014-04-01 18:33:59 -0700906 // Only broadcast changes for calls that are being tracked.
907 if (shouldNotify) {
908 for (CallsManagerListener listener : mListeners) {
909 listener.onCallRemoved(call);
910 }
911 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700912 }
913 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800914
Sailesh Nepal810735e2014-03-18 18:15:46 -0700915 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700916 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700917 *
918 * @param call The call.
919 * @param newState The new state of the call.
920 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700921 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700922 if (call == null) {
923 return;
924 }
Ihab Awad6fb37c82014-08-07 19:48:57 -0700925 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -0700926 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
927 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700928 if (newState != oldState) {
929 // Unfortunately, in the telephony world the radio is king. So if the call notifies
930 // 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 -0700931 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700932 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700933 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700934 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700935 // unexpected transition occurs.
936 call.setState(newState);
937
938 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700939 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700940 for (CallsManagerListener listener : mListeners) {
941 listener.onCallStateChanged(call, oldState, newState);
942 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700943 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800944 }
945 }
946 }
947
948 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700949 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800950 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700951 private void updateForegroundCall() {
952 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700953 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700954 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700955 // of its state will be foreground by default and instead the connection service should
956 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700957 // the call should play audio and listen to microphone if it wants.
958
Santos Cordonf193ba42014-09-12 06:37:39 -0700959 // Only top-level calls can be in foreground
960 if (call.getParentCall() != null) {
961 continue;
962 }
963
Santos Cordon40f78c22014-04-07 02:11:42 -0700964 // Active calls have priority.
965 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700966 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800967 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700968 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700969
970 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700971 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700972 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800973 }
974 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800975
Sailesh Nepal810735e2014-03-18 18:15:46 -0700976 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700977 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700978 Call oldForegroundCall = mForegroundCall;
979 mForegroundCall = newForegroundCall;
Ihab Awada1ce6672014-09-15 15:02:06 -0700980 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
981 mForegroundCall.unhold();
982 }
Santos Cordona56f2762014-03-24 15:55:53 -0700983 for (CallsManagerListener listener : mListeners) {
984 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
985 }
Santos Cordon681663d2014-01-30 04:32:15 -0800986 }
987 }
Yorke Leeb701f6d2014-08-12 14:04:19 -0700988
989 private boolean isPotentialMMICode(Uri handle) {
990 return (handle != null && handle.getSchemeSpecificPart() != null
991 && handle.getSchemeSpecificPart().contains("#"));
992 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700993
994 private int getNumCallsWithState(int... states) {
995 int count = 0;
996 for (int state : states) {
997 for (Call call : mCalls) {
998 if (call.getState() == state) {
999 count++;
1000 }
1001 }
1002 }
1003 return count;
1004 }
1005
1006 private boolean hasMaximumLiveCalls() {
1007 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1008 }
1009
1010 private boolean hasMaximumHoldingCalls() {
1011 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1012 }
1013
1014 private boolean hasMaximumRingingCalls() {
1015 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1016 }
1017
1018 private boolean hasMaximumOutgoingCalls() {
1019 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1020 }
1021
1022 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1023 if (hasMaximumLiveCalls()) {
1024 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1025 // have to change.
1026 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1027
Andrew Leee6595182014-09-23 18:43:05 -07001028 if (hasMaximumOutgoingCalls()) {
1029 // Disconnect the current outgoing call if it's not an emergency call. If the user
1030 // tries to make two outgoing calls to different emergency call numbers, we will try
1031 // to connect the first outgoing call.
1032 if (isEmergency) {
1033 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1034 if (!outgoingCall.isEmergencyCall()) {
1035 outgoingCall.disconnect();
1036 return true;
1037 }
1038 }
1039 return false;
1040 }
1041
Santos Cordonf193ba42014-09-12 06:37:39 -07001042 if (hasMaximumHoldingCalls()) {
1043 // There is no more room for any more calls, unless it's an emergency.
1044 if (isEmergency) {
1045 // Kill the current active call, this is easier then trying to disconnect a
1046 // holding call and hold an active call.
1047 liveCall.disconnect();
1048 return true;
1049 }
1050 return false; // No more room!
1051 }
1052
1053 // We have room for at least one more holding call at this point.
1054
1055 // First thing, if we are trying to make a call with the same phone account as the live
1056 // call, then allow it so that the connection service can make its own decision about
1057 // how to handle the new call relative to the current one.
1058 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1059 return true;
1060 } else if (call.getTargetPhoneAccount() == null) {
1061 // Without a phone account, we can't say reliably that the call will fail.
1062 // If the user chooses the same phone account as the live call, then it's
1063 // still possible that the call can be made (like with CDMA calls not supporting
1064 // hold but they still support adding a call by going immediately into conference
1065 // mode). Return true here and we'll run this code again after user chooses an
1066 // account.
1067 return true;
1068 }
1069
1070 // Try to hold the live call before attempting the new outgoing call.
1071 if (liveCall.can(PhoneCapabilities.HOLD)) {
1072 liveCall.hold();
1073 return true;
1074 }
1075
1076 // The live call cannot be held so we're out of luck here. There's no room.
1077 return false;
1078 }
1079 return true;
1080 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001081
1082 /**
1083 * Dumps the state of the {@link CallsManager}.
1084 *
1085 * @param pw The {@code IndentingPrintWriter} to write the state to.
1086 */
1087 public void dump(IndentingPrintWriter pw) {
1088 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1089 pw.increaseIndent();
1090 if (mCalls != null) {
1091 pw.println("mCalls: ");
1092 pw.increaseIndent();
1093 for (Call call : mCalls) {
1094 pw.println(call);
1095 }
1096 pw.decreaseIndent();
1097 }
1098 pw.decreaseIndent();
1099 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001100}