blob: 85946b2f6c22fd8a4c2c320fd0724dccd28cf5a5 [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 Awade6dbc9d2015-03-26 10:33:44 -070023import android.os.Looper;
Rekha Kumard240fe82015-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;
Yorke Lee2a66f7b2015-05-13 14:21:19 -070027import android.telecom.CallAudioState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080028import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080029import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070030import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070031import android.telecom.GatewayInfo;
32import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070033import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070035import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070036import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070037import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080038import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070039import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080040
Ihab Awada3653902015-01-23 13:44:46 -080041import com.android.internal.annotations.VisibleForTesting;
Rekha Kumard240fe82015-04-01 21:45:57 -070042import com.android.internal.telephony.PhoneConstants;
43import com.android.internal.telephony.TelephonyProperties;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070044import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080045
Santos Cordon4bc02452014-11-19 15:51:12 -080046import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070047import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070048import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070049import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070050import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070051import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070052import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080053
Ben Giladdd8c6082013-12-30 14:44:08 -080054/**
55 * Singleton.
56 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070057 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080058 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070059 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080060 */
Ihab Awadaa383cc2015-03-22 22:12:28 -070061@VisibleForTesting
62public class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080063
Santos Cordondf399862014-08-06 04:39:15 -070064 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070065 interface CallsManagerListener {
66 void onCallAdded(Call call);
67 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070068 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070069 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070070 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070071 ConnectionServiceWrapper oldService,
72 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070073 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070074 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Yorke Lee2a66f7b2015-05-13 14:21:19 -070076 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070077 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070078 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070079 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070080 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080081 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070082 }
83
Tyler Gunn91d43cf2014-09-17 12:19:39 -070084 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Santos Cordonf193ba42014-09-12 06:37:39 -070086 private static final int MAXIMUM_LIVE_CALLS = 1;
87 private static final int MAXIMUM_HOLD_CALLS = 1;
88 private static final int MAXIMUM_RINGING_CALLS = 1;
89 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080090 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070091
92 private static final int[] OUTGOING_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -070093 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -070094
Santos Cordon91bd74c2014-11-07 16:10:22 -080095 private static final int[] LIVE_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -070096 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING, CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -080097
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070099 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
100 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800101 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700102 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
103 * load factor before resizing, 1 means we only expect a single thread to
104 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800105 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700106 private final Set<Call> mCalls = Collections.newSetFromMap(
107 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800108
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700109 private final ConnectionServiceRepository mConnectionServiceRepository;
110 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
111 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700112 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800113 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700114 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800115 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700116 // For this set initial table size to 16 because we add 13 listeners in
117 // the CallsManager constructor.
118 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
119 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700120 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700121 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800122 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700123 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700124 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700125 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700126 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700127 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700128 private final TelecomSystem.SyncRoot mLock;
129 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700130 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
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 Awade6dbc9d2015-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,
Ihab Awadabcbce42015-04-07 14:04:01 -0700155 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700156 MissedCallNotifier missedCallNotifier,
157 PhoneAccountRegistrar phoneAccountRegistrar,
158 HeadsetMediaButtonFactory headsetMediaButtonFactory,
159 ProximitySensorManagerFactory proximitySensorManagerFactory,
160 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700161 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700162 mLock = lock;
163 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700164 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700165 mPhoneAccountRegistrar = phoneAccountRegistrar;
166 mMissedCallNotifier = missedCallNotifier;
167 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
168 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800169 mDockManager = new DockManager(context);
170 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700171 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700172 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700173 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700174 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700175 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800176 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
177 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700178 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700179 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700180 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800181 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700182 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800183 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700184
Santos Cordondeb8c892014-05-30 01:38:03 -0700185 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700186 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700187 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700188 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700189 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700190 mListeners.add(new RingbackPlayer(this, playerFactory));
191 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700192 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700193 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700194 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700195 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700196 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700197
Ihab Awadabcbce42015-04-07 14:04:01 -0700198 mMissedCallNotifier.updateOnStartup(
199 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800200 }
201
Ihab Awad8de76912015-02-17 12:25:52 -0800202 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
203 if (mRespondViaSmsManager != null) {
204 mListeners.remove(mRespondViaSmsManager);
205 }
206 mRespondViaSmsManager = respondViaSmsManager;
207 mListeners.add(respondViaSmsManager);
208 }
209
210 public RespondViaSmsManager getRespondViaSmsManager() {
211 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800212 }
213
Santos Cordon766d04f2014-05-06 10:28:25 -0700214 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700215 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700216 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700217
Tyler Gunncde2e502014-08-12 14:35:58 -0700218 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700219 if (!mCalls.contains(call)) {
220 // Call was not added previously in startOutgoingCall due to it being a potential MMI
221 // code, so add it now.
222 addCall(call);
223 }
224
225 // The call's ConnectionService has been updated.
226 for (CallsManagerListener listener : mListeners) {
227 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700228 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700229
230 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700231 }
232
233 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700234 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700235 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700236
Andrew Leee6288a72014-10-01 13:50:02 -0700237 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700238 }
239
240 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700241 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700242 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700243 setCallState(incomingCall, CallState.RINGING);
244
245 if (hasMaximumRingingCalls()) {
246 incomingCall.reject(false, null);
247 // since the call was not added to the list of calls, we have to call the missed
248 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700249 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700250 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
251 } else {
252 addCall(incomingCall);
253 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700254 }
255
256 @Override
257 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700258 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700259 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800260 }
261
Ihab Awadcb387ac2014-05-28 16:49:38 -0700262 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700263 public void onSuccessfulUnknownCall(Call call, int callState) {
264 setCallState(call, callState);
265 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
266 addCall(call);
267 }
268
269 @Override
270 public void onFailedUnknownCall(Call call) {
271 Log.i(this, "onFailedUnknownCall for call %s", call);
272 setCallState(call, CallState.DISCONNECTED);
273 call.removeListener(this);
274 }
275
276 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700277 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700278 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700279 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700280 }
281 }
282
Evan Charlton352105c2014-06-03 14:10:54 -0700283 @Override
284 public void onPostDialWait(Call call, String remaining) {
285 mInCallController.onPostDialWait(call, remaining);
286 }
287
Santos Cordona1610702014-06-04 20:22:56 -0700288 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800289 public void onPostDialChar(final Call call, char nextChar) {
290 if (PhoneNumberUtils.is12Key(nextChar)) {
291 // Play tone if it is one of the dialpad digits, canceling out the previously queued
292 // up stopTone runnable since playing a new tone automatically stops the previous tone.
293 if (mStopTone != null) {
294 mHandler.removeCallbacks(mStopTone);
295 }
296
297 mDtmfLocalTonePlayer.playTone(call, nextChar);
298
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700299 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800300 mStopTone = new Runnable() {
301 @Override
302 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700303 synchronized (mLock) {
304 // Set a timeout to stop the tone in case there isn't another tone to follow.
305 mDtmfLocalTonePlayer.stopTone(call);
306 }
Nancy Chena469f762014-12-09 18:29:20 -0800307 }
308 };
309 mHandler.postDelayed(
310 mStopTone,
311 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
312 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
313 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
314 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
315 // the previous tone is being stopped anyway.
316 if (mStopTone != null) {
317 mHandler.removeCallbacks(mStopTone);
318 }
319 mDtmfLocalTonePlayer.stopTone(call);
320 } else {
321 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
322 }
323 }
324
325 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700326 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700327 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800328 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700329 for (CallsManagerListener listener : mListeners) {
330 listener.onIsConferencedChanged(call);
331 }
332 }
333
334 @Override
335 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700336 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800337 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700338 for (CallsManagerListener listener : mListeners) {
339 listener.onIsConferencedChanged(call);
340 }
341 }
342
Ihab Awadff7493a2014-06-10 13:47:44 -0700343 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700344 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700345 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700346 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700347 }
348 }
349
Andrew Lee4a796602014-07-11 17:23:03 -0700350 @Override
351 public void onVideoStateChanged(Call call) {
352 for (CallsManagerListener listener : mListeners) {
353 listener.onVideoStateChanged(call);
354 }
355 }
356
Santos Cordoncf5b2912014-11-05 21:57:54 -0800357 @Override
358 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
359 mPendingCallsToDisconnect.add(call);
360 mHandler.postDelayed(new Runnable() {
361 @Override
362 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700363 synchronized (mLock) {
364 if (mPendingCallsToDisconnect.remove(call)) {
365 Log.i(this, "Delayed disconnection of call: %s", call);
366 call.disconnect();
367 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800368 }
369 }
370 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
371
372 return true;
373 }
374
Santos Cordon4bc02452014-11-19 15:51:12 -0800375 Collection<Call> getCalls() {
376 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700377 }
378
379 Call getForegroundCall() {
380 return mForegroundCall;
381 }
382
Santos Cordonae193062014-05-21 21:21:49 -0700383 Ringer getRinger() {
384 return mRinger;
385 }
386
Santos Cordonf3671a62014-05-29 21:51:53 -0700387 InCallController getInCallController() {
388 return mInCallController;
389 }
390
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700391 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700392 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700393 if (call.isEmergencyCall()) {
394 return true;
395 }
396 }
397 return false;
398 }
399
Andrew Lee45506232014-10-22 17:54:33 -0700400 boolean hasVideoCall() {
401 for (Call call : mCalls) {
Tyler Gunn5b882492015-06-03 10:03:13 -0700402 if (call.getVideoState() != VideoProfile.STATE_AUDIO_ONLY) {
Andrew Lee45506232014-10-22 17:54:33 -0700403 return true;
404 }
405 }
406 return false;
407 }
408
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700409 CallAudioState getAudioState() {
410 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700411 }
412
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700413 boolean isTtySupported() {
414 return mTtyManager.isTtySupported();
415 }
416
417 int getCurrentTtyMode() {
418 return mTtyManager.getCurrentTtyMode();
419 }
420
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700421 void addListener(CallsManagerListener listener) {
422 mListeners.add(listener);
423 }
424
425 void removeListener(CallsManagerListener listener) {
426 mListeners.remove(listener);
427 }
428
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800429 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700430 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800431 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700432 * @param phoneAccountHandle The phone account which contains the component name of the
433 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800434 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800435 */
Evan Charlton89176372014-07-19 18:23:09 -0700436 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800437 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700438 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
439 if (handle == null) {
440 // Required for backwards compatibility
441 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
442 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700443 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700444 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800445 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700446 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700447 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700448 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700449 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700450 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700451 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700452 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700453 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700454 true /* isIncoming */,
455 false /* isConference */);
456
Andrew Lee1b906652015-06-01 11:44:55 -0700457 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700458 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700459 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700460 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800461 }
462
Yorke Lee9250e5f2014-10-01 13:39:09 -0700463 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
464 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
465 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
466 Call call = new Call(
467 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800468 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700469 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700470 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700471 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700472 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700473 handle,
474 null /* gatewayInfo */,
475 null /* connectionManagerPhoneAccount */,
476 phoneAccountHandle,
477 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
478 // to the existing connection instead of trying to create a new one.
479 true /* isIncoming */,
480 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700481 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700482 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700483 call.addListener(this);
484 call.startCreateConnection(mPhoneAccountRegistrar);
485 }
486
Santos Cordoncf5b2912014-11-05 21:57:54 -0800487 private Call getNewOutgoingCall(Uri handle) {
488 // First check to see if we can reuse any of the calls that are waiting to disconnect.
489 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800490 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800491 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800492 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800493 mPendingCallsToDisconnect.remove(pendingCall);
494 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800495 reusedCall = pendingCall;
496 } else {
497 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800498 }
499 }
Yorke Lee59979512014-12-02 01:03:40 -0800500 if (reusedCall != null) {
501 return reusedCall;
502 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800503
504 // Create a call with original handle. The handle may be changed when the call is attached
505 // to a connection service, but in most cases will remain the same.
506 return new Call(
507 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800508 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700509 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800510 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700511 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700512 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800513 handle,
514 null /* gatewayInfo */,
515 null /* connectionManagerPhoneAccount */,
516 null /* phoneAccountHandle */,
517 false /* isIncoming */,
518 false /* isConference */);
519 }
520
Nancy Chen0d3076c2014-07-30 14:45:44 -0700521 /**
522 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
523 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700524 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700525 * @param phoneAccountHandle The phone account which contains the component name of the
526 * connection service to use for this call.
527 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700528 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700529 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800530 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700531
532 List<PhoneAccountHandle> accounts =
Santos Cordonea5cb932015-05-07 16:28:38 -0700533 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme(), false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700534
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800535 Log.v(this, "startOutgoingCall found accounts = " + accounts);
536
Nancy Chenbc95db92014-11-24 13:19:18 -0800537 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
538 // If there is an ongoing call, use the same phone account to place this new call.
539 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
540 }
541
Nancy Chenbc9ef172014-08-15 17:11:57 -0700542 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
543 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700544 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700545 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700546 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700547 phoneAccountHandle = null;
548 }
549 }
550
551 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700552 // No preset account, check if default exists that supports the URI scheme for the
553 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700554 phoneAccountHandle =
555 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700556 }
557
Nancy Chen1c5926f2014-09-17 14:44:14 -0700558 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700559
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700560 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
561 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700562 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700563
564 // Do not support any more live calls. Our options are to move a call to hold, disconnect
565 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700566 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700567 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800568 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800569 if (mCalls.contains(call)) {
570 // This call can already exist if it is a reused call,
571 // See {@link #getNewOutgoingCall}.
572 call.disconnect();
573 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700574 return null;
575 }
576
Nancy Chen8d92f452014-10-20 21:43:44 -0700577 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
578 !isEmergencyCall;
579
580 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700581 // This is the state where the user is expected to select an account
Santos Cordon92694512015-04-23 14:47:28 -0700582 call.setState(CallState.SELECT_PHONE_ACCOUNT);
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700583 // Create our own instance to modify (since extras may be Bundle.EMPTY)
584 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700585 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700586 } else {
587 call.setState(CallState.CONNECTING);
588 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700589
Andrew Lee1b906652015-06-01 11:44:55 -0700590 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700591
Tyler Gunn504eb832014-10-13 14:15:27 -0700592 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700593 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700594 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800595 } else if (!mCalls.contains(call)) {
596 // We check if mCalls already contains the call because we could potentially be reusing
597 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700598 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700599 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700600
601 return call;
602 }
603
Ben Gilada0d9f752014-02-26 11:49:03 -0800604 /**
Yorke Lee33501632014-03-17 19:24:12 -0700605 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800606 *
Yorke Lee33501632014-03-17 19:24:12 -0700607 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700608 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700609 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700610 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700611 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800612 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700613 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
614 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700615 if (call == null) {
616 // don't do anything if the call no longer exists
617 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700618 return;
619 }
620
Nancy Chen201b4372014-09-08 14:18:24 -0700621 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700622
623 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700624 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700625 } else {
626 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
627 Log.pii(uriHandle), Log.pii(handle));
628 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700629
Nancy Chen0d3076c2014-07-30 14:45:44 -0700630 call.setHandle(uriHandle);
631 call.setGatewayInfo(gatewayInfo);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700632 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700633
Santos Cordona04bdca2015-03-04 16:57:55 -0800634 if (speakerphoneOn) {
635 Log.i(this, "%s Starting with speakerphone as requested", call);
636 } else {
637 Log.i(this, "%s Starting with speakerphone because car is docked.", call);
638 }
639 call.setStartWithSpeakerphoneOn(speakerphoneOn || mDockManager.isDocked());
640
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700641 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
642 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700643 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700644 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700645 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700646 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700647
Santos Cordonf193ba42014-09-12 06:37:39 -0700648 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700649 // If the account has been set, proceed to place the outgoing call.
650 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700651 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700652 }
Yorke Leef98fb572014-03-05 10:56:55 -0800653 }
654
655 /**
Santos Cordona1610702014-06-04 20:22:56 -0700656 * Attempts to start a conference call for the specified call.
657 *
Santos Cordon12d61822014-07-29 16:02:20 -0700658 * @param call The call to conference.
659 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700660 */
Santos Cordon12d61822014-07-29 16:02:20 -0700661 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700662 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700663 }
664
665 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700666 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
667 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800668 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700669 *
670 * @param call The call to answer.
671 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800672 */
Andrew Lee38931d02014-07-16 10:17:36 -0700673 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700674 if (!mCalls.contains(call)) {
675 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800676 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700677 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700678 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700679 if (mForegroundCall != null && mForegroundCall != call &&
680 (mForegroundCall.isActive() ||
681 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800682 if (0 == (mForegroundCall.getConnectionCapabilities()
683 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700684 // This call does not support hold. If it is from a different connection
685 // service, then disconnect it, otherwise allow the connection service to
686 // figure out the right states.
687 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
688 mForegroundCall.disconnect();
689 }
690 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800691 Call heldCall = getHeldCall();
692 if (heldCall != null) {
693 Log.v(this, "Disconnecting held call %s before holding active call.",
694 heldCall);
695 heldCall.disconnect();
696 }
697
Santos Cordonf193ba42014-09-12 06:37:39 -0700698 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
699 mForegroundCall, call);
700 mForegroundCall.hold();
701 }
Santos Cordondf399862014-08-06 04:39:15 -0700702 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700703 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700704 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700705 }
706
Santos Cordona56f2762014-03-24 15:55:53 -0700707 for (CallsManagerListener listener : mListeners) {
708 listener.onIncomingCallAnswered(call);
709 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800710
Santos Cordon61d0f702014-02-19 02:52:23 -0800711 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700712 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700713 call.answer(videoState);
Tyler Gunn5b882492015-06-03 10:03:13 -0700714 if (VideoProfile.isVideo(videoState) &&
Rekha Kumard240fe82015-04-01 21:45:57 -0700715 !mWiredHeadsetManager.isPluggedIn() &&
716 !mCallAudioManager.isBluetoothDeviceAvailable() &&
717 isSpeakerEnabledForVideoCalls()) {
718 call.setStartWithSpeakerphoneOn(true);
719 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800720 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800721 }
722
Rekha Kumard240fe82015-04-01 21:45:57 -0700723 private static boolean isSpeakerEnabledForVideoCalls() {
724 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
725 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
726 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
727 }
728
Santos Cordone3d76ab2014-01-28 17:25:20 -0800729 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700730 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
731 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800732 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800733 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700734 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700735 if (!mCalls.contains(call)) {
736 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800737 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700738 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700739 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700740 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700741 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800742 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800743 }
744
745 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700746 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700747 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700748 * @param digit The DTMF digit to play.
749 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700750 void playDtmfTone(Call call, char digit) {
751 if (!mCalls.contains(call)) {
752 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700753 } else {
754 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700755 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700756 }
757 }
758
759 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700760 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700761 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700762 void stopDtmfTone(Call call) {
763 if (!mCalls.contains(call)) {
764 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700765 } else {
766 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700767 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700768 }
769 }
770
771 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700772 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700773 */
Evan Charlton352105c2014-06-03 14:10:54 -0700774 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700775 if (!mCalls.contains(call)) {
776 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700777 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700778 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700779 }
780 }
781
782 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700783 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800784 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
785 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800786 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700787 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700788 Log.v(this, "disconnectCall %s", call);
789
Sailesh Nepale59bb192014-04-01 18:33:59 -0700790 if (!mCalls.contains(call)) {
791 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800792 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700793 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800794 call.disconnect();
795 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800796 }
Santos Cordon681663d2014-01-30 04:32:15 -0800797
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700798 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700799 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700800 */
801 void disconnectAllCalls() {
802 Log.v(this, "disconnectAllCalls");
803
804 for (Call call : mCalls) {
805 disconnectCall(call);
806 }
807 }
808
Nancy Chenbc9ef172014-08-15 17:11:57 -0700809
Tyler Gunn61b92102014-08-19 07:42:20 -0700810 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700811 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700812 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
813 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700814 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700815 void holdCall(Call call) {
816 if (!mCalls.contains(call)) {
817 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700818 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700819 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700820 call.hold();
821 }
822 }
823
824 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700825 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700826 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
827 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700828 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700829 void unholdCall(Call call) {
830 if (!mCalls.contains(call)) {
831 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700832 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700833 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700834 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800835 // Only attempt to hold parent calls and not the individual children.
836 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700837 c.hold();
838 }
839 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700840 call.unhold();
841 }
842 }
843
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700844 /** Called by the in-call UI to change the mute state. */
845 void mute(boolean shouldMute) {
846 mCallAudioManager.mute(shouldMute);
847 }
848
849 /**
850 * Called by the in-call UI to change the audio route, for example to change from earpiece to
851 * speaker phone.
852 */
853 void setAudioRoute(int route) {
854 mCallAudioManager.setAudioRoute(route);
855 }
856
Yorke Leed1346872014-07-28 14:38:45 -0700857 /** Called by the in-call UI to turn the proximity sensor on. */
858 void turnOnProximitySensor() {
859 mProximitySensorManager.turnOn();
860 }
861
862 /**
863 * Called by the in-call UI to turn the proximity sensor off.
864 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
865 * the screen will be kept off until the proximity sensor goes negative.
866 */
867 void turnOffProximitySensor(boolean screenOnImmediately) {
868 mProximitySensorManager.turnOff(screenOnImmediately);
869 }
870
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700871 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700872 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700873 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700874 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700875 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -0700876 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -0700877 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
878 // respecting a rewritten number or a canceled number. This is unlikely since
879 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
880 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700881 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700882
883 // Note: emergency calls never go through account selection dialog so they never
884 // arrive here.
885 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700886 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700887 } else {
888 call.disconnect();
889 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700890
891 if (setDefault) {
892 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
893 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700894 }
895 }
896
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700897 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700898 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700899 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700900 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700901 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700902 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700903 }
904
Sailesh Nepale59bb192014-04-01 18:33:59 -0700905 void markCallAsRinging(Call call) {
906 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800907 }
908
Sailesh Nepale59bb192014-04-01 18:33:59 -0700909 void markCallAsDialing(Call call) {
910 setCallState(call, CallState.DIALING);
Santos Cordond6782cd2015-04-16 09:57:50 -0700911 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800912 }
913
Sailesh Nepale59bb192014-04-01 18:33:59 -0700914 void markCallAsActive(Call call) {
915 setCallState(call, CallState.ACTIVE);
Santos Cordond6782cd2015-04-16 09:57:50 -0700916 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800917 }
918
Sailesh Nepale59bb192014-04-01 18:33:59 -0700919 void markCallAsOnHold(Call call) {
920 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700921 }
922
Santos Cordon049b7b62014-01-30 05:34:26 -0800923 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700924 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
925 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800926 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800927 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800928 */
Andrew Lee701dc002014-09-11 21:29:12 -0700929 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
930 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700931 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700932 }
933
Ben Gilada0d9f752014-02-26 11:49:03 -0800934 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700935 * Removes an existing disconnected call, and notifies the in-call app.
936 */
937 void markCallAsRemoved(Call call) {
938 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700939 if (mLocallyDisconnectingCalls.contains(call)) {
940 mLocallyDisconnectingCalls.remove(call);
941 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
942 mForegroundCall.unhold();
943 }
944 }
Ihab Awada02bef52014-08-13 18:18:42 -0700945 }
946
947 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700948 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700949 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700950 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700951 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700952 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700953 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700954 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700955 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700956 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800957 if (call.getState() != CallState.DISCONNECTED) {
958 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
959 }
960 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700961 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700962 }
963 }
964 }
965
Santos Cordondeb8c892014-05-30 01:38:03 -0700966 boolean hasAnyCalls() {
967 return !mCalls.isEmpty();
968 }
969
Santos Cordonc7e85d42014-05-22 02:51:48 -0700970 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700971 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700972 }
973
974 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700975 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700976 }
977
Santos Cordondeb8c892014-05-30 01:38:03 -0700978 boolean onMediaButton(int type) {
979 if (hasAnyCalls()) {
980 if (HeadsetMediaButton.SHORT_PRESS == type) {
981 Call ringingCall = getFirstCallWithState(CallState.RINGING);
982 if (ringingCall == null) {
983 mCallAudioManager.toggleMute();
984 return true;
985 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700986 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700987 return true;
988 }
989 } else if (HeadsetMediaButton.LONG_PRESS == type) {
990 Log.d(this, "handleHeadsetHook: longpress -> hangup");
991 Call callToHangup = getFirstCallWithState(
992 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
993 if (callToHangup != null) {
994 callToHangup.disconnect();
995 return true;
996 }
997 }
998 }
999 return false;
1000 }
1001
1002 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001003 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001004 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001005 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -08001006 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1007 return false;
1008 }
1009
Santos Cordon91bd74c2014-11-07 16:10:22 -08001010 int count = 0;
1011 for (Call call : mCalls) {
1012 if (call.isEmergencyCall()) {
1013 // We never support add call if one of the calls is an emergency call.
1014 return false;
1015 } else if (call.getParentCall() == null) {
1016 count++;
1017 }
Santos Cordon10838c22014-06-11 17:36:04 -07001018
Santos Cordon91bd74c2014-11-07 16:10:22 -08001019 // We do not check states for canAddCall. We treat disconnected calls the same
1020 // and wait until they are removed instead. If we didn't count disconnected calls,
1021 // we could put InCallServices into a state where they are showing two calls but
1022 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1023 // and acts better if we wait until the call is removed.
1024 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001025 return false;
1026 }
1027 }
1028 return true;
1029 }
1030
Ihab Awada3653902015-01-23 13:44:46 -08001031 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001032 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001033 return getFirstCallWithState(CallState.RINGING);
1034 }
1035
1036 Call getActiveCall() {
1037 return getFirstCallWithState(CallState.ACTIVE);
1038 }
1039
Nancy Chen05a9e402014-09-26 14:14:32 -07001040 Call getDialingCall() {
1041 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001042 }
1043
1044 Call getHeldCall() {
1045 return getFirstCallWithState(CallState.ON_HOLD);
1046 }
1047
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001048 int getNumHeldCalls() {
1049 int count = 0;
1050 for (Call call : mCalls) {
1051 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1052 count++;
1053 }
1054 }
1055 return count;
1056 }
1057
Santos Cordonf193ba42014-09-12 06:37:39 -07001058 Call getFirstCallWithState(int... states) {
1059 return getFirstCallWithState(null, states);
1060 }
1061
Santos Cordon10838c22014-06-11 17:36:04 -07001062 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001063 * Returns the first call that it finds with the given states. The states are treated as having
1064 * priority order so that any call with the first state will be returned before any call with
1065 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001066 *
1067 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001068 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001069 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001070 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001071 // check the foreground first
1072 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1073 return mForegroundCall;
1074 }
1075
Santos Cordondeb8c892014-05-30 01:38:03 -07001076 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001077 if (Objects.equals(callToSkip, call)) {
1078 continue;
1079 }
1080
1081 // Only operate on top-level calls
1082 if (call.getParentCall() != null) {
1083 continue;
1084 }
1085
Santos Cordondeb8c892014-05-30 01:38:03 -07001086 if (currentState == call.getState()) {
1087 return call;
1088 }
1089 }
1090 }
1091 return null;
1092 }
1093
Santos Cordon0fbe6322014-08-14 04:04:25 -07001094 Call createConferenceCall(
1095 PhoneAccountHandle phoneAccount,
1096 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001097
1098 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1099 // which is the default value for new Calls.
1100 long connectTime =
1101 parcelableConference.getConnectTimeMillis() ==
1102 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1103 parcelableConference.getConnectTimeMillis();
1104
Santos Cordon0fbe6322014-08-14 04:04:25 -07001105 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001106 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001107 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001108 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001109 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001110 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001111 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001112 null /* handle */,
1113 null /* gatewayInfo */,
1114 null /* connectionManagerPhoneAccount */,
1115 phoneAccount,
1116 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001117 true /* isConference */,
1118 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001119
1120 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001121 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001122 call.setVideoState(parcelableConference.getVideoState());
1123 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001124 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001125 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001126
1127 // TODO: Move this to be a part of addCall()
1128 call.addListener(this);
1129 addCall(call);
1130 return call;
1131 }
1132
Yorke Leef86db2e2014-09-12 17:56:48 -07001133 /**
1134 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1135 */
1136 int getCallState() {
1137 return mPhoneStateBroadcaster.getCallState();
1138 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001139
Santos Cordon4b2c1192014-03-19 18:15:38 -07001140 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001141 * Retrieves the {@link PhoneAccountRegistrar}.
1142 *
1143 * @return The {@link PhoneAccountRegistrar}.
1144 */
1145 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1146 return mPhoneAccountRegistrar;
1147 }
1148
1149 /**
1150 * Retrieves the {@link MissedCallNotifier}
1151 * @return The {@link MissedCallNotifier}.
1152 */
1153 MissedCallNotifier getMissedCallNotifier() {
1154 return mMissedCallNotifier;
1155 }
1156
1157 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001158 * Adds the specified call to the main list of live calls.
1159 *
1160 * @param call The call to add.
1161 */
1162 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001163 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001164 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001165 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001166 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001167
Santos Cordondf399862014-08-06 04:39:15 -07001168 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001169 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001170 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001171 if (Log.SYSTRACE_DEBUG) {
1172 Trace.beginSection(listener.getClass().toString() + " addCall");
1173 }
Santos Cordona56f2762014-03-24 15:55:53 -07001174 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001175 if (Log.SYSTRACE_DEBUG) {
1176 Trace.endSection();
1177 }
Santos Cordona56f2762014-03-24 15:55:53 -07001178 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001179 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001180 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001181 }
1182
Sailesh Nepal810735e2014-03-18 18:15:46 -07001183 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001184 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001185 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001186
Santos Cordon672321e2014-08-21 14:38:58 -07001187 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001188 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001189 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001190
1191 boolean shouldNotify = false;
1192 if (mCalls.contains(call)) {
1193 mCalls.remove(call);
1194 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001195 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001196
Santos Cordon2685dab2015-04-17 17:12:09 -07001197 call.destroy();
1198
Sailesh Nepale59bb192014-04-01 18:33:59 -07001199 // Only broadcast changes for calls that are being tracked.
1200 if (shouldNotify) {
1201 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001202 if (Log.SYSTRACE_DEBUG) {
1203 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1204 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001205 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001206 if (Log.SYSTRACE_DEBUG) {
1207 Trace.endSection();
1208 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001209 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001210 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001211 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001212 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001213 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001214
Sailesh Nepal810735e2014-03-18 18:15:46 -07001215 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001216 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001217 *
1218 * @param call The call.
1219 * @param newState The new state of the call.
1220 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001221 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001222 if (call == null) {
1223 return;
1224 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001225 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001226 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1227 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001228 if (newState != oldState) {
1229 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1230 // 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 -07001231 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001232 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001233 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001234 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001235 // unexpected transition occurs.
1236 call.setState(newState);
1237
Yorke Leee4a9c412014-11-14 16:59:42 -08001238 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001239 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001240 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001241 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001242 if (Log.SYSTRACE_DEBUG) {
1243 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1244 }
Santos Cordona56f2762014-03-24 15:55:53 -07001245 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001246 if (Log.SYSTRACE_DEBUG) {
1247 Trace.endSection();
1248 }
Santos Cordona56f2762014-03-24 15:55:53 -07001249 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001250 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001251 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001252 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001253 }
1254 }
1255
1256 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001257 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001258 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001259 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001260 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001261 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001262 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001263 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001264 // of its state will be foreground by default and instead the connection service should
1265 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001266 // the call should play audio and listen to microphone if it wants.
1267
Santos Cordonf193ba42014-09-12 06:37:39 -07001268 // Only top-level calls can be in foreground
1269 if (call.getParentCall() != null) {
1270 continue;
1271 }
1272
Santos Cordon40f78c22014-04-07 02:11:42 -07001273 // Active calls have priority.
1274 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001275 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001276 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001277 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001278
1279 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001280 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001281 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001282 }
1283 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001284
Sailesh Nepal810735e2014-03-18 18:15:46 -07001285 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001286 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001287 Call oldForegroundCall = mForegroundCall;
1288 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001289
Santos Cordona56f2762014-03-24 15:55:53 -07001290 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001291 if (Log.SYSTRACE_DEBUG) {
1292 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1293 }
Santos Cordona56f2762014-03-24 15:55:53 -07001294 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001295 if (Log.SYSTRACE_DEBUG) {
1296 Trace.endSection();
1297 }
Santos Cordona56f2762014-03-24 15:55:53 -07001298 }
Santos Cordon681663d2014-01-30 04:32:15 -08001299 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001300 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001301 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001302
Santos Cordon91bd74c2014-11-07 16:10:22 -08001303 private void updateCanAddCall() {
1304 boolean newCanAddCall = canAddCall();
1305 if (newCanAddCall != mCanAddCall) {
1306 mCanAddCall = newCanAddCall;
1307 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001308 if (Log.SYSTRACE_DEBUG) {
1309 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1310 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001311 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001312 if (Log.SYSTRACE_DEBUG) {
1313 Trace.endSection();
1314 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001315 }
1316 }
1317 }
1318
1319 private void updateCallsManagerState() {
1320 updateForegroundCall();
1321 updateCanAddCall();
1322 }
1323
Yorke Leeb701f6d2014-08-12 14:04:19 -07001324 private boolean isPotentialMMICode(Uri handle) {
1325 return (handle != null && handle.getSchemeSpecificPart() != null
1326 && handle.getSchemeSpecificPart().contains("#"));
1327 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001328
Tyler Gunn5b452df2014-10-01 15:02:58 -07001329 /**
1330 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1331 * MMI codes which can be dialed when one or more calls are in progress.
1332 * <P>
1333 * Checks for numbers formatted similar to the MMI codes defined in:
1334 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1335 * and
1336 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1337 *
1338 * @param handle The URI to call.
1339 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1340 */
1341 private boolean isPotentialInCallMMICode(Uri handle) {
1342 if (handle != null && handle.getSchemeSpecificPart() != null &&
1343 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1344
1345 String dialedNumber = handle.getSchemeSpecificPart();
1346 return (dialedNumber.equals("0") ||
1347 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1348 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1349 dialedNumber.equals("3") ||
1350 dialedNumber.equals("4") ||
1351 dialedNumber.equals("5"));
1352 }
1353 return false;
1354 }
1355
Santos Cordonf193ba42014-09-12 06:37:39 -07001356 private int getNumCallsWithState(int... states) {
1357 int count = 0;
1358 for (int state : states) {
1359 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001360 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001361 count++;
1362 }
1363 }
1364 }
1365 return count;
1366 }
1367
1368 private boolean hasMaximumLiveCalls() {
1369 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1370 }
1371
1372 private boolean hasMaximumHoldingCalls() {
1373 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1374 }
1375
1376 private boolean hasMaximumRingingCalls() {
1377 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1378 }
1379
1380 private boolean hasMaximumOutgoingCalls() {
1381 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1382 }
1383
1384 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1385 if (hasMaximumLiveCalls()) {
1386 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1387 // have to change.
1388 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301389 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1390 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001391
Santos Cordond1766502014-09-26 15:45:39 -07001392 if (call == liveCall) {
1393 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001394 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001395 // state since the call was already populated into the list.
1396 return true;
1397 }
1398
Andrew Leee6595182014-09-23 18:43:05 -07001399 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001400 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1401 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1402 // Disconnect the current outgoing call if it's not an emergency call. If the
1403 // user tries to make two outgoing calls to different emergency call numbers,
1404 // we will try to connect the first outgoing call.
1405 outgoingCall.disconnect();
1406 return true;
1407 }
1408 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1409 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1410 // state, just disconnect it since the user has explicitly started a new call.
1411 outgoingCall.disconnect();
1412 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001413 }
1414 return false;
1415 }
1416
Santos Cordonf193ba42014-09-12 06:37:39 -07001417 if (hasMaximumHoldingCalls()) {
1418 // There is no more room for any more calls, unless it's an emergency.
1419 if (isEmergency) {
1420 // Kill the current active call, this is easier then trying to disconnect a
1421 // holding call and hold an active call.
1422 liveCall.disconnect();
1423 return true;
1424 }
1425 return false; // No more room!
1426 }
1427
1428 // We have room for at least one more holding call at this point.
1429
1430 // First thing, if we are trying to make a call with the same phone account as the live
1431 // call, then allow it so that the connection service can make its own decision about
1432 // how to handle the new call relative to the current one.
1433 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1434 return true;
1435 } else if (call.getTargetPhoneAccount() == null) {
1436 // Without a phone account, we can't say reliably that the call will fail.
1437 // If the user chooses the same phone account as the live call, then it's
1438 // still possible that the call can be made (like with CDMA calls not supporting
1439 // hold but they still support adding a call by going immediately into conference
1440 // mode). Return true here and we'll run this code again after user chooses an
1441 // account.
1442 return true;
1443 }
1444
1445 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001446 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001447 liveCall.hold();
1448 return true;
1449 }
1450
1451 // The live call cannot be held so we're out of luck here. There's no room.
1452 return false;
1453 }
1454 return true;
1455 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001456
1457 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001458 * Checks to see if the call should be on speakerphone and if so, set it.
1459 */
1460 private void maybeMoveToSpeakerPhone(Call call) {
1461 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001462 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001463 call.setStartWithSpeakerphoneOn(false);
1464 }
1465 }
1466
1467 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001468 * Creates a new call for an existing connection.
1469 *
1470 * @param callId The id of the new call.
1471 * @param connection The connection information.
1472 * @return The new call.
1473 */
1474 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1475 Call call = new Call(
1476 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001477 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001478 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001479 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001480 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001481 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001482 connection.getHandle() /* handle */,
1483 null /* gatewayInfo */,
1484 null /* connectionManagerPhoneAccount */,
1485 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1486 false /* isIncoming */,
1487 false /* isConference */);
1488
1489 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001490 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001491 call.setCallerDisplayName(connection.getCallerDisplayName(),
1492 connection.getCallerDisplayNamePresentation());
1493
1494 call.addListener(this);
1495 addCall(call);
1496
1497 return call;
1498 }
1499
1500 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001501 * Dumps the state of the {@link CallsManager}.
1502 *
1503 * @param pw The {@code IndentingPrintWriter} to write the state to.
1504 */
1505 public void dump(IndentingPrintWriter pw) {
1506 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001507 if (mCalls != null) {
1508 pw.println("mCalls: ");
1509 pw.increaseIndent();
1510 for (Call call : mCalls) {
1511 pw.println(call);
1512 }
1513 pw.decreaseIndent();
1514 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001515 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1516
1517 if (mCallAudioManager != null) {
1518 pw.println("mCallAudioManager:");
1519 pw.increaseIndent();
1520 mCallAudioManager.dump(pw);
1521 pw.decreaseIndent();
1522 }
1523
1524 if (mTtyManager != null) {
1525 pw.println("mTtyManager:");
1526 pw.increaseIndent();
1527 mTtyManager.dump(pw);
1528 pw.decreaseIndent();
1529 }
1530
1531 if (mInCallController != null) {
1532 pw.println("mInCallController:");
1533 pw.increaseIndent();
1534 mInCallController.dump(pw);
1535 pw.decreaseIndent();
1536 }
1537
1538 if (mConnectionServiceRepository != null) {
1539 pw.println("mConnectionServiceRepository:");
1540 pw.increaseIndent();
1541 mConnectionServiceRepository.dump(pw);
1542 pw.decreaseIndent();
1543 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001544 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001545}