blob: c383745934c9862732c79f71bb4afd548838097c [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
Shriram Ganeshd7dd7272015-12-11 15:52:15 -08001316 int getNumTopLevelCalls() {
1317 int count = 0;
1318 for (Call call : mCalls) {
1319 if (call.getParentCall() == null) {
1320 count++;
1321 }
1322 }
1323 return count;
1324 }
1325
Roshan Pius93c9c012015-08-28 11:10:56 -07001326 Call getOutgoingCall() {
1327 return getFirstCallWithState(OUTGOING_CALL_STATES);
1328 }
1329
Santos Cordonf193ba42014-09-12 06:37:39 -07001330 Call getFirstCallWithState(int... states) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301331 return getFirstCallWithState((Call) null, states);
Santos Cordonf193ba42014-09-12 06:37:39 -07001332 }
1333
Santos Cordon10838c22014-06-11 17:36:04 -07001334 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001335 * Returns the first call that it finds with the given states. The states are treated as having
1336 * priority order so that any call with the first state will be returned before any call with
1337 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001338 *
1339 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001340 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001341 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001342 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001343 // check the foreground first
1344 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1345 return mForegroundCall;
1346 }
1347
Santos Cordondeb8c892014-05-30 01:38:03 -07001348 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001349 if (Objects.equals(callToSkip, call)) {
1350 continue;
1351 }
1352
1353 // Only operate on top-level calls
1354 if (call.getParentCall() != null) {
1355 continue;
1356 }
1357
Santos Cordondeb8c892014-05-30 01:38:03 -07001358 if (currentState == call.getState()) {
1359 return call;
1360 }
1361 }
1362 }
1363 return null;
1364 }
1365
Sandeep Kunta39b77672014-09-01 17:22:57 +05301366 /**
1367 * Returns the first call that it finds with the given states for given subscription.
1368 * the states are treated as having priority order so that any call with the first
1369 * state will be returned before any call with states listed later in the parameter list.
1370 *
1371 * @param subId check calls only on this subscription
1372 * @param callToSkip Call that this method should skip while searching
1373 */
1374 Call getFirstCallWithState(String subId, Call callToSkip, int... states) {
1375 for (int currentState : states) {
1376 // check the foreground first
1377 if (mForegroundCall != null && mForegroundCall.getState() == currentState
1378 && mForegroundCall.getTargetPhoneAccount() != null
1379 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(), subId)) {
1380 return mForegroundCall;
1381 }
1382
1383 for (Call call : mCalls) {
1384 if (Objects.equals(callToSkip, call)) {
1385 continue;
1386 }
1387
1388 // Only operate on top-level calls
1389 if (call.getParentCall() != null) {
1390 continue;
1391 }
1392
1393 if (currentState == call.getState() && call.getTargetPhoneAccount() != null
1394 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1395 return call;
1396 }
1397 }
1398 }
1399 return null;
1400 }
1401
Santos Cordon0fbe6322014-08-14 04:04:25 -07001402 Call createConferenceCall(
1403 PhoneAccountHandle phoneAccount,
1404 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001405
1406 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1407 // which is the default value for new Calls.
1408 long connectTime =
1409 parcelableConference.getConnectTimeMillis() ==
1410 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1411 parcelableConference.getConnectTimeMillis();
1412
Santos Cordon0fbe6322014-08-14 04:04:25 -07001413 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001414 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001415 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001416 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001417 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001418 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001419 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001420 null /* handle */,
1421 null /* gatewayInfo */,
1422 null /* connectionManagerPhoneAccount */,
1423 phoneAccount,
1424 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001425 true /* isConference */,
1426 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001427
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001428 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1429 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001430 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001431 call.setVideoState(parcelableConference.getVideoState());
1432 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001433 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001434 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001435
1436 // TODO: Move this to be a part of addCall()
1437 call.addListener(this);
1438 addCall(call);
1439 return call;
1440 }
1441
Yorke Leef86db2e2014-09-12 17:56:48 -07001442 /**
1443 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1444 */
1445 int getCallState() {
1446 return mPhoneStateBroadcaster.getCallState();
1447 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001448
Santos Cordon4b2c1192014-03-19 18:15:38 -07001449 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001450 * Retrieves the {@link PhoneAccountRegistrar}.
1451 *
1452 * @return The {@link PhoneAccountRegistrar}.
1453 */
1454 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1455 return mPhoneAccountRegistrar;
1456 }
1457
1458 /**
1459 * Retrieves the {@link MissedCallNotifier}
1460 * @return The {@link MissedCallNotifier}.
1461 */
1462 MissedCallNotifier getMissedCallNotifier() {
1463 return mMissedCallNotifier;
1464 }
1465
1466 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001467 * Adds the specified call to the main list of live calls.
1468 *
1469 * @param call The call to add.
1470 */
1471 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001472 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001473 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001474 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001475 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001476
Santos Cordondf399862014-08-06 04:39:15 -07001477 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001478 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001479 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001480 if (Log.SYSTRACE_DEBUG) {
1481 Trace.beginSection(listener.getClass().toString() + " addCall");
1482 }
Santos Cordona56f2762014-03-24 15:55:53 -07001483 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001484 if (Log.SYSTRACE_DEBUG) {
1485 Trace.endSection();
1486 }
Santos Cordona56f2762014-03-24 15:55:53 -07001487 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001488 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001489 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001490 }
1491
Sailesh Nepal810735e2014-03-18 18:15:46 -07001492 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001493 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001494 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001495
Santos Cordon672321e2014-08-21 14:38:58 -07001496 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001497 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001498 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001499
1500 boolean shouldNotify = false;
1501 if (mCalls.contains(call)) {
1502 mCalls.remove(call);
1503 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001504 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001505
Santos Cordon2685dab2015-04-17 17:12:09 -07001506 call.destroy();
1507
Sailesh Nepale59bb192014-04-01 18:33:59 -07001508 // Only broadcast changes for calls that are being tracked.
1509 if (shouldNotify) {
1510 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001511 if (Log.SYSTRACE_DEBUG) {
1512 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1513 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001514 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001515 if (Log.SYSTRACE_DEBUG) {
1516 Trace.endSection();
1517 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001518 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001519 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001520 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001521 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001522 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001523
Sailesh Nepal810735e2014-03-18 18:15:46 -07001524 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001525 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001526 *
1527 * @param call The call.
1528 * @param newState The new state of the call.
1529 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001530 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001531 if (call == null) {
1532 return;
1533 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001534 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001535 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1536 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001537 if (newState != oldState) {
1538 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1539 // 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 -07001540 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001541 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001542 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001543 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001544 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001545 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001546
Yorke Leee4a9c412014-11-14 16:59:42 -08001547 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001548 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001549 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001550 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001551 if (Log.SYSTRACE_DEBUG) {
1552 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1553 }
Santos Cordona56f2762014-03-24 15:55:53 -07001554 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001555 if (Log.SYSTRACE_DEBUG) {
1556 Trace.endSection();
1557 }
Santos Cordona56f2762014-03-24 15:55:53 -07001558 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001559 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001560 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001561 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001562 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301563 manageDsdaInCallTones(false);
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001564 }
1565
1566 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001567 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001568 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001569 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001570 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001571 Call newForegroundCall = null;
Santos Cordondf399862014-08-06 04:39:15 -07001572 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001573 // of its state will be foreground by default and instead the connection service should
1574 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001575 // the call should play audio and listen to microphone if it wants.
1576
Sandeep Kunta39b77672014-09-01 17:22:57 +05301577 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1578 == TelephonyManager.MultiSimVariants.DSDA) {
1579 String lchSub = getLchSub();
1580 for (Call call : mCalls) {
1581 // Only top-level calls can be in foreground
1582 if (call.getParentCall() != null) {
1583 continue;
1584 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001585
Sandeep Kunta39b77672014-09-01 17:22:57 +05301586 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1587 if (ph != null && ph.getId().equals(lchSub)) continue;
1588 // Active calls have priority.
1589 if (call.isActive()) {
1590 newForegroundCall = call;
1591 break;
1592 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001593
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001594 // If only call in call list is held call it's also a foreground call
Shriram Ganeshd7dd7272015-12-11 15:52:15 -08001595 if (getNumTopLevelCalls() == 1 && call.getState() == CallState.ON_HOLD) {
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001596 newForegroundCall = call;
1597 }
1598
Shriram Ganesh74254492015-10-26 10:36:19 -07001599 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
1600 || call.getState() == CallState.RINGING) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301601 newForegroundCall = call;
1602 // Don't break in case there's an active call that has priority.
1603 }
1604 }
1605 // if active sub doesn't have any calls, then consider calls on all subs,
1606 // which ever call is active set that as foreground call. give more priority
1607 // to ringing call on LCH sub over active call.
1608 if (newForegroundCall == null) {
1609 newForegroundCall = getFirstCallWithState(CallState.RINGING);
1610 if (newForegroundCall == null) {
1611 newForegroundCall = getFirstCallWithState(CallState.ACTIVE);
1612 }
1613 }
1614 } else {
1615 for (Call call : mCalls) {
1616 // Only top-level calls can be in foreground
1617 if (call.getParentCall() != null) {
1618 continue;
1619 }
1620
1621 // Active calls have priority.
1622 if (call.isActive()) {
1623 newForegroundCall = call;
1624 break;
1625 }
1626
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001627 // If only call in call list is held call it's also a foreground call
Shriram Ganeshd7dd7272015-12-11 15:52:15 -08001628 if (getNumTopLevelCalls() == 1 && call.getState() == CallState.ON_HOLD) {
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001629 newForegroundCall = call;
1630 }
1631
Shriram Ganesh74254492015-10-26 10:36:19 -07001632 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
1633 || call.getState() == CallState.RINGING) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301634 newForegroundCall = call;
1635 // Don't break in case there's an active call that has priority.
1636 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001637 }
1638 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001639
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001640 /* In the case where there are 2 calls, when the Hold button is pressed for active
1641 * call, as an intermediate state we would have both calls in held state before
1642 * the background call is moved to active state. In such an intermediate stage
1643 * updating the newForegroundCall to null causes issues with abandoning audio
1644 * focus. Skip updating the foreground call with null in such cases. */
1645 if (newForegroundCall == null && getFirstCallWithState(CallState.ON_HOLD) != null) {
1646 Log.v(this, "Skip updating foreground call in intermediate stage");
1647 newForegroundCall = mForegroundCall;
1648 }
1649
Sailesh Nepal810735e2014-03-18 18:15:46 -07001650 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001651 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001652 Call oldForegroundCall = mForegroundCall;
1653 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001654
Santos Cordona56f2762014-03-24 15:55:53 -07001655 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001656 if (Log.SYSTRACE_DEBUG) {
1657 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1658 }
Santos Cordona56f2762014-03-24 15:55:53 -07001659 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001660 if (Log.SYSTRACE_DEBUG) {
1661 Trace.endSection();
1662 }
Santos Cordona56f2762014-03-24 15:55:53 -07001663 }
Santos Cordon681663d2014-01-30 04:32:15 -08001664 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001665 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001666 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001667
Santos Cordon91bd74c2014-11-07 16:10:22 -08001668 private void updateCanAddCall() {
1669 boolean newCanAddCall = canAddCall();
1670 if (newCanAddCall != mCanAddCall) {
1671 mCanAddCall = newCanAddCall;
1672 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001673 if (Log.SYSTRACE_DEBUG) {
1674 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1675 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001676 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001677 if (Log.SYSTRACE_DEBUG) {
1678 Trace.endSection();
1679 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001680 }
1681 }
1682 }
1683
1684 private void updateCallsManagerState() {
1685 updateForegroundCall();
1686 updateCanAddCall();
1687 }
1688
Yorke Leeb701f6d2014-08-12 14:04:19 -07001689 private boolean isPotentialMMICode(Uri handle) {
1690 return (handle != null && handle.getSchemeSpecificPart() != null
1691 && handle.getSchemeSpecificPart().contains("#"));
1692 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001693
Tyler Gunn5b452df2014-10-01 15:02:58 -07001694 /**
1695 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1696 * MMI codes which can be dialed when one or more calls are in progress.
1697 * <P>
1698 * Checks for numbers formatted similar to the MMI codes defined in:
1699 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1700 * and
1701 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1702 *
1703 * @param handle The URI to call.
1704 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1705 */
1706 private boolean isPotentialInCallMMICode(Uri handle) {
1707 if (handle != null && handle.getSchemeSpecificPart() != null &&
1708 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1709
1710 String dialedNumber = handle.getSchemeSpecificPart();
1711 return (dialedNumber.equals("0") ||
1712 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1713 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1714 dialedNumber.equals("3") ||
1715 dialedNumber.equals("4") ||
1716 dialedNumber.equals("5"));
1717 }
1718 return false;
1719 }
1720
Santos Cordonf193ba42014-09-12 06:37:39 -07001721 private int getNumCallsWithState(int... states) {
1722 int count = 0;
1723 for (int state : states) {
1724 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001725 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001726 count++;
1727 }
1728 }
1729 }
1730 return count;
1731 }
1732
Sandeep Kunta39b77672014-09-01 17:22:57 +05301733 private int getNumCallsWithState(String subId, int... states) {
1734 int count = 0;
1735 for (int state : states) {
1736 for (Call call : mCalls) {
1737 if (call.getParentCall() == null && call.getState() == state
1738 && call.getTargetPhoneAccount() != null
1739 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1740 count++;
1741 }
1742 }
1743 }
1744 return count;
1745 }
1746
Santos Cordonf193ba42014-09-12 06:37:39 -07001747 private boolean hasMaximumLiveCalls() {
1748 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1749 }
1750
Sandeep Kunta39b77672014-09-01 17:22:57 +05301751 private boolean hasMaximumLiveCalls(String subId) {
1752 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(subId, LIVE_CALL_STATES);
1753 }
1754
Santos Cordonf193ba42014-09-12 06:37:39 -07001755 private boolean hasMaximumHoldingCalls() {
1756 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1757 }
1758
Sandeep Kunta39b77672014-09-01 17:22:57 +05301759 private boolean hasMaximumHoldingCalls(String subId) {
1760 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(subId, CallState.ON_HOLD);
1761 }
1762
Santos Cordonf193ba42014-09-12 06:37:39 -07001763 private boolean hasMaximumRingingCalls() {
1764 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1765 }
1766
Sandeep Kunta39b77672014-09-01 17:22:57 +05301767 private boolean hasMaximumRingingCalls(String subId) {
1768 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(subId, CallState.RINGING);
1769 }
1770
Santos Cordonf193ba42014-09-12 06:37:39 -07001771 private boolean hasMaximumOutgoingCalls() {
1772 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1773 }
1774
Roshan Pius4de4a892015-08-24 11:06:58 -07001775 private boolean hasMaximumDialingCalls() {
1776 return MAXIMUM_DIALING_CALLS <= getNumCallsWithState(CallState.DIALING);
1777 }
1778
Santos Cordonf193ba42014-09-12 06:37:39 -07001779 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301780 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1781 == TelephonyManager.MultiSimVariants.DSDA) {
1782 return makeRoomForOutgoingCallForDsda(call, isEmergency);
1783 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001784 if (hasMaximumLiveCalls()) {
1785 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1786 // have to change.
1787 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301788 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1789 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001790
Santos Cordond1766502014-09-26 15:45:39 -07001791 if (call == liveCall) {
1792 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001793 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001794 // state since the call was already populated into the list.
1795 return true;
1796 }
1797
Andrew Leee6595182014-09-23 18:43:05 -07001798 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001799 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1800 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1801 // Disconnect the current outgoing call if it's not an emergency call. If the
1802 // user tries to make two outgoing calls to different emergency call numbers,
1803 // we will try to connect the first outgoing call.
1804 outgoingCall.disconnect();
1805 return true;
1806 }
1807 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1808 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1809 // state, just disconnect it since the user has explicitly started a new call.
1810 outgoingCall.disconnect();
1811 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001812 }
1813 return false;
1814 }
1815
Santos Cordonf193ba42014-09-12 06:37:39 -07001816 if (hasMaximumHoldingCalls()) {
1817 // There is no more room for any more calls, unless it's an emergency.
1818 if (isEmergency) {
1819 // Kill the current active call, this is easier then trying to disconnect a
1820 // holding call and hold an active call.
1821 liveCall.disconnect();
1822 return true;
1823 }
1824 return false; // No more room!
1825 }
1826
1827 // We have room for at least one more holding call at this point.
1828
Tyler Gunne4cd9162015-08-11 15:36:04 -07001829 // TODO: Remove once b/23035408 has been corrected.
1830 // If the live call is a conference, it will not have a target phone account set. This
1831 // means the check to see if the live call has the same target phone account as the new
1832 // call will not cause us to bail early. As a result, we'll end up holding the
1833 // ongoing conference call. However, the ConnectionService is already doing that. This
1834 // has caused problems with some carriers. As a workaround until b/23035408 is
1835 // corrected, we will try and get the target phone account for one of the conference's
1836 // children and use that instead.
1837 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
1838 if (liveCallPhoneAccount == null && liveCall.isConference() &&
1839 !liveCall.getChildCalls().isEmpty()) {
1840 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
1841 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
1842 liveCallPhoneAccount);
1843 }
1844
Santos Cordonf193ba42014-09-12 06:37:39 -07001845 // First thing, if we are trying to make a call with the same phone account as the live
1846 // call, then allow it so that the connection service can make its own decision about
1847 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07001848 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
1849 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001850 return true;
1851 } else if (call.getTargetPhoneAccount() == null) {
1852 // Without a phone account, we can't say reliably that the call will fail.
1853 // If the user chooses the same phone account as the live call, then it's
1854 // still possible that the call can be made (like with CDMA calls not supporting
1855 // hold but they still support adding a call by going immediately into conference
1856 // mode). Return true here and we'll run this code again after user chooses an
1857 // account.
1858 return true;
1859 }
1860
1861 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001862 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07001863 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001864 liveCall.hold();
1865 return true;
1866 }
1867
1868 // The live call cannot be held so we're out of luck here. There's no room.
1869 return false;
1870 }
1871 return true;
1872 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001873
Sandeep Kunta39b77672014-09-01 17:22:57 +05301874 private boolean makeRoomForOutgoingCallForDsda(Call call, boolean isEmergency) {
1875 if (isEmergency || (call.getState() == CallState.SELECT_PHONE_ACCOUNT)) {
1876 return true;
1877 }
1878
1879 PhoneAccountHandle phAcc = call.getTargetPhoneAccount();
1880 if (phAcc == null) {
1881 if (getNumCallsWithState(LIVE_CALL_STATES) == MAXIMUM_DSDA_LIVE_CALLS
1882 && getNumCallsWithState(CallState.ON_HOLD) == MAXIMUM_DSDA_HOLD_CALLS) {
1883 return false;
1884 } else {
1885 return true;
1886 }
1887 }
1888 if (hasMaximumLiveCalls(phAcc.getId())) {
1889 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1890 // have to change.
1891 Call liveCall = getFirstCallWithState(phAcc.getId(), call, LIVE_CALL_STATES);
1892
1893 if (hasMaximumHoldingCalls(phAcc.getId())) {
1894 // There is no more room for any more calls, unless it's an emergency.
1895 return false; // No more room!
1896 }
1897 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1898 return true;
1899 }
1900 // Try to hold the live call before attempting the new outgoing call.
1901 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
1902 liveCall.hold();
1903 return true;
1904 }
1905
1906 // The live call cannot be held so we're out of luck here. There's no room.
1907 return false;
1908 }
1909 return true;
1910 }
1911
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001912 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07001913 * Given a call, find the first non-null phone account handle of its children.
1914 *
1915 * @param parentCall The parent call.
1916 * @return The first non-null phone account handle of the children, or {@code null} if none.
1917 */
1918 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
1919 for (Call childCall : parentCall.getChildCalls()) {
1920 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
1921 if (childPhoneAccount != null) {
1922 return childPhoneAccount;
1923 }
1924 }
1925 return null;
1926 }
1927
1928 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001929 * Checks to see if the call should be on speakerphone and if so, set it.
1930 */
1931 private void maybeMoveToSpeakerPhone(Call call) {
1932 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001933 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001934 call.setStartWithSpeakerphoneOn(false);
1935 }
1936 }
1937
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001938 private void maybeMoveToEarpiece(Call call) {
1939 if (!call.getStartWithSpeakerphoneOn() && !mWiredHeadsetManager.isPluggedIn() &&
1940 !mCallAudioManager.isBluetoothDeviceAvailable()) {
1941 setAudioRoute(CallAudioState.ROUTE_EARPIECE);
1942 }
1943 }
1944
Santos Cordond6782cd2015-04-16 09:57:50 -07001945 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001946 * Creates a new call for an existing connection.
1947 *
1948 * @param callId The id of the new call.
1949 * @param connection The connection information.
1950 * @return The new call.
1951 */
1952 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1953 Call call = new Call(
1954 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001955 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001956 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001957 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001958 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001959 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001960 connection.getHandle() /* handle */,
1961 null /* gatewayInfo */,
1962 null /* connectionManagerPhoneAccount */,
1963 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1964 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001965 false /* isConference */,
1966 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001967
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001968 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1969 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001970 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001971 call.setCallerDisplayName(connection.getCallerDisplayName(),
1972 connection.getCallerDisplayNamePresentation());
1973
1974 call.addListener(this);
1975 addCall(call);
1976
1977 return call;
1978 }
1979
1980 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001981 * Dumps the state of the {@link CallsManager}.
1982 *
1983 * @param pw The {@code IndentingPrintWriter} to write the state to.
1984 */
1985 public void dump(IndentingPrintWriter pw) {
1986 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001987 if (mCalls != null) {
1988 pw.println("mCalls: ");
1989 pw.increaseIndent();
1990 for (Call call : mCalls) {
1991 pw.println(call);
1992 }
1993 pw.decreaseIndent();
1994 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001995 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1996
1997 if (mCallAudioManager != null) {
1998 pw.println("mCallAudioManager:");
1999 pw.increaseIndent();
2000 mCallAudioManager.dump(pw);
2001 pw.decreaseIndent();
2002 }
2003
2004 if (mTtyManager != null) {
2005 pw.println("mTtyManager:");
2006 pw.increaseIndent();
2007 mTtyManager.dump(pw);
2008 pw.decreaseIndent();
2009 }
2010
2011 if (mInCallController != null) {
2012 pw.println("mInCallController:");
2013 pw.increaseIndent();
2014 mInCallController.dump(pw);
2015 pw.decreaseIndent();
2016 }
2017
2018 if (mConnectionServiceRepository != null) {
2019 pw.println("mConnectionServiceRepository:");
2020 pw.increaseIndent();
2021 mConnectionServiceRepository.dump(pw);
2022 pw.decreaseIndent();
2023 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002024 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05302025
2026 private final Handler mLchHandler = new LchHandler();
2027 private final class LchHandler extends Handler {
2028 @Override
2029 public void handleMessage(Message msg) {
2030 switch (msg.what) {
2031 case PHONE_START_DSDA_INCALL_TONE:
2032 Log.d(this, "Start DSDA incall tones...");
2033 startDsdaInCallTones();
2034 break;
2035 case LCH_PLAY_DTMF:
2036 playLchDtmf();
2037 break;
2038 case LCH_STOP_DTMF:
2039 stopLchDtmf();
2040 break;
2041 }
2042 }
2043 }
2044
2045 public void switchToOtherActiveSub(String subId) {
2046 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2047 != TelephonyManager.MultiSimVariants.DSDA) {
2048 return;
2049 }
2050 Log.d(this, "switchToOtherActiveSub sub:" + subId);
2051 setActiveSubscription(subId);
2052 updateLchStatus(subId);
2053 manageDsdaInCallTones(true);
2054 updateForegroundCall();
2055 }
2056
2057 public synchronized void setActiveSubscription(String subId) {
2058 Log.d(this, "setActiveSubscription = " + subId);
2059 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2060 != TelephonyManager.MultiSimVariants.DSDA) {
2061 return;
2062 }
2063 boolean changed = false;
2064 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2065 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2066 if (subId != null && subId.equals(ph.getId())
2067 && (phAcc != null) && !phAcc.isSet(PhoneAccount.ACTIVE)) {
2068 changed = true;
2069 phAcc.setBit(PhoneAccount.ACTIVE);
2070 } else if (subId != null && !subId.equals(ph.getId())
2071 && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2072 changed = true;
2073 phAcc.unSetBit(PhoneAccount.ACTIVE);
2074 } else if ((subId == null) && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2075 phAcc.unSetBit(PhoneAccount.ACTIVE);
2076 }
2077 }
2078 if (!changed) {
2079 Log.d(this, "setActiveSubscription not changed ");
2080 return;
2081 } else {
2082 Log.d(this, "setActiveSubscription changed " );
2083 for (Call call : mCalls) {
2084 PhoneAccountHandle ph = call.getTargetPhoneAccount();
2085 if (ph != null) {
2086 call.mIsActiveSub = ph.getId().equals(subId) ? true : false;
2087 }
2088 for (CallsManagerListener listener : mListeners) {
2089 listener.onCallStateChanged(call, call.getState(), call.getState());
2090 }
2091 }
2092 }
2093 }
2094
2095 public String getActiveSubscription() {
2096 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2097 if (getPhoneAccountRegistrar()
2098 .getPhoneAccount(ph).isSet(PhoneAccount.ACTIVE)) {
2099 Log.d(this, "getActiveSubscription: " + ph.getId());
2100 return ph.getId();
2101 }
2102 }
2103 return null;
2104 }
2105
2106 private String getConversationSub() {
2107 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2108 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2109 if (phAcc != null && !phAcc.isSet(PhoneAccount.LCH) &&
2110 (getFirstCallWithState(ph.getId(), CallState.ACTIVE, CallState.DIALING,
2111 CallState.ON_HOLD) != null)) {
2112 Log.d(this, "getConversationSub: " + ph.getId());
2113 return ph.getId();
2114 }
2115 }
2116 return null;
2117 }
2118
2119 void manageDsdaInCallTones(boolean isSubSwitch) {
2120 Log.d(this, " entered manageDsdaInCallTones ");
2121
2122 // If there is no background active subscription available, stop playing the tones.
2123 // Do not start/stop LCH/SCH tones when phone is in RINGING state.
2124 if (getLchSub() != null && !hasRingingCall()) {
2125 //If sub switch happens re-start the tones with a delay of 100msec.
2126 if (isSubSwitch) {
2127 Log.d(this, " manageDsdaInCallTones: re-start playing tones, lch sub = "
2128 + getLchSub());
2129 reStartDsdaInCallTones();
2130 } else {
2131 Log.d(this, " entered manageDsdaInCallTones ");
2132 startDsdaInCallTones();
2133 }
2134 } else if (getLchSub() == null) {
2135 // if there is no sub in Lch state, then stop playing the tones
2136 stopMSimInCallTones();
2137 }
2138 }
2139
2140 private void reStartDsdaInCallTones() {
2141 Log.d(this, " reStartDsdaInCallTones");
2142 stopMSimInCallTones();
2143 /* Remove any pending PHONE_START_DSDA_INCALL_TONE messages from queue */
2144 mLchHandler.removeMessages(PHONE_START_DSDA_INCALL_TONE);
2145 Message message = Message.obtain(mLchHandler, PHONE_START_DSDA_INCALL_TONE);
2146 mLchHandler.sendMessageDelayed(message, 100);
2147 }
2148
2149 /**
2150 * Returns the first call that it finds with the given states for given subscription.
2151 * The states are treated as having priority order so that any call with the first
2152 * state will be returned before any call with states listed later in the parameter list.
2153 */
2154 Call getFirstCallWithState(String sub, int... states) {
2155 for (int currentState : states) {
2156 // check the foreground first
2157 if (mForegroundCall != null && mForegroundCall.getState() == currentState
2158 && (mForegroundCall.getTargetPhoneAccount() != null)
2159 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(),
2160 sub)) {
2161 return mForegroundCall;
2162 }
2163
2164 for (Call call : mCalls) {
2165 if ((currentState == call.getState()) &&
2166 (call.getTargetPhoneAccount() != null) &&
2167 (isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), sub))) {
2168 return call;
2169 }
2170 }
2171 }
2172 return null;
2173 }
2174
2175 private String getLchSub() {
2176 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2177 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2178 if (phAcc != null && phAcc.isSet(PhoneAccount.LCH)) {
2179 return ph.getId();
2180 }
2181 }
2182 return null;
2183 }
2184
2185 private void playLchDtmf() {
2186 if (mLchSub != null || mLchHandler.hasMessages(LCH_PLAY_DTMF)) {
2187 // Ignore any redundant requests to start playing tones
2188 return;
2189 }
2190 mLchSub = getLchSub();
2191 Log.d(this, " playLchDtmf... lch sub " + mLchSub);
2192 if (mLchSub == null) return;
2193 removeAnyPendingDtmfMsgs();
2194 char c = mContext.getResources()
2195 .getString(R.string.lch_dtmf_key).charAt(0);
2196 Call call = getNonRingingLiveCall(mLchSub);
2197 if (call == null) {
2198 mLchSub = null;
2199 return;
2200 }
2201 call.playDtmfTone(c);
2202 // Keep playing LCH DTMF tone to remote party on LCH call, with periodicity
2203 // "LCH_DTMF_PERIODICITY" until call moves out of LCH.
2204 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_PLAY_DTMF),
2205 LCH_DTMF_PERIODICITY);
2206 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_STOP_DTMF), LCH_DTMF_PERIOD);
2207 }
2208
2209 private Call getNonRingingLiveCall(String subId) {
2210 return getFirstCallWithState(subId, CallState.DIALING,
2211 CallState.ACTIVE, CallState.ON_HOLD);
2212 }
2213
2214 private void stopLchDtmf() {
2215 if (mLchSub != null) {
2216 // Ignore any redundant requests to stop playing tones
2217 Call call = getNonRingingLiveCall(mLchSub);
2218 Log.d(this, " stopLchDtmf... call: " + call + " mLchSub:" + mLchSub);
2219 if (call == null) {
2220 mLchSub = null;
2221 return;
2222 }
2223 call.stopDtmfTone();
2224 }
2225 mLchSub = null;
2226 }
2227
2228 private void startDsdaInCallTones() {
2229 if (mLocalCallReminderTonePlayer == null) {
2230 Log.d(this, " Play local call hold reminder tone ");
2231 mLocalCallReminderTonePlayer =
2232 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_HOLD_RECALL);
2233 mLocalCallReminderTonePlayer.start();
2234 }
2235 if (sSupervisoryCallHoldToneConfig.equals("inband")) {
2236 // if "persist.radio.sch_tone" is set to "inband", play inband supervisory
2237 // call hold tone. if set to "dtmf", play the SCH tones
2238 // over DTMF, don't play SCH tones for anyother value.
2239 if (mSupervisoryCallHoldTonePlayer == null) {
2240 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone ");
2241 mSupervisoryCallHoldTonePlayer =
2242 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_SUPERVISORY_CH);
2243 mSupervisoryCallHoldTonePlayer.start();
2244 }
2245 } else if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2246 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone over dtmf ");
2247 playLchDtmf();
2248 }
2249 }
2250
2251 private void removeAnyPendingDtmfMsgs() {
2252 mLchHandler.removeMessages(LCH_PLAY_DTMF);
2253 mLchHandler.removeMessages(LCH_STOP_DTMF);
2254 }
2255
2256 protected void stopMSimInCallTones() {
2257 if (mLocalCallReminderTonePlayer != null) {
2258 Log.d(this, " stopMSimInCallTones: local call hold reminder tone ");
2259 mLocalCallReminderTonePlayer.stopTone();
2260 mLocalCallReminderTonePlayer = null;
2261 }
2262 if (mSupervisoryCallHoldTonePlayer != null) {
2263 Log.d(this, " stopMSimInCallTones: Supervisory call hold tone ");
2264 mSupervisoryCallHoldTonePlayer.stopTone();
2265 mSupervisoryCallHoldTonePlayer = null;
2266 }
2267 if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2268 Log.d(this, " stopMSimInCallTones: stop SCH Dtmf call hold tone ");
2269 stopLchDtmf();
2270 /* Remove any previous dtmf nssages from queue */
2271 removeAnyPendingDtmfMsgs();
2272 }
2273 }
2274
2275 /**
2276 * Update the local call hold state for all subscriptions
2277 * 1 -- if call on local hold, 0 -- if call is not on local hold
2278 *
2279 * @param subInConversation is the sub user is currently active in subsription.
2280 * so if this sub is in LCH, then bring that sub out of LCH.
2281 */
2282 private void updateLchStatus(String subInConversation) {
2283 Call removeFromLch = null;
2284 Log.d(this, "updateLchStatus subInConversation: " + subInConversation);
2285 if (subInConversation != null && subInConversation.contains("sip")) {
2286 return;
2287 }
2288 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2289 String sub = ph.getId();
2290 if (sub != null && sub.contains("sip")) {
2291 Log.d(this, "update lch. Skipping account: " + sub);
2292 continue;
2293 }
2294 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2295 boolean lchState = false;
2296 if (subInConversation != null && hasActiveOrHoldingCall(sub) &&
2297 !sub.equals(subInConversation)) {
2298 // if sub is not conversation sub and if it has an active
2299 // voice call then update lchStatus as Active
2300 lchState = true;
2301 }
2302 // Update state only if the new state is different
2303 if ((phAcc != null) && (lchState != phAcc.isSet(PhoneAccount.LCH))) {
2304 Call call = getNonRingingLiveCall(sub);
2305 Log.d(this, " setLocal Call Hold to = " + lchState + " sub:" + sub);
2306
2307 if (call != null) {
2308 if (call.getChildCalls().size() > 1) {
2309 // Since for a conference call corresponding telephonyconnection doesn't
2310 // exist, so send lch request on its child call
2311 call = call.getChildCalls().get(0);
2312 }
2313 if (lchState) {
2314 call.setLocalCallHold(true);
2315 } else {
2316 removeFromLch = call;
2317 }
2318 }
2319 if (lchState) {
2320 phAcc.setBit(PhoneAccount.LCH);
2321 } else {
2322 phAcc.unSetBit(PhoneAccount.LCH);
2323 }
2324 }
2325 }
2326 if (removeFromLch != null) {
2327 // Ensure to send LCH disable request at last, to make sure that during switch
2328 // subscription, both subscriptions not to be in active(non-LCH) at any moment.
2329 removeFromLch.setLocalCallHold(false);
2330 }
2331 }
cqiang72e31202015-09-14 17:04:19 +08002332
2333 void resetCdmaConnectionTime(Call call) {
2334 call.setConnectTimeMillis(System.currentTimeMillis());
2335 if (mCalls.contains(call)) {
2336 for (CallsManagerListener listener : mListeners) {
2337 listener.onCallStateChanged(call, CallState.ACTIVE, CallState.ACTIVE);
2338 }
2339 updateForegroundCall();
2340 }
2341 }
Ravindra205cb782015-10-07 16:57:53 +05302342
2343 void onMergeFailed(Call call) {
2344 if (mCalls.contains(call)) {
2345 for (CallsManagerListener listener : mListeners) {
2346 listener.onMergeFailed(call);
2347 }
2348 }
2349 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08002350}