blob: 420bdf845e8d26156e23491e803113fdf3e62f7b [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 -070022
23import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070024import android.telecom.AudioState;
25import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070026import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070027import android.telecom.GatewayInfo;
28import android.telecom.ParcelableConference;
29import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070030import android.telecom.PhoneCapabilities;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070031import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080032
Santos Cordonf193ba42014-09-12 06:37:39 -070033import com.android.internal.util.ArrayUtils;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070034import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal810735e2014-03-18 18:15:46 -070035import com.google.common.collect.ImmutableCollection;
36import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080037
Santos Cordonf193ba42014-09-12 06:37:39 -070038import java.util.Arrays;
Jay Shraunera82c8f72014-08-14 15:49:16 -070039import java.util.Collections;
Santos Cordon7cdb9092014-07-24 21:33:33 -070040import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070041import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070042import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070043import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080044
Ben Giladdd8c6082013-12-30 14:44:08 -080045/**
46 * Singleton.
47 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070048 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080049 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070050 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080051 */
Santos Cordon64c7e962014-07-02 15:15:27 -070052public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080053
Santos Cordondf399862014-08-06 04:39:15 -070054 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070055 interface CallsManagerListener {
56 void onCallAdded(Call call);
57 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070058 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070059 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070060 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070061 ConnectionServiceWrapper oldService,
62 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070063 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070064 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070065 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070066 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070067 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070068 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070069 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070070 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070071 }
72
Tyler Gunn91d43cf2014-09-17 12:19:39 -070073 /**
74 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
75 */
76 private static CallsManager INSTANCE = null;
77
78 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080079
Santos Cordonf193ba42014-09-12 06:37:39 -070080 private static final int MAXIMUM_LIVE_CALLS = 1;
81 private static final int MAXIMUM_HOLD_CALLS = 1;
82 private static final int MAXIMUM_RINGING_CALLS = 1;
83 private static final int MAXIMUM_OUTGOING_CALLS = 1;
84
85 private static final int[] LIVE_CALL_STATES =
86 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
87
88 private static final int[] OUTGOING_CALL_STATES =
89 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
90
Santos Cordon8e8b8d22013-12-19 14:14:05 -080091 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070092 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
93 * calls are added to the map and removed when the calls move to the disconnected state.
Jay Shraunera82c8f72014-08-14 15:49:16 -070094 *
95 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
96 * load factor before resizing, 1 means we only expect a single thread to
97 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -080098 */
Jay Shraunera82c8f72014-08-14 15:49:16 -070099 private final Set<Call> mCalls = Collections.newSetFromMap(
100 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800101
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700102 private final ConnectionServiceRepository mConnectionServiceRepository;
103 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
104 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700105 private final CallAudioManager mCallAudioManager;
106 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700107 // For this set initial table size to 16 because we add 13 listeners in
108 // the CallsManager constructor.
109 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
110 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700111 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700112 private final WiredHeadsetManager mWiredHeadsetManager;
113 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700114 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700115 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700116 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700117 private final Context mContext;
118 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
119 private final MissedCallNotifier mMissedCallNotifier;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700120
121 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700122 * The call the user is currently interacting with. This is the call that should have audio
123 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800124 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700125 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800126
Santos Cordon766d04f2014-05-06 10:28:25 -0700127 /** Singleton accessor. */
128 static CallsManager getInstance() {
129 return INSTANCE;
130 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800131
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800132 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700133 * Sets the static singleton instance.
134 *
135 * @param instance The instance to set.
136 */
137 static void initialize(CallsManager instance) {
138 INSTANCE = instance;
139 }
140
141 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700142 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800143 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700144 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
145 PhoneAccountRegistrar phoneAccountRegistrar) {
146 mContext = context;
147 mPhoneAccountRegistrar = phoneAccountRegistrar;
148 mMissedCallNotifier = missedCallNotifier;
149 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
150 mWiredHeadsetManager = new WiredHeadsetManager(context);
151 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700152 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700153 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
154 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
155 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
156 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700157 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700158 mCallLogManager = new CallLogManager(context);
159 mInCallController = new InCallController(context);
160 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
161 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
162 context);
Santos Cordonae193062014-05-21 21:21:49 -0700163
Santos Cordondeb8c892014-05-30 01:38:03 -0700164 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700165 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700166 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700167 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700168 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700169 mListeners.add(new RingbackPlayer(this, playerFactory));
170 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700171 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700172 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700173 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700174 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700175 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700176 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800177 }
178
Santos Cordon766d04f2014-05-06 10:28:25 -0700179 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700180 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700181 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700182
Tyler Gunncde2e502014-08-12 14:35:58 -0700183 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700184 if (!mCalls.contains(call)) {
185 // Call was not added previously in startOutgoingCall due to it being a potential MMI
186 // code, so add it now.
187 addCall(call);
188 }
189
190 // The call's ConnectionService has been updated.
191 for (CallsManagerListener listener : mListeners) {
192 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700193 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700194
195 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700196 }
197
198 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700199 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700200 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700201
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700202 // TODO: Replace disconnect cause with more specific disconnect causes.
Andrew Lee701dc002014-09-11 21:29:12 -0700203 markCallAsDisconnected(call, disconnectCause);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700204 }
205
206 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700207 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700208 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700209 setCallState(incomingCall, CallState.RINGING);
210
211 if (hasMaximumRingingCalls()) {
212 incomingCall.reject(false, null);
213 // since the call was not added to the list of calls, we have to call the missed
214 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700215 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700216 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
217 } else {
218 addCall(incomingCall);
219 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700220 }
221
222 @Override
223 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700224 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700225 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800226 }
227
Ihab Awadcb387ac2014-05-28 16:49:38 -0700228 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700229 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700230 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700231 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700232 }
233 }
234
Evan Charlton352105c2014-06-03 14:10:54 -0700235 @Override
236 public void onPostDialWait(Call call, String remaining) {
237 mInCallController.onPostDialWait(call, remaining);
238 }
239
Santos Cordona1610702014-06-04 20:22:56 -0700240 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700241 public void onParentChanged(Call call) {
242 for (CallsManagerListener listener : mListeners) {
243 listener.onIsConferencedChanged(call);
244 }
245 }
246
247 @Override
248 public void onChildrenChanged(Call call) {
249 for (CallsManagerListener listener : mListeners) {
250 listener.onIsConferencedChanged(call);
251 }
252 }
253
Ihab Awadff7493a2014-06-10 13:47:44 -0700254 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700255 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700256 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700257 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700258 }
259 }
260
Andrew Lee4a796602014-07-11 17:23:03 -0700261 @Override
262 public void onVideoStateChanged(Call call) {
263 for (CallsManagerListener listener : mListeners) {
264 listener.onVideoStateChanged(call);
265 }
266 }
267
Sailesh Nepal810735e2014-03-18 18:15:46 -0700268 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700269 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700270 }
271
272 Call getForegroundCall() {
273 return mForegroundCall;
274 }
275
Santos Cordonae193062014-05-21 21:21:49 -0700276 Ringer getRinger() {
277 return mRinger;
278 }
279
Santos Cordonf3671a62014-05-29 21:51:53 -0700280 InCallController getInCallController() {
281 return mInCallController;
282 }
283
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700284 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700285 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700286 if (call.isEmergencyCall()) {
287 return true;
288 }
289 }
290 return false;
291 }
292
Ihab Awad6fb37c82014-08-07 19:48:57 -0700293 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700294 return mCallAudioManager.getAudioState();
295 }
296
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700297 boolean isTtySupported() {
298 return mTtyManager.isTtySupported();
299 }
300
301 int getCurrentTtyMode() {
302 return mTtyManager.getCurrentTtyMode();
303 }
304
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800305 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700306 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800307 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700308 * @param phoneAccountHandle The phone account which contains the component name of the
309 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800310 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800311 */
Evan Charlton89176372014-07-19 18:23:09 -0700312 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800313 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700314 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700315 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700316 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700317 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700318 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700319 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700320 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700321 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700322 true /* isIncoming */,
323 false /* isConference */);
324
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700325 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700326 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700327 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700328 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800329 }
330
Nancy Chen0d3076c2014-07-30 14:45:44 -0700331 /**
332 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
333 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700334 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700335 * @param phoneAccountHandle The phone account which contains the component name of the
336 * connection service to use for this call.
337 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700338 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700339 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700340 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
341 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700342 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700343 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700344 List<PhoneAccountHandle> accounts =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700345 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
Nancy Chen309198e2014-09-15 18:02:49 -0700346 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700347 phoneAccountHandle = null;
348 }
349 }
350
351 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700352 // No preset account, check if default exists that supports the URI scheme for the
353 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700354 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700355 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700356 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700357 if (defaultAccountHandle != null) {
358 phoneAccountHandle = defaultAccountHandle;
359 }
360 }
361
Nancy Chen0d3076c2014-07-30 14:45:44 -0700362 // Create a call with original handle. The handle may be changed when the call is attached
363 // to a connection service, but in most cases will remain the same.
Santos Cordonf193ba42014-09-12 06:37:39 -0700364 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700365 mContext,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700366 mConnectionServiceRepository,
367 handle,
368 null /* gatewayInfo */,
369 null /* connectionManagerPhoneAccount */,
370 phoneAccountHandle,
371 false /* isIncoming */,
372 false /* isConference */);
Nancy Chena9d91da2014-08-12 14:31:06 -0700373 call.setExtras(extras);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700374
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700375 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
376 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700377
378 // Do not support any more live calls. Our options are to move a call to hold, disconnect
379 // a call, or cancel this call altogether.
380 if (!makeRoomForOutgoingCall(call, isEmergencyCall)) {
381 // just cancel at this point.
382 return null;
383 }
384
385 if (phoneAccountHandle == null && !isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700386 // This is the state where the user is expected to select an account
387 call.setState(CallState.PRE_DIAL_WAIT);
388 } else {
389 call.setState(CallState.CONNECTING);
390 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700391
Yorke Leeb701f6d2014-08-12 14:04:19 -0700392 if (!isPotentialMMICode(handle)) {
393 addCall(call);
394 } else {
395 call.addListener(this);
396 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700397
398 return call;
399 }
400
Ben Gilada0d9f752014-02-26 11:49:03 -0800401 /**
Yorke Lee33501632014-03-17 19:24:12 -0700402 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800403 *
Yorke Lee33501632014-03-17 19:24:12 -0700404 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700405 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700406 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700407 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700408 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800409 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700410 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
411 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700412 if (call == null) {
413 // don't do anything if the call no longer exists
414 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700415 return;
416 }
417
Nancy Chen201b4372014-09-08 14:18:24 -0700418 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700419
420 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700421 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700422 } else {
423 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
424 Log.pii(uriHandle), Log.pii(handle));
425 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700426
Nancy Chen0d3076c2014-07-30 14:45:44 -0700427 call.setHandle(uriHandle);
428 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700429 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700430 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700431
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700432 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
433 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700434 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700435 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700436 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700437 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700438
Santos Cordonf193ba42014-09-12 06:37:39 -0700439 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700440 // If the account has been set, proceed to place the outgoing call.
441 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700442 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700443 }
Yorke Leef98fb572014-03-05 10:56:55 -0800444 }
445
446 /**
Santos Cordona1610702014-06-04 20:22:56 -0700447 * Attempts to start a conference call for the specified call.
448 *
Santos Cordon12d61822014-07-29 16:02:20 -0700449 * @param call The call to conference.
450 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700451 */
Santos Cordon12d61822014-07-29 16:02:20 -0700452 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700453 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700454 }
455
456 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700457 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
458 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800459 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700460 *
461 * @param call The call to answer.
462 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800463 */
Andrew Lee38931d02014-07-16 10:17:36 -0700464 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700465 if (!mCalls.contains(call)) {
466 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800467 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700468 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700469 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700470 if (mForegroundCall != null && mForegroundCall != call &&
471 (mForegroundCall.isActive() ||
472 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700473 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
474 // This call does not support hold. If it is from a different connection
475 // service, then disconnect it, otherwise allow the connection service to
476 // figure out the right states.
477 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
478 mForegroundCall.disconnect();
479 }
480 } else {
481 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
482 mForegroundCall, call);
483 mForegroundCall.hold();
484 }
Santos Cordondf399862014-08-06 04:39:15 -0700485 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700486 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700487 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700488 }
489
Santos Cordona56f2762014-03-24 15:55:53 -0700490 for (CallsManagerListener listener : mListeners) {
491 listener.onIncomingCallAnswered(call);
492 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800493
Santos Cordon61d0f702014-02-19 02:52:23 -0800494 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700495 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700496 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800497 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800498 }
499
500 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700501 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
502 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800503 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800504 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700505 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700506 if (!mCalls.contains(call)) {
507 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800508 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700509 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700510 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700511 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700512 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800513 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800514 }
515
516 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700517 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700518 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700519 * @param digit The DTMF digit to play.
520 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700521 void playDtmfTone(Call call, char digit) {
522 if (!mCalls.contains(call)) {
523 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700524 } else {
525 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700526 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700527 }
528 }
529
530 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700531 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700532 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700533 void stopDtmfTone(Call call) {
534 if (!mCalls.contains(call)) {
535 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700536 } else {
537 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700538 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700539 }
540 }
541
542 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700543 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700544 */
Evan Charlton352105c2014-06-03 14:10:54 -0700545 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700546 if (!mCalls.contains(call)) {
547 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700548 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700549 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700550 }
551 }
552
553 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700554 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800555 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
556 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800557 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700558 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700559 Log.v(this, "disconnectCall %s", call);
560
Sailesh Nepale59bb192014-04-01 18:33:59 -0700561 if (!mCalls.contains(call)) {
562 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800563 } else {
564 call.disconnect();
565 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800566 }
Santos Cordon681663d2014-01-30 04:32:15 -0800567
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700568 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700569 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700570 */
571 void disconnectAllCalls() {
572 Log.v(this, "disconnectAllCalls");
573
574 for (Call call : mCalls) {
575 disconnectCall(call);
576 }
577 }
578
Nancy Chenbc9ef172014-08-15 17:11:57 -0700579
Tyler Gunn61b92102014-08-19 07:42:20 -0700580 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700581 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700582 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
583 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700584 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700585 void holdCall(Call call) {
586 if (!mCalls.contains(call)) {
587 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700588 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700589 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700590 call.hold();
591 }
592 }
593
594 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700595 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700596 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
597 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700598 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700599 void unholdCall(Call call) {
600 if (!mCalls.contains(call)) {
601 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700602 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700603 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700604 for (Call c : mCalls) {
605 if (c != null && c.isAlive() && c != call) {
606 c.hold();
607 }
608 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700609 call.unhold();
610 }
611 }
612
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700613 /** Called by the in-call UI to change the mute state. */
614 void mute(boolean shouldMute) {
615 mCallAudioManager.mute(shouldMute);
616 }
617
618 /**
619 * Called by the in-call UI to change the audio route, for example to change from earpiece to
620 * speaker phone.
621 */
622 void setAudioRoute(int route) {
623 mCallAudioManager.setAudioRoute(route);
624 }
625
Yorke Leed1346872014-07-28 14:38:45 -0700626 /** Called by the in-call UI to turn the proximity sensor on. */
627 void turnOnProximitySensor() {
628 mProximitySensorManager.turnOn();
629 }
630
631 /**
632 * Called by the in-call UI to turn the proximity sensor off.
633 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
634 * the screen will be kept off until the proximity sensor goes negative.
635 */
636 void turnOffProximitySensor(boolean screenOnImmediately) {
637 mProximitySensorManager.turnOff(screenOnImmediately);
638 }
639
Evan Charlton89176372014-07-19 18:23:09 -0700640 void phoneAccountSelected(Call call, PhoneAccountHandle account) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700641 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700642 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700643 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700644 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
645 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
646 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
647 // respecting a rewritten number or a canceled number. This is unlikely since
648 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
649 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700650 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700651
652 // Note: emergency calls never go through account selection dialog so they never
653 // arrive here.
654 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700655 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700656 } else {
657 call.disconnect();
658 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700659 }
660 }
661
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700662 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700663 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700664 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700665 for (CallsManagerListener listener : mListeners) {
666 listener.onAudioStateChanged(oldAudioState, newAudioState);
667 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700668 }
669
Sailesh Nepale59bb192014-04-01 18:33:59 -0700670 void markCallAsRinging(Call call) {
671 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800672 }
673
Sailesh Nepale59bb192014-04-01 18:33:59 -0700674 void markCallAsDialing(Call call) {
675 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800676 }
677
Sailesh Nepale59bb192014-04-01 18:33:59 -0700678 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700679 if (call.getConnectTimeMillis() == 0) {
680 call.setConnectTimeMillis(System.currentTimeMillis());
681 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700682 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700683
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700684 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700685 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700686 }
Santos Cordon681663d2014-01-30 04:32:15 -0800687 }
688
Sailesh Nepale59bb192014-04-01 18:33:59 -0700689 void markCallAsOnHold(Call call) {
690 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700691 }
692
Santos Cordon049b7b62014-01-30 05:34:26 -0800693 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700694 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
695 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800696 *
Andrew Lee701dc002014-09-11 21:29:12 -0700697 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800698 */
Andrew Lee701dc002014-09-11 21:29:12 -0700699 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
700 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700701 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700702 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700703 }
704
Ben Gilada0d9f752014-02-26 11:49:03 -0800705 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700706 * Removes an existing disconnected call, and notifies the in-call app.
707 */
708 void markCallAsRemoved(Call call) {
709 removeCall(call);
710 }
711
712 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700713 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700714 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700715 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700716 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700717 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700718 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700719 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700720 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700721 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700722 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700723 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700724 }
725 }
726 }
727
Santos Cordondeb8c892014-05-30 01:38:03 -0700728 boolean hasAnyCalls() {
729 return !mCalls.isEmpty();
730 }
731
Santos Cordonc7e85d42014-05-22 02:51:48 -0700732 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700733 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700734 }
735
736 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700737 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700738 }
739
Santos Cordondeb8c892014-05-30 01:38:03 -0700740 boolean onMediaButton(int type) {
741 if (hasAnyCalls()) {
742 if (HeadsetMediaButton.SHORT_PRESS == type) {
743 Call ringingCall = getFirstCallWithState(CallState.RINGING);
744 if (ringingCall == null) {
745 mCallAudioManager.toggleMute();
746 return true;
747 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700748 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700749 return true;
750 }
751 } else if (HeadsetMediaButton.LONG_PRESS == type) {
752 Log.d(this, "handleHeadsetHook: longpress -> hangup");
753 Call callToHangup = getFirstCallWithState(
754 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
755 if (callToHangup != null) {
756 callToHangup.disconnect();
757 return true;
758 }
759 }
760 }
761 return false;
762 }
763
764 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700765 * Checks to see if the specified call is the only high-level call and if so, enable the
766 * "Add-call" button. We allow you to add a second call but not a third or beyond.
767 *
768 * @param call The call to test for add-call.
769 * @return Whether the add-call feature should be enabled for the call.
770 */
771 protected boolean isAddCallCapable(Call call) {
772 if (call.getParentCall() != null) {
773 // Never true for child calls.
774 return false;
775 }
776
777 // Loop through all the other calls and there exists a top level (has no parent) call
778 // that is not the specified call, return false.
779 for (Call otherCall : mCalls) {
780 if (call != otherCall && otherCall.getParentCall() == null) {
781 return false;
782 }
783 }
784 return true;
785 }
786
Santos Cordonf193ba42014-09-12 06:37:39 -0700787 Call getFirstCallWithState(int... states) {
788 return getFirstCallWithState(null, states);
789 }
790
Santos Cordon10838c22014-06-11 17:36:04 -0700791 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700792 * Returns the first call that it finds with the given states. The states are treated as having
793 * priority order so that any call with the first state will be returned before any call with
794 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700795 *
796 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700797 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700798 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700799 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700800 // check the foreground first
801 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
802 return mForegroundCall;
803 }
804
Santos Cordondeb8c892014-05-30 01:38:03 -0700805 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700806 if (Objects.equals(callToSkip, call)) {
807 continue;
808 }
809
810 // Only operate on top-level calls
811 if (call.getParentCall() != null) {
812 continue;
813 }
814
Santos Cordondeb8c892014-05-30 01:38:03 -0700815 if (currentState == call.getState()) {
816 return call;
817 }
818 }
819 }
820 return null;
821 }
822
Santos Cordon0fbe6322014-08-14 04:04:25 -0700823 Call createConferenceCall(
824 PhoneAccountHandle phoneAccount,
825 ParcelableConference parcelableConference) {
826 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700827 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700828 mConnectionServiceRepository,
829 null /* handle */,
830 null /* gatewayInfo */,
831 null /* connectionManagerPhoneAccount */,
832 phoneAccount,
833 false /* isIncoming */,
834 true /* isConference */);
835
836 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700837 if (call.getState() == CallState.ACTIVE) {
838 call.setConnectTimeMillis(System.currentTimeMillis());
839 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700840 call.setCallCapabilities(parcelableConference.getCapabilities());
841
842 // TODO: Move this to be a part of addCall()
843 call.addListener(this);
844 addCall(call);
845 return call;
846 }
847
Yorke Leef86db2e2014-09-12 17:56:48 -0700848 /**
849 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
850 */
851 int getCallState() {
852 return mPhoneStateBroadcaster.getCallState();
853 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700854
Santos Cordon4b2c1192014-03-19 18:15:38 -0700855 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700856 * Retrieves the {@link PhoneAccountRegistrar}.
857 *
858 * @return The {@link PhoneAccountRegistrar}.
859 */
860 PhoneAccountRegistrar getPhoneAccountRegistrar() {
861 return mPhoneAccountRegistrar;
862 }
863
864 /**
865 * Retrieves the {@link MissedCallNotifier}
866 * @return The {@link MissedCallNotifier}.
867 */
868 MissedCallNotifier getMissedCallNotifier() {
869 return mMissedCallNotifier;
870 }
871
872 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800873 * Adds the specified call to the main list of live calls.
874 *
875 * @param call The call to add.
876 */
877 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700878 Log.v(this, "addCall(%s)", call);
879
880 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700881 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700882
Santos Cordondf399862014-08-06 04:39:15 -0700883 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -0700884 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700885 for (CallsManagerListener listener : mListeners) {
886 listener.onCallAdded(call);
887 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700888 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800889 }
890
Sailesh Nepal810735e2014-03-18 18:15:46 -0700891 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700892 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700893
Santos Cordon672321e2014-08-21 14:38:58 -0700894 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -0700895 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700896 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700897
898 boolean shouldNotify = false;
899 if (mCalls.contains(call)) {
900 mCalls.remove(call);
901 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700902 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800903
Sailesh Nepale59bb192014-04-01 18:33:59 -0700904 // Only broadcast changes for calls that are being tracked.
905 if (shouldNotify) {
906 for (CallsManagerListener listener : mListeners) {
907 listener.onCallRemoved(call);
908 }
909 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700910 }
911 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800912
Sailesh Nepal810735e2014-03-18 18:15:46 -0700913 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700914 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700915 *
916 * @param call The call.
917 * @param newState The new state of the call.
918 */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700919 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700920 if (call == null) {
921 return;
922 }
Ihab Awad6fb37c82014-08-07 19:48:57 -0700923 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -0700924 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
925 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700926 if (newState != oldState) {
927 // Unfortunately, in the telephony world the radio is king. So if the call notifies
928 // 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 -0700929 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -0700930 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -0700931 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -0700932 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -0700933 // unexpected transition occurs.
934 call.setState(newState);
935
936 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700937 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700938 for (CallsManagerListener listener : mListeners) {
939 listener.onCallStateChanged(call, oldState, newState);
940 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700941 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800942 }
943 }
944 }
945
946 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700947 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800948 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700949 private void updateForegroundCall() {
950 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700951 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -0700952 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700953 // of its state will be foreground by default and instead the connection service should
954 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700955 // the call should play audio and listen to microphone if it wants.
956
Santos Cordonf193ba42014-09-12 06:37:39 -0700957 // Only top-level calls can be in foreground
958 if (call.getParentCall() != null) {
959 continue;
960 }
961
Santos Cordon40f78c22014-04-07 02:11:42 -0700962 // Active calls have priority.
963 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700964 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800965 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700966 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700967
968 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700969 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700970 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800971 }
972 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800973
Sailesh Nepal810735e2014-03-18 18:15:46 -0700974 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700975 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700976 Call oldForegroundCall = mForegroundCall;
977 mForegroundCall = newForegroundCall;
978
Santos Cordona56f2762014-03-24 15:55:53 -0700979 for (CallsManagerListener listener : mListeners) {
980 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
981 }
Santos Cordon681663d2014-01-30 04:32:15 -0800982 }
983 }
Yorke Leeb701f6d2014-08-12 14:04:19 -0700984
985 private boolean isPotentialMMICode(Uri handle) {
986 return (handle != null && handle.getSchemeSpecificPart() != null
987 && handle.getSchemeSpecificPart().contains("#"));
988 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700989
990 private int getNumCallsWithState(int... states) {
991 int count = 0;
992 for (int state : states) {
993 for (Call call : mCalls) {
994 if (call.getState() == state) {
995 count++;
996 }
997 }
998 }
999 return count;
1000 }
1001
1002 private boolean hasMaximumLiveCalls() {
1003 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1004 }
1005
1006 private boolean hasMaximumHoldingCalls() {
1007 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1008 }
1009
1010 private boolean hasMaximumRingingCalls() {
1011 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1012 }
1013
1014 private boolean hasMaximumOutgoingCalls() {
1015 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1016 }
1017
1018 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1019 if (hasMaximumLiveCalls()) {
1020 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1021 // have to change.
1022 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1023
1024 if (hasMaximumHoldingCalls()) {
1025 // There is no more room for any more calls, unless it's an emergency.
1026 if (isEmergency) {
1027 // Kill the current active call, this is easier then trying to disconnect a
1028 // holding call and hold an active call.
1029 liveCall.disconnect();
1030 return true;
1031 }
1032 return false; // No more room!
1033 }
1034
1035 // We have room for at least one more holding call at this point.
1036
1037 // First thing, if we are trying to make a call with the same phone account as the live
1038 // call, then allow it so that the connection service can make its own decision about
1039 // how to handle the new call relative to the current one.
1040 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1041 return true;
1042 } else if (call.getTargetPhoneAccount() == null) {
1043 // Without a phone account, we can't say reliably that the call will fail.
1044 // If the user chooses the same phone account as the live call, then it's
1045 // still possible that the call can be made (like with CDMA calls not supporting
1046 // hold but they still support adding a call by going immediately into conference
1047 // mode). Return true here and we'll run this code again after user chooses an
1048 // account.
1049 return true;
1050 }
1051
1052 // Try to hold the live call before attempting the new outgoing call.
1053 if (liveCall.can(PhoneCapabilities.HOLD)) {
1054 liveCall.hold();
1055 return true;
1056 }
1057
1058 // The live call cannot be held so we're out of luck here. There's no room.
1059 return false;
1060 }
1061 return true;
1062 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001063
1064 /**
1065 * Dumps the state of the {@link CallsManager}.
1066 *
1067 * @param pw The {@code IndentingPrintWriter} to write the state to.
1068 */
1069 public void dump(IndentingPrintWriter pw) {
1070 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1071 pw.increaseIndent();
1072 if (mCalls != null) {
1073 pw.println("mCalls: ");
1074 pw.increaseIndent();
1075 for (Call call : mCalls) {
1076 pw.println(call);
1077 }
1078 pw.decreaseIndent();
1079 }
1080 pw.decreaseIndent();
1081 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001082}