blob: 69a4ab798219f011c0a5de9da30a9e226599d557 [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);
Sailesh Nepal810735e2014-03-18 18:15:46 -070085 }
86
Tyler Gunn91d43cf2014-09-17 12:19:39 -070087 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080088
Santos Cordonf193ba42014-09-12 06:37:39 -070089 private static final int MAXIMUM_LIVE_CALLS = 1;
90 private static final int MAXIMUM_HOLD_CALLS = 1;
91 private static final int MAXIMUM_RINGING_CALLS = 1;
92 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080093 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Sandeep Kunta39b77672014-09-01 17:22:57 +053094 private static final int MAXIMUM_DSDA_LIVE_CALLS = 2;
95 private static final int MAXIMUM_DSDA_HOLD_CALLS = 2;
96 private static final int MAXIMUM_DSDA_TOP_LEVEL_CALLS = 4;
Santos Cordonf193ba42014-09-12 06:37:39 -070097
98 private static final int[] OUTGOING_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -070099 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -0700100
Santos Cordon91bd74c2014-11-07 16:10:22 -0800101 private static final int[] LIVE_CALL_STATES =
Sandeep Kunta39b77672014-09-01 17:22:57 +0530102 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING,
103 CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -0800104
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800105 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700106 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
107 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800108 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700109 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
110 * load factor before resizing, 1 means we only expect a single thread to
111 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800112 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700113 private final Set<Call> mCalls = Collections.newSetFromMap(
114 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800115
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700116 private final ConnectionServiceRepository mConnectionServiceRepository;
117 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
118 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700119 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800120 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700121 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800122 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700123 // For this set initial table size to 16 because we add 13 listeners in
124 // the CallsManager constructor.
125 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
126 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700127 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700128 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800129 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700130 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700131 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700132 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700133 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700134 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700135 private final TelecomSystem.SyncRoot mLock;
136 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700137 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700138 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
139 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700140 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800141 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
142 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700143 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700144
Santos Cordon91bd74c2014-11-07 16:10:22 -0800145 private boolean mCanAddCall = true;
146
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700147 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700148 * The call the user is currently interacting with. This is the call that should have audio
149 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800150 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700151 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800152
Sandeep Kunta39b77672014-09-01 17:22:57 +0530153 private static final int LCH_PLAY_DTMF = 100;
154 private static final int LCH_STOP_DTMF = 101;
155 private static final int PHONE_START_DSDA_INCALL_TONE = 102;
156 private static final int LCH_DTMF_PERIODICITY = 3000;
157 private static final int LCH_DTMF_PERIOD = 500;
158 private static final String sSupervisoryCallHoldToneConfig =
159 SystemProperties.get("persist.radio.sch_tone", "none");
160
161 private static InCallTonePlayer.Factory mPlayerFactory;
162 private String mLchSub = null;
163
164 private InCallTonePlayer mLocalCallReminderTonePlayer = null;
165 private InCallTonePlayer mSupervisoryCallHoldTonePlayer = null;
166 private String mSubInConversation = null;
167
Nancy Chena469f762014-12-09 18:29:20 -0800168 private Runnable mStopTone;
169
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700170 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700171 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800172 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700173 CallsManager(
174 Context context,
175 TelecomSystem.SyncRoot lock,
176 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700177 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700178 MissedCallNotifier missedCallNotifier,
179 PhoneAccountRegistrar phoneAccountRegistrar,
180 HeadsetMediaButtonFactory headsetMediaButtonFactory,
181 ProximitySensorManagerFactory proximitySensorManagerFactory,
182 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700183 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700184 mLock = lock;
185 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700186 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700187 mPhoneAccountRegistrar = phoneAccountRegistrar;
188 mMissedCallNotifier = missedCallNotifier;
189 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
190 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800191 mDockManager = new DockManager(context);
192 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700193 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700194 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530195 mPlayerFactory = playerFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700196 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700197 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700198 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800199 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
200 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700201 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700202 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700203 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800204 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700205 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800206 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700207
Santos Cordondeb8c892014-05-30 01:38:03 -0700208 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700209 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700210 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700211 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700212 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700213 mListeners.add(new RingbackPlayer(this, playerFactory));
214 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700215 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700216 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700217 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700218 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700219 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700220
Ihab Awadabcbce42015-04-07 14:04:01 -0700221 mMissedCallNotifier.updateOnStartup(
222 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800223 }
224
Ihab Awad8de76912015-02-17 12:25:52 -0800225 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
226 if (mRespondViaSmsManager != null) {
227 mListeners.remove(mRespondViaSmsManager);
228 }
229 mRespondViaSmsManager = respondViaSmsManager;
230 mListeners.add(respondViaSmsManager);
231 }
232
233 public RespondViaSmsManager getRespondViaSmsManager() {
234 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800235 }
236
Santos Cordon766d04f2014-05-06 10:28:25 -0700237 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700238 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700239 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700240
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700241 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700242 if (!mCalls.contains(call)) {
243 // Call was not added previously in startOutgoingCall due to it being a potential MMI
244 // code, so add it now.
245 addCall(call);
246 }
247
248 // The call's ConnectionService has been updated.
249 for (CallsManagerListener listener : mListeners) {
250 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700251 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700252
253 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700254 }
255
256 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700257 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700258 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700259
Andrew Leee6288a72014-10-01 13:50:02 -0700260 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700261 }
262
263 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700264 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700265 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700266 setCallState(incomingCall, CallState.RINGING, "successful incoming call");
Santos Cordonf193ba42014-09-12 06:37:39 -0700267
Sandeep Kunta39b77672014-09-01 17:22:57 +0530268 if (hasMaximumRingingCalls(incomingCall.getTargetPhoneAccount().getId())) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700269 incomingCall.reject(false, null);
270 // since the call was not added to the list of calls, we have to call the missed
271 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700272 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700273 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
274 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530275 if (TelephonyManager.getDefault().getMultiSimConfiguration()
276 == TelephonyManager.MultiSimVariants.DSDA) {
277 incomingCall.mIsActiveSub = true;
278 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700279 addCall(incomingCall);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530280 setActiveSubscription(incomingCall.getTargetPhoneAccount().getId());
Santos Cordonf193ba42014-09-12 06:37:39 -0700281 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700282 }
283
284 @Override
285 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700286 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700287 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800288 }
289
Ihab Awadcb387ac2014-05-28 16:49:38 -0700290 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700291 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700292 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700293 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
294 addCall(call);
295 }
296
297 @Override
298 public void onFailedUnknownCall(Call call) {
299 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700300 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700301 call.removeListener(this);
302 }
303
304 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700305 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700306 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700307 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700308 }
309 }
310
Evan Charlton352105c2014-06-03 14:10:54 -0700311 @Override
312 public void onPostDialWait(Call call, String remaining) {
313 mInCallController.onPostDialWait(call, remaining);
314 }
315
Santos Cordona1610702014-06-04 20:22:56 -0700316 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800317 public void onPostDialChar(final Call call, char nextChar) {
318 if (PhoneNumberUtils.is12Key(nextChar)) {
319 // Play tone if it is one of the dialpad digits, canceling out the previously queued
320 // up stopTone runnable since playing a new tone automatically stops the previous tone.
321 if (mStopTone != null) {
322 mHandler.removeCallbacks(mStopTone);
323 }
324
325 mDtmfLocalTonePlayer.playTone(call, nextChar);
326
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700327 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800328 mStopTone = new Runnable() {
329 @Override
330 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700331 synchronized (mLock) {
332 // Set a timeout to stop the tone in case there isn't another tone to follow.
333 mDtmfLocalTonePlayer.stopTone(call);
334 }
Nancy Chena469f762014-12-09 18:29:20 -0800335 }
336 };
337 mHandler.postDelayed(
338 mStopTone,
339 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
340 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
341 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
342 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
343 // the previous tone is being stopped anyway.
344 if (mStopTone != null) {
345 mHandler.removeCallbacks(mStopTone);
346 }
347 mDtmfLocalTonePlayer.stopTone(call);
348 } else {
349 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
350 }
351 }
352
353 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700354 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700355 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800356 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700357 for (CallsManagerListener listener : mListeners) {
358 listener.onIsConferencedChanged(call);
359 }
360 }
361
362 @Override
363 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700364 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800365 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700366 for (CallsManagerListener listener : mListeners) {
367 listener.onIsConferencedChanged(call);
368 }
369 }
370
Ihab Awadff7493a2014-06-10 13:47:44 -0700371 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700372 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700373 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700374 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700375 }
376 }
377
Andrew Lee4a796602014-07-11 17:23:03 -0700378 @Override
379 public void onVideoStateChanged(Call call) {
380 for (CallsManagerListener listener : mListeners) {
381 listener.onVideoStateChanged(call);
382 }
383 }
384
Santos Cordoncf5b2912014-11-05 21:57:54 -0800385 @Override
386 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
387 mPendingCallsToDisconnect.add(call);
388 mHandler.postDelayed(new Runnable() {
389 @Override
390 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700391 synchronized (mLock) {
392 if (mPendingCallsToDisconnect.remove(call)) {
393 Log.i(this, "Delayed disconnection of call: %s", call);
394 call.disconnect();
395 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800396 }
397 }
398 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
399
400 return true;
401 }
402
Tyler Gunn86014fc2015-06-12 14:31:25 -0700403 /**
404 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
405 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
406 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
407 * respond to callbacks from the {@link VideoProviderProxy}.
408 *
409 * @param call The call.
410 */
411 @Override
412 public void onVideoCallProviderChanged(Call call) {
413 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
414
415 if (videoProviderProxy == null) {
416 return;
417 }
418
419 videoProviderProxy.addListener(this);
420 }
421
422 /**
423 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
424 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
425 * modification request.
426 *
427 * @param call The call.
428 * @param videoProfile The {@link VideoProfile}.
429 */
430 @Override
431 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
432 int videoState = videoProfile != null ? videoProfile.getVideoState() :
433 VideoProfile.STATE_AUDIO_ONLY;
434 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
435 .videoStateToString(videoState));
436
437 for (CallsManagerListener listener : mListeners) {
438 listener.onSessionModifyRequestReceived(call, videoProfile);
439 }
440 }
441
Santos Cordon4bc02452014-11-19 15:51:12 -0800442 Collection<Call> getCalls() {
443 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700444 }
445
446 Call getForegroundCall() {
447 return mForegroundCall;
448 }
449
Santos Cordonae193062014-05-21 21:21:49 -0700450 Ringer getRinger() {
451 return mRinger;
452 }
453
Santos Cordonf3671a62014-05-29 21:51:53 -0700454 InCallController getInCallController() {
455 return mInCallController;
456 }
457
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700458 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700459 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700460 if (call.isEmergencyCall()) {
461 return true;
462 }
463 }
464 return false;
465 }
466
Andrew Lee45506232014-10-22 17:54:33 -0700467 boolean hasVideoCall() {
468 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700469 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700470 return true;
471 }
472 }
473 return false;
474 }
475
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700476 CallAudioState getAudioState() {
477 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700478 }
479
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700480 boolean isTtySupported() {
481 return mTtyManager.isTtySupported();
482 }
483
484 int getCurrentTtyMode() {
485 return mTtyManager.getCurrentTtyMode();
486 }
487
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700488 void addListener(CallsManagerListener listener) {
489 mListeners.add(listener);
490 }
491
492 void removeListener(CallsManagerListener listener) {
493 mListeners.remove(listener);
494 }
495
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800496 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700497 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800498 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700499 * @param phoneAccountHandle The phone account which contains the component name of the
500 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800501 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800502 */
Evan Charlton89176372014-07-19 18:23:09 -0700503 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800504 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700505 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
506 if (handle == null) {
507 // Required for backwards compatibility
508 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
509 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700510 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700511 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800512 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700513 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700514 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700515 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700516 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700517 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700518 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700519 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700520 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700521 true /* isIncoming */,
522 false /* isConference */);
523
Andrew Lee1b906652015-06-01 11:44:55 -0700524 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700525 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700526 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700527 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800528 }
529
Yorke Lee9250e5f2014-10-01 13:39:09 -0700530 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
531 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
532 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
533 Call call = new Call(
534 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800535 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700536 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700537 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700538 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700539 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700540 handle,
541 null /* gatewayInfo */,
542 null /* connectionManagerPhoneAccount */,
543 phoneAccountHandle,
544 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
545 // to the existing connection instead of trying to create a new one.
546 true /* isIncoming */,
547 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700548 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700549 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700550 call.addListener(this);
551 call.startCreateConnection(mPhoneAccountRegistrar);
552 }
553
Yorke Lee8d3f2692015-05-08 11:44:39 -0700554 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
555 if (handle1 == null || handle2 == null) {
556 return handle1 == handle2;
557 }
558
559 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
560 return false;
561 }
562
563 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
564 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
565 return TextUtils.equals(number1, number2);
566 }
567
Santos Cordoncf5b2912014-11-05 21:57:54 -0800568 private Call getNewOutgoingCall(Uri handle) {
569 // First check to see if we can reuse any of the calls that are waiting to disconnect.
570 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800571 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800572 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700573 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800574 mPendingCallsToDisconnect.remove(pendingCall);
575 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800576 reusedCall = pendingCall;
577 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700578 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800579 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800580 }
581 }
Yorke Lee59979512014-12-02 01:03:40 -0800582 if (reusedCall != null) {
583 return reusedCall;
584 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800585
586 // Create a call with original handle. The handle may be changed when the call is attached
587 // to a connection service, but in most cases will remain the same.
588 return new Call(
589 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800590 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700591 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800592 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700593 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700594 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800595 handle,
596 null /* gatewayInfo */,
597 null /* connectionManagerPhoneAccount */,
598 null /* phoneAccountHandle */,
599 false /* isIncoming */,
600 false /* isConference */);
601 }
602
Nancy Chen0d3076c2014-07-30 14:45:44 -0700603 /**
604 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
605 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700606 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700607 * @param phoneAccountHandle The phone account which contains the component name of the
608 * connection service to use for this call.
609 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700610 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700611 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800612 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700613
Garik Badalyan7e1524a2015-08-25 11:08:18 -0700614 if (extras!=null) {
615 call.setVideoState(extras.getInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
616 VideoProfile.STATE_AUDIO_ONLY));
617 }
618
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530619 boolean isAddParticipant = ((extras != null) && (extras.getBoolean(
620 TelephonyProperties.ADD_PARTICIPANT_KEY, false)));
621 boolean isSkipSchemaOrConfUri = ((extras != null) && (extras.getBoolean(
622 TelephonyProperties.EXTRA_SKIP_SCHEMA_PARSING, false) ||
623 extras.getBoolean(TelephonyProperties.EXTRA_DIAL_CONFERENCE_URI, false)));
624
625 if (isAddParticipant) {
626 String number = handle.getSchemeSpecificPart();
627 if (!isSkipSchemaOrConfUri) {
628 number = PhoneNumberUtils.stripSeparators(number);
629 }
630 addParticipant(number);
631 mInCallController.bringToForeground(false);
632 return null;
633 }
634
635 // Force tel scheme for ims conf uri/skip schema calls to avoid selection of sip accounts
636 String scheme = (isSkipSchemaOrConfUri? PhoneAccount.SCHEME_TEL: handle.getScheme());
637
638 Log.d(this, "startOutgoingCall :: isAddParticipant=" + isAddParticipant
639 + " isSkipSchemaOrConfUri=" + isSkipSchemaOrConfUri + " scheme=" + scheme);
640
Nancy Chen1c5926f2014-09-17 14:44:14 -0700641 List<PhoneAccountHandle> accounts =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530642 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(scheme, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700643
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800644 Log.v(this, "startOutgoingCall found accounts = " + accounts);
645
Sandeep Kunta39b77672014-09-01 17:22:57 +0530646 if (mForegroundCall != null && TelephonyManager.getDefault().getMultiSimConfiguration()
647 != TelephonyManager.MultiSimVariants.DSDA) {
Roshan Pius1a373832015-08-07 11:39:05 -0700648 Call ongoingCall = mForegroundCall;
Nancy Chenbc95db92014-11-24 13:19:18 -0800649 // If there is an ongoing call, use the same phone account to place this new call.
Roshan Pius1a373832015-08-07 11:39:05 -0700650 // If the ongoing call is a conference call, we fetch the phone account from the
651 // child calls because we don't have targetPhoneAccount set on Conference calls.
652 // TODO: Set targetPhoneAccount for all conference calls (b/23035408).
653 if (ongoingCall.getTargetPhoneAccount() == null &&
654 !ongoingCall.getChildCalls().isEmpty()) {
655 ongoingCall = ongoingCall.getChildCalls().get(0);
656 }
657 if (ongoingCall.getTargetPhoneAccount() != null) {
658 phoneAccountHandle = ongoingCall.getTargetPhoneAccount();
659 }
Nancy Chenbc95db92014-11-24 13:19:18 -0800660 }
661
Nancy Chenbc9ef172014-08-15 17:11:57 -0700662 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
663 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700664 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700665 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700666 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700667 phoneAccountHandle = null;
668 }
669 }
670
671 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700672 // No preset account, check if default exists that supports the URI scheme for the
673 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700674 phoneAccountHandle =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530675 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(scheme);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700676 }
677
Nancy Chen1c5926f2014-09-17 14:44:14 -0700678 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700679
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700680 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
681 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700682 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700683
684 // Do not support any more live calls. Our options are to move a call to hold, disconnect
685 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700686 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700687 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800688 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800689 if (mCalls.contains(call)) {
690 // This call can already exist if it is a reused call,
691 // See {@link #getNewOutgoingCall}.
692 call.disconnect();
693 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700694 return null;
695 }
696
Nancy Chen8d92f452014-10-20 21:43:44 -0700697 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
698 !isEmergencyCall;
699
700 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700701 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700702 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700703 // Create our own instance to modify (since extras may be Bundle.EMPTY)
704 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700705 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700706 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700707 call.setState(
708 CallState.CONNECTING,
709 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700710 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700711
Andrew Lee1b906652015-06-01 11:44:55 -0700712 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700713
Tyler Gunn504eb832014-10-13 14:15:27 -0700714 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700715 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700716 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800717 } else if (!mCalls.contains(call)) {
718 // We check if mCalls already contains the call because we could potentially be reusing
719 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700720 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700721 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700722
723 return call;
724 }
725
Ben Gilada0d9f752014-02-26 11:49:03 -0800726 /**
Yorke Lee33501632014-03-17 19:24:12 -0700727 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800728 *
Yorke Lee33501632014-03-17 19:24:12 -0700729 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700730 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700731 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700732 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700733 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800734 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700735 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
736 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700737 if (call == null) {
738 // don't do anything if the call no longer exists
739 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700740 return;
741 }
742
Nancy Chen201b4372014-09-08 14:18:24 -0700743 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700744
745 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700746 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700747 } else {
748 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
749 Log.pii(uriHandle), Log.pii(handle));
750 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700751
Nancy Chen0d3076c2014-07-30 14:45:44 -0700752 call.setHandle(uriHandle);
753 call.setGatewayInfo(gatewayInfo);
Tyler Gunnd373c202015-05-20 08:21:03 -0700754 // Auto-enable speakerphone if the originating intent specified to do so, or if the call
Nivedita Sarkardae2c652015-08-31 10:55:51 -0700755 // is a video call or if the phone is docked.
756 call.setStartWithSpeakerphoneOn(speakerphoneOn || isSpeakerphoneAutoEnabled(videoState)
757 || mDockManager.isDocked());
Tyler Gunnc4abd912014-07-08 14:22:10 -0700758 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700759
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700760 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
761 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700762 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700763 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700764 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700765 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700766
Santos Cordonf193ba42014-09-12 06:37:39 -0700767 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530768 if (!isEmergencyCall) {
769 updateLchStatus(call.getTargetPhoneAccount().getId());
770 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700771 // If the account has been set, proceed to place the outgoing call.
772 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700773 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700774 }
Yorke Leef98fb572014-03-05 10:56:55 -0800775 }
776
777 /**
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530778 * Attempts to add participant in a call.
779 *
780 * @param number number to connect the call with.
781 */
782 private void addParticipant(String number) {
783 Log.i(this, "addParticipant number ="+number);
784 if (getForegroundCall() == null) {
785 // don't do anything if the call no longer exists
786 Log.i(this, "Canceling unknown call.");
787 return;
788 } else {
789 getForegroundCall().addParticipantWithConference(number);
790 }
791 }
792
793
794 /**
Santos Cordona1610702014-06-04 20:22:56 -0700795 * Attempts to start a conference call for the specified call.
796 *
Santos Cordon12d61822014-07-29 16:02:20 -0700797 * @param call The call to conference.
798 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700799 */
Santos Cordon12d61822014-07-29 16:02:20 -0700800 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700801 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700802 }
803
804 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700805 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
806 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800807 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700808 *
809 * @param call The call to answer.
810 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800811 */
Andrew Lee38931d02014-07-16 10:17:36 -0700812 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700813 if (!mCalls.contains(call)) {
814 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800815 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530816 Call activeCall = getFirstCallWithState(call.getTargetPhoneAccount()
817 .getId(), CallState.ACTIVE, CallState.DIALING);
Santos Cordon40f78c22014-04-07 02:11:42 -0700818 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700819 // STATE_DIALING, put it on hold before answering the call.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530820 if (activeCall != null && activeCall != call &&
821 (activeCall.isActive() ||
822 activeCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800823 if (0 == (mForegroundCall.getConnectionCapabilities()
824 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700825 // This call does not support hold. If it is from a different connection
826 // service, then disconnect it, otherwise allow the connection service to
827 // figure out the right states.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530828 if (activeCall.getConnectionService() != call.getConnectionService()) {
829 activeCall.disconnect();
Santos Cordonf193ba42014-09-12 06:37:39 -0700830 }
831 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800832 Call heldCall = getHeldCall();
833 if (heldCall != null) {
834 Log.v(this, "Disconnecting held call %s before holding active call.",
835 heldCall);
836 heldCall.disconnect();
837 }
838
Santos Cordonf193ba42014-09-12 06:37:39 -0700839 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
840 mForegroundCall, call);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530841 activeCall.hold();
Santos Cordonf193ba42014-09-12 06:37:39 -0700842 }
Santos Cordondf399862014-08-06 04:39:15 -0700843 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700844 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700845 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700846 }
847
Santos Cordona56f2762014-03-24 15:55:53 -0700848 for (CallsManagerListener listener : mListeners) {
849 listener.onIncomingCallAnswered(call);
850 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530851 updateLchStatus(call.getTargetPhoneAccount().getId());
Santos Cordon61d0f702014-02-19 02:52:23 -0800852 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700853 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700854 call.answer(videoState);
Tyler Gunnd373c202015-05-20 08:21:03 -0700855 if (isSpeakerphoneAutoEnabled(videoState)) {
Rekha Kumard240fe82015-04-01 21:45:57 -0700856 call.setStartWithSpeakerphoneOn(true);
857 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800858 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800859 }
860
Tyler Gunnd373c202015-05-20 08:21:03 -0700861 /**
862 * Determines if the speakerphone should be automatically enabled for the call. Speakerphone
863 * should be enabled if the call is a video call and bluetooth or the wired headset are not in
864 * use.
865 *
866 * @param videoState The video state of the call.
867 * @return {@code true} if the speakerphone should be enabled.
868 */
869 private boolean isSpeakerphoneAutoEnabled(int videoState) {
Etan Cohen7bcbb342015-06-25 15:39:21 -0700870 return VideoProfile.isVideo(videoState) &&
Tyler Gunnd373c202015-05-20 08:21:03 -0700871 !mWiredHeadsetManager.isPluggedIn() &&
872 !mCallAudioManager.isBluetoothDeviceAvailable() &&
873 isSpeakerEnabledForVideoCalls();
874 }
875
876 /**
877 * Determines if the speakerphone should be automatically enabled for video calls.
878 *
879 * @return {@code true} if the speakerphone should automatically be enabled.
880 */
Rekha Kumard240fe82015-04-01 21:45:57 -0700881 private static boolean isSpeakerEnabledForVideoCalls() {
882 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
883 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
884 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
885 }
886
Santos Cordone3d76ab2014-01-28 17:25:20 -0800887 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700888 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
889 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800890 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800891 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700892 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700893 if (!mCalls.contains(call)) {
894 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800895 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700896 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700897 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700898 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530899 setActiveSubscription(getConversationSub());
Ihab Awadff7493a2014-06-10 13:47:44 -0700900 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800901 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800902 }
903
904 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700905 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700906 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700907 * @param digit The DTMF digit to play.
908 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700909 void playDtmfTone(Call call, char digit) {
910 if (!mCalls.contains(call)) {
911 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700912 } else {
913 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700914 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700915 }
916 }
917
918 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700919 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700920 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700921 void stopDtmfTone(Call call) {
922 if (!mCalls.contains(call)) {
923 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700924 } else {
925 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700926 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700927 }
928 }
929
930 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700931 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700932 */
Evan Charlton352105c2014-06-03 14:10:54 -0700933 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700934 if (!mCalls.contains(call)) {
935 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700936 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700937 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700938 }
939 }
940
941 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700942 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800943 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
944 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800945 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700946 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700947 Log.v(this, "disconnectCall %s", call);
948
Sailesh Nepale59bb192014-04-01 18:33:59 -0700949 if (!mCalls.contains(call)) {
950 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800951 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700952 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800953 call.disconnect();
954 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800955 }
Santos Cordon681663d2014-01-30 04:32:15 -0800956
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700957 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700958 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700959 */
960 void disconnectAllCalls() {
961 Log.v(this, "disconnectAllCalls");
962
963 for (Call call : mCalls) {
964 disconnectCall(call);
965 }
966 }
967
Nancy Chenbc9ef172014-08-15 17:11:57 -0700968
Tyler Gunn61b92102014-08-19 07:42:20 -0700969 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700970 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700971 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
972 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700973 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700974 void holdCall(Call call) {
975 if (!mCalls.contains(call)) {
976 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700977 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700978 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700979 call.hold();
980 }
981 }
982
983 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700984 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700985 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
986 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700987 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700988 void unholdCall(Call call) {
989 if (!mCalls.contains(call)) {
990 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700991 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700992 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700993 for (Call c : mCalls) {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530994 PhoneAccountHandle ph = call.getTargetPhoneAccount();
995 PhoneAccountHandle ph1 = c.getTargetPhoneAccount();
Tyler Gunne44adb42015-01-02 10:55:35 -0800996 // Only attempt to hold parent calls and not the individual children.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530997 // if 'c' is not for same subscription as call, then don't disturb 'c'
998 if (c != null && c.isAlive() && c != call && c.getParentCall() == null
999 && (ph != null && ph1 != null &&
1000 isSamePhAccIdOrSipId(ph.getId(), ph1.getId()))) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001001 c.hold();
1002 }
1003 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001004 call.unhold();
1005 }
1006 }
1007
Sandeep Kunta39b77672014-09-01 17:22:57 +05301008 /**
1009 * Returns true if the ids are same or one of the ids is sip id.
1010 */
1011 private boolean isSamePhAccIdOrSipId(String id1, String id2) {
1012 boolean ret = ((id1 != null && id2 != null) &&
1013 (id1.equals(id2) || id1.contains("sip") || id2.contains("sip")));
1014 Log.d(this, "isSamePhAccIdOrSipId: id1 = " + id1 + " id2 = " + id2 + " ret = " + ret);
1015 return ret;
1016 }
1017
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001018 /** Called by the in-call UI to change the mute state. */
1019 void mute(boolean shouldMute) {
1020 mCallAudioManager.mute(shouldMute);
1021 }
1022
1023 /**
1024 * Called by the in-call UI to change the audio route, for example to change from earpiece to
1025 * speaker phone.
1026 */
1027 void setAudioRoute(int route) {
1028 mCallAudioManager.setAudioRoute(route);
1029 }
1030
Yorke Leed1346872014-07-28 14:38:45 -07001031 /** Called by the in-call UI to turn the proximity sensor on. */
1032 void turnOnProximitySensor() {
1033 mProximitySensorManager.turnOn();
1034 }
1035
1036 /**
1037 * Called by the in-call UI to turn the proximity sensor off.
1038 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
1039 * the screen will be kept off until the proximity sensor goes negative.
1040 */
1041 void turnOffProximitySensor(boolean screenOnImmediately) {
1042 mProximitySensorManager.turnOff(screenOnImmediately);
1043 }
1044
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001045 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -07001046 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001047 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -07001048 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -07001049 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -07001050 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -07001051 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
1052 // respecting a rewritten number or a canceled number. This is unlikely since
1053 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
1054 // a phone account from the in-call UI.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301055 Log.i(this, "phoneAccountSelected , id = %s", account.getId());
1056 updateLchStatus(account.getId());
Ihab Awadb78b2762014-07-25 15:16:23 -07001057 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -07001058
1059 // Note: emergency calls never go through account selection dialog so they never
1060 // arrive here.
1061 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001062 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -07001063 } else {
1064 call.disconnect();
1065 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001066
1067 if (setDefault) {
1068 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
1069 }
Nancy Chen53ceedc2014-07-08 18:56:51 -07001070 }
1071 }
1072
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001073 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001074 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001075 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001076 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001077 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001078 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001079 }
1080
Sailesh Nepale59bb192014-04-01 18:33:59 -07001081 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001082 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -08001083 }
1084
Sailesh Nepale59bb192014-04-01 18:33:59 -07001085 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001086 setCallState(call, CallState.DIALING, "dialing set explicitly");
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001087 maybeMoveToEarpiece(call);
Santos Cordond6782cd2015-04-16 09:57:50 -07001088 maybeMoveToSpeakerPhone(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301089 setActiveSubscription(call.getTargetPhoneAccount().getId());
Santos Cordon681663d2014-01-30 04:32:15 -08001090 }
1091
Sailesh Nepale59bb192014-04-01 18:33:59 -07001092 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001093 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -07001094 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -08001095 }
1096
Sailesh Nepale59bb192014-04-01 18:33:59 -07001097 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001098 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001099 }
1100
Santos Cordon049b7b62014-01-30 05:34:26 -08001101 /**
Santos Cordonf193ba42014-09-12 06:37:39 -07001102 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
1103 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -08001104 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001105 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -08001106 */
Andrew Lee701dc002014-09-11 21:29:12 -07001107 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
1108 call.setDisconnectCause(disconnectCause);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301109 int prevState = call.getState();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001110 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sandeep Kunta39b77672014-09-01 17:22:57 +05301111 String activeSub = getActiveSubscription();
1112 String conversationSub = getConversationSub();
1113 String lchSub = IsAnySubInLch();
1114
1115 PhoneAccount phAcc =
1116 getPhoneAccountRegistrar().getPhoneAccount(call.getTargetPhoneAccount());
1117 if (activeSub != null && (call.getTargetPhoneAccount() != null &&
1118 call.getTargetPhoneAccount().getId().equals(activeSub)) &&
1119 (phAcc != null) && (phAcc.isSet(PhoneAccount.LCH)) &&
1120 (conversationSub != null) &&
1121 (!conversationSub.equals(activeSub))) {
1122 Log.d(this,"Set active sub to conversation sub");
1123 setActiveSubscription(conversationSub);
1124 } else if ((conversationSub == null) && (lchSub != null) &&
1125 ((prevState == CallState.CONNECTING) || (prevState ==
1126 CallState.SELECT_PHONE_ACCOUNT)) &&
1127 (call.getState() == CallState.DISCONNECTED)) {
1128 Log.d(this,"remove sub with call from LCH");
1129 updateLchStatus(lchSub);
1130 setActiveSubscription(lchSub);
1131 manageDsdaInCallTones(false);
1132 }
1133
1134 if ((call.getTargetPhoneAccount() != null) && (phAcc != null) &&
1135 (phAcc.isSet(PhoneAccount.LCH))) {
1136 Call activecall = getFirstCallWithState(call.getTargetPhoneAccount().getId(),
1137 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1138 Log.d(this,"activecall: " + activecall);
1139 if (activecall == null) {
1140 phAcc.unSetBit(PhoneAccount.LCH);
1141 manageDsdaInCallTones(false);
1142 }
1143 }
1144 }
1145
1146 private String IsAnySubInLch() {
1147 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
1148 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
1149 if ((phAcc != null) && (phAcc.isSet(PhoneAccount.LCH))) {
1150 Log.d(this, "Sub in LCH: " + ph.getId());
1151 return ph.getId();
1152 }
1153 }
1154 return null;
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -07001155 }
1156
Ben Gilada0d9f752014-02-26 11:49:03 -08001157 /**
Ihab Awada02bef52014-08-13 18:18:42 -07001158 * Removes an existing disconnected call, and notifies the in-call app.
1159 */
1160 void markCallAsRemoved(Call call) {
1161 removeCall(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301162 if (!hasAnyCalls()) {
1163 updateLchStatus(null);
1164 setActiveSubscription(null);
1165 manageDsdaInCallTones(false);
1166 }
Ihab Awad5b281322014-09-25 18:25:29 -07001167 if (mLocallyDisconnectingCalls.contains(call)) {
1168 mLocallyDisconnectingCalls.remove(call);
1169 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1170 mForegroundCall.unhold();
1171 }
1172 }
Ihab Awada02bef52014-08-13 18:18:42 -07001173 }
1174
1175 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001176 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001177 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001178 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001179 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001180 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001181 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001182 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001183 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001184 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001185 if (call.getState() != CallState.DISCONNECTED) {
1186 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1187 }
1188 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001189 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001190 }
1191 }
1192 }
1193
Santos Cordondeb8c892014-05-30 01:38:03 -07001194 boolean hasAnyCalls() {
1195 return !mCalls.isEmpty();
1196 }
1197
Santos Cordonc7e85d42014-05-22 02:51:48 -07001198 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001199 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001200 }
1201
Sandeep Kunta39b77672014-09-01 17:22:57 +05301202 boolean hasActiveOrHoldingCall(String sub) {
1203 return (getFirstCallWithState(sub, CallState.ACTIVE, CallState.ON_HOLD) != null);
1204 }
1205
Santos Cordonc7e85d42014-05-22 02:51:48 -07001206 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001207 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001208 }
1209
Santos Cordondeb8c892014-05-30 01:38:03 -07001210 boolean onMediaButton(int type) {
1211 if (hasAnyCalls()) {
1212 if (HeadsetMediaButton.SHORT_PRESS == type) {
1213 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1214 if (ringingCall == null) {
1215 mCallAudioManager.toggleMute();
1216 return true;
1217 } else {
Andrew Lee38931d02014-07-16 10:17:36 -07001218 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -07001219 return true;
1220 }
1221 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1222 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1223 Call callToHangup = getFirstCallWithState(
1224 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1225 if (callToHangup != null) {
1226 callToHangup.disconnect();
1227 return true;
1228 }
1229 }
1230 }
1231 return false;
1232 }
1233
1234 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001235 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001236 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001237 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -08001238 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1239 return false;
1240 }
1241
Santos Cordon91bd74c2014-11-07 16:10:22 -08001242 int count = 0;
1243 for (Call call : mCalls) {
1244 if (call.isEmergencyCall()) {
1245 // We never support add call if one of the calls is an emergency call.
1246 return false;
Roshan Piusd9544092015-07-31 11:31:52 -07001247 } else if (!call.getChildCalls().isEmpty() && !call.can(Connection.CAPABILITY_HOLD)) {
1248 // This is to deal with CDMA conference calls. CDMA conference calls do not
1249 // allow the addition of another call when it is already in a 3 way conference.
1250 // So, we detect that it is a CDMA conference call by checking if the call has
1251 // some children and it does not support the CAPABILILTY_HOLD
1252 // TODO: This maybe cleaner if the lower layers can explicitly signal to telecom
1253 // about this limitation (b/22880180).
1254 return false;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001255 } else if (call.getParentCall() == null) {
1256 count++;
1257 }
Santos Cordon10838c22014-06-11 17:36:04 -07001258
Santos Cordon91bd74c2014-11-07 16:10:22 -08001259 // We do not check states for canAddCall. We treat disconnected calls the same
1260 // and wait until they are removed instead. If we didn't count disconnected calls,
1261 // we could put InCallServices into a state where they are showing two calls but
1262 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1263 // and acts better if we wait until the call is removed.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301264 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1265 == TelephonyManager.MultiSimVariants.DSDA) {
1266 if (count >= MAXIMUM_DSDA_TOP_LEVEL_CALLS) {
1267 return false;
1268 }
1269 } else if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001270 return false;
1271 }
1272 }
1273 return true;
1274 }
1275
Ihab Awada3653902015-01-23 13:44:46 -08001276 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001277 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001278 return getFirstCallWithState(CallState.RINGING);
1279 }
1280
1281 Call getActiveCall() {
1282 return getFirstCallWithState(CallState.ACTIVE);
1283 }
1284
Nancy Chen05a9e402014-09-26 14:14:32 -07001285 Call getDialingCall() {
1286 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001287 }
1288
1289 Call getHeldCall() {
1290 return getFirstCallWithState(CallState.ON_HOLD);
1291 }
1292
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001293 int getNumHeldCalls() {
1294 int count = 0;
1295 for (Call call : mCalls) {
1296 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1297 count++;
1298 }
1299 }
1300 return count;
1301 }
1302
Roshan Pius93c9c012015-08-28 11:10:56 -07001303 Call getOutgoingCall() {
1304 return getFirstCallWithState(OUTGOING_CALL_STATES);
1305 }
1306
Santos Cordonf193ba42014-09-12 06:37:39 -07001307 Call getFirstCallWithState(int... states) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301308 return getFirstCallWithState((Call) null, states);
Santos Cordonf193ba42014-09-12 06:37:39 -07001309 }
1310
Santos Cordon10838c22014-06-11 17:36:04 -07001311 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001312 * Returns the first call that it finds with the given states. The states are treated as having
1313 * priority order so that any call with the first state will be returned before any call with
1314 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001315 *
1316 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001317 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001318 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001319 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001320 // check the foreground first
1321 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1322 return mForegroundCall;
1323 }
1324
Santos Cordondeb8c892014-05-30 01:38:03 -07001325 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001326 if (Objects.equals(callToSkip, call)) {
1327 continue;
1328 }
1329
1330 // Only operate on top-level calls
1331 if (call.getParentCall() != null) {
1332 continue;
1333 }
1334
Santos Cordondeb8c892014-05-30 01:38:03 -07001335 if (currentState == call.getState()) {
1336 return call;
1337 }
1338 }
1339 }
1340 return null;
1341 }
1342
Sandeep Kunta39b77672014-09-01 17:22:57 +05301343 /**
1344 * Returns the first call that it finds with the given states for given subscription.
1345 * the states are treated as having priority order so that any call with the first
1346 * state will be returned before any call with states listed later in the parameter list.
1347 *
1348 * @param subId check calls only on this subscription
1349 * @param callToSkip Call that this method should skip while searching
1350 */
1351 Call getFirstCallWithState(String subId, Call callToSkip, int... states) {
1352 for (int currentState : states) {
1353 // check the foreground first
1354 if (mForegroundCall != null && mForegroundCall.getState() == currentState
1355 && mForegroundCall.getTargetPhoneAccount() != null
1356 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(), subId)) {
1357 return mForegroundCall;
1358 }
1359
1360 for (Call call : mCalls) {
1361 if (Objects.equals(callToSkip, call)) {
1362 continue;
1363 }
1364
1365 // Only operate on top-level calls
1366 if (call.getParentCall() != null) {
1367 continue;
1368 }
1369
1370 if (currentState == call.getState() && call.getTargetPhoneAccount() != null
1371 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1372 return call;
1373 }
1374 }
1375 }
1376 return null;
1377 }
1378
Santos Cordon0fbe6322014-08-14 04:04:25 -07001379 Call createConferenceCall(
1380 PhoneAccountHandle phoneAccount,
1381 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001382
1383 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1384 // which is the default value for new Calls.
1385 long connectTime =
1386 parcelableConference.getConnectTimeMillis() ==
1387 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1388 parcelableConference.getConnectTimeMillis();
1389
Santos Cordon0fbe6322014-08-14 04:04:25 -07001390 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001391 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001392 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001393 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001394 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001395 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001396 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001397 null /* handle */,
1398 null /* gatewayInfo */,
1399 null /* connectionManagerPhoneAccount */,
1400 phoneAccount,
1401 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001402 true /* isConference */,
1403 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001404
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001405 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1406 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001407 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001408 call.setVideoState(parcelableConference.getVideoState());
1409 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001410 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001411 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001412
1413 // TODO: Move this to be a part of addCall()
1414 call.addListener(this);
1415 addCall(call);
1416 return call;
1417 }
1418
Yorke Leef86db2e2014-09-12 17:56:48 -07001419 /**
1420 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1421 */
1422 int getCallState() {
1423 return mPhoneStateBroadcaster.getCallState();
1424 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001425
Santos Cordon4b2c1192014-03-19 18:15:38 -07001426 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001427 * Retrieves the {@link PhoneAccountRegistrar}.
1428 *
1429 * @return The {@link PhoneAccountRegistrar}.
1430 */
1431 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1432 return mPhoneAccountRegistrar;
1433 }
1434
1435 /**
1436 * Retrieves the {@link MissedCallNotifier}
1437 * @return The {@link MissedCallNotifier}.
1438 */
1439 MissedCallNotifier getMissedCallNotifier() {
1440 return mMissedCallNotifier;
1441 }
1442
1443 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001444 * Adds the specified call to the main list of live calls.
1445 *
1446 * @param call The call to add.
1447 */
1448 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001449 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001450 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001451 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001452 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001453
Santos Cordondf399862014-08-06 04:39:15 -07001454 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001455 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001456 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001457 if (Log.SYSTRACE_DEBUG) {
1458 Trace.beginSection(listener.getClass().toString() + " addCall");
1459 }
Santos Cordona56f2762014-03-24 15:55:53 -07001460 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001461 if (Log.SYSTRACE_DEBUG) {
1462 Trace.endSection();
1463 }
Santos Cordona56f2762014-03-24 15:55:53 -07001464 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001465 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001466 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001467 }
1468
Sailesh Nepal810735e2014-03-18 18:15:46 -07001469 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001470 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001471 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001472
Santos Cordon672321e2014-08-21 14:38:58 -07001473 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001474 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001475 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001476
1477 boolean shouldNotify = false;
1478 if (mCalls.contains(call)) {
1479 mCalls.remove(call);
1480 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001481 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001482
Santos Cordon2685dab2015-04-17 17:12:09 -07001483 call.destroy();
1484
Sailesh Nepale59bb192014-04-01 18:33:59 -07001485 // Only broadcast changes for calls that are being tracked.
1486 if (shouldNotify) {
1487 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001488 if (Log.SYSTRACE_DEBUG) {
1489 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1490 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001491 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001492 if (Log.SYSTRACE_DEBUG) {
1493 Trace.endSection();
1494 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001495 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001496 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001497 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001498 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001499 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001500
Sailesh Nepal810735e2014-03-18 18:15:46 -07001501 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001502 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001503 *
1504 * @param call The call.
1505 * @param newState The new state of the call.
1506 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001507 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001508 if (call == null) {
1509 return;
1510 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001511 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001512 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1513 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001514 if (newState != oldState) {
1515 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1516 // 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 -07001517 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001518 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001519 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001520 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001521 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001522 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001523
Yorke Leee4a9c412014-11-14 16:59:42 -08001524 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001525 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001526 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001527 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001528 if (Log.SYSTRACE_DEBUG) {
1529 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1530 }
Santos Cordona56f2762014-03-24 15:55:53 -07001531 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001532 if (Log.SYSTRACE_DEBUG) {
1533 Trace.endSection();
1534 }
Santos Cordona56f2762014-03-24 15:55:53 -07001535 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001536 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001537 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001538 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001539 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301540 manageDsdaInCallTones(false);
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001541 }
1542
1543 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001544 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001545 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001546 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001547 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001548 Call newForegroundCall = null;
Santos Cordondf399862014-08-06 04:39:15 -07001549 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001550 // of its state will be foreground by default and instead the connection service should
1551 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001552 // the call should play audio and listen to microphone if it wants.
1553
Sandeep Kunta39b77672014-09-01 17:22:57 +05301554 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1555 == TelephonyManager.MultiSimVariants.DSDA) {
1556 String lchSub = getLchSub();
1557 for (Call call : mCalls) {
1558 // Only top-level calls can be in foreground
1559 if (call.getParentCall() != null) {
1560 continue;
1561 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001562
Sandeep Kunta39b77672014-09-01 17:22:57 +05301563 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1564 if (ph != null && ph.getId().equals(lchSub)) continue;
1565 // Active calls have priority.
1566 if (call.isActive()) {
1567 newForegroundCall = call;
1568 break;
1569 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001570
Sandeep Kunta39b77672014-09-01 17:22:57 +05301571 if (call.isAlive() || call.getState() == CallState.RINGING) {
1572 newForegroundCall = call;
1573 // Don't break in case there's an active call that has priority.
1574 }
1575 }
1576 // if active sub doesn't have any calls, then consider calls on all subs,
1577 // which ever call is active set that as foreground call. give more priority
1578 // to ringing call on LCH sub over active call.
1579 if (newForegroundCall == null) {
1580 newForegroundCall = getFirstCallWithState(CallState.RINGING);
1581 if (newForegroundCall == null) {
1582 newForegroundCall = getFirstCallWithState(CallState.ACTIVE);
1583 }
1584 }
1585 } else {
1586 for (Call call : mCalls) {
1587 // Only top-level calls can be in foreground
1588 if (call.getParentCall() != null) {
1589 continue;
1590 }
1591
1592 // Active calls have priority.
1593 if (call.isActive()) {
1594 newForegroundCall = call;
1595 break;
1596 }
1597
1598 if (call.isAlive() || call.getState() == CallState.RINGING) {
1599 newForegroundCall = call;
1600 // Don't break in case there's an active call that has priority.
1601 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001602 }
1603 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001604
Sailesh Nepal810735e2014-03-18 18:15:46 -07001605 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001606 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001607 Call oldForegroundCall = mForegroundCall;
1608 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001609
Santos Cordona56f2762014-03-24 15:55:53 -07001610 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001611 if (Log.SYSTRACE_DEBUG) {
1612 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1613 }
Santos Cordona56f2762014-03-24 15:55:53 -07001614 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001615 if (Log.SYSTRACE_DEBUG) {
1616 Trace.endSection();
1617 }
Santos Cordona56f2762014-03-24 15:55:53 -07001618 }
Santos Cordon681663d2014-01-30 04:32:15 -08001619 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001620 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001621 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001622
Santos Cordon91bd74c2014-11-07 16:10:22 -08001623 private void updateCanAddCall() {
1624 boolean newCanAddCall = canAddCall();
1625 if (newCanAddCall != mCanAddCall) {
1626 mCanAddCall = newCanAddCall;
1627 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001628 if (Log.SYSTRACE_DEBUG) {
1629 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1630 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001631 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001632 if (Log.SYSTRACE_DEBUG) {
1633 Trace.endSection();
1634 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001635 }
1636 }
1637 }
1638
1639 private void updateCallsManagerState() {
1640 updateForegroundCall();
1641 updateCanAddCall();
1642 }
1643
Yorke Leeb701f6d2014-08-12 14:04:19 -07001644 private boolean isPotentialMMICode(Uri handle) {
1645 return (handle != null && handle.getSchemeSpecificPart() != null
1646 && handle.getSchemeSpecificPart().contains("#"));
1647 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001648
Tyler Gunn5b452df2014-10-01 15:02:58 -07001649 /**
1650 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1651 * MMI codes which can be dialed when one or more calls are in progress.
1652 * <P>
1653 * Checks for numbers formatted similar to the MMI codes defined in:
1654 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1655 * and
1656 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1657 *
1658 * @param handle The URI to call.
1659 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1660 */
1661 private boolean isPotentialInCallMMICode(Uri handle) {
1662 if (handle != null && handle.getSchemeSpecificPart() != null &&
1663 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1664
1665 String dialedNumber = handle.getSchemeSpecificPart();
1666 return (dialedNumber.equals("0") ||
1667 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1668 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1669 dialedNumber.equals("3") ||
1670 dialedNumber.equals("4") ||
1671 dialedNumber.equals("5"));
1672 }
1673 return false;
1674 }
1675
Santos Cordonf193ba42014-09-12 06:37:39 -07001676 private int getNumCallsWithState(int... states) {
1677 int count = 0;
1678 for (int state : states) {
1679 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001680 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001681 count++;
1682 }
1683 }
1684 }
1685 return count;
1686 }
1687
Sandeep Kunta39b77672014-09-01 17:22:57 +05301688 private int getNumCallsWithState(String subId, int... states) {
1689 int count = 0;
1690 for (int state : states) {
1691 for (Call call : mCalls) {
1692 if (call.getParentCall() == null && call.getState() == state
1693 && call.getTargetPhoneAccount() != null
1694 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1695 count++;
1696 }
1697 }
1698 }
1699 return count;
1700 }
1701
Santos Cordonf193ba42014-09-12 06:37:39 -07001702 private boolean hasMaximumLiveCalls() {
1703 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1704 }
1705
Sandeep Kunta39b77672014-09-01 17:22:57 +05301706 private boolean hasMaximumLiveCalls(String subId) {
1707 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(subId, LIVE_CALL_STATES);
1708 }
1709
Santos Cordonf193ba42014-09-12 06:37:39 -07001710 private boolean hasMaximumHoldingCalls() {
1711 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1712 }
1713
Sandeep Kunta39b77672014-09-01 17:22:57 +05301714 private boolean hasMaximumHoldingCalls(String subId) {
1715 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(subId, CallState.ON_HOLD);
1716 }
1717
Santos Cordonf193ba42014-09-12 06:37:39 -07001718 private boolean hasMaximumRingingCalls() {
1719 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1720 }
1721
Sandeep Kunta39b77672014-09-01 17:22:57 +05301722 private boolean hasMaximumRingingCalls(String subId) {
1723 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(subId, CallState.RINGING);
1724 }
1725
Santos Cordonf193ba42014-09-12 06:37:39 -07001726 private boolean hasMaximumOutgoingCalls() {
1727 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1728 }
1729
1730 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301731 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1732 == TelephonyManager.MultiSimVariants.DSDA) {
1733 return makeRoomForOutgoingCallForDsda(call, isEmergency);
1734 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001735 if (hasMaximumLiveCalls()) {
1736 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1737 // have to change.
1738 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301739 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1740 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001741
Santos Cordond1766502014-09-26 15:45:39 -07001742 if (call == liveCall) {
1743 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001744 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001745 // state since the call was already populated into the list.
1746 return true;
1747 }
1748
Andrew Leee6595182014-09-23 18:43:05 -07001749 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001750 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1751 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1752 // Disconnect the current outgoing call if it's not an emergency call. If the
1753 // user tries to make two outgoing calls to different emergency call numbers,
1754 // we will try to connect the first outgoing call.
1755 outgoingCall.disconnect();
1756 return true;
1757 }
1758 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1759 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1760 // state, just disconnect it since the user has explicitly started a new call.
1761 outgoingCall.disconnect();
1762 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001763 }
1764 return false;
1765 }
1766
Santos Cordonf193ba42014-09-12 06:37:39 -07001767 if (hasMaximumHoldingCalls()) {
1768 // There is no more room for any more calls, unless it's an emergency.
1769 if (isEmergency) {
1770 // Kill the current active call, this is easier then trying to disconnect a
1771 // holding call and hold an active call.
1772 liveCall.disconnect();
1773 return true;
1774 }
1775 return false; // No more room!
1776 }
1777
1778 // We have room for at least one more holding call at this point.
1779
Tyler Gunne4cd9162015-08-11 15:36:04 -07001780 // TODO: Remove once b/23035408 has been corrected.
1781 // If the live call is a conference, it will not have a target phone account set. This
1782 // means the check to see if the live call has the same target phone account as the new
1783 // call will not cause us to bail early. As a result, we'll end up holding the
1784 // ongoing conference call. However, the ConnectionService is already doing that. This
1785 // has caused problems with some carriers. As a workaround until b/23035408 is
1786 // corrected, we will try and get the target phone account for one of the conference's
1787 // children and use that instead.
1788 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
1789 if (liveCallPhoneAccount == null && liveCall.isConference() &&
1790 !liveCall.getChildCalls().isEmpty()) {
1791 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
1792 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
1793 liveCallPhoneAccount);
1794 }
1795
Santos Cordonf193ba42014-09-12 06:37:39 -07001796 // First thing, if we are trying to make a call with the same phone account as the live
1797 // call, then allow it so that the connection service can make its own decision about
1798 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07001799 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
1800 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001801 return true;
1802 } else if (call.getTargetPhoneAccount() == null) {
1803 // Without a phone account, we can't say reliably that the call will fail.
1804 // If the user chooses the same phone account as the live call, then it's
1805 // still possible that the call can be made (like with CDMA calls not supporting
1806 // hold but they still support adding a call by going immediately into conference
1807 // mode). Return true here and we'll run this code again after user chooses an
1808 // account.
1809 return true;
1810 }
1811
1812 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001813 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07001814 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001815 liveCall.hold();
1816 return true;
1817 }
1818
1819 // The live call cannot be held so we're out of luck here. There's no room.
1820 return false;
1821 }
1822 return true;
1823 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001824
Sandeep Kunta39b77672014-09-01 17:22:57 +05301825 private boolean makeRoomForOutgoingCallForDsda(Call call, boolean isEmergency) {
1826 if (isEmergency || (call.getState() == CallState.SELECT_PHONE_ACCOUNT)) {
1827 return true;
1828 }
1829
1830 PhoneAccountHandle phAcc = call.getTargetPhoneAccount();
1831 if (phAcc == null) {
1832 if (getNumCallsWithState(LIVE_CALL_STATES) == MAXIMUM_DSDA_LIVE_CALLS
1833 && getNumCallsWithState(CallState.ON_HOLD) == MAXIMUM_DSDA_HOLD_CALLS) {
1834 return false;
1835 } else {
1836 return true;
1837 }
1838 }
1839 if (hasMaximumLiveCalls(phAcc.getId())) {
1840 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1841 // have to change.
1842 Call liveCall = getFirstCallWithState(phAcc.getId(), call, LIVE_CALL_STATES);
1843
1844 if (hasMaximumHoldingCalls(phAcc.getId())) {
1845 // There is no more room for any more calls, unless it's an emergency.
1846 return false; // No more room!
1847 }
1848 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1849 return true;
1850 }
1851 // Try to hold the live call before attempting the new outgoing call.
1852 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
1853 liveCall.hold();
1854 return true;
1855 }
1856
1857 // The live call cannot be held so we're out of luck here. There's no room.
1858 return false;
1859 }
1860 return true;
1861 }
1862
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001863 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07001864 * Given a call, find the first non-null phone account handle of its children.
1865 *
1866 * @param parentCall The parent call.
1867 * @return The first non-null phone account handle of the children, or {@code null} if none.
1868 */
1869 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
1870 for (Call childCall : parentCall.getChildCalls()) {
1871 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
1872 if (childPhoneAccount != null) {
1873 return childPhoneAccount;
1874 }
1875 }
1876 return null;
1877 }
1878
1879 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001880 * Checks to see if the call should be on speakerphone and if so, set it.
1881 */
1882 private void maybeMoveToSpeakerPhone(Call call) {
1883 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001884 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001885 call.setStartWithSpeakerphoneOn(false);
1886 }
1887 }
1888
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001889 private void maybeMoveToEarpiece(Call call) {
1890 if (!call.getStartWithSpeakerphoneOn() && !mWiredHeadsetManager.isPluggedIn() &&
1891 !mCallAudioManager.isBluetoothDeviceAvailable()) {
1892 setAudioRoute(CallAudioState.ROUTE_EARPIECE);
1893 }
1894 }
1895
Santos Cordond6782cd2015-04-16 09:57:50 -07001896 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001897 * Creates a new call for an existing connection.
1898 *
1899 * @param callId The id of the new call.
1900 * @param connection The connection information.
1901 * @return The new call.
1902 */
1903 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1904 Call call = new Call(
1905 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001906 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001907 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001908 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001909 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001910 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001911 connection.getHandle() /* handle */,
1912 null /* gatewayInfo */,
1913 null /* connectionManagerPhoneAccount */,
1914 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1915 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001916 false /* isConference */,
1917 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001918
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001919 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1920 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001921 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001922 call.setCallerDisplayName(connection.getCallerDisplayName(),
1923 connection.getCallerDisplayNamePresentation());
1924
1925 call.addListener(this);
1926 addCall(call);
1927
1928 return call;
1929 }
1930
1931 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001932 * Dumps the state of the {@link CallsManager}.
1933 *
1934 * @param pw The {@code IndentingPrintWriter} to write the state to.
1935 */
1936 public void dump(IndentingPrintWriter pw) {
1937 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001938 if (mCalls != null) {
1939 pw.println("mCalls: ");
1940 pw.increaseIndent();
1941 for (Call call : mCalls) {
1942 pw.println(call);
1943 }
1944 pw.decreaseIndent();
1945 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001946 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1947
1948 if (mCallAudioManager != null) {
1949 pw.println("mCallAudioManager:");
1950 pw.increaseIndent();
1951 mCallAudioManager.dump(pw);
1952 pw.decreaseIndent();
1953 }
1954
1955 if (mTtyManager != null) {
1956 pw.println("mTtyManager:");
1957 pw.increaseIndent();
1958 mTtyManager.dump(pw);
1959 pw.decreaseIndent();
1960 }
1961
1962 if (mInCallController != null) {
1963 pw.println("mInCallController:");
1964 pw.increaseIndent();
1965 mInCallController.dump(pw);
1966 pw.decreaseIndent();
1967 }
1968
1969 if (mConnectionServiceRepository != null) {
1970 pw.println("mConnectionServiceRepository:");
1971 pw.increaseIndent();
1972 mConnectionServiceRepository.dump(pw);
1973 pw.decreaseIndent();
1974 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001975 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301976
1977 private final Handler mLchHandler = new LchHandler();
1978 private final class LchHandler extends Handler {
1979 @Override
1980 public void handleMessage(Message msg) {
1981 switch (msg.what) {
1982 case PHONE_START_DSDA_INCALL_TONE:
1983 Log.d(this, "Start DSDA incall tones...");
1984 startDsdaInCallTones();
1985 break;
1986 case LCH_PLAY_DTMF:
1987 playLchDtmf();
1988 break;
1989 case LCH_STOP_DTMF:
1990 stopLchDtmf();
1991 break;
1992 }
1993 }
1994 }
1995
1996 public void switchToOtherActiveSub(String subId) {
1997 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1998 != TelephonyManager.MultiSimVariants.DSDA) {
1999 return;
2000 }
2001 Log.d(this, "switchToOtherActiveSub sub:" + subId);
2002 setActiveSubscription(subId);
2003 updateLchStatus(subId);
2004 manageDsdaInCallTones(true);
2005 updateForegroundCall();
2006 }
2007
2008 public synchronized void setActiveSubscription(String subId) {
2009 Log.d(this, "setActiveSubscription = " + subId);
2010 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2011 != TelephonyManager.MultiSimVariants.DSDA) {
2012 return;
2013 }
2014 boolean changed = false;
2015 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2016 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2017 if (subId != null && subId.equals(ph.getId())
2018 && (phAcc != null) && !phAcc.isSet(PhoneAccount.ACTIVE)) {
2019 changed = true;
2020 phAcc.setBit(PhoneAccount.ACTIVE);
2021 } else if (subId != null && !subId.equals(ph.getId())
2022 && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2023 changed = true;
2024 phAcc.unSetBit(PhoneAccount.ACTIVE);
2025 } else if ((subId == null) && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2026 phAcc.unSetBit(PhoneAccount.ACTIVE);
2027 }
2028 }
2029 if (!changed) {
2030 Log.d(this, "setActiveSubscription not changed ");
2031 return;
2032 } else {
2033 Log.d(this, "setActiveSubscription changed " );
2034 for (Call call : mCalls) {
2035 PhoneAccountHandle ph = call.getTargetPhoneAccount();
2036 if (ph != null) {
2037 call.mIsActiveSub = ph.getId().equals(subId) ? true : false;
2038 }
2039 for (CallsManagerListener listener : mListeners) {
2040 listener.onCallStateChanged(call, call.getState(), call.getState());
2041 }
2042 }
2043 }
2044 }
2045
2046 public String getActiveSubscription() {
2047 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2048 if (getPhoneAccountRegistrar()
2049 .getPhoneAccount(ph).isSet(PhoneAccount.ACTIVE)) {
2050 Log.d(this, "getActiveSubscription: " + ph.getId());
2051 return ph.getId();
2052 }
2053 }
2054 return null;
2055 }
2056
2057 private String getConversationSub() {
2058 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2059 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2060 if (phAcc != null && !phAcc.isSet(PhoneAccount.LCH) &&
2061 (getFirstCallWithState(ph.getId(), CallState.ACTIVE, CallState.DIALING,
2062 CallState.ON_HOLD) != null)) {
2063 Log.d(this, "getConversationSub: " + ph.getId());
2064 return ph.getId();
2065 }
2066 }
2067 return null;
2068 }
2069
2070 void manageDsdaInCallTones(boolean isSubSwitch) {
2071 Log.d(this, " entered manageDsdaInCallTones ");
2072
2073 // If there is no background active subscription available, stop playing the tones.
2074 // Do not start/stop LCH/SCH tones when phone is in RINGING state.
2075 if (getLchSub() != null && !hasRingingCall()) {
2076 //If sub switch happens re-start the tones with a delay of 100msec.
2077 if (isSubSwitch) {
2078 Log.d(this, " manageDsdaInCallTones: re-start playing tones, lch sub = "
2079 + getLchSub());
2080 reStartDsdaInCallTones();
2081 } else {
2082 Log.d(this, " entered manageDsdaInCallTones ");
2083 startDsdaInCallTones();
2084 }
2085 } else if (getLchSub() == null) {
2086 // if there is no sub in Lch state, then stop playing the tones
2087 stopMSimInCallTones();
2088 }
2089 }
2090
2091 private void reStartDsdaInCallTones() {
2092 Log.d(this, " reStartDsdaInCallTones");
2093 stopMSimInCallTones();
2094 /* Remove any pending PHONE_START_DSDA_INCALL_TONE messages from queue */
2095 mLchHandler.removeMessages(PHONE_START_DSDA_INCALL_TONE);
2096 Message message = Message.obtain(mLchHandler, PHONE_START_DSDA_INCALL_TONE);
2097 mLchHandler.sendMessageDelayed(message, 100);
2098 }
2099
2100 /**
2101 * Returns the first call that it finds with the given states for given subscription.
2102 * The states are treated as having priority order so that any call with the first
2103 * state will be returned before any call with states listed later in the parameter list.
2104 */
2105 Call getFirstCallWithState(String sub, int... states) {
2106 for (int currentState : states) {
2107 // check the foreground first
2108 if (mForegroundCall != null && mForegroundCall.getState() == currentState
2109 && (mForegroundCall.getTargetPhoneAccount() != null)
2110 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(),
2111 sub)) {
2112 return mForegroundCall;
2113 }
2114
2115 for (Call call : mCalls) {
2116 if ((currentState == call.getState()) &&
2117 (call.getTargetPhoneAccount() != null) &&
2118 (isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), sub))) {
2119 return call;
2120 }
2121 }
2122 }
2123 return null;
2124 }
2125
2126 private String getLchSub() {
2127 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2128 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2129 if (phAcc != null && phAcc.isSet(PhoneAccount.LCH)) {
2130 return ph.getId();
2131 }
2132 }
2133 return null;
2134 }
2135
2136 private void playLchDtmf() {
2137 if (mLchSub != null || mLchHandler.hasMessages(LCH_PLAY_DTMF)) {
2138 // Ignore any redundant requests to start playing tones
2139 return;
2140 }
2141 mLchSub = getLchSub();
2142 Log.d(this, " playLchDtmf... lch sub " + mLchSub);
2143 if (mLchSub == null) return;
2144 removeAnyPendingDtmfMsgs();
2145 char c = mContext.getResources()
2146 .getString(R.string.lch_dtmf_key).charAt(0);
2147 Call call = getNonRingingLiveCall(mLchSub);
2148 if (call == null) {
2149 mLchSub = null;
2150 return;
2151 }
2152 call.playDtmfTone(c);
2153 // Keep playing LCH DTMF tone to remote party on LCH call, with periodicity
2154 // "LCH_DTMF_PERIODICITY" until call moves out of LCH.
2155 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_PLAY_DTMF),
2156 LCH_DTMF_PERIODICITY);
2157 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_STOP_DTMF), LCH_DTMF_PERIOD);
2158 }
2159
2160 private Call getNonRingingLiveCall(String subId) {
2161 return getFirstCallWithState(subId, CallState.DIALING,
2162 CallState.ACTIVE, CallState.ON_HOLD);
2163 }
2164
2165 private void stopLchDtmf() {
2166 if (mLchSub != null) {
2167 // Ignore any redundant requests to stop playing tones
2168 Call call = getNonRingingLiveCall(mLchSub);
2169 Log.d(this, " stopLchDtmf... call: " + call + " mLchSub:" + mLchSub);
2170 if (call == null) {
2171 mLchSub = null;
2172 return;
2173 }
2174 call.stopDtmfTone();
2175 }
2176 mLchSub = null;
2177 }
2178
2179 private void startDsdaInCallTones() {
2180 if (mLocalCallReminderTonePlayer == null) {
2181 Log.d(this, " Play local call hold reminder tone ");
2182 mLocalCallReminderTonePlayer =
2183 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_HOLD_RECALL);
2184 mLocalCallReminderTonePlayer.start();
2185 }
2186 if (sSupervisoryCallHoldToneConfig.equals("inband")) {
2187 // if "persist.radio.sch_tone" is set to "inband", play inband supervisory
2188 // call hold tone. if set to "dtmf", play the SCH tones
2189 // over DTMF, don't play SCH tones for anyother value.
2190 if (mSupervisoryCallHoldTonePlayer == null) {
2191 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone ");
2192 mSupervisoryCallHoldTonePlayer =
2193 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_SUPERVISORY_CH);
2194 mSupervisoryCallHoldTonePlayer.start();
2195 }
2196 } else if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2197 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone over dtmf ");
2198 playLchDtmf();
2199 }
2200 }
2201
2202 private void removeAnyPendingDtmfMsgs() {
2203 mLchHandler.removeMessages(LCH_PLAY_DTMF);
2204 mLchHandler.removeMessages(LCH_STOP_DTMF);
2205 }
2206
2207 protected void stopMSimInCallTones() {
2208 if (mLocalCallReminderTonePlayer != null) {
2209 Log.d(this, " stopMSimInCallTones: local call hold reminder tone ");
2210 mLocalCallReminderTonePlayer.stopTone();
2211 mLocalCallReminderTonePlayer = null;
2212 }
2213 if (mSupervisoryCallHoldTonePlayer != null) {
2214 Log.d(this, " stopMSimInCallTones: Supervisory call hold tone ");
2215 mSupervisoryCallHoldTonePlayer.stopTone();
2216 mSupervisoryCallHoldTonePlayer = null;
2217 }
2218 if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2219 Log.d(this, " stopMSimInCallTones: stop SCH Dtmf call hold tone ");
2220 stopLchDtmf();
2221 /* Remove any previous dtmf nssages from queue */
2222 removeAnyPendingDtmfMsgs();
2223 }
2224 }
2225
2226 /**
2227 * Update the local call hold state for all subscriptions
2228 * 1 -- if call on local hold, 0 -- if call is not on local hold
2229 *
2230 * @param subInConversation is the sub user is currently active in subsription.
2231 * so if this sub is in LCH, then bring that sub out of LCH.
2232 */
2233 private void updateLchStatus(String subInConversation) {
2234 Call removeFromLch = null;
2235 Log.d(this, "updateLchStatus subInConversation: " + subInConversation);
2236 if (subInConversation != null && subInConversation.contains("sip")) {
2237 return;
2238 }
2239 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2240 String sub = ph.getId();
2241 if (sub != null && sub.contains("sip")) {
2242 Log.d(this, "update lch. Skipping account: " + sub);
2243 continue;
2244 }
2245 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2246 boolean lchState = false;
2247 if (subInConversation != null && hasActiveOrHoldingCall(sub) &&
2248 !sub.equals(subInConversation)) {
2249 // if sub is not conversation sub and if it has an active
2250 // voice call then update lchStatus as Active
2251 lchState = true;
2252 }
2253 // Update state only if the new state is different
2254 if ((phAcc != null) && (lchState != phAcc.isSet(PhoneAccount.LCH))) {
2255 Call call = getNonRingingLiveCall(sub);
2256 Log.d(this, " setLocal Call Hold to = " + lchState + " sub:" + sub);
2257
2258 if (call != null) {
2259 if (call.getChildCalls().size() > 1) {
2260 // Since for a conference call corresponding telephonyconnection doesn't
2261 // exist, so send lch request on its child call
2262 call = call.getChildCalls().get(0);
2263 }
2264 if (lchState) {
2265 call.setLocalCallHold(true);
2266 } else {
2267 removeFromLch = call;
2268 }
2269 }
2270 if (lchState) {
2271 phAcc.setBit(PhoneAccount.LCH);
2272 } else {
2273 phAcc.unSetBit(PhoneAccount.LCH);
2274 }
2275 }
2276 }
2277 if (removeFromLch != null) {
2278 // Ensure to send LCH disable request at last, to make sure that during switch
2279 // subscription, both subscriptions not to be in active(non-LCH) at any moment.
2280 removeFromLch.setLocalCallHold(false);
2281 }
2282 }
cqiang72e31202015-09-14 17:04:19 +08002283
2284 void resetCdmaConnectionTime(Call call) {
2285 call.setConnectTimeMillis(System.currentTimeMillis());
2286 if (mCalls.contains(call)) {
2287 for (CallsManagerListener listener : mListeners) {
2288 listener.onCallStateChanged(call, CallState.ACTIVE, CallState.ACTIVE);
2289 }
2290 updateForegroundCall();
2291 }
2292 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08002293}