blob: 4775ff5efb194e87d61d92c06a2faaf9e0ca5489 [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 Cordon68d1a6b2014-09-19 12:25:58 -0700302 void addListener(CallsManagerListener listener) {
303 mListeners.add(listener);
304 }
305
306 void removeListener(CallsManagerListener listener) {
307 mListeners.remove(listener);
308 }
309
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800310 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700311 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800312 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700313 * @param phoneAccountHandle The phone account which contains the component name of the
314 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800315 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800316 */
Evan Charlton89176372014-07-19 18:23:09 -0700317 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800318 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700319 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700320 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700321 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700322 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700323 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700324 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700325 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700326 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700327 true /* isIncoming */,
328 false /* isConference */);
329
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700330 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700331 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700332 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700333 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800334 }
335
Nancy Chen0d3076c2014-07-30 14:45:44 -0700336 /**
337 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
338 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700339 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700340 * @param phoneAccountHandle The phone account which contains the component name of the
341 * connection service to use for this call.
342 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700343 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700344 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700345 // Create a call with original handle. The handle may be changed when the call is attached
346 // to a connection service, but in most cases will remain the same.
347 Call call = new Call(
348 mContext,
349 mConnectionServiceRepository,
350 handle,
351 null /* gatewayInfo */,
352 null /* connectionManagerPhoneAccount */,
353 null /* phoneAccountHandle */,
354 false /* isIncoming */,
355 false /* isConference */);
356
357 List<PhoneAccountHandle> accounts =
358 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
359
Nancy Chenbc9ef172014-08-15 17:11:57 -0700360 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
361 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700362 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700363 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700364 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700365 phoneAccountHandle = null;
366 }
367 }
368
369 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700370 // No preset account, check if default exists that supports the URI scheme for the
371 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700372 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700373 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700374 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700375 if (defaultAccountHandle != null) {
376 phoneAccountHandle = defaultAccountHandle;
377 }
378 }
379
Nancy Chen1c5926f2014-09-17 14:44:14 -0700380 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700381
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700382 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
383 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700384
385 // Do not support any more live calls. Our options are to move a call to hold, disconnect
386 // a call, or cancel this call altogether.
387 if (!makeRoomForOutgoingCall(call, isEmergencyCall)) {
388 // just cancel at this point.
389 return null;
390 }
391
Nancy Chen1ccab202014-09-18 15:28:36 -0700392 if (phoneAccountHandle == null && accounts.size() > 1 && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700393 // This is the state where the user is expected to select an account
394 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700395 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700396 } else {
397 call.setState(CallState.CONNECTING);
398 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700399
Nancy Chen1c5926f2014-09-17 14:44:14 -0700400 call.setExtras(extras);
401
Yorke Leeb701f6d2014-08-12 14:04:19 -0700402 if (!isPotentialMMICode(handle)) {
403 addCall(call);
404 } else {
405 call.addListener(this);
406 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700407
408 return call;
409 }
410
Ben Gilada0d9f752014-02-26 11:49:03 -0800411 /**
Yorke Lee33501632014-03-17 19:24:12 -0700412 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800413 *
Yorke Lee33501632014-03-17 19:24:12 -0700414 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700415 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700416 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700417 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700418 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800419 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700420 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
421 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700422 if (call == null) {
423 // don't do anything if the call no longer exists
424 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700425 return;
426 }
427
Nancy Chen201b4372014-09-08 14:18:24 -0700428 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700429
430 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700431 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700432 } else {
433 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
434 Log.pii(uriHandle), Log.pii(handle));
435 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700436
Nancy Chen0d3076c2014-07-30 14:45:44 -0700437 call.setHandle(uriHandle);
438 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700439 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700440 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700441
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700442 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
443 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700444 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700445 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700446 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700447 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700448
Santos Cordonf193ba42014-09-12 06:37:39 -0700449 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700450 // If the account has been set, proceed to place the outgoing call.
451 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700452 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700453 }
Yorke Leef98fb572014-03-05 10:56:55 -0800454 }
455
456 /**
Santos Cordona1610702014-06-04 20:22:56 -0700457 * Attempts to start a conference call for the specified call.
458 *
Santos Cordon12d61822014-07-29 16:02:20 -0700459 * @param call The call to conference.
460 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700461 */
Santos Cordon12d61822014-07-29 16:02:20 -0700462 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700463 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700464 }
465
466 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700467 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
468 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800469 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700470 *
471 * @param call The call to answer.
472 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800473 */
Andrew Lee38931d02014-07-16 10:17:36 -0700474 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700475 if (!mCalls.contains(call)) {
476 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800477 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700478 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700479 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700480 if (mForegroundCall != null && mForegroundCall != call &&
481 (mForegroundCall.isActive() ||
482 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700483 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
484 // This call does not support hold. If it is from a different connection
485 // service, then disconnect it, otherwise allow the connection service to
486 // figure out the right states.
487 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
488 mForegroundCall.disconnect();
489 }
490 } else {
491 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
492 mForegroundCall, call);
493 mForegroundCall.hold();
494 }
Santos Cordondf399862014-08-06 04:39:15 -0700495 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700496 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700497 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700498 }
499
Santos Cordona56f2762014-03-24 15:55:53 -0700500 for (CallsManagerListener listener : mListeners) {
501 listener.onIncomingCallAnswered(call);
502 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800503
Santos Cordon61d0f702014-02-19 02:52:23 -0800504 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700505 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700506 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800507 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800508 }
509
510 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700511 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
512 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800513 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800514 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700515 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700516 if (!mCalls.contains(call)) {
517 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800518 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700519 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700520 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700521 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700522 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800523 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800524 }
525
526 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700527 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700528 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700529 * @param digit The DTMF digit to play.
530 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700531 void playDtmfTone(Call call, char digit) {
532 if (!mCalls.contains(call)) {
533 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700534 } else {
535 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700536 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700537 }
538 }
539
540 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700541 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700542 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700543 void stopDtmfTone(Call call) {
544 if (!mCalls.contains(call)) {
545 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700546 } else {
547 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700548 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700549 }
550 }
551
552 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700553 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700554 */
Evan Charlton352105c2014-06-03 14:10:54 -0700555 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700556 if (!mCalls.contains(call)) {
557 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700558 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700559 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700560 }
561 }
562
563 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700564 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800565 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
566 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800567 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700568 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700569 Log.v(this, "disconnectCall %s", call);
570
Sailesh Nepale59bb192014-04-01 18:33:59 -0700571 if (!mCalls.contains(call)) {
572 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800573 } else {
574 call.disconnect();
575 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800576 }
Santos Cordon681663d2014-01-30 04:32:15 -0800577
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700578 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700579 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700580 */
581 void disconnectAllCalls() {
582 Log.v(this, "disconnectAllCalls");
583
584 for (Call call : mCalls) {
585 disconnectCall(call);
586 }
587 }
588
Nancy Chenbc9ef172014-08-15 17:11:57 -0700589
Tyler Gunn61b92102014-08-19 07:42:20 -0700590 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700591 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700592 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
593 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700594 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700595 void holdCall(Call call) {
596 if (!mCalls.contains(call)) {
597 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700598 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700599 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700600 call.hold();
601 }
602 }
603
604 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700605 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700606 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
607 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700608 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700609 void unholdCall(Call call) {
610 if (!mCalls.contains(call)) {
611 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700612 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700613 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700614 for (Call c : mCalls) {
615 if (c != null && c.isAlive() && c != call) {
616 c.hold();
617 }
618 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700619 call.unhold();
620 }
621 }
622
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700623 /** Called by the in-call UI to change the mute state. */
624 void mute(boolean shouldMute) {
625 mCallAudioManager.mute(shouldMute);
626 }
627
628 /**
629 * Called by the in-call UI to change the audio route, for example to change from earpiece to
630 * speaker phone.
631 */
632 void setAudioRoute(int route) {
633 mCallAudioManager.setAudioRoute(route);
634 }
635
Yorke Leed1346872014-07-28 14:38:45 -0700636 /** Called by the in-call UI to turn the proximity sensor on. */
637 void turnOnProximitySensor() {
638 mProximitySensorManager.turnOn();
639 }
640
641 /**
642 * Called by the in-call UI to turn the proximity sensor off.
643 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
644 * the screen will be kept off until the proximity sensor goes negative.
645 */
646 void turnOffProximitySensor(boolean screenOnImmediately) {
647 mProximitySensorManager.turnOff(screenOnImmediately);
648 }
649
Evan Charlton89176372014-07-19 18:23:09 -0700650 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700651 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700652 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700653 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700654 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
655 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
656 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
657 // respecting a rewritten number or a canceled number. This is unlikely since
658 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
659 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700660 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700661
662 // Note: emergency calls never go through account selection dialog so they never
663 // arrive here.
664 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700665 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700666 } else {
667 call.disconnect();
668 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700669 }
670 }
671
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700672 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700673 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700674 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700675 for (CallsManagerListener listener : mListeners) {
676 listener.onAudioStateChanged(oldAudioState, newAudioState);
677 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700678 }
679
Sailesh Nepale59bb192014-04-01 18:33:59 -0700680 void markCallAsRinging(Call call) {
681 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800682 }
683
Sailesh Nepale59bb192014-04-01 18:33:59 -0700684 void markCallAsDialing(Call call) {
685 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800686 }
687
Sailesh Nepale59bb192014-04-01 18:33:59 -0700688 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700689 if (call.getConnectTimeMillis() == 0) {
690 call.setConnectTimeMillis(System.currentTimeMillis());
691 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700692 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700693
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700694 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700695 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700696 }
Santos Cordon681663d2014-01-30 04:32:15 -0800697 }
698
Sailesh Nepale59bb192014-04-01 18:33:59 -0700699 void markCallAsOnHold(Call call) {
700 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700701 }
702
Santos Cordon049b7b62014-01-30 05:34:26 -0800703 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700704 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
705 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800706 *
Andrew Lee701dc002014-09-11 21:29:12 -0700707 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800708 */
Andrew Lee701dc002014-09-11 21:29:12 -0700709 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
710 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700711 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700712 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700713 }
714
Ben Gilada0d9f752014-02-26 11:49:03 -0800715 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700716 * Removes an existing disconnected call, and notifies the in-call app.
717 */
718 void markCallAsRemoved(Call call) {
719 removeCall(call);
720 }
721
722 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700723 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700724 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700725 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700726 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700727 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700728 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700729 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700730 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700731 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700732 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700733 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700734 }
735 }
736 }
737
Santos Cordondeb8c892014-05-30 01:38:03 -0700738 boolean hasAnyCalls() {
739 return !mCalls.isEmpty();
740 }
741
Santos Cordonc7e85d42014-05-22 02:51:48 -0700742 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700743 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700744 }
745
746 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700747 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700748 }
749
Santos Cordondeb8c892014-05-30 01:38:03 -0700750 boolean onMediaButton(int type) {
751 if (hasAnyCalls()) {
752 if (HeadsetMediaButton.SHORT_PRESS == type) {
753 Call ringingCall = getFirstCallWithState(CallState.RINGING);
754 if (ringingCall == null) {
755 mCallAudioManager.toggleMute();
756 return true;
757 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700758 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700759 return true;
760 }
761 } else if (HeadsetMediaButton.LONG_PRESS == type) {
762 Log.d(this, "handleHeadsetHook: longpress -> hangup");
763 Call callToHangup = getFirstCallWithState(
764 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
765 if (callToHangup != null) {
766 callToHangup.disconnect();
767 return true;
768 }
769 }
770 }
771 return false;
772 }
773
774 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700775 * Checks to see if the specified call is the only high-level call and if so, enable the
776 * "Add-call" button. We allow you to add a second call but not a third or beyond.
777 *
778 * @param call The call to test for add-call.
779 * @return Whether the add-call feature should be enabled for the call.
780 */
781 protected boolean isAddCallCapable(Call call) {
782 if (call.getParentCall() != null) {
783 // Never true for child calls.
784 return false;
785 }
786
Andrew Leec11fcd92014-09-19 18:31:46 -0700787 // Use canManageConference as a mechanism to check if the call is CDMA.
788 // Disable "Add Call" for CDMA calls which are conference calls.
789 boolean canManageConference = PhoneCapabilities.MANAGE_CONFERENCE
790 == (call.getCallCapabilities() & PhoneCapabilities.MANAGE_CONFERENCE);
791 if (call.isConference() && !canManageConference) {
792 return false;
793 }
794
Santos Cordon10838c22014-06-11 17:36:04 -0700795 // Loop through all the other calls and there exists a top level (has no parent) call
796 // that is not the specified call, return false.
797 for (Call otherCall : mCalls) {
798 if (call != otherCall && otherCall.getParentCall() == null) {
799 return false;
800 }
801 }
802 return true;
803 }
804
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700805 Call getRingingCall() {
806 return getFirstCallWithState(CallState.RINGING);
807 }
808
809 Call getActiveCall() {
810 return getFirstCallWithState(CallState.ACTIVE);
811 }
812
Nancy Chen05a9e402014-09-26 14:14:32 -0700813 Call getDialingCall() {
814 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700815 }
816
817 Call getHeldCall() {
818 return getFirstCallWithState(CallState.ON_HOLD);
819 }
820
Santos Cordonf193ba42014-09-12 06:37:39 -0700821 Call getFirstCallWithState(int... states) {
822 return getFirstCallWithState(null, states);
823 }
824
Santos Cordon10838c22014-06-11 17:36:04 -0700825 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700826 * Returns the first call that it finds with the given states. The states are treated as having
827 * priority order so that any call with the first state will be returned before any call with
828 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700829 *
830 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700831 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700832 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700833 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700834 // check the foreground first
835 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
836 return mForegroundCall;
837 }
838
Santos Cordondeb8c892014-05-30 01:38:03 -0700839 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700840 if (Objects.equals(callToSkip, call)) {
841 continue;
842 }
843
844 // Only operate on top-level calls
845 if (call.getParentCall() != null) {
846 continue;
847 }
848
Santos Cordondeb8c892014-05-30 01:38:03 -0700849 if (currentState == call.getState()) {
850 return call;
851 }
852 }
853 }
854 return null;
855 }
856
Santos Cordon0fbe6322014-08-14 04:04:25 -0700857 Call createConferenceCall(
858 PhoneAccountHandle phoneAccount,
859 ParcelableConference parcelableConference) {
860 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700861 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700862 mConnectionServiceRepository,
863 null /* handle */,
864 null /* gatewayInfo */,
865 null /* connectionManagerPhoneAccount */,
866 phoneAccount,
867 false /* isIncoming */,
868 true /* isConference */);
869
870 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700871 if (call.getState() == CallState.ACTIVE) {
872 call.setConnectTimeMillis(System.currentTimeMillis());
873 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700874 call.setCallCapabilities(parcelableConference.getCapabilities());
875
876 // TODO: Move this to be a part of addCall()
877 call.addListener(this);
878 addCall(call);
879 return call;
880 }
881
Yorke Leef86db2e2014-09-12 17:56:48 -0700882 /**
883 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
884 */
885 int getCallState() {
886 return mPhoneStateBroadcaster.getCallState();
887 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700888
Santos Cordon4b2c1192014-03-19 18:15:38 -0700889 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700890 * Retrieves the {@link PhoneAccountRegistrar}.
891 *
892 * @return The {@link PhoneAccountRegistrar}.
893 */
894 PhoneAccountRegistrar getPhoneAccountRegistrar() {
895 return mPhoneAccountRegistrar;
896 }
897
898 /**
899 * Retrieves the {@link MissedCallNotifier}
900 * @return The {@link MissedCallNotifier}.
901 */
902 MissedCallNotifier getMissedCallNotifier() {
903 return mMissedCallNotifier;
904 }
905
906 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800907 * Adds the specified call to the main list of live calls.
908 *
909 * @param call The call to add.
910 */
911 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700912 Log.v(this, "addCall(%s)", call);
913
914 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700915 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700916
Santos Cordondf399862014-08-06 04:39:15 -0700917 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700918 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700919 for (CallsManagerListener listener : mListeners) {
920 listener.onCallAdded(call);
921 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700922 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800923 }
924
Sailesh Nepal810735e2014-03-18 18:15:46 -0700925 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700926 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700927
Santos Cordon672321e2014-08-21 14:38:58 -0700928 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700929 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700930 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700931
932 boolean shouldNotify = false;
933 if (mCalls.contains(call)) {
934 mCalls.remove(call);
935 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700936 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800937
Sailesh Nepale59bb192014-04-01 18:33:59 -0700938 // Only broadcast changes for calls that are being tracked.
939 if (shouldNotify) {
940 for (CallsManagerListener listener : mListeners) {
941 listener.onCallRemoved(call);
942 }
943 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700944 }
945 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800946
Sailesh Nepal810735e2014-03-18 18:15:46 -0700947 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700948 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700949 *
950 * @param call The call.
951 * @param newState The new state of the call.
952 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700953 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700954 if (call == null) {
955 return;
956 }
Ihab Awad6fb37c82014-08-07 19:48:57 -0700957 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -0700958 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
959 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700960 if (newState != oldState) {
961 // Unfortunately, in the telephony world the radio is king. So if the call notifies
962 // 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 -0700963 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700964 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700965 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700966 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700967 // unexpected transition occurs.
968 call.setState(newState);
969
970 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700971 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700972 for (CallsManagerListener listener : mListeners) {
973 listener.onCallStateChanged(call, oldState, newState);
974 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700975 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800976 }
977 }
978 }
979
980 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700981 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800982 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700983 private void updateForegroundCall() {
984 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700985 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700986 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700987 // of its state will be foreground by default and instead the connection service should
988 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700989 // the call should play audio and listen to microphone if it wants.
990
Santos Cordonf193ba42014-09-12 06:37:39 -0700991 // Only top-level calls can be in foreground
992 if (call.getParentCall() != null) {
993 continue;
994 }
995
Santos Cordon40f78c22014-04-07 02:11:42 -0700996 // Active calls have priority.
997 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700998 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800999 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001000 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001001
1002 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001003 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001004 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001005 }
1006 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001007
Sailesh Nepal810735e2014-03-18 18:15:46 -07001008 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001009 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001010 Call oldForegroundCall = mForegroundCall;
1011 mForegroundCall = newForegroundCall;
Ihab Awada1ce6672014-09-15 15:02:06 -07001012 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1013 mForegroundCall.unhold();
1014 }
Santos Cordona56f2762014-03-24 15:55:53 -07001015 for (CallsManagerListener listener : mListeners) {
1016 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1017 }
Santos Cordon681663d2014-01-30 04:32:15 -08001018 }
1019 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001020
1021 private boolean isPotentialMMICode(Uri handle) {
1022 return (handle != null && handle.getSchemeSpecificPart() != null
1023 && handle.getSchemeSpecificPart().contains("#"));
1024 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001025
1026 private int getNumCallsWithState(int... states) {
1027 int count = 0;
1028 for (int state : states) {
1029 for (Call call : mCalls) {
1030 if (call.getState() == state) {
1031 count++;
1032 }
1033 }
1034 }
1035 return count;
1036 }
1037
1038 private boolean hasMaximumLiveCalls() {
1039 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1040 }
1041
1042 private boolean hasMaximumHoldingCalls() {
1043 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1044 }
1045
1046 private boolean hasMaximumRingingCalls() {
1047 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1048 }
1049
1050 private boolean hasMaximumOutgoingCalls() {
1051 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1052 }
1053
1054 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1055 if (hasMaximumLiveCalls()) {
1056 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1057 // have to change.
1058 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1059
Andrew Leee6595182014-09-23 18:43:05 -07001060 if (hasMaximumOutgoingCalls()) {
1061 // Disconnect the current outgoing call if it's not an emergency call. If the user
1062 // tries to make two outgoing calls to different emergency call numbers, we will try
1063 // to connect the first outgoing call.
1064 if (isEmergency) {
1065 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1066 if (!outgoingCall.isEmergencyCall()) {
1067 outgoingCall.disconnect();
1068 return true;
1069 }
1070 }
1071 return false;
1072 }
1073
Santos Cordonf193ba42014-09-12 06:37:39 -07001074 if (hasMaximumHoldingCalls()) {
1075 // There is no more room for any more calls, unless it's an emergency.
1076 if (isEmergency) {
1077 // Kill the current active call, this is easier then trying to disconnect a
1078 // holding call and hold an active call.
1079 liveCall.disconnect();
1080 return true;
1081 }
1082 return false; // No more room!
1083 }
1084
1085 // We have room for at least one more holding call at this point.
1086
1087 // First thing, if we are trying to make a call with the same phone account as the live
1088 // call, then allow it so that the connection service can make its own decision about
1089 // how to handle the new call relative to the current one.
1090 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1091 return true;
1092 } else if (call.getTargetPhoneAccount() == null) {
1093 // Without a phone account, we can't say reliably that the call will fail.
1094 // If the user chooses the same phone account as the live call, then it's
1095 // still possible that the call can be made (like with CDMA calls not supporting
1096 // hold but they still support adding a call by going immediately into conference
1097 // mode). Return true here and we'll run this code again after user chooses an
1098 // account.
1099 return true;
1100 }
1101
1102 // Try to hold the live call before attempting the new outgoing call.
1103 if (liveCall.can(PhoneCapabilities.HOLD)) {
1104 liveCall.hold();
1105 return true;
1106 }
1107
1108 // The live call cannot be held so we're out of luck here. There's no room.
1109 return false;
1110 }
1111 return true;
1112 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001113
1114 /**
1115 * Dumps the state of the {@link CallsManager}.
1116 *
1117 * @param pw The {@code IndentingPrintWriter} to write the state to.
1118 */
1119 public void dump(IndentingPrintWriter pw) {
1120 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1121 pw.increaseIndent();
1122 if (mCalls != null) {
1123 pw.println("mCalls: ");
1124 pw.increaseIndent();
1125 for (Call call : mCalls) {
1126 pw.println(call);
1127 }
1128 pw.decreaseIndent();
1129 }
1130 pw.decreaseIndent();
1131 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001132}