blob: 4761fd8aad5b3db88aa1e9b9c4dbf547bfcbfc2b [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;
Yorke Lee8d3f2692015-05-08 11:44:39 -070040import android.text.TextUtils;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080041
Ihab Awada3653902015-01-23 13:44:46 -080042import com.android.internal.annotations.VisibleForTesting;
Rekha Kumard240fe82015-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 Awadaa383cc2015-03-22 22:12:28 -070062@VisibleForTesting
Tyler Gunn86014fc2015-06-12 14:31:25 -070063public class CallsManager extends Call.ListenerBase implements VideoProviderProxy.Listener {
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);
Yorke Lee2a66f7b2015-05-13 14:21:19 -070077 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState 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);
Tyler Gunn86014fc2015-06-12 14:31:25 -070083 void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile);
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 =
Santos Cordon92694512015-04-23 14:47:28 -070095 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -070096
Santos Cordon91bd74c2014-11-07 16:10:22 -080097 private static final int[] LIVE_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -070098 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING, CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -080099
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;
Santos Cordona04bdca2015-03-04 16:57:55 -0800124 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700125 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700126 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700127 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700128 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700129 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700130 private final TelecomSystem.SyncRoot mLock;
131 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700132 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700133 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
134 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700135 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800136 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
137 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700138 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700139
Santos Cordon91bd74c2014-11-07 16:10:22 -0800140 private boolean mCanAddCall = true;
141
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700142 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700143 * The call the user is currently interacting with. This is the call that should have audio
144 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800145 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700146 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800147
Nancy Chena469f762014-12-09 18:29:20 -0800148 private Runnable mStopTone;
149
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700150 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700151 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800152 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700153 CallsManager(
154 Context context,
155 TelecomSystem.SyncRoot lock,
156 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700157 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700158 MissedCallNotifier missedCallNotifier,
159 PhoneAccountRegistrar phoneAccountRegistrar,
160 HeadsetMediaButtonFactory headsetMediaButtonFactory,
161 ProximitySensorManagerFactory proximitySensorManagerFactory,
162 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700163 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700164 mLock = lock;
165 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700166 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700167 mPhoneAccountRegistrar = phoneAccountRegistrar;
168 mMissedCallNotifier = missedCallNotifier;
169 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
170 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800171 mDockManager = new DockManager(context);
172 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700173 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700174 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700175 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700176 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700177 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800178 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
179 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700180 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700181 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700182 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800183 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700184 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800185 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700186
Santos Cordondeb8c892014-05-30 01:38:03 -0700187 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700188 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700189 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700190 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700191 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700192 mListeners.add(new RingbackPlayer(this, playerFactory));
193 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700194 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700195 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700196 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700197 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700198 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700199
Ihab Awadabcbce42015-04-07 14:04:01 -0700200 mMissedCallNotifier.updateOnStartup(
201 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800202 }
203
Ihab Awad8de76912015-02-17 12:25:52 -0800204 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
205 if (mRespondViaSmsManager != null) {
206 mListeners.remove(mRespondViaSmsManager);
207 }
208 mRespondViaSmsManager = respondViaSmsManager;
209 mListeners.add(respondViaSmsManager);
210 }
211
212 public RespondViaSmsManager getRespondViaSmsManager() {
213 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800214 }
215
Santos Cordon766d04f2014-05-06 10:28:25 -0700216 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700217 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700218 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700219
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700220 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700221 if (!mCalls.contains(call)) {
222 // Call was not added previously in startOutgoingCall due to it being a potential MMI
223 // code, so add it now.
224 addCall(call);
225 }
226
227 // The call's ConnectionService has been updated.
228 for (CallsManagerListener listener : mListeners) {
229 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700230 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700231
232 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700233 }
234
235 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700236 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700237 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700238
Andrew Leee6288a72014-10-01 13:50:02 -0700239 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700240 }
241
242 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700243 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700244 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700245 setCallState(incomingCall, CallState.RINGING, "successful incoming call");
Santos Cordonf193ba42014-09-12 06:37:39 -0700246
247 if (hasMaximumRingingCalls()) {
248 incomingCall.reject(false, null);
249 // since the call was not added to the list of calls, we have to call the missed
250 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700251 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700252 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
253 } else {
254 addCall(incomingCall);
255 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700256 }
257
258 @Override
259 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700260 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700261 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800262 }
263
Ihab Awadcb387ac2014-05-28 16:49:38 -0700264 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700265 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700266 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700267 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
268 addCall(call);
269 }
270
271 @Override
272 public void onFailedUnknownCall(Call call) {
273 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700274 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700275 call.removeListener(this);
276 }
277
278 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700279 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700280 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700281 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700282 }
283 }
284
Evan Charlton352105c2014-06-03 14:10:54 -0700285 @Override
286 public void onPostDialWait(Call call, String remaining) {
287 mInCallController.onPostDialWait(call, remaining);
288 }
289
Santos Cordona1610702014-06-04 20:22:56 -0700290 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800291 public void onPostDialChar(final Call call, char nextChar) {
292 if (PhoneNumberUtils.is12Key(nextChar)) {
293 // Play tone if it is one of the dialpad digits, canceling out the previously queued
294 // up stopTone runnable since playing a new tone automatically stops the previous tone.
295 if (mStopTone != null) {
296 mHandler.removeCallbacks(mStopTone);
297 }
298
299 mDtmfLocalTonePlayer.playTone(call, nextChar);
300
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700301 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800302 mStopTone = new Runnable() {
303 @Override
304 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700305 synchronized (mLock) {
306 // Set a timeout to stop the tone in case there isn't another tone to follow.
307 mDtmfLocalTonePlayer.stopTone(call);
308 }
Nancy Chena469f762014-12-09 18:29:20 -0800309 }
310 };
311 mHandler.postDelayed(
312 mStopTone,
313 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
314 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
315 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
316 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
317 // the previous tone is being stopped anyway.
318 if (mStopTone != null) {
319 mHandler.removeCallbacks(mStopTone);
320 }
321 mDtmfLocalTonePlayer.stopTone(call);
322 } else {
323 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
324 }
325 }
326
327 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700328 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700329 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800330 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700331 for (CallsManagerListener listener : mListeners) {
332 listener.onIsConferencedChanged(call);
333 }
334 }
335
336 @Override
337 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700338 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800339 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700340 for (CallsManagerListener listener : mListeners) {
341 listener.onIsConferencedChanged(call);
342 }
343 }
344
Ihab Awadff7493a2014-06-10 13:47:44 -0700345 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700346 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700347 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700348 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700349 }
350 }
351
Andrew Lee4a796602014-07-11 17:23:03 -0700352 @Override
353 public void onVideoStateChanged(Call call) {
354 for (CallsManagerListener listener : mListeners) {
355 listener.onVideoStateChanged(call);
356 }
357 }
358
Santos Cordoncf5b2912014-11-05 21:57:54 -0800359 @Override
360 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
361 mPendingCallsToDisconnect.add(call);
362 mHandler.postDelayed(new Runnable() {
363 @Override
364 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700365 synchronized (mLock) {
366 if (mPendingCallsToDisconnect.remove(call)) {
367 Log.i(this, "Delayed disconnection of call: %s", call);
368 call.disconnect();
369 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800370 }
371 }
372 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
373
374 return true;
375 }
376
Tyler Gunn86014fc2015-06-12 14:31:25 -0700377 /**
378 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
379 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
380 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
381 * respond to callbacks from the {@link VideoProviderProxy}.
382 *
383 * @param call The call.
384 */
385 @Override
386 public void onVideoCallProviderChanged(Call call) {
387 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
388
389 if (videoProviderProxy == null) {
390 return;
391 }
392
393 videoProviderProxy.addListener(this);
394 }
395
396 /**
397 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
398 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
399 * modification request.
400 *
401 * @param call The call.
402 * @param videoProfile The {@link VideoProfile}.
403 */
404 @Override
405 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
406 int videoState = videoProfile != null ? videoProfile.getVideoState() :
407 VideoProfile.STATE_AUDIO_ONLY;
408 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
409 .videoStateToString(videoState));
410
411 for (CallsManagerListener listener : mListeners) {
412 listener.onSessionModifyRequestReceived(call, videoProfile);
413 }
414 }
415
Santos Cordon4bc02452014-11-19 15:51:12 -0800416 Collection<Call> getCalls() {
417 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700418 }
419
420 Call getForegroundCall() {
421 return mForegroundCall;
422 }
423
Santos Cordonae193062014-05-21 21:21:49 -0700424 Ringer getRinger() {
425 return mRinger;
426 }
427
Santos Cordonf3671a62014-05-29 21:51:53 -0700428 InCallController getInCallController() {
429 return mInCallController;
430 }
431
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700432 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700434 if (call.isEmergencyCall()) {
435 return true;
436 }
437 }
438 return false;
439 }
440
Andrew Lee45506232014-10-22 17:54:33 -0700441 boolean hasVideoCall() {
442 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700443 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700444 return true;
445 }
446 }
447 return false;
448 }
449
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700450 CallAudioState getAudioState() {
451 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700452 }
453
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700454 boolean isTtySupported() {
455 return mTtyManager.isTtySupported();
456 }
457
458 int getCurrentTtyMode() {
459 return mTtyManager.getCurrentTtyMode();
460 }
461
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700462 void addListener(CallsManagerListener listener) {
463 mListeners.add(listener);
464 }
465
466 void removeListener(CallsManagerListener listener) {
467 mListeners.remove(listener);
468 }
469
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800470 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700471 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800472 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700473 * @param phoneAccountHandle The phone account which contains the component name of the
474 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800475 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800476 */
Evan Charlton89176372014-07-19 18:23:09 -0700477 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800478 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700479 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
480 if (handle == null) {
481 // Required for backwards compatibility
482 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
483 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700484 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700485 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800486 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700487 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700488 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700489 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700490 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700491 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700492 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700493 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700494 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700495 true /* isIncoming */,
496 false /* isConference */);
497
Andrew Lee1b906652015-06-01 11:44:55 -0700498 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700499 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700500 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700501 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800502 }
503
Yorke Lee9250e5f2014-10-01 13:39:09 -0700504 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
505 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
506 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
507 Call call = new Call(
508 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800509 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700510 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700511 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700512 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700513 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700514 handle,
515 null /* gatewayInfo */,
516 null /* connectionManagerPhoneAccount */,
517 phoneAccountHandle,
518 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
519 // to the existing connection instead of trying to create a new one.
520 true /* isIncoming */,
521 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700522 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700523 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700524 call.addListener(this);
525 call.startCreateConnection(mPhoneAccountRegistrar);
526 }
527
Yorke Lee8d3f2692015-05-08 11:44:39 -0700528 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
529 if (handle1 == null || handle2 == null) {
530 return handle1 == handle2;
531 }
532
533 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
534 return false;
535 }
536
537 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
538 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
539 return TextUtils.equals(number1, number2);
540 }
541
Santos Cordoncf5b2912014-11-05 21:57:54 -0800542 private Call getNewOutgoingCall(Uri handle) {
543 // First check to see if we can reuse any of the calls that are waiting to disconnect.
544 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800545 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800546 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700547 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800548 mPendingCallsToDisconnect.remove(pendingCall);
549 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800550 reusedCall = pendingCall;
551 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700552 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800553 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800554 }
555 }
Yorke Lee59979512014-12-02 01:03:40 -0800556 if (reusedCall != null) {
557 return reusedCall;
558 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800559
560 // Create a call with original handle. The handle may be changed when the call is attached
561 // to a connection service, but in most cases will remain the same.
562 return new Call(
563 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800564 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700565 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800566 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700567 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700568 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800569 handle,
570 null /* gatewayInfo */,
571 null /* connectionManagerPhoneAccount */,
572 null /* phoneAccountHandle */,
573 false /* isIncoming */,
574 false /* isConference */);
575 }
576
Nancy Chen0d3076c2014-07-30 14:45:44 -0700577 /**
578 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
579 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700580 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700581 * @param phoneAccountHandle The phone account which contains the component name of the
582 * connection service to use for this call.
583 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700584 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700585 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800586 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700587
588 List<PhoneAccountHandle> accounts =
Santos Cordonea5cb932015-05-07 16:28:38 -0700589 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme(), false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700590
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800591 Log.v(this, "startOutgoingCall found accounts = " + accounts);
592
Nancy Chenbc95db92014-11-24 13:19:18 -0800593 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
594 // If there is an ongoing call, use the same phone account to place this new call.
595 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
596 }
597
Nancy Chenbc9ef172014-08-15 17:11:57 -0700598 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
599 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700600 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700601 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700602 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700603 phoneAccountHandle = null;
604 }
605 }
606
607 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700608 // No preset account, check if default exists that supports the URI scheme for the
609 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700610 phoneAccountHandle =
611 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700612 }
613
Nancy Chen1c5926f2014-09-17 14:44:14 -0700614 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700615
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700616 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
617 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700618 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700619
620 // Do not support any more live calls. Our options are to move a call to hold, disconnect
621 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700622 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700623 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800624 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800625 if (mCalls.contains(call)) {
626 // This call can already exist if it is a reused call,
627 // See {@link #getNewOutgoingCall}.
628 call.disconnect();
629 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700630 return null;
631 }
632
Nancy Chen8d92f452014-10-20 21:43:44 -0700633 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
634 !isEmergencyCall;
635
636 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700637 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700638 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700639 // Create our own instance to modify (since extras may be Bundle.EMPTY)
640 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700641 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700642 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700643 call.setState(
644 CallState.CONNECTING,
645 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700646 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700647
Andrew Lee1b906652015-06-01 11:44:55 -0700648 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700649
Tyler Gunn504eb832014-10-13 14:15:27 -0700650 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700651 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700652 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800653 } else if (!mCalls.contains(call)) {
654 // We check if mCalls already contains the call because we could potentially be reusing
655 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700656 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700657 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700658
659 return call;
660 }
661
Ben Gilada0d9f752014-02-26 11:49:03 -0800662 /**
Yorke Lee33501632014-03-17 19:24:12 -0700663 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800664 *
Yorke Lee33501632014-03-17 19:24:12 -0700665 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700666 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700667 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700668 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700669 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800670 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700671 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
672 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700673 if (call == null) {
674 // don't do anything if the call no longer exists
675 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700676 return;
677 }
678
Nancy Chen201b4372014-09-08 14:18:24 -0700679 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700680
681 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700682 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700683 } else {
684 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
685 Log.pii(uriHandle), Log.pii(handle));
686 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700687
Nancy Chen0d3076c2014-07-30 14:45:44 -0700688 call.setHandle(uriHandle);
689 call.setGatewayInfo(gatewayInfo);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700690 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700691
Santos Cordona04bdca2015-03-04 16:57:55 -0800692 if (speakerphoneOn) {
693 Log.i(this, "%s Starting with speakerphone as requested", call);
694 } else {
695 Log.i(this, "%s Starting with speakerphone because car is docked.", call);
696 }
697 call.setStartWithSpeakerphoneOn(speakerphoneOn || mDockManager.isDocked());
698
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700699 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
700 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700701 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700702 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700703 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700704 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700705
Santos Cordonf193ba42014-09-12 06:37:39 -0700706 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700707 // If the account has been set, proceed to place the outgoing call.
708 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700709 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700710 }
Yorke Leef98fb572014-03-05 10:56:55 -0800711 }
712
713 /**
Santos Cordona1610702014-06-04 20:22:56 -0700714 * Attempts to start a conference call for the specified call.
715 *
Santos Cordon12d61822014-07-29 16:02:20 -0700716 * @param call The call to conference.
717 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700718 */
Santos Cordon12d61822014-07-29 16:02:20 -0700719 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700720 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700721 }
722
723 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700724 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
725 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800726 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700727 *
728 * @param call The call to answer.
729 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800730 */
Andrew Lee38931d02014-07-16 10:17:36 -0700731 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700732 if (!mCalls.contains(call)) {
733 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800734 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700735 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700736 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700737 if (mForegroundCall != null && mForegroundCall != call &&
738 (mForegroundCall.isActive() ||
739 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800740 if (0 == (mForegroundCall.getConnectionCapabilities()
741 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700742 // This call does not support hold. If it is from a different connection
743 // service, then disconnect it, otherwise allow the connection service to
744 // figure out the right states.
745 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
746 mForegroundCall.disconnect();
747 }
748 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800749 Call heldCall = getHeldCall();
750 if (heldCall != null) {
751 Log.v(this, "Disconnecting held call %s before holding active call.",
752 heldCall);
753 heldCall.disconnect();
754 }
755
Santos Cordonf193ba42014-09-12 06:37:39 -0700756 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
757 mForegroundCall, call);
758 mForegroundCall.hold();
759 }
Santos Cordondf399862014-08-06 04:39:15 -0700760 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700761 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700762 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700763 }
764
Santos Cordona56f2762014-03-24 15:55:53 -0700765 for (CallsManagerListener listener : mListeners) {
766 listener.onIncomingCallAnswered(call);
767 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800768
Santos Cordon61d0f702014-02-19 02:52:23 -0800769 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700770 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700771 call.answer(videoState);
Tyler Gunn5b882492015-06-03 10:03:13 -0700772 if (VideoProfile.isVideo(videoState) &&
Rekha Kumard240fe82015-04-01 21:45:57 -0700773 !mWiredHeadsetManager.isPluggedIn() &&
774 !mCallAudioManager.isBluetoothDeviceAvailable() &&
775 isSpeakerEnabledForVideoCalls()) {
776 call.setStartWithSpeakerphoneOn(true);
777 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800778 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800779 }
780
Rekha Kumard240fe82015-04-01 21:45:57 -0700781 private static boolean isSpeakerEnabledForVideoCalls() {
782 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
783 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
784 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
785 }
786
Santos Cordone3d76ab2014-01-28 17:25:20 -0800787 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700788 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
789 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800790 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800791 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700792 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700793 if (!mCalls.contains(call)) {
794 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800795 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700796 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700797 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700798 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700799 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800800 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800801 }
802
803 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700804 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700805 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700806 * @param digit The DTMF digit to play.
807 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700808 void playDtmfTone(Call call, char digit) {
809 if (!mCalls.contains(call)) {
810 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700811 } else {
812 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700813 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700814 }
815 }
816
817 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700818 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700819 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700820 void stopDtmfTone(Call call) {
821 if (!mCalls.contains(call)) {
822 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700823 } else {
824 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700825 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700826 }
827 }
828
829 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700830 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700831 */
Evan Charlton352105c2014-06-03 14:10:54 -0700832 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700833 if (!mCalls.contains(call)) {
834 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700835 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700836 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700837 }
838 }
839
840 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700841 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800842 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
843 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800844 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700845 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700846 Log.v(this, "disconnectCall %s", call);
847
Sailesh Nepale59bb192014-04-01 18:33:59 -0700848 if (!mCalls.contains(call)) {
849 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800850 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700851 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800852 call.disconnect();
853 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800854 }
Santos Cordon681663d2014-01-30 04:32:15 -0800855
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700856 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700857 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700858 */
859 void disconnectAllCalls() {
860 Log.v(this, "disconnectAllCalls");
861
862 for (Call call : mCalls) {
863 disconnectCall(call);
864 }
865 }
866
Nancy Chenbc9ef172014-08-15 17:11:57 -0700867
Tyler Gunn61b92102014-08-19 07:42:20 -0700868 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700869 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700870 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
871 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700872 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700873 void holdCall(Call call) {
874 if (!mCalls.contains(call)) {
875 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700876 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700877 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700878 call.hold();
879 }
880 }
881
882 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700883 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700884 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
885 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700886 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700887 void unholdCall(Call call) {
888 if (!mCalls.contains(call)) {
889 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700890 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700891 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700892 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800893 // Only attempt to hold parent calls and not the individual children.
894 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700895 c.hold();
896 }
897 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700898 call.unhold();
899 }
900 }
901
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700902 /** Called by the in-call UI to change the mute state. */
903 void mute(boolean shouldMute) {
904 mCallAudioManager.mute(shouldMute);
905 }
906
907 /**
908 * Called by the in-call UI to change the audio route, for example to change from earpiece to
909 * speaker phone.
910 */
911 void setAudioRoute(int route) {
912 mCallAudioManager.setAudioRoute(route);
913 }
914
Yorke Leed1346872014-07-28 14:38:45 -0700915 /** Called by the in-call UI to turn the proximity sensor on. */
916 void turnOnProximitySensor() {
917 mProximitySensorManager.turnOn();
918 }
919
920 /**
921 * Called by the in-call UI to turn the proximity sensor off.
922 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
923 * the screen will be kept off until the proximity sensor goes negative.
924 */
925 void turnOffProximitySensor(boolean screenOnImmediately) {
926 mProximitySensorManager.turnOff(screenOnImmediately);
927 }
928
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700929 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700930 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700931 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700932 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700933 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -0700934 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -0700935 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
936 // respecting a rewritten number or a canceled number. This is unlikely since
937 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
938 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700939 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700940
941 // Note: emergency calls never go through account selection dialog so they never
942 // arrive here.
943 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700944 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700945 } else {
946 call.disconnect();
947 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700948
949 if (setDefault) {
950 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
951 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700952 }
953 }
954
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700955 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700956 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700957 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700958 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700959 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700960 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700961 }
962
Sailesh Nepale59bb192014-04-01 18:33:59 -0700963 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700964 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -0800965 }
966
Sailesh Nepale59bb192014-04-01 18:33:59 -0700967 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700968 setCallState(call, CallState.DIALING, "dialing set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -0700969 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800970 }
971
Sailesh Nepale59bb192014-04-01 18:33:59 -0700972 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700973 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -0700974 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800975 }
976
Sailesh Nepale59bb192014-04-01 18:33:59 -0700977 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700978 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700979 }
980
Santos Cordon049b7b62014-01-30 05:34:26 -0800981 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700982 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
983 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800984 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800985 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800986 */
Andrew Lee701dc002014-09-11 21:29:12 -0700987 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
988 call.setDisconnectCause(disconnectCause);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700989 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700990 }
991
Ben Gilada0d9f752014-02-26 11:49:03 -0800992 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700993 * Removes an existing disconnected call, and notifies the in-call app.
994 */
995 void markCallAsRemoved(Call call) {
996 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700997 if (mLocallyDisconnectingCalls.contains(call)) {
998 mLocallyDisconnectingCalls.remove(call);
999 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1000 mForegroundCall.unhold();
1001 }
1002 }
Ihab Awada02bef52014-08-13 18:18:42 -07001003 }
1004
1005 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001006 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001007 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001008 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001009 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001010 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001011 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001012 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001013 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001014 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001015 if (call.getState() != CallState.DISCONNECTED) {
1016 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1017 }
1018 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001019 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001020 }
1021 }
1022 }
1023
Santos Cordondeb8c892014-05-30 01:38:03 -07001024 boolean hasAnyCalls() {
1025 return !mCalls.isEmpty();
1026 }
1027
Santos Cordonc7e85d42014-05-22 02:51:48 -07001028 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001029 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001030 }
1031
1032 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001033 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001034 }
1035
Santos Cordondeb8c892014-05-30 01:38:03 -07001036 boolean onMediaButton(int type) {
1037 if (hasAnyCalls()) {
1038 if (HeadsetMediaButton.SHORT_PRESS == type) {
1039 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1040 if (ringingCall == null) {
1041 mCallAudioManager.toggleMute();
1042 return true;
1043 } else {
Andrew Lee38931d02014-07-16 10:17:36 -07001044 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -07001045 return true;
1046 }
1047 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1048 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1049 Call callToHangup = getFirstCallWithState(
1050 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1051 if (callToHangup != null) {
1052 callToHangup.disconnect();
1053 return true;
1054 }
1055 }
1056 }
1057 return false;
1058 }
1059
1060 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001061 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001062 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001063 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -08001064 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1065 return false;
1066 }
1067
Santos Cordon91bd74c2014-11-07 16:10:22 -08001068 int count = 0;
1069 for (Call call : mCalls) {
1070 if (call.isEmergencyCall()) {
1071 // We never support add call if one of the calls is an emergency call.
1072 return false;
Roshan Piusd9544092015-07-31 11:31:52 -07001073 } else if (!call.getChildCalls().isEmpty() && !call.can(Connection.CAPABILITY_HOLD)) {
1074 // This is to deal with CDMA conference calls. CDMA conference calls do not
1075 // allow the addition of another call when it is already in a 3 way conference.
1076 // So, we detect that it is a CDMA conference call by checking if the call has
1077 // some children and it does not support the CAPABILILTY_HOLD
1078 // TODO: This maybe cleaner if the lower layers can explicitly signal to telecom
1079 // about this limitation (b/22880180).
1080 return false;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001081 } else if (call.getParentCall() == null) {
1082 count++;
1083 }
Santos Cordon10838c22014-06-11 17:36:04 -07001084
Santos Cordon91bd74c2014-11-07 16:10:22 -08001085 // We do not check states for canAddCall. We treat disconnected calls the same
1086 // and wait until they are removed instead. If we didn't count disconnected calls,
1087 // we could put InCallServices into a state where they are showing two calls but
1088 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1089 // and acts better if we wait until the call is removed.
1090 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001091 return false;
1092 }
1093 }
1094 return true;
1095 }
1096
Ihab Awada3653902015-01-23 13:44:46 -08001097 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001098 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001099 return getFirstCallWithState(CallState.RINGING);
1100 }
1101
1102 Call getActiveCall() {
1103 return getFirstCallWithState(CallState.ACTIVE);
1104 }
1105
Nancy Chen05a9e402014-09-26 14:14:32 -07001106 Call getDialingCall() {
1107 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001108 }
1109
1110 Call getHeldCall() {
1111 return getFirstCallWithState(CallState.ON_HOLD);
1112 }
1113
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001114 int getNumHeldCalls() {
1115 int count = 0;
1116 for (Call call : mCalls) {
1117 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1118 count++;
1119 }
1120 }
1121 return count;
1122 }
1123
Santos Cordonf193ba42014-09-12 06:37:39 -07001124 Call getFirstCallWithState(int... states) {
1125 return getFirstCallWithState(null, states);
1126 }
1127
Santos Cordon10838c22014-06-11 17:36:04 -07001128 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001129 * Returns the first call that it finds with the given states. The states are treated as having
1130 * priority order so that any call with the first state will be returned before any call with
1131 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001132 *
1133 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001134 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001135 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001136 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001137 // check the foreground first
1138 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1139 return mForegroundCall;
1140 }
1141
Santos Cordondeb8c892014-05-30 01:38:03 -07001142 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001143 if (Objects.equals(callToSkip, call)) {
1144 continue;
1145 }
1146
1147 // Only operate on top-level calls
1148 if (call.getParentCall() != null) {
1149 continue;
1150 }
1151
Santos Cordondeb8c892014-05-30 01:38:03 -07001152 if (currentState == call.getState()) {
1153 return call;
1154 }
1155 }
1156 }
1157 return null;
1158 }
1159
Santos Cordon0fbe6322014-08-14 04:04:25 -07001160 Call createConferenceCall(
1161 PhoneAccountHandle phoneAccount,
1162 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001163
1164 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1165 // which is the default value for new Calls.
1166 long connectTime =
1167 parcelableConference.getConnectTimeMillis() ==
1168 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1169 parcelableConference.getConnectTimeMillis();
1170
Santos Cordon0fbe6322014-08-14 04:04:25 -07001171 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001172 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001173 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001174 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001175 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001176 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001177 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001178 null /* handle */,
1179 null /* gatewayInfo */,
1180 null /* connectionManagerPhoneAccount */,
1181 phoneAccount,
1182 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001183 true /* isConference */,
1184 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001185
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001186 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1187 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001188 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001189 call.setVideoState(parcelableConference.getVideoState());
1190 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001191 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001192 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001193
1194 // TODO: Move this to be a part of addCall()
1195 call.addListener(this);
1196 addCall(call);
1197 return call;
1198 }
1199
Yorke Leef86db2e2014-09-12 17:56:48 -07001200 /**
1201 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1202 */
1203 int getCallState() {
1204 return mPhoneStateBroadcaster.getCallState();
1205 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001206
Santos Cordon4b2c1192014-03-19 18:15:38 -07001207 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001208 * Retrieves the {@link PhoneAccountRegistrar}.
1209 *
1210 * @return The {@link PhoneAccountRegistrar}.
1211 */
1212 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1213 return mPhoneAccountRegistrar;
1214 }
1215
1216 /**
1217 * Retrieves the {@link MissedCallNotifier}
1218 * @return The {@link MissedCallNotifier}.
1219 */
1220 MissedCallNotifier getMissedCallNotifier() {
1221 return mMissedCallNotifier;
1222 }
1223
1224 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001225 * Adds the specified call to the main list of live calls.
1226 *
1227 * @param call The call to add.
1228 */
1229 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001230 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001231 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001232 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001233 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001234
Santos Cordondf399862014-08-06 04:39:15 -07001235 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001236 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001237 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001238 if (Log.SYSTRACE_DEBUG) {
1239 Trace.beginSection(listener.getClass().toString() + " addCall");
1240 }
Santos Cordona56f2762014-03-24 15:55:53 -07001241 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001242 if (Log.SYSTRACE_DEBUG) {
1243 Trace.endSection();
1244 }
Santos Cordona56f2762014-03-24 15:55:53 -07001245 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001246 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001247 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001248 }
1249
Sailesh Nepal810735e2014-03-18 18:15:46 -07001250 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001251 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001252 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001253
Santos Cordon672321e2014-08-21 14:38:58 -07001254 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001255 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001256 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001257
1258 boolean shouldNotify = false;
1259 if (mCalls.contains(call)) {
1260 mCalls.remove(call);
1261 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001262 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001263
Santos Cordon2685dab2015-04-17 17:12:09 -07001264 call.destroy();
1265
Sailesh Nepale59bb192014-04-01 18:33:59 -07001266 // Only broadcast changes for calls that are being tracked.
1267 if (shouldNotify) {
1268 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001269 if (Log.SYSTRACE_DEBUG) {
1270 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1271 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001272 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001273 if (Log.SYSTRACE_DEBUG) {
1274 Trace.endSection();
1275 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001276 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001277 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001278 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001279 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001280 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001281
Sailesh Nepal810735e2014-03-18 18:15:46 -07001282 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001283 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001284 *
1285 * @param call The call.
1286 * @param newState The new state of the call.
1287 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001288 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001289 if (call == null) {
1290 return;
1291 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001292 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001293 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1294 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001295 if (newState != oldState) {
1296 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1297 // 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 -07001298 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001299 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001300 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001301 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001302 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001303 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001304
Yorke Leee4a9c412014-11-14 16:59:42 -08001305 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001306 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001307 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001308 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001309 if (Log.SYSTRACE_DEBUG) {
1310 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1311 }
Santos Cordona56f2762014-03-24 15:55:53 -07001312 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001313 if (Log.SYSTRACE_DEBUG) {
1314 Trace.endSection();
1315 }
Santos Cordona56f2762014-03-24 15:55:53 -07001316 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001317 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001318 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001319 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001320 }
1321 }
1322
1323 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001324 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001325 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001326 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001327 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001328 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001329 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001330 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001331 // of its state will be foreground by default and instead the connection service should
1332 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001333 // the call should play audio and listen to microphone if it wants.
1334
Santos Cordonf193ba42014-09-12 06:37:39 -07001335 // Only top-level calls can be in foreground
1336 if (call.getParentCall() != null) {
1337 continue;
1338 }
1339
Santos Cordon40f78c22014-04-07 02:11:42 -07001340 // Active calls have priority.
1341 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001342 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001343 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001344 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001345
1346 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001347 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001348 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001349 }
1350 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001351
Sailesh Nepal810735e2014-03-18 18:15:46 -07001352 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001353 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001354 Call oldForegroundCall = mForegroundCall;
1355 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001356
Santos Cordona56f2762014-03-24 15:55:53 -07001357 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001358 if (Log.SYSTRACE_DEBUG) {
1359 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1360 }
Santos Cordona56f2762014-03-24 15:55:53 -07001361 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001362 if (Log.SYSTRACE_DEBUG) {
1363 Trace.endSection();
1364 }
Santos Cordona56f2762014-03-24 15:55:53 -07001365 }
Santos Cordon681663d2014-01-30 04:32:15 -08001366 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001367 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001368 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001369
Santos Cordon91bd74c2014-11-07 16:10:22 -08001370 private void updateCanAddCall() {
1371 boolean newCanAddCall = canAddCall();
1372 if (newCanAddCall != mCanAddCall) {
1373 mCanAddCall = newCanAddCall;
1374 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001375 if (Log.SYSTRACE_DEBUG) {
1376 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1377 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001378 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001379 if (Log.SYSTRACE_DEBUG) {
1380 Trace.endSection();
1381 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001382 }
1383 }
1384 }
1385
1386 private void updateCallsManagerState() {
1387 updateForegroundCall();
1388 updateCanAddCall();
1389 }
1390
Yorke Leeb701f6d2014-08-12 14:04:19 -07001391 private boolean isPotentialMMICode(Uri handle) {
1392 return (handle != null && handle.getSchemeSpecificPart() != null
1393 && handle.getSchemeSpecificPart().contains("#"));
1394 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001395
Tyler Gunn5b452df2014-10-01 15:02:58 -07001396 /**
1397 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1398 * MMI codes which can be dialed when one or more calls are in progress.
1399 * <P>
1400 * Checks for numbers formatted similar to the MMI codes defined in:
1401 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1402 * and
1403 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1404 *
1405 * @param handle The URI to call.
1406 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1407 */
1408 private boolean isPotentialInCallMMICode(Uri handle) {
1409 if (handle != null && handle.getSchemeSpecificPart() != null &&
1410 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1411
1412 String dialedNumber = handle.getSchemeSpecificPart();
1413 return (dialedNumber.equals("0") ||
1414 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1415 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1416 dialedNumber.equals("3") ||
1417 dialedNumber.equals("4") ||
1418 dialedNumber.equals("5"));
1419 }
1420 return false;
1421 }
1422
Santos Cordonf193ba42014-09-12 06:37:39 -07001423 private int getNumCallsWithState(int... states) {
1424 int count = 0;
1425 for (int state : states) {
1426 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001427 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001428 count++;
1429 }
1430 }
1431 }
1432 return count;
1433 }
1434
1435 private boolean hasMaximumLiveCalls() {
1436 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1437 }
1438
1439 private boolean hasMaximumHoldingCalls() {
1440 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1441 }
1442
1443 private boolean hasMaximumRingingCalls() {
1444 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1445 }
1446
1447 private boolean hasMaximumOutgoingCalls() {
1448 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1449 }
1450
1451 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1452 if (hasMaximumLiveCalls()) {
1453 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1454 // have to change.
1455 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301456 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1457 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001458
Santos Cordond1766502014-09-26 15:45:39 -07001459 if (call == liveCall) {
1460 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001461 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001462 // state since the call was already populated into the list.
1463 return true;
1464 }
1465
Andrew Leee6595182014-09-23 18:43:05 -07001466 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001467 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1468 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1469 // Disconnect the current outgoing call if it's not an emergency call. If the
1470 // user tries to make two outgoing calls to different emergency call numbers,
1471 // we will try to connect the first outgoing call.
1472 outgoingCall.disconnect();
1473 return true;
1474 }
1475 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1476 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1477 // state, just disconnect it since the user has explicitly started a new call.
1478 outgoingCall.disconnect();
1479 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001480 }
1481 return false;
1482 }
1483
Santos Cordonf193ba42014-09-12 06:37:39 -07001484 if (hasMaximumHoldingCalls()) {
1485 // There is no more room for any more calls, unless it's an emergency.
1486 if (isEmergency) {
1487 // Kill the current active call, this is easier then trying to disconnect a
1488 // holding call and hold an active call.
1489 liveCall.disconnect();
1490 return true;
1491 }
1492 return false; // No more room!
1493 }
1494
1495 // We have room for at least one more holding call at this point.
1496
1497 // First thing, if we are trying to make a call with the same phone account as the live
1498 // call, then allow it so that the connection service can make its own decision about
1499 // how to handle the new call relative to the current one.
1500 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1501 return true;
1502 } else if (call.getTargetPhoneAccount() == null) {
1503 // Without a phone account, we can't say reliably that the call will fail.
1504 // If the user chooses the same phone account as the live call, then it's
1505 // still possible that the call can be made (like with CDMA calls not supporting
1506 // hold but they still support adding a call by going immediately into conference
1507 // mode). Return true here and we'll run this code again after user chooses an
1508 // account.
1509 return true;
1510 }
1511
1512 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001513 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001514 liveCall.hold();
1515 return true;
1516 }
1517
1518 // The live call cannot be held so we're out of luck here. There's no room.
1519 return false;
1520 }
1521 return true;
1522 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001523
1524 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001525 * Checks to see if the call should be on speakerphone and if so, set it.
1526 */
1527 private void maybeMoveToSpeakerPhone(Call call) {
1528 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001529 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001530 call.setStartWithSpeakerphoneOn(false);
1531 }
1532 }
1533
1534 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001535 * Creates a new call for an existing connection.
1536 *
1537 * @param callId The id of the new call.
1538 * @param connection The connection information.
1539 * @return The new call.
1540 */
1541 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1542 Call call = new Call(
1543 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001544 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001545 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001546 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001547 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001548 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001549 connection.getHandle() /* handle */,
1550 null /* gatewayInfo */,
1551 null /* connectionManagerPhoneAccount */,
1552 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1553 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001554 false /* isConference */,
1555 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001556
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001557 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1558 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001559 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001560 call.setCallerDisplayName(connection.getCallerDisplayName(),
1561 connection.getCallerDisplayNamePresentation());
1562
1563 call.addListener(this);
1564 addCall(call);
1565
1566 return call;
1567 }
1568
1569 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001570 * Dumps the state of the {@link CallsManager}.
1571 *
1572 * @param pw The {@code IndentingPrintWriter} to write the state to.
1573 */
1574 public void dump(IndentingPrintWriter pw) {
1575 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001576 if (mCalls != null) {
1577 pw.println("mCalls: ");
1578 pw.increaseIndent();
1579 for (Call call : mCalls) {
1580 pw.println(call);
1581 }
1582 pw.decreaseIndent();
1583 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001584 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1585
1586 if (mCallAudioManager != null) {
1587 pw.println("mCallAudioManager:");
1588 pw.increaseIndent();
1589 mCallAudioManager.dump(pw);
1590 pw.decreaseIndent();
1591 }
1592
1593 if (mTtyManager != null) {
1594 pw.println("mTtyManager:");
1595 pw.increaseIndent();
1596 mTtyManager.dump(pw);
1597 pw.decreaseIndent();
1598 }
1599
1600 if (mInCallController != null) {
1601 pw.println("mInCallController:");
1602 pw.increaseIndent();
1603 mInCallController.dump(pw);
1604 pw.decreaseIndent();
1605 }
1606
1607 if (mConnectionServiceRepository != null) {
1608 pw.println("mConnectionServiceRepository:");
1609 pw.increaseIndent();
1610 mConnectionServiceRepository.dump(pw);
1611 pw.decreaseIndent();
1612 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001613 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001614}