blob: 7a910598d97a489c35ceda8eedc7492d4f210f5b [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;
Roshan Pius4de4a892015-08-24 11:06:58 -070091 private static final int MAXIMUM_DIALING_CALLS = 1;
Santos Cordonf193ba42014-09-12 06:37:39 -070092 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080093 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070094
95 private static final int[] OUTGOING_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -070096 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -070097
Santos Cordon91bd74c2014-11-07 16:10:22 -080098 private static final int[] LIVE_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -070099 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING, CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -0800100
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700102 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
103 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800104 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700105 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
106 * load factor before resizing, 1 means we only expect a single thread to
107 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800108 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700109 private final Set<Call> mCalls = Collections.newSetFromMap(
110 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800111
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700112 private final ConnectionServiceRepository mConnectionServiceRepository;
113 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
114 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700115 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800116 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700117 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800118 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700119 // For this set initial table size to 16 because we add 13 listeners in
120 // the CallsManager constructor.
121 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
122 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700123 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700124 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800125 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700126 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700127 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700128 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700129 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700130 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700131 private final TelecomSystem.SyncRoot mLock;
132 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700133 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700134 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
135 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700136 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800137 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
138 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700139 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700140
Santos Cordon91bd74c2014-11-07 16:10:22 -0800141 private boolean mCanAddCall = true;
142
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700143 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700144 * The call the user is currently interacting with. This is the call that should have audio
145 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800146 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700147 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800148
Nancy Chena469f762014-12-09 18:29:20 -0800149 private Runnable mStopTone;
150
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700151 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700152 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800153 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700154 CallsManager(
155 Context context,
156 TelecomSystem.SyncRoot lock,
157 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700158 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700159 MissedCallNotifier missedCallNotifier,
160 PhoneAccountRegistrar phoneAccountRegistrar,
161 HeadsetMediaButtonFactory headsetMediaButtonFactory,
162 ProximitySensorManagerFactory proximitySensorManagerFactory,
163 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700164 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700165 mLock = lock;
166 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700167 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700168 mPhoneAccountRegistrar = phoneAccountRegistrar;
169 mMissedCallNotifier = missedCallNotifier;
170 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
171 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800172 mDockManager = new DockManager(context);
173 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700174 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700175 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700176 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700177 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700178 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800179 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
180 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700181 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700182 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700183 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800184 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700185 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800186 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700187
Santos Cordondeb8c892014-05-30 01:38:03 -0700188 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700189 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700190 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700191 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700192 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700193 mListeners.add(new RingbackPlayer(this, playerFactory));
194 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700195 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700196 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700197 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700198 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700199 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700200
Ihab Awadabcbce42015-04-07 14:04:01 -0700201 mMissedCallNotifier.updateOnStartup(
202 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800203 }
204
Ihab Awad8de76912015-02-17 12:25:52 -0800205 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
206 if (mRespondViaSmsManager != null) {
207 mListeners.remove(mRespondViaSmsManager);
208 }
209 mRespondViaSmsManager = respondViaSmsManager;
210 mListeners.add(respondViaSmsManager);
211 }
212
213 public RespondViaSmsManager getRespondViaSmsManager() {
214 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800215 }
216
Santos Cordon766d04f2014-05-06 10:28:25 -0700217 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700218 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700219 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700220
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700221 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700222 if (!mCalls.contains(call)) {
223 // Call was not added previously in startOutgoingCall due to it being a potential MMI
224 // code, so add it now.
225 addCall(call);
226 }
227
228 // The call's ConnectionService has been updated.
229 for (CallsManagerListener listener : mListeners) {
230 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700231 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700232
233 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700234 }
235
236 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700237 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700238 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700239
Andrew Leee6288a72014-10-01 13:50:02 -0700240 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700241 }
242
243 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700244 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700245 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700246 setCallState(incomingCall, CallState.RINGING, "successful incoming call");
Santos Cordonf193ba42014-09-12 06:37:39 -0700247
Roshan Pius4de4a892015-08-24 11:06:58 -0700248 if (hasMaximumRingingCalls() || hasMaximumDialingCalls()) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700249 incomingCall.reject(false, null);
250 // since the call was not added to the list of calls, we have to call the missed
251 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700252 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700253 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
254 } else {
255 addCall(incomingCall);
256 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700257 }
258
259 @Override
260 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700261 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700262 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800263 }
264
Ihab Awadcb387ac2014-05-28 16:49:38 -0700265 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700266 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700267 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700268 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
269 addCall(call);
270 }
271
272 @Override
273 public void onFailedUnknownCall(Call call) {
274 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700275 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700276 call.removeListener(this);
277 }
278
279 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700280 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700281 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700282 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700283 }
284 }
285
Evan Charlton352105c2014-06-03 14:10:54 -0700286 @Override
287 public void onPostDialWait(Call call, String remaining) {
288 mInCallController.onPostDialWait(call, remaining);
289 }
290
Santos Cordona1610702014-06-04 20:22:56 -0700291 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800292 public void onPostDialChar(final Call call, char nextChar) {
293 if (PhoneNumberUtils.is12Key(nextChar)) {
294 // Play tone if it is one of the dialpad digits, canceling out the previously queued
295 // up stopTone runnable since playing a new tone automatically stops the previous tone.
296 if (mStopTone != null) {
297 mHandler.removeCallbacks(mStopTone);
298 }
299
300 mDtmfLocalTonePlayer.playTone(call, nextChar);
301
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700302 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800303 mStopTone = new Runnable() {
304 @Override
305 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700306 synchronized (mLock) {
307 // Set a timeout to stop the tone in case there isn't another tone to follow.
308 mDtmfLocalTonePlayer.stopTone(call);
309 }
Nancy Chena469f762014-12-09 18:29:20 -0800310 }
311 };
312 mHandler.postDelayed(
313 mStopTone,
314 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
315 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
316 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
317 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
318 // the previous tone is being stopped anyway.
319 if (mStopTone != null) {
320 mHandler.removeCallbacks(mStopTone);
321 }
322 mDtmfLocalTonePlayer.stopTone(call);
323 } else {
324 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
325 }
326 }
327
328 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700329 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700330 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800331 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700332 for (CallsManagerListener listener : mListeners) {
333 listener.onIsConferencedChanged(call);
334 }
335 }
336
337 @Override
338 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700339 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800340 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700341 for (CallsManagerListener listener : mListeners) {
342 listener.onIsConferencedChanged(call);
343 }
344 }
345
Ihab Awadff7493a2014-06-10 13:47:44 -0700346 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700347 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700348 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700349 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700350 }
351 }
352
Andrew Lee4a796602014-07-11 17:23:03 -0700353 @Override
354 public void onVideoStateChanged(Call call) {
355 for (CallsManagerListener listener : mListeners) {
356 listener.onVideoStateChanged(call);
357 }
358 }
359
Santos Cordoncf5b2912014-11-05 21:57:54 -0800360 @Override
361 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
362 mPendingCallsToDisconnect.add(call);
363 mHandler.postDelayed(new Runnable() {
364 @Override
365 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700366 synchronized (mLock) {
367 if (mPendingCallsToDisconnect.remove(call)) {
368 Log.i(this, "Delayed disconnection of call: %s", call);
369 call.disconnect();
370 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800371 }
372 }
373 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
374
375 return true;
376 }
377
Tyler Gunn86014fc2015-06-12 14:31:25 -0700378 /**
379 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
380 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
381 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
382 * respond to callbacks from the {@link VideoProviderProxy}.
383 *
384 * @param call The call.
385 */
386 @Override
387 public void onVideoCallProviderChanged(Call call) {
388 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
389
390 if (videoProviderProxy == null) {
391 return;
392 }
393
394 videoProviderProxy.addListener(this);
395 }
396
397 /**
398 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
399 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
400 * modification request.
401 *
402 * @param call The call.
403 * @param videoProfile The {@link VideoProfile}.
404 */
405 @Override
406 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
407 int videoState = videoProfile != null ? videoProfile.getVideoState() :
408 VideoProfile.STATE_AUDIO_ONLY;
409 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
410 .videoStateToString(videoState));
411
412 for (CallsManagerListener listener : mListeners) {
413 listener.onSessionModifyRequestReceived(call, videoProfile);
414 }
415 }
416
Santos Cordon4bc02452014-11-19 15:51:12 -0800417 Collection<Call> getCalls() {
418 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700419 }
420
421 Call getForegroundCall() {
422 return mForegroundCall;
423 }
424
Santos Cordonae193062014-05-21 21:21:49 -0700425 Ringer getRinger() {
426 return mRinger;
427 }
428
Santos Cordonf3671a62014-05-29 21:51:53 -0700429 InCallController getInCallController() {
430 return mInCallController;
431 }
432
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700433 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700434 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700435 if (call.isEmergencyCall()) {
436 return true;
437 }
438 }
439 return false;
440 }
441
Andrew Lee45506232014-10-22 17:54:33 -0700442 boolean hasVideoCall() {
443 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700444 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700445 return true;
446 }
447 }
448 return false;
449 }
450
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700451 CallAudioState getAudioState() {
452 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700453 }
454
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700455 boolean isTtySupported() {
456 return mTtyManager.isTtySupported();
457 }
458
459 int getCurrentTtyMode() {
460 return mTtyManager.getCurrentTtyMode();
461 }
462
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700463 void addListener(CallsManagerListener listener) {
464 mListeners.add(listener);
465 }
466
467 void removeListener(CallsManagerListener listener) {
468 mListeners.remove(listener);
469 }
470
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800471 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700472 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800473 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700474 * @param phoneAccountHandle The phone account which contains the component name of the
475 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800476 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800477 */
Evan Charlton89176372014-07-19 18:23:09 -0700478 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800479 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700480 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
481 if (handle == null) {
482 // Required for backwards compatibility
483 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
484 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700485 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700486 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800487 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700488 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700489 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700490 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700491 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700492 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700493 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700494 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700495 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700496 true /* isIncoming */,
497 false /* isConference */);
498
Andrew Lee1b906652015-06-01 11:44:55 -0700499 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700500 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700501 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700502 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800503 }
504
Yorke Lee9250e5f2014-10-01 13:39:09 -0700505 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
506 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
507 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
508 Call call = new Call(
509 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800510 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700511 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700512 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700513 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700514 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700515 handle,
516 null /* gatewayInfo */,
517 null /* connectionManagerPhoneAccount */,
518 phoneAccountHandle,
519 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
520 // to the existing connection instead of trying to create a new one.
521 true /* isIncoming */,
522 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700523 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700524 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700525 call.addListener(this);
526 call.startCreateConnection(mPhoneAccountRegistrar);
527 }
528
Yorke Lee8d3f2692015-05-08 11:44:39 -0700529 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
530 if (handle1 == null || handle2 == null) {
531 return handle1 == handle2;
532 }
533
534 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
535 return false;
536 }
537
538 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
539 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
540 return TextUtils.equals(number1, number2);
541 }
542
Santos Cordoncf5b2912014-11-05 21:57:54 -0800543 private Call getNewOutgoingCall(Uri handle) {
544 // First check to see if we can reuse any of the calls that are waiting to disconnect.
545 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800546 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800547 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700548 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800549 mPendingCallsToDisconnect.remove(pendingCall);
550 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800551 reusedCall = pendingCall;
552 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700553 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800554 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800555 }
556 }
Yorke Lee59979512014-12-02 01:03:40 -0800557 if (reusedCall != null) {
558 return reusedCall;
559 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800560
561 // Create a call with original handle. The handle may be changed when the call is attached
562 // to a connection service, but in most cases will remain the same.
563 return new Call(
564 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800565 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700566 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800567 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700568 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700569 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800570 handle,
571 null /* gatewayInfo */,
572 null /* connectionManagerPhoneAccount */,
573 null /* phoneAccountHandle */,
574 false /* isIncoming */,
575 false /* isConference */);
576 }
577
Nancy Chen0d3076c2014-07-30 14:45:44 -0700578 /**
579 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
580 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700581 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700582 * @param phoneAccountHandle The phone account which contains the component name of the
583 * connection service to use for this call.
584 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700585 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700586 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800587 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700588
589 List<PhoneAccountHandle> accounts =
Santos Cordonea5cb932015-05-07 16:28:38 -0700590 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme(), false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700591
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800592 Log.v(this, "startOutgoingCall found accounts = " + accounts);
593
Roshan Pius1a373832015-08-07 11:39:05 -0700594 if (mForegroundCall != null) {
595 Call ongoingCall = mForegroundCall;
Nancy Chenbc95db92014-11-24 13:19:18 -0800596 // If there is an ongoing call, use the same phone account to place this new call.
Roshan Pius1a373832015-08-07 11:39:05 -0700597 // If the ongoing call is a conference call, we fetch the phone account from the
598 // child calls because we don't have targetPhoneAccount set on Conference calls.
599 // TODO: Set targetPhoneAccount for all conference calls (b/23035408).
600 if (ongoingCall.getTargetPhoneAccount() == null &&
601 !ongoingCall.getChildCalls().isEmpty()) {
602 ongoingCall = ongoingCall.getChildCalls().get(0);
603 }
604 if (ongoingCall.getTargetPhoneAccount() != null) {
605 phoneAccountHandle = ongoingCall.getTargetPhoneAccount();
606 }
Nancy Chenbc95db92014-11-24 13:19:18 -0800607 }
608
Nancy Chenbc9ef172014-08-15 17:11:57 -0700609 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
610 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700611 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700612 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700613 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700614 phoneAccountHandle = null;
615 }
616 }
617
618 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700619 // No preset account, check if default exists that supports the URI scheme for the
620 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700621 phoneAccountHandle =
622 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700623 }
624
Nancy Chen1c5926f2014-09-17 14:44:14 -0700625 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700626
Tyler Gunn5b452df2014-10-01 15:02:58 -0700627 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700628
629 // Do not support any more live calls. Our options are to move a call to hold, disconnect
630 // a call, or cancel this call altogether.
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700631 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, call.isEmergencyCall())) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700632 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800633 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800634 if (mCalls.contains(call)) {
635 // This call can already exist if it is a reused call,
636 // See {@link #getNewOutgoingCall}.
637 call.disconnect();
638 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700639 return null;
640 }
641
Nancy Chen8d92f452014-10-20 21:43:44 -0700642 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700643 !call.isEmergencyCall();
Nancy Chen8d92f452014-10-20 21:43:44 -0700644
645 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700646 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700647 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700648 // Create our own instance to modify (since extras may be Bundle.EMPTY)
649 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700650 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700651 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700652 call.setState(
653 CallState.CONNECTING,
654 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700655 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700656
Andrew Lee1b906652015-06-01 11:44:55 -0700657 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700658
Tyler Gunn504eb832014-10-13 14:15:27 -0700659 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700660 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700661 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800662 } else if (!mCalls.contains(call)) {
663 // We check if mCalls already contains the call because we could potentially be reusing
664 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700665 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700666 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700667
668 return call;
669 }
670
Ben Gilada0d9f752014-02-26 11:49:03 -0800671 /**
Yorke Lee33501632014-03-17 19:24:12 -0700672 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800673 *
Yorke Lee33501632014-03-17 19:24:12 -0700674 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700675 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700676 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700677 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700678 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800679 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700680 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
681 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700682 if (call == null) {
683 // don't do anything if the call no longer exists
684 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700685 return;
686 }
687
Nancy Chen201b4372014-09-08 14:18:24 -0700688 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700689
690 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700691 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700692 } else {
693 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
694 Log.pii(uriHandle), Log.pii(handle));
695 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700696
Nancy Chen0d3076c2014-07-30 14:45:44 -0700697 call.setHandle(uriHandle);
698 call.setGatewayInfo(gatewayInfo);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700699 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700700
Santos Cordona04bdca2015-03-04 16:57:55 -0800701 if (speakerphoneOn) {
702 Log.i(this, "%s Starting with speakerphone as requested", call);
703 } else {
704 Log.i(this, "%s Starting with speakerphone because car is docked.", call);
705 }
706 call.setStartWithSpeakerphoneOn(speakerphoneOn || mDockManager.isDocked());
707
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700708 if (call.isEmergencyCall()) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700709 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700710 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700711 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700712
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700713 if (call.getTargetPhoneAccount() != null || call.isEmergencyCall()) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700714 // If the account has been set, proceed to place the outgoing call.
715 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700716 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700717 }
Yorke Leef98fb572014-03-05 10:56:55 -0800718 }
719
720 /**
Santos Cordona1610702014-06-04 20:22:56 -0700721 * Attempts to start a conference call for the specified call.
722 *
Santos Cordon12d61822014-07-29 16:02:20 -0700723 * @param call The call to conference.
724 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700725 */
Santos Cordon12d61822014-07-29 16:02:20 -0700726 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700727 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700728 }
729
730 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700731 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
732 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800733 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700734 *
735 * @param call The call to answer.
736 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800737 */
Andrew Lee38931d02014-07-16 10:17:36 -0700738 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700739 if (!mCalls.contains(call)) {
740 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800741 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700742 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700743 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700744 if (mForegroundCall != null && mForegroundCall != call &&
745 (mForegroundCall.isActive() ||
746 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800747 if (0 == (mForegroundCall.getConnectionCapabilities()
748 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700749 // This call does not support hold. If it is from a different connection
750 // service, then disconnect it, otherwise allow the connection service to
751 // figure out the right states.
752 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
753 mForegroundCall.disconnect();
754 }
755 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800756 Call heldCall = getHeldCall();
757 if (heldCall != null) {
758 Log.v(this, "Disconnecting held call %s before holding active call.",
759 heldCall);
760 heldCall.disconnect();
761 }
762
Santos Cordonf193ba42014-09-12 06:37:39 -0700763 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
764 mForegroundCall, call);
765 mForegroundCall.hold();
766 }
Santos Cordondf399862014-08-06 04:39:15 -0700767 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700768 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700769 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700770 }
771
Santos Cordona56f2762014-03-24 15:55:53 -0700772 for (CallsManagerListener listener : mListeners) {
773 listener.onIncomingCallAnswered(call);
774 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800775
Santos Cordon61d0f702014-02-19 02:52:23 -0800776 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700777 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700778 call.answer(videoState);
Tyler Gunn5b882492015-06-03 10:03:13 -0700779 if (VideoProfile.isVideo(videoState) &&
Rekha Kumard240fe82015-04-01 21:45:57 -0700780 !mWiredHeadsetManager.isPluggedIn() &&
781 !mCallAudioManager.isBluetoothDeviceAvailable() &&
782 isSpeakerEnabledForVideoCalls()) {
783 call.setStartWithSpeakerphoneOn(true);
784 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800785 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800786 }
787
Rekha Kumard240fe82015-04-01 21:45:57 -0700788 private static boolean isSpeakerEnabledForVideoCalls() {
789 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
790 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
791 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
792 }
793
Santos Cordone3d76ab2014-01-28 17:25:20 -0800794 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700795 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
796 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800797 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800798 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700799 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700800 if (!mCalls.contains(call)) {
801 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800802 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700803 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700804 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700805 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700806 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800807 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800808 }
809
810 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700811 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700812 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700813 * @param digit The DTMF digit to play.
814 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700815 void playDtmfTone(Call call, char digit) {
816 if (!mCalls.contains(call)) {
817 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700818 } else {
819 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700820 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700821 }
822 }
823
824 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700825 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700826 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700827 void stopDtmfTone(Call call) {
828 if (!mCalls.contains(call)) {
829 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700830 } else {
831 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700832 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700833 }
834 }
835
836 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700837 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700838 */
Evan Charlton352105c2014-06-03 14:10:54 -0700839 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700840 if (!mCalls.contains(call)) {
841 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700842 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700843 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700844 }
845 }
846
847 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700848 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800849 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
850 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800851 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700852 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700853 Log.v(this, "disconnectCall %s", call);
854
Sailesh Nepale59bb192014-04-01 18:33:59 -0700855 if (!mCalls.contains(call)) {
856 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800857 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700858 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800859 call.disconnect();
860 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800861 }
Santos Cordon681663d2014-01-30 04:32:15 -0800862
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700863 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700864 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700865 */
866 void disconnectAllCalls() {
867 Log.v(this, "disconnectAllCalls");
868
869 for (Call call : mCalls) {
870 disconnectCall(call);
871 }
872 }
873
Nancy Chenbc9ef172014-08-15 17:11:57 -0700874
Tyler Gunn61b92102014-08-19 07:42:20 -0700875 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700876 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700877 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
878 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700879 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700880 void holdCall(Call call) {
881 if (!mCalls.contains(call)) {
882 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700883 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700884 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700885 call.hold();
886 }
887 }
888
889 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700890 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700891 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
892 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700893 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700894 void unholdCall(Call call) {
895 if (!mCalls.contains(call)) {
896 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700897 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700898 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700899 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800900 // Only attempt to hold parent calls and not the individual children.
901 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700902 c.hold();
903 }
904 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700905 call.unhold();
906 }
907 }
908
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700909 /** Called by the in-call UI to change the mute state. */
910 void mute(boolean shouldMute) {
911 mCallAudioManager.mute(shouldMute);
912 }
913
914 /**
915 * Called by the in-call UI to change the audio route, for example to change from earpiece to
916 * speaker phone.
917 */
918 void setAudioRoute(int route) {
919 mCallAudioManager.setAudioRoute(route);
920 }
921
Yorke Leed1346872014-07-28 14:38:45 -0700922 /** Called by the in-call UI to turn the proximity sensor on. */
923 void turnOnProximitySensor() {
924 mProximitySensorManager.turnOn();
925 }
926
927 /**
928 * Called by the in-call UI to turn the proximity sensor off.
929 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
930 * the screen will be kept off until the proximity sensor goes negative.
931 */
932 void turnOffProximitySensor(boolean screenOnImmediately) {
933 mProximitySensorManager.turnOff(screenOnImmediately);
934 }
935
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700936 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700937 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700938 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700939 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700940 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -0700941 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -0700942 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
943 // respecting a rewritten number or a canceled number. This is unlikely since
944 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
945 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700946 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700947
948 // Note: emergency calls never go through account selection dialog so they never
949 // arrive here.
950 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700951 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700952 } else {
953 call.disconnect();
954 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700955
956 if (setDefault) {
957 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
958 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700959 }
960 }
961
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700962 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700963 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700964 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700965 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700966 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700967 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700968 }
969
Sailesh Nepale59bb192014-04-01 18:33:59 -0700970 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700971 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -0800972 }
973
Sailesh Nepale59bb192014-04-01 18:33:59 -0700974 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700975 setCallState(call, CallState.DIALING, "dialing set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -0700976 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800977 }
978
Sailesh Nepale59bb192014-04-01 18:33:59 -0700979 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700980 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -0700981 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800982 }
983
Sailesh Nepale59bb192014-04-01 18:33:59 -0700984 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700985 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700986 }
987
Santos Cordon049b7b62014-01-30 05:34:26 -0800988 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700989 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
990 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800991 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800992 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800993 */
Andrew Lee701dc002014-09-11 21:29:12 -0700994 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
995 call.setDisconnectCause(disconnectCause);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700996 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700997 }
998
Ben Gilada0d9f752014-02-26 11:49:03 -0800999 /**
Ihab Awada02bef52014-08-13 18:18:42 -07001000 * Removes an existing disconnected call, and notifies the in-call app.
1001 */
1002 void markCallAsRemoved(Call call) {
1003 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -07001004 if (mLocallyDisconnectingCalls.contains(call)) {
1005 mLocallyDisconnectingCalls.remove(call);
1006 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1007 mForegroundCall.unhold();
1008 }
1009 }
Ihab Awada02bef52014-08-13 18:18:42 -07001010 }
1011
1012 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001013 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001014 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001015 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001016 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001017 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001018 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001019 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001020 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001021 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001022 if (call.getState() != CallState.DISCONNECTED) {
1023 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1024 }
1025 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001026 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001027 }
1028 }
1029 }
1030
Santos Cordondeb8c892014-05-30 01:38:03 -07001031 boolean hasAnyCalls() {
1032 return !mCalls.isEmpty();
1033 }
1034
Santos Cordonc7e85d42014-05-22 02:51:48 -07001035 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001036 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001037 }
1038
1039 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001040 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001041 }
1042
Santos Cordondeb8c892014-05-30 01:38:03 -07001043 boolean onMediaButton(int type) {
1044 if (hasAnyCalls()) {
1045 if (HeadsetMediaButton.SHORT_PRESS == type) {
1046 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1047 if (ringingCall == null) {
1048 mCallAudioManager.toggleMute();
1049 return true;
1050 } else {
Andrew Lee38931d02014-07-16 10:17:36 -07001051 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -07001052 return true;
1053 }
1054 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1055 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1056 Call callToHangup = getFirstCallWithState(
1057 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1058 if (callToHangup != null) {
1059 callToHangup.disconnect();
1060 return true;
1061 }
1062 }
1063 }
1064 return false;
1065 }
1066
1067 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001068 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001069 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001070 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -08001071 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1072 return false;
1073 }
1074
Santos Cordon91bd74c2014-11-07 16:10:22 -08001075 int count = 0;
1076 for (Call call : mCalls) {
1077 if (call.isEmergencyCall()) {
1078 // We never support add call if one of the calls is an emergency call.
1079 return false;
Roshan Piusd9544092015-07-31 11:31:52 -07001080 } else if (!call.getChildCalls().isEmpty() && !call.can(Connection.CAPABILITY_HOLD)) {
1081 // This is to deal with CDMA conference calls. CDMA conference calls do not
1082 // allow the addition of another call when it is already in a 3 way conference.
1083 // So, we detect that it is a CDMA conference call by checking if the call has
1084 // some children and it does not support the CAPABILILTY_HOLD
1085 // TODO: This maybe cleaner if the lower layers can explicitly signal to telecom
1086 // about this limitation (b/22880180).
1087 return false;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001088 } else if (call.getParentCall() == null) {
1089 count++;
1090 }
Santos Cordon10838c22014-06-11 17:36:04 -07001091
Santos Cordon91bd74c2014-11-07 16:10:22 -08001092 // We do not check states for canAddCall. We treat disconnected calls the same
1093 // and wait until they are removed instead. If we didn't count disconnected calls,
1094 // we could put InCallServices into a state where they are showing two calls but
1095 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1096 // and acts better if we wait until the call is removed.
1097 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001098 return false;
1099 }
1100 }
1101 return true;
1102 }
1103
Ihab Awada3653902015-01-23 13:44:46 -08001104 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001105 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001106 return getFirstCallWithState(CallState.RINGING);
1107 }
1108
1109 Call getActiveCall() {
1110 return getFirstCallWithState(CallState.ACTIVE);
1111 }
1112
Nancy Chen05a9e402014-09-26 14:14:32 -07001113 Call getDialingCall() {
1114 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001115 }
1116
1117 Call getHeldCall() {
1118 return getFirstCallWithState(CallState.ON_HOLD);
1119 }
1120
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001121 int getNumHeldCalls() {
1122 int count = 0;
1123 for (Call call : mCalls) {
1124 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1125 count++;
1126 }
1127 }
1128 return count;
1129 }
1130
Santos Cordonf193ba42014-09-12 06:37:39 -07001131 Call getFirstCallWithState(int... states) {
1132 return getFirstCallWithState(null, states);
1133 }
1134
Santos Cordon10838c22014-06-11 17:36:04 -07001135 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001136 * Returns the first call that it finds with the given states. The states are treated as having
1137 * priority order so that any call with the first state will be returned before any call with
1138 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001139 *
1140 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001141 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001142 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001143 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001144 // check the foreground first
1145 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1146 return mForegroundCall;
1147 }
1148
Santos Cordondeb8c892014-05-30 01:38:03 -07001149 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001150 if (Objects.equals(callToSkip, call)) {
1151 continue;
1152 }
1153
1154 // Only operate on top-level calls
1155 if (call.getParentCall() != null) {
1156 continue;
1157 }
1158
Santos Cordondeb8c892014-05-30 01:38:03 -07001159 if (currentState == call.getState()) {
1160 return call;
1161 }
1162 }
1163 }
1164 return null;
1165 }
1166
Santos Cordon0fbe6322014-08-14 04:04:25 -07001167 Call createConferenceCall(
1168 PhoneAccountHandle phoneAccount,
1169 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001170
1171 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1172 // which is the default value for new Calls.
1173 long connectTime =
1174 parcelableConference.getConnectTimeMillis() ==
1175 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1176 parcelableConference.getConnectTimeMillis();
1177
Santos Cordon0fbe6322014-08-14 04:04:25 -07001178 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001179 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001180 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001181 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001182 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001183 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001184 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001185 null /* handle */,
1186 null /* gatewayInfo */,
1187 null /* connectionManagerPhoneAccount */,
1188 phoneAccount,
1189 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001190 true /* isConference */,
1191 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001192
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001193 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1194 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001195 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001196 call.setVideoState(parcelableConference.getVideoState());
1197 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001198 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001199 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001200
1201 // TODO: Move this to be a part of addCall()
1202 call.addListener(this);
1203 addCall(call);
1204 return call;
1205 }
1206
Yorke Leef86db2e2014-09-12 17:56:48 -07001207 /**
1208 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1209 */
1210 int getCallState() {
1211 return mPhoneStateBroadcaster.getCallState();
1212 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001213
Santos Cordon4b2c1192014-03-19 18:15:38 -07001214 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001215 * Retrieves the {@link PhoneAccountRegistrar}.
1216 *
1217 * @return The {@link PhoneAccountRegistrar}.
1218 */
1219 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1220 return mPhoneAccountRegistrar;
1221 }
1222
1223 /**
1224 * Retrieves the {@link MissedCallNotifier}
1225 * @return The {@link MissedCallNotifier}.
1226 */
1227 MissedCallNotifier getMissedCallNotifier() {
1228 return mMissedCallNotifier;
1229 }
1230
1231 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001232 * Adds the specified call to the main list of live calls.
1233 *
1234 * @param call The call to add.
1235 */
1236 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001237 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001238 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001239 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001240 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001241
Santos Cordondf399862014-08-06 04:39:15 -07001242 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001243 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001244 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001245 if (Log.SYSTRACE_DEBUG) {
1246 Trace.beginSection(listener.getClass().toString() + " addCall");
1247 }
Santos Cordona56f2762014-03-24 15:55:53 -07001248 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001249 if (Log.SYSTRACE_DEBUG) {
1250 Trace.endSection();
1251 }
Santos Cordona56f2762014-03-24 15:55:53 -07001252 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001253 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001254 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001255 }
1256
Sailesh Nepal810735e2014-03-18 18:15:46 -07001257 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001258 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001259 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001260
Santos Cordon672321e2014-08-21 14:38:58 -07001261 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001262 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001263 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001264
1265 boolean shouldNotify = false;
1266 if (mCalls.contains(call)) {
1267 mCalls.remove(call);
1268 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001269 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001270
Santos Cordon2685dab2015-04-17 17:12:09 -07001271 call.destroy();
1272
Sailesh Nepale59bb192014-04-01 18:33:59 -07001273 // Only broadcast changes for calls that are being tracked.
1274 if (shouldNotify) {
1275 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001276 if (Log.SYSTRACE_DEBUG) {
1277 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1278 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001279 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001280 if (Log.SYSTRACE_DEBUG) {
1281 Trace.endSection();
1282 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001283 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001284 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001285 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001286 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001287 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001288
Sailesh Nepal810735e2014-03-18 18:15:46 -07001289 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001290 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001291 *
1292 * @param call The call.
1293 * @param newState The new state of the call.
1294 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001295 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001296 if (call == null) {
1297 return;
1298 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001299 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001300 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1301 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001302 if (newState != oldState) {
1303 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1304 // 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 -07001305 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001306 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001307 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001308 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001309 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001310 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001311
Yorke Leee4a9c412014-11-14 16:59:42 -08001312 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001313 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001314 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001315 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001316 if (Log.SYSTRACE_DEBUG) {
1317 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1318 }
Santos Cordona56f2762014-03-24 15:55:53 -07001319 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001320 if (Log.SYSTRACE_DEBUG) {
1321 Trace.endSection();
1322 }
Santos Cordona56f2762014-03-24 15:55:53 -07001323 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001324 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001325 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001326 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001327 }
1328 }
1329
1330 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001331 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001332 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001333 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001334 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001335 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001336 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001337 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001338 // of its state will be foreground by default and instead the connection service should
1339 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001340 // the call should play audio and listen to microphone if it wants.
1341
Santos Cordonf193ba42014-09-12 06:37:39 -07001342 // Only top-level calls can be in foreground
1343 if (call.getParentCall() != null) {
1344 continue;
1345 }
1346
Santos Cordon40f78c22014-04-07 02:11:42 -07001347 // Active calls have priority.
1348 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001349 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001350 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001351 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001352
1353 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001354 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001355 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001356 }
1357 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001358
Sailesh Nepal810735e2014-03-18 18:15:46 -07001359 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001360 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001361 Call oldForegroundCall = mForegroundCall;
1362 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001363
Santos Cordona56f2762014-03-24 15:55:53 -07001364 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001365 if (Log.SYSTRACE_DEBUG) {
1366 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1367 }
Santos Cordona56f2762014-03-24 15:55:53 -07001368 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001369 if (Log.SYSTRACE_DEBUG) {
1370 Trace.endSection();
1371 }
Santos Cordona56f2762014-03-24 15:55:53 -07001372 }
Santos Cordon681663d2014-01-30 04:32:15 -08001373 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001374 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001375 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001376
Santos Cordon91bd74c2014-11-07 16:10:22 -08001377 private void updateCanAddCall() {
1378 boolean newCanAddCall = canAddCall();
1379 if (newCanAddCall != mCanAddCall) {
1380 mCanAddCall = newCanAddCall;
1381 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001382 if (Log.SYSTRACE_DEBUG) {
1383 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1384 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001385 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001386 if (Log.SYSTRACE_DEBUG) {
1387 Trace.endSection();
1388 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001389 }
1390 }
1391 }
1392
1393 private void updateCallsManagerState() {
1394 updateForegroundCall();
1395 updateCanAddCall();
1396 }
1397
Yorke Leeb701f6d2014-08-12 14:04:19 -07001398 private boolean isPotentialMMICode(Uri handle) {
1399 return (handle != null && handle.getSchemeSpecificPart() != null
1400 && handle.getSchemeSpecificPart().contains("#"));
1401 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001402
Tyler Gunn5b452df2014-10-01 15:02:58 -07001403 /**
1404 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1405 * MMI codes which can be dialed when one or more calls are in progress.
1406 * <P>
1407 * Checks for numbers formatted similar to the MMI codes defined in:
1408 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1409 * and
1410 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1411 *
1412 * @param handle The URI to call.
1413 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1414 */
1415 private boolean isPotentialInCallMMICode(Uri handle) {
1416 if (handle != null && handle.getSchemeSpecificPart() != null &&
1417 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1418
1419 String dialedNumber = handle.getSchemeSpecificPart();
1420 return (dialedNumber.equals("0") ||
1421 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1422 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1423 dialedNumber.equals("3") ||
1424 dialedNumber.equals("4") ||
1425 dialedNumber.equals("5"));
1426 }
1427 return false;
1428 }
1429
Santos Cordonf193ba42014-09-12 06:37:39 -07001430 private int getNumCallsWithState(int... states) {
1431 int count = 0;
1432 for (int state : states) {
1433 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001434 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001435 count++;
1436 }
1437 }
1438 }
1439 return count;
1440 }
1441
1442 private boolean hasMaximumLiveCalls() {
1443 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1444 }
1445
1446 private boolean hasMaximumHoldingCalls() {
1447 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1448 }
1449
1450 private boolean hasMaximumRingingCalls() {
1451 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1452 }
1453
1454 private boolean hasMaximumOutgoingCalls() {
1455 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1456 }
1457
Roshan Pius4de4a892015-08-24 11:06:58 -07001458 private boolean hasMaximumDialingCalls() {
1459 return MAXIMUM_DIALING_CALLS <= getNumCallsWithState(CallState.DIALING);
1460 }
1461
Santos Cordonf193ba42014-09-12 06:37:39 -07001462 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1463 if (hasMaximumLiveCalls()) {
1464 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1465 // have to change.
1466 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301467 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1468 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001469
Santos Cordond1766502014-09-26 15:45:39 -07001470 if (call == liveCall) {
1471 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001472 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001473 // state since the call was already populated into the list.
1474 return true;
1475 }
1476
Andrew Leee6595182014-09-23 18:43:05 -07001477 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001478 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1479 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1480 // Disconnect the current outgoing call if it's not an emergency call. If the
1481 // user tries to make two outgoing calls to different emergency call numbers,
1482 // we will try to connect the first outgoing call.
1483 outgoingCall.disconnect();
1484 return true;
1485 }
1486 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1487 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1488 // state, just disconnect it since the user has explicitly started a new call.
1489 outgoingCall.disconnect();
1490 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001491 }
1492 return false;
1493 }
1494
Santos Cordonf193ba42014-09-12 06:37:39 -07001495 if (hasMaximumHoldingCalls()) {
1496 // There is no more room for any more calls, unless it's an emergency.
1497 if (isEmergency) {
1498 // Kill the current active call, this is easier then trying to disconnect a
1499 // holding call and hold an active call.
1500 liveCall.disconnect();
1501 return true;
1502 }
1503 return false; // No more room!
1504 }
1505
1506 // We have room for at least one more holding call at this point.
1507
Tyler Gunne4cd9162015-08-11 15:36:04 -07001508 // TODO: Remove once b/23035408 has been corrected.
1509 // If the live call is a conference, it will not have a target phone account set. This
1510 // means the check to see if the live call has the same target phone account as the new
1511 // call will not cause us to bail early. As a result, we'll end up holding the
1512 // ongoing conference call. However, the ConnectionService is already doing that. This
1513 // has caused problems with some carriers. As a workaround until b/23035408 is
1514 // corrected, we will try and get the target phone account for one of the conference's
1515 // children and use that instead.
1516 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
1517 if (liveCallPhoneAccount == null && liveCall.isConference() &&
1518 !liveCall.getChildCalls().isEmpty()) {
1519 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
1520 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
1521 liveCallPhoneAccount);
1522 }
1523
Santos Cordonf193ba42014-09-12 06:37:39 -07001524 // First thing, if we are trying to make a call with the same phone account as the live
1525 // call, then allow it so that the connection service can make its own decision about
1526 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07001527 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
1528 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001529 return true;
1530 } else if (call.getTargetPhoneAccount() == null) {
1531 // Without a phone account, we can't say reliably that the call will fail.
1532 // If the user chooses the same phone account as the live call, then it's
1533 // still possible that the call can be made (like with CDMA calls not supporting
1534 // hold but they still support adding a call by going immediately into conference
1535 // mode). Return true here and we'll run this code again after user chooses an
1536 // account.
1537 return true;
1538 }
1539
1540 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001541 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07001542 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001543 liveCall.hold();
1544 return true;
1545 }
1546
1547 // The live call cannot be held so we're out of luck here. There's no room.
1548 return false;
1549 }
1550 return true;
1551 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001552
1553 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07001554 * Given a call, find the first non-null phone account handle of its children.
1555 *
1556 * @param parentCall The parent call.
1557 * @return The first non-null phone account handle of the children, or {@code null} if none.
1558 */
1559 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
1560 for (Call childCall : parentCall.getChildCalls()) {
1561 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
1562 if (childPhoneAccount != null) {
1563 return childPhoneAccount;
1564 }
1565 }
1566 return null;
1567 }
1568
1569 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001570 * Checks to see if the call should be on speakerphone and if so, set it.
1571 */
1572 private void maybeMoveToSpeakerPhone(Call call) {
1573 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001574 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001575 call.setStartWithSpeakerphoneOn(false);
1576 }
1577 }
1578
1579 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001580 * Creates a new call for an existing connection.
1581 *
1582 * @param callId The id of the new call.
1583 * @param connection The connection information.
1584 * @return The new call.
1585 */
1586 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1587 Call call = new Call(
1588 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001589 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001590 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001591 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001592 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001593 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001594 connection.getHandle() /* handle */,
1595 null /* gatewayInfo */,
1596 null /* connectionManagerPhoneAccount */,
1597 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1598 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001599 false /* isConference */,
1600 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001601
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001602 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1603 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001604 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001605 call.setCallerDisplayName(connection.getCallerDisplayName(),
1606 connection.getCallerDisplayNamePresentation());
1607
1608 call.addListener(this);
1609 addCall(call);
1610
1611 return call;
1612 }
1613
1614 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001615 * Dumps the state of the {@link CallsManager}.
1616 *
1617 * @param pw The {@code IndentingPrintWriter} to write the state to.
1618 */
1619 public void dump(IndentingPrintWriter pw) {
1620 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001621 if (mCalls != null) {
1622 pw.println("mCalls: ");
1623 pw.increaseIndent();
1624 for (Call call : mCalls) {
1625 pw.println(call);
1626 }
1627 pw.decreaseIndent();
1628 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001629 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1630
1631 if (mCallAudioManager != null) {
1632 pw.println("mCallAudioManager:");
1633 pw.increaseIndent();
1634 mCallAudioManager.dump(pw);
1635 pw.decreaseIndent();
1636 }
1637
1638 if (mTtyManager != null) {
1639 pw.println("mTtyManager:");
1640 pw.increaseIndent();
1641 mTtyManager.dump(pw);
1642 pw.decreaseIndent();
1643 }
1644
1645 if (mInCallController != null) {
1646 pw.println("mInCallController:");
1647 pw.increaseIndent();
1648 mInCallController.dump(pw);
1649 pw.decreaseIndent();
1650 }
1651
1652 if (mConnectionServiceRepository != null) {
1653 pw.println("mConnectionServiceRepository:");
1654 pw.increaseIndent();
1655 mConnectionServiceRepository.dump(pw);
1656 pw.decreaseIndent();
1657 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001658 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001659}