blob: 8d164ecb971734e8b7cf36038bb89ee737c581c7 [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Sailesh Nepalce704b92014-03-17 18:31:43 -070020import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
Santos Cordoncf5b2912014-11-05 21:57:54 -080022import android.os.Handler;
Ihab Awade6dbc9d2015-03-26 10:33:44 -070023import android.os.Looper;
Sandeep Kunta39b77672014-09-01 17:22:57 +053024import android.os.Message;
Rekha Kumard240fe82015-04-01 21:45:57 -070025import android.os.SystemProperties;
Yorke Leee4a9c412014-11-14 16:59:42 -080026import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070027import android.provider.CallLog.Calls;
Yorke Lee2a66f7b2015-05-13 14:21:19 -070028import android.telecom.CallAudioState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080029import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080030import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070031import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070032import android.telecom.GatewayInfo;
33import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070034import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070035import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070036import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070037import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070038import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080039import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070040import android.telephony.TelephonyManager;
Yorke Lee8d3f2692015-05-08 11:44:39 -070041import android.text.TextUtils;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080042
Ihab Awada3653902015-01-23 13:44:46 -080043import com.android.internal.annotations.VisibleForTesting;
Rekha Kumard240fe82015-04-01 21:45:57 -070044import com.android.internal.telephony.PhoneConstants;
45import com.android.internal.telephony.TelephonyProperties;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070046import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080047
Santos Cordon4bc02452014-11-19 15:51:12 -080048import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070049import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070050import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070051import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070052import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070053import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070054import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080055
Ben Giladdd8c6082013-12-30 14:44:08 -080056/**
57 * Singleton.
58 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070059 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080060 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070061 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080062 */
Ihab Awadaa383cc2015-03-22 22:12:28 -070063@VisibleForTesting
Tyler Gunn86014fc2015-06-12 14:31:25 -070064public class CallsManager extends Call.ListenerBase implements VideoProviderProxy.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080065
Santos Cordondf399862014-08-06 04:39:15 -070066 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 interface CallsManagerListener {
68 void onCallAdded(Call call);
69 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070070 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070071 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070072 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070073 ConnectionServiceWrapper oldService,
74 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070076 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070077 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Yorke Lee2a66f7b2015-05-13 14:21:19 -070078 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070079 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070080 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070081 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070082 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080083 void onCanAddCallChanged(boolean canAddCall);
Tyler Gunn86014fc2015-06-12 14:31:25 -070084 void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile);
Ravindra205cb782015-10-07 16:57:53 +053085 void onMergeFailed(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070086 }
87
Tyler Gunn91d43cf2014-09-17 12:19:39 -070088 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080089
Santos Cordonf193ba42014-09-12 06:37:39 -070090 private static final int MAXIMUM_LIVE_CALLS = 1;
91 private static final int MAXIMUM_HOLD_CALLS = 1;
92 private static final int MAXIMUM_RINGING_CALLS = 1;
93 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080094 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Sandeep Kunta39b77672014-09-01 17:22:57 +053095 private static final int MAXIMUM_DSDA_LIVE_CALLS = 2;
96 private static final int MAXIMUM_DSDA_HOLD_CALLS = 2;
97 private static final int MAXIMUM_DSDA_TOP_LEVEL_CALLS = 4;
Santos Cordonf193ba42014-09-12 06:37:39 -070098
99 private static final int[] OUTGOING_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -0700100 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -0700101
Santos Cordon91bd74c2014-11-07 16:10:22 -0800102 private static final int[] LIVE_CALL_STATES =
Sandeep Kunta39b77672014-09-01 17:22:57 +0530103 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING,
104 CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -0800105
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800106 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700107 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
108 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800109 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700110 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
111 * load factor before resizing, 1 means we only expect a single thread to
112 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800113 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700114 private final Set<Call> mCalls = Collections.newSetFromMap(
115 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800116
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700117 private final ConnectionServiceRepository mConnectionServiceRepository;
118 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
119 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700120 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800121 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700122 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800123 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700124 // For this set initial table size to 16 because we add 13 listeners in
125 // the CallsManager constructor.
126 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
127 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700128 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700129 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800130 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700131 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700132 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700133 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700134 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700135 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700136 private final TelecomSystem.SyncRoot mLock;
137 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700138 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700139 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
140 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700141 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800142 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
143 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700144 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700145
Santos Cordon91bd74c2014-11-07 16:10:22 -0800146 private boolean mCanAddCall = true;
147
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700148 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700149 * The call the user is currently interacting with. This is the call that should have audio
150 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800151 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700152 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800153
Sandeep Kunta39b77672014-09-01 17:22:57 +0530154 private static final int LCH_PLAY_DTMF = 100;
155 private static final int LCH_STOP_DTMF = 101;
156 private static final int PHONE_START_DSDA_INCALL_TONE = 102;
157 private static final int LCH_DTMF_PERIODICITY = 3000;
158 private static final int LCH_DTMF_PERIOD = 500;
159 private static final String sSupervisoryCallHoldToneConfig =
160 SystemProperties.get("persist.radio.sch_tone", "none");
161
162 private static InCallTonePlayer.Factory mPlayerFactory;
163 private String mLchSub = null;
164
165 private InCallTonePlayer mLocalCallReminderTonePlayer = null;
166 private InCallTonePlayer mSupervisoryCallHoldTonePlayer = null;
167 private String mSubInConversation = null;
168
Nancy Chena469f762014-12-09 18:29:20 -0800169 private Runnable mStopTone;
170
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700171 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700172 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800173 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700174 CallsManager(
175 Context context,
176 TelecomSystem.SyncRoot lock,
177 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700178 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700179 MissedCallNotifier missedCallNotifier,
180 PhoneAccountRegistrar phoneAccountRegistrar,
181 HeadsetMediaButtonFactory headsetMediaButtonFactory,
182 ProximitySensorManagerFactory proximitySensorManagerFactory,
183 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700184 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700185 mLock = lock;
186 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700187 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700188 mPhoneAccountRegistrar = phoneAccountRegistrar;
189 mMissedCallNotifier = missedCallNotifier;
190 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
191 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800192 mDockManager = new DockManager(context);
193 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700194 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700195 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530196 mPlayerFactory = playerFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700197 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700198 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700199 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800200 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
201 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700202 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700203 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700204 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800205 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700206 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800207 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700208
Santos Cordondeb8c892014-05-30 01:38:03 -0700209 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700210 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700211 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700212 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700213 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700214 mListeners.add(new RingbackPlayer(this, playerFactory));
215 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700216 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700217 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700218 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700219 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700220 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700221
Ihab Awadabcbce42015-04-07 14:04:01 -0700222 mMissedCallNotifier.updateOnStartup(
223 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800224 }
225
Ihab Awad8de76912015-02-17 12:25:52 -0800226 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
227 if (mRespondViaSmsManager != null) {
228 mListeners.remove(mRespondViaSmsManager);
229 }
230 mRespondViaSmsManager = respondViaSmsManager;
231 mListeners.add(respondViaSmsManager);
232 }
233
234 public RespondViaSmsManager getRespondViaSmsManager() {
235 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800236 }
237
Santos Cordon766d04f2014-05-06 10:28:25 -0700238 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700239 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700240 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700241
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700242 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700243 if (!mCalls.contains(call)) {
244 // Call was not added previously in startOutgoingCall due to it being a potential MMI
245 // code, so add it now.
246 addCall(call);
247 }
248
249 // The call's ConnectionService has been updated.
250 for (CallsManagerListener listener : mListeners) {
251 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700252 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700253
254 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700255 }
256
257 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700258 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700259 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700260
Andrew Leee6288a72014-10-01 13:50:02 -0700261 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700262 }
263
264 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700265 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700266 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700267 setCallState(incomingCall, CallState.RINGING, "successful incoming call");
Santos Cordonf193ba42014-09-12 06:37:39 -0700268
Sandeep Kunta39b77672014-09-01 17:22:57 +0530269 if (hasMaximumRingingCalls(incomingCall.getTargetPhoneAccount().getId())) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700270 incomingCall.reject(false, null);
271 // since the call was not added to the list of calls, we have to call the missed
272 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700273 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700274 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
275 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530276 if (TelephonyManager.getDefault().getMultiSimConfiguration()
277 == TelephonyManager.MultiSimVariants.DSDA) {
278 incomingCall.mIsActiveSub = true;
279 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700280 addCall(incomingCall);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530281 setActiveSubscription(incomingCall.getTargetPhoneAccount().getId());
Santos Cordonf193ba42014-09-12 06:37:39 -0700282 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700283 }
284
285 @Override
286 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700287 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700288 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800289 }
290
Ihab Awadcb387ac2014-05-28 16:49:38 -0700291 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700292 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700293 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700294 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
295 addCall(call);
296 }
297
298 @Override
299 public void onFailedUnknownCall(Call call) {
300 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700301 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700302 call.removeListener(this);
303 }
304
305 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700306 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700307 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700308 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700309 }
310 }
311
Evan Charlton352105c2014-06-03 14:10:54 -0700312 @Override
313 public void onPostDialWait(Call call, String remaining) {
314 mInCallController.onPostDialWait(call, remaining);
315 }
316
Santos Cordona1610702014-06-04 20:22:56 -0700317 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800318 public void onPostDialChar(final Call call, char nextChar) {
319 if (PhoneNumberUtils.is12Key(nextChar)) {
320 // Play tone if it is one of the dialpad digits, canceling out the previously queued
321 // up stopTone runnable since playing a new tone automatically stops the previous tone.
322 if (mStopTone != null) {
323 mHandler.removeCallbacks(mStopTone);
324 }
325
326 mDtmfLocalTonePlayer.playTone(call, nextChar);
327
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700328 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800329 mStopTone = new Runnable() {
330 @Override
331 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700332 synchronized (mLock) {
333 // Set a timeout to stop the tone in case there isn't another tone to follow.
334 mDtmfLocalTonePlayer.stopTone(call);
335 }
Nancy Chena469f762014-12-09 18:29:20 -0800336 }
337 };
338 mHandler.postDelayed(
339 mStopTone,
340 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
341 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
342 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
343 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
344 // the previous tone is being stopped anyway.
345 if (mStopTone != null) {
346 mHandler.removeCallbacks(mStopTone);
347 }
348 mDtmfLocalTonePlayer.stopTone(call);
349 } else {
350 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
351 }
352 }
353
354 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700355 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700356 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800357 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700358 for (CallsManagerListener listener : mListeners) {
359 listener.onIsConferencedChanged(call);
360 }
361 }
362
363 @Override
364 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700365 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800366 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700367 for (CallsManagerListener listener : mListeners) {
368 listener.onIsConferencedChanged(call);
369 }
370 }
371
Ihab Awadff7493a2014-06-10 13:47:44 -0700372 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700373 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700374 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700375 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700376 }
377 }
378
Andrew Lee4a796602014-07-11 17:23:03 -0700379 @Override
380 public void onVideoStateChanged(Call call) {
381 for (CallsManagerListener listener : mListeners) {
382 listener.onVideoStateChanged(call);
383 }
384 }
385
Santos Cordoncf5b2912014-11-05 21:57:54 -0800386 @Override
387 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
388 mPendingCallsToDisconnect.add(call);
389 mHandler.postDelayed(new Runnable() {
390 @Override
391 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700392 synchronized (mLock) {
393 if (mPendingCallsToDisconnect.remove(call)) {
394 Log.i(this, "Delayed disconnection of call: %s", call);
395 call.disconnect();
396 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800397 }
398 }
399 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
400
401 return true;
402 }
403
Tyler Gunn86014fc2015-06-12 14:31:25 -0700404 /**
405 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
406 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
407 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
408 * respond to callbacks from the {@link VideoProviderProxy}.
409 *
410 * @param call The call.
411 */
412 @Override
413 public void onVideoCallProviderChanged(Call call) {
414 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
415
416 if (videoProviderProxy == null) {
417 return;
418 }
419
420 videoProviderProxy.addListener(this);
421 }
422
423 /**
424 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
425 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
426 * modification request.
427 *
428 * @param call The call.
429 * @param videoProfile The {@link VideoProfile}.
430 */
431 @Override
432 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
433 int videoState = videoProfile != null ? videoProfile.getVideoState() :
434 VideoProfile.STATE_AUDIO_ONLY;
435 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
436 .videoStateToString(videoState));
437
438 for (CallsManagerListener listener : mListeners) {
439 listener.onSessionModifyRequestReceived(call, videoProfile);
440 }
441 }
442
Santos Cordon4bc02452014-11-19 15:51:12 -0800443 Collection<Call> getCalls() {
444 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700445 }
446
447 Call getForegroundCall() {
448 return mForegroundCall;
449 }
450
Santos Cordonae193062014-05-21 21:21:49 -0700451 Ringer getRinger() {
452 return mRinger;
453 }
454
Santos Cordonf3671a62014-05-29 21:51:53 -0700455 InCallController getInCallController() {
456 return mInCallController;
457 }
458
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700459 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700460 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700461 if (call.isEmergencyCall()) {
462 return true;
463 }
464 }
465 return false;
466 }
467
Andrew Lee45506232014-10-22 17:54:33 -0700468 boolean hasVideoCall() {
469 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700470 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700471 return true;
472 }
473 }
474 return false;
475 }
476
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700477 CallAudioState getAudioState() {
478 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700479 }
480
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700481 boolean isTtySupported() {
482 return mTtyManager.isTtySupported();
483 }
484
485 int getCurrentTtyMode() {
486 return mTtyManager.getCurrentTtyMode();
487 }
488
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700489 void addListener(CallsManagerListener listener) {
490 mListeners.add(listener);
491 }
492
493 void removeListener(CallsManagerListener listener) {
494 mListeners.remove(listener);
495 }
496
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800497 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700498 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800499 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700500 * @param phoneAccountHandle The phone account which contains the component name of the
501 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800502 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800503 */
Evan Charlton89176372014-07-19 18:23:09 -0700504 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800505 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700506 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
507 if (handle == null) {
508 // Required for backwards compatibility
509 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
510 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700511 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700512 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800513 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700514 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700515 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700516 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700517 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700518 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700519 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700520 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700521 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700522 true /* isIncoming */,
523 false /* isConference */);
524
Andrew Lee1b906652015-06-01 11:44:55 -0700525 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700526 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700527 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700528 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800529 }
530
Yorke Lee9250e5f2014-10-01 13:39:09 -0700531 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
532 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
533 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
534 Call call = new Call(
535 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800536 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700537 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700538 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700539 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700540 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700541 handle,
542 null /* gatewayInfo */,
543 null /* connectionManagerPhoneAccount */,
544 phoneAccountHandle,
545 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
546 // to the existing connection instead of trying to create a new one.
547 true /* isIncoming */,
548 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700549 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700550 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700551 call.addListener(this);
552 call.startCreateConnection(mPhoneAccountRegistrar);
553 }
554
Yorke Lee8d3f2692015-05-08 11:44:39 -0700555 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
556 if (handle1 == null || handle2 == null) {
557 return handle1 == handle2;
558 }
559
560 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
561 return false;
562 }
563
564 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
565 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
566 return TextUtils.equals(number1, number2);
567 }
568
Santos Cordoncf5b2912014-11-05 21:57:54 -0800569 private Call getNewOutgoingCall(Uri handle) {
570 // First check to see if we can reuse any of the calls that are waiting to disconnect.
571 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800572 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800573 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700574 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800575 mPendingCallsToDisconnect.remove(pendingCall);
576 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800577 reusedCall = pendingCall;
578 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700579 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800580 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800581 }
582 }
Yorke Lee59979512014-12-02 01:03:40 -0800583 if (reusedCall != null) {
584 return reusedCall;
585 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800586
587 // Create a call with original handle. The handle may be changed when the call is attached
588 // to a connection service, but in most cases will remain the same.
589 return new Call(
590 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800591 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700592 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800593 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700594 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700595 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800596 handle,
597 null /* gatewayInfo */,
598 null /* connectionManagerPhoneAccount */,
599 null /* phoneAccountHandle */,
600 false /* isIncoming */,
601 false /* isConference */);
602 }
603
Nancy Chen0d3076c2014-07-30 14:45:44 -0700604 /**
605 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
606 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700607 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700608 * @param phoneAccountHandle The phone account which contains the component name of the
609 * connection service to use for this call.
610 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700611 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700612 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800613 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700614
Garik Badalyan7e1524a2015-08-25 11:08:18 -0700615 if (extras!=null) {
616 call.setVideoState(extras.getInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
617 VideoProfile.STATE_AUDIO_ONLY));
618 }
619
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530620 boolean isAddParticipant = ((extras != null) && (extras.getBoolean(
621 TelephonyProperties.ADD_PARTICIPANT_KEY, false)));
622 boolean isSkipSchemaOrConfUri = ((extras != null) && (extras.getBoolean(
623 TelephonyProperties.EXTRA_SKIP_SCHEMA_PARSING, false) ||
624 extras.getBoolean(TelephonyProperties.EXTRA_DIAL_CONFERENCE_URI, false)));
625
626 if (isAddParticipant) {
627 String number = handle.getSchemeSpecificPart();
628 if (!isSkipSchemaOrConfUri) {
629 number = PhoneNumberUtils.stripSeparators(number);
630 }
631 addParticipant(number);
632 mInCallController.bringToForeground(false);
633 return null;
634 }
635
636 // Force tel scheme for ims conf uri/skip schema calls to avoid selection of sip accounts
637 String scheme = (isSkipSchemaOrConfUri? PhoneAccount.SCHEME_TEL: handle.getScheme());
638
639 Log.d(this, "startOutgoingCall :: isAddParticipant=" + isAddParticipant
640 + " isSkipSchemaOrConfUri=" + isSkipSchemaOrConfUri + " scheme=" + scheme);
641
Nancy Chen1c5926f2014-09-17 14:44:14 -0700642 List<PhoneAccountHandle> accounts =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530643 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(scheme, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700644
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800645 Log.v(this, "startOutgoingCall found accounts = " + accounts);
646
Sandeep Kunta39b77672014-09-01 17:22:57 +0530647 if (mForegroundCall != null && TelephonyManager.getDefault().getMultiSimConfiguration()
648 != TelephonyManager.MultiSimVariants.DSDA) {
Roshan Pius1a373832015-08-07 11:39:05 -0700649 Call ongoingCall = mForegroundCall;
Nancy Chenbc95db92014-11-24 13:19:18 -0800650 // If there is an ongoing call, use the same phone account to place this new call.
Roshan Pius1a373832015-08-07 11:39:05 -0700651 // If the ongoing call is a conference call, we fetch the phone account from the
652 // child calls because we don't have targetPhoneAccount set on Conference calls.
653 // TODO: Set targetPhoneAccount for all conference calls (b/23035408).
654 if (ongoingCall.getTargetPhoneAccount() == null &&
655 !ongoingCall.getChildCalls().isEmpty()) {
656 ongoingCall = ongoingCall.getChildCalls().get(0);
657 }
658 if (ongoingCall.getTargetPhoneAccount() != null) {
659 phoneAccountHandle = ongoingCall.getTargetPhoneAccount();
660 }
Nancy Chenbc95db92014-11-24 13:19:18 -0800661 }
662
Nancy Chenbc9ef172014-08-15 17:11:57 -0700663 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
664 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700665 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700666 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700667 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700668 phoneAccountHandle = null;
669 }
670 }
671
672 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700673 // No preset account, check if default exists that supports the URI scheme for the
674 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700675 phoneAccountHandle =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530676 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(scheme);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700677 }
678
Nancy Chen1c5926f2014-09-17 14:44:14 -0700679 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700680
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700681 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
682 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700683 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700684
685 // Do not support any more live calls. Our options are to move a call to hold, disconnect
686 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700687 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700688 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800689 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800690 if (mCalls.contains(call)) {
691 // This call can already exist if it is a reused call,
692 // See {@link #getNewOutgoingCall}.
693 call.disconnect();
694 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700695 return null;
696 }
697
Nancy Chen8d92f452014-10-20 21:43:44 -0700698 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
699 !isEmergencyCall;
700
701 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700702 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700703 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700704 // Create our own instance to modify (since extras may be Bundle.EMPTY)
705 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700706 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700707 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700708 call.setState(
709 CallState.CONNECTING,
710 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700711 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700712
Andrew Lee1b906652015-06-01 11:44:55 -0700713 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700714
Tyler Gunn504eb832014-10-13 14:15:27 -0700715 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700716 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700717 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800718 } else if (!mCalls.contains(call)) {
719 // We check if mCalls already contains the call because we could potentially be reusing
720 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700721 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700722 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700723
724 return call;
725 }
726
Ben Gilada0d9f752014-02-26 11:49:03 -0800727 /**
Yorke Lee33501632014-03-17 19:24:12 -0700728 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800729 *
Yorke Lee33501632014-03-17 19:24:12 -0700730 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700731 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700732 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700733 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700734 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800735 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700736 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
737 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700738 if (call == null) {
739 // don't do anything if the call no longer exists
740 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700741 return;
742 }
743
Nancy Chen201b4372014-09-08 14:18:24 -0700744 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700745
746 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700747 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700748 } else {
749 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
750 Log.pii(uriHandle), Log.pii(handle));
751 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700752
Nancy Chen0d3076c2014-07-30 14:45:44 -0700753 call.setHandle(uriHandle);
754 call.setGatewayInfo(gatewayInfo);
Tyler Gunnd373c202015-05-20 08:21:03 -0700755 // Auto-enable speakerphone if the originating intent specified to do so, or if the call
Nivedita Sarkardae2c652015-08-31 10:55:51 -0700756 // is a video call or if the phone is docked.
757 call.setStartWithSpeakerphoneOn(speakerphoneOn || isSpeakerphoneAutoEnabled(videoState)
758 || mDockManager.isDocked());
Tyler Gunnc4abd912014-07-08 14:22:10 -0700759 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700760
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700761 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
762 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700763 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700764 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700765 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700766 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700767
Santos Cordonf193ba42014-09-12 06:37:39 -0700768 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530769 if (!isEmergencyCall) {
770 updateLchStatus(call.getTargetPhoneAccount().getId());
771 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700772 // If the account has been set, proceed to place the outgoing call.
773 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700774 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700775 }
Yorke Leef98fb572014-03-05 10:56:55 -0800776 }
777
778 /**
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530779 * Attempts to add participant in a call.
780 *
781 * @param number number to connect the call with.
782 */
783 private void addParticipant(String number) {
784 Log.i(this, "addParticipant number ="+number);
785 if (getForegroundCall() == null) {
786 // don't do anything if the call no longer exists
787 Log.i(this, "Canceling unknown call.");
788 return;
789 } else {
790 getForegroundCall().addParticipantWithConference(number);
791 }
792 }
793
794
795 /**
Santos Cordona1610702014-06-04 20:22:56 -0700796 * Attempts to start a conference call for the specified call.
797 *
Santos Cordon12d61822014-07-29 16:02:20 -0700798 * @param call The call to conference.
799 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700800 */
Santos Cordon12d61822014-07-29 16:02:20 -0700801 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700802 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700803 }
804
805 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700806 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
807 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800808 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700809 *
810 * @param call The call to answer.
811 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800812 */
Andrew Lee38931d02014-07-16 10:17:36 -0700813 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700814 if (!mCalls.contains(call)) {
815 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800816 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530817 Call activeCall = getFirstCallWithState(call.getTargetPhoneAccount()
818 .getId(), CallState.ACTIVE, CallState.DIALING);
Santos Cordon40f78c22014-04-07 02:11:42 -0700819 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700820 // STATE_DIALING, put it on hold before answering the call.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530821 if (activeCall != null && activeCall != call &&
822 (activeCall.isActive() ||
823 activeCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800824 if (0 == (mForegroundCall.getConnectionCapabilities()
825 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700826 // This call does not support hold. If it is from a different connection
827 // service, then disconnect it, otherwise allow the connection service to
828 // figure out the right states.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530829 if (activeCall.getConnectionService() != call.getConnectionService()) {
830 activeCall.disconnect();
Santos Cordonf193ba42014-09-12 06:37:39 -0700831 }
832 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800833 Call heldCall = getHeldCall();
834 if (heldCall != null) {
835 Log.v(this, "Disconnecting held call %s before holding active call.",
836 heldCall);
837 heldCall.disconnect();
838 }
839
Santos Cordonf193ba42014-09-12 06:37:39 -0700840 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
841 mForegroundCall, call);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530842 activeCall.hold();
Santos Cordonf193ba42014-09-12 06:37:39 -0700843 }
Santos Cordondf399862014-08-06 04:39:15 -0700844 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700845 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700846 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700847 }
848
Santos Cordona56f2762014-03-24 15:55:53 -0700849 for (CallsManagerListener listener : mListeners) {
850 listener.onIncomingCallAnswered(call);
851 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530852 updateLchStatus(call.getTargetPhoneAccount().getId());
Santos Cordon61d0f702014-02-19 02:52:23 -0800853 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700854 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700855 call.answer(videoState);
Tyler Gunnd373c202015-05-20 08:21:03 -0700856 if (isSpeakerphoneAutoEnabled(videoState)) {
Rekha Kumard240fe82015-04-01 21:45:57 -0700857 call.setStartWithSpeakerphoneOn(true);
858 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800859 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800860 }
861
Tyler Gunnd373c202015-05-20 08:21:03 -0700862 /**
863 * Determines if the speakerphone should be automatically enabled for the call. Speakerphone
864 * should be enabled if the call is a video call and bluetooth or the wired headset are not in
865 * use.
866 *
867 * @param videoState The video state of the call.
868 * @return {@code true} if the speakerphone should be enabled.
869 */
870 private boolean isSpeakerphoneAutoEnabled(int videoState) {
Etan Cohen7bcbb342015-06-25 15:39:21 -0700871 return VideoProfile.isVideo(videoState) &&
Tyler Gunnd373c202015-05-20 08:21:03 -0700872 !mWiredHeadsetManager.isPluggedIn() &&
873 !mCallAudioManager.isBluetoothDeviceAvailable() &&
874 isSpeakerEnabledForVideoCalls();
875 }
876
877 /**
878 * Determines if the speakerphone should be automatically enabled for video calls.
879 *
880 * @return {@code true} if the speakerphone should automatically be enabled.
881 */
Rekha Kumard240fe82015-04-01 21:45:57 -0700882 private static boolean isSpeakerEnabledForVideoCalls() {
883 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
884 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
885 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
886 }
887
Santos Cordone3d76ab2014-01-28 17:25:20 -0800888 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700889 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
890 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800891 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800892 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700893 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700894 if (!mCalls.contains(call)) {
895 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800896 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700897 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700898 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700899 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530900 setActiveSubscription(getConversationSub());
Ihab Awadff7493a2014-06-10 13:47:44 -0700901 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800902 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800903 }
904
905 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700906 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700907 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700908 * @param digit The DTMF digit to play.
909 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700910 void playDtmfTone(Call call, char digit) {
911 if (!mCalls.contains(call)) {
912 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700913 } else {
914 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700915 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700916 }
917 }
918
919 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700920 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700921 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700922 void stopDtmfTone(Call call) {
923 if (!mCalls.contains(call)) {
924 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700925 } else {
926 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700927 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700928 }
929 }
930
931 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700932 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700933 */
Evan Charlton352105c2014-06-03 14:10:54 -0700934 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700935 if (!mCalls.contains(call)) {
936 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700937 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700938 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700939 }
940 }
941
942 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700943 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800944 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
945 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800946 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700947 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700948 Log.v(this, "disconnectCall %s", call);
949
Sailesh Nepale59bb192014-04-01 18:33:59 -0700950 if (!mCalls.contains(call)) {
951 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800952 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700953 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800954 call.disconnect();
955 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800956 }
Santos Cordon681663d2014-01-30 04:32:15 -0800957
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700958 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700959 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700960 */
961 void disconnectAllCalls() {
962 Log.v(this, "disconnectAllCalls");
963
964 for (Call call : mCalls) {
965 disconnectCall(call);
966 }
967 }
968
Nancy Chenbc9ef172014-08-15 17:11:57 -0700969
Tyler Gunn61b92102014-08-19 07:42:20 -0700970 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700971 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700972 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
973 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700974 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700975 void holdCall(Call call) {
976 if (!mCalls.contains(call)) {
977 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700978 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700979 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700980 call.hold();
981 }
982 }
983
984 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700985 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700986 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
987 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700988 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700989 void unholdCall(Call call) {
990 if (!mCalls.contains(call)) {
991 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700992 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700993 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700994 for (Call c : mCalls) {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530995 PhoneAccountHandle ph = call.getTargetPhoneAccount();
996 PhoneAccountHandle ph1 = c.getTargetPhoneAccount();
Tyler Gunne44adb42015-01-02 10:55:35 -0800997 // Only attempt to hold parent calls and not the individual children.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530998 // if 'c' is not for same subscription as call, then don't disturb 'c'
999 if (c != null && c.isAlive() && c != call && c.getParentCall() == null
1000 && (ph != null && ph1 != null &&
1001 isSamePhAccIdOrSipId(ph.getId(), ph1.getId()))) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001002 c.hold();
1003 }
1004 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001005 call.unhold();
1006 }
1007 }
1008
Sandeep Kunta39b77672014-09-01 17:22:57 +05301009 /**
1010 * Returns true if the ids are same or one of the ids is sip id.
1011 */
1012 private boolean isSamePhAccIdOrSipId(String id1, String id2) {
1013 boolean ret = ((id1 != null && id2 != null) &&
1014 (id1.equals(id2) || id1.contains("sip") || id2.contains("sip")));
1015 Log.d(this, "isSamePhAccIdOrSipId: id1 = " + id1 + " id2 = " + id2 + " ret = " + ret);
1016 return ret;
1017 }
1018
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001019 /** Called by the in-call UI to change the mute state. */
1020 void mute(boolean shouldMute) {
1021 mCallAudioManager.mute(shouldMute);
1022 }
1023
1024 /**
1025 * Called by the in-call UI to change the audio route, for example to change from earpiece to
1026 * speaker phone.
1027 */
1028 void setAudioRoute(int route) {
1029 mCallAudioManager.setAudioRoute(route);
1030 }
1031
Yorke Leed1346872014-07-28 14:38:45 -07001032 /** Called by the in-call UI to turn the proximity sensor on. */
1033 void turnOnProximitySensor() {
1034 mProximitySensorManager.turnOn();
1035 }
1036
1037 /**
1038 * Called by the in-call UI to turn the proximity sensor off.
1039 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
1040 * the screen will be kept off until the proximity sensor goes negative.
1041 */
1042 void turnOffProximitySensor(boolean screenOnImmediately) {
1043 mProximitySensorManager.turnOff(screenOnImmediately);
1044 }
1045
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001046 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -07001047 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001048 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -07001049 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -07001050 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -07001051 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -07001052 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
1053 // respecting a rewritten number or a canceled number. This is unlikely since
1054 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
1055 // a phone account from the in-call UI.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301056 Log.i(this, "phoneAccountSelected , id = %s", account.getId());
1057 updateLchStatus(account.getId());
Ihab Awadb78b2762014-07-25 15:16:23 -07001058 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -07001059
1060 // Note: emergency calls never go through account selection dialog so they never
1061 // arrive here.
1062 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001063 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -07001064 } else {
1065 call.disconnect();
1066 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001067
1068 if (setDefault) {
1069 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
1070 }
Nancy Chen53ceedc2014-07-08 18:56:51 -07001071 }
1072 }
1073
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001074 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001075 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001076 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001077 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001078 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001079 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001080 }
1081
Sailesh Nepale59bb192014-04-01 18:33:59 -07001082 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001083 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -08001084 }
1085
Sailesh Nepale59bb192014-04-01 18:33:59 -07001086 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001087 setCallState(call, CallState.DIALING, "dialing set explicitly");
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001088 maybeMoveToEarpiece(call);
Santos Cordond6782cd2015-04-16 09:57:50 -07001089 maybeMoveToSpeakerPhone(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301090 setActiveSubscription(call.getTargetPhoneAccount().getId());
Santos Cordon681663d2014-01-30 04:32:15 -08001091 }
1092
Sailesh Nepale59bb192014-04-01 18:33:59 -07001093 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001094 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -07001095 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -08001096 }
1097
Sailesh Nepale59bb192014-04-01 18:33:59 -07001098 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001099 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001100 }
1101
Santos Cordon049b7b62014-01-30 05:34:26 -08001102 /**
Santos Cordonf193ba42014-09-12 06:37:39 -07001103 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
1104 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -08001105 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001106 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -08001107 */
Andrew Lee701dc002014-09-11 21:29:12 -07001108 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
Suresh Koletief564312015-08-24 19:31:55 +05301109 // Show MT call in call log as a missed call if immediately disconnected
1110 // before creating a connection.
1111 if (!mCalls.contains(call) && (DisconnectCause.MISSED == disconnectCause.getCode())) {
1112 addCall(call);
1113 mMissedCallNotifier.showMissedCallNotification(call);
1114 }
Andrew Lee701dc002014-09-11 21:29:12 -07001115 call.setDisconnectCause(disconnectCause);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301116 int prevState = call.getState();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001117 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sandeep Kunta39b77672014-09-01 17:22:57 +05301118 String activeSub = getActiveSubscription();
1119 String conversationSub = getConversationSub();
1120 String lchSub = IsAnySubInLch();
1121
1122 PhoneAccount phAcc =
1123 getPhoneAccountRegistrar().getPhoneAccount(call.getTargetPhoneAccount());
1124 if (activeSub != null && (call.getTargetPhoneAccount() != null &&
1125 call.getTargetPhoneAccount().getId().equals(activeSub)) &&
1126 (phAcc != null) && (phAcc.isSet(PhoneAccount.LCH)) &&
1127 (conversationSub != null) &&
1128 (!conversationSub.equals(activeSub))) {
1129 Log.d(this,"Set active sub to conversation sub");
1130 setActiveSubscription(conversationSub);
1131 } else if ((conversationSub == null) && (lchSub != null) &&
1132 ((prevState == CallState.CONNECTING) || (prevState ==
1133 CallState.SELECT_PHONE_ACCOUNT)) &&
1134 (call.getState() == CallState.DISCONNECTED)) {
1135 Log.d(this,"remove sub with call from LCH");
1136 updateLchStatus(lchSub);
1137 setActiveSubscription(lchSub);
1138 manageDsdaInCallTones(false);
1139 }
1140
1141 if ((call.getTargetPhoneAccount() != null) && (phAcc != null) &&
1142 (phAcc.isSet(PhoneAccount.LCH))) {
1143 Call activecall = getFirstCallWithState(call.getTargetPhoneAccount().getId(),
1144 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1145 Log.d(this,"activecall: " + activecall);
1146 if (activecall == null) {
1147 phAcc.unSetBit(PhoneAccount.LCH);
1148 manageDsdaInCallTones(false);
1149 }
1150 }
1151 }
1152
1153 private String IsAnySubInLch() {
1154 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
1155 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
1156 if ((phAcc != null) && (phAcc.isSet(PhoneAccount.LCH))) {
1157 Log.d(this, "Sub in LCH: " + ph.getId());
1158 return ph.getId();
1159 }
1160 }
1161 return null;
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -07001162 }
1163
Ben Gilada0d9f752014-02-26 11:49:03 -08001164 /**
Ihab Awada02bef52014-08-13 18:18:42 -07001165 * Removes an existing disconnected call, and notifies the in-call app.
1166 */
1167 void markCallAsRemoved(Call call) {
1168 removeCall(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301169 if (!hasAnyCalls()) {
1170 updateLchStatus(null);
1171 setActiveSubscription(null);
1172 manageDsdaInCallTones(false);
1173 }
Ihab Awad5b281322014-09-25 18:25:29 -07001174 if (mLocallyDisconnectingCalls.contains(call)) {
1175 mLocallyDisconnectingCalls.remove(call);
1176 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1177 mForegroundCall.unhold();
1178 }
1179 }
Ihab Awada02bef52014-08-13 18:18:42 -07001180 }
1181
1182 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001183 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001184 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001185 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001186 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001187 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001188 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001189 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001190 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001191 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001192 if (call.getState() != CallState.DISCONNECTED) {
1193 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1194 }
1195 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001196 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001197 }
1198 }
1199 }
1200
Santos Cordondeb8c892014-05-30 01:38:03 -07001201 boolean hasAnyCalls() {
1202 return !mCalls.isEmpty();
1203 }
1204
Santos Cordonc7e85d42014-05-22 02:51:48 -07001205 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001206 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001207 }
1208
Sandeep Kunta39b77672014-09-01 17:22:57 +05301209 boolean hasActiveOrHoldingCall(String sub) {
1210 return (getFirstCallWithState(sub, CallState.ACTIVE, CallState.ON_HOLD) != null);
1211 }
1212
Santos Cordonc7e85d42014-05-22 02:51:48 -07001213 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001214 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001215 }
1216
Santos Cordondeb8c892014-05-30 01:38:03 -07001217 boolean onMediaButton(int type) {
1218 if (hasAnyCalls()) {
1219 if (HeadsetMediaButton.SHORT_PRESS == type) {
1220 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1221 if (ringingCall == null) {
1222 mCallAudioManager.toggleMute();
1223 return true;
1224 } else {
Andrew Lee38931d02014-07-16 10:17:36 -07001225 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -07001226 return true;
1227 }
1228 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1229 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1230 Call callToHangup = getFirstCallWithState(
1231 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1232 if (callToHangup != null) {
1233 callToHangup.disconnect();
1234 return true;
1235 }
1236 }
1237 }
1238 return false;
1239 }
1240
1241 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001242 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001243 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001244 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -08001245 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1246 return false;
1247 }
1248
Santos Cordon91bd74c2014-11-07 16:10:22 -08001249 int count = 0;
1250 for (Call call : mCalls) {
1251 if (call.isEmergencyCall()) {
1252 // We never support add call if one of the calls is an emergency call.
1253 return false;
Roshan Piusd9544092015-07-31 11:31:52 -07001254 } else if (!call.getChildCalls().isEmpty() && !call.can(Connection.CAPABILITY_HOLD)) {
1255 // This is to deal with CDMA conference calls. CDMA conference calls do not
1256 // allow the addition of another call when it is already in a 3 way conference.
1257 // So, we detect that it is a CDMA conference call by checking if the call has
1258 // some children and it does not support the CAPABILILTY_HOLD
1259 // TODO: This maybe cleaner if the lower layers can explicitly signal to telecom
1260 // about this limitation (b/22880180).
1261 return false;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001262 } else if (call.getParentCall() == null) {
1263 count++;
1264 }
Santos Cordon10838c22014-06-11 17:36:04 -07001265
Santos Cordon91bd74c2014-11-07 16:10:22 -08001266 // We do not check states for canAddCall. We treat disconnected calls the same
1267 // and wait until they are removed instead. If we didn't count disconnected calls,
1268 // we could put InCallServices into a state where they are showing two calls but
1269 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1270 // and acts better if we wait until the call is removed.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301271 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1272 == TelephonyManager.MultiSimVariants.DSDA) {
1273 if (count >= MAXIMUM_DSDA_TOP_LEVEL_CALLS) {
1274 return false;
1275 }
1276 } else if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001277 return false;
1278 }
1279 }
1280 return true;
1281 }
1282
Ihab Awada3653902015-01-23 13:44:46 -08001283 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001284 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001285 return getFirstCallWithState(CallState.RINGING);
1286 }
1287
1288 Call getActiveCall() {
1289 return getFirstCallWithState(CallState.ACTIVE);
1290 }
1291
Nancy Chen05a9e402014-09-26 14:14:32 -07001292 Call getDialingCall() {
1293 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001294 }
1295
1296 Call getHeldCall() {
1297 return getFirstCallWithState(CallState.ON_HOLD);
1298 }
1299
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001300 int getNumHeldCalls() {
1301 int count = 0;
1302 for (Call call : mCalls) {
1303 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1304 count++;
1305 }
1306 }
1307 return count;
1308 }
1309
Roshan Pius93c9c012015-08-28 11:10:56 -07001310 Call getOutgoingCall() {
1311 return getFirstCallWithState(OUTGOING_CALL_STATES);
1312 }
1313
Santos Cordonf193ba42014-09-12 06:37:39 -07001314 Call getFirstCallWithState(int... states) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301315 return getFirstCallWithState((Call) null, states);
Santos Cordonf193ba42014-09-12 06:37:39 -07001316 }
1317
Santos Cordon10838c22014-06-11 17:36:04 -07001318 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001319 * Returns the first call that it finds with the given states. The states are treated as having
1320 * priority order so that any call with the first state will be returned before any call with
1321 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001322 *
1323 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001324 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001325 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001326 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001327 // check the foreground first
1328 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1329 return mForegroundCall;
1330 }
1331
Santos Cordondeb8c892014-05-30 01:38:03 -07001332 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001333 if (Objects.equals(callToSkip, call)) {
1334 continue;
1335 }
1336
1337 // Only operate on top-level calls
1338 if (call.getParentCall() != null) {
1339 continue;
1340 }
1341
Santos Cordondeb8c892014-05-30 01:38:03 -07001342 if (currentState == call.getState()) {
1343 return call;
1344 }
1345 }
1346 }
1347 return null;
1348 }
1349
Sandeep Kunta39b77672014-09-01 17:22:57 +05301350 /**
1351 * Returns the first call that it finds with the given states for given subscription.
1352 * the states are treated as having priority order so that any call with the first
1353 * state will be returned before any call with states listed later in the parameter list.
1354 *
1355 * @param subId check calls only on this subscription
1356 * @param callToSkip Call that this method should skip while searching
1357 */
1358 Call getFirstCallWithState(String subId, Call callToSkip, int... states) {
1359 for (int currentState : states) {
1360 // check the foreground first
1361 if (mForegroundCall != null && mForegroundCall.getState() == currentState
1362 && mForegroundCall.getTargetPhoneAccount() != null
1363 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(), subId)) {
1364 return mForegroundCall;
1365 }
1366
1367 for (Call call : mCalls) {
1368 if (Objects.equals(callToSkip, call)) {
1369 continue;
1370 }
1371
1372 // Only operate on top-level calls
1373 if (call.getParentCall() != null) {
1374 continue;
1375 }
1376
1377 if (currentState == call.getState() && call.getTargetPhoneAccount() != null
1378 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1379 return call;
1380 }
1381 }
1382 }
1383 return null;
1384 }
1385
Santos Cordon0fbe6322014-08-14 04:04:25 -07001386 Call createConferenceCall(
1387 PhoneAccountHandle phoneAccount,
1388 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001389
1390 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1391 // which is the default value for new Calls.
1392 long connectTime =
1393 parcelableConference.getConnectTimeMillis() ==
1394 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1395 parcelableConference.getConnectTimeMillis();
1396
Santos Cordon0fbe6322014-08-14 04:04:25 -07001397 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001398 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001399 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001400 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001401 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001402 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001403 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001404 null /* handle */,
1405 null /* gatewayInfo */,
1406 null /* connectionManagerPhoneAccount */,
1407 phoneAccount,
1408 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001409 true /* isConference */,
1410 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001411
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001412 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1413 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001414 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001415 call.setVideoState(parcelableConference.getVideoState());
1416 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001417 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001418 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001419
1420 // TODO: Move this to be a part of addCall()
1421 call.addListener(this);
1422 addCall(call);
1423 return call;
1424 }
1425
Yorke Leef86db2e2014-09-12 17:56:48 -07001426 /**
1427 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1428 */
1429 int getCallState() {
1430 return mPhoneStateBroadcaster.getCallState();
1431 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001432
Santos Cordon4b2c1192014-03-19 18:15:38 -07001433 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001434 * Retrieves the {@link PhoneAccountRegistrar}.
1435 *
1436 * @return The {@link PhoneAccountRegistrar}.
1437 */
1438 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1439 return mPhoneAccountRegistrar;
1440 }
1441
1442 /**
1443 * Retrieves the {@link MissedCallNotifier}
1444 * @return The {@link MissedCallNotifier}.
1445 */
1446 MissedCallNotifier getMissedCallNotifier() {
1447 return mMissedCallNotifier;
1448 }
1449
1450 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001451 * Adds the specified call to the main list of live calls.
1452 *
1453 * @param call The call to add.
1454 */
1455 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001456 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001457 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001458 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001459 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001460
Santos Cordondf399862014-08-06 04:39:15 -07001461 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001462 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001463 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001464 if (Log.SYSTRACE_DEBUG) {
1465 Trace.beginSection(listener.getClass().toString() + " addCall");
1466 }
Santos Cordona56f2762014-03-24 15:55:53 -07001467 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001468 if (Log.SYSTRACE_DEBUG) {
1469 Trace.endSection();
1470 }
Santos Cordona56f2762014-03-24 15:55:53 -07001471 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001472 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001473 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001474 }
1475
Sailesh Nepal810735e2014-03-18 18:15:46 -07001476 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001477 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001478 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001479
Santos Cordon672321e2014-08-21 14:38:58 -07001480 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001481 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001482 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001483
1484 boolean shouldNotify = false;
1485 if (mCalls.contains(call)) {
1486 mCalls.remove(call);
1487 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001488 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001489
Santos Cordon2685dab2015-04-17 17:12:09 -07001490 call.destroy();
1491
Sailesh Nepale59bb192014-04-01 18:33:59 -07001492 // Only broadcast changes for calls that are being tracked.
1493 if (shouldNotify) {
1494 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001495 if (Log.SYSTRACE_DEBUG) {
1496 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1497 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001498 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001499 if (Log.SYSTRACE_DEBUG) {
1500 Trace.endSection();
1501 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001502 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001503 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001504 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001505 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001506 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001507
Sailesh Nepal810735e2014-03-18 18:15:46 -07001508 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001509 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001510 *
1511 * @param call The call.
1512 * @param newState The new state of the call.
1513 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001514 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001515 if (call == null) {
1516 return;
1517 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001518 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001519 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1520 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001521 if (newState != oldState) {
1522 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1523 // 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 -07001524 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001525 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001526 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001527 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001528 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001529 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001530
Yorke Leee4a9c412014-11-14 16:59:42 -08001531 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001532 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001533 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001534 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001535 if (Log.SYSTRACE_DEBUG) {
1536 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1537 }
Santos Cordona56f2762014-03-24 15:55:53 -07001538 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001539 if (Log.SYSTRACE_DEBUG) {
1540 Trace.endSection();
1541 }
Santos Cordona56f2762014-03-24 15:55:53 -07001542 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001543 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001544 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001545 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001546 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301547 manageDsdaInCallTones(false);
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001548 }
1549
1550 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001551 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001552 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001553 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001554 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001555 Call newForegroundCall = null;
Santos Cordondf399862014-08-06 04:39:15 -07001556 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001557 // of its state will be foreground by default and instead the connection service should
1558 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001559 // the call should play audio and listen to microphone if it wants.
1560
Sandeep Kunta39b77672014-09-01 17:22:57 +05301561 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1562 == TelephonyManager.MultiSimVariants.DSDA) {
1563 String lchSub = getLchSub();
1564 for (Call call : mCalls) {
1565 // Only top-level calls can be in foreground
1566 if (call.getParentCall() != null) {
1567 continue;
1568 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001569
Sandeep Kunta39b77672014-09-01 17:22:57 +05301570 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1571 if (ph != null && ph.getId().equals(lchSub)) continue;
1572 // Active calls have priority.
1573 if (call.isActive()) {
1574 newForegroundCall = call;
1575 break;
1576 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001577
Sandeep Kunta39b77672014-09-01 17:22:57 +05301578 if (call.isAlive() || call.getState() == CallState.RINGING) {
1579 newForegroundCall = call;
1580 // Don't break in case there's an active call that has priority.
1581 }
1582 }
1583 // if active sub doesn't have any calls, then consider calls on all subs,
1584 // which ever call is active set that as foreground call. give more priority
1585 // to ringing call on LCH sub over active call.
1586 if (newForegroundCall == null) {
1587 newForegroundCall = getFirstCallWithState(CallState.RINGING);
1588 if (newForegroundCall == null) {
1589 newForegroundCall = getFirstCallWithState(CallState.ACTIVE);
1590 }
1591 }
1592 } else {
1593 for (Call call : mCalls) {
1594 // Only top-level calls can be in foreground
1595 if (call.getParentCall() != null) {
1596 continue;
1597 }
1598
1599 // Active calls have priority.
1600 if (call.isActive()) {
1601 newForegroundCall = call;
1602 break;
1603 }
1604
1605 if (call.isAlive() || call.getState() == CallState.RINGING) {
1606 newForegroundCall = call;
1607 // Don't break in case there's an active call that has priority.
1608 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001609 }
1610 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001611
Sailesh Nepal810735e2014-03-18 18:15:46 -07001612 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001613 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001614 Call oldForegroundCall = mForegroundCall;
1615 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001616
Santos Cordona56f2762014-03-24 15:55:53 -07001617 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001618 if (Log.SYSTRACE_DEBUG) {
1619 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1620 }
Santos Cordona56f2762014-03-24 15:55:53 -07001621 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001622 if (Log.SYSTRACE_DEBUG) {
1623 Trace.endSection();
1624 }
Santos Cordona56f2762014-03-24 15:55:53 -07001625 }
Santos Cordon681663d2014-01-30 04:32:15 -08001626 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001627 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001628 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001629
Santos Cordon91bd74c2014-11-07 16:10:22 -08001630 private void updateCanAddCall() {
1631 boolean newCanAddCall = canAddCall();
1632 if (newCanAddCall != mCanAddCall) {
1633 mCanAddCall = newCanAddCall;
1634 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001635 if (Log.SYSTRACE_DEBUG) {
1636 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1637 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001638 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001639 if (Log.SYSTRACE_DEBUG) {
1640 Trace.endSection();
1641 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001642 }
1643 }
1644 }
1645
1646 private void updateCallsManagerState() {
1647 updateForegroundCall();
1648 updateCanAddCall();
1649 }
1650
Yorke Leeb701f6d2014-08-12 14:04:19 -07001651 private boolean isPotentialMMICode(Uri handle) {
1652 return (handle != null && handle.getSchemeSpecificPart() != null
1653 && handle.getSchemeSpecificPart().contains("#"));
1654 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001655
Tyler Gunn5b452df2014-10-01 15:02:58 -07001656 /**
1657 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1658 * MMI codes which can be dialed when one or more calls are in progress.
1659 * <P>
1660 * Checks for numbers formatted similar to the MMI codes defined in:
1661 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1662 * and
1663 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1664 *
1665 * @param handle The URI to call.
1666 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1667 */
1668 private boolean isPotentialInCallMMICode(Uri handle) {
1669 if (handle != null && handle.getSchemeSpecificPart() != null &&
1670 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1671
1672 String dialedNumber = handle.getSchemeSpecificPart();
1673 return (dialedNumber.equals("0") ||
1674 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1675 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1676 dialedNumber.equals("3") ||
1677 dialedNumber.equals("4") ||
1678 dialedNumber.equals("5"));
1679 }
1680 return false;
1681 }
1682
Santos Cordonf193ba42014-09-12 06:37:39 -07001683 private int getNumCallsWithState(int... states) {
1684 int count = 0;
1685 for (int state : states) {
1686 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001687 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001688 count++;
1689 }
1690 }
1691 }
1692 return count;
1693 }
1694
Sandeep Kunta39b77672014-09-01 17:22:57 +05301695 private int getNumCallsWithState(String subId, int... states) {
1696 int count = 0;
1697 for (int state : states) {
1698 for (Call call : mCalls) {
1699 if (call.getParentCall() == null && call.getState() == state
1700 && call.getTargetPhoneAccount() != null
1701 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1702 count++;
1703 }
1704 }
1705 }
1706 return count;
1707 }
1708
Santos Cordonf193ba42014-09-12 06:37:39 -07001709 private boolean hasMaximumLiveCalls() {
1710 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1711 }
1712
Sandeep Kunta39b77672014-09-01 17:22:57 +05301713 private boolean hasMaximumLiveCalls(String subId) {
1714 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(subId, LIVE_CALL_STATES);
1715 }
1716
Santos Cordonf193ba42014-09-12 06:37:39 -07001717 private boolean hasMaximumHoldingCalls() {
1718 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1719 }
1720
Sandeep Kunta39b77672014-09-01 17:22:57 +05301721 private boolean hasMaximumHoldingCalls(String subId) {
1722 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(subId, CallState.ON_HOLD);
1723 }
1724
Santos Cordonf193ba42014-09-12 06:37:39 -07001725 private boolean hasMaximumRingingCalls() {
1726 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1727 }
1728
Sandeep Kunta39b77672014-09-01 17:22:57 +05301729 private boolean hasMaximumRingingCalls(String subId) {
1730 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(subId, CallState.RINGING);
1731 }
1732
Santos Cordonf193ba42014-09-12 06:37:39 -07001733 private boolean hasMaximumOutgoingCalls() {
1734 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1735 }
1736
1737 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301738 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1739 == TelephonyManager.MultiSimVariants.DSDA) {
1740 return makeRoomForOutgoingCallForDsda(call, isEmergency);
1741 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001742 if (hasMaximumLiveCalls()) {
1743 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1744 // have to change.
1745 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301746 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1747 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001748
Santos Cordond1766502014-09-26 15:45:39 -07001749 if (call == liveCall) {
1750 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001751 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001752 // state since the call was already populated into the list.
1753 return true;
1754 }
1755
Andrew Leee6595182014-09-23 18:43:05 -07001756 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001757 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1758 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1759 // Disconnect the current outgoing call if it's not an emergency call. If the
1760 // user tries to make two outgoing calls to different emergency call numbers,
1761 // we will try to connect the first outgoing call.
1762 outgoingCall.disconnect();
1763 return true;
1764 }
1765 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1766 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1767 // state, just disconnect it since the user has explicitly started a new call.
1768 outgoingCall.disconnect();
1769 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001770 }
1771 return false;
1772 }
1773
Santos Cordonf193ba42014-09-12 06:37:39 -07001774 if (hasMaximumHoldingCalls()) {
1775 // There is no more room for any more calls, unless it's an emergency.
1776 if (isEmergency) {
1777 // Kill the current active call, this is easier then trying to disconnect a
1778 // holding call and hold an active call.
1779 liveCall.disconnect();
1780 return true;
1781 }
1782 return false; // No more room!
1783 }
1784
1785 // We have room for at least one more holding call at this point.
1786
Tyler Gunne4cd9162015-08-11 15:36:04 -07001787 // TODO: Remove once b/23035408 has been corrected.
1788 // If the live call is a conference, it will not have a target phone account set. This
1789 // means the check to see if the live call has the same target phone account as the new
1790 // call will not cause us to bail early. As a result, we'll end up holding the
1791 // ongoing conference call. However, the ConnectionService is already doing that. This
1792 // has caused problems with some carriers. As a workaround until b/23035408 is
1793 // corrected, we will try and get the target phone account for one of the conference's
1794 // children and use that instead.
1795 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
1796 if (liveCallPhoneAccount == null && liveCall.isConference() &&
1797 !liveCall.getChildCalls().isEmpty()) {
1798 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
1799 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
1800 liveCallPhoneAccount);
1801 }
1802
Santos Cordonf193ba42014-09-12 06:37:39 -07001803 // First thing, if we are trying to make a call with the same phone account as the live
1804 // call, then allow it so that the connection service can make its own decision about
1805 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07001806 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
1807 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001808 return true;
1809 } else if (call.getTargetPhoneAccount() == null) {
1810 // Without a phone account, we can't say reliably that the call will fail.
1811 // If the user chooses the same phone account as the live call, then it's
1812 // still possible that the call can be made (like with CDMA calls not supporting
1813 // hold but they still support adding a call by going immediately into conference
1814 // mode). Return true here and we'll run this code again after user chooses an
1815 // account.
1816 return true;
1817 }
1818
1819 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001820 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07001821 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001822 liveCall.hold();
1823 return true;
1824 }
1825
1826 // The live call cannot be held so we're out of luck here. There's no room.
1827 return false;
1828 }
1829 return true;
1830 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001831
Sandeep Kunta39b77672014-09-01 17:22:57 +05301832 private boolean makeRoomForOutgoingCallForDsda(Call call, boolean isEmergency) {
1833 if (isEmergency || (call.getState() == CallState.SELECT_PHONE_ACCOUNT)) {
1834 return true;
1835 }
1836
1837 PhoneAccountHandle phAcc = call.getTargetPhoneAccount();
1838 if (phAcc == null) {
1839 if (getNumCallsWithState(LIVE_CALL_STATES) == MAXIMUM_DSDA_LIVE_CALLS
1840 && getNumCallsWithState(CallState.ON_HOLD) == MAXIMUM_DSDA_HOLD_CALLS) {
1841 return false;
1842 } else {
1843 return true;
1844 }
1845 }
1846 if (hasMaximumLiveCalls(phAcc.getId())) {
1847 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1848 // have to change.
1849 Call liveCall = getFirstCallWithState(phAcc.getId(), call, LIVE_CALL_STATES);
1850
1851 if (hasMaximumHoldingCalls(phAcc.getId())) {
1852 // There is no more room for any more calls, unless it's an emergency.
1853 return false; // No more room!
1854 }
1855 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1856 return true;
1857 }
1858 // Try to hold the live call before attempting the new outgoing call.
1859 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
1860 liveCall.hold();
1861 return true;
1862 }
1863
1864 // The live call cannot be held so we're out of luck here. There's no room.
1865 return false;
1866 }
1867 return true;
1868 }
1869
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001870 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07001871 * Given a call, find the first non-null phone account handle of its children.
1872 *
1873 * @param parentCall The parent call.
1874 * @return The first non-null phone account handle of the children, or {@code null} if none.
1875 */
1876 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
1877 for (Call childCall : parentCall.getChildCalls()) {
1878 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
1879 if (childPhoneAccount != null) {
1880 return childPhoneAccount;
1881 }
1882 }
1883 return null;
1884 }
1885
1886 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001887 * Checks to see if the call should be on speakerphone and if so, set it.
1888 */
1889 private void maybeMoveToSpeakerPhone(Call call) {
1890 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001891 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001892 call.setStartWithSpeakerphoneOn(false);
1893 }
1894 }
1895
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001896 private void maybeMoveToEarpiece(Call call) {
1897 if (!call.getStartWithSpeakerphoneOn() && !mWiredHeadsetManager.isPluggedIn() &&
1898 !mCallAudioManager.isBluetoothDeviceAvailable()) {
1899 setAudioRoute(CallAudioState.ROUTE_EARPIECE);
1900 }
1901 }
1902
Santos Cordond6782cd2015-04-16 09:57:50 -07001903 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001904 * Creates a new call for an existing connection.
1905 *
1906 * @param callId The id of the new call.
1907 * @param connection The connection information.
1908 * @return The new call.
1909 */
1910 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1911 Call call = new Call(
1912 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001913 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001914 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001915 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001916 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001917 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001918 connection.getHandle() /* handle */,
1919 null /* gatewayInfo */,
1920 null /* connectionManagerPhoneAccount */,
1921 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1922 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001923 false /* isConference */,
1924 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001925
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001926 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1927 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001928 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001929 call.setCallerDisplayName(connection.getCallerDisplayName(),
1930 connection.getCallerDisplayNamePresentation());
1931
1932 call.addListener(this);
1933 addCall(call);
1934
1935 return call;
1936 }
1937
1938 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001939 * Dumps the state of the {@link CallsManager}.
1940 *
1941 * @param pw The {@code IndentingPrintWriter} to write the state to.
1942 */
1943 public void dump(IndentingPrintWriter pw) {
1944 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001945 if (mCalls != null) {
1946 pw.println("mCalls: ");
1947 pw.increaseIndent();
1948 for (Call call : mCalls) {
1949 pw.println(call);
1950 }
1951 pw.decreaseIndent();
1952 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001953 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1954
1955 if (mCallAudioManager != null) {
1956 pw.println("mCallAudioManager:");
1957 pw.increaseIndent();
1958 mCallAudioManager.dump(pw);
1959 pw.decreaseIndent();
1960 }
1961
1962 if (mTtyManager != null) {
1963 pw.println("mTtyManager:");
1964 pw.increaseIndent();
1965 mTtyManager.dump(pw);
1966 pw.decreaseIndent();
1967 }
1968
1969 if (mInCallController != null) {
1970 pw.println("mInCallController:");
1971 pw.increaseIndent();
1972 mInCallController.dump(pw);
1973 pw.decreaseIndent();
1974 }
1975
1976 if (mConnectionServiceRepository != null) {
1977 pw.println("mConnectionServiceRepository:");
1978 pw.increaseIndent();
1979 mConnectionServiceRepository.dump(pw);
1980 pw.decreaseIndent();
1981 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001982 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301983
1984 private final Handler mLchHandler = new LchHandler();
1985 private final class LchHandler extends Handler {
1986 @Override
1987 public void handleMessage(Message msg) {
1988 switch (msg.what) {
1989 case PHONE_START_DSDA_INCALL_TONE:
1990 Log.d(this, "Start DSDA incall tones...");
1991 startDsdaInCallTones();
1992 break;
1993 case LCH_PLAY_DTMF:
1994 playLchDtmf();
1995 break;
1996 case LCH_STOP_DTMF:
1997 stopLchDtmf();
1998 break;
1999 }
2000 }
2001 }
2002
2003 public void switchToOtherActiveSub(String subId) {
2004 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2005 != TelephonyManager.MultiSimVariants.DSDA) {
2006 return;
2007 }
2008 Log.d(this, "switchToOtherActiveSub sub:" + subId);
2009 setActiveSubscription(subId);
2010 updateLchStatus(subId);
2011 manageDsdaInCallTones(true);
2012 updateForegroundCall();
2013 }
2014
2015 public synchronized void setActiveSubscription(String subId) {
2016 Log.d(this, "setActiveSubscription = " + subId);
2017 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2018 != TelephonyManager.MultiSimVariants.DSDA) {
2019 return;
2020 }
2021 boolean changed = false;
2022 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2023 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2024 if (subId != null && subId.equals(ph.getId())
2025 && (phAcc != null) && !phAcc.isSet(PhoneAccount.ACTIVE)) {
2026 changed = true;
2027 phAcc.setBit(PhoneAccount.ACTIVE);
2028 } else if (subId != null && !subId.equals(ph.getId())
2029 && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2030 changed = true;
2031 phAcc.unSetBit(PhoneAccount.ACTIVE);
2032 } else if ((subId == null) && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2033 phAcc.unSetBit(PhoneAccount.ACTIVE);
2034 }
2035 }
2036 if (!changed) {
2037 Log.d(this, "setActiveSubscription not changed ");
2038 return;
2039 } else {
2040 Log.d(this, "setActiveSubscription changed " );
2041 for (Call call : mCalls) {
2042 PhoneAccountHandle ph = call.getTargetPhoneAccount();
2043 if (ph != null) {
2044 call.mIsActiveSub = ph.getId().equals(subId) ? true : false;
2045 }
2046 for (CallsManagerListener listener : mListeners) {
2047 listener.onCallStateChanged(call, call.getState(), call.getState());
2048 }
2049 }
2050 }
2051 }
2052
2053 public String getActiveSubscription() {
2054 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2055 if (getPhoneAccountRegistrar()
2056 .getPhoneAccount(ph).isSet(PhoneAccount.ACTIVE)) {
2057 Log.d(this, "getActiveSubscription: " + ph.getId());
2058 return ph.getId();
2059 }
2060 }
2061 return null;
2062 }
2063
2064 private String getConversationSub() {
2065 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2066 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2067 if (phAcc != null && !phAcc.isSet(PhoneAccount.LCH) &&
2068 (getFirstCallWithState(ph.getId(), CallState.ACTIVE, CallState.DIALING,
2069 CallState.ON_HOLD) != null)) {
2070 Log.d(this, "getConversationSub: " + ph.getId());
2071 return ph.getId();
2072 }
2073 }
2074 return null;
2075 }
2076
2077 void manageDsdaInCallTones(boolean isSubSwitch) {
2078 Log.d(this, " entered manageDsdaInCallTones ");
2079
2080 // If there is no background active subscription available, stop playing the tones.
2081 // Do not start/stop LCH/SCH tones when phone is in RINGING state.
2082 if (getLchSub() != null && !hasRingingCall()) {
2083 //If sub switch happens re-start the tones with a delay of 100msec.
2084 if (isSubSwitch) {
2085 Log.d(this, " manageDsdaInCallTones: re-start playing tones, lch sub = "
2086 + getLchSub());
2087 reStartDsdaInCallTones();
2088 } else {
2089 Log.d(this, " entered manageDsdaInCallTones ");
2090 startDsdaInCallTones();
2091 }
2092 } else if (getLchSub() == null) {
2093 // if there is no sub in Lch state, then stop playing the tones
2094 stopMSimInCallTones();
2095 }
2096 }
2097
2098 private void reStartDsdaInCallTones() {
2099 Log.d(this, " reStartDsdaInCallTones");
2100 stopMSimInCallTones();
2101 /* Remove any pending PHONE_START_DSDA_INCALL_TONE messages from queue */
2102 mLchHandler.removeMessages(PHONE_START_DSDA_INCALL_TONE);
2103 Message message = Message.obtain(mLchHandler, PHONE_START_DSDA_INCALL_TONE);
2104 mLchHandler.sendMessageDelayed(message, 100);
2105 }
2106
2107 /**
2108 * Returns the first call that it finds with the given states for given subscription.
2109 * The states are treated as having priority order so that any call with the first
2110 * state will be returned before any call with states listed later in the parameter list.
2111 */
2112 Call getFirstCallWithState(String sub, int... states) {
2113 for (int currentState : states) {
2114 // check the foreground first
2115 if (mForegroundCall != null && mForegroundCall.getState() == currentState
2116 && (mForegroundCall.getTargetPhoneAccount() != null)
2117 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(),
2118 sub)) {
2119 return mForegroundCall;
2120 }
2121
2122 for (Call call : mCalls) {
2123 if ((currentState == call.getState()) &&
2124 (call.getTargetPhoneAccount() != null) &&
2125 (isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), sub))) {
2126 return call;
2127 }
2128 }
2129 }
2130 return null;
2131 }
2132
2133 private String getLchSub() {
2134 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2135 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2136 if (phAcc != null && phAcc.isSet(PhoneAccount.LCH)) {
2137 return ph.getId();
2138 }
2139 }
2140 return null;
2141 }
2142
2143 private void playLchDtmf() {
2144 if (mLchSub != null || mLchHandler.hasMessages(LCH_PLAY_DTMF)) {
2145 // Ignore any redundant requests to start playing tones
2146 return;
2147 }
2148 mLchSub = getLchSub();
2149 Log.d(this, " playLchDtmf... lch sub " + mLchSub);
2150 if (mLchSub == null) return;
2151 removeAnyPendingDtmfMsgs();
2152 char c = mContext.getResources()
2153 .getString(R.string.lch_dtmf_key).charAt(0);
2154 Call call = getNonRingingLiveCall(mLchSub);
2155 if (call == null) {
2156 mLchSub = null;
2157 return;
2158 }
2159 call.playDtmfTone(c);
2160 // Keep playing LCH DTMF tone to remote party on LCH call, with periodicity
2161 // "LCH_DTMF_PERIODICITY" until call moves out of LCH.
2162 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_PLAY_DTMF),
2163 LCH_DTMF_PERIODICITY);
2164 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_STOP_DTMF), LCH_DTMF_PERIOD);
2165 }
2166
2167 private Call getNonRingingLiveCall(String subId) {
2168 return getFirstCallWithState(subId, CallState.DIALING,
2169 CallState.ACTIVE, CallState.ON_HOLD);
2170 }
2171
2172 private void stopLchDtmf() {
2173 if (mLchSub != null) {
2174 // Ignore any redundant requests to stop playing tones
2175 Call call = getNonRingingLiveCall(mLchSub);
2176 Log.d(this, " stopLchDtmf... call: " + call + " mLchSub:" + mLchSub);
2177 if (call == null) {
2178 mLchSub = null;
2179 return;
2180 }
2181 call.stopDtmfTone();
2182 }
2183 mLchSub = null;
2184 }
2185
2186 private void startDsdaInCallTones() {
2187 if (mLocalCallReminderTonePlayer == null) {
2188 Log.d(this, " Play local call hold reminder tone ");
2189 mLocalCallReminderTonePlayer =
2190 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_HOLD_RECALL);
2191 mLocalCallReminderTonePlayer.start();
2192 }
2193 if (sSupervisoryCallHoldToneConfig.equals("inband")) {
2194 // if "persist.radio.sch_tone" is set to "inband", play inband supervisory
2195 // call hold tone. if set to "dtmf", play the SCH tones
2196 // over DTMF, don't play SCH tones for anyother value.
2197 if (mSupervisoryCallHoldTonePlayer == null) {
2198 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone ");
2199 mSupervisoryCallHoldTonePlayer =
2200 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_SUPERVISORY_CH);
2201 mSupervisoryCallHoldTonePlayer.start();
2202 }
2203 } else if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2204 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone over dtmf ");
2205 playLchDtmf();
2206 }
2207 }
2208
2209 private void removeAnyPendingDtmfMsgs() {
2210 mLchHandler.removeMessages(LCH_PLAY_DTMF);
2211 mLchHandler.removeMessages(LCH_STOP_DTMF);
2212 }
2213
2214 protected void stopMSimInCallTones() {
2215 if (mLocalCallReminderTonePlayer != null) {
2216 Log.d(this, " stopMSimInCallTones: local call hold reminder tone ");
2217 mLocalCallReminderTonePlayer.stopTone();
2218 mLocalCallReminderTonePlayer = null;
2219 }
2220 if (mSupervisoryCallHoldTonePlayer != null) {
2221 Log.d(this, " stopMSimInCallTones: Supervisory call hold tone ");
2222 mSupervisoryCallHoldTonePlayer.stopTone();
2223 mSupervisoryCallHoldTonePlayer = null;
2224 }
2225 if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2226 Log.d(this, " stopMSimInCallTones: stop SCH Dtmf call hold tone ");
2227 stopLchDtmf();
2228 /* Remove any previous dtmf nssages from queue */
2229 removeAnyPendingDtmfMsgs();
2230 }
2231 }
2232
2233 /**
2234 * Update the local call hold state for all subscriptions
2235 * 1 -- if call on local hold, 0 -- if call is not on local hold
2236 *
2237 * @param subInConversation is the sub user is currently active in subsription.
2238 * so if this sub is in LCH, then bring that sub out of LCH.
2239 */
2240 private void updateLchStatus(String subInConversation) {
2241 Call removeFromLch = null;
2242 Log.d(this, "updateLchStatus subInConversation: " + subInConversation);
2243 if (subInConversation != null && subInConversation.contains("sip")) {
2244 return;
2245 }
2246 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2247 String sub = ph.getId();
2248 if (sub != null && sub.contains("sip")) {
2249 Log.d(this, "update lch. Skipping account: " + sub);
2250 continue;
2251 }
2252 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2253 boolean lchState = false;
2254 if (subInConversation != null && hasActiveOrHoldingCall(sub) &&
2255 !sub.equals(subInConversation)) {
2256 // if sub is not conversation sub and if it has an active
2257 // voice call then update lchStatus as Active
2258 lchState = true;
2259 }
2260 // Update state only if the new state is different
2261 if ((phAcc != null) && (lchState != phAcc.isSet(PhoneAccount.LCH))) {
2262 Call call = getNonRingingLiveCall(sub);
2263 Log.d(this, " setLocal Call Hold to = " + lchState + " sub:" + sub);
2264
2265 if (call != null) {
2266 if (call.getChildCalls().size() > 1) {
2267 // Since for a conference call corresponding telephonyconnection doesn't
2268 // exist, so send lch request on its child call
2269 call = call.getChildCalls().get(0);
2270 }
2271 if (lchState) {
2272 call.setLocalCallHold(true);
2273 } else {
2274 removeFromLch = call;
2275 }
2276 }
2277 if (lchState) {
2278 phAcc.setBit(PhoneAccount.LCH);
2279 } else {
2280 phAcc.unSetBit(PhoneAccount.LCH);
2281 }
2282 }
2283 }
2284 if (removeFromLch != null) {
2285 // Ensure to send LCH disable request at last, to make sure that during switch
2286 // subscription, both subscriptions not to be in active(non-LCH) at any moment.
2287 removeFromLch.setLocalCallHold(false);
2288 }
2289 }
cqiang72e31202015-09-14 17:04:19 +08002290
2291 void resetCdmaConnectionTime(Call call) {
2292 call.setConnectTimeMillis(System.currentTimeMillis());
2293 if (mCalls.contains(call)) {
2294 for (CallsManagerListener listener : mListeners) {
2295 listener.onCallStateChanged(call, CallState.ACTIVE, CallState.ACTIVE);
2296 }
2297 updateForegroundCall();
2298 }
2299 }
Ravindra205cb782015-10-07 16:57:53 +05302300
2301 void onMergeFailed(Call call) {
2302 if (mCalls.contains(call)) {
2303 for (CallsManagerListener listener : mListeners) {
2304 listener.onMergeFailed(call);
2305 }
2306 }
2307 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08002308}