blob: 34d04e36429aa49cad8d37b192a226db463eb682 [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;
Hall Liuea61e7a2016-01-27 11:07:07 -080028import android.provider.Settings;
Yorke Lee2a66f7b2015-05-13 14:21:19 -070029import android.telecom.CallAudioState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080030import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080031import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070032import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070033import android.telecom.GatewayInfo;
34import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070035import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070036import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070037import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070038import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070039import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080040import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070041import android.telephony.TelephonyManager;
Yorke Lee8d3f2692015-05-08 11:44:39 -070042import android.text.TextUtils;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080043
Ihab Awada3653902015-01-23 13:44:46 -080044import com.android.internal.annotations.VisibleForTesting;
Rekha Kumard240fe82015-04-01 21:45:57 -070045import com.android.internal.telephony.PhoneConstants;
46import com.android.internal.telephony.TelephonyProperties;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070047import com.android.internal.util.IndentingPrintWriter;
Anju Mathapatic35eb462015-10-10 02:28:16 -070048import com.android.server.telecom.ui.ViceNotificationImpl;
Yorke Leee4a9c412014-11-14 16:59:42 -080049
Santos Cordon4bc02452014-11-19 15:51:12 -080050import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070051import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070052import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070053import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070054import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070055import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070056import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080057
Ben Giladdd8c6082013-12-30 14:44:08 -080058/**
59 * Singleton.
60 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070061 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080062 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070063 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080064 */
Ihab Awadaa383cc2015-03-22 22:12:28 -070065@VisibleForTesting
Tyler Gunn86014fc2015-06-12 14:31:25 -070066public class CallsManager extends Call.ListenerBase implements VideoProviderProxy.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080067
Santos Cordondf399862014-08-06 04:39:15 -070068 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070069 interface CallsManagerListener {
70 void onCallAdded(Call call);
71 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070072 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070073 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070074 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070075 ConnectionServiceWrapper oldService,
76 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070077 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070078 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070079 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Yorke Lee2a66f7b2015-05-13 14:21:19 -070080 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070081 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070082 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070083 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070084 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080085 void onCanAddCallChanged(boolean canAddCall);
Tyler Gunn86014fc2015-06-12 14:31:25 -070086 void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile);
Ravindra205cb782015-10-07 16:57:53 +053087 void onMergeFailed(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070088 }
89
Tyler Gunn91d43cf2014-09-17 12:19:39 -070090 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080091
Santos Cordonf193ba42014-09-12 06:37:39 -070092 private static final int MAXIMUM_LIVE_CALLS = 1;
93 private static final int MAXIMUM_HOLD_CALLS = 1;
94 private static final int MAXIMUM_RINGING_CALLS = 1;
Roshan Pius4de4a892015-08-24 11:06:58 -070095 private static final int MAXIMUM_DIALING_CALLS = 1;
Santos Cordonf193ba42014-09-12 06:37:39 -070096 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080097 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Sandeep Kunta39b77672014-09-01 17:22:57 +053098 private static final int MAXIMUM_DSDA_LIVE_CALLS = 2;
99 private static final int MAXIMUM_DSDA_HOLD_CALLS = 2;
100 private static final int MAXIMUM_DSDA_TOP_LEVEL_CALLS = 4;
Santos Cordonf193ba42014-09-12 06:37:39 -0700101
102 private static final int[] OUTGOING_CALL_STATES =
Santos Cordon92694512015-04-23 14:47:28 -0700103 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING};
Santos Cordonf193ba42014-09-12 06:37:39 -0700104
Santos Cordon91bd74c2014-11-07 16:10:22 -0800105 private static final int[] LIVE_CALL_STATES =
Sandeep Kunta39b77672014-09-01 17:22:57 +0530106 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING,
107 CallState.ACTIVE};
Santos Cordon91bd74c2014-11-07 16:10:22 -0800108
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800109 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700110 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
111 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800112 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700113 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
114 * load factor before resizing, 1 means we only expect a single thread to
115 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800116 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700117 private final Set<Call> mCalls = Collections.newSetFromMap(
118 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800119
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700120 private final ConnectionServiceRepository mConnectionServiceRepository;
121 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
122 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700123 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800124 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700125 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800126 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700127 // For this set initial table size to 16 because we add 13 listeners in
128 // the CallsManager constructor.
129 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
130 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700131 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700132 private final WiredHeadsetManager mWiredHeadsetManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800133 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700134 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700135 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700136 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700137 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700138 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700139 private final TelecomSystem.SyncRoot mLock;
140 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700141 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700142 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
143 private final MissedCallNotifier mMissedCallNotifier;
Anju Mathapatic35eb462015-10-10 02:28:16 -0700144 private final ViceNotificationImpl mViceNotificationImpl;
Ihab Awad5b281322014-09-25 18:25:29 -0700145 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800146 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
147 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700148 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700149
Santos Cordon91bd74c2014-11-07 16:10:22 -0800150 private boolean mCanAddCall = true;
151
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700152 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700153 * The call the user is currently interacting with. This is the call that should have audio
154 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800155 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700156 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800157
Sandeep Kunta39b77672014-09-01 17:22:57 +0530158 private static final int LCH_PLAY_DTMF = 100;
159 private static final int LCH_STOP_DTMF = 101;
160 private static final int PHONE_START_DSDA_INCALL_TONE = 102;
161 private static final int LCH_DTMF_PERIODICITY = 3000;
162 private static final int LCH_DTMF_PERIOD = 500;
163 private static final String sSupervisoryCallHoldToneConfig =
164 SystemProperties.get("persist.radio.sch_tone", "none");
165
166 private static InCallTonePlayer.Factory mPlayerFactory;
167 private String mLchSub = null;
168
169 private InCallTonePlayer mLocalCallReminderTonePlayer = null;
170 private InCallTonePlayer mSupervisoryCallHoldTonePlayer = null;
171 private String mSubInConversation = null;
172
Nancy Chena469f762014-12-09 18:29:20 -0800173 private Runnable mStopTone;
174
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700175 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700176 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800177 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700178 CallsManager(
179 Context context,
180 TelecomSystem.SyncRoot lock,
181 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700182 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700183 MissedCallNotifier missedCallNotifier,
184 PhoneAccountRegistrar phoneAccountRegistrar,
185 HeadsetMediaButtonFactory headsetMediaButtonFactory,
186 ProximitySensorManagerFactory proximitySensorManagerFactory,
Anju Mathapatic35eb462015-10-10 02:28:16 -0700187 InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
188 ViceNotifier viceNotifier) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700189 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700190 mLock = lock;
191 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700192 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700193 mPhoneAccountRegistrar = phoneAccountRegistrar;
194 mMissedCallNotifier = missedCallNotifier;
195 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
196 mWiredHeadsetManager = new WiredHeadsetManager(context);
Santos Cordona04bdca2015-03-04 16:57:55 -0800197 mDockManager = new DockManager(context);
198 mCallAudioManager = new CallAudioManager(
Ihab Awadb60f0062015-05-26 16:20:32 -0700199 context, mLock, statusBarNotifier, mWiredHeadsetManager, mDockManager, this);
Vinit Deshpande73ff3722015-04-14 17:58:09 -0700200 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager, lock);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530201 mPlayerFactory = playerFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700202 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad731369c2015-05-19 09:23:21 -0700203 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700204 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800205 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
206 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700207 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700208 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700209 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800210 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700211 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800212 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Anju Mathapatic35eb462015-10-10 02:28:16 -0700213 mViceNotificationImpl = viceNotifier.create(mContext, this);
Santos Cordonae193062014-05-21 21:21:49 -0700214
Santos Cordondeb8c892014-05-30 01:38:03 -0700215 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700216 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700217 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700218 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700219 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700220 mListeners.add(new RingbackPlayer(this, playerFactory));
221 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700222 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700223 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700224 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700225 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700226 mListeners.add(mProximitySensorManager);
Anju Mathapatic35eb462015-10-10 02:28:16 -0700227 mListeners.add(mViceNotificationImpl);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700228
Ihab Awadabcbce42015-04-07 14:04:01 -0700229 mMissedCallNotifier.updateOnStartup(
230 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800231 }
232
Anju Mathapatic35eb462015-10-10 02:28:16 -0700233 ViceNotificationImpl getViceNotificationImpl() {
234 return mViceNotificationImpl;
235 }
236
Ihab Awad8de76912015-02-17 12:25:52 -0800237 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
238 if (mRespondViaSmsManager != null) {
239 mListeners.remove(mRespondViaSmsManager);
240 }
241 mRespondViaSmsManager = respondViaSmsManager;
242 mListeners.add(respondViaSmsManager);
243 }
244
245 public RespondViaSmsManager getRespondViaSmsManager() {
246 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800247 }
248
Santos Cordon766d04f2014-05-06 10:28:25 -0700249 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700250 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700251 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700252
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700253 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700254 if (!mCalls.contains(call)) {
255 // Call was not added previously in startOutgoingCall due to it being a potential MMI
256 // code, so add it now.
257 addCall(call);
258 }
259
260 // The call's ConnectionService has been updated.
261 for (CallsManagerListener listener : mListeners) {
262 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700263 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700264
265 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700266 }
267
268 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700269 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700270 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700271
Andrew Leee6288a72014-10-01 13:50:02 -0700272 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700273 }
274
275 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700276 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700277 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700278 setCallState(incomingCall, CallState.RINGING, "successful incoming call");
Santos Cordonf193ba42014-09-12 06:37:39 -0700279
David Ngca0c6782015-11-30 17:00:46 -0800280 if (hasMaximumRingingCalls(incomingCall.getTargetPhoneAccount().getId()) || hasMaximumDialingCalls() ) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700281 incomingCall.reject(false, null);
282 // since the call was not added to the list of calls, we have to call the missed
283 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700284 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700285 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
286 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530287 if (TelephonyManager.getDefault().getMultiSimConfiguration()
288 == TelephonyManager.MultiSimVariants.DSDA) {
289 incomingCall.mIsActiveSub = true;
290 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700291 addCall(incomingCall);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530292 setActiveSubscription(incomingCall.getTargetPhoneAccount().getId());
Santos Cordonf193ba42014-09-12 06:37:39 -0700293 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700294 }
295
296 @Override
297 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700298 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700299 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800300 }
301
Ihab Awadcb387ac2014-05-28 16:49:38 -0700302 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700303 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700304 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700305 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
306 addCall(call);
307 }
308
309 @Override
310 public void onFailedUnknownCall(Call call) {
311 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700312 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700313 call.removeListener(this);
314 }
315
316 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700317 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700318 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700319 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700320 }
321 }
322
Evan Charlton352105c2014-06-03 14:10:54 -0700323 @Override
324 public void onPostDialWait(Call call, String remaining) {
325 mInCallController.onPostDialWait(call, remaining);
326 }
327
Santos Cordona1610702014-06-04 20:22:56 -0700328 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800329 public void onPostDialChar(final Call call, char nextChar) {
330 if (PhoneNumberUtils.is12Key(nextChar)) {
331 // Play tone if it is one of the dialpad digits, canceling out the previously queued
332 // up stopTone runnable since playing a new tone automatically stops the previous tone.
333 if (mStopTone != null) {
334 mHandler.removeCallbacks(mStopTone);
335 }
336
337 mDtmfLocalTonePlayer.playTone(call, nextChar);
338
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700339 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800340 mStopTone = new Runnable() {
341 @Override
342 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700343 synchronized (mLock) {
344 // Set a timeout to stop the tone in case there isn't another tone to follow.
345 mDtmfLocalTonePlayer.stopTone(call);
346 }
Nancy Chena469f762014-12-09 18:29:20 -0800347 }
348 };
349 mHandler.postDelayed(
350 mStopTone,
351 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
352 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
353 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
354 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
355 // the previous tone is being stopped anyway.
356 if (mStopTone != null) {
357 mHandler.removeCallbacks(mStopTone);
358 }
359 mDtmfLocalTonePlayer.stopTone(call);
360 } else {
361 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
362 }
363 }
364
365 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700366 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700367 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800368 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700369 for (CallsManagerListener listener : mListeners) {
370 listener.onIsConferencedChanged(call);
371 }
372 }
373
374 @Override
375 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700376 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800377 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700378 for (CallsManagerListener listener : mListeners) {
379 listener.onIsConferencedChanged(call);
380 }
381 }
382
Ihab Awadff7493a2014-06-10 13:47:44 -0700383 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700384 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700385 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700386 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700387 }
388 }
389
Andrew Lee4a796602014-07-11 17:23:03 -0700390 @Override
391 public void onVideoStateChanged(Call call) {
392 for (CallsManagerListener listener : mListeners) {
393 listener.onVideoStateChanged(call);
394 }
395 }
396
Santos Cordoncf5b2912014-11-05 21:57:54 -0800397 @Override
398 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
399 mPendingCallsToDisconnect.add(call);
400 mHandler.postDelayed(new Runnable() {
401 @Override
402 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700403 synchronized (mLock) {
404 if (mPendingCallsToDisconnect.remove(call)) {
405 Log.i(this, "Delayed disconnection of call: %s", call);
406 call.disconnect();
407 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800408 }
409 }
410 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
411
412 return true;
413 }
414
Tyler Gunn86014fc2015-06-12 14:31:25 -0700415 /**
416 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
417 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
418 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
419 * respond to callbacks from the {@link VideoProviderProxy}.
420 *
421 * @param call The call.
422 */
423 @Override
424 public void onVideoCallProviderChanged(Call call) {
425 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
426
427 if (videoProviderProxy == null) {
428 return;
429 }
430
431 videoProviderProxy.addListener(this);
432 }
433
434 /**
435 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
436 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
437 * modification request.
438 *
439 * @param call The call.
440 * @param videoProfile The {@link VideoProfile}.
441 */
442 @Override
443 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
444 int videoState = videoProfile != null ? videoProfile.getVideoState() :
445 VideoProfile.STATE_AUDIO_ONLY;
446 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
447 .videoStateToString(videoState));
448
449 for (CallsManagerListener listener : mListeners) {
450 listener.onSessionModifyRequestReceived(call, videoProfile);
451 }
452 }
453
Santos Cordon4bc02452014-11-19 15:51:12 -0800454 Collection<Call> getCalls() {
455 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700456 }
457
458 Call getForegroundCall() {
459 return mForegroundCall;
460 }
461
Santos Cordonae193062014-05-21 21:21:49 -0700462 Ringer getRinger() {
463 return mRinger;
464 }
465
Santos Cordonf3671a62014-05-29 21:51:53 -0700466 InCallController getInCallController() {
467 return mInCallController;
468 }
469
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700470 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700471 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700472 if (call.isEmergencyCall()) {
473 return true;
474 }
475 }
476 return false;
477 }
478
Roshan Pius669f90b2015-09-02 16:21:39 -0700479 boolean hasOnlyDisconnectedCalls() {
480 for (Call call : mCalls) {
481 if (!call.isDisconnected()) {
482 return false;
483 }
484 }
485 return true;
486 }
487
Andrew Lee45506232014-10-22 17:54:33 -0700488 boolean hasVideoCall() {
489 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700490 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700491 return true;
492 }
493 }
494 return false;
495 }
496
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700497 CallAudioState getAudioState() {
498 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700499 }
500
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700501 boolean isTtySupported() {
502 return mTtyManager.isTtySupported();
503 }
504
505 int getCurrentTtyMode() {
506 return mTtyManager.getCurrentTtyMode();
507 }
508
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700509 void addListener(CallsManagerListener listener) {
510 mListeners.add(listener);
511 }
512
513 void removeListener(CallsManagerListener listener) {
514 mListeners.remove(listener);
515 }
516
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800517 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700518 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800519 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700520 * @param phoneAccountHandle The phone account which contains the component name of the
521 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800522 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800523 */
Evan Charlton89176372014-07-19 18:23:09 -0700524 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800525 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700526 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
527 if (handle == null) {
528 // Required for backwards compatibility
529 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
530 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700531 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700532 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800533 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700534 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700535 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700536 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700537 mCallerInfoAsyncQueryFactory,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700538 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700539 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700540 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700541 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700542 true /* isIncoming */,
543 false /* isConference */);
544
Andrew Lee1b906652015-06-01 11:44:55 -0700545 call.setIntentExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700546 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700547 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700548 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800549 }
550
Yorke Lee9250e5f2014-10-01 13:39:09 -0700551 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
552 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
553 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
554 Call call = new Call(
555 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800556 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700557 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700558 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700559 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700560 mCallerInfoAsyncQueryFactory,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700561 handle,
562 null /* gatewayInfo */,
563 null /* connectionManagerPhoneAccount */,
564 phoneAccountHandle,
565 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
566 // to the existing connection instead of trying to create a new one.
567 true /* isIncoming */,
568 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700569 call.setIsUnknown(true);
Andrew Lee1b906652015-06-01 11:44:55 -0700570 call.setIntentExtras(extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700571 call.addListener(this);
572 call.startCreateConnection(mPhoneAccountRegistrar);
573 }
574
Yorke Lee8d3f2692015-05-08 11:44:39 -0700575 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
576 if (handle1 == null || handle2 == null) {
577 return handle1 == handle2;
578 }
579
580 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
581 return false;
582 }
583
584 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
585 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
586 return TextUtils.equals(number1, number2);
587 }
588
Santos Cordoncf5b2912014-11-05 21:57:54 -0800589 private Call getNewOutgoingCall(Uri handle) {
590 // First check to see if we can reuse any of the calls that are waiting to disconnect.
591 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800592 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800593 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700594 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800595 mPendingCallsToDisconnect.remove(pendingCall);
596 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800597 reusedCall = pendingCall;
598 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700599 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800600 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800601 }
602 }
Yorke Lee59979512014-12-02 01:03:40 -0800603 if (reusedCall != null) {
604 return reusedCall;
605 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800606
607 // Create a call with original handle. The handle may be changed when the call is attached
608 // to a connection service, but in most cases will remain the same.
609 return new Call(
610 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800611 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700612 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800613 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700614 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700615 mCallerInfoAsyncQueryFactory,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800616 handle,
617 null /* gatewayInfo */,
618 null /* connectionManagerPhoneAccount */,
619 null /* phoneAccountHandle */,
620 false /* isIncoming */,
621 false /* isConference */);
622 }
623
Nancy Chen0d3076c2014-07-30 14:45:44 -0700624 /**
625 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
626 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700627 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700628 * @param phoneAccountHandle The phone account which contains the component name of the
629 * connection service to use for this call.
630 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700631 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700632 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800633 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700634
Garik Badalyan7e1524a2015-08-25 11:08:18 -0700635 if (extras!=null) {
636 call.setVideoState(extras.getInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
637 VideoProfile.STATE_AUDIO_ONLY));
638 }
639
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530640 boolean isAddParticipant = ((extras != null) && (extras.getBoolean(
641 TelephonyProperties.ADD_PARTICIPANT_KEY, false)));
642 boolean isSkipSchemaOrConfUri = ((extras != null) && (extras.getBoolean(
643 TelephonyProperties.EXTRA_SKIP_SCHEMA_PARSING, false) ||
644 extras.getBoolean(TelephonyProperties.EXTRA_DIAL_CONFERENCE_URI, false)));
645
646 if (isAddParticipant) {
647 String number = handle.getSchemeSpecificPart();
648 if (!isSkipSchemaOrConfUri) {
649 number = PhoneNumberUtils.stripSeparators(number);
650 }
651 addParticipant(number);
652 mInCallController.bringToForeground(false);
653 return null;
654 }
655
656 // Force tel scheme for ims conf uri/skip schema calls to avoid selection of sip accounts
657 String scheme = (isSkipSchemaOrConfUri? PhoneAccount.SCHEME_TEL: handle.getScheme());
658
659 Log.d(this, "startOutgoingCall :: isAddParticipant=" + isAddParticipant
660 + " isSkipSchemaOrConfUri=" + isSkipSchemaOrConfUri + " scheme=" + scheme);
661
Nancy Chen1c5926f2014-09-17 14:44:14 -0700662 List<PhoneAccountHandle> accounts =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530663 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(scheme, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700664
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800665 Log.v(this, "startOutgoingCall found accounts = " + accounts);
666
Sandeep Kunta39b77672014-09-01 17:22:57 +0530667 if (mForegroundCall != null && TelephonyManager.getDefault().getMultiSimConfiguration()
668 != TelephonyManager.MultiSimVariants.DSDA) {
Roshan Pius1a373832015-08-07 11:39:05 -0700669 Call ongoingCall = mForegroundCall;
Nancy Chenbc95db92014-11-24 13:19:18 -0800670 // If there is an ongoing call, use the same phone account to place this new call.
Roshan Pius1a373832015-08-07 11:39:05 -0700671 // If the ongoing call is a conference call, we fetch the phone account from the
672 // child calls because we don't have targetPhoneAccount set on Conference calls.
673 // TODO: Set targetPhoneAccount for all conference calls (b/23035408).
674 if (ongoingCall.getTargetPhoneAccount() == null &&
675 !ongoingCall.getChildCalls().isEmpty()) {
676 ongoingCall = ongoingCall.getChildCalls().get(0);
677 }
678 if (ongoingCall.getTargetPhoneAccount() != null) {
679 phoneAccountHandle = ongoingCall.getTargetPhoneAccount();
680 }
Nancy Chenbc95db92014-11-24 13:19:18 -0800681 }
682
Nancy Chenbc9ef172014-08-15 17:11:57 -0700683 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
684 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700685 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700686 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700687 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700688 phoneAccountHandle = null;
689 }
690 }
691
692 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700693 // No preset account, check if default exists that supports the URI scheme for the
694 // handle.
Santos Cordon6a212642015-05-08 16:35:23 -0700695 phoneAccountHandle =
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530696 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(scheme);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700697 }
698
Nancy Chen1c5926f2014-09-17 14:44:14 -0700699 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700700
Tyler Gunn5b452df2014-10-01 15:02:58 -0700701 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700702
703 // Do not support any more live calls. Our options are to move a call to hold, disconnect
704 // a call, or cancel this call altogether.
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700705 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, call.isEmergencyCall())) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700706 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800707 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800708 if (mCalls.contains(call)) {
709 // This call can already exist if it is a reused call,
710 // See {@link #getNewOutgoingCall}.
711 call.disconnect();
712 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700713 return null;
714 }
715
Nancy Chen8d92f452014-10-20 21:43:44 -0700716 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700717 !call.isEmergencyCall();
Nancy Chen8d92f452014-10-20 21:43:44 -0700718
719 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700720 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700721 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700722 // Create our own instance to modify (since extras may be Bundle.EMPTY)
723 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700724 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700725 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700726 call.setState(
727 CallState.CONNECTING,
728 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700729 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700730
Andrew Lee1b906652015-06-01 11:44:55 -0700731 call.setIntentExtras(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700732
Tyler Gunn504eb832014-10-13 14:15:27 -0700733 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700734 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700735 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800736 } else if (!mCalls.contains(call)) {
737 // We check if mCalls already contains the call because we could potentially be reusing
738 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700739 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700740 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700741
742 return call;
743 }
744
Ben Gilada0d9f752014-02-26 11:49:03 -0800745 /**
Yorke Lee33501632014-03-17 19:24:12 -0700746 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800747 *
Yorke Lee33501632014-03-17 19:24:12 -0700748 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700749 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700750 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700751 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700752 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800753 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700754 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
755 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700756 if (call == null) {
757 // don't do anything if the call no longer exists
758 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700759 return;
760 }
761
Nancy Chen201b4372014-09-08 14:18:24 -0700762 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700763
764 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700765 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700766 } else {
767 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
768 Log.pii(uriHandle), Log.pii(handle));
769 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700770
Nancy Chen0d3076c2014-07-30 14:45:44 -0700771 call.setHandle(uriHandle);
772 call.setGatewayInfo(gatewayInfo);
Tyler Gunnd373c202015-05-20 08:21:03 -0700773 // Auto-enable speakerphone if the originating intent specified to do so, or if the call
Nivedita Sarkardae2c652015-08-31 10:55:51 -0700774 // is a video call or if the phone is docked.
775 call.setStartWithSpeakerphoneOn(speakerphoneOn || isSpeakerphoneAutoEnabled(videoState)
776 || mDockManager.isDocked());
Tyler Gunnc4abd912014-07-08 14:22:10 -0700777 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700778
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700779 if (call.isEmergencyCall()) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700780 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700781 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700782 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700783
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700784 if (call.getTargetPhoneAccount() != null || call.isEmergencyCall()) {
David Ngca0c6782015-11-30 17:00:46 -0800785 if (!call.isEmergencyCall()) {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530786 updateLchStatus(call.getTargetPhoneAccount().getId());
787 }
Nancy Chenbc9ef172014-08-15 17:11:57 -0700788 // If the account has been set, proceed to place the outgoing call.
789 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700790 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700791 }
Yorke Leef98fb572014-03-05 10:56:55 -0800792 }
793
794 /**
Suchand Ghosh97f3cb12014-09-22 12:13:47 +0530795 * Attempts to add participant in a call.
796 *
797 * @param number number to connect the call with.
798 */
799 private void addParticipant(String number) {
800 Log.i(this, "addParticipant number ="+number);
801 if (getForegroundCall() == null) {
802 // don't do anything if the call no longer exists
803 Log.i(this, "Canceling unknown call.");
804 return;
805 } else {
806 getForegroundCall().addParticipantWithConference(number);
807 }
808 }
809
810
811 /**
Santos Cordona1610702014-06-04 20:22:56 -0700812 * Attempts to start a conference call for the specified call.
813 *
Santos Cordon12d61822014-07-29 16:02:20 -0700814 * @param call The call to conference.
815 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700816 */
Santos Cordon12d61822014-07-29 16:02:20 -0700817 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700818 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700819 }
820
821 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700822 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
823 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800824 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700825 *
826 * @param call The call to answer.
827 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800828 */
Andrew Lee38931d02014-07-16 10:17:36 -0700829 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700830 if (!mCalls.contains(call)) {
831 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800832 } else {
Sandeep Kunta39b77672014-09-01 17:22:57 +0530833 Call activeCall = getFirstCallWithState(call.getTargetPhoneAccount()
834 .getId(), CallState.ACTIVE, CallState.DIALING);
Santos Cordon40f78c22014-04-07 02:11:42 -0700835 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700836 // STATE_DIALING, put it on hold before answering the call.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530837 if (activeCall != null && activeCall != call &&
838 (activeCall.isActive() ||
839 activeCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800840 if (0 == (mForegroundCall.getConnectionCapabilities()
841 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700842 // This call does not support hold. If it is from a different connection
843 // service, then disconnect it, otherwise allow the connection service to
844 // figure out the right states.
Sandeep Kunta39b77672014-09-01 17:22:57 +0530845 if (activeCall.getConnectionService() != call.getConnectionService()) {
846 activeCall.disconnect();
Santos Cordonf193ba42014-09-12 06:37:39 -0700847 }
848 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800849 Call heldCall = getHeldCall();
850 if (heldCall != null) {
851 Log.v(this, "Disconnecting held call %s before holding active call.",
852 heldCall);
853 heldCall.disconnect();
854 }
855
Santos Cordonf193ba42014-09-12 06:37:39 -0700856 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
857 mForegroundCall, call);
Sandeep Kunta39b77672014-09-01 17:22:57 +0530858 activeCall.hold();
Santos Cordonf193ba42014-09-12 06:37:39 -0700859 }
Santos Cordondf399862014-08-06 04:39:15 -0700860 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700861 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700862 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700863 }
864
Santos Cordona56f2762014-03-24 15:55:53 -0700865 for (CallsManagerListener listener : mListeners) {
866 listener.onIncomingCallAnswered(call);
867 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530868 updateLchStatus(call.getTargetPhoneAccount().getId());
Santos Cordon61d0f702014-02-19 02:52:23 -0800869 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700870 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700871 call.answer(videoState);
Tyler Gunnd373c202015-05-20 08:21:03 -0700872 if (isSpeakerphoneAutoEnabled(videoState)) {
Rekha Kumard240fe82015-04-01 21:45:57 -0700873 call.setStartWithSpeakerphoneOn(true);
874 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800875 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800876 }
877
Tyler Gunnd373c202015-05-20 08:21:03 -0700878 /**
879 * Determines if the speakerphone should be automatically enabled for the call. Speakerphone
880 * should be enabled if the call is a video call and bluetooth or the wired headset are not in
881 * use.
882 *
883 * @param videoState The video state of the call.
884 * @return {@code true} if the speakerphone should be enabled.
885 */
886 private boolean isSpeakerphoneAutoEnabled(int videoState) {
Etan Cohen7bcbb342015-06-25 15:39:21 -0700887 return VideoProfile.isVideo(videoState) &&
Tyler Gunnd373c202015-05-20 08:21:03 -0700888 !mWiredHeadsetManager.isPluggedIn() &&
889 !mCallAudioManager.isBluetoothDeviceAvailable() &&
890 isSpeakerEnabledForVideoCalls();
891 }
892
893 /**
894 * Determines if the speakerphone should be automatically enabled for video calls.
895 *
896 * @return {@code true} if the speakerphone should automatically be enabled.
897 */
Rekha Kumard240fe82015-04-01 21:45:57 -0700898 private static boolean isSpeakerEnabledForVideoCalls() {
899 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
900 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
901 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
902 }
903
Santos Cordone3d76ab2014-01-28 17:25:20 -0800904 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700905 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
906 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800907 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800908 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700909 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700910 if (!mCalls.contains(call)) {
911 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800912 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700913 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700914 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700915 }
Sandeep Kunta39b77672014-09-01 17:22:57 +0530916 setActiveSubscription(getConversationSub());
Ihab Awadff7493a2014-06-10 13:47:44 -0700917 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800918 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800919 }
920
921 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700922 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700923 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700924 * @param digit The DTMF digit to play.
925 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700926 void playDtmfTone(Call call, char digit) {
927 if (!mCalls.contains(call)) {
928 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700929 } else {
930 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700931 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700932 }
933 }
934
935 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700936 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700937 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700938 void stopDtmfTone(Call call) {
939 if (!mCalls.contains(call)) {
940 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700941 } else {
942 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700943 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700944 }
945 }
946
947 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700948 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700949 */
Evan Charlton352105c2014-06-03 14:10:54 -0700950 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700951 if (!mCalls.contains(call)) {
952 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700953 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700954 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700955 }
956 }
957
958 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700959 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800960 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
961 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800962 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700963 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700964 Log.v(this, "disconnectCall %s", call);
965
Sailesh Nepale59bb192014-04-01 18:33:59 -0700966 if (!mCalls.contains(call)) {
967 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800968 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700969 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800970 call.disconnect();
971 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800972 }
Santos Cordon681663d2014-01-30 04:32:15 -0800973
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700974 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700975 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700976 */
977 void disconnectAllCalls() {
978 Log.v(this, "disconnectAllCalls");
979
980 for (Call call : mCalls) {
981 disconnectCall(call);
982 }
983 }
984
Nancy Chenbc9ef172014-08-15 17:11:57 -0700985
Tyler Gunn61b92102014-08-19 07:42:20 -0700986 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700987 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700988 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
989 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700990 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700991 void holdCall(Call call) {
992 if (!mCalls.contains(call)) {
993 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700994 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700995 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700996 call.hold();
997 }
998 }
999
1000 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001001 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001002 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
1003 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001004 */
Sailesh Nepale59bb192014-04-01 18:33:59 -07001005 void unholdCall(Call call) {
1006 if (!mCalls.contains(call)) {
1007 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001008 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -07001009 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001010 for (Call c : mCalls) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301011 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1012 PhoneAccountHandle ph1 = c.getTargetPhoneAccount();
Tyler Gunne44adb42015-01-02 10:55:35 -08001013 // Only attempt to hold parent calls and not the individual children.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301014 // if 'c' is not for same subscription as call, then don't disturb 'c'
1015 if (c != null && c.isAlive() && c != call && c.getParentCall() == null
1016 && (ph != null && ph1 != null &&
1017 isSamePhAccIdOrSipId(ph.getId(), ph1.getId()))) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001018 c.hold();
1019 }
1020 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001021 call.unhold();
1022 }
1023 }
1024
Sandeep Kunta39b77672014-09-01 17:22:57 +05301025 /**
1026 * Returns true if the ids are same or one of the ids is sip id.
1027 */
1028 private boolean isSamePhAccIdOrSipId(String id1, String id2) {
1029 boolean ret = ((id1 != null && id2 != null) &&
1030 (id1.equals(id2) || id1.contains("sip") || id2.contains("sip")));
1031 Log.d(this, "isSamePhAccIdOrSipId: id1 = " + id1 + " id2 = " + id2 + " ret = " + ret);
1032 return ret;
1033 }
1034
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001035 /** Called by the in-call UI to change the mute state. */
1036 void mute(boolean shouldMute) {
1037 mCallAudioManager.mute(shouldMute);
1038 }
1039
1040 /**
1041 * Called by the in-call UI to change the audio route, for example to change from earpiece to
1042 * speaker phone.
1043 */
1044 void setAudioRoute(int route) {
Ravindra5675ace2016-01-06 18:45:58 +05301045 Log.d(this, "Audio routed by user");
1046 mCallAudioManager.mUserSetAudioRoute = true;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001047 mCallAudioManager.setAudioRoute(route);
Ravindra5675ace2016-01-06 18:45:58 +05301048 mCallAudioManager.mUserSetAudioRoute = false;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001049 }
1050
Yorke Leed1346872014-07-28 14:38:45 -07001051 /** Called by the in-call UI to turn the proximity sensor on. */
1052 void turnOnProximitySensor() {
1053 mProximitySensorManager.turnOn();
1054 }
1055
1056 /**
1057 * Called by the in-call UI to turn the proximity sensor off.
1058 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
1059 * the screen will be kept off until the proximity sensor goes negative.
1060 */
1061 void turnOffProximitySensor(boolean screenOnImmediately) {
1062 mProximitySensorManager.turnOff(screenOnImmediately);
1063 }
1064
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001065 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -07001066 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001067 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -07001068 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -07001069 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
Santos Cordon92694512015-04-23 14:47:28 -07001070 // the SELECT_PHONE_ACCOUNT sequence run in parallel, if the user selects an account before the
Santos Cordonf193ba42014-09-12 06:37:39 -07001071 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
1072 // respecting a rewritten number or a canceled number. This is unlikely since
1073 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
1074 // a phone account from the in-call UI.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301075 Log.i(this, "phoneAccountSelected , id = %s", account.getId());
1076 updateLchStatus(account.getId());
Ihab Awadb78b2762014-07-25 15:16:23 -07001077 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -07001078
1079 // Note: emergency calls never go through account selection dialog so they never
1080 // arrive here.
1081 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001082 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -07001083 } else {
1084 call.disconnect();
1085 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001086
1087 if (setDefault) {
1088 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
1089 }
Nancy Chen53ceedc2014-07-08 18:56:51 -07001090 }
1091 }
1092
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001093 /** Called when the audio state changes. */
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001094 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001095 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001096 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001097 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001098 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001099 }
1100
Sailesh Nepale59bb192014-04-01 18:33:59 -07001101 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001102 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -08001103 }
1104
Sailesh Nepale59bb192014-04-01 18:33:59 -07001105 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001106 setCallState(call, CallState.DIALING, "dialing set explicitly");
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001107 maybeMoveToEarpiece(call);
Santos Cordond6782cd2015-04-16 09:57:50 -07001108 maybeMoveToSpeakerPhone(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301109 setActiveSubscription(call.getTargetPhoneAccount().getId());
Santos Cordon681663d2014-01-30 04:32:15 -08001110 }
1111
Sailesh Nepale59bb192014-04-01 18:33:59 -07001112 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001113 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -07001114 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -08001115 }
1116
Sailesh Nepale59bb192014-04-01 18:33:59 -07001117 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001118 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001119 }
1120
Santos Cordon049b7b62014-01-30 05:34:26 -08001121 /**
Santos Cordonf193ba42014-09-12 06:37:39 -07001122 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
1123 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -08001124 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001125 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -08001126 */
Andrew Lee701dc002014-09-11 21:29:12 -07001127 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
Suresh Koletief564312015-08-24 19:31:55 +05301128 // Show MT call in call log as a missed call if immediately disconnected
1129 // before creating a connection.
1130 if (!mCalls.contains(call) && (DisconnectCause.MISSED == disconnectCause.getCode())) {
1131 addCall(call);
1132 mMissedCallNotifier.showMissedCallNotification(call);
1133 }
Andrew Lee701dc002014-09-11 21:29:12 -07001134 call.setDisconnectCause(disconnectCause);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301135 int prevState = call.getState();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001136 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sandeep Kunta39b77672014-09-01 17:22:57 +05301137 String activeSub = getActiveSubscription();
1138 String conversationSub = getConversationSub();
1139 String lchSub = IsAnySubInLch();
1140
1141 PhoneAccount phAcc =
1142 getPhoneAccountRegistrar().getPhoneAccount(call.getTargetPhoneAccount());
1143 if (activeSub != null && (call.getTargetPhoneAccount() != null &&
1144 call.getTargetPhoneAccount().getId().equals(activeSub)) &&
1145 (phAcc != null) && (phAcc.isSet(PhoneAccount.LCH)) &&
1146 (conversationSub != null) &&
1147 (!conversationSub.equals(activeSub))) {
1148 Log.d(this,"Set active sub to conversation sub");
1149 setActiveSubscription(conversationSub);
1150 } else if ((conversationSub == null) && (lchSub != null) &&
1151 ((prevState == CallState.CONNECTING) || (prevState ==
1152 CallState.SELECT_PHONE_ACCOUNT)) &&
1153 (call.getState() == CallState.DISCONNECTED)) {
1154 Log.d(this,"remove sub with call from LCH");
1155 updateLchStatus(lchSub);
1156 setActiveSubscription(lchSub);
1157 manageDsdaInCallTones(false);
1158 }
1159
1160 if ((call.getTargetPhoneAccount() != null) && (phAcc != null) &&
1161 (phAcc.isSet(PhoneAccount.LCH))) {
1162 Call activecall = getFirstCallWithState(call.getTargetPhoneAccount().getId(),
1163 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1164 Log.d(this,"activecall: " + activecall);
1165 if (activecall == null) {
1166 phAcc.unSetBit(PhoneAccount.LCH);
1167 manageDsdaInCallTones(false);
1168 }
1169 }
1170 }
1171
1172 private String IsAnySubInLch() {
1173 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
1174 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
1175 if ((phAcc != null) && (phAcc.isSet(PhoneAccount.LCH))) {
1176 Log.d(this, "Sub in LCH: " + ph.getId());
1177 return ph.getId();
1178 }
1179 }
1180 return null;
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -07001181 }
1182
Ben Gilada0d9f752014-02-26 11:49:03 -08001183 /**
Ihab Awada02bef52014-08-13 18:18:42 -07001184 * Removes an existing disconnected call, and notifies the in-call app.
1185 */
1186 void markCallAsRemoved(Call call) {
1187 removeCall(call);
Sandeep Kunta39b77672014-09-01 17:22:57 +05301188 if (!hasAnyCalls()) {
1189 updateLchStatus(null);
1190 setActiveSubscription(null);
1191 manageDsdaInCallTones(false);
1192 }
Ihab Awad5b281322014-09-25 18:25:29 -07001193 if (mLocallyDisconnectingCalls.contains(call)) {
1194 mLocallyDisconnectingCalls.remove(call);
1195 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
1196 mForegroundCall.unhold();
1197 }
1198 }
Ihab Awada02bef52014-08-13 18:18:42 -07001199 }
1200
1201 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001202 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001203 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001204 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001205 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001206 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001207 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001208 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001209 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001210 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001211 if (call.getState() != CallState.DISCONNECTED) {
1212 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1213 }
1214 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001215 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001216 }
1217 }
1218 }
1219
Santos Cordondeb8c892014-05-30 01:38:03 -07001220 boolean hasAnyCalls() {
1221 return !mCalls.isEmpty();
1222 }
1223
Santos Cordonc7e85d42014-05-22 02:51:48 -07001224 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001225 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001226 }
1227
Sandeep Kunta39b77672014-09-01 17:22:57 +05301228 boolean hasActiveOrHoldingCall(String sub) {
1229 return (getFirstCallWithState(sub, CallState.ACTIVE, CallState.ON_HOLD) != null);
1230 }
1231
Santos Cordonc7e85d42014-05-22 02:51:48 -07001232 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001233 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001234 }
1235
Santos Cordondeb8c892014-05-30 01:38:03 -07001236 boolean onMediaButton(int type) {
1237 if (hasAnyCalls()) {
1238 if (HeadsetMediaButton.SHORT_PRESS == type) {
1239 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1240 if (ringingCall == null) {
1241 mCallAudioManager.toggleMute();
1242 return true;
1243 } else {
Andrew Lee38931d02014-07-16 10:17:36 -07001244 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -07001245 return true;
1246 }
1247 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1248 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1249 Call callToHangup = getFirstCallWithState(
1250 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
1251 if (callToHangup != null) {
1252 callToHangup.disconnect();
1253 return true;
1254 }
1255 }
1256 }
1257 return false;
1258 }
1259
1260 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001261 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001262 */
Santos Cordon91bd74c2014-11-07 16:10:22 -08001263 boolean canAddCall() {
Hall Liuea61e7a2016-01-27 11:07:07 -08001264 boolean isDeviceProvisioned = Settings.Global.getInt(mContext.getContentResolver(),
1265 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1266 if (!isDeviceProvisioned) {
1267 Log.d(TAG, "Device not provisioned, canAddCall is false.");
1268 return false;
1269 }
1270
Andrew Lee113ad622014-12-01 18:33:10 -08001271 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1272 return false;
1273 }
1274
Santos Cordon91bd74c2014-11-07 16:10:22 -08001275 int count = 0;
1276 for (Call call : mCalls) {
1277 if (call.isEmergencyCall()) {
1278 // We never support add call if one of the calls is an emergency call.
1279 return false;
Roshan Piusd9544092015-07-31 11:31:52 -07001280 } else if (!call.getChildCalls().isEmpty() && !call.can(Connection.CAPABILITY_HOLD)) {
1281 // This is to deal with CDMA conference calls. CDMA conference calls do not
1282 // allow the addition of another call when it is already in a 3 way conference.
1283 // So, we detect that it is a CDMA conference call by checking if the call has
1284 // some children and it does not support the CAPABILILTY_HOLD
1285 // TODO: This maybe cleaner if the lower layers can explicitly signal to telecom
1286 // about this limitation (b/22880180).
1287 return false;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001288 } else if (call.getParentCall() == null) {
1289 count++;
1290 }
Santos Cordon10838c22014-06-11 17:36:04 -07001291
Santos Cordon91bd74c2014-11-07 16:10:22 -08001292 // We do not check states for canAddCall. We treat disconnected calls the same
1293 // and wait until they are removed instead. If we didn't count disconnected calls,
1294 // we could put InCallServices into a state where they are showing two calls but
1295 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1296 // and acts better if we wait until the call is removed.
Sandeep Kunta39b77672014-09-01 17:22:57 +05301297 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1298 == TelephonyManager.MultiSimVariants.DSDA) {
1299 if (count >= MAXIMUM_DSDA_TOP_LEVEL_CALLS) {
1300 return false;
1301 }
1302 } else if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001303 return false;
1304 }
1305 }
1306 return true;
1307 }
1308
Ihab Awada3653902015-01-23 13:44:46 -08001309 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001310 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001311 return getFirstCallWithState(CallState.RINGING);
1312 }
1313
1314 Call getActiveCall() {
1315 return getFirstCallWithState(CallState.ACTIVE);
1316 }
1317
Nancy Chen05a9e402014-09-26 14:14:32 -07001318 Call getDialingCall() {
1319 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001320 }
1321
1322 Call getHeldCall() {
1323 return getFirstCallWithState(CallState.ON_HOLD);
1324 }
1325
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001326 int getNumHeldCalls() {
1327 int count = 0;
1328 for (Call call : mCalls) {
1329 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1330 count++;
1331 }
1332 }
1333 return count;
1334 }
1335
Shriram Ganeshd7dd7272015-12-11 15:52:15 -08001336 int getNumTopLevelCalls() {
1337 int count = 0;
1338 for (Call call : mCalls) {
1339 if (call.getParentCall() == null) {
1340 count++;
1341 }
1342 }
1343 return count;
1344 }
1345
Roshan Pius93c9c012015-08-28 11:10:56 -07001346 Call getOutgoingCall() {
1347 return getFirstCallWithState(OUTGOING_CALL_STATES);
1348 }
1349
Santos Cordonf193ba42014-09-12 06:37:39 -07001350 Call getFirstCallWithState(int... states) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301351 return getFirstCallWithState((Call) null, states);
Santos Cordonf193ba42014-09-12 06:37:39 -07001352 }
1353
Santos Cordon10838c22014-06-11 17:36:04 -07001354 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001355 * Returns the first call that it finds with the given states. The states are treated as having
1356 * priority order so that any call with the first state will be returned before any call with
1357 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001358 *
1359 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001360 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001361 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001362 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001363 // check the foreground first
1364 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1365 return mForegroundCall;
1366 }
1367
Santos Cordondeb8c892014-05-30 01:38:03 -07001368 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001369 if (Objects.equals(callToSkip, call)) {
1370 continue;
1371 }
1372
1373 // Only operate on top-level calls
1374 if (call.getParentCall() != null) {
1375 continue;
1376 }
1377
Santos Cordondeb8c892014-05-30 01:38:03 -07001378 if (currentState == call.getState()) {
1379 return call;
1380 }
1381 }
1382 }
1383 return null;
1384 }
1385
Sandeep Kunta39b77672014-09-01 17:22:57 +05301386 /**
1387 * Returns the first call that it finds with the given states for given subscription.
1388 * the states are treated as having priority order so that any call with the first
1389 * state will be returned before any call with states listed later in the parameter list.
1390 *
1391 * @param subId check calls only on this subscription
1392 * @param callToSkip Call that this method should skip while searching
1393 */
1394 Call getFirstCallWithState(String subId, Call callToSkip, int... states) {
1395 for (int currentState : states) {
1396 // check the foreground first
1397 if (mForegroundCall != null && mForegroundCall.getState() == currentState
1398 && mForegroundCall.getTargetPhoneAccount() != null
1399 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(), subId)) {
1400 return mForegroundCall;
1401 }
1402
1403 for (Call call : mCalls) {
1404 if (Objects.equals(callToSkip, call)) {
1405 continue;
1406 }
1407
1408 // Only operate on top-level calls
1409 if (call.getParentCall() != null) {
1410 continue;
1411 }
1412
1413 if (currentState == call.getState() && call.getTargetPhoneAccount() != null
1414 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1415 return call;
1416 }
1417 }
1418 }
1419 return null;
1420 }
1421
Santos Cordon0fbe6322014-08-14 04:04:25 -07001422 Call createConferenceCall(
1423 PhoneAccountHandle phoneAccount,
1424 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001425
1426 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1427 // which is the default value for new Calls.
1428 long connectTime =
1429 parcelableConference.getConnectTimeMillis() ==
1430 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1431 parcelableConference.getConnectTimeMillis();
1432
Santos Cordon0fbe6322014-08-14 04:04:25 -07001433 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001434 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001435 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001436 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001437 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001438 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001439 mCallerInfoAsyncQueryFactory,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001440 null /* handle */,
1441 null /* gatewayInfo */,
1442 null /* connectionManagerPhoneAccount */,
1443 phoneAccount,
1444 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001445 true /* isConference */,
1446 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001447
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001448 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1449 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001450 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Rekha Kumard240fe82015-04-01 21:45:57 -07001451 call.setVideoState(parcelableConference.getVideoState());
1452 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001453 call.setStatusHints(parcelableConference.getStatusHints());
Santos Cordonb3907b32015-05-27 17:39:59 -07001454 call.setExtras(parcelableConference.getExtras());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001455
1456 // TODO: Move this to be a part of addCall()
1457 call.addListener(this);
1458 addCall(call);
1459 return call;
1460 }
1461
Yorke Leef86db2e2014-09-12 17:56:48 -07001462 /**
1463 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1464 */
1465 int getCallState() {
1466 return mPhoneStateBroadcaster.getCallState();
1467 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001468
Santos Cordon4b2c1192014-03-19 18:15:38 -07001469 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001470 * Retrieves the {@link PhoneAccountRegistrar}.
1471 *
1472 * @return The {@link PhoneAccountRegistrar}.
1473 */
1474 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1475 return mPhoneAccountRegistrar;
1476 }
1477
1478 /**
1479 * Retrieves the {@link MissedCallNotifier}
1480 * @return The {@link MissedCallNotifier}.
1481 */
1482 MissedCallNotifier getMissedCallNotifier() {
1483 return mMissedCallNotifier;
1484 }
1485
1486 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001487 * Adds the specified call to the main list of live calls.
1488 *
1489 * @param call The call to add.
1490 */
1491 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001492 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001493 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001494 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001495 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001496
Santos Cordondf399862014-08-06 04:39:15 -07001497 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001498 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001499 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001500 if (Log.SYSTRACE_DEBUG) {
1501 Trace.beginSection(listener.getClass().toString() + " addCall");
1502 }
Santos Cordona56f2762014-03-24 15:55:53 -07001503 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001504 if (Log.SYSTRACE_DEBUG) {
1505 Trace.endSection();
1506 }
Santos Cordona56f2762014-03-24 15:55:53 -07001507 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001508 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001509 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001510 }
1511
Sailesh Nepal810735e2014-03-18 18:15:46 -07001512 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001513 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001514 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001515
Santos Cordon672321e2014-08-21 14:38:58 -07001516 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001517 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001518 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001519
1520 boolean shouldNotify = false;
1521 if (mCalls.contains(call)) {
1522 mCalls.remove(call);
1523 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001524 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001525
Santos Cordon2685dab2015-04-17 17:12:09 -07001526 call.destroy();
1527
Sailesh Nepale59bb192014-04-01 18:33:59 -07001528 // Only broadcast changes for calls that are being tracked.
1529 if (shouldNotify) {
1530 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001531 if (Log.SYSTRACE_DEBUG) {
1532 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1533 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001534 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001535 if (Log.SYSTRACE_DEBUG) {
1536 Trace.endSection();
1537 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001538 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001539 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001540 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001541 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001542 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001543
Sailesh Nepal810735e2014-03-18 18:15:46 -07001544 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001545 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001546 *
1547 * @param call The call.
1548 * @param newState The new state of the call.
1549 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001550 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001551 if (call == null) {
1552 return;
1553 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001554 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001555 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1556 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001557 if (newState != oldState) {
1558 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1559 // 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 -07001560 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001561 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001562 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001563 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001564 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001565 call.setState(newState, tag);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001566
Yorke Leee4a9c412014-11-14 16:59:42 -08001567 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001568 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001569 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001570 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001571 if (Log.SYSTRACE_DEBUG) {
1572 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1573 }
Santos Cordona56f2762014-03-24 15:55:53 -07001574 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001575 if (Log.SYSTRACE_DEBUG) {
1576 Trace.endSection();
1577 }
Santos Cordona56f2762014-03-24 15:55:53 -07001578 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001579 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001580 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001581 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001582 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05301583 manageDsdaInCallTones(false);
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001584 }
1585
1586 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001587 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001588 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001589 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001590 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001591 Call newForegroundCall = null;
Santos Cordondf399862014-08-06 04:39:15 -07001592 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001593 // of its state will be foreground by default and instead the connection service should
1594 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001595 // the call should play audio and listen to microphone if it wants.
1596
Sandeep Kunta39b77672014-09-01 17:22:57 +05301597 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1598 == TelephonyManager.MultiSimVariants.DSDA) {
1599 String lchSub = getLchSub();
1600 for (Call call : mCalls) {
1601 // Only top-level calls can be in foreground
1602 if (call.getParentCall() != null) {
1603 continue;
1604 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001605
Sandeep Kunta39b77672014-09-01 17:22:57 +05301606 PhoneAccountHandle ph = call.getTargetPhoneAccount();
1607 if (ph != null && ph.getId().equals(lchSub)) continue;
1608 // Active calls have priority.
1609 if (call.isActive()) {
1610 newForegroundCall = call;
1611 break;
1612 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001613
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001614 // If only call in call list is held call it's also a foreground call
Shriram Ganeshd7dd7272015-12-11 15:52:15 -08001615 if (getNumTopLevelCalls() == 1 && call.getState() == CallState.ON_HOLD) {
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001616 newForegroundCall = call;
1617 }
1618
Shriram Ganesh74254492015-10-26 10:36:19 -07001619 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
1620 || call.getState() == CallState.RINGING) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301621 newForegroundCall = call;
1622 // Don't break in case there's an active call that has priority.
1623 }
1624 }
1625 // if active sub doesn't have any calls, then consider calls on all subs,
1626 // which ever call is active set that as foreground call. give more priority
1627 // to ringing call on LCH sub over active call.
1628 if (newForegroundCall == null) {
1629 newForegroundCall = getFirstCallWithState(CallState.RINGING);
1630 if (newForegroundCall == null) {
1631 newForegroundCall = getFirstCallWithState(CallState.ACTIVE);
1632 }
1633 }
1634 } else {
1635 for (Call call : mCalls) {
1636 // Only top-level calls can be in foreground
1637 if (call.getParentCall() != null) {
1638 continue;
1639 }
1640
1641 // Active calls have priority.
1642 if (call.isActive()) {
1643 newForegroundCall = call;
1644 break;
1645 }
1646
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001647 // If only call in call list is held call it's also a foreground call
Shriram Ganeshd7dd7272015-12-11 15:52:15 -08001648 if (getNumTopLevelCalls() == 1 && call.getState() == CallState.ON_HOLD) {
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001649 newForegroundCall = call;
1650 }
1651
Shriram Ganesh74254492015-10-26 10:36:19 -07001652 if ((call.isAlive() && call.getState() != CallState.ON_HOLD)
1653 || call.getState() == CallState.RINGING) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301654 newForegroundCall = call;
1655 // Don't break in case there's an active call that has priority.
1656 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001657 }
1658 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001659
Shriram Ganesh8ad27bd2015-11-03 17:12:59 -08001660 /* In the case where there are 2 calls, when the Hold button is pressed for active
1661 * call, as an intermediate state we would have both calls in held state before
1662 * the background call is moved to active state. In such an intermediate stage
1663 * updating the newForegroundCall to null causes issues with abandoning audio
1664 * focus. Skip updating the foreground call with null in such cases. */
1665 if (newForegroundCall == null && getFirstCallWithState(CallState.ON_HOLD) != null) {
1666 Log.v(this, "Skip updating foreground call in intermediate stage");
1667 newForegroundCall = mForegroundCall;
1668 }
1669
Sailesh Nepal810735e2014-03-18 18:15:46 -07001670 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001671 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001672 Call oldForegroundCall = mForegroundCall;
1673 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001674
Santos Cordona56f2762014-03-24 15:55:53 -07001675 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001676 if (Log.SYSTRACE_DEBUG) {
1677 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1678 }
Santos Cordona56f2762014-03-24 15:55:53 -07001679 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001680 if (Log.SYSTRACE_DEBUG) {
1681 Trace.endSection();
1682 }
Santos Cordona56f2762014-03-24 15:55:53 -07001683 }
Santos Cordon681663d2014-01-30 04:32:15 -08001684 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001685 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001686 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001687
Santos Cordon91bd74c2014-11-07 16:10:22 -08001688 private void updateCanAddCall() {
1689 boolean newCanAddCall = canAddCall();
1690 if (newCanAddCall != mCanAddCall) {
1691 mCanAddCall = newCanAddCall;
1692 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001693 if (Log.SYSTRACE_DEBUG) {
1694 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1695 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001696 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001697 if (Log.SYSTRACE_DEBUG) {
1698 Trace.endSection();
1699 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001700 }
1701 }
1702 }
1703
1704 private void updateCallsManagerState() {
1705 updateForegroundCall();
1706 updateCanAddCall();
1707 }
1708
Yorke Leeb701f6d2014-08-12 14:04:19 -07001709 private boolean isPotentialMMICode(Uri handle) {
1710 return (handle != null && handle.getSchemeSpecificPart() != null
1711 && handle.getSchemeSpecificPart().contains("#"));
1712 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001713
Tyler Gunn5b452df2014-10-01 15:02:58 -07001714 /**
1715 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1716 * MMI codes which can be dialed when one or more calls are in progress.
1717 * <P>
1718 * Checks for numbers formatted similar to the MMI codes defined in:
1719 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1720 * and
1721 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1722 *
1723 * @param handle The URI to call.
1724 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1725 */
1726 private boolean isPotentialInCallMMICode(Uri handle) {
1727 if (handle != null && handle.getSchemeSpecificPart() != null &&
1728 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1729
1730 String dialedNumber = handle.getSchemeSpecificPart();
1731 return (dialedNumber.equals("0") ||
1732 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1733 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1734 dialedNumber.equals("3") ||
1735 dialedNumber.equals("4") ||
1736 dialedNumber.equals("5"));
1737 }
1738 return false;
1739 }
1740
Santos Cordonf193ba42014-09-12 06:37:39 -07001741 private int getNumCallsWithState(int... states) {
1742 int count = 0;
1743 for (int state : states) {
1744 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001745 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001746 count++;
1747 }
1748 }
1749 }
1750 return count;
1751 }
1752
Sandeep Kunta39b77672014-09-01 17:22:57 +05301753 private int getNumCallsWithState(String subId, int... states) {
1754 int count = 0;
1755 for (int state : states) {
1756 for (Call call : mCalls) {
1757 if (call.getParentCall() == null && call.getState() == state
1758 && call.getTargetPhoneAccount() != null
1759 && isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), subId)) {
1760 count++;
1761 }
1762 }
1763 }
1764 return count;
1765 }
1766
Santos Cordonf193ba42014-09-12 06:37:39 -07001767 private boolean hasMaximumLiveCalls() {
1768 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1769 }
1770
Sandeep Kunta39b77672014-09-01 17:22:57 +05301771 private boolean hasMaximumLiveCalls(String subId) {
1772 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(subId, LIVE_CALL_STATES);
1773 }
1774
Santos Cordonf193ba42014-09-12 06:37:39 -07001775 private boolean hasMaximumHoldingCalls() {
1776 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1777 }
1778
Sandeep Kunta39b77672014-09-01 17:22:57 +05301779 private boolean hasMaximumHoldingCalls(String subId) {
1780 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(subId, CallState.ON_HOLD);
1781 }
1782
Santos Cordonf193ba42014-09-12 06:37:39 -07001783 private boolean hasMaximumRingingCalls() {
1784 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1785 }
1786
Sandeep Kunta39b77672014-09-01 17:22:57 +05301787 private boolean hasMaximumRingingCalls(String subId) {
1788 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(subId, CallState.RINGING);
1789 }
1790
Santos Cordonf193ba42014-09-12 06:37:39 -07001791 private boolean hasMaximumOutgoingCalls() {
1792 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1793 }
1794
Roshan Pius4de4a892015-08-24 11:06:58 -07001795 private boolean hasMaximumDialingCalls() {
1796 return MAXIMUM_DIALING_CALLS <= getNumCallsWithState(CallState.DIALING);
1797 }
1798
Santos Cordonf193ba42014-09-12 06:37:39 -07001799 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
Sandeep Kunta39b77672014-09-01 17:22:57 +05301800 if (TelephonyManager.getDefault().getMultiSimConfiguration()
1801 == TelephonyManager.MultiSimVariants.DSDA) {
1802 return makeRoomForOutgoingCallForDsda(call, isEmergency);
1803 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001804 if (hasMaximumLiveCalls()) {
1805 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1806 // have to change.
1807 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301808 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1809 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001810
Santos Cordond1766502014-09-26 15:45:39 -07001811 if (call == liveCall) {
1812 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001813 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001814 // state since the call was already populated into the list.
1815 return true;
1816 }
1817
Andrew Leee6595182014-09-23 18:43:05 -07001818 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001819 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1820 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1821 // Disconnect the current outgoing call if it's not an emergency call. If the
1822 // user tries to make two outgoing calls to different emergency call numbers,
1823 // we will try to connect the first outgoing call.
1824 outgoingCall.disconnect();
1825 return true;
1826 }
1827 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1828 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1829 // state, just disconnect it since the user has explicitly started a new call.
1830 outgoingCall.disconnect();
1831 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001832 }
1833 return false;
1834 }
1835
Santos Cordonf193ba42014-09-12 06:37:39 -07001836 if (hasMaximumHoldingCalls()) {
1837 // There is no more room for any more calls, unless it's an emergency.
1838 if (isEmergency) {
1839 // Kill the current active call, this is easier then trying to disconnect a
1840 // holding call and hold an active call.
1841 liveCall.disconnect();
1842 return true;
1843 }
1844 return false; // No more room!
1845 }
1846
1847 // We have room for at least one more holding call at this point.
1848
Tyler Gunne4cd9162015-08-11 15:36:04 -07001849 // TODO: Remove once b/23035408 has been corrected.
1850 // If the live call is a conference, it will not have a target phone account set. This
1851 // means the check to see if the live call has the same target phone account as the new
1852 // call will not cause us to bail early. As a result, we'll end up holding the
1853 // ongoing conference call. However, the ConnectionService is already doing that. This
1854 // has caused problems with some carriers. As a workaround until b/23035408 is
1855 // corrected, we will try and get the target phone account for one of the conference's
1856 // children and use that instead.
1857 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
1858 if (liveCallPhoneAccount == null && liveCall.isConference() &&
1859 !liveCall.getChildCalls().isEmpty()) {
1860 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
1861 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
1862 liveCallPhoneAccount);
1863 }
1864
Santos Cordonf193ba42014-09-12 06:37:39 -07001865 // First thing, if we are trying to make a call with the same phone account as the live
1866 // call, then allow it so that the connection service can make its own decision about
1867 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07001868 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
1869 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001870 return true;
1871 } else if (call.getTargetPhoneAccount() == null) {
1872 // Without a phone account, we can't say reliably that the call will fail.
1873 // If the user chooses the same phone account as the live call, then it's
1874 // still possible that the call can be made (like with CDMA calls not supporting
1875 // hold but they still support adding a call by going immediately into conference
1876 // mode). Return true here and we'll run this code again after user chooses an
1877 // account.
1878 return true;
1879 }
1880
1881 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001882 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07001883 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Santos Cordonf193ba42014-09-12 06:37:39 -07001884 liveCall.hold();
1885 return true;
1886 }
1887
1888 // The live call cannot be held so we're out of luck here. There's no room.
1889 return false;
1890 }
1891 return true;
1892 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001893
Sandeep Kunta39b77672014-09-01 17:22:57 +05301894 private boolean makeRoomForOutgoingCallForDsda(Call call, boolean isEmergency) {
1895 if (isEmergency || (call.getState() == CallState.SELECT_PHONE_ACCOUNT)) {
1896 return true;
1897 }
1898
1899 PhoneAccountHandle phAcc = call.getTargetPhoneAccount();
1900 if (phAcc == null) {
1901 if (getNumCallsWithState(LIVE_CALL_STATES) == MAXIMUM_DSDA_LIVE_CALLS
1902 && getNumCallsWithState(CallState.ON_HOLD) == MAXIMUM_DSDA_HOLD_CALLS) {
1903 return false;
1904 } else {
1905 return true;
1906 }
1907 }
1908 if (hasMaximumLiveCalls(phAcc.getId())) {
1909 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1910 // have to change.
1911 Call liveCall = getFirstCallWithState(phAcc.getId(), call, LIVE_CALL_STATES);
1912
1913 if (hasMaximumHoldingCalls(phAcc.getId())) {
1914 // There is no more room for any more calls, unless it's an emergency.
1915 return false; // No more room!
1916 }
1917 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1918 return true;
1919 }
1920 // Try to hold the live call before attempting the new outgoing call.
1921 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
1922 liveCall.hold();
1923 return true;
1924 }
1925
1926 // The live call cannot be held so we're out of luck here. There's no room.
1927 return false;
1928 }
1929 return true;
1930 }
1931
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001932 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07001933 * Given a call, find the first non-null phone account handle of its children.
1934 *
1935 * @param parentCall The parent call.
1936 * @return The first non-null phone account handle of the children, or {@code null} if none.
1937 */
1938 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
1939 for (Call childCall : parentCall.getChildCalls()) {
1940 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
1941 if (childPhoneAccount != null) {
1942 return childPhoneAccount;
1943 }
1944 }
1945 return null;
1946 }
1947
1948 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07001949 * Checks to see if the call should be on speakerphone and if so, set it.
1950 */
1951 private void maybeMoveToSpeakerPhone(Call call) {
1952 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001953 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07001954 call.setStartWithSpeakerphoneOn(false);
1955 }
1956 }
1957
Nivedita Sarkardae2c652015-08-31 10:55:51 -07001958 private void maybeMoveToEarpiece(Call call) {
1959 if (!call.getStartWithSpeakerphoneOn() && !mWiredHeadsetManager.isPluggedIn() &&
1960 !mCallAudioManager.isBluetoothDeviceAvailable()) {
1961 setAudioRoute(CallAudioState.ROUTE_EARPIECE);
1962 }
1963 }
1964
Santos Cordond6782cd2015-04-16 09:57:50 -07001965 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001966 * Creates a new call for an existing connection.
1967 *
1968 * @param callId The id of the new call.
1969 * @param connection The connection information.
1970 * @return The new call.
1971 */
1972 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1973 Call call = new Call(
1974 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001975 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001976 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001977 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001978 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001979 mCallerInfoAsyncQueryFactory,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001980 connection.getHandle() /* handle */,
1981 null /* gatewayInfo */,
1982 null /* connectionManagerPhoneAccount */,
1983 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1984 false /* isIncoming */,
Roshan Pius084ab082015-07-15 12:17:04 -07001985 false /* isConference */,
1986 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001987
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001988 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
1989 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001990 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001991 call.setCallerDisplayName(connection.getCallerDisplayName(),
1992 connection.getCallerDisplayNamePresentation());
1993
1994 call.addListener(this);
1995 addCall(call);
1996
1997 return call;
1998 }
1999
2000 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002001 * Dumps the state of the {@link CallsManager}.
2002 *
2003 * @param pw The {@code IndentingPrintWriter} to write the state to.
2004 */
2005 public void dump(IndentingPrintWriter pw) {
2006 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002007 if (mCalls != null) {
2008 pw.println("mCalls: ");
2009 pw.increaseIndent();
2010 for (Call call : mCalls) {
2011 pw.println(call);
2012 }
2013 pw.decreaseIndent();
2014 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07002015 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
2016
2017 if (mCallAudioManager != null) {
2018 pw.println("mCallAudioManager:");
2019 pw.increaseIndent();
2020 mCallAudioManager.dump(pw);
2021 pw.decreaseIndent();
2022 }
2023
2024 if (mTtyManager != null) {
2025 pw.println("mTtyManager:");
2026 pw.increaseIndent();
2027 mTtyManager.dump(pw);
2028 pw.decreaseIndent();
2029 }
2030
2031 if (mInCallController != null) {
2032 pw.println("mInCallController:");
2033 pw.increaseIndent();
2034 mInCallController.dump(pw);
2035 pw.decreaseIndent();
2036 }
2037
2038 if (mConnectionServiceRepository != null) {
2039 pw.println("mConnectionServiceRepository:");
2040 pw.increaseIndent();
2041 mConnectionServiceRepository.dump(pw);
2042 pw.decreaseIndent();
2043 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002044 }
Sandeep Kunta39b77672014-09-01 17:22:57 +05302045
2046 private final Handler mLchHandler = new LchHandler();
2047 private final class LchHandler extends Handler {
2048 @Override
2049 public void handleMessage(Message msg) {
2050 switch (msg.what) {
2051 case PHONE_START_DSDA_INCALL_TONE:
2052 Log.d(this, "Start DSDA incall tones...");
2053 startDsdaInCallTones();
2054 break;
2055 case LCH_PLAY_DTMF:
2056 playLchDtmf();
2057 break;
2058 case LCH_STOP_DTMF:
2059 stopLchDtmf();
2060 break;
2061 }
2062 }
2063 }
2064
2065 public void switchToOtherActiveSub(String subId) {
2066 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2067 != TelephonyManager.MultiSimVariants.DSDA) {
2068 return;
2069 }
2070 Log.d(this, "switchToOtherActiveSub sub:" + subId);
2071 setActiveSubscription(subId);
2072 updateLchStatus(subId);
2073 manageDsdaInCallTones(true);
2074 updateForegroundCall();
2075 }
2076
2077 public synchronized void setActiveSubscription(String subId) {
2078 Log.d(this, "setActiveSubscription = " + subId);
2079 if (TelephonyManager.getDefault().getMultiSimConfiguration()
2080 != TelephonyManager.MultiSimVariants.DSDA) {
2081 return;
2082 }
2083 boolean changed = false;
2084 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2085 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2086 if (subId != null && subId.equals(ph.getId())
2087 && (phAcc != null) && !phAcc.isSet(PhoneAccount.ACTIVE)) {
2088 changed = true;
2089 phAcc.setBit(PhoneAccount.ACTIVE);
2090 } else if (subId != null && !subId.equals(ph.getId())
2091 && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2092 changed = true;
2093 phAcc.unSetBit(PhoneAccount.ACTIVE);
2094 } else if ((subId == null) && (phAcc != null) && phAcc.isSet(PhoneAccount.ACTIVE)) {
2095 phAcc.unSetBit(PhoneAccount.ACTIVE);
2096 }
2097 }
2098 if (!changed) {
2099 Log.d(this, "setActiveSubscription not changed ");
2100 return;
2101 } else {
2102 Log.d(this, "setActiveSubscription changed " );
2103 for (Call call : mCalls) {
2104 PhoneAccountHandle ph = call.getTargetPhoneAccount();
2105 if (ph != null) {
2106 call.mIsActiveSub = ph.getId().equals(subId) ? true : false;
2107 }
2108 for (CallsManagerListener listener : mListeners) {
2109 listener.onCallStateChanged(call, call.getState(), call.getState());
2110 }
2111 }
2112 }
2113 }
2114
2115 public String getActiveSubscription() {
2116 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2117 if (getPhoneAccountRegistrar()
2118 .getPhoneAccount(ph).isSet(PhoneAccount.ACTIVE)) {
2119 Log.d(this, "getActiveSubscription: " + ph.getId());
2120 return ph.getId();
2121 }
2122 }
2123 return null;
2124 }
2125
2126 private String getConversationSub() {
2127 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2128 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2129 if (phAcc != null && !phAcc.isSet(PhoneAccount.LCH) &&
2130 (getFirstCallWithState(ph.getId(), CallState.ACTIVE, CallState.DIALING,
2131 CallState.ON_HOLD) != null)) {
2132 Log.d(this, "getConversationSub: " + ph.getId());
2133 return ph.getId();
2134 }
2135 }
2136 return null;
2137 }
2138
2139 void manageDsdaInCallTones(boolean isSubSwitch) {
2140 Log.d(this, " entered manageDsdaInCallTones ");
2141
2142 // If there is no background active subscription available, stop playing the tones.
2143 // Do not start/stop LCH/SCH tones when phone is in RINGING state.
2144 if (getLchSub() != null && !hasRingingCall()) {
2145 //If sub switch happens re-start the tones with a delay of 100msec.
2146 if (isSubSwitch) {
2147 Log.d(this, " manageDsdaInCallTones: re-start playing tones, lch sub = "
2148 + getLchSub());
2149 reStartDsdaInCallTones();
2150 } else {
2151 Log.d(this, " entered manageDsdaInCallTones ");
2152 startDsdaInCallTones();
2153 }
2154 } else if (getLchSub() == null) {
2155 // if there is no sub in Lch state, then stop playing the tones
2156 stopMSimInCallTones();
2157 }
2158 }
2159
2160 private void reStartDsdaInCallTones() {
2161 Log.d(this, " reStartDsdaInCallTones");
2162 stopMSimInCallTones();
2163 /* Remove any pending PHONE_START_DSDA_INCALL_TONE messages from queue */
2164 mLchHandler.removeMessages(PHONE_START_DSDA_INCALL_TONE);
2165 Message message = Message.obtain(mLchHandler, PHONE_START_DSDA_INCALL_TONE);
2166 mLchHandler.sendMessageDelayed(message, 100);
2167 }
2168
2169 /**
2170 * Returns the first call that it finds with the given states for given subscription.
2171 * The states are treated as having priority order so that any call with the first
2172 * state will be returned before any call with states listed later in the parameter list.
2173 */
2174 Call getFirstCallWithState(String sub, int... states) {
2175 for (int currentState : states) {
2176 // check the foreground first
2177 if (mForegroundCall != null && mForegroundCall.getState() == currentState
2178 && (mForegroundCall.getTargetPhoneAccount() != null)
2179 && isSamePhAccIdOrSipId(mForegroundCall.getTargetPhoneAccount().getId(),
2180 sub)) {
2181 return mForegroundCall;
2182 }
2183
2184 for (Call call : mCalls) {
2185 if ((currentState == call.getState()) &&
2186 (call.getTargetPhoneAccount() != null) &&
2187 (isSamePhAccIdOrSipId(call.getTargetPhoneAccount().getId(), sub))) {
2188 return call;
2189 }
2190 }
2191 }
2192 return null;
2193 }
2194
2195 private String getLchSub() {
2196 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2197 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2198 if (phAcc != null && phAcc.isSet(PhoneAccount.LCH)) {
2199 return ph.getId();
2200 }
2201 }
2202 return null;
2203 }
2204
2205 private void playLchDtmf() {
2206 if (mLchSub != null || mLchHandler.hasMessages(LCH_PLAY_DTMF)) {
2207 // Ignore any redundant requests to start playing tones
2208 return;
2209 }
2210 mLchSub = getLchSub();
2211 Log.d(this, " playLchDtmf... lch sub " + mLchSub);
2212 if (mLchSub == null) return;
2213 removeAnyPendingDtmfMsgs();
2214 char c = mContext.getResources()
2215 .getString(R.string.lch_dtmf_key).charAt(0);
2216 Call call = getNonRingingLiveCall(mLchSub);
2217 if (call == null) {
2218 mLchSub = null;
2219 return;
2220 }
2221 call.playDtmfTone(c);
2222 // Keep playing LCH DTMF tone to remote party on LCH call, with periodicity
2223 // "LCH_DTMF_PERIODICITY" until call moves out of LCH.
2224 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_PLAY_DTMF),
2225 LCH_DTMF_PERIODICITY);
2226 mLchHandler.sendMessageDelayed(Message.obtain(mLchHandler, LCH_STOP_DTMF), LCH_DTMF_PERIOD);
2227 }
2228
2229 private Call getNonRingingLiveCall(String subId) {
2230 return getFirstCallWithState(subId, CallState.DIALING,
2231 CallState.ACTIVE, CallState.ON_HOLD);
2232 }
2233
2234 private void stopLchDtmf() {
2235 if (mLchSub != null) {
2236 // Ignore any redundant requests to stop playing tones
2237 Call call = getNonRingingLiveCall(mLchSub);
2238 Log.d(this, " stopLchDtmf... call: " + call + " mLchSub:" + mLchSub);
2239 if (call == null) {
2240 mLchSub = null;
2241 return;
2242 }
2243 call.stopDtmfTone();
2244 }
2245 mLchSub = null;
2246 }
2247
2248 private void startDsdaInCallTones() {
2249 if (mLocalCallReminderTonePlayer == null) {
2250 Log.d(this, " Play local call hold reminder tone ");
2251 mLocalCallReminderTonePlayer =
2252 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_HOLD_RECALL);
2253 mLocalCallReminderTonePlayer.start();
2254 }
2255 if (sSupervisoryCallHoldToneConfig.equals("inband")) {
2256 // if "persist.radio.sch_tone" is set to "inband", play inband supervisory
2257 // call hold tone. if set to "dtmf", play the SCH tones
2258 // over DTMF, don't play SCH tones for anyother value.
2259 if (mSupervisoryCallHoldTonePlayer == null) {
2260 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone ");
2261 mSupervisoryCallHoldTonePlayer =
2262 mPlayerFactory.createPlayer(InCallTonePlayer.TONE_SUPERVISORY_CH);
2263 mSupervisoryCallHoldTonePlayer.start();
2264 }
2265 } else if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2266 Log.d(this, " startDsdaInCallTones: Supervisory call hold tone over dtmf ");
2267 playLchDtmf();
2268 }
2269 }
2270
2271 private void removeAnyPendingDtmfMsgs() {
2272 mLchHandler.removeMessages(LCH_PLAY_DTMF);
2273 mLchHandler.removeMessages(LCH_STOP_DTMF);
2274 }
2275
2276 protected void stopMSimInCallTones() {
2277 if (mLocalCallReminderTonePlayer != null) {
2278 Log.d(this, " stopMSimInCallTones: local call hold reminder tone ");
2279 mLocalCallReminderTonePlayer.stopTone();
2280 mLocalCallReminderTonePlayer = null;
2281 }
2282 if (mSupervisoryCallHoldTonePlayer != null) {
2283 Log.d(this, " stopMSimInCallTones: Supervisory call hold tone ");
2284 mSupervisoryCallHoldTonePlayer.stopTone();
2285 mSupervisoryCallHoldTonePlayer = null;
2286 }
2287 if (sSupervisoryCallHoldToneConfig.equals("dtmf")) {
2288 Log.d(this, " stopMSimInCallTones: stop SCH Dtmf call hold tone ");
2289 stopLchDtmf();
2290 /* Remove any previous dtmf nssages from queue */
2291 removeAnyPendingDtmfMsgs();
2292 }
2293 }
2294
2295 /**
2296 * Update the local call hold state for all subscriptions
2297 * 1 -- if call on local hold, 0 -- if call is not on local hold
2298 *
2299 * @param subInConversation is the sub user is currently active in subsription.
2300 * so if this sub is in LCH, then bring that sub out of LCH.
2301 */
2302 private void updateLchStatus(String subInConversation) {
2303 Call removeFromLch = null;
2304 Log.d(this, "updateLchStatus subInConversation: " + subInConversation);
2305 if (subInConversation != null && subInConversation.contains("sip")) {
2306 return;
2307 }
2308 for (PhoneAccountHandle ph : getPhoneAccountRegistrar().getSimPhoneAccounts()) {
2309 String sub = ph.getId();
2310 if (sub != null && sub.contains("sip")) {
2311 Log.d(this, "update lch. Skipping account: " + sub);
2312 continue;
2313 }
2314 PhoneAccount phAcc = getPhoneAccountRegistrar().getPhoneAccount(ph);
2315 boolean lchState = false;
2316 if (subInConversation != null && hasActiveOrHoldingCall(sub) &&
2317 !sub.equals(subInConversation)) {
2318 // if sub is not conversation sub and if it has an active
2319 // voice call then update lchStatus as Active
2320 lchState = true;
2321 }
2322 // Update state only if the new state is different
2323 if ((phAcc != null) && (lchState != phAcc.isSet(PhoneAccount.LCH))) {
2324 Call call = getNonRingingLiveCall(sub);
2325 Log.d(this, " setLocal Call Hold to = " + lchState + " sub:" + sub);
2326
2327 if (call != null) {
2328 if (call.getChildCalls().size() > 1) {
2329 // Since for a conference call corresponding telephonyconnection doesn't
2330 // exist, so send lch request on its child call
2331 call = call.getChildCalls().get(0);
2332 }
2333 if (lchState) {
2334 call.setLocalCallHold(true);
2335 } else {
2336 removeFromLch = call;
2337 }
2338 }
2339 if (lchState) {
2340 phAcc.setBit(PhoneAccount.LCH);
2341 } else {
2342 phAcc.unSetBit(PhoneAccount.LCH);
2343 }
2344 }
2345 }
2346 if (removeFromLch != null) {
2347 // Ensure to send LCH disable request at last, to make sure that during switch
2348 // subscription, both subscriptions not to be in active(non-LCH) at any moment.
2349 removeFromLch.setLocalCallHold(false);
2350 }
2351 }
cqiang72e31202015-09-14 17:04:19 +08002352
2353 void resetCdmaConnectionTime(Call call) {
2354 call.setConnectTimeMillis(System.currentTimeMillis());
2355 if (mCalls.contains(call)) {
2356 for (CallsManagerListener listener : mListeners) {
2357 listener.onCallStateChanged(call, CallState.ACTIVE, CallState.ACTIVE);
2358 }
2359 updateForegroundCall();
2360 }
2361 }
Ravindra205cb782015-10-07 16:57:53 +05302362
2363 void onMergeFailed(Call call) {
2364 if (mCalls.contains(call)) {
2365 for (CallsManagerListener listener : mListeners) {
2366 listener.onMergeFailed(call);
2367 }
2368 }
2369 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08002370}