blob: ca4a436d482b5edff4852157ed94beb1a2145911 [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;
Ihab Awadd1d63e12015-03-26 10:33:44 -070023import android.os.Looper;
Rekha Kumar032f8e42015-04-01 21:45:57 -070024import android.os.SystemProperties;
Yorke Leee4a9c412014-11-14 16:59:42 -080025import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070026import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070027import android.telecom.AudioState;
28import android.telecom.CallState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080029import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080030import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070031import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070032import android.telecom.GatewayInfo;
33import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070034import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070035import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070036import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070037import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070038import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080039import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070040import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080041
Ihab Awada3653902015-01-23 13:44:46 -080042import com.android.internal.annotations.VisibleForTesting;
Rekha Kumar032f8e42015-04-01 21:45:57 -070043import com.android.internal.telephony.PhoneConstants;
44import com.android.internal.telephony.TelephonyProperties;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070045import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080046
Santos Cordon4bc02452014-11-19 15:51:12 -080047import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070048import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070049import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070050import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070051import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070052import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070053import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080054
Ben Giladdd8c6082013-12-30 14:44:08 -080055/**
56 * Singleton.
57 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070058 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080059 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070060 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080061 */
Ihab Awadb63b57e2015-03-22 22:12:28 -070062@VisibleForTesting
63public class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080064
Santos Cordondf399862014-08-06 04:39:15 -070065 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070066 interface CallsManagerListener {
67 void onCallAdded(Call call);
68 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070069 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070070 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070071 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070072 ConnectionServiceWrapper oldService,
73 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070074 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070075 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070076 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070077 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070078 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070079 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070080 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070081 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080082 void onCanAddCallChanged(boolean canAddCall);
Rekha Kumar032f8e42015-04-01 21:45:57 -070083 void onCallSubstateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070084 }
85
Tyler Gunn91d43cf2014-09-17 12:19:39 -070086 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080087
Santos Cordonf193ba42014-09-12 06:37:39 -070088 private static final int MAXIMUM_LIVE_CALLS = 1;
89 private static final int MAXIMUM_HOLD_CALLS = 1;
90 private static final int MAXIMUM_RINGING_CALLS = 1;
91 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080092 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070093
94 private static final int[] OUTGOING_CALL_STATES =
95 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
96
Santos Cordon91bd74c2014-11-07 16:10:22 -080097 private static final int[] LIVE_CALL_STATES =
98 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
99
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700101 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
102 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800103 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700104 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
105 * load factor before resizing, 1 means we only expect a single thread to
106 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800107 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700108 private final Set<Call> mCalls = Collections.newSetFromMap(
109 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800110
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700111 private final ConnectionServiceRepository mConnectionServiceRepository;
112 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
113 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700114 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800115 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700116 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800117 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700118 // For this set initial table size to 16 because we add 13 listeners in
119 // the CallsManager constructor.
120 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
121 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700122 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700123 private final WiredHeadsetManager mWiredHeadsetManager;
124 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700125 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700126 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700127 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700128 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700129 private final TelecomSystem.SyncRoot mLock;
130 private final ContactsAsyncHelper mContactsAsyncHelper;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700131 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
132 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700133 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800134 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
135 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awadd1d63e12015-03-26 10:33:44 -0700136 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700137
Santos Cordon91bd74c2014-11-07 16:10:22 -0800138 private boolean mCanAddCall = true;
139
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700140 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700141 * The call the user is currently interacting with. This is the call that should have audio
142 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800143 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700144 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800145
Nancy Chena469f762014-12-09 18:29:20 -0800146 private Runnable mStopTone;
147
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700148 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700149 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800150 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700151 CallsManager(
152 Context context,
153 TelecomSystem.SyncRoot lock,
154 ContactsAsyncHelper contactsAsyncHelper,
155 MissedCallNotifier missedCallNotifier,
156 PhoneAccountRegistrar phoneAccountRegistrar,
157 HeadsetMediaButtonFactory headsetMediaButtonFactory,
158 ProximitySensorManagerFactory proximitySensorManagerFactory,
159 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700160 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700161 mLock = lock;
162 mContactsAsyncHelper = contactsAsyncHelper;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700163 mPhoneAccountRegistrar = phoneAccountRegistrar;
164 mMissedCallNotifier = missedCallNotifier;
165 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
166 mWiredHeadsetManager = new WiredHeadsetManager(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800167 mCallAudioManager = new CallAudioManager(
168 context, statusBarNotifier, mWiredHeadsetManager, this);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700169 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700170 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad8de76912015-02-17 12:25:52 -0800171 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700172 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800173 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
174 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700175 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700176 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700177 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800178 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700179 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800180 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700181
Santos Cordondeb8c892014-05-30 01:38:03 -0700182 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700183 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700184 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700185 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700186 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700187 mListeners.add(new RingbackPlayer(this, playerFactory));
188 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700189 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700190 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700191 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700192 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700193 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700194
195 mMissedCallNotifier.updateOnStartup(mLock, this, mContactsAsyncHelper);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800196 }
197
Ihab Awad8de76912015-02-17 12:25:52 -0800198 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
199 if (mRespondViaSmsManager != null) {
200 mListeners.remove(mRespondViaSmsManager);
201 }
202 mRespondViaSmsManager = respondViaSmsManager;
203 mListeners.add(respondViaSmsManager);
204 }
205
206 public RespondViaSmsManager getRespondViaSmsManager() {
207 return mRespondViaSmsManager;
208 }
209
Santos Cordon766d04f2014-05-06 10:28:25 -0700210 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700211 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700212 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700213
Tyler Gunncde2e502014-08-12 14:35:58 -0700214 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700215 if (!mCalls.contains(call)) {
216 // Call was not added previously in startOutgoingCall due to it being a potential MMI
217 // code, so add it now.
218 addCall(call);
219 }
220
221 // The call's ConnectionService has been updated.
222 for (CallsManagerListener listener : mListeners) {
223 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700224 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700225
226 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700227 }
228
229 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700230 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700231 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700232
Andrew Leee6288a72014-10-01 13:50:02 -0700233 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700234 }
235
236 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700237 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700238 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700239 setCallState(incomingCall, CallState.RINGING);
240
241 if (hasMaximumRingingCalls()) {
242 incomingCall.reject(false, null);
243 // since the call was not added to the list of calls, we have to call the missed
244 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700245 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700246 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
247 } else {
248 addCall(incomingCall);
249 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700250 }
251
252 @Override
253 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700254 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700255 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800256 }
257
Ihab Awadcb387ac2014-05-28 16:49:38 -0700258 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700259 public void onSuccessfulUnknownCall(Call call, int callState) {
260 setCallState(call, callState);
261 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
262 addCall(call);
263 }
264
265 @Override
266 public void onFailedUnknownCall(Call call) {
267 Log.i(this, "onFailedUnknownCall for call %s", call);
268 setCallState(call, CallState.DISCONNECTED);
269 call.removeListener(this);
270 }
271
272 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700273 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700274 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700275 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700276 }
277 }
278
Evan Charlton352105c2014-06-03 14:10:54 -0700279 @Override
280 public void onPostDialWait(Call call, String remaining) {
281 mInCallController.onPostDialWait(call, remaining);
282 }
283
Santos Cordona1610702014-06-04 20:22:56 -0700284 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800285 public void onPostDialChar(final Call call, char nextChar) {
286 if (PhoneNumberUtils.is12Key(nextChar)) {
287 // Play tone if it is one of the dialpad digits, canceling out the previously queued
288 // up stopTone runnable since playing a new tone automatically stops the previous tone.
289 if (mStopTone != null) {
290 mHandler.removeCallbacks(mStopTone);
291 }
292
293 mDtmfLocalTonePlayer.playTone(call, nextChar);
294
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700295 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800296 mStopTone = new Runnable() {
297 @Override
298 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700299 synchronized (mLock) {
300 // Set a timeout to stop the tone in case there isn't another tone to follow.
301 mDtmfLocalTonePlayer.stopTone(call);
302 }
Nancy Chena469f762014-12-09 18:29:20 -0800303 }
304 };
305 mHandler.postDelayed(
306 mStopTone,
307 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
308 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
309 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
310 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
311 // the previous tone is being stopped anyway.
312 if (mStopTone != null) {
313 mHandler.removeCallbacks(mStopTone);
314 }
315 mDtmfLocalTonePlayer.stopTone(call);
316 } else {
317 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
318 }
319 }
320
321 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700322 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700323 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800324 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700325 for (CallsManagerListener listener : mListeners) {
326 listener.onIsConferencedChanged(call);
327 }
328 }
329
330 @Override
331 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700332 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800333 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700334 for (CallsManagerListener listener : mListeners) {
335 listener.onIsConferencedChanged(call);
336 }
337 }
338
Ihab Awadff7493a2014-06-10 13:47:44 -0700339 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700340 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700341 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700342 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700343 }
344 }
345
Andrew Lee4a796602014-07-11 17:23:03 -0700346 @Override
347 public void onVideoStateChanged(Call call) {
348 for (CallsManagerListener listener : mListeners) {
349 listener.onVideoStateChanged(call);
350 }
351 }
352
Santos Cordoncf5b2912014-11-05 21:57:54 -0800353 @Override
354 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
355 mPendingCallsToDisconnect.add(call);
356 mHandler.postDelayed(new Runnable() {
357 @Override
358 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700359 synchronized (mLock) {
360 if (mPendingCallsToDisconnect.remove(call)) {
361 Log.i(this, "Delayed disconnection of call: %s", call);
362 call.disconnect();
363 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800364 }
365 }
366 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
367
368 return true;
369 }
370
Santos Cordon4bc02452014-11-19 15:51:12 -0800371 Collection<Call> getCalls() {
372 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700373 }
374
Rekha Kumar032f8e42015-04-01 21:45:57 -0700375 public void onCallSubstateChanged(Call call) {
376 for (CallsManagerListener listener : mListeners) {
377 listener.onCallSubstateChanged(call);
378 }
379 }
380
Sailesh Nepal810735e2014-03-18 18:15:46 -0700381 Call getForegroundCall() {
382 return mForegroundCall;
383 }
384
Santos Cordonae193062014-05-21 21:21:49 -0700385 Ringer getRinger() {
386 return mRinger;
387 }
388
Santos Cordonf3671a62014-05-29 21:51:53 -0700389 InCallController getInCallController() {
390 return mInCallController;
391 }
392
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700393 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700394 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700395 if (call.isEmergencyCall()) {
396 return true;
397 }
398 }
399 return false;
400 }
401
Andrew Lee45506232014-10-22 17:54:33 -0700402 boolean hasVideoCall() {
403 for (Call call : mCalls) {
404 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
405 return true;
406 }
407 }
408 return false;
409 }
410
Ihab Awad6fb37c82014-08-07 19:48:57 -0700411 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700412 return mCallAudioManager.getAudioState();
413 }
414
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700415 boolean isTtySupported() {
416 return mTtyManager.isTtySupported();
417 }
418
419 int getCurrentTtyMode() {
420 return mTtyManager.getCurrentTtyMode();
421 }
422
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700423 void addListener(CallsManagerListener listener) {
424 mListeners.add(listener);
425 }
426
427 void removeListener(CallsManagerListener listener) {
428 mListeners.remove(listener);
429 }
430
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800431 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700432 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800433 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700434 * @param phoneAccountHandle The phone account which contains the component name of the
435 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800436 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800437 */
Evan Charlton89176372014-07-19 18:23:09 -0700438 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800439 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700440 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700441 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700442 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800443 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -0700444 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700445 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700446 mContactsAsyncHelper,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700447 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700448 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700449 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700450 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700451 true /* isIncoming */,
452 false /* isConference */);
453
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700454 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700455 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700456 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700457 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800458 }
459
Yorke Lee9250e5f2014-10-01 13:39:09 -0700460 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
461 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
462 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
463 Call call = new Call(
464 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800465 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -0700466 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700467 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700468 mContactsAsyncHelper,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700469 handle,
470 null /* gatewayInfo */,
471 null /* connectionManagerPhoneAccount */,
472 phoneAccountHandle,
473 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
474 // to the existing connection instead of trying to create a new one.
475 true /* isIncoming */,
476 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700477 call.setIsUnknown(true);
478 call.setExtras(extras);
479 call.addListener(this);
480 call.startCreateConnection(mPhoneAccountRegistrar);
481 }
482
Santos Cordoncf5b2912014-11-05 21:57:54 -0800483 private Call getNewOutgoingCall(Uri handle) {
484 // First check to see if we can reuse any of the calls that are waiting to disconnect.
485 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800486 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800487 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800488 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800489 mPendingCallsToDisconnect.remove(pendingCall);
490 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800491 reusedCall = pendingCall;
492 } else {
493 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800494 }
495 }
Yorke Lee59979512014-12-02 01:03:40 -0800496 if (reusedCall != null) {
497 return reusedCall;
498 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800499
500 // Create a call with original handle. The handle may be changed when the call is attached
501 // to a connection service, but in most cases will remain the same.
502 return new Call(
503 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800504 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -0700505 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800506 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700507 mContactsAsyncHelper,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800508 handle,
509 null /* gatewayInfo */,
510 null /* connectionManagerPhoneAccount */,
511 null /* phoneAccountHandle */,
512 false /* isIncoming */,
513 false /* isConference */);
514 }
515
Nancy Chen0d3076c2014-07-30 14:45:44 -0700516 /**
517 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
518 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700519 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700520 * @param phoneAccountHandle The phone account which contains the component name of the
521 * connection service to use for this call.
522 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700523 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700524 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800525 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700526
527 List<PhoneAccountHandle> accounts =
528 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
529
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800530 Log.v(this, "startOutgoingCall found accounts = " + accounts);
531
Nancy Chenbc95db92014-11-24 13:19:18 -0800532 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
533 // If there is an ongoing call, use the same phone account to place this new call.
534 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
535 }
536
Nancy Chenbc9ef172014-08-15 17:11:57 -0700537 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
538 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700539 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700540 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700541 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700542 phoneAccountHandle = null;
543 }
544 }
545
546 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700547 // No preset account, check if default exists that supports the URI scheme for the
548 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700549 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700550 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700551 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700552 if (defaultAccountHandle != null) {
553 phoneAccountHandle = defaultAccountHandle;
554 }
555 }
556
Nancy Chen1c5926f2014-09-17 14:44:14 -0700557 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700558
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700559 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
560 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700561 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700562
563 // Do not support any more live calls. Our options are to move a call to hold, disconnect
564 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700565 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700566 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800567 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800568 if (mCalls.contains(call)) {
569 // This call can already exist if it is a reused call,
570 // See {@link #getNewOutgoingCall}.
571 call.disconnect();
572 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700573 return null;
574 }
575
Nancy Chen8d92f452014-10-20 21:43:44 -0700576 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
577 !isEmergencyCall;
578
579 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700580 // This is the state where the user is expected to select an account
581 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700582 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700583 } else {
584 call.setState(CallState.CONNECTING);
585 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700586
Nancy Chen1c5926f2014-09-17 14:44:14 -0700587 call.setExtras(extras);
588
Tyler Gunn504eb832014-10-13 14:15:27 -0700589 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700590 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700591 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800592 } else if (!mCalls.contains(call)) {
593 // We check if mCalls already contains the call because we could potentially be reusing
594 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700595 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700596 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700597
598 return call;
599 }
600
Ben Gilada0d9f752014-02-26 11:49:03 -0800601 /**
Yorke Lee33501632014-03-17 19:24:12 -0700602 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800603 *
Yorke Lee33501632014-03-17 19:24:12 -0700604 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700605 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700606 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700607 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700608 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800609 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700610 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
611 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700612 if (call == null) {
613 // don't do anything if the call no longer exists
614 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700615 return;
616 }
617
Nancy Chen201b4372014-09-08 14:18:24 -0700618 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700619
620 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700621 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700622 } else {
623 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
624 Log.pii(uriHandle), Log.pii(handle));
625 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700626
Nancy Chen0d3076c2014-07-30 14:45:44 -0700627 call.setHandle(uriHandle);
628 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700629 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700630 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700631
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700632 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
633 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700634 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700635 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700636 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700637 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700638
Santos Cordonf193ba42014-09-12 06:37:39 -0700639 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700640 // If the account has been set, proceed to place the outgoing call.
641 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700642 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700643 }
Yorke Leef98fb572014-03-05 10:56:55 -0800644 }
645
646 /**
Santos Cordona1610702014-06-04 20:22:56 -0700647 * Attempts to start a conference call for the specified call.
648 *
Santos Cordon12d61822014-07-29 16:02:20 -0700649 * @param call The call to conference.
650 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700651 */
Santos Cordon12d61822014-07-29 16:02:20 -0700652 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700653 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700654 }
655
656 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700657 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
658 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800659 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700660 *
661 * @param call The call to answer.
662 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800663 */
Andrew Lee38931d02014-07-16 10:17:36 -0700664 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700665 if (!mCalls.contains(call)) {
666 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800667 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700668 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700669 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700670 if (mForegroundCall != null && mForegroundCall != call &&
671 (mForegroundCall.isActive() ||
672 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800673 if (0 == (mForegroundCall.getConnectionCapabilities()
674 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700675 // This call does not support hold. If it is from a different connection
676 // service, then disconnect it, otherwise allow the connection service to
677 // figure out the right states.
678 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
679 mForegroundCall.disconnect();
680 }
681 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800682 Call heldCall = getHeldCall();
683 if (heldCall != null) {
684 Log.v(this, "Disconnecting held call %s before holding active call.",
685 heldCall);
686 heldCall.disconnect();
687 }
688
Santos Cordonf193ba42014-09-12 06:37:39 -0700689 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
690 mForegroundCall, call);
691 mForegroundCall.hold();
692 }
Santos Cordondf399862014-08-06 04:39:15 -0700693 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700694 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700695 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700696 }
697
Santos Cordona56f2762014-03-24 15:55:53 -0700698 for (CallsManagerListener listener : mListeners) {
699 listener.onIncomingCallAnswered(call);
700 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800701
Santos Cordon61d0f702014-02-19 02:52:23 -0800702 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700703 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700704 call.answer(videoState);
Rekha Kumar032f8e42015-04-01 21:45:57 -0700705 if (VideoProfile.VideoState.isVideo(videoState) &&
706 !mWiredHeadsetManager.isPluggedIn() &&
707 !mCallAudioManager.isBluetoothDeviceAvailable() &&
708 isSpeakerEnabledForVideoCalls()) {
709 call.setStartWithSpeakerphoneOn(true);
710 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800711 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800712 }
713
Rekha Kumar032f8e42015-04-01 21:45:57 -0700714 private static boolean isSpeakerEnabledForVideoCalls() {
715 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
716 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
717 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
718 }
719
Santos Cordone3d76ab2014-01-28 17:25:20 -0800720 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700721 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
722 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800723 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800724 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700725 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700726 if (!mCalls.contains(call)) {
727 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800728 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700729 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700730 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700731 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700732 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800733 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800734 }
735
736 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700737 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700738 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700739 * @param digit The DTMF digit to play.
740 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700741 void playDtmfTone(Call call, char digit) {
742 if (!mCalls.contains(call)) {
743 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700744 } else {
745 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700746 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700747 }
748 }
749
750 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700751 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700752 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700753 void stopDtmfTone(Call call) {
754 if (!mCalls.contains(call)) {
755 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700756 } else {
757 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700758 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700759 }
760 }
761
762 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700763 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700764 */
Evan Charlton352105c2014-06-03 14:10:54 -0700765 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700766 if (!mCalls.contains(call)) {
767 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700768 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700769 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700770 }
771 }
772
773 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700774 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800775 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
776 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800777 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700778 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700779 Log.v(this, "disconnectCall %s", call);
780
Sailesh Nepale59bb192014-04-01 18:33:59 -0700781 if (!mCalls.contains(call)) {
782 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800783 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700784 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800785 call.disconnect();
786 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800787 }
Santos Cordon681663d2014-01-30 04:32:15 -0800788
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700789 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700790 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700791 */
792 void disconnectAllCalls() {
793 Log.v(this, "disconnectAllCalls");
794
795 for (Call call : mCalls) {
796 disconnectCall(call);
797 }
798 }
799
Nancy Chenbc9ef172014-08-15 17:11:57 -0700800
Tyler Gunn61b92102014-08-19 07:42:20 -0700801 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700802 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700803 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
804 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700805 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700806 void holdCall(Call call) {
807 if (!mCalls.contains(call)) {
808 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700809 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700810 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700811 call.hold();
812 }
813 }
814
815 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700816 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700817 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
818 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700819 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700820 void unholdCall(Call call) {
821 if (!mCalls.contains(call)) {
822 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700823 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700824 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700825 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800826 // Only attempt to hold parent calls and not the individual children.
827 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700828 c.hold();
829 }
830 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700831 call.unhold();
832 }
833 }
834
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700835 /** Called by the in-call UI to change the mute state. */
836 void mute(boolean shouldMute) {
837 mCallAudioManager.mute(shouldMute);
838 }
839
840 /**
841 * Called by the in-call UI to change the audio route, for example to change from earpiece to
842 * speaker phone.
843 */
844 void setAudioRoute(int route) {
845 mCallAudioManager.setAudioRoute(route);
846 }
847
Yorke Leed1346872014-07-28 14:38:45 -0700848 /** Called by the in-call UI to turn the proximity sensor on. */
849 void turnOnProximitySensor() {
850 mProximitySensorManager.turnOn();
851 }
852
853 /**
854 * Called by the in-call UI to turn the proximity sensor off.
855 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
856 * the screen will be kept off until the proximity sensor goes negative.
857 */
858 void turnOffProximitySensor(boolean screenOnImmediately) {
859 mProximitySensorManager.turnOff(screenOnImmediately);
860 }
861
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700862 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700863 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700864 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700865 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700866 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
867 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
868 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
869 // respecting a rewritten number or a canceled number. This is unlikely since
870 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
871 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700872 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700873
874 // Note: emergency calls never go through account selection dialog so they never
875 // arrive here.
876 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700877 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700878 } else {
879 call.disconnect();
880 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700881
882 if (setDefault) {
883 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
884 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700885 }
886 }
887
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700888 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700889 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700890 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700891 for (CallsManagerListener listener : mListeners) {
892 listener.onAudioStateChanged(oldAudioState, newAudioState);
893 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700894 }
895
Sailesh Nepale59bb192014-04-01 18:33:59 -0700896 void markCallAsRinging(Call call) {
897 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800898 }
899
Sailesh Nepale59bb192014-04-01 18:33:59 -0700900 void markCallAsDialing(Call call) {
901 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800902 }
903
Sailesh Nepale59bb192014-04-01 18:33:59 -0700904 void markCallAsActive(Call call) {
905 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700906
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700907 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700908 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700909 }
Santos Cordon681663d2014-01-30 04:32:15 -0800910 }
911
Sailesh Nepale59bb192014-04-01 18:33:59 -0700912 void markCallAsOnHold(Call call) {
913 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700914 }
915
Santos Cordon049b7b62014-01-30 05:34:26 -0800916 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700917 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
918 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800919 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800920 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800921 */
Andrew Lee701dc002014-09-11 21:29:12 -0700922 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
923 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700924 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700925 }
926
Ben Gilada0d9f752014-02-26 11:49:03 -0800927 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700928 * Removes an existing disconnected call, and notifies the in-call app.
929 */
930 void markCallAsRemoved(Call call) {
931 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700932 if (mLocallyDisconnectingCalls.contains(call)) {
933 mLocallyDisconnectingCalls.remove(call);
934 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
935 mForegroundCall.unhold();
936 }
937 }
Ihab Awada02bef52014-08-13 18:18:42 -0700938 }
939
940 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700941 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700942 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700943 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700944 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700945 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700946 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700947 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700948 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700949 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800950 if (call.getState() != CallState.DISCONNECTED) {
951 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
952 }
953 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700954 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700955 }
956 }
957 }
958
Santos Cordondeb8c892014-05-30 01:38:03 -0700959 boolean hasAnyCalls() {
960 return !mCalls.isEmpty();
961 }
962
Santos Cordonc7e85d42014-05-22 02:51:48 -0700963 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700964 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700965 }
966
967 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700968 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700969 }
970
Santos Cordondeb8c892014-05-30 01:38:03 -0700971 boolean onMediaButton(int type) {
972 if (hasAnyCalls()) {
973 if (HeadsetMediaButton.SHORT_PRESS == type) {
974 Call ringingCall = getFirstCallWithState(CallState.RINGING);
975 if (ringingCall == null) {
976 mCallAudioManager.toggleMute();
977 return true;
978 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700979 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700980 return true;
981 }
982 } else if (HeadsetMediaButton.LONG_PRESS == type) {
983 Log.d(this, "handleHeadsetHook: longpress -> hangup");
984 Call callToHangup = getFirstCallWithState(
985 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
986 if (callToHangup != null) {
987 callToHangup.disconnect();
988 return true;
989 }
990 }
991 }
992 return false;
993 }
994
995 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800996 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700997 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800998 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -0800999 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1000 return false;
1001 }
1002
Santos Cordon91bd74c2014-11-07 16:10:22 -08001003 int count = 0;
1004 for (Call call : mCalls) {
1005 if (call.isEmergencyCall()) {
1006 // We never support add call if one of the calls is an emergency call.
1007 return false;
1008 } else if (call.getParentCall() == null) {
1009 count++;
1010 }
Santos Cordon10838c22014-06-11 17:36:04 -07001011
Santos Cordon91bd74c2014-11-07 16:10:22 -08001012 // We do not check states for canAddCall. We treat disconnected calls the same
1013 // and wait until they are removed instead. If we didn't count disconnected calls,
1014 // we could put InCallServices into a state where they are showing two calls but
1015 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1016 // and acts better if we wait until the call is removed.
1017 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001018 return false;
1019 }
1020 }
1021 return true;
1022 }
1023
Ihab Awada3653902015-01-23 13:44:46 -08001024 @VisibleForTesting
1025 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001026 return getFirstCallWithState(CallState.RINGING);
1027 }
1028
1029 Call getActiveCall() {
1030 return getFirstCallWithState(CallState.ACTIVE);
1031 }
1032
Nancy Chen05a9e402014-09-26 14:14:32 -07001033 Call getDialingCall() {
1034 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001035 }
1036
1037 Call getHeldCall() {
1038 return getFirstCallWithState(CallState.ON_HOLD);
1039 }
1040
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001041 int getNumHeldCalls() {
1042 int count = 0;
1043 for (Call call : mCalls) {
1044 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1045 count++;
1046 }
1047 }
1048 return count;
1049 }
1050
Santos Cordonf193ba42014-09-12 06:37:39 -07001051 Call getFirstCallWithState(int... states) {
1052 return getFirstCallWithState(null, states);
1053 }
1054
Santos Cordon10838c22014-06-11 17:36:04 -07001055 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001056 * Returns the first call that it finds with the given states. The states are treated as having
1057 * priority order so that any call with the first state will be returned before any call with
1058 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001059 *
1060 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001061 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001062 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001063 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001064 // check the foreground first
1065 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1066 return mForegroundCall;
1067 }
1068
Santos Cordondeb8c892014-05-30 01:38:03 -07001069 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001070 if (Objects.equals(callToSkip, call)) {
1071 continue;
1072 }
1073
1074 // Only operate on top-level calls
1075 if (call.getParentCall() != null) {
1076 continue;
1077 }
1078
Santos Cordondeb8c892014-05-30 01:38:03 -07001079 if (currentState == call.getState()) {
1080 return call;
1081 }
1082 }
1083 }
1084 return null;
1085 }
1086
Santos Cordon0fbe6322014-08-14 04:04:25 -07001087 Call createConferenceCall(
1088 PhoneAccountHandle phoneAccount,
1089 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001090
1091 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1092 // which is the default value for new Calls.
1093 long connectTime =
1094 parcelableConference.getConnectTimeMillis() ==
1095 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1096 parcelableConference.getConnectTimeMillis();
1097
Santos Cordon0fbe6322014-08-14 04:04:25 -07001098 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001099 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001100 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -07001101 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001102 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001103 mContactsAsyncHelper,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001104 null /* handle */,
1105 null /* gatewayInfo */,
1106 null /* connectionManagerPhoneAccount */,
1107 phoneAccount,
1108 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001109 true /* isConference */,
1110 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001111
1112 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001113 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumar032f8e42015-04-01 21:45:57 -07001114 call.setVideoState(parcelableConference.getVideoState());
1115 call.setVideoProvider(parcelableConference.getVideoProvider());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001116
1117 // TODO: Move this to be a part of addCall()
1118 call.addListener(this);
1119 addCall(call);
1120 return call;
1121 }
1122
Yorke Leef86db2e2014-09-12 17:56:48 -07001123 /**
1124 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1125 */
1126 int getCallState() {
1127 return mPhoneStateBroadcaster.getCallState();
1128 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001129
Santos Cordon4b2c1192014-03-19 18:15:38 -07001130 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001131 * Retrieves the {@link PhoneAccountRegistrar}.
1132 *
1133 * @return The {@link PhoneAccountRegistrar}.
1134 */
1135 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1136 return mPhoneAccountRegistrar;
1137 }
1138
1139 /**
1140 * Retrieves the {@link MissedCallNotifier}
1141 * @return The {@link MissedCallNotifier}.
1142 */
1143 MissedCallNotifier getMissedCallNotifier() {
1144 return mMissedCallNotifier;
1145 }
1146
1147 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001148 * Adds the specified call to the main list of live calls.
1149 *
1150 * @param call The call to add.
1151 */
1152 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001153 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001154 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001155 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001156 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001157
Santos Cordondf399862014-08-06 04:39:15 -07001158 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001159 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001160 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001161 if (Log.SYSTRACE_DEBUG) {
1162 Trace.beginSection(listener.getClass().toString() + " addCall");
1163 }
Santos Cordona56f2762014-03-24 15:55:53 -07001164 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001165 if (Log.SYSTRACE_DEBUG) {
1166 Trace.endSection();
1167 }
Santos Cordona56f2762014-03-24 15:55:53 -07001168 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001169 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001170 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001171 }
1172
Sailesh Nepal810735e2014-03-18 18:15:46 -07001173 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001174 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001175 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001176
Santos Cordon672321e2014-08-21 14:38:58 -07001177 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001178 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001179 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001180
1181 boolean shouldNotify = false;
1182 if (mCalls.contains(call)) {
1183 mCalls.remove(call);
1184 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001185 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001186
Sailesh Nepale59bb192014-04-01 18:33:59 -07001187 // Only broadcast changes for calls that are being tracked.
1188 if (shouldNotify) {
1189 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001190 if (Log.SYSTRACE_DEBUG) {
1191 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1192 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001193 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001194 if (Log.SYSTRACE_DEBUG) {
1195 Trace.endSection();
1196 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001197 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001198 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001199 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001200 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001201 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001202
Sailesh Nepal810735e2014-03-18 18:15:46 -07001203 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001204 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001205 *
1206 * @param call The call.
1207 * @param newState The new state of the call.
1208 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001209 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001210 if (call == null) {
1211 return;
1212 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001213 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001214 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1215 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001216 if (newState != oldState) {
1217 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1218 // 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 -07001219 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001220 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001221 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001222 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001223 // unexpected transition occurs.
1224 call.setState(newState);
1225
Yorke Leee4a9c412014-11-14 16:59:42 -08001226 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001227 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001228 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001229 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001230 if (Log.SYSTRACE_DEBUG) {
1231 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1232 }
Santos Cordona56f2762014-03-24 15:55:53 -07001233 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001234 if (Log.SYSTRACE_DEBUG) {
1235 Trace.endSection();
1236 }
Santos Cordona56f2762014-03-24 15:55:53 -07001237 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001238 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001239 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001240 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001241 }
1242 }
1243
1244 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001245 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001246 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001247 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001248 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001249 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001250 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001251 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001252 // of its state will be foreground by default and instead the connection service should
1253 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001254 // the call should play audio and listen to microphone if it wants.
1255
Santos Cordonf193ba42014-09-12 06:37:39 -07001256 // Only top-level calls can be in foreground
1257 if (call.getParentCall() != null) {
1258 continue;
1259 }
1260
Santos Cordon40f78c22014-04-07 02:11:42 -07001261 // Active calls have priority.
1262 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001263 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001264 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001265 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001266
1267 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001268 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001269 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001270 }
1271 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001272
Sailesh Nepal810735e2014-03-18 18:15:46 -07001273 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001274 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001275 Call oldForegroundCall = mForegroundCall;
1276 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001277
Santos Cordona56f2762014-03-24 15:55:53 -07001278 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001279 if (Log.SYSTRACE_DEBUG) {
1280 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1281 }
Santos Cordona56f2762014-03-24 15:55:53 -07001282 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001283 if (Log.SYSTRACE_DEBUG) {
1284 Trace.endSection();
1285 }
Santos Cordona56f2762014-03-24 15:55:53 -07001286 }
Santos Cordon681663d2014-01-30 04:32:15 -08001287 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001288 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001289 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001290
Santos Cordon91bd74c2014-11-07 16:10:22 -08001291 private void updateCanAddCall() {
1292 boolean newCanAddCall = canAddCall();
1293 if (newCanAddCall != mCanAddCall) {
1294 mCanAddCall = newCanAddCall;
1295 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001296 if (Log.SYSTRACE_DEBUG) {
1297 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1298 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001299 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001300 if (Log.SYSTRACE_DEBUG) {
1301 Trace.endSection();
1302 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001303 }
1304 }
1305 }
1306
1307 private void updateCallsManagerState() {
1308 updateForegroundCall();
1309 updateCanAddCall();
1310 }
1311
Yorke Leeb701f6d2014-08-12 14:04:19 -07001312 private boolean isPotentialMMICode(Uri handle) {
1313 return (handle != null && handle.getSchemeSpecificPart() != null
1314 && handle.getSchemeSpecificPart().contains("#"));
1315 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001316
Tyler Gunn5b452df2014-10-01 15:02:58 -07001317 /**
1318 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1319 * MMI codes which can be dialed when one or more calls are in progress.
1320 * <P>
1321 * Checks for numbers formatted similar to the MMI codes defined in:
1322 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1323 * and
1324 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1325 *
1326 * @param handle The URI to call.
1327 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1328 */
1329 private boolean isPotentialInCallMMICode(Uri handle) {
1330 if (handle != null && handle.getSchemeSpecificPart() != null &&
1331 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1332
1333 String dialedNumber = handle.getSchemeSpecificPart();
1334 return (dialedNumber.equals("0") ||
1335 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1336 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1337 dialedNumber.equals("3") ||
1338 dialedNumber.equals("4") ||
1339 dialedNumber.equals("5"));
1340 }
1341 return false;
1342 }
1343
Santos Cordonf193ba42014-09-12 06:37:39 -07001344 private int getNumCallsWithState(int... states) {
1345 int count = 0;
1346 for (int state : states) {
1347 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001348 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001349 count++;
1350 }
1351 }
1352 }
1353 return count;
1354 }
1355
1356 private boolean hasMaximumLiveCalls() {
1357 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1358 }
1359
1360 private boolean hasMaximumHoldingCalls() {
1361 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1362 }
1363
1364 private boolean hasMaximumRingingCalls() {
1365 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1366 }
1367
1368 private boolean hasMaximumOutgoingCalls() {
1369 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1370 }
1371
1372 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1373 if (hasMaximumLiveCalls()) {
1374 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1375 // have to change.
1376 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301377 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1378 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001379
Santos Cordond1766502014-09-26 15:45:39 -07001380 if (call == liveCall) {
1381 // If the call is already the foreground call, then we are golden.
1382 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1383 // state since the call was already populated into the list.
1384 return true;
1385 }
1386
Andrew Leee6595182014-09-23 18:43:05 -07001387 if (hasMaximumOutgoingCalls()) {
1388 // Disconnect the current outgoing call if it's not an emergency call. If the user
1389 // tries to make two outgoing calls to different emergency call numbers, we will try
1390 // to connect the first outgoing call.
1391 if (isEmergency) {
1392 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1393 if (!outgoingCall.isEmergencyCall()) {
1394 outgoingCall.disconnect();
1395 return true;
1396 }
1397 }
1398 return false;
1399 }
1400
Santos Cordonf193ba42014-09-12 06:37:39 -07001401 if (hasMaximumHoldingCalls()) {
1402 // There is no more room for any more calls, unless it's an emergency.
1403 if (isEmergency) {
1404 // Kill the current active call, this is easier then trying to disconnect a
1405 // holding call and hold an active call.
1406 liveCall.disconnect();
1407 return true;
1408 }
1409 return false; // No more room!
1410 }
1411
1412 // We have room for at least one more holding call at this point.
1413
1414 // First thing, if we are trying to make a call with the same phone account as the live
1415 // call, then allow it so that the connection service can make its own decision about
1416 // how to handle the new call relative to the current one.
1417 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1418 return true;
1419 } else if (call.getTargetPhoneAccount() == null) {
1420 // Without a phone account, we can't say reliably that the call will fail.
1421 // If the user chooses the same phone account as the live call, then it's
1422 // still possible that the call can be made (like with CDMA calls not supporting
1423 // hold but they still support adding a call by going immediately into conference
1424 // mode). Return true here and we'll run this code again after user chooses an
1425 // account.
1426 return true;
1427 }
1428
1429 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001430 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001431 liveCall.hold();
1432 return true;
1433 }
1434
1435 // The live call cannot be held so we're out of luck here. There's no room.
1436 return false;
1437 }
1438 return true;
1439 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001440
1441 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001442 * Creates a new call for an existing connection.
1443 *
1444 * @param callId The id of the new call.
1445 * @param connection The connection information.
1446 * @return The new call.
1447 */
1448 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1449 Call call = new Call(
1450 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001451 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -07001452 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001453 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001454 mContactsAsyncHelper,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001455 connection.getHandle() /* handle */,
1456 null /* gatewayInfo */,
1457 null /* connectionManagerPhoneAccount */,
1458 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1459 false /* isIncoming */,
1460 false /* isConference */);
1461
1462 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001463 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001464 call.setCallerDisplayName(connection.getCallerDisplayName(),
1465 connection.getCallerDisplayNamePresentation());
1466
1467 call.addListener(this);
1468 addCall(call);
1469
1470 return call;
1471 }
1472
1473 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001474 * Dumps the state of the {@link CallsManager}.
1475 *
1476 * @param pw The {@code IndentingPrintWriter} to write the state to.
1477 */
1478 public void dump(IndentingPrintWriter pw) {
1479 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001480 if (mCalls != null) {
1481 pw.println("mCalls: ");
1482 pw.increaseIndent();
1483 for (Call call : mCalls) {
1484 pw.println(call);
1485 }
1486 pw.decreaseIndent();
1487 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001488 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1489
1490 if (mCallAudioManager != null) {
1491 pw.println("mCallAudioManager:");
1492 pw.increaseIndent();
1493 mCallAudioManager.dump(pw);
1494 pw.decreaseIndent();
1495 }
1496
1497 if (mTtyManager != null) {
1498 pw.println("mTtyManager:");
1499 pw.increaseIndent();
1500 mTtyManager.dump(pw);
1501 pw.decreaseIndent();
1502 }
1503
1504 if (mInCallController != null) {
1505 pw.println("mInCallController:");
1506 pw.increaseIndent();
1507 mInCallController.dump(pw);
1508 pw.decreaseIndent();
1509 }
1510
1511 if (mConnectionServiceRepository != null) {
1512 pw.println("mConnectionServiceRepository:");
1513 pw.increaseIndent();
1514 mConnectionServiceRepository.dump(pw);
1515 pw.decreaseIndent();
1516 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001517 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001518}