blob: dc65c9e8b2cc088102bbd575eaa9cce3c654726a [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 Cordoncf5b2912014-11-05 21:57:54 -080022import android.os.Handler;
Yorke Leee4a9c412014-11-14 16:59:42 -080023import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070024import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070025import android.telecom.AudioState;
26import android.telecom.CallState;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080027import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070028import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.GatewayInfo;
30import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070031import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070032import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070033import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070035import android.telecom.VideoProfile;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070036import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080037
Tyler Gunn91d43cf2014-09-17 12:19:39 -070038import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080039
Santos Cordon4bc02452014-11-19 15:51:12 -080040import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070041import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070042import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070043import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070044import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070045import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070046import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080047
Ben Giladdd8c6082013-12-30 14:44:08 -080048/**
49 * Singleton.
50 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070051 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080052 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070053 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080054 */
Santos Cordon64c7e962014-07-02 15:15:27 -070055public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080056
Santos Cordondf399862014-08-06 04:39:15 -070057 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 interface CallsManagerListener {
59 void onCallAdded(Call call);
60 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070061 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070062 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070063 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070064 ConnectionServiceWrapper oldService,
65 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070066 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070067 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070069 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070070 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070071 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070072 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070073 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080074 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 }
76
Tyler Gunn91d43cf2014-09-17 12:19:39 -070077 /**
78 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
79 */
Santos Cordone6a33f22014-12-02 13:19:00 -080080 private static CallsManager sInstance = null;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070081
82 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Santos Cordonf193ba42014-09-12 06:37:39 -070084 private static final int MAXIMUM_LIVE_CALLS = 1;
85 private static final int MAXIMUM_HOLD_CALLS = 1;
86 private static final int MAXIMUM_RINGING_CALLS = 1;
87 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080088 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070089
90 private static final int[] OUTGOING_CALL_STATES =
91 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
92
Santos Cordon91bd74c2014-11-07 16:10:22 -080093 private static final int[] LIVE_CALL_STATES =
94 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
95
Santos Cordon8e8b8d22013-12-19 14:14:05 -080096 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070097 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
98 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -080099 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700100 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
101 * load factor before resizing, 1 means we only expect a single thread to
102 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800103 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700104 private final Set<Call> mCalls = Collections.newSetFromMap(
105 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800106
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700107 private final ConnectionServiceRepository mConnectionServiceRepository;
108 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
109 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700110 private final CallAudioManager mCallAudioManager;
111 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800112 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700113 // For this set initial table size to 16 because we add 13 listeners in
114 // the CallsManager constructor.
115 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
116 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700117 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700118 private final WiredHeadsetManager mWiredHeadsetManager;
119 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700120 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700121 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700122 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700123 private final Context mContext;
124 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
125 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700126 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800127 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
128 /* Handler tied to thread in which CallManager was initialized. */
129 private final Handler mHandler = new Handler();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700130
Santos Cordon91bd74c2014-11-07 16:10:22 -0800131 private boolean mCanAddCall = true;
132
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700133 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700134 * The call the user is currently interacting with. This is the call that should have audio
135 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800136 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700137 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800138
Santos Cordon766d04f2014-05-06 10:28:25 -0700139 /** Singleton accessor. */
140 static CallsManager getInstance() {
Santos Cordone6a33f22014-12-02 13:19:00 -0800141 return sInstance;
Santos Cordon766d04f2014-05-06 10:28:25 -0700142 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800143
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800144 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700145 * Sets the static singleton instance.
146 *
147 * @param instance The instance to set.
148 */
149 static void initialize(CallsManager instance) {
Santos Cordone6a33f22014-12-02 13:19:00 -0800150 sInstance = instance;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700151 }
152
153 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700154 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800155 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700156 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
157 PhoneAccountRegistrar phoneAccountRegistrar) {
158 mContext = context;
159 mPhoneAccountRegistrar = phoneAccountRegistrar;
160 mMissedCallNotifier = missedCallNotifier;
161 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
162 mWiredHeadsetManager = new WiredHeadsetManager(context);
163 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700164 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700165 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
166 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
167 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
168 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700169 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700170 mCallLogManager = new CallLogManager(context);
171 mInCallController = new InCallController(context);
172 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
173 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
174 context);
Santos Cordon61e6f352014-12-03 02:53:34 -0800175 mInCallWakeLockController = new InCallWakeLockController(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700176
Santos Cordondeb8c892014-05-30 01:38:03 -0700177 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700178 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700179 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700180 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700181 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700182 mListeners.add(new RingbackPlayer(this, playerFactory));
183 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700184 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700185 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700186 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700187 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700188 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700189 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800190 }
191
Santos Cordon766d04f2014-05-06 10:28:25 -0700192 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700193 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700194 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700195
Tyler Gunncde2e502014-08-12 14:35:58 -0700196 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700197 if (!mCalls.contains(call)) {
198 // Call was not added previously in startOutgoingCall due to it being a potential MMI
199 // code, so add it now.
200 addCall(call);
201 }
202
203 // The call's ConnectionService has been updated.
204 for (CallsManagerListener listener : mListeners) {
205 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700206 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700207
208 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700209 }
210
211 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700212 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700213 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700214
Andrew Leee6288a72014-10-01 13:50:02 -0700215 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700216 }
217
218 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700219 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700220 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700221 setCallState(incomingCall, CallState.RINGING);
222
223 if (hasMaximumRingingCalls()) {
224 incomingCall.reject(false, null);
225 // since the call was not added to the list of calls, we have to call the missed
226 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700227 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700228 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
229 } else {
230 addCall(incomingCall);
231 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700232 }
233
234 @Override
235 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700236 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700237 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800238 }
239
Ihab Awadcb387ac2014-05-28 16:49:38 -0700240 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700241 public void onSuccessfulUnknownCall(Call call, int callState) {
242 setCallState(call, callState);
243 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
244 addCall(call);
245 }
246
247 @Override
248 public void onFailedUnknownCall(Call call) {
249 Log.i(this, "onFailedUnknownCall for call %s", call);
250 setCallState(call, CallState.DISCONNECTED);
251 call.removeListener(this);
252 }
253
254 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700255 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700256 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700257 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700258 }
259 }
260
Evan Charlton352105c2014-06-03 14:10:54 -0700261 @Override
262 public void onPostDialWait(Call call, String remaining) {
263 mInCallController.onPostDialWait(call, remaining);
264 }
265
Santos Cordona1610702014-06-04 20:22:56 -0700266 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700267 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700268 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800269 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700270 for (CallsManagerListener listener : mListeners) {
271 listener.onIsConferencedChanged(call);
272 }
273 }
274
275 @Override
276 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700277 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800278 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700279 for (CallsManagerListener listener : mListeners) {
280 listener.onIsConferencedChanged(call);
281 }
282 }
283
Ihab Awadff7493a2014-06-10 13:47:44 -0700284 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700285 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700286 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700287 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700288 }
289 }
290
Andrew Lee4a796602014-07-11 17:23:03 -0700291 @Override
292 public void onVideoStateChanged(Call call) {
293 for (CallsManagerListener listener : mListeners) {
294 listener.onVideoStateChanged(call);
295 }
296 }
297
Santos Cordoncf5b2912014-11-05 21:57:54 -0800298 @Override
299 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
300 mPendingCallsToDisconnect.add(call);
301 mHandler.postDelayed(new Runnable() {
302 @Override
303 public void run() {
304 if (mPendingCallsToDisconnect.remove(call)) {
305 Log.i(this, "Delayed disconnection of call: %s", call);
306 call.disconnect();
307 }
308 }
309 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
310
311 return true;
312 }
313
Santos Cordon4bc02452014-11-19 15:51:12 -0800314 Collection<Call> getCalls() {
315 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700316 }
317
318 Call getForegroundCall() {
319 return mForegroundCall;
320 }
321
Santos Cordonae193062014-05-21 21:21:49 -0700322 Ringer getRinger() {
323 return mRinger;
324 }
325
Santos Cordonf3671a62014-05-29 21:51:53 -0700326 InCallController getInCallController() {
327 return mInCallController;
328 }
329
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700330 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700331 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700332 if (call.isEmergencyCall()) {
333 return true;
334 }
335 }
336 return false;
337 }
338
Andrew Lee45506232014-10-22 17:54:33 -0700339 boolean hasVideoCall() {
340 for (Call call : mCalls) {
341 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
342 return true;
343 }
344 }
345 return false;
346 }
347
Ihab Awad6fb37c82014-08-07 19:48:57 -0700348 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700349 return mCallAudioManager.getAudioState();
350 }
351
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700352 boolean isTtySupported() {
353 return mTtyManager.isTtySupported();
354 }
355
356 int getCurrentTtyMode() {
357 return mTtyManager.getCurrentTtyMode();
358 }
359
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700360 void addListener(CallsManagerListener listener) {
361 mListeners.add(listener);
362 }
363
364 void removeListener(CallsManagerListener listener) {
365 mListeners.remove(listener);
366 }
367
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800368 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700369 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800370 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700371 * @param phoneAccountHandle The phone account which contains the component name of the
372 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800373 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800374 */
Evan Charlton89176372014-07-19 18:23:09 -0700375 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800376 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700377 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700378 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700379 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700380 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700381 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700382 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700383 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700384 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700385 true /* isIncoming */,
386 false /* isConference */);
387
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700388 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700389 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700390 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700391 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800392 }
393
Yorke Lee9250e5f2014-10-01 13:39:09 -0700394 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
395 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
396 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
397 Call call = new Call(
398 mContext,
399 mConnectionServiceRepository,
400 handle,
401 null /* gatewayInfo */,
402 null /* connectionManagerPhoneAccount */,
403 phoneAccountHandle,
404 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
405 // to the existing connection instead of trying to create a new one.
406 true /* isIncoming */,
407 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700408 call.setIsUnknown(true);
409 call.setExtras(extras);
410 call.addListener(this);
411 call.startCreateConnection(mPhoneAccountRegistrar);
412 }
413
Santos Cordoncf5b2912014-11-05 21:57:54 -0800414 private Call getNewOutgoingCall(Uri handle) {
415 // First check to see if we can reuse any of the calls that are waiting to disconnect.
416 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800417 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800418 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800419 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800420 mPendingCallsToDisconnect.remove(pendingCall);
421 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800422 reusedCall = pendingCall;
423 } else {
424 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800425 }
426 }
Yorke Lee59979512014-12-02 01:03:40 -0800427 if (reusedCall != null) {
428 return reusedCall;
429 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800430
431 // Create a call with original handle. The handle may be changed when the call is attached
432 // to a connection service, but in most cases will remain the same.
433 return new Call(
434 mContext,
435 mConnectionServiceRepository,
436 handle,
437 null /* gatewayInfo */,
438 null /* connectionManagerPhoneAccount */,
439 null /* phoneAccountHandle */,
440 false /* isIncoming */,
441 false /* isConference */);
442 }
443
Nancy Chen0d3076c2014-07-30 14:45:44 -0700444 /**
445 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
446 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700447 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700448 * @param phoneAccountHandle The phone account which contains the component name of the
449 * connection service to use for this call.
450 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700451 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700452 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800453 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700454
455 List<PhoneAccountHandle> accounts =
456 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
457
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800458 Log.v(this, "startOutgoingCall found accounts = " + accounts);
459
Nancy Chenbc95db92014-11-24 13:19:18 -0800460 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
461 // If there is an ongoing call, use the same phone account to place this new call.
462 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
463 }
464
Nancy Chenbc9ef172014-08-15 17:11:57 -0700465 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
466 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700467 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700468 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700469 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700470 phoneAccountHandle = null;
471 }
472 }
473
474 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700475 // No preset account, check if default exists that supports the URI scheme for the
476 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700477 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700478 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700479 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700480 if (defaultAccountHandle != null) {
481 phoneAccountHandle = defaultAccountHandle;
482 }
483 }
484
Nancy Chen1c5926f2014-09-17 14:44:14 -0700485 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700486
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700487 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
488 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700489 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700490
491 // Do not support any more live calls. Our options are to move a call to hold, disconnect
492 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700493 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700494 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800495 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800496 if (mCalls.contains(call)) {
497 // This call can already exist if it is a reused call,
498 // See {@link #getNewOutgoingCall}.
499 call.disconnect();
500 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700501 return null;
502 }
503
Nancy Chen8d92f452014-10-20 21:43:44 -0700504 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
505 !isEmergencyCall;
506
507 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700508 // This is the state where the user is expected to select an account
509 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700510 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700511 } else {
512 call.setState(CallState.CONNECTING);
513 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700514
Nancy Chen1c5926f2014-09-17 14:44:14 -0700515 call.setExtras(extras);
516
Tyler Gunn504eb832014-10-13 14:15:27 -0700517 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700518 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700519 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800520 } else if (!mCalls.contains(call)) {
521 // We check if mCalls already contains the call because we could potentially be reusing
522 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700523 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700524 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700525
526 return call;
527 }
528
Ben Gilada0d9f752014-02-26 11:49:03 -0800529 /**
Yorke Lee33501632014-03-17 19:24:12 -0700530 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800531 *
Yorke Lee33501632014-03-17 19:24:12 -0700532 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700533 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700534 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700535 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700536 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800537 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700538 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
539 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700540 if (call == null) {
541 // don't do anything if the call no longer exists
542 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700543 return;
544 }
545
Nancy Chen201b4372014-09-08 14:18:24 -0700546 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700547
548 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700549 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700550 } else {
551 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
552 Log.pii(uriHandle), Log.pii(handle));
553 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700554
Nancy Chen0d3076c2014-07-30 14:45:44 -0700555 call.setHandle(uriHandle);
556 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700557 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700558 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700559
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700560 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
561 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700562 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700563 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700564 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700565 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700566
Santos Cordonf193ba42014-09-12 06:37:39 -0700567 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700568 // If the account has been set, proceed to place the outgoing call.
569 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700570 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700571 }
Yorke Leef98fb572014-03-05 10:56:55 -0800572 }
573
574 /**
Santos Cordona1610702014-06-04 20:22:56 -0700575 * Attempts to start a conference call for the specified call.
576 *
Santos Cordon12d61822014-07-29 16:02:20 -0700577 * @param call The call to conference.
578 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700579 */
Santos Cordon12d61822014-07-29 16:02:20 -0700580 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700581 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700582 }
583
584 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700585 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
586 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800587 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700588 *
589 * @param call The call to answer.
590 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800591 */
Andrew Lee38931d02014-07-16 10:17:36 -0700592 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700593 if (!mCalls.contains(call)) {
594 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800595 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700596 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700597 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700598 if (mForegroundCall != null && mForegroundCall != call &&
599 (mForegroundCall.isActive() ||
600 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800601 if (0 == (mForegroundCall.getConnectionCapabilities()
602 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700603 // This call does not support hold. If it is from a different connection
604 // service, then disconnect it, otherwise allow the connection service to
605 // figure out the right states.
606 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
607 mForegroundCall.disconnect();
608 }
609 } else {
610 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
611 mForegroundCall, call);
612 mForegroundCall.hold();
613 }
Santos Cordondf399862014-08-06 04:39:15 -0700614 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700615 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700616 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700617 }
618
Santos Cordona56f2762014-03-24 15:55:53 -0700619 for (CallsManagerListener listener : mListeners) {
620 listener.onIncomingCallAnswered(call);
621 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800622
Santos Cordon61d0f702014-02-19 02:52:23 -0800623 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700624 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700625 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800626 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800627 }
628
629 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700630 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
631 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800632 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800633 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700634 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700635 if (!mCalls.contains(call)) {
636 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800637 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700638 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700639 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700640 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700641 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800642 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800643 }
644
645 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700646 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700647 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700648 * @param digit The DTMF digit to play.
649 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700650 void playDtmfTone(Call call, char digit) {
651 if (!mCalls.contains(call)) {
652 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700653 } else {
654 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700655 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700656 }
657 }
658
659 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700660 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700661 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700662 void stopDtmfTone(Call call) {
663 if (!mCalls.contains(call)) {
664 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700665 } else {
666 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700667 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700668 }
669 }
670
671 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700672 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700673 */
Evan Charlton352105c2014-06-03 14:10:54 -0700674 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700675 if (!mCalls.contains(call)) {
676 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700677 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700678 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700679 }
680 }
681
682 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700683 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800684 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
685 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800686 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700687 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700688 Log.v(this, "disconnectCall %s", call);
689
Sailesh Nepale59bb192014-04-01 18:33:59 -0700690 if (!mCalls.contains(call)) {
691 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800692 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700693 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800694 call.disconnect();
695 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800696 }
Santos Cordon681663d2014-01-30 04:32:15 -0800697
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700698 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700699 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700700 */
701 void disconnectAllCalls() {
702 Log.v(this, "disconnectAllCalls");
703
704 for (Call call : mCalls) {
705 disconnectCall(call);
706 }
707 }
708
Nancy Chenbc9ef172014-08-15 17:11:57 -0700709
Tyler Gunn61b92102014-08-19 07:42:20 -0700710 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700711 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700712 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
713 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700714 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700715 void holdCall(Call call) {
716 if (!mCalls.contains(call)) {
717 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700718 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700719 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700720 call.hold();
721 }
722 }
723
724 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700725 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700726 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
727 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700728 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700729 void unholdCall(Call call) {
730 if (!mCalls.contains(call)) {
731 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700732 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700733 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700734 for (Call c : mCalls) {
735 if (c != null && c.isAlive() && c != call) {
736 c.hold();
737 }
738 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700739 call.unhold();
740 }
741 }
742
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700743 /** Called by the in-call UI to change the mute state. */
744 void mute(boolean shouldMute) {
745 mCallAudioManager.mute(shouldMute);
746 }
747
748 /**
749 * Called by the in-call UI to change the audio route, for example to change from earpiece to
750 * speaker phone.
751 */
752 void setAudioRoute(int route) {
753 mCallAudioManager.setAudioRoute(route);
754 }
755
Yorke Leed1346872014-07-28 14:38:45 -0700756 /** Called by the in-call UI to turn the proximity sensor on. */
757 void turnOnProximitySensor() {
758 mProximitySensorManager.turnOn();
759 }
760
761 /**
762 * Called by the in-call UI to turn the proximity sensor off.
763 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
764 * the screen will be kept off until the proximity sensor goes negative.
765 */
766 void turnOffProximitySensor(boolean screenOnImmediately) {
767 mProximitySensorManager.turnOff(screenOnImmediately);
768 }
769
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700770 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700771 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700772 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700773 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700774 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
775 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
776 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
777 // respecting a rewritten number or a canceled number. This is unlikely since
778 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
779 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700780 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700781
782 // Note: emergency calls never go through account selection dialog so they never
783 // arrive here.
784 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700785 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700786 } else {
787 call.disconnect();
788 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700789
790 if (setDefault) {
791 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
792 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700793 }
794 }
795
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700796 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700797 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700798 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700799 for (CallsManagerListener listener : mListeners) {
800 listener.onAudioStateChanged(oldAudioState, newAudioState);
801 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700802 }
803
Sailesh Nepale59bb192014-04-01 18:33:59 -0700804 void markCallAsRinging(Call call) {
805 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800806 }
807
Sailesh Nepale59bb192014-04-01 18:33:59 -0700808 void markCallAsDialing(Call call) {
809 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800810 }
811
Sailesh Nepale59bb192014-04-01 18:33:59 -0700812 void markCallAsActive(Call call) {
813 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700814
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700815 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700816 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700817 }
Santos Cordon681663d2014-01-30 04:32:15 -0800818 }
819
Sailesh Nepale59bb192014-04-01 18:33:59 -0700820 void markCallAsOnHold(Call call) {
821 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700822 }
823
Santos Cordon049b7b62014-01-30 05:34:26 -0800824 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700825 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
826 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800827 *
Andrew Lee701dc002014-09-11 21:29:12 -0700828 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800829 */
Andrew Lee701dc002014-09-11 21:29:12 -0700830 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
831 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700832 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700833 }
834
Ben Gilada0d9f752014-02-26 11:49:03 -0800835 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700836 * Removes an existing disconnected call, and notifies the in-call app.
837 */
838 void markCallAsRemoved(Call call) {
839 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700840 if (mLocallyDisconnectingCalls.contains(call)) {
841 mLocallyDisconnectingCalls.remove(call);
842 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
843 mForegroundCall.unhold();
844 }
845 }
Ihab Awada02bef52014-08-13 18:18:42 -0700846 }
847
848 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700849 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700850 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700851 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700852 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700853 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700854 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700855 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700856 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700857 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800858 if (call.getState() != CallState.DISCONNECTED) {
859 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
860 }
861 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700862 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700863 }
864 }
865 }
866
Santos Cordondeb8c892014-05-30 01:38:03 -0700867 boolean hasAnyCalls() {
868 return !mCalls.isEmpty();
869 }
870
Santos Cordonc7e85d42014-05-22 02:51:48 -0700871 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700872 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700873 }
874
875 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700876 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700877 }
878
Santos Cordondeb8c892014-05-30 01:38:03 -0700879 boolean onMediaButton(int type) {
880 if (hasAnyCalls()) {
881 if (HeadsetMediaButton.SHORT_PRESS == type) {
882 Call ringingCall = getFirstCallWithState(CallState.RINGING);
883 if (ringingCall == null) {
884 mCallAudioManager.toggleMute();
885 return true;
886 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700887 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700888 return true;
889 }
890 } else if (HeadsetMediaButton.LONG_PRESS == type) {
891 Log.d(this, "handleHeadsetHook: longpress -> hangup");
892 Call callToHangup = getFirstCallWithState(
893 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
894 if (callToHangup != null) {
895 callToHangup.disconnect();
896 return true;
897 }
898 }
899 }
900 return false;
901 }
902
903 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800904 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700905 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800906 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -0800907 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
908 return false;
909 }
910
Santos Cordon91bd74c2014-11-07 16:10:22 -0800911 int count = 0;
912 for (Call call : mCalls) {
913 if (call.isEmergencyCall()) {
914 // We never support add call if one of the calls is an emergency call.
915 return false;
916 } else if (call.getParentCall() == null) {
917 count++;
918 }
Santos Cordon10838c22014-06-11 17:36:04 -0700919
Santos Cordon91bd74c2014-11-07 16:10:22 -0800920 // We do not check states for canAddCall. We treat disconnected calls the same
921 // and wait until they are removed instead. If we didn't count disconnected calls,
922 // we could put InCallServices into a state where they are showing two calls but
923 // also support add-call. Technically it's right, but overall looks better (UI-wise)
924 // and acts better if we wait until the call is removed.
925 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700926 return false;
927 }
928 }
929 return true;
930 }
931
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700932 Call getRingingCall() {
933 return getFirstCallWithState(CallState.RINGING);
934 }
935
936 Call getActiveCall() {
937 return getFirstCallWithState(CallState.ACTIVE);
938 }
939
Nancy Chen05a9e402014-09-26 14:14:32 -0700940 Call getDialingCall() {
941 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700942 }
943
944 Call getHeldCall() {
945 return getFirstCallWithState(CallState.ON_HOLD);
946 }
947
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700948 int getNumHeldCalls() {
949 int count = 0;
950 for (Call call : mCalls) {
951 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
952 count++;
953 }
954 }
955 return count;
956 }
957
Santos Cordonf193ba42014-09-12 06:37:39 -0700958 Call getFirstCallWithState(int... states) {
959 return getFirstCallWithState(null, states);
960 }
961
Santos Cordon10838c22014-06-11 17:36:04 -0700962 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700963 * Returns the first call that it finds with the given states. The states are treated as having
964 * priority order so that any call with the first state will be returned before any call with
965 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700966 *
967 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700968 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700969 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700970 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700971 // check the foreground first
972 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
973 return mForegroundCall;
974 }
975
Santos Cordondeb8c892014-05-30 01:38:03 -0700976 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700977 if (Objects.equals(callToSkip, call)) {
978 continue;
979 }
980
981 // Only operate on top-level calls
982 if (call.getParentCall() != null) {
983 continue;
984 }
985
Santos Cordondeb8c892014-05-30 01:38:03 -0700986 if (currentState == call.getState()) {
987 return call;
988 }
989 }
990 }
991 return null;
992 }
993
Santos Cordon0fbe6322014-08-14 04:04:25 -0700994 Call createConferenceCall(
995 PhoneAccountHandle phoneAccount,
996 ParcelableConference parcelableConference) {
997 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700998 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700999 mConnectionServiceRepository,
1000 null /* handle */,
1001 null /* gatewayInfo */,
1002 null /* connectionManagerPhoneAccount */,
1003 phoneAccount,
1004 false /* isIncoming */,
1005 true /* isConference */);
1006
1007 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001008 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001009
1010 // TODO: Move this to be a part of addCall()
1011 call.addListener(this);
1012 addCall(call);
1013 return call;
1014 }
1015
Yorke Leef86db2e2014-09-12 17:56:48 -07001016 /**
1017 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1018 */
1019 int getCallState() {
1020 return mPhoneStateBroadcaster.getCallState();
1021 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001022
Santos Cordon4b2c1192014-03-19 18:15:38 -07001023 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001024 * Retrieves the {@link PhoneAccountRegistrar}.
1025 *
1026 * @return The {@link PhoneAccountRegistrar}.
1027 */
1028 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1029 return mPhoneAccountRegistrar;
1030 }
1031
1032 /**
1033 * Retrieves the {@link MissedCallNotifier}
1034 * @return The {@link MissedCallNotifier}.
1035 */
1036 MissedCallNotifier getMissedCallNotifier() {
1037 return mMissedCallNotifier;
1038 }
1039
1040 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001041 * Adds the specified call to the main list of live calls.
1042 *
1043 * @param call The call to add.
1044 */
1045 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001046 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001047 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001048 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001049 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001050
Santos Cordondf399862014-08-06 04:39:15 -07001051 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001052 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001053 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001054 if (Log.SYSTRACE_DEBUG) {
1055 Trace.beginSection(listener.getClass().toString() + " addCall");
1056 }
Santos Cordona56f2762014-03-24 15:55:53 -07001057 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001058 if (Log.SYSTRACE_DEBUG) {
1059 Trace.endSection();
1060 }
Santos Cordona56f2762014-03-24 15:55:53 -07001061 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001062 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001063 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001064 }
1065
Sailesh Nepal810735e2014-03-18 18:15:46 -07001066 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001067 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001068 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001069
Santos Cordon672321e2014-08-21 14:38:58 -07001070 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001071 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001072 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001073
1074 boolean shouldNotify = false;
1075 if (mCalls.contains(call)) {
1076 mCalls.remove(call);
1077 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001078 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001079
Sailesh Nepale59bb192014-04-01 18:33:59 -07001080 // Only broadcast changes for calls that are being tracked.
1081 if (shouldNotify) {
1082 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001083 if (Log.SYSTRACE_DEBUG) {
1084 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1085 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001086 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001087 if (Log.SYSTRACE_DEBUG) {
1088 Trace.endSection();
1089 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001090 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001091 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001092 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001093 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001094 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001095
Sailesh Nepal810735e2014-03-18 18:15:46 -07001096 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001097 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001098 *
1099 * @param call The call.
1100 * @param newState The new state of the call.
1101 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001102 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001103 if (call == null) {
1104 return;
1105 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001106 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001107 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1108 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001109 if (newState != oldState) {
1110 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1111 // 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 -07001112 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001113 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001114 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001115 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001116 // unexpected transition occurs.
1117 call.setState(newState);
1118
Yorke Leee4a9c412014-11-14 16:59:42 -08001119 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001120 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001121 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001122 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001123 if (Log.SYSTRACE_DEBUG) {
1124 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1125 }
Santos Cordona56f2762014-03-24 15:55:53 -07001126 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001127 if (Log.SYSTRACE_DEBUG) {
1128 Trace.endSection();
1129 }
Santos Cordona56f2762014-03-24 15:55:53 -07001130 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001131 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001132 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001133 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001134 }
1135 }
1136
1137 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001138 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001139 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001140 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001141 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001142 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001143 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001144 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001145 // of its state will be foreground by default and instead the connection service should
1146 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001147 // the call should play audio and listen to microphone if it wants.
1148
Santos Cordonf193ba42014-09-12 06:37:39 -07001149 // Only top-level calls can be in foreground
1150 if (call.getParentCall() != null) {
1151 continue;
1152 }
1153
Santos Cordon40f78c22014-04-07 02:11:42 -07001154 // Active calls have priority.
1155 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001156 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001157 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001158 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001159
1160 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001161 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001162 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001163 }
1164 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001165
Sailesh Nepal810735e2014-03-18 18:15:46 -07001166 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001167 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001168 Call oldForegroundCall = mForegroundCall;
1169 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001170
Santos Cordona56f2762014-03-24 15:55:53 -07001171 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001172 if (Log.SYSTRACE_DEBUG) {
1173 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1174 }
Santos Cordona56f2762014-03-24 15:55:53 -07001175 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001176 if (Log.SYSTRACE_DEBUG) {
1177 Trace.endSection();
1178 }
Santos Cordona56f2762014-03-24 15:55:53 -07001179 }
Santos Cordon681663d2014-01-30 04:32:15 -08001180 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001181 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001182 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001183
Santos Cordon91bd74c2014-11-07 16:10:22 -08001184 private void updateCanAddCall() {
1185 boolean newCanAddCall = canAddCall();
1186 if (newCanAddCall != mCanAddCall) {
1187 mCanAddCall = newCanAddCall;
1188 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001189 if (Log.SYSTRACE_DEBUG) {
1190 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1191 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001192 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001193 if (Log.SYSTRACE_DEBUG) {
1194 Trace.endSection();
1195 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001196 }
1197 }
1198 }
1199
1200 private void updateCallsManagerState() {
1201 updateForegroundCall();
1202 updateCanAddCall();
1203 }
1204
Yorke Leeb701f6d2014-08-12 14:04:19 -07001205 private boolean isPotentialMMICode(Uri handle) {
1206 return (handle != null && handle.getSchemeSpecificPart() != null
1207 && handle.getSchemeSpecificPart().contains("#"));
1208 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001209
Tyler Gunn5b452df2014-10-01 15:02:58 -07001210 /**
1211 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1212 * MMI codes which can be dialed when one or more calls are in progress.
1213 * <P>
1214 * Checks for numbers formatted similar to the MMI codes defined in:
1215 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1216 * and
1217 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1218 *
1219 * @param handle The URI to call.
1220 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1221 */
1222 private boolean isPotentialInCallMMICode(Uri handle) {
1223 if (handle != null && handle.getSchemeSpecificPart() != null &&
1224 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1225
1226 String dialedNumber = handle.getSchemeSpecificPart();
1227 return (dialedNumber.equals("0") ||
1228 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1229 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1230 dialedNumber.equals("3") ||
1231 dialedNumber.equals("4") ||
1232 dialedNumber.equals("5"));
1233 }
1234 return false;
1235 }
1236
Santos Cordonf193ba42014-09-12 06:37:39 -07001237 private int getNumCallsWithState(int... states) {
1238 int count = 0;
1239 for (int state : states) {
1240 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001241 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001242 count++;
1243 }
1244 }
1245 }
1246 return count;
1247 }
1248
1249 private boolean hasMaximumLiveCalls() {
1250 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1251 }
1252
1253 private boolean hasMaximumHoldingCalls() {
1254 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1255 }
1256
1257 private boolean hasMaximumRingingCalls() {
1258 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1259 }
1260
1261 private boolean hasMaximumOutgoingCalls() {
1262 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1263 }
1264
1265 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1266 if (hasMaximumLiveCalls()) {
1267 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1268 // have to change.
1269 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1270
Santos Cordond1766502014-09-26 15:45:39 -07001271 if (call == liveCall) {
1272 // If the call is already the foreground call, then we are golden.
1273 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1274 // state since the call was already populated into the list.
1275 return true;
1276 }
1277
Andrew Leee6595182014-09-23 18:43:05 -07001278 if (hasMaximumOutgoingCalls()) {
1279 // Disconnect the current outgoing call if it's not an emergency call. If the user
1280 // tries to make two outgoing calls to different emergency call numbers, we will try
1281 // to connect the first outgoing call.
1282 if (isEmergency) {
1283 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1284 if (!outgoingCall.isEmergencyCall()) {
1285 outgoingCall.disconnect();
1286 return true;
1287 }
1288 }
1289 return false;
1290 }
1291
Santos Cordonf193ba42014-09-12 06:37:39 -07001292 if (hasMaximumHoldingCalls()) {
1293 // There is no more room for any more calls, unless it's an emergency.
1294 if (isEmergency) {
1295 // Kill the current active call, this is easier then trying to disconnect a
1296 // holding call and hold an active call.
1297 liveCall.disconnect();
1298 return true;
1299 }
1300 return false; // No more room!
1301 }
1302
1303 // We have room for at least one more holding call at this point.
1304
1305 // First thing, if we are trying to make a call with the same phone account as the live
1306 // call, then allow it so that the connection service can make its own decision about
1307 // how to handle the new call relative to the current one.
1308 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1309 return true;
1310 } else if (call.getTargetPhoneAccount() == null) {
1311 // Without a phone account, we can't say reliably that the call will fail.
1312 // If the user chooses the same phone account as the live call, then it's
1313 // still possible that the call can be made (like with CDMA calls not supporting
1314 // hold but they still support adding a call by going immediately into conference
1315 // mode). Return true here and we'll run this code again after user chooses an
1316 // account.
1317 return true;
1318 }
1319
1320 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001321 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001322 liveCall.hold();
1323 return true;
1324 }
1325
1326 // The live call cannot be held so we're out of luck here. There's no room.
1327 return false;
1328 }
1329 return true;
1330 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001331
1332 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001333 * Creates a new call for an existing connection.
1334 *
1335 * @param callId The id of the new call.
1336 * @param connection The connection information.
1337 * @return The new call.
1338 */
1339 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1340 Call call = new Call(
1341 mContext,
1342 mConnectionServiceRepository,
1343 connection.getHandle() /* handle */,
1344 null /* gatewayInfo */,
1345 null /* connectionManagerPhoneAccount */,
1346 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1347 false /* isIncoming */,
1348 false /* isConference */);
1349
1350 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001351 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001352 call.setCallerDisplayName(connection.getCallerDisplayName(),
1353 connection.getCallerDisplayNamePresentation());
1354
1355 call.addListener(this);
1356 addCall(call);
1357
1358 return call;
1359 }
1360
1361 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001362 * Dumps the state of the {@link CallsManager}.
1363 *
1364 * @param pw The {@code IndentingPrintWriter} to write the state to.
1365 */
1366 public void dump(IndentingPrintWriter pw) {
1367 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001368 if (mCalls != null) {
1369 pw.println("mCalls: ");
1370 pw.increaseIndent();
1371 for (Call call : mCalls) {
1372 pw.println(call);
1373 }
1374 pw.decreaseIndent();
1375 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001376 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1377
1378 if (mCallAudioManager != null) {
1379 pw.println("mCallAudioManager:");
1380 pw.increaseIndent();
1381 mCallAudioManager.dump(pw);
1382 pw.decreaseIndent();
1383 }
1384
1385 if (mTtyManager != null) {
1386 pw.println("mTtyManager:");
1387 pw.increaseIndent();
1388 mTtyManager.dump(pw);
1389 pw.decreaseIndent();
1390 }
1391
1392 if (mInCallController != null) {
1393 pw.println("mInCallController:");
1394 pw.increaseIndent();
1395 mInCallController.dump(pw);
1396 pw.decreaseIndent();
1397 }
1398
1399 if (mConnectionServiceRepository != null) {
1400 pw.println("mConnectionServiceRepository:");
1401 pw.increaseIndent();
1402 mConnectionServiceRepository.dump(pw);
1403 pw.decreaseIndent();
1404 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001405 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001406}