blob: ef9793a586e923092e0263d55ec6fc969c7c0c06 [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;
Sandeep Kunta39b77672014-09-01 17:22:57 +053024import android.os.Message;
Rekha Kumard240fe82015-04-01 21:45:57 -070025import android.os.SystemProperties;
Yorke Leee4a9c412014-11-14 16:59:42 -080026import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070027import android.provider.CallLog.Calls;
Yorke Lee2a66f7b2015-05-13 14:21:19 -070028import android.telecom.CallAudioState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080029import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080030import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070031import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070032import android.telecom.GatewayInfo;
33import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070034import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070035import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070036import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070037import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070038import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080039import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070040import android.telephony.TelephonyManager;
Yorke Lee8d3f2692015-05-08 11:44:39 -070041import android.text.TextUtils;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080042
Ihab Awada3653902015-01-23 13:44:46 -080043import com.android.internal.annotations.VisibleForTesting;
Rekha Kumard240fe82015-04-01 21:45:57 -070044import com.android.internal.telephony.PhoneConstants;
45import com.android.internal.telephony.TelephonyProperties;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070046import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080047
Santos Cordon4bc02452014-11-19 15:51:12 -080048import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070049import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070050import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070051import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070052import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070053import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070054import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080055
Ben Giladdd8c6082013-12-30 14:44:08 -080056/**
57 * Singleton.
58 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070059 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080060 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070061 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080062 */
Ihab Awadaa383cc2015-03-22 22:12:28 -070063@VisibleForTesting
Tyler Gunn86014fc2015-06-12 14:31:25 -070064public class CallsManager extends Call.ListenerBase implements VideoProviderProxy.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080065
Santos Cordondf399862014-08-06 04:39:15 -070066 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 interface CallsManagerListener {
68 void onCallAdded(Call call);
69 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070070 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070071 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070072 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070073 ConnectionServiceWrapper oldService,
74 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070076 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070077 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Yorke Lee2a66f7b2015-05-13 14:21:19 -070078 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070079 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070080 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070081 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070082 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080083 void onCanAddCallChanged(boolean canAddCall);
Tyler Gunn86014fc2015-06-12 14:31:25 -070084 void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile);
Ravindra205cb782015-10-07 16:57:53 +053085 void onMergeFailed(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070086 }
87
Tyler Gunn91d43cf2014-09-17 12:19:39 -070088 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080089
Santos Cordonf193ba42014-09-12 06:37:39 -070090 private static final int MAXIMUM_LIVE_CALLS = 1;
91 private static final int MAXIMUM_HOLD_CALLS = 1;
92 private static final int MAXIMUM_RINGING_CALLS = 1;
Roshan Pius4de4a892015-08-24 11:06:58 -070093 private static final int MAXIMUM_DIALING_CALLS = 1;
Santos Cordonf193ba42014-09-12 06:37:39 -070094 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080095 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Sandeep Kunta39b77672014-09-01 17:22:57 +053096 private static final int MAXIMUM_DSDA_LIVE_CALLS = 2;
97 private static final int MAXIMUM_DSDA_HOLD_CALLS = 2;
98 private static final int MAXIMUM_DSDA_TOP_LEVEL_CALLS = 4;
Santos Cordonf193ba42014-09-12 06:37:39 -070099
100 private static final int[] OUTGOING_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -0700101 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -0700102
Santos Cordon91bd74c2014-11-07 16:10:22 -0800103 private static final int[] LIVE_CALL_STATES =
Sandeep Kunta39b77672014-09-01 17:22:57 +0530104 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING,
105 CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -0800106
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800107 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700108 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
109 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800110 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700111 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
112 * load factor before resizing, 1 means we only expect a single thread to
113 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800114 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700115 private final Set<Call> mCalls = Collections.newSetFromMap(
116 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800117
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700118 private final ConnectionServiceRepository mConnectionServiceRepository;
119 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
120 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700121 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800122 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700123 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800124 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700125 // For this set initial table size to 16 because we add 13 listeners in
126 // the CallsManager constructor.
127 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
128 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700129 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700130 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800131 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700132 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700133 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700134 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700135 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700136 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700137 private final TelecomSystem.SyncRoot mLock;
138 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700139 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700140 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
141 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700142 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800143 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
144 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700145 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700146
Santos Cordon91bd74c2014-11-07 16:10:22 -0800147 private boolean mCanAddCall = true;
148
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700149 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700150 * The call the user is currently interacting with. This is the call that should have audio
151 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800152 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700153 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800154
Sandeep Kunta39b77672014-09-01 17:22:57 +0530155 private static final int LCH_PLAY_DTMF = 100;
156 private static final int LCH_STOP_DTMF = 101;
157 private static final int PHONE_START_DSDA_INCALL_TONE = 102;
158 private static final int LCH_DTMF_PERIODICITY = 3000;
159 private static final int LCH_DTMF_PERIOD = 500;
160 private static final String sSupervisoryCallHoldToneConfig =
161 SystemProperties.get("persist.radio.sch_tone", "none");
162
163 private static InCallTonePlayer.Factory mPlayerFactory;
164 private String mLchSub = null;
165
166 private InCallTonePlayer mLocalCallReminderTonePlayer = null;
167 private InCallTonePlayer mSupervisoryCallHoldTonePlayer = null;
168 private String mSubInConversation = null;
169
Nancy Chena469f762014-12-09 18:29:20 -0800170 private Runnable mStopTone;
171
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700172 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700173 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800174 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700175 CallsManager(
176 Context context,
177 TelecomSystem.SyncRoot lock,
178 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700179 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700180 MissedCallNotifier missedCallNotifier,
181 PhoneAccountRegistrar phoneAccountRegistrar,
182 HeadsetMediaButtonFactory headsetMediaButtonFactory,
183 ProximitySensorManagerFactory proximitySensorManagerFactory,
184 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700185 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700186 mLock = lock;
187 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700188 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700189 mPhoneAccountRegistrar = phoneAccountRegistrar;
190 mMissedCallNotifier = missedCallNotifier;
191 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
192 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800193 mDockManager = new DockManager(context);
194 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700195 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700196 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530197 mPlayerFactory = playerFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700198 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700199 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700200 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800201 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
202 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700203 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700204 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700205 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800206 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700207 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800208 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700209
Santos Cordondeb8c892014-05-30 01:38:03 -0700210 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700211 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700212 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700213 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700214 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700215 mListeners.add(new RingbackPlayer(this, playerFactory));
216 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700217 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700218 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700219 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700220 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700221 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700222
Ihab Awadabcbce42015-04-07 14:04:01 -0700223 mMissedCallNotifier.updateOnStartup(
224 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800225 }
226
Ihab Awad8de76912015-02-17 12:25:52 -0800227 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
228 if (mRespondViaSmsManager != null) {
229 mListeners.remove(mRespondViaSmsManager);
230 }
231 mRespondViaSmsManager = respondViaSmsManager;
232 mListeners.add(respondViaSmsManager);
233 }
234
235 public RespondViaSmsManager getRespondViaSmsManager() {
236 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800237 }
238
Santos Cordon766d04f2014-05-06 10:28:25 -0700239 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700240 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700241 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700242
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700243 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700244 if (!mCalls.contains(call)) {
245 // Call was not added previously in startOutgoingCall due to it being a potential MMI
246 // code, so add it now.
247 addCall(call);
248 }
249
250 // The call's ConnectionService has been updated.
251 for (CallsManagerListener listener : mListeners) {
252 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700253 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700254
255 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700256 }
257
258 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700259 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700260 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700261
Andrew Leee6288a72014-10-01 13:50:02 -0700262 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700263 }
264
265 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700266 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700267 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700268 setCallState(incomingCall, CallState.RINGING, "successful incoming call");
Santos Cordonf193ba42014-09-12 06:37:39 -0700269
David Ngca0c6782015-11-30 17:00:46 -0800270 if (hasMaximumRingingCalls(incomingCall.getTargetPhoneAccount().getId()) || hasMaximumDialingCalls() ) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700271 incomingCall.reject(false, null);
272 // since the call was not added to the list of calls, we have to call the missed
273 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700274 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700275 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
276 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530277 if (TelephonyManager.getDefault().getMultiSimConfiguration()
278 == TelephonyManager.MultiSimVariants.DSDA) {
279 incomingCall.mIsActiveSub = true;
280 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700281 addCall(incomingCall);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530282 setActiveSubscription(incomingCall.getTargetPhoneAccount().getId());
Santos Cordonf193ba42014-09-12 06:37:39 -0700283 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700284 }
285
286 @Override
287 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700288 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700289 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800290 }
291
Ihab Awadcb387ac2014-05-28 16:49:38 -0700292 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700293 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700294 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700295 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
296 addCall(call);
297 }
298
299 @Override
300 public void onFailedUnknownCall(Call call) {
301 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700302 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700303 call.removeListener(this);
304 }
305
306 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700307 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700308 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700309 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700310 }
311 }
312
Evan Charlton352105c2014-06-03 14:10:54 -0700313 @Override
314 public void onPostDialWait(Call call, String remaining) {
315 mInCallController.onPostDialWait(call, remaining);
316 }
317
Santos Cordona1610702014-06-04 20:22:56 -0700318 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800319 public void onPostDialChar(final Call call, char nextChar) {
320 if (PhoneNumberUtils.is12Key(nextChar)) {
321 // Play tone if it is one of the dialpad digits, canceling out the previously queued
322 // up stopTone runnable since playing a new tone automatically stops the previous tone.
323 if (mStopTone != null) {
324 mHandler.removeCallbacks(mStopTone);
325 }
326
327 mDtmfLocalTonePlayer.playTone(call, nextChar);
328
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700329 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800330 mStopTone = new Runnable() {
331 @Override
332 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700333 synchronized (mLock) {
334 // Set a timeout to stop the tone in case there isn't another tone to follow.
335 mDtmfLocalTonePlayer.stopTone(call);
336 }
Nancy Chena469f762014-12-09 18:29:20 -0800337 }
338 };
339 mHandler.postDelayed(
340 mStopTone,
341 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
342 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
343 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
344 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
345 // the previous tone is being stopped anyway.
346 if (mStopTone != null) {
347 mHandler.removeCallbacks(mStopTone);
348 }
349 mDtmfLocalTonePlayer.stopTone(call);
350 } else {
351 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
352 }
353 }
354
355 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700356 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700357 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800358 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700359 for (CallsManagerListener listener : mListeners) {
360 listener.onIsConferencedChanged(call);
361 }
362 }
363
364 @Override
365 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700366 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800367 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700368 for (CallsManagerListener listener : mListeners) {
369 listener.onIsConferencedChanged(call);
370 }
371 }
372
Ihab Awadff7493a2014-06-10 13:47:44 -0700373 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700374 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700375 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700376 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700377 }
378 }
379
Andrew Lee4a796602014-07-11 17:23:03 -0700380 @Override
381 public void onVideoStateChanged(Call call) {
382 for (CallsManagerListener listener : mListeners) {
383 listener.onVideoStateChanged(call);
384 }
385 }
386
Santos Cordoncf5b2912014-11-05 21:57:54 -0800387 @Override
388 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
389 mPendingCallsToDisconnect.add(call);
390 mHandler.postDelayed(new Runnable() {
391 @Override
392 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700393 synchronized (mLock) {
394 if (mPendingCallsToDisconnect.remove(call)) {
395 Log.i(this, "Delayed disconnection of call: %s", call);
396 call.disconnect();
397 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800398 }
399 }
400 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
401
402 return true;
403 }
404
Tyler Gunn86014fc2015-06-12 14:31:25 -0700405 /**
406 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
407 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
408 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
409 * respond to callbacks from the {@link VideoProviderProxy}.
410 *
411 * @param call The call.
412 */
413 @Override
414 public void onVideoCallProviderChanged(Call call) {
415 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
416
417 if (videoProviderProxy == null) {
418 return;
419 }
420
421 videoProviderProxy.addListener(this);
422 }
423
424 /**
425 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
426 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
427 * modification request.
428 *
429 * @param call The call.
430 * @param videoProfile The {@link VideoProfile}.
431 */
432 @Override
433 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
434 int videoState = videoProfile != null ? videoProfile.getVideoState() :
435 VideoProfile.STATE_AUDIO_ONLY;
436 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
437 .videoStateToString(videoState));
438
439 for (CallsManagerListener listener : mListeners) {
440 listener.onSessionModifyRequestReceived(call, videoProfile);
441 }
442 }
443
Santos Cordon4bc02452014-11-19 15:51:12 -0800444 Collection<Call> getCalls() {
445 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700446 }
447
448 Call getForegroundCall() {
449 return mForegroundCall;
450 }
451
Santos Cordonae193062014-05-21 21:21:49 -0700452 Ringer getRinger() {
453 return mRinger;
454 }
455
Santos Cordonf3671a62014-05-29 21:51:53 -0700456 InCallController getInCallController() {
457 return mInCallController;
458 }
459
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700460 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700461 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700462 if (call.isEmergencyCall()) {
463 return true;
464 }
465 }
466 return false;
467 }
468
Roshan Pius669f90b2015-09-02 16:21:39 -0700469 boolean hasOnlyDisconnectedCalls() {
470 for (Call call : mCalls) {
471 if (!call.isDisconnected()) {
472 return false;
473 }
474 }
475 return true;
476 }
477
Andrew Lee45506232014-10-22 17:54:33 -0700478 boolean hasVideoCall() {
479 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700480 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700481 return true;
482 }
483 }
484 return false;
485 }
486
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700487 CallAudioState getAudioState() {
488 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700489 }
490
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700491 boolean isTtySupported() {
492 return mTtyManager.isTtySupported();
493 }
494
495 int getCurrentTtyMode() {
496 return mTtyManager.getCurrentTtyMode();
497 }
498
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700499 void addListener(CallsManagerListener listener) {
500 mListeners.add(listener);
501 }
502
503 void removeListener(CallsManagerListener listener) {
504 mListeners.remove(listener);
505 }
506
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800507 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700508 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800509 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700510 * @param phoneAccountHandle The phone account which contains the component name of the
511 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800512 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800513 */
Evan Charlton89176372014-07-19 18:23:09 -0700514 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800515 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700516 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
517 if (handle == null) {
518 // Required for backwards compatibility
519 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
520 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700521 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700522 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800523 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700524 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700525 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700526 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700527 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700528 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700529 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700530 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700531 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700532 true /* isIncoming */,
533 false /* isConference */);
534
Andrew Lee1b906652015-06-01 11:44:55 -0700535 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700536 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700537 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700538 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800539 }
540
Yorke Lee9250e5f2014-10-01 13:39:09 -0700541 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
542 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
543 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
544 Call call = new Call(
545 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800546 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700547 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700548 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700549 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700550 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700551 handle,
552 null /* gatewayInfo */,
553 null /* connectionManagerPhoneAccount */,
554 phoneAccountHandle,
555 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
556 // to the existing connection instead of trying to create a new one.
557 true /* isIncoming */,
558 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700559 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700560 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700561 call.addListener(this);
562 call.startCreateConnection(mPhoneAccountRegistrar);
563 }
564
Yorke Lee8d3f2692015-05-08 11:44:39 -0700565 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
566 if (handle1 == null || handle2 == null) {
567 return handle1 == handle2;
568 }
569
570 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
571 return false;
572 }
573
574 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
575 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
576 return TextUtils.equals(number1, number2);
577 }
578
Santos Cordoncf5b2912014-11-05 21:57:54 -0800579 private Call getNewOutgoingCall(Uri handle) {
580 // First check to see if we can reuse any of the calls that are waiting to disconnect.
581 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800582 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800583 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700584 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800585 mPendingCallsToDisconnect.remove(pendingCall);
586 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800587 reusedCall = pendingCall;
588 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700589 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800590 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800591 }
592 }
Yorke Lee59979512014-12-02 01:03:40 -0800593 if (reusedCall != null) {
594 return reusedCall;
595 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800596
597 // Create a call with original handle. The handle may be changed when the call is attached
598 // to a connection service, but in most cases will remain the same.
599 return new Call(
600 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800601 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700602 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800603 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700604 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700605 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800606 handle,
607 null /* gatewayInfo */,
608 null /* connectionManagerPhoneAccount */,
609 null /* phoneAccountHandle */,
610 false /* isIncoming */,
611 false /* isConference */);
612 }
613
Nancy Chen0d3076c2014-07-30 14:45:44 -0700614 /**
615 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
616 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700617 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700618 * @param phoneAccountHandle The phone account which contains the component name of the
619 * connection service to use for this call.
620 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700621 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700622 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800623 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700624
Garik Badalyan7e1524a2015-08-25 11:08:18 -0700625 if (extras!=null) {
626 call.setVideoState(extras.getInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
627 VideoProfile.STATE_AUDIO_ONLY));
628 }
629
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530630 boolean isAddParticipant = ((extras != null) && (extras.getBoolean(
631 TelephonyProperties.ADD_PARTICIPANT_KEY, false)));
632 boolean isSkipSchemaOrConfUri = ((extras != null) && (extras.getBoolean(
633 TelephonyProperties.EXTRA_SKIP_SCHEMA_PARSING, false) ||
634 extras.getBoolean(TelephonyProperties.EXTRA_DIAL_CONFERENCE_URI, false)));
635
636 if (isAddParticipant) {
637 String number = handle.getSchemeSpecificPart();
638 if (!isSkipSchemaOrConfUri) {
639 number = PhoneNumberUtils.stripSeparators(number);
640 }
641 addParticipant(number);
642 mInCallController.bringToForeground(false);
643 return null;
644 }
645
646 // Force tel scheme for ims conf uri/skip schema calls to avoid selection of sip accounts
647 String scheme = (isSkipSchemaOrConfUri? PhoneAccount.SCHEME_TEL: handle.getScheme());
648
649 Log.d(this, "startOutgoingCall :: isAddParticipant=" + isAddParticipant
650 + " isSkipSchemaOrConfUri=" + isSkipSchemaOrConfUri + " scheme=" + scheme);
651
Nancy Chen1c5926f2014-09-17 14:44:14 -0700652 List<PhoneAccountHandle> accounts =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530653 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(scheme, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700654
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800655 Log.v(this, "startOutgoingCall found accounts = " + accounts);
656
Sandeep Kunta39b77672014-09-01 17:22:57 +0530657 if (mForegroundCall != null && TelephonyManager.getDefault().getMultiSimConfiguration()
658 != TelephonyManager.MultiSimVariants.DSDA) {
Roshan Pius1a373832015-08-07 11:39:05 -0700659 Call ongoingCall = mForegroundCall;
Nancy Chenbc95db92014-11-24 13:19:18 -0800660 // If there is an ongoing call, use the same phone account to place this new call.
Roshan Pius1a373832015-08-07 11:39:05 -0700661 // If the ongoing call is a conference call, we fetch the phone account from the
662 // child calls because we don't have targetPhoneAccount set on Conference calls.
663 // TODO: Set targetPhoneAccount for all conference calls (b/23035408).
664 if (ongoingCall.getTargetPhoneAccount() == null &&
665 !ongoingCall.getChildCalls().isEmpty()) {
666 ongoingCall = ongoingCall.getChildCalls().get(0);
667 }
668 if (ongoingCall.getTargetPhoneAccount() != null) {
669 phoneAccountHandle = ongoingCall.getTargetPhoneAccount();
670 }
Nancy Chenbc95db92014-11-24 13:19:18 -0800671 }
672
Nancy Chenbc9ef172014-08-15 17:11:57 -0700673 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
674 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700675 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700676 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700677 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700678 phoneAccountHandle = null;
679 }
680 }
681
682 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700683 // No preset account, check if default exists that supports the URI scheme for the
684 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700685 phoneAccountHandle =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530686 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(scheme);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700687 }
688
Nancy Chen1c5926f2014-09-17 14:44:14 -0700689 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700690
Tyler Gunn5b452df2014-10-01 15:02:58 -0700691 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700692
693 // Do not support any more live calls. Our options are to move a call to hold, disconnect
694 // a call, or cancel this call altogether.
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700695 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, call.isEmergencyCall())) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700696 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800697 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800698 if (mCalls.contains(call)) {
699 // This call can already exist if it is a reused call,
700 // See {@link #getNewOutgoingCall}.
701 call.disconnect();
702 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700703 return null;
704 }
705
Nancy Chen8d92f452014-10-20 21:43:44 -0700706 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700707 !call.isEmergencyCall();
Nancy Chen8d92f452014-10-20 21:43:44 -0700708
709 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700710 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700711 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700712 // Create our own instance to modify (since extras may be Bundle.EMPTY)
713 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700714 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700715 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700716 call.setState(
717 CallState.CONNECTING,
718 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700719 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700720
Andrew Lee1b906652015-06-01 11:44:55 -0700721 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700722
Tyler Gunn504eb832014-10-13 14:15:27 -0700723 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700724 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700725 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800726 } else if (!mCalls.contains(call)) {
727 // We check if mCalls already contains the call because we could potentially be reusing
728 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700729 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700730 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700731
732 return call;
733 }
734
Ben Gilada0d9f752014-02-26 11:49:03 -0800735 /**
Yorke Lee33501632014-03-17 19:24:12 -0700736 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800737 *
Yorke Lee33501632014-03-17 19:24:12 -0700738 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700739 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700740 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700741 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700742 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800743 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700744 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
745 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700746 if (call == null) {
747 // don't do anything if the call no longer exists
748 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700749 return;
750 }
751
Nancy Chen201b4372014-09-08 14:18:24 -0700752 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700753
754 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700755 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700756 } else {
757 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
758 Log.pii(uriHandle), Log.pii(handle));
759 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700760
Nancy Chen0d3076c2014-07-30 14:45:44 -0700761 call.setHandle(uriHandle);
762 call.setGatewayInfo(gatewayInfo);
Tyler Gunnd373c202015-05-20 08:21:03 -0700763 // Auto-enable speakerphone if the originating intent specified to do so, or if the call
Nivedita Sarkardae2c652015-08-31 10:55:51 -0700764 // is a video call or if the phone is docked.
765 call.setStartWithSpeakerphoneOn(speakerphoneOn || isSpeakerphoneAutoEnabled(videoState)
766 || mDockManager.isDocked());
Tyler Gunnc4abd912014-07-08 14:22:10 -0700767 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700768
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700769 if (call.isEmergencyCall()) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700770 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700771 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700772 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700773
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700774 if (call.getTargetPhoneAccount() != null || call.isEmergencyCall()) {
David Ngca0c6782015-11-30 17:00:46 -0800775 if (!call.isEmergencyCall()) {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530776 updateLchStatus(call.getTargetPhoneAccount().getId());
777 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700778 // If the account has been set, proceed to place the outgoing call.
779 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700780 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700781 }
Yorke Leef98fb572014-03-05 10:56:55 -0800782 }
783
784 /**
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530785 * Attempts to add participant in a call.
786 *
787 * @param number number to connect the call with.
788 */
789 private void addParticipant(String number) {
790 Log.i(this, "addParticipant number ="+number);
791 if (getForegroundCall() == null) {
792 // don't do anything if the call no longer exists
793 Log.i(this, "Canceling unknown call.");
794 return;
795 } else {
796 getForegroundCall().addParticipantWithConference(number);
797 }
798 }
799
800
801 /**
Santos Cordona1610702014-06-04 20:22:56 -0700802 * Attempts to start a conference call for the specified call.
803 *
Santos Cordon12d61822014-07-29 16:02:20 -0700804 * @param call The call to conference.
805 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700806 */
Santos Cordon12d61822014-07-29 16:02:20 -0700807 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700808 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700809 }
810
811 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700812 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
813 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800814 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700815 *
816 * @param call The call to answer.
817 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800818 */
Andrew Lee38931d02014-07-16 10:17:36 -0700819 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700820 if (!mCalls.contains(call)) {
821 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800822 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530823 Call activeCall = getFirstCallWithState(call.getTargetPhoneAccount()
824 .getId(), CallState.ACTIVE, CallState.DIALING);
Santos Cordon40f78c22014-04-07 02:11:42 -0700825 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700826 // STATE_DIALING, put it on hold before answering the call.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530827 if (activeCall != null && activeCall != call &&
828 (activeCall.isActive() ||
829 activeCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800830 if (0 == (mForegroundCall.getConnectionCapabilities()
831 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700832 // This call does not support hold. If it is from a different connection
833 // service, then disconnect it, otherwise allow the connection service to
834 // figure out the right states.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530835 if (activeCall.getConnectionService() != call.getConnectionService()) {
836 activeCall.disconnect();
Santos Cordonf193ba42014-09-12 06:37:39 -0700837 }
838 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800839 Call heldCall = getHeldCall();
840 if (heldCall != null) {
841 Log.v(this, "Disconnecting held call %s before holding active call.",
842 heldCall);
843 heldCall.disconnect();
844 }
845
Santos Cordonf193ba42014-09-12 06:37:39 -0700846 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
847 mForegroundCall, call);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530848 activeCall.hold();
Santos Cordonf193ba42014-09-12 06:37:39 -0700849 }
Santos Cordondf399862014-08-06 04:39:15 -0700850 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700851 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700852 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700853 }
854
Santos Cordona56f2762014-03-24 15:55:53 -0700855 for (CallsManagerListener listener : mListeners) {
856 listener.onIncomingCallAnswered(call);
857 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530858 updateLchStatus(call.getTargetPhoneAccount().getId());
Santos Cordon61d0f702014-02-19 02:52:23 -0800859 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700860 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700861 call.answer(videoState);
Tyler Gunnd373c202015-05-20 08:21:03 -0700862 if (isSpeakerphoneAutoEnabled(videoState)) {
Rekha Kumard240fe82015-04-01 21:45:57 -0700863 call.setStartWithSpeakerphoneOn(true);
864 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800865 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800866 }
867
Tyler Gunnd373c202015-05-20 08:21:03 -0700868 /**
869 * Determines if the speakerphone should be automatically enabled for the call. Speakerphone
870 * should be enabled if the call is a video call and bluetooth or the wired headset are not in
871 * use.
872 *
873 * @param videoState The video state of the call.
874 * @return {@code true} if the speakerphone should be enabled.
875 */
876 private boolean isSpeakerphoneAutoEnabled(int videoState) {
Etan Cohen7bcbb342015-06-25 15:39:21 -0700877 return VideoProfile.isVideo(videoState) &&
Tyler Gunnd373c202015-05-20 08:21:03 -0700878 !mWiredHeadsetManager.isPluggedIn() &&
879 !mCallAudioManager.isBluetoothDeviceAvailable() &&
880 isSpeakerEnabledForVideoCalls();
881 }
882
883 /**
884 * Determines if the speakerphone should be automatically enabled for video calls.
885 *
886 * @return {@code true} if the speakerphone should automatically be enabled.
887 */
Rekha Kumard240fe82015-04-01 21:45:57 -0700888 private static boolean isSpeakerEnabledForVideoCalls() {
889 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
890 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
891 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
892 }
893
Santos Cordone3d76ab2014-01-28 17:25:20 -0800894 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700895 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
896 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800897 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800898 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700899 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700900 if (!mCalls.contains(call)) {
901 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800902 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700903 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700904 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700905 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530906 setActiveSubscription(getConversationSub());
Ihab Awadff7493a2014-06-10 13:47:44 -0700907 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800908 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800909 }
910
911 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700912 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700913 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700914 * @param digit The DTMF digit to play.
915 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700916 void playDtmfTone(Call call, char digit) {
917 if (!mCalls.contains(call)) {
918 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700919 } else {
920 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700921 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700922 }
923 }
924
925 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700926 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700927 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700928 void stopDtmfTone(Call call) {
929 if (!mCalls.contains(call)) {
930 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700931 } else {
932 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700933 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700934 }
935 }
936
937 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700938 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700939 */
Evan Charlton352105c2014-06-03 14:10:54 -0700940 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700941 if (!mCalls.contains(call)) {
942 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700943 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700944 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700945 }
946 }
947
948 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700949 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800950 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
951 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800952 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700953 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700954 Log.v(this, "disconnectCall %s", call);
955
Sailesh Nepale59bb192014-04-01 18:33:59 -0700956 if (!mCalls.contains(call)) {
957 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800958 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700959 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800960 call.disconnect();
961 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800962 }
Santos Cordon681663d2014-01-30 04:32:15 -0800963
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700964 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700965 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700966 */
967 void disconnectAllCalls() {
968 Log.v(this, "disconnectAllCalls");
969
970 for (Call call : mCalls) {
971 disconnectCall(call);
972 }
973 }
974
Nancy Chenbc9ef172014-08-15 17:11:57 -0700975
Tyler Gunn61b92102014-08-19 07:42:20 -0700976 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700977 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700978 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
979 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700980 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700981 void holdCall(Call call) {
982 if (!mCalls.contains(call)) {
983 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700984 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700985 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700986 call.hold();
987 }
988 }
989
990 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700991 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700992 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
993 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700994 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700995 void unholdCall(Call call) {
996 if (!mCalls.contains(call)) {
997 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700998 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700999 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001000 for (Call c : mCalls) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301001 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1002 PhoneAccountHandle ph1 = c.getTargetPhoneAccount();
Tyler Gunne44adb42015-01-02 10:55:35 -08001003 // Only attempt to hold parent calls and not the individual children.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301004 // if 'c' is not for same subscription as call, then don't disturb 'c'
1005 if (c != null && c.isAlive() && c != call && c.getParentCall() == null
1006 && (ph != null && ph1 != null &&
1007 isSamePhAccIdOrSipId(ph.getId(), ph1.getId()))) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001008 c.hold();
1009 }
1010 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001011 call.unhold();
1012 }
1013 }
1014
Sandeep Kunta39b77672014-09-01 17:22:57 +05301015 /**
1016 * Returns true if the ids are same or one of the ids is sip id.
1017 */
1018 private boolean isSamePhAccIdOrSipId(String id1, String id2) {
1019 boolean ret = ((id1 != null && id2 != null) &&
1020 (id1.equals(id2) || id1.contains("sip") || id2.contains("sip")));
1021 Log.d(this, "isSamePhAccIdOrSipId: id1 = " + id1 + " id2 = " + id2 + " ret = " + ret);
1022 return ret;
1023 }
1024
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001025 /** Called by the in-call UI to change the mute state. */
1026 void mute(boolean shouldMute) {
1027 mCallAudioManager.mute(shouldMute);
1028 }
1029
1030 /**
1031 * Called by the in-call UI to change the audio route, for example to change from earpiece to
1032 * speaker phone.
1033 */
1034 void setAudioRoute(int route) {
1035 mCallAudioManager.setAudioRoute(route);
1036 }
1037
Yorke Leed1346872014-07-28 14:38:45 -07001038 /** Called by the in-call UI to turn the proximity sensor on. */
1039 void turnOnProximitySensor() {
1040 mProximitySensorManager.turnOn();
1041 }
1042
1043 /**
1044 * Called by the in-call UI to turn the proximity sensor off.
1045 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
1046 * the screen will be kept off until the proximity sensor goes negative.
1047 */
1048 void turnOffProximitySensor(boolean screenOnImmediately) {
1049 mProximitySensorManager.turnOff(screenOnImmediately);
1050 }
1051
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001052 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -07001053 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001054 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -07001055 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -07001056 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -07001057 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -07001058 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
1059 // respecting a rewritten number or a canceled number. This is unlikely since
1060 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
1061 // a phone account from the in-call UI.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301062 Log.i(this, "phoneAccountSelected , id = %s", account.getId());
1063 updateLchStatus(account.getId());
Ihab Awadb78b2762014-07-25 15:16:23 -07001064 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -07001065
1066 // Note: emergency calls never go through account selection dialog so they never
1067 // arrive here.
1068 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001069 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -07001070 } else {
1071 call.disconnect();
1072 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001073
1074 if (setDefault) {
1075 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
1076 }
Nancy Chen53ceedc2014-07-08 18:56:51 -07001077 }
1078 }
1079
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001080 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001081 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001082 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001083 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001084 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001085 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001086 }
1087
Sailesh Nepale59bb192014-04-01 18:33:59 -07001088 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001089 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -08001090 }
1091
Sailesh Nepale59bb192014-04-01 18:33:59 -07001092 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001093 setCallState(call, CallState.DIALING, "dialing set explicitly");
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001094 maybeMoveToEarpiece(call);
Santos Cordond6782cd2015-04-16 09:57:50 -07001095 maybeMoveToSpeakerPhone(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301096 setActiveSubscription(call.getTargetPhoneAccount().getId());
Santos Cordon681663d2014-01-30 04:32:15 -08001097 }
1098
Sailesh Nepale59bb192014-04-01 18:33:59 -07001099 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001100 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -07001101 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -08001102 }
1103
Sailesh Nepale59bb192014-04-01 18:33:59 -07001104 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001105 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001106 }
1107
Santos Cordon049b7b62014-01-30 05:34:26 -08001108 /**
Santos Cordonf193ba42014-09-12 06:37:39 -07001109 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
1110 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -08001111 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001112 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -08001113 */
Andrew Lee701dc002014-09-11 21:29:12 -07001114 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
Suresh Koletief564312015-08-24 19:31:55 +05301115 // Show MT call in call log as a missed call if immediately disconnected
1116 // before creating a connection.
1117 if (!mCalls.contains(call) && (DisconnectCause.MISSED == disconnectCause.getCode())) {
1118 addCall(call);
1119 mMissedCallNotifier.showMissedCallNotification(call);
1120 }
Andrew Lee701dc002014-09-11 21:29:12 -07001121 call.setDisconnectCause(disconnectCause);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301122 int prevState = call.getState();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001123 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sandeep Kunta39b77672014-09-01 17:22:57 +05301124 String activeSub = getActiveSubscription();
1125 String conversationSub = getConversationSub();
1126 String lchSub = IsAnySubInLch();
1127
1128 PhoneAccount phAcc =
1129 getPhoneAccountRegistrar().getPhoneAccount(call.getTargetPhoneAccount());
1130 if (activeSub != null && (call.getTargetPhoneAccount() != null &&
1131 call.getTargetPhoneAccount().getId().equals(activeSub)) &&
1132 (phAcc != null) && (phAcc.isSet(PhoneAccount.LCH)) &&
1133 (conversationSub != null) &&
1134 (!conversationSub.equals(activeSub))) {
1135 Log.d(this,"Set active sub to conversation sub");
1136 setActiveSubscription(conversationSub);
1137 } else if ((conversationSub == null) && (lchSub != null) &&
1138 ((prevState == CallState.CONNECTING) || (prevState ==
1139 CallState.SELECT_PHONE_ACCOUNT)) &&
1140 (call.getState() == CallState.DISCONNECTED)) {
1141 Log.d(this,"remove sub with call from LCH");
1142 updateLchStatus(lchSub);
1143 setActiveSubscription(lchSub);
1144 manageDsdaInCallTones(false);
1145 }
1146
1147 if ((call.getTargetPhoneAccount() != null) && (phAcc != null) &&
1148 (phAcc.isSet(PhoneAccount.LCH))) {
1149 Call activecall = getFirstCallWithState(call.getTargetPhoneAccount().getId(),
1150 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1151 Log.d(this,"activecall: " + activecall);
1152 if (activecall == null) {
1153 phAcc.unSetBit(PhoneAccount.LCH);
1154 manageDsdaInCallTones(false);
1155 }
1156 }
1157 }
1158
1159 private String IsAnySubInLch() {
1160 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
1161 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
1162 if ((phAcc != null) && (phAcc.isSet(PhoneAccount.LCH))) {
1163 Log.d(this, "Sub in LCH: " + ph.getId());
1164 return ph.getId();
1165 }
1166 }
1167 return null;
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -07001168 }
1169
Ben Gilada0d9f752014-02-26 11:49:03 -08001170 /**
Ihab Awada02bef52014-08-13 18:18:42 -07001171 * Removes an existing disconnected call, and notifies the in-call app.
1172 */
1173 void markCallAsRemoved(Call call) {
1174 removeCall(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301175 if (!hasAnyCalls()) {
1176 updateLchStatus(null);
1177 setActiveSubscription(null);
1178 manageDsdaInCallTones(false);
1179 }
Ihab Awad5b281322014-09-25 18:25:29 -07001180 if (mLocallyDisconnectingCalls.contains(call)) {
1181 mLocallyDisconnectingCalls.remove(call);
1182 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1183 mForegroundCall.unhold();
1184 }
1185 }
Ihab Awada02bef52014-08-13 18:18:42 -07001186 }
1187
1188 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001189 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001190 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001191 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001192 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001193 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001194 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001195 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001196 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001197 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001198 if (call.getState() != CallState.DISCONNECTED) {
1199 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1200 }
1201 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001202 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001203 }
1204 }
1205 }
1206
Santos Cordondeb8c892014-05-30 01:38:03 -07001207 boolean hasAnyCalls() {
1208 return !mCalls.isEmpty();
1209 }
1210
Santos Cordonc7e85d42014-05-22 02:51:48 -07001211 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001212 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001213 }
1214
Sandeep Kunta39b77672014-09-01 17:22:57 +05301215 boolean hasActiveOrHoldingCall(String sub) {
1216 return (getFirstCallWithState(sub, CallState.ACTIVE, CallState.ON_HOLD) != null);
1217 }
1218
Santos Cordonc7e85d42014-05-22 02:51:48 -07001219 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001220 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001221 }
1222
Santos Cordondeb8c892014-05-30 01:38:03 -07001223 boolean onMediaButton(int type) {
1224 if (hasAnyCalls()) {
1225 if (HeadsetMediaButton.SHORT_PRESS == type) {
1226 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1227 if (ringingCall == null) {
1228 mCallAudioManager.toggleMute();
1229 return true;
1230 } else {
Andrew Lee38931d02014-07-16 10:17:36 -07001231 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -07001232 return true;
1233 }
1234 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1235 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1236 Call callToHangup = getFirstCallWithState(
1237 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1238 if (callToHangup != null) {
1239 callToHangup.disconnect();
1240 return true;
1241 }
1242 }
1243 }
1244 return false;
1245 }
1246
1247 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001248 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001249 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001250 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -08001251 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1252 return false;
1253 }
1254
Santos Cordon91bd74c2014-11-07 16:10:22 -08001255 int count = 0;
1256 for (Call call : mCalls) {
1257 if (call.isEmergencyCall()) {
1258 // We never support add call if one of the calls is an emergency call.
1259 return false;
Roshan Piusd9544092015-07-31 11:31:52 -07001260 } else if (!call.getChildCalls().isEmpty() && !call.can(Connection.CAPABILITY_HOLD)) {
1261 // This is to deal with CDMA conference calls. CDMA conference calls do not
1262 // allow the addition of another call when it is already in a 3 way conference.
1263 // So, we detect that it is a CDMA conference call by checking if the call has
1264 // some children and it does not support the CAPABILILTY_HOLD
1265 // TODO: This maybe cleaner if the lower layers can explicitly signal to telecom
1266 // about this limitation (b/22880180).
1267 return false;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001268 } else if (call.getParentCall() == null) {
1269 count++;
1270 }
Santos Cordon10838c22014-06-11 17:36:04 -07001271
Santos Cordon91bd74c2014-11-07 16:10:22 -08001272 // We do not check states for canAddCall. We treat disconnected calls the same
1273 // and wait until they are removed instead. If we didn't count disconnected calls,
1274 // we could put InCallServices into a state where they are showing two calls but
1275 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1276 // and acts better if we wait until the call is removed.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301277 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1278 == TelephonyManager.MultiSimVariants.DSDA) {
1279 if (count >= MAXIMUM_DSDA_TOP_LEVEL_CALLS) {
1280 return false;
1281 }
1282 } else if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001283 return false;
1284 }
1285 }
1286 return true;
1287 }
1288
Ihab Awada3653902015-01-23 13:44:46 -08001289 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001290 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001291 return getFirstCallWithState(CallState.RINGING);
1292 }
1293
1294 Call getActiveCall() {
1295 return getFirstCallWithState(CallState.ACTIVE);
1296 }
1297
Nancy Chen05a9e402014-09-26 14:14:32 -07001298 Call getDialingCall() {
1299 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001300 }
1301
1302 Call getHeldCall() {
1303 return getFirstCallWithState(CallState.ON_HOLD);
1304 }
1305
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001306 int getNumHeldCalls() {
1307 int count = 0;
1308 for (Call call : mCalls) {
1309 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1310 count++;
1311 }
1312 }
1313 return count;
1314 }
1315
Roshan Pius93c9c012015-08-28 11:10:56 -07001316 Call getOutgoingCall() {
1317 return getFirstCallWithState(OUTGOING_CALL_STATES);
1318 }
1319
Santos Cordonf193ba42014-09-12 06:37:39 -07001320 Call getFirstCallWithState(int... states) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301321 return getFirstCallWithState((Call) null, states);
Santos Cordonf193ba42014-09-12 06:37:39 -07001322 }
1323
Santos Cordon10838c22014-06-11 17:36:04 -07001324 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001325 * Returns the first call that it finds with the given states. The states are treated as having
1326 * priority order so that any call with the first state will be returned before any call with
1327 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001328 *
1329 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001330 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001331 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001332 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001333 // check the foreground first
1334 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1335 return mForegroundCall;
1336 }
1337
Santos Cordondeb8c892014-05-30 01:38:03 -07001338 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001339 if (Objects.equals(callToSkip, call)) {
1340 continue;
1341 }
1342
1343 // Only operate on top-level calls
1344 if (call.getParentCall() != null) {
1345 continue;
1346 }
1347
Santos Cordondeb8c892014-05-30 01:38:03 -07001348 if (currentState == call.getState()) {
1349 return call;
1350 }
1351 }
1352 }
1353 return null;
1354 }
1355
Sandeep Kunta39b77672014-09-01 17:22:57 +05301356 /**
1357 * Returns the first call that it finds with the given states for given subscription.
1358 * the states are treated as having priority order so that any call with the first
1359 * state will be returned before any call with states listed later in the parameter list.
1360 *
1361 * @param subId check calls only on this subscription
1362 * @param callToSkip Call that this method should skip while searching
1363 */
1364 Call getFirstCallWithState(String subId, Call callToSkip, int... states) {
1365 for (int currentState : states) {
1366 // check the foreground first
1367 if (mForegroundCall != null && mForegroundCall.getState() == currentState
1368 && mForegroundCall.getTargetPhoneAccount() != null
1369 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(), subId)) {
1370 return mForegroundCall;
1371 }
1372
1373 for (Call call : mCalls) {
1374 if (Objects.equals(callToSkip, call)) {
1375 continue;
1376 }
1377
1378 // Only operate on top-level calls
1379 if (call.getParentCall() != null) {
1380 continue;
1381 }
1382
1383 if (currentState == call.getState() && call.getTargetPhoneAccount() != null
1384 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1385 return call;
1386 }
1387 }
1388 }
1389 return null;
1390 }
1391
Santos Cordon0fbe6322014-08-14 04:04:25 -07001392 Call createConferenceCall(
1393 PhoneAccountHandle phoneAccount,
1394 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001395
1396 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1397 // which is the default value for new Calls.
1398 long connectTime =
1399 parcelableConference.getConnectTimeMillis() ==
1400 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1401 parcelableConference.getConnectTimeMillis();
1402
Santos Cordon0fbe6322014-08-14 04:04:25 -07001403 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001404 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001405 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001406 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001407 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001408 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001409 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001410 null /* handle */,
1411 null /* gatewayInfo */,
1412 null /* connectionManagerPhoneAccount */,
1413 phoneAccount,
1414 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001415 true /* isConference */,
1416 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001417
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001418 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1419 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001420 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001421 call.setVideoState(parcelableConference.getVideoState());
1422 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001423 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001424 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001425
1426 // TODO: Move this to be a part of addCall()
1427 call.addListener(this);
1428 addCall(call);
1429 return call;
1430 }
1431
Yorke Leef86db2e2014-09-12 17:56:48 -07001432 /**
1433 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1434 */
1435 int getCallState() {
1436 return mPhoneStateBroadcaster.getCallState();
1437 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001438
Santos Cordon4b2c1192014-03-19 18:15:38 -07001439 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001440 * Retrieves the {@link PhoneAccountRegistrar}.
1441 *
1442 * @return The {@link PhoneAccountRegistrar}.
1443 */
1444 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1445 return mPhoneAccountRegistrar;
1446 }
1447
1448 /**
1449 * Retrieves the {@link MissedCallNotifier}
1450 * @return The {@link MissedCallNotifier}.
1451 */
1452 MissedCallNotifier getMissedCallNotifier() {
1453 return mMissedCallNotifier;
1454 }
1455
1456 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001457 * Adds the specified call to the main list of live calls.
1458 *
1459 * @param call The call to add.
1460 */
1461 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001462 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001463 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001464 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001465 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001466
Santos Cordondf399862014-08-06 04:39:15 -07001467 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001468 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001469 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001470 if (Log.SYSTRACE_DEBUG) {
1471 Trace.beginSection(listener.getClass().toString() + " addCall");
1472 }
Santos Cordona56f2762014-03-24 15:55:53 -07001473 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001474 if (Log.SYSTRACE_DEBUG) {
1475 Trace.endSection();
1476 }
Santos Cordona56f2762014-03-24 15:55:53 -07001477 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001478 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001479 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001480 }
1481
Sailesh Nepal810735e2014-03-18 18:15:46 -07001482 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001483 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001484 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001485
Santos Cordon672321e2014-08-21 14:38:58 -07001486 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001487 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001488 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001489
1490 boolean shouldNotify = false;
1491 if (mCalls.contains(call)) {
1492 mCalls.remove(call);
1493 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001494 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001495
Santos Cordon2685dab2015-04-17 17:12:09 -07001496 call.destroy();
1497
Sailesh Nepale59bb192014-04-01 18:33:59 -07001498 // Only broadcast changes for calls that are being tracked.
1499 if (shouldNotify) {
1500 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001501 if (Log.SYSTRACE_DEBUG) {
1502 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1503 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001504 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001505 if (Log.SYSTRACE_DEBUG) {
1506 Trace.endSection();
1507 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001508 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001509 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001510 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001511 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001512 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001513
Sailesh Nepal810735e2014-03-18 18:15:46 -07001514 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001515 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001516 *
1517 * @param call The call.
1518 * @param newState The new state of the call.
1519 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001520 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001521 if (call == null) {
1522 return;
1523 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001524 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001525 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1526 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001527 if (newState != oldState) {
1528 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1529 // 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 -07001530 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001531 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001532 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001533 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001534 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001535 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001536
Yorke Leee4a9c412014-11-14 16:59:42 -08001537 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001538 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001539 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001540 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001541 if (Log.SYSTRACE_DEBUG) {
1542 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1543 }
Santos Cordona56f2762014-03-24 15:55:53 -07001544 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001545 if (Log.SYSTRACE_DEBUG) {
1546 Trace.endSection();
1547 }
Santos Cordona56f2762014-03-24 15:55:53 -07001548 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001549 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001550 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001551 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001552 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301553 manageDsdaInCallTones(false);
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001554 }
1555
1556 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001557 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001558 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001559 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001560 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001561 Call newForegroundCall = null;
Santos Cordondf399862014-08-06 04:39:15 -07001562 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001563 // of its state will be foreground by default and instead the connection service should
1564 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001565 // the call should play audio and listen to microphone if it wants.
1566
Sandeep Kunta39b77672014-09-01 17:22:57 +05301567 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1568 == TelephonyManager.MultiSimVariants.DSDA) {
1569 String lchSub = getLchSub();
1570 for (Call call : mCalls) {
1571 // Only top-level calls can be in foreground
1572 if (call.getParentCall() != null) {
1573 continue;
1574 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001575
Sandeep Kunta39b77672014-09-01 17:22:57 +05301576 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1577 if (ph != null && ph.getId().equals(lchSub)) continue;
1578 // Active calls have priority.
1579 if (call.isActive()) {
1580 newForegroundCall = call;
1581 break;
1582 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001583
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001584 // If only call in call list is held call it's also a foreground call
1585 if (mCalls.size() == 1 && call.getState() == CallState.ON_HOLD) {
1586 newForegroundCall = call;
1587 }
1588
Shriram Ganesh74254492015-10-26 10:36:19 -07001589 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
1590 || call.getState() == CallState.RINGING) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301591 newForegroundCall = call;
1592 // Don't break in case there's an active call that has priority.
1593 }
1594 }
1595 // if active sub doesn't have any calls, then consider calls on all subs,
1596 // which ever call is active set that as foreground call. give more priority
1597 // to ringing call on LCH sub over active call.
1598 if (newForegroundCall == null) {
1599 newForegroundCall = getFirstCallWithState(CallState.RINGING);
1600 if (newForegroundCall == null) {
1601 newForegroundCall = getFirstCallWithState(CallState.ACTIVE);
1602 }
1603 }
1604 } else {
1605 for (Call call : mCalls) {
1606 // Only top-level calls can be in foreground
1607 if (call.getParentCall() != null) {
1608 continue;
1609 }
1610
1611 // Active calls have priority.
1612 if (call.isActive()) {
1613 newForegroundCall = call;
1614 break;
1615 }
1616
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001617 // If only call in call list is held call it's also a foreground call
1618 if (mCalls.size() == 1 && call.getState() == CallState.ON_HOLD) {
1619 newForegroundCall = call;
1620 }
1621
Shriram Ganesh74254492015-10-26 10:36:19 -07001622 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
1623 || call.getState() == CallState.RINGING) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301624 newForegroundCall = call;
1625 // Don't break in case there's an active call that has priority.
1626 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001627 }
1628 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001629
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001630 /* In the case where there are 2 calls, when the Hold button is pressed for active
1631 * call, as an intermediate state we would have both calls in held state before
1632 * the background call is moved to active state. In such an intermediate stage
1633 * updating the newForegroundCall to null causes issues with abandoning audio
1634 * focus. Skip updating the foreground call with null in such cases. */
1635 if (newForegroundCall == null && getFirstCallWithState(CallState.ON_HOLD) != null) {
1636 Log.v(this, "Skip updating foreground call in intermediate stage");
1637 newForegroundCall = mForegroundCall;
1638 }
1639
Sailesh Nepal810735e2014-03-18 18:15:46 -07001640 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001641 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001642 Call oldForegroundCall = mForegroundCall;
1643 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001644
Santos Cordona56f2762014-03-24 15:55:53 -07001645 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001646 if (Log.SYSTRACE_DEBUG) {
1647 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1648 }
Santos Cordona56f2762014-03-24 15:55:53 -07001649 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001650 if (Log.SYSTRACE_DEBUG) {
1651 Trace.endSection();
1652 }
Santos Cordona56f2762014-03-24 15:55:53 -07001653 }
Santos Cordon681663d2014-01-30 04:32:15 -08001654 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001655 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001656 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001657
Santos Cordon91bd74c2014-11-07 16:10:22 -08001658 private void updateCanAddCall() {
1659 boolean newCanAddCall = canAddCall();
1660 if (newCanAddCall != mCanAddCall) {
1661 mCanAddCall = newCanAddCall;
1662 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001663 if (Log.SYSTRACE_DEBUG) {
1664 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1665 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001666 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001667 if (Log.SYSTRACE_DEBUG) {
1668 Trace.endSection();
1669 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001670 }
1671 }
1672 }
1673
1674 private void updateCallsManagerState() {
1675 updateForegroundCall();
1676 updateCanAddCall();
1677 }
1678
Yorke Leeb701f6d2014-08-12 14:04:19 -07001679 private boolean isPotentialMMICode(Uri handle) {
1680 return (handle != null && handle.getSchemeSpecificPart() != null
1681 && handle.getSchemeSpecificPart().contains("#"));
1682 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001683
Tyler Gunn5b452df2014-10-01 15:02:58 -07001684 /**
1685 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1686 * MMI codes which can be dialed when one or more calls are in progress.
1687 * <P>
1688 * Checks for numbers formatted similar to the MMI codes defined in:
1689 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1690 * and
1691 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1692 *
1693 * @param handle The URI to call.
1694 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1695 */
1696 private boolean isPotentialInCallMMICode(Uri handle) {
1697 if (handle != null && handle.getSchemeSpecificPart() != null &&
1698 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1699
1700 String dialedNumber = handle.getSchemeSpecificPart();
1701 return (dialedNumber.equals("0") ||
1702 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1703 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1704 dialedNumber.equals("3") ||
1705 dialedNumber.equals("4") ||
1706 dialedNumber.equals("5"));
1707 }
1708 return false;
1709 }
1710
Santos Cordonf193ba42014-09-12 06:37:39 -07001711 private int getNumCallsWithState(int... states) {
1712 int count = 0;
1713 for (int state : states) {
1714 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001715 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001716 count++;
1717 }
1718 }
1719 }
1720 return count;
1721 }
1722
Sandeep Kunta39b77672014-09-01 17:22:57 +05301723 private int getNumCallsWithState(String subId, int... states) {
1724 int count = 0;
1725 for (int state : states) {
1726 for (Call call : mCalls) {
1727 if (call.getParentCall() == null && call.getState() == state
1728 && call.getTargetPhoneAccount() != null
1729 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1730 count++;
1731 }
1732 }
1733 }
1734 return count;
1735 }
1736
Santos Cordonf193ba42014-09-12 06:37:39 -07001737 private boolean hasMaximumLiveCalls() {
1738 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1739 }
1740
Sandeep Kunta39b77672014-09-01 17:22:57 +05301741 private boolean hasMaximumLiveCalls(String subId) {
1742 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(subId, LIVE_CALL_STATES);
1743 }
1744
Santos Cordonf193ba42014-09-12 06:37:39 -07001745 private boolean hasMaximumHoldingCalls() {
1746 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1747 }
1748
Sandeep Kunta39b77672014-09-01 17:22:57 +05301749 private boolean hasMaximumHoldingCalls(String subId) {
1750 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(subId, CallState.ON_HOLD);
1751 }
1752
Santos Cordonf193ba42014-09-12 06:37:39 -07001753 private boolean hasMaximumRingingCalls() {
1754 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1755 }
1756
Sandeep Kunta39b77672014-09-01 17:22:57 +05301757 private boolean hasMaximumRingingCalls(String subId) {
1758 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(subId, CallState.RINGING);
1759 }
1760
Santos Cordonf193ba42014-09-12 06:37:39 -07001761 private boolean hasMaximumOutgoingCalls() {
1762 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1763 }
1764
Roshan Pius4de4a892015-08-24 11:06:58 -07001765 private boolean hasMaximumDialingCalls() {
1766 return MAXIMUM_DIALING_CALLS <= getNumCallsWithState(CallState.DIALING);
1767 }
1768
Santos Cordonf193ba42014-09-12 06:37:39 -07001769 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301770 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1771 == TelephonyManager.MultiSimVariants.DSDA) {
1772 return makeRoomForOutgoingCallForDsda(call, isEmergency);
1773 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001774 if (hasMaximumLiveCalls()) {
1775 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1776 // have to change.
1777 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301778 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1779 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001780
Santos Cordond1766502014-09-26 15:45:39 -07001781 if (call == liveCall) {
1782 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001783 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001784 // state since the call was already populated into the list.
1785 return true;
1786 }
1787
Andrew Leee6595182014-09-23 18:43:05 -07001788 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001789 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1790 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1791 // Disconnect the current outgoing call if it's not an emergency call. If the
1792 // user tries to make two outgoing calls to different emergency call numbers,
1793 // we will try to connect the first outgoing call.
1794 outgoingCall.disconnect();
1795 return true;
1796 }
1797 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1798 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1799 // state, just disconnect it since the user has explicitly started a new call.
1800 outgoingCall.disconnect();
1801 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001802 }
1803 return false;
1804 }
1805
Santos Cordonf193ba42014-09-12 06:37:39 -07001806 if (hasMaximumHoldingCalls()) {
1807 // There is no more room for any more calls, unless it's an emergency.
1808 if (isEmergency) {
1809 // Kill the current active call, this is easier then trying to disconnect a
1810 // holding call and hold an active call.
1811 liveCall.disconnect();
1812 return true;
1813 }
1814 return false; // No more room!
1815 }
1816
1817 // We have room for at least one more holding call at this point.
1818
Tyler Gunne4cd9162015-08-11 15:36:04 -07001819 // TODO: Remove once b/23035408 has been corrected.
1820 // If the live call is a conference, it will not have a target phone account set. This
1821 // means the check to see if the live call has the same target phone account as the new
1822 // call will not cause us to bail early. As a result, we'll end up holding the
1823 // ongoing conference call. However, the ConnectionService is already doing that. This
1824 // has caused problems with some carriers. As a workaround until b/23035408 is
1825 // corrected, we will try and get the target phone account for one of the conference's
1826 // children and use that instead.
1827 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
1828 if (liveCallPhoneAccount == null && liveCall.isConference() &&
1829 !liveCall.getChildCalls().isEmpty()) {
1830 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
1831 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
1832 liveCallPhoneAccount);
1833 }
1834
Santos Cordonf193ba42014-09-12 06:37:39 -07001835 // First thing, if we are trying to make a call with the same phone account as the live
1836 // call, then allow it so that the connection service can make its own decision about
1837 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07001838 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
1839 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001840 return true;
1841 } else if (call.getTargetPhoneAccount() == null) {
1842 // Without a phone account, we can't say reliably that the call will fail.
1843 // If the user chooses the same phone account as the live call, then it's
1844 // still possible that the call can be made (like with CDMA calls not supporting
1845 // hold but they still support adding a call by going immediately into conference
1846 // mode). Return true here and we'll run this code again after user chooses an
1847 // account.
1848 return true;
1849 }
1850
1851 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001852 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07001853 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001854 liveCall.hold();
1855 return true;
1856 }
1857
1858 // The live call cannot be held so we're out of luck here. There's no room.
1859 return false;
1860 }
1861 return true;
1862 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001863
Sandeep Kunta39b77672014-09-01 17:22:57 +05301864 private boolean makeRoomForOutgoingCallForDsda(Call call, boolean isEmergency) {
1865 if (isEmergency || (call.getState() == CallState.SELECT_PHONE_ACCOUNT)) {
1866 return true;
1867 }
1868
1869 PhoneAccountHandle phAcc = call.getTargetPhoneAccount();
1870 if (phAcc == null) {
1871 if (getNumCallsWithState(LIVE_CALL_STATES) == MAXIMUM_DSDA_LIVE_CALLS
1872 && getNumCallsWithState(CallState.ON_HOLD) == MAXIMUM_DSDA_HOLD_CALLS) {
1873 return false;
1874 } else {
1875 return true;
1876 }
1877 }
1878 if (hasMaximumLiveCalls(phAcc.getId())) {
1879 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1880 // have to change.
1881 Call liveCall = getFirstCallWithState(phAcc.getId(), call, LIVE_CALL_STATES);
1882
1883 if (hasMaximumHoldingCalls(phAcc.getId())) {
1884 // There is no more room for any more calls, unless it's an emergency.
1885 return false; // No more room!
1886 }
1887 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1888 return true;
1889 }
1890 // Try to hold the live call before attempting the new outgoing call.
1891 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
1892 liveCall.hold();
1893 return true;
1894 }
1895
1896 // The live call cannot be held so we're out of luck here. There's no room.
1897 return false;
1898 }
1899 return true;
1900 }
1901
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001902 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07001903 * Given a call, find the first non-null phone account handle of its children.
1904 *
1905 * @param parentCall The parent call.
1906 * @return The first non-null phone account handle of the children, or {@code null} if none.
1907 */
1908 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
1909 for (Call childCall : parentCall.getChildCalls()) {
1910 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
1911 if (childPhoneAccount != null) {
1912 return childPhoneAccount;
1913 }
1914 }
1915 return null;
1916 }
1917
1918 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001919 * Checks to see if the call should be on speakerphone and if so, set it.
1920 */
1921 private void maybeMoveToSpeakerPhone(Call call) {
1922 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001923 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001924 call.setStartWithSpeakerphoneOn(false);
1925 }
1926 }
1927
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001928 private void maybeMoveToEarpiece(Call call) {
1929 if (!call.getStartWithSpeakerphoneOn() && !mWiredHeadsetManager.isPluggedIn() &&
1930 !mCallAudioManager.isBluetoothDeviceAvailable()) {
1931 setAudioRoute(CallAudioState.ROUTE_EARPIECE);
1932 }
1933 }
1934
Santos Cordond6782cd2015-04-16 09:57:50 -07001935 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001936 * Creates a new call for an existing connection.
1937 *
1938 * @param callId The id of the new call.
1939 * @param connection The connection information.
1940 * @return The new call.
1941 */
1942 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1943 Call call = new Call(
1944 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001945 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001946 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001947 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001948 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001949 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001950 connection.getHandle() /* handle */,
1951 null /* gatewayInfo */,
1952 null /* connectionManagerPhoneAccount */,
1953 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1954 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001955 false /* isConference */,
1956 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001957
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001958 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1959 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001960 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001961 call.setCallerDisplayName(connection.getCallerDisplayName(),
1962 connection.getCallerDisplayNamePresentation());
1963
1964 call.addListener(this);
1965 addCall(call);
1966
1967 return call;
1968 }
1969
1970 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001971 * Dumps the state of the {@link CallsManager}.
1972 *
1973 * @param pw The {@code IndentingPrintWriter} to write the state to.
1974 */
1975 public void dump(IndentingPrintWriter pw) {
1976 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001977 if (mCalls != null) {
1978 pw.println("mCalls: ");
1979 pw.increaseIndent();
1980 for (Call call : mCalls) {
1981 pw.println(call);
1982 }
1983 pw.decreaseIndent();
1984 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001985 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1986
1987 if (mCallAudioManager != null) {
1988 pw.println("mCallAudioManager:");
1989 pw.increaseIndent();
1990 mCallAudioManager.dump(pw);
1991 pw.decreaseIndent();
1992 }
1993
1994 if (mTtyManager != null) {
1995 pw.println("mTtyManager:");
1996 pw.increaseIndent();
1997 mTtyManager.dump(pw);
1998 pw.decreaseIndent();
1999 }
2000
2001 if (mInCallController != null) {
2002 pw.println("mInCallController:");
2003 pw.increaseIndent();
2004 mInCallController.dump(pw);
2005 pw.decreaseIndent();
2006 }
2007
2008 if (mConnectionServiceRepository != null) {
2009 pw.println("mConnectionServiceRepository:");
2010 pw.increaseIndent();
2011 mConnectionServiceRepository.dump(pw);
2012 pw.decreaseIndent();
2013 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002014 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05302015
2016 private final Handler mLchHandler = new LchHandler();
2017 private final class LchHandler extends Handler {
2018 @Override
2019 public void handleMessage(Message msg) {
2020 switch (msg.what) {
2021 case PHONE_START_DSDA_INCALL_TONE:
2022 Log.d(this, "Start DSDA incall tones...");
2023 startDsdaInCallTones();
2024 break;
2025 case LCH_PLAY_DTMF:
2026 playLchDtmf();
2027 break;
2028 case LCH_STOP_DTMF:
2029 stopLchDtmf();
2030 break;
2031 }
2032 }
2033 }
2034
2035 public void switchToOtherActiveSub(String subId) {
2036 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2037 != TelephonyManager.MultiSimVariants.DSDA) {
2038 return;
2039 }
2040 Log.d(this, "switchToOtherActiveSub sub:" + subId);
2041 setActiveSubscription(subId);
2042 updateLchStatus(subId);
2043 manageDsdaInCallTones(true);
2044 updateForegroundCall();
2045 }
2046
2047 public synchronized void setActiveSubscription(String subId) {
2048 Log.d(this, "setActiveSubscription = " + subId);
2049 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2050 != TelephonyManager.MultiSimVariants.DSDA) {
2051 return;
2052 }
2053 boolean changed = false;
2054 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2055 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2056 if (subId != null && subId.equals(ph.getId())
2057 && (phAcc != null) && !phAcc.isSet(PhoneAccount.ACTIVE)) {
2058 changed = true;
2059 phAcc.setBit(PhoneAccount.ACTIVE);
2060 } else if (subId != null && !subId.equals(ph.getId())
2061 && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2062 changed = true;
2063 phAcc.unSetBit(PhoneAccount.ACTIVE);
2064 } else if ((subId == null) && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2065 phAcc.unSetBit(PhoneAccount.ACTIVE);
2066 }
2067 }
2068 if (!changed) {
2069 Log.d(this, "setActiveSubscription not changed ");
2070 return;
2071 } else {
2072 Log.d(this, "setActiveSubscription changed " );
2073 for (Call call : mCalls) {
2074 PhoneAccountHandle ph = call.getTargetPhoneAccount();
2075 if (ph != null) {
2076 call.mIsActiveSub = ph.getId().equals(subId) ? true : false;
2077 }
2078 for (CallsManagerListener listener : mListeners) {
2079 listener.onCallStateChanged(call, call.getState(), call.getState());
2080 }
2081 }
2082 }
2083 }
2084
2085 public String getActiveSubscription() {
2086 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2087 if (getPhoneAccountRegistrar()
2088 .getPhoneAccount(ph).isSet(PhoneAccount.ACTIVE)) {
2089 Log.d(this, "getActiveSubscription: " + ph.getId());
2090 return ph.getId();
2091 }
2092 }
2093 return null;
2094 }
2095
2096 private String getConversationSub() {
2097 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2098 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2099 if (phAcc != null && !phAcc.isSet(PhoneAccount.LCH) &&
2100 (getFirstCallWithState(ph.getId(), CallState.ACTIVE, CallState.DIALING,
2101 CallState.ON_HOLD) != null)) {
2102 Log.d(this, "getConversationSub: " + ph.getId());
2103 return ph.getId();
2104 }
2105 }
2106 return null;
2107 }
2108
2109 void manageDsdaInCallTones(boolean isSubSwitch) {
2110 Log.d(this, " entered manageDsdaInCallTones ");
2111
2112 // If there is no background active subscription available, stop playing the tones.
2113 // Do not start/stop LCH/SCH tones when phone is in RINGING state.
2114 if (getLchSub() != null && !hasRingingCall()) {
2115 //If sub switch happens re-start the tones with a delay of 100msec.
2116 if (isSubSwitch) {
2117 Log.d(this, " manageDsdaInCallTones: re-start playing tones, lch sub = "
2118 + getLchSub());
2119 reStartDsdaInCallTones();
2120 } else {
2121 Log.d(this, " entered manageDsdaInCallTones ");
2122 startDsdaInCallTones();
2123 }
2124 } else if (getLchSub() == null) {
2125 // if there is no sub in Lch state, then stop playing the tones
2126 stopMSimInCallTones();
2127 }
2128 }
2129
2130 private void reStartDsdaInCallTones() {
2131 Log.d(this, " reStartDsdaInCallTones");
2132 stopMSimInCallTones();
2133 /* Remove any pending PHONE_START_DSDA_INCALL_TONE messages from queue */
2134 mLchHandler.removeMessages(PHONE_START_DSDA_INCALL_TONE);
2135 Message message = Message.obtain(mLchHandler, PHONE_START_DSDA_INCALL_TONE);
2136 mLchHandler.sendMessageDelayed(message, 100);
2137 }
2138
2139 /**
2140 * Returns the first call that it finds with the given states for given subscription.
2141 * The states are treated as having priority order so that any call with the first
2142 * state will be returned before any call with states listed later in the parameter list.
2143 */
2144 Call getFirstCallWithState(String sub, int... states) {
2145 for (int currentState : states) {
2146 // check the foreground first
2147 if (mForegroundCall != null && mForegroundCall.getState() == currentState
2148 && (mForegroundCall.getTargetPhoneAccount() != null)
2149 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(),
2150 sub)) {
2151 return mForegroundCall;
2152 }
2153
2154 for (Call call : mCalls) {
2155 if ((currentState == call.getState()) &&
2156 (call.getTargetPhoneAccount() != null) &&
2157 (isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), sub))) {
2158 return call;
2159 }
2160 }
2161 }
2162 return null;
2163 }
2164
2165 private String getLchSub() {
2166 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2167 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2168 if (phAcc != null && phAcc.isSet(PhoneAccount.LCH)) {
2169 return ph.getId();
2170 }
2171 }
2172 return null;
2173 }
2174
2175 private void playLchDtmf() {
2176 if (mLchSub != null || mLchHandler.hasMessages(LCH_PLAY_DTMF)) {
2177 // Ignore any redundant requests to start playing tones
2178 return;
2179 }
2180 mLchSub = getLchSub();
2181 Log.d(this, " playLchDtmf... lch sub " + mLchSub);
2182 if (mLchSub == null) return;
2183 removeAnyPendingDtmfMsgs();
2184 char c = mContext.getResources()
2185 .getString(R.string.lch_dtmf_key).charAt(0);
2186 Call call = getNonRingingLiveCall(mLchSub);
2187 if (call == null) {
2188 mLchSub = null;
2189 return;
2190 }
2191 call.playDtmfTone(c);
2192 // Keep playing LCH DTMF tone to remote party on LCH call, with periodicity
2193 // "LCH_DTMF_PERIODICITY" until call moves out of LCH.
2194 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_PLAY_DTMF),
2195 LCH_DTMF_PERIODICITY);
2196 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_STOP_DTMF), LCH_DTMF_PERIOD);
2197 }
2198
2199 private Call getNonRingingLiveCall(String subId) {
2200 return getFirstCallWithState(subId, CallState.DIALING,
2201 CallState.ACTIVE, CallState.ON_HOLD);
2202 }
2203
2204 private void stopLchDtmf() {
2205 if (mLchSub != null) {
2206 // Ignore any redundant requests to stop playing tones
2207 Call call = getNonRingingLiveCall(mLchSub);
2208 Log.d(this, " stopLchDtmf... call: " + call + " mLchSub:" + mLchSub);
2209 if (call == null) {
2210 mLchSub = null;
2211 return;
2212 }
2213 call.stopDtmfTone();
2214 }
2215 mLchSub = null;
2216 }
2217
2218 private void startDsdaInCallTones() {
2219 if (mLocalCallReminderTonePlayer == null) {
2220 Log.d(this, " Play local call hold reminder tone ");
2221 mLocalCallReminderTonePlayer =
2222 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_HOLD_RECALL);
2223 mLocalCallReminderTonePlayer.start();
2224 }
2225 if (sSupervisoryCallHoldToneConfig.equals("inband")) {
2226 // if "persist.radio.sch_tone" is set to "inband", play inband supervisory
2227 // call hold tone. if set to "dtmf", play the SCH tones
2228 // over DTMF, don't play SCH tones for anyother value.
2229 if (mSupervisoryCallHoldTonePlayer == null) {
2230 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone ");
2231 mSupervisoryCallHoldTonePlayer =
2232 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_SUPERVISORY_CH);
2233 mSupervisoryCallHoldTonePlayer.start();
2234 }
2235 } else if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2236 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone over dtmf ");
2237 playLchDtmf();
2238 }
2239 }
2240
2241 private void removeAnyPendingDtmfMsgs() {
2242 mLchHandler.removeMessages(LCH_PLAY_DTMF);
2243 mLchHandler.removeMessages(LCH_STOP_DTMF);
2244 }
2245
2246 protected void stopMSimInCallTones() {
2247 if (mLocalCallReminderTonePlayer != null) {
2248 Log.d(this, " stopMSimInCallTones: local call hold reminder tone ");
2249 mLocalCallReminderTonePlayer.stopTone();
2250 mLocalCallReminderTonePlayer = null;
2251 }
2252 if (mSupervisoryCallHoldTonePlayer != null) {
2253 Log.d(this, " stopMSimInCallTones: Supervisory call hold tone ");
2254 mSupervisoryCallHoldTonePlayer.stopTone();
2255 mSupervisoryCallHoldTonePlayer = null;
2256 }
2257 if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2258 Log.d(this, " stopMSimInCallTones: stop SCH Dtmf call hold tone ");
2259 stopLchDtmf();
2260 /* Remove any previous dtmf nssages from queue */
2261 removeAnyPendingDtmfMsgs();
2262 }
2263 }
2264
2265 /**
2266 * Update the local call hold state for all subscriptions
2267 * 1 -- if call on local hold, 0 -- if call is not on local hold
2268 *
2269 * @param subInConversation is the sub user is currently active in subsription.
2270 * so if this sub is in LCH, then bring that sub out of LCH.
2271 */
2272 private void updateLchStatus(String subInConversation) {
2273 Call removeFromLch = null;
2274 Log.d(this, "updateLchStatus subInConversation: " + subInConversation);
2275 if (subInConversation != null && subInConversation.contains("sip")) {
2276 return;
2277 }
2278 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2279 String sub = ph.getId();
2280 if (sub != null && sub.contains("sip")) {
2281 Log.d(this, "update lch. Skipping account: " + sub);
2282 continue;
2283 }
2284 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2285 boolean lchState = false;
2286 if (subInConversation != null && hasActiveOrHoldingCall(sub) &&
2287 !sub.equals(subInConversation)) {
2288 // if sub is not conversation sub and if it has an active
2289 // voice call then update lchStatus as Active
2290 lchState = true;
2291 }
2292 // Update state only if the new state is different
2293 if ((phAcc != null) && (lchState != phAcc.isSet(PhoneAccount.LCH))) {
2294 Call call = getNonRingingLiveCall(sub);
2295 Log.d(this, " setLocal Call Hold to = " + lchState + " sub:" + sub);
2296
2297 if (call != null) {
2298 if (call.getChildCalls().size() > 1) {
2299 // Since for a conference call corresponding telephonyconnection doesn't
2300 // exist, so send lch request on its child call
2301 call = call.getChildCalls().get(0);
2302 }
2303 if (lchState) {
2304 call.setLocalCallHold(true);
2305 } else {
2306 removeFromLch = call;
2307 }
2308 }
2309 if (lchState) {
2310 phAcc.setBit(PhoneAccount.LCH);
2311 } else {
2312 phAcc.unSetBit(PhoneAccount.LCH);
2313 }
2314 }
2315 }
2316 if (removeFromLch != null) {
2317 // Ensure to send LCH disable request at last, to make sure that during switch
2318 // subscription, both subscriptions not to be in active(non-LCH) at any moment.
2319 removeFromLch.setLocalCallHold(false);
2320 }
2321 }
cqiang72e31202015-09-14 17:04:19 +08002322
2323 void resetCdmaConnectionTime(Call call) {
2324 call.setConnectTimeMillis(System.currentTimeMillis());
2325 if (mCalls.contains(call)) {
2326 for (CallsManagerListener listener : mListeners) {
2327 listener.onCallStateChanged(call, CallState.ACTIVE, CallState.ACTIVE);
2328 }
2329 updateForegroundCall();
2330 }
2331 }
Ravindra205cb782015-10-07 16:57:53 +05302332
2333 void onMergeFailed(Call call) {
2334 if (mCalls.contains(call)) {
2335 for (CallsManagerListener listener : mListeners) {
2336 listener.onMergeFailed(call);
2337 }
2338 }
2339 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08002340}