blob: cd2aa8532015cbd77a5304c05597b262908fe6a2 [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
Santos Cordonf0f99f32016-02-18 16:13:57 -080019import android.app.ActivityManager;
Tyler Gunneaaf0742016-09-15 15:36:38 -070020import android.app.NotificationManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070021import android.content.Context;
Tony Maka9930942016-01-15 10:57:14 +000022import android.content.pm.UserInfo;
Xueren Zhangc0e8e9d2015-03-06 14:16:55 +010023import android.content.Intent;
Hall Liufbed0162016-01-14 18:06:29 -080024import android.media.AudioManager;
Sailesh Nepalce704b92014-03-17 18:31:43 -070025import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080026import android.os.Bundle;
Santos Cordoncf5b2912014-11-05 21:57:54 -080027import android.os.Handler;
Ihab Awade6dbc9d2015-03-26 10:33:44 -070028import android.os.Looper;
Tony Maka9930942016-01-15 10:57:14 +000029import android.os.Process;
Sailesh Nepal703a1af2016-04-14 20:10:12 -070030import android.os.SystemClock;
Rekha Kumard240fe82015-04-01 21:45:57 -070031import android.os.SystemProperties;
Brad Ebingerd931a012015-10-21 12:54:08 -070032import android.os.SystemVibrator;
Yorke Leee4a9c412014-11-14 16:59:42 -080033import android.os.Trace;
Tony Mak578a4e62015-11-23 11:18:51 +000034import android.os.UserHandle;
Tony Maka9930942016-01-15 10:57:14 +000035import android.os.UserManager;
Santos Cordonf193ba42014-09-12 06:37:39 -070036import android.provider.CallLog.Calls;
Hall Liuf1422e72016-01-27 11:07:07 -080037import android.provider.Settings;
Yorke Lee2a66f7b2015-05-13 14:21:19 -070038import android.telecom.CallAudioState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080039import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080040import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070041import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070042import android.telecom.GatewayInfo;
43import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070044import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070045import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070046import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070047import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070048import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080049import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070050import android.telephony.TelephonyManager;
Yorke Lee8d3f2692015-05-08 11:44:39 -070051import android.text.TextUtils;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080052
Ihab Awada3653902015-01-23 13:44:46 -080053import com.android.internal.annotations.VisibleForTesting;
Abhijith Shastrycf27f682016-03-01 18:28:44 -080054import com.android.internal.telephony.AsyncEmergencyContactNotifier;
Rekha Kumard240fe82015-04-01 21:45:57 -070055import com.android.internal.telephony.PhoneConstants;
56import com.android.internal.telephony.TelephonyProperties;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070057import com.android.internal.util.IndentingPrintWriter;
Santos Cordon501b9b32016-03-07 14:40:07 -080058import com.android.server.telecom.TelecomServiceImpl.DefaultDialerManagerAdapter;
Hall Liu6d4b66d2016-04-01 16:31:13 -070059import com.android.server.telecom.callfiltering.AsyncBlockCheckFilter;
60import com.android.server.telecom.callfiltering.BlockCheckerAdapter;
61import com.android.server.telecom.callfiltering.CallFilterResultCallback;
62import com.android.server.telecom.callfiltering.CallFilteringResult;
63import com.android.server.telecom.callfiltering.CallScreeningServiceFilter;
64import com.android.server.telecom.callfiltering.DirectToVoicemailCallFilter;
65import com.android.server.telecom.callfiltering.IncomingCallFilter;
Xueren Zhangc0e8e9d2015-03-06 14:16:55 +010066import com.android.server.telecom.components.ErrorDialogActivity;
Yorke Leee4a9c412014-11-14 16:59:42 -080067
Hall Liu6d4b66d2016-04-01 16:31:13 -070068import java.util.ArrayList;
Santos Cordon4bc02452014-11-19 15:51:12 -080069import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070070import java.util.Collections;
Hall Liu32587202015-11-18 11:10:08 -080071import java.util.HashMap;
Ihab Awad5b281322014-09-25 18:25:29 -070072import java.util.HashSet;
Brad Ebingere680e662016-08-26 11:45:43 -070073import java.util.Iterator;
Santos Cordon7cdb9092014-07-24 21:33:33 -070074import java.util.List;
Hall Liu32587202015-11-18 11:10:08 -080075import java.util.Map;
Santos Cordonf193ba42014-09-12 06:37:39 -070076import java.util.Objects;
Tyler Gunn9b618b82016-10-17 15:54:35 -070077import java.util.Optional;
Santos Cordona56f2762014-03-24 15:55:53 -070078import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070079import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080080
Ben Giladdd8c6082013-12-30 14:44:08 -080081/**
82 * Singleton.
83 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070084 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080085 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070086 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080087 */
Ihab Awadaa383cc2015-03-22 22:12:28 -070088@VisibleForTesting
Sailesh Nepalae925952016-01-24 18:56:58 -080089public class CallsManager extends Call.ListenerBase
Hall Liu6d4b66d2016-04-01 16:31:13 -070090 implements VideoProviderProxy.Listener, CallFilterResultCallback {
Ben Gilada0d9f752014-02-26 11:49:03 -080091
Santos Cordondf399862014-08-06 04:39:15 -070092 // TODO: Consider renaming this CallsManagerPlugin.
Brad Ebinger53855132015-10-30 10:58:19 -070093 @VisibleForTesting
94 public interface CallsManagerListener {
Sailesh Nepal810735e2014-03-18 18:15:46 -070095 void onCallAdded(Call call);
96 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070097 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070098 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070099 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700100 ConnectionServiceWrapper oldService,
101 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700102 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700103 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700104 void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -0700105 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -0700106 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -0700107 void onIsVoipAudioModeChanged(Call call);
Hall Liu9696c212016-06-24 16:09:02 -0700108 void onVideoStateChanged(Call call, int previousVideoState, int newVideoState);
Santos Cordon91bd74c2014-11-07 16:10:22 -0800109 void onCanAddCallChanged(boolean canAddCall);
Tyler Gunn86014fc2015-06-12 14:31:25 -0700110 void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile);
Tyler Gunndb821912016-02-16 14:35:25 -0800111 void onHoldToneRequested(Call call);
Tyler Gunn1a40c4f2016-04-14 14:29:45 -0700112 void onExternalCallChanged(Call call, boolean isExternalCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700113 }
114
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700115 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -0800116
Santos Cordonf193ba42014-09-12 06:37:39 -0700117 private static final int MAXIMUM_LIVE_CALLS = 1;
118 private static final int MAXIMUM_HOLD_CALLS = 1;
119 private static final int MAXIMUM_RINGING_CALLS = 1;
Roshan Pius4de4a892015-08-24 11:06:58 -0700120 private static final int MAXIMUM_DIALING_CALLS = 1;
Santos Cordonf193ba42014-09-12 06:37:39 -0700121 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -0800122 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -0700123
124 private static final int[] OUTGOING_CALL_STATES =
Tyler Gunn1e37be52016-07-11 08:54:23 -0700125 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING,
126 CallState.PULLING};
Santos Cordonf193ba42014-09-12 06:37:39 -0700127
Santos Cordon91bd74c2014-11-07 16:10:22 -0800128 private static final int[] LIVE_CALL_STATES =
Santos Cordonf0f99f32016-02-18 16:13:57 -0800129 {CallState.CONNECTING, CallState.SELECT_PHONE_ACCOUNT, CallState.DIALING,
Tyler Gunn1e37be52016-07-11 08:54:23 -0700130 CallState.PULLING, CallState.ACTIVE};
131
Tyler Gunn8452be02015-09-17 09:57:02 -0700132 public static final String TELECOM_CALL_ID_PREFIX = "TC@";
Santos Cordon91bd74c2014-11-07 16:10:22 -0800133
Hall Liu32587202015-11-18 11:10:08 -0800134 // Maps call technologies in PhoneConstants to those in Analytics.
135 private static final Map<Integer, Integer> sAnalyticsTechnologyMap;
136 static {
137 sAnalyticsTechnologyMap = new HashMap<>(5);
138 sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_CDMA, Analytics.CDMA_PHONE);
139 sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_GSM, Analytics.GSM_PHONE);
140 sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_IMS, Analytics.IMS_PHONE);
141 sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_SIP, Analytics.SIP_PHONE);
142 sAnalyticsTechnologyMap.put(PhoneConstants.PHONE_TYPE_THIRD_PARTY,
143 Analytics.THIRD_PARTY_PHONE);
144 }
145
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800146 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700147 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
148 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800149 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700150 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
151 * load factor before resizing, 1 means we only expect a single thread to
152 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800153 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700154 private final Set<Call> mCalls = Collections.newSetFromMap(
155 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800156
Tyler Gunn8452be02015-09-17 09:57:02 -0700157 /**
158 * The current telecom call ID. Used when creating new instances of {@link Call}. Should
159 * only be accessed using the {@link #getNextCallId()} method which synchronizes on the
160 * {@link #mLock} sync root.
161 */
162 private int mCallId = 0;
163
Santos Cordonf0f99f32016-02-18 16:13:57 -0800164 /**
165 * Stores the current foreground user.
166 */
167 private UserHandle mCurrentUserHandle = UserHandle.of(ActivityManager.getCurrentUser());
168
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700169 private final ConnectionServiceRepository mConnectionServiceRepository;
170 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
171 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700172 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800173 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700174 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800175 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700176 // For this set initial table size to 16 because we add 13 listeners in
177 // the CallsManager constructor.
178 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
179 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700180 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700181 private final WiredHeadsetManager mWiredHeadsetManager;
Hall Liuf62630a2015-10-27 14:53:39 -0700182 private final BluetoothManager mBluetoothManager;
Santos Cordona04bdca2015-03-04 16:57:55 -0800183 private final DockManager mDockManager;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700184 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700185 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700186 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700187 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700188 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700189 private final TelecomSystem.SyncRoot mLock;
190 private final ContactsAsyncHelper mContactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700191 private final CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700192 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
193 private final MissedCallNotifier mMissedCallNotifier;
Hall Liu5b70c1c2016-03-03 18:42:57 -0800194 private final CallerInfoLookupHelper mCallerInfoLookupHelper;
Hall Liu6d4b66d2016-04-01 16:31:13 -0700195 private final DefaultDialerManagerAdapter mDefaultDialerManagerAdapter;
196 private final Timeouts.Adapter mTimeoutsAdapter;
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700197 private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
Tyler Gunneaaf0742016-09-15 15:36:38 -0700198 private final NotificationManager mNotificationManager;
Ihab Awad5b281322014-09-25 18:25:29 -0700199 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800200 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
201 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700202 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700203
Santos Cordon91bd74c2014-11-07 16:10:22 -0800204 private boolean mCanAddCall = true;
205
Brad Ebinger3da57642016-04-04 17:32:19 -0700206 private TelephonyManager.MultiSimVariants mRadioSimVariants = null;
207
Nancy Chena469f762014-12-09 18:29:20 -0800208 private Runnable mStopTone;
209
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700210 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700211 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800212 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700213 CallsManager(
214 Context context,
215 TelecomSystem.SyncRoot lock,
216 ContactsAsyncHelper contactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700217 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700218 MissedCallNotifier missedCallNotifier,
219 PhoneAccountRegistrar phoneAccountRegistrar,
220 HeadsetMediaButtonFactory headsetMediaButtonFactory,
221 ProximitySensorManagerFactory proximitySensorManagerFactory,
Hall Liu8fb1fb72015-10-22 15:24:40 -0700222 InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
Hall Liuf62630a2015-10-27 14:53:39 -0700223 CallAudioManager.AudioServiceFactory audioServiceFactory,
224 BluetoothManager bluetoothManager,
Santos Cordonf78a72f2016-01-21 22:10:32 +0000225 WiredHeadsetManager wiredHeadsetManager,
Santos Cordon501b9b32016-03-07 14:40:07 -0800226 SystemStateProvider systemStateProvider,
Brad Ebinger7ade5e22016-04-05 18:43:50 -0700227 DefaultDialerManagerAdapter defaultDialerAdapter,
Hall Liu6d4b66d2016-04-01 16:31:13 -0700228 Timeouts.Adapter timeoutsAdapter,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700229 AsyncRingtonePlayer asyncRingtonePlayer,
Tyler Gunneaaf0742016-09-15 15:36:38 -0700230 PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
231 InterruptionFilterProxy interruptionFilterProxy) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700232 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700233 mLock = lock;
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700234 mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700235 mContactsAsyncHelper = contactsAsyncHelper;
Ihab Awadabcbce42015-04-07 14:04:01 -0700236 mCallerInfoAsyncQueryFactory = callerInfoAsyncQueryFactory;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700237 mPhoneAccountRegistrar = phoneAccountRegistrar;
238 mMissedCallNotifier = missedCallNotifier;
239 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
Hall Liuf62630a2015-10-27 14:53:39 -0700240 mWiredHeadsetManager = wiredHeadsetManager;
241 mBluetoothManager = bluetoothManager;
Hall Liu6d4b66d2016-04-01 16:31:13 -0700242 mDefaultDialerManagerAdapter = defaultDialerAdapter;
Santos Cordona04bdca2015-03-04 16:57:55 -0800243 mDockManager = new DockManager(context);
Hall Liu6d4b66d2016-04-01 16:31:13 -0700244 mTimeoutsAdapter = timeoutsAdapter;
Hall Liu5b70c1c2016-03-03 18:42:57 -0800245 mCallerInfoLookupHelper = new CallerInfoLookupHelper(context, mCallerInfoAsyncQueryFactory,
Hall Liu46598272016-04-05 12:58:51 -0700246 mContactsAsyncHelper, mLock);
Hall Liue091ab92015-12-18 17:05:30 -0800247
248 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
Tyler Gunneaaf0742016-09-15 15:36:38 -0700249 mNotificationManager = (NotificationManager) context.getSystemService(
250 Context.NOTIFICATION_SERVICE);
Hall Liuf62630a2015-10-27 14:53:39 -0700251 CallAudioRouteStateMachine callAudioRouteStateMachine = new CallAudioRouteStateMachine(
252 context,
253 this,
254 bluetoothManager,
255 wiredHeadsetManager,
256 statusBarNotifier,
Hall Liua3e9dda2015-12-15 17:52:50 -0800257 audioServiceFactory,
Tyler Gunneaaf0742016-09-15 15:36:38 -0700258 interruptionFilterProxy,
Hall Liua3e9dda2015-12-15 17:52:50 -0800259 CallAudioRouteStateMachine.doesDeviceSupportEarpieceRoute()
Hall Liuf62630a2015-10-27 14:53:39 -0700260 );
Hall Liu29855702015-12-11 17:42:03 -0800261 callAudioRouteStateMachine.initialize();
262
Hall Liuf62630a2015-10-27 14:53:39 -0700263 CallAudioRoutePeripheralAdapter callAudioRoutePeripheralAdapter =
264 new CallAudioRoutePeripheralAdapter(
265 callAudioRouteStateMachine,
266 bluetoothManager,
267 wiredHeadsetManager,
268 mDockManager);
269
Hall Liue091ab92015-12-18 17:05:30 -0800270 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(
Hall Liuf62630a2015-10-27 14:53:39 -0700271 callAudioRoutePeripheralAdapter, lock);
272
Hall Liue091ab92015-12-18 17:05:30 -0800273 SystemSettingsUtil systemSettingsUtil = new SystemSettingsUtil();
Brad Ebingerc9286f42016-03-10 16:02:54 -0800274 RingtoneFactory ringtoneFactory = new RingtoneFactory(this, context);
Brad Ebingerd931a012015-10-21 12:54:08 -0700275 SystemVibrator systemVibrator = new SystemVibrator(context);
Santos Cordon501b9b32016-03-07 14:40:07 -0800276 mInCallController = new InCallController(
Hall Liu28b82f02016-07-26 17:38:56 -0700277 context, mLock, this, systemStateProvider, defaultDialerAdapter, mTimeoutsAdapter);
Hall Liue091ab92015-12-18 17:05:30 -0800278 mRinger = new Ringer(playerFactory, context, systemSettingsUtil, asyncRingtonePlayer,
Santos Cordonf0f99f32016-02-18 16:13:57 -0800279 ringtoneFactory, systemVibrator, mInCallController);
Hall Liue091ab92015-12-18 17:05:30 -0800280
281 mCallAudioManager = new CallAudioManager(callAudioRouteStateMachine,
Hall Liufbed0162016-01-14 18:06:29 -0800282 this,new CallAudioModeStateMachine((AudioManager)
283 mContext.getSystemService(Context.AUDIO_SERVICE)),
284 playerFactory, mRinger, new RingbackPlayer(playerFactory), mDtmfLocalTonePlayer);
Hall Liue091ab92015-12-18 17:05:30 -0800285
Hall Liuf62630a2015-10-27 14:53:39 -0700286 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this, mLock);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700287 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800288 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
289 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Ta-wei Yen982c0bd2016-04-14 13:59:54 -0700290 mCallLogManager = new CallLogManager(context, phoneAccountRegistrar, mMissedCallNotifier);
Ihab Awad8de76912015-02-17 12:25:52 -0800291 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700292 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800293 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700294
Tyler Gunnae694b12015-11-11 17:46:03 -0800295 mListeners.add(mInCallWakeLockController);
Santos Cordondeb8c892014-05-30 01:38:03 -0700296 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700297 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700298 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700299 mListeners.add(mInCallController);
Santos Cordona56f2762014-03-24 15:55:53 -0700300 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700301 mListeners.add(missedCallNotifier);
Santos Cordon81289982014-06-03 16:03:08 -0700302 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700303 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700304
Tony Maka9930942016-01-15 10:57:14 +0000305 // There is no USER_SWITCHED broadcast for user 0, handle it here explicitly.
306 final UserManager userManager = UserManager.get(mContext);
307 // Don't load missed call if it is run in split user model.
308 if (userManager.isPrimaryUser()) {
309 onUserSwitch(Process.myUserHandle());
310 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800311 }
312
Ihab Awad8de76912015-02-17 12:25:52 -0800313 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
314 if (mRespondViaSmsManager != null) {
315 mListeners.remove(mRespondViaSmsManager);
316 }
317 mRespondViaSmsManager = respondViaSmsManager;
318 mListeners.add(respondViaSmsManager);
319 }
320
321 public RespondViaSmsManager getRespondViaSmsManager() {
322 return mRespondViaSmsManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800323 }
324
Hall Liu5b70c1c2016-03-03 18:42:57 -0800325 public CallerInfoLookupHelper getCallerInfoLookupHelper() {
326 return mCallerInfoLookupHelper;
327 }
328
Santos Cordon766d04f2014-05-06 10:28:25 -0700329 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700330 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700331 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700332
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700333 setCallState(call, callState, "successful outgoing call");
Yorke Leeb701f6d2014-08-12 14:04:19 -0700334 if (!mCalls.contains(call)) {
335 // Call was not added previously in startOutgoingCall due to it being a potential MMI
336 // code, so add it now.
337 addCall(call);
338 }
339
340 // The call's ConnectionService has been updated.
341 for (CallsManagerListener listener : mListeners) {
342 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700343 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700344
345 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700346 }
347
348 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700349 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700350 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700351
Andrew Leee6288a72014-10-01 13:50:02 -0700352 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700353 }
354
355 @Override
Hall Liu6d4b66d2016-04-01 16:31:13 -0700356 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700357 Log.d(this, "onSuccessfulIncomingCall");
Hall Liuddf3f9c2016-08-30 13:38:52 -0700358 if (incomingCall.hasProperty(Connection.PROPERTY_EMERGENCY_CALLBACK_MODE)) {
359 Log.i(this, "Skipping call filtering due to ECBM");
360 onCallFilteringComplete(incomingCall, new CallFilteringResult(true, false, true, true));
361 return;
362 }
363
Hall Liu6d4b66d2016-04-01 16:31:13 -0700364 List<IncomingCallFilter.CallFilter> filters = new ArrayList<>();
365 filters.add(new DirectToVoicemailCallFilter(mCallerInfoLookupHelper));
366 filters.add(new AsyncBlockCheckFilter(mContext, new BlockCheckerAdapter()));
367 filters.add(new CallScreeningServiceFilter(mContext, this, mPhoneAccountRegistrar,
368 mDefaultDialerManagerAdapter,
369 new ParcelableCallUtils.Converter(), mLock));
370 new IncomingCallFilter(mContext, this, incomingCall, mLock,
371 mTimeoutsAdapter, filters).performFiltering();
Sailesh Nepalae925952016-01-24 18:56:58 -0800372 }
373
374 @Override
Hall Liu6d4b66d2016-04-01 16:31:13 -0700375 public void onCallFilteringComplete(Call incomingCall, CallFilteringResult result) {
Hall Liue091ab92015-12-18 17:05:30 -0800376 // Only set the incoming call as ringing if it isn't already disconnected. It is possible
Tyler Gunne6276e12015-10-22 20:48:50 -0700377 // that the connection service disconnected the call before it was even added to Telecom, in
378 // which case it makes no sense to set it back to a ringing state.
379 if (incomingCall.getState() != CallState.DISCONNECTED &&
380 incomingCall.getState() != CallState.DISCONNECTING) {
381 setCallState(incomingCall, CallState.RINGING,
Hall Liu6d4b66d2016-04-01 16:31:13 -0700382 result.shouldAllowCall ? "successful incoming call" : "blocking call");
Tyler Gunne6276e12015-10-22 20:48:50 -0700383 } else {
Hall Liu6d4b66d2016-04-01 16:31:13 -0700384 Log.i(this, "onCallFilteringCompleted: call already disconnected.");
Tyler Gunn2ecfd882016-09-16 15:32:11 -0700385 return;
Tyler Gunne6276e12015-10-22 20:48:50 -0700386 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700387
Hall Liu6d4b66d2016-04-01 16:31:13 -0700388 if (result.shouldAllowCall) {
Sailesh Nepalae925952016-01-24 18:56:58 -0800389 if (hasMaximumRingingCalls()) {
Wei Liu13791b92016-10-04 17:46:44 -0700390 if (shouldSilenceInsteadOfReject(incomingCall)) {
391 incomingCall.silence();
392 } else {
393 Log.i(this, "onCallFilteringCompleted: Call rejected! " +
394 "Exceeds maximum number of ringing calls.");
395 rejectCallAndLog(incomingCall);
396 }
Sailesh Nepalae925952016-01-24 18:56:58 -0800397 } else if (hasMaximumDialingCalls()) {
Hall Liu6d4b66d2016-04-01 16:31:13 -0700398 Log.i(this, "onCallFilteringCompleted: Call rejected! Exceeds maximum number of " +
Sailesh Nepalae925952016-01-24 18:56:58 -0800399 "dialing calls.");
400 rejectCallAndLog(incomingCall);
401 } else {
402 addCall(incomingCall);
403 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700404 } else {
Hall Liu6d4b66d2016-04-01 16:31:13 -0700405 if (result.shouldReject) {
406 Log.i(this, "onCallFilteringCompleted: blocked call, rejecting.");
Sailesh Nepalae925952016-01-24 18:56:58 -0800407 incomingCall.reject(false, null);
408 }
Hall Liu6d4b66d2016-04-01 16:31:13 -0700409 if (result.shouldAddToCallLog) {
Sailesh Nepalae925952016-01-24 18:56:58 -0800410 Log.i(this, "onCallScreeningCompleted: blocked call, adding to call log.");
Hall Liu6d4b66d2016-04-01 16:31:13 -0700411 if (result.shouldShowNotification) {
Ta-wei Yen982c0bd2016-04-14 13:59:54 -0700412 Log.w(this, "onCallScreeningCompleted: blocked call, showing notification.");
413 }
Hall Liu6d4b66d2016-04-01 16:31:13 -0700414 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE,
415 result.shouldShowNotification);
416 } else if (result.shouldShowNotification) {
Sailesh Nepalae925952016-01-24 18:56:58 -0800417 Log.i(this, "onCallScreeningCompleted: blocked call, showing notification.");
418 mMissedCallNotifier.showMissedCallNotification(incomingCall);
419 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700420 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700421 }
422
Wei Liu13791b92016-10-04 17:46:44 -0700423 /**
424 * Whether allow (silence rather than reject) the incoming call if it has a different source
425 * (connection service) from the existing ringing call when reaching maximum ringing calls.
426 */
427 private boolean shouldSilenceInsteadOfReject(Call incomingCall) {
428 if (!mContext.getResources().getBoolean(
429 R.bool.silence_incoming_when_different_service_and_maximum_ringing)) {
430 return false;
431 }
432
433 Call ringingCall = null;
434
435 for (Call call : mCalls) {
436 // Only operate on top-level calls
437 if (call.getParentCall() != null) {
438 continue;
439 }
440
441 if (call.isExternalCall()) {
442 continue;
443 }
444
445 if (CallState.RINGING == call.getState() &&
446 call.getConnectionService() == incomingCall.getConnectionService()) {
447 return false;
448 }
449 }
450
451 return true;
452 }
453
Santos Cordon766d04f2014-05-06 10:28:25 -0700454 @Override
455 public void onFailedIncomingCall(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700456 setCallState(call, CallState.DISCONNECTED, "failed incoming call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700457 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800458 }
459
Ihab Awadcb387ac2014-05-28 16:49:38 -0700460 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700461 public void onSuccessfulUnknownCall(Call call, int callState) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700462 setCallState(call, callState, "successful unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700463 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
464 addCall(call);
465 }
466
467 @Override
468 public void onFailedUnknownCall(Call call) {
469 Log.i(this, "onFailedUnknownCall for call %s", call);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700470 setCallState(call, CallState.DISCONNECTED, "failed unknown call");
Yorke Lee9250e5f2014-10-01 13:39:09 -0700471 call.removeListener(this);
472 }
473
474 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700475 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700476 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700477 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700478 }
479 }
480
Evan Charlton352105c2014-06-03 14:10:54 -0700481 @Override
482 public void onPostDialWait(Call call, String remaining) {
483 mInCallController.onPostDialWait(call, remaining);
484 }
485
Santos Cordona1610702014-06-04 20:22:56 -0700486 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800487 public void onPostDialChar(final Call call, char nextChar) {
488 if (PhoneNumberUtils.is12Key(nextChar)) {
489 // Play tone if it is one of the dialpad digits, canceling out the previously queued
490 // up stopTone runnable since playing a new tone automatically stops the previous tone.
491 if (mStopTone != null) {
Brad Ebingere62e9e82016-02-01 18:26:40 -0800492 mHandler.removeCallbacks(mStopTone.getRunnableToCancel());
493 mStopTone.cancel();
Nancy Chena469f762014-12-09 18:29:20 -0800494 }
495
496 mDtmfLocalTonePlayer.playTone(call, nextChar);
497
Brad Ebingerf5e06662016-08-25 16:16:27 -0700498 mStopTone = new Runnable("CM.oPDC", mLock) {
Nancy Chena469f762014-12-09 18:29:20 -0800499 @Override
Brad Ebingere62e9e82016-02-01 18:26:40 -0800500 public void loggedRun() {
Brad Ebingerf5e06662016-08-25 16:16:27 -0700501 // Set a timeout to stop the tone in case there isn't another tone to
502 // follow.
503 mDtmfLocalTonePlayer.stopTone(call);
Nancy Chena469f762014-12-09 18:29:20 -0800504 }
505 };
Brad Ebingere62e9e82016-02-01 18:26:40 -0800506 mHandler.postDelayed(mStopTone.prepare(),
Nancy Chena469f762014-12-09 18:29:20 -0800507 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
508 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
509 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
510 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
511 // the previous tone is being stopped anyway.
512 if (mStopTone != null) {
Brad Ebingere62e9e82016-02-01 18:26:40 -0800513 mHandler.removeCallbacks(mStopTone.getRunnableToCancel());
514 mStopTone.cancel();
Nancy Chena469f762014-12-09 18:29:20 -0800515 }
516 mDtmfLocalTonePlayer.stopTone(call);
517 } else {
518 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
519 }
520 }
521
522 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700523 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700524 // parent-child relationship affects which call should be foreground, so do an update.
Hall Liufc130b22016-06-15 17:54:48 -0700525 updateCanAddCall();
Santos Cordona1610702014-06-04 20:22:56 -0700526 for (CallsManagerListener listener : mListeners) {
527 listener.onIsConferencedChanged(call);
528 }
529 }
530
531 @Override
532 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700533 // parent-child relationship affects which call should be foreground, so do an update.
Hall Liufc130b22016-06-15 17:54:48 -0700534 updateCanAddCall();
Santos Cordona1610702014-06-04 20:22:56 -0700535 for (CallsManagerListener listener : mListeners) {
536 listener.onIsConferencedChanged(call);
537 }
538 }
539
Ihab Awadff7493a2014-06-10 13:47:44 -0700540 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700541 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700542 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700543 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700544 }
545 }
546
Andrew Lee4a796602014-07-11 17:23:03 -0700547 @Override
Hall Liu9696c212016-06-24 16:09:02 -0700548 public void onVideoStateChanged(Call call, int previousVideoState, int newVideoState) {
Andrew Lee4a796602014-07-11 17:23:03 -0700549 for (CallsManagerListener listener : mListeners) {
Hall Liu9696c212016-06-24 16:09:02 -0700550 listener.onVideoStateChanged(call, previousVideoState, newVideoState);
Andrew Lee4a796602014-07-11 17:23:03 -0700551 }
552 }
553
Santos Cordoncf5b2912014-11-05 21:57:54 -0800554 @Override
Hall Liuad18dc92017-02-14 18:05:03 -0800555 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call,
556 long disconnectionTimeout) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800557 mPendingCallsToDisconnect.add(call);
Brad Ebingerf5e06662016-08-25 16:16:27 -0700558 mHandler.postDelayed(new Runnable("CM.oCVNOCB", mLock) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800559 @Override
Brad Ebingere62e9e82016-02-01 18:26:40 -0800560 public void loggedRun() {
Brad Ebingerf5e06662016-08-25 16:16:27 -0700561 if (mPendingCallsToDisconnect.remove(call)) {
562 Log.i(this, "Delayed disconnection of call: %s", call);
563 call.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800564 }
565 }
Hall Liuad18dc92017-02-14 18:05:03 -0800566 }.prepare(), disconnectionTimeout);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800567
568 return true;
569 }
570
Tyler Gunn86014fc2015-06-12 14:31:25 -0700571 /**
572 * Handles changes to the {@link Connection.VideoProvider} for a call. Adds the
573 * {@link CallsManager} as a listener for the {@link VideoProviderProxy} which is created
574 * in {@link Call#setVideoProvider(IVideoProvider)}. This allows the {@link CallsManager} to
575 * respond to callbacks from the {@link VideoProviderProxy}.
576 *
577 * @param call The call.
578 */
579 @Override
580 public void onVideoCallProviderChanged(Call call) {
581 VideoProviderProxy videoProviderProxy = call.getVideoProviderProxy();
582
583 if (videoProviderProxy == null) {
584 return;
585 }
586
587 videoProviderProxy.addListener(this);
588 }
589
590 /**
591 * Handles session modification requests received via the {@link TelecomVideoCallCallback} for
592 * a call. Notifies listeners of the {@link CallsManager.CallsManagerListener} of the session
593 * modification request.
594 *
595 * @param call The call.
596 * @param videoProfile The {@link VideoProfile}.
597 */
598 @Override
599 public void onSessionModifyRequestReceived(Call call, VideoProfile videoProfile) {
600 int videoState = videoProfile != null ? videoProfile.getVideoState() :
601 VideoProfile.STATE_AUDIO_ONLY;
602 Log.v(TAG, "onSessionModifyRequestReceived : videoProfile = " + VideoProfile
603 .videoStateToString(videoState));
604
605 for (CallsManagerListener listener : mListeners) {
606 listener.onSessionModifyRequestReceived(call, videoProfile);
607 }
608 }
609
Brad Ebinger53855132015-10-30 10:58:19 -0700610 public Collection<Call> getCalls() {
Santos Cordon4bc02452014-11-19 15:51:12 -0800611 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700612 }
613
Tyler Gunndb821912016-02-16 14:35:25 -0800614 /**
615 * Play or stop a call hold tone for a call. Triggered via
616 * {@link Connection#sendConnectionEvent(String)} when the
617 * {@link Connection#EVENT_ON_HOLD_TONE_START} event or
618 * {@link Connection#EVENT_ON_HOLD_TONE_STOP} event is passed through to the
619 *
620 * @param call The call which requested the hold tone.
621 */
622 @Override
623 public void onHoldToneRequested(Call call) {
624 for (CallsManagerListener listener : mListeners) {
625 listener.onHoldToneRequested(call);
626 }
627 }
628
Brad Ebingerd931a012015-10-21 12:54:08 -0700629 @VisibleForTesting
630 public Call getForegroundCall() {
Hall Liue091ab92015-12-18 17:05:30 -0800631 if (mCallAudioManager == null) {
632 // Happens when getForegroundCall is called before full initialization.
633 return null;
634 }
635 return mCallAudioManager.getForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700636 }
637
Santos Cordonf0f99f32016-02-18 16:13:57 -0800638 public UserHandle getCurrentUserHandle() {
639 return mCurrentUserHandle;
640 }
641
Hall Liu9ecbb1c2016-04-14 14:35:48 -0700642 public CallAudioManager getCallAudioManager() {
Hall Liue091ab92015-12-18 17:05:30 -0800643 return mCallAudioManager;
Santos Cordonae193062014-05-21 21:21:49 -0700644 }
645
Santos Cordonf3671a62014-05-29 21:51:53 -0700646 InCallController getInCallController() {
647 return mInCallController;
648 }
649
Santos Cordonf0f99f32016-02-18 16:13:57 -0800650 @VisibleForTesting
651 public boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700652 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700653 if (call.isEmergencyCall()) {
654 return true;
655 }
656 }
657 return false;
658 }
659
Roshan Pius669f90b2015-09-02 16:21:39 -0700660 boolean hasOnlyDisconnectedCalls() {
661 for (Call call : mCalls) {
662 if (!call.isDisconnected()) {
663 return false;
664 }
665 }
666 return true;
667 }
668
Andrew Lee45506232014-10-22 17:54:33 -0700669 boolean hasVideoCall() {
670 for (Call call : mCalls) {
Tyler Gunn467b64f2015-06-09 13:48:47 -0700671 if (VideoProfile.isVideo(call.getVideoState())) {
Andrew Lee45506232014-10-22 17:54:33 -0700672 return true;
673 }
674 }
675 return false;
676 }
677
Hall Liu28b82f02016-07-26 17:38:56 -0700678 @VisibleForTesting
679 public CallAudioState getAudioState() {
Yorke Lee2a66f7b2015-05-13 14:21:19 -0700680 return mCallAudioManager.getCallAudioState();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700681 }
682
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700683 boolean isTtySupported() {
684 return mTtyManager.isTtySupported();
685 }
686
687 int getCurrentTtyMode() {
688 return mTtyManager.getCurrentTtyMode();
689 }
690
Brad Ebinger53855132015-10-30 10:58:19 -0700691 @VisibleForTesting
692 public void addListener(CallsManagerListener listener) {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700693 mListeners.add(listener);
694 }
695
696 void removeListener(CallsManagerListener listener) {
697 mListeners.remove(listener);
698 }
699
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800700 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700701 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800702 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700703 * @param phoneAccountHandle The phone account which contains the component name of the
704 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800705 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800706 */
Evan Charlton89176372014-07-19 18:23:09 -0700707 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800708 Log.d(this, "processIncomingCallIntent");
Yorke Lee69fa8972015-06-08 11:25:32 -0700709 Uri handle = extras.getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
710 if (handle == null) {
711 // Required for backwards compatibility
712 handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
713 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700714 Call call = new Call(
Tyler Gunn8452be02015-09-17 09:57:02 -0700715 getNextCallId(),
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700716 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800717 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700718 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700719 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700720 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700721 mCallerInfoAsyncQueryFactory,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700722 mPhoneNumberUtilsAdapter,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700723 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700724 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700725 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700726 phoneAccountHandle,
Hall Liu32587202015-11-18 11:10:08 -0800727 Call.CALL_DIRECTION_INCOMING /* callDirection */,
728 false /* forceAttachToExistingConnection */,
Tony Mak578a4e62015-11-23 11:18:51 +0000729 false /* isConference */
730 );
Hall Liu32587202015-11-18 11:10:08 -0800731
732 call.initAnalytics();
Hall Liue091ab92015-12-18 17:05:30 -0800733 if (getForegroundCall() != null) {
734 getForegroundCall().getAnalytics().setCallIsInterrupted(true);
Hall Liu32587202015-11-18 11:10:08 -0800735 call.getAnalytics().setCallIsAdditional(true);
736 }
737
Sailesh Nepal703a1af2016-04-14 20:10:12 -0700738 setIntentExtrasAndStartTime(call, extras);
Santos Cordondf399862014-08-06 04:39:15 -0700739 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700740 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700741 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800742 }
743
Yorke Lee9250e5f2014-10-01 13:39:09 -0700744 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
745 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
746 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
747 Call call = new Call(
Tyler Gunn8452be02015-09-17 09:57:02 -0700748 getNextCallId(),
Yorke Lee9250e5f2014-10-01 13:39:09 -0700749 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800750 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -0700751 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700752 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700753 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700754 mCallerInfoAsyncQueryFactory,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700755 mPhoneNumberUtilsAdapter,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700756 handle,
757 null /* gatewayInfo */,
758 null /* connectionManagerPhoneAccount */,
759 phoneAccountHandle,
Hall Liu32587202015-11-18 11:10:08 -0800760 Call.CALL_DIRECTION_UNKNOWN /* callDirection */,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700761 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
762 // to the existing connection instead of trying to create a new one.
Hall Liu32587202015-11-18 11:10:08 -0800763 true /* forceAttachToExistingConnection */,
Tony Mak578a4e62015-11-23 11:18:51 +0000764 false /* isConference */
765 );
Hall Liu32587202015-11-18 11:10:08 -0800766 call.initAnalytics();
767
Sailesh Nepal703a1af2016-04-14 20:10:12 -0700768 setIntentExtrasAndStartTime(call, extras);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700769 call.addListener(this);
770 call.startCreateConnection(mPhoneAccountRegistrar);
771 }
772
Yorke Lee8d3f2692015-05-08 11:44:39 -0700773 private boolean areHandlesEqual(Uri handle1, Uri handle2) {
774 if (handle1 == null || handle2 == null) {
775 return handle1 == handle2;
776 }
777
778 if (!TextUtils.equals(handle1.getScheme(), handle2.getScheme())) {
779 return false;
780 }
781
782 final String number1 = PhoneNumberUtils.normalizeNumber(handle1.getSchemeSpecificPart());
783 final String number2 = PhoneNumberUtils.normalizeNumber(handle2.getSchemeSpecificPart());
784 return TextUtils.equals(number1, number2);
785 }
786
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800787 private Call reuseOutgoingCall(Uri handle) {
788 // Check to see if we can reuse any of the calls that are waiting to disconnect.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800789 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800790 Call reusedCall = null;
Brad Ebingere680e662016-08-26 11:45:43 -0700791 for (Iterator<Call> callIter = mPendingCallsToDisconnect.iterator(); callIter.hasNext();) {
792 Call pendingCall = callIter.next();
Yorke Lee8d3f2692015-05-08 11:44:39 -0700793 if (reusedCall == null && areHandlesEqual(pendingCall.getHandle(), handle)) {
Brad Ebingere680e662016-08-26 11:45:43 -0700794 callIter.remove();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800795 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800796 reusedCall = pendingCall;
797 } else {
Yorke Lee8d3f2692015-05-08 11:44:39 -0700798 Log.i(this, "Not reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800799 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800800 }
801 }
802
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800803 return reusedCall;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800804 }
805
Nancy Chen0d3076c2014-07-30 14:45:44 -0700806 /**
807 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
808 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700809 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700810 * @param phoneAccountHandle The phone account which contains the component name of the
811 * connection service to use for this call.
812 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Tony Mak578a4e62015-11-23 11:18:51 +0000813 * @param initiatingUser {@link UserHandle} of user that place the outgoing call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700814 */
Tony Mak578a4e62015-11-23 11:18:51 +0000815 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras,
816 UserHandle initiatingUser) {
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800817 boolean isReusedCall = true;
818 Call call = reuseOutgoingCall(handle);
819
820 // Create a call with original handle. The handle may be changed when the call is attached
821 // to a connection service, but in most cases will remain the same.
822 if (call == null) {
823 call = new Call(getNextCallId(), mContext,
824 this,
825 mLock,
826 mConnectionServiceRepository,
827 mContactsAsyncHelper,
828 mCallerInfoAsyncQueryFactory,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700829 mPhoneNumberUtilsAdapter,
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800830 handle,
831 null /* gatewayInfo */,
832 null /* connectionManagerPhoneAccount */,
833 null /* phoneAccountHandle */,
Hall Liu32587202015-11-18 11:10:08 -0800834 Call.CALL_DIRECTION_OUTGOING /* callDirection */,
835 false /* forceAttachToExistingConnection */,
Tony Mak578a4e62015-11-23 11:18:51 +0000836 false /* isConference */
837 );
Hall Liu32587202015-11-18 11:10:08 -0800838 call.initAnalytics();
839
Hall Liu874c0f82016-04-29 18:13:18 -0700840 call.setInitiatingUser(initiatingUser);
841
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800842 isReusedCall = false;
843 }
Nancy Chen1c5926f2014-09-17 14:44:14 -0700844
Tyler Gunn228f95e2016-03-03 16:24:45 -0800845 // Set the video state on the call early so that when it is added to the InCall UI the UI
846 // knows to configure itself as a video call immediately.
847 if (extras != null) {
Tyler Gunnf8156382016-03-30 08:35:29 -0700848 int videoState = extras.getInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
849 VideoProfile.STATE_AUDIO_ONLY);
850
851 // If this is an emergency video call, we need to check if the phone account supports
852 // emergency video calling.
Tyler Gunn10274f72016-07-26 15:38:16 -0700853 // Also, ensure we don't try to place an outgoing call with video if video is not
854 // supported.
855 if (VideoProfile.isVideo(videoState)) {
Tyler Gunnf8156382016-03-30 08:35:29 -0700856 PhoneAccount account =
857 mPhoneAccountRegistrar.getPhoneAccount(phoneAccountHandle, initiatingUser);
858
Tyler Gunn10274f72016-07-26 15:38:16 -0700859 if (call.isEmergencyCall() && account != null &&
Tyler Gunnf8156382016-03-30 08:35:29 -0700860 !account.hasCapabilities(PhoneAccount.CAPABILITY_EMERGENCY_VIDEO_CALLING)) {
861 // Phone account doesn't support emergency video calling, so fallback to
862 // audio-only now to prevent the InCall UI from setting up video surfaces
863 // needlessly.
864 Log.i(this, "startOutgoingCall - emergency video calls not supported; " +
865 "falling back to audio-only");
866 videoState = VideoProfile.STATE_AUDIO_ONLY;
Tyler Gunn10274f72016-07-26 15:38:16 -0700867 } else if (account != null &&
868 !account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
869 // Phone account doesn't support video calling, so fallback to audio-only.
870 Log.i(this, "startOutgoingCall - video calls not supported; fallback to " +
871 "audio-only.");
872 videoState = VideoProfile.STATE_AUDIO_ONLY;
Tyler Gunnf8156382016-03-30 08:35:29 -0700873 }
874 }
875
876 call.setVideoState(videoState);
Omkar Kolangade005612a2015-08-25 11:08:18 -0700877 }
878
Brad Ebinger3da57642016-04-04 17:32:19 -0700879 List<PhoneAccountHandle> accounts = constructPossiblePhoneAccounts(handle, initiatingUser);
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800880 Log.v(this, "startOutgoingCall found accounts = " + accounts);
881
Nancy Chenbc9ef172014-08-15 17:11:57 -0700882 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
883 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700884 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700885 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700886 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700887 phoneAccountHandle = null;
888 }
889 }
890
Brad Ebingere6c481a2016-05-12 14:13:26 -0700891 if (phoneAccountHandle == null && accounts.size() > 0) {
Tyler Gunn84253572014-09-02 14:50:05 -0700892 // No preset account, check if default exists that supports the URI scheme for the
Brad Ebinger3da57642016-04-04 17:32:19 -0700893 // handle and verify it can be used.
894 if(accounts.size() > 1) {
895 PhoneAccountHandle defaultPhoneAccountHandle =
896 mPhoneAccountRegistrar.getOutgoingPhoneAccountForScheme(handle.getScheme(),
897 initiatingUser);
898 if (defaultPhoneAccountHandle != null &&
899 accounts.contains(defaultPhoneAccountHandle)) {
900 phoneAccountHandle = defaultPhoneAccountHandle;
901 }
902 } else {
903 // Use the only PhoneAccount that is available
904 phoneAccountHandle = accounts.get(0);
905 }
906
Nancy Chenbc9ef172014-08-15 17:11:57 -0700907 }
908
Nancy Chen1c5926f2014-09-17 14:44:14 -0700909 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700910
Tyler Gunn5b452df2014-10-01 15:02:58 -0700911 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700912
913 // Do not support any more live calls. Our options are to move a call to hold, disconnect
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800914 // a call, or cancel this call altogether. If a call is being reused, then it has already
915 // passed the makeRoomForOutgoingCall check once and will fail the second time due to the
916 // call transitioning into the CONNECTING state.
917 if (!isPotentialInCallMMICode && (!isReusedCall &&
918 !makeRoomForOutgoingCall(call, call.isEmergencyCall()))) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700919 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800920 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800921 if (mCalls.contains(call)) {
922 // This call can already exist if it is a reused call,
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800923 // See {@link #reuseOutgoingCall}.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800924 call.disconnect();
925 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700926 return null;
927 }
928
Nancy Chen8d92f452014-10-20 21:43:44 -0700929 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
Tyler Gunn6ffe5312015-08-12 08:19:20 -0700930 !call.isEmergencyCall();
Nancy Chen8d92f452014-10-20 21:43:44 -0700931
932 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700933 // This is the state where the user is expected to select an account
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700934 call.setState(CallState.SELECT_PHONE_ACCOUNT, "needs account selection");
Pawit Pornkitprasan1b2fd282015-03-15 08:33:32 +0700935 // Create our own instance to modify (since extras may be Bundle.EMPTY)
936 extras = new Bundle(extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700937 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700938 } else {
Roshan Pius781d0252015-06-25 14:13:07 -0700939 call.setState(
940 CallState.CONNECTING,
941 phoneAccountHandle == null ? "no-handle" : phoneAccountHandle.toString());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700942 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700943
Sailesh Nepal703a1af2016-04-14 20:10:12 -0700944 setIntentExtrasAndStartTime(call, extras);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700945
Tyler Gunn504eb832014-10-13 14:15:27 -0700946 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700947 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700948 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800949 } else if (!mCalls.contains(call)) {
950 // We check if mCalls already contains the call because we could potentially be reusing
Brad Ebingerb1e3d752015-11-09 17:54:17 -0800951 // a call which was previously added (See {@link #reuseOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700952 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700953 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700954
955 return call;
956 }
957
Ben Gilada0d9f752014-02-26 11:49:03 -0800958 /**
Yorke Lee33501632014-03-17 19:24:12 -0700959 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800960 *
Yorke Lee33501632014-03-17 19:24:12 -0700961 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700962 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700963 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700964 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700965 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800966 */
Hall Liu220b4192015-12-11 11:33:08 -0800967 @VisibleForTesting
968 public void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo,
969 boolean speakerphoneOn, int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700970 if (call == null) {
971 // don't do anything if the call no longer exists
972 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700973 return;
974 }
975
Nancy Chen201b4372014-09-08 14:18:24 -0700976 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700977
978 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700979 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700980 } else {
981 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
982 Log.pii(uriHandle), Log.pii(handle));
983 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700984
Nancy Chen0d3076c2014-07-30 14:45:44 -0700985 call.setHandle(uriHandle);
986 call.setGatewayInfo(gatewayInfo);
Tyler Gunnb492f4c2015-12-15 08:15:43 -0800987
988 final boolean useSpeakerWhenDocked = mContext.getResources().getBoolean(
989 R.bool.use_speaker_when_docked);
John Eckerdal63486002015-11-20 13:17:29 +0100990 final boolean useSpeakerForDock = isSpeakerphoneEnabledForDock();
Hall Liu9696c212016-06-24 16:09:02 -0700991 final boolean useSpeakerForVideoCall = isSpeakerphoneAutoEnabledForVideoCalls(videoState);
Tyler Gunnb492f4c2015-12-15 08:15:43 -0800992
993 // Auto-enable speakerphone if the originating intent specified to do so, if the call
994 // is a video call, of if using speaker when docked
995 call.setStartWithSpeakerphoneOn(speakerphoneOn || useSpeakerForVideoCall
John Eckerdal63486002015-11-20 13:17:29 +0100996 || (useSpeakerWhenDocked && useSpeakerForDock));
Tyler Gunnc4abd912014-07-08 14:22:10 -0700997 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700998
Santos Cordona04bdca2015-03-04 16:57:55 -0800999 if (speakerphoneOn) {
1000 Log.i(this, "%s Starting with speakerphone as requested", call);
John Eckerdal63486002015-11-20 13:17:29 +01001001 } else if (useSpeakerWhenDocked && useSpeakerForDock) {
Santos Cordona04bdca2015-03-04 16:57:55 -08001002 Log.i(this, "%s Starting with speakerphone because car is docked.", call);
Tyler Gunnb492f4c2015-12-15 08:15:43 -08001003 } else if (useSpeakerForVideoCall) {
1004 Log.i(this, "%s Starting with speakerphone because its a video call.", call);
Santos Cordona04bdca2015-03-04 16:57:55 -08001005 }
Bryce Lee88f900a2015-11-06 08:51:22 -08001006
Tyler Gunn6ffe5312015-08-12 08:19:20 -07001007 if (call.isEmergencyCall()) {
Abhijith Shastrycf27f682016-03-01 18:28:44 -08001008 new AsyncEmergencyContactNotifier(mContext).execute();
Nancy Chen53ceedc2014-07-08 18:56:51 -07001009 }
Nancy Chend9de92c2014-07-21 16:09:13 -07001010
Bryce Lee6afefa42015-10-25 22:25:53 -07001011 final boolean requireCallCapableAccountByHandle = mContext.getResources().getBoolean(
1012 com.android.internal.R.bool.config_requireCallCapableAccountForHandle);
1013
Tyler Gunn6ffe5312015-08-12 08:19:20 -07001014 if (call.getTargetPhoneAccount() != null || call.isEmergencyCall()) {
Nancy Chenbc9ef172014-08-15 17:11:57 -07001015 // If the account has been set, proceed to place the outgoing call.
1016 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001017 call.startCreateConnection(mPhoneAccountRegistrar);
Bryce Lee6afefa42015-10-25 22:25:53 -07001018 } else if (mPhoneAccountRegistrar.getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +00001019 requireCallCapableAccountByHandle ? call.getHandle().getScheme() : null, false,
1020 call.getInitiatingUser()).isEmpty()) {
Bryce Leede0ac372015-10-23 16:06:00 -07001021 // If there are no call capable accounts, disconnect the call.
1022 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.CANCELED,
1023 "No registered PhoneAccounts"));
1024 markCallAsRemoved(call);
Nancy Chend9de92c2014-07-21 16:09:13 -07001025 }
Yorke Leef98fb572014-03-05 10:56:55 -08001026 }
1027
1028 /**
Santos Cordona1610702014-06-04 20:22:56 -07001029 * Attempts to start a conference call for the specified call.
1030 *
Santos Cordon12d61822014-07-29 16:02:20 -07001031 * @param call The call to conference.
1032 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -07001033 */
Brad Ebinger53855132015-10-30 10:58:19 -07001034 @VisibleForTesting
1035 public void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -07001036 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -07001037 }
1038
1039 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001040 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
1041 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -08001042 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -07001043 *
1044 * @param call The call to answer.
1045 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -08001046 */
Brad Ebinger53855132015-10-30 10:58:19 -07001047 @VisibleForTesting
1048 public void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001049 if (!mCalls.contains(call)) {
1050 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -08001051 } else {
Hall Liue091ab92015-12-18 17:05:30 -08001052 Call foregroundCall = getForegroundCall();
Santos Cordon40f78c22014-04-07 02:11:42 -07001053 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -07001054 // STATE_DIALING, put it on hold before answering the call.
Hall Liue091ab92015-12-18 17:05:30 -08001055 if (foregroundCall != null && foregroundCall != call &&
1056 (foregroundCall.isActive() ||
Tyler Gunn1e37be52016-07-11 08:54:23 -07001057 foregroundCall.getState() == CallState.DIALING ||
1058 foregroundCall.getState() == CallState.PULLING)) {
Hall Liue091ab92015-12-18 17:05:30 -08001059 if (0 == (foregroundCall.getConnectionCapabilities()
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001060 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001061 // This call does not support hold. If it is from a different connection
1062 // service, then disconnect it, otherwise allow the connection service to
1063 // figure out the right states.
Hall Liue091ab92015-12-18 17:05:30 -08001064 if (foregroundCall.getConnectionService() != call.getConnectionService()) {
1065 foregroundCall.disconnect();
Santos Cordonf193ba42014-09-12 06:37:39 -07001066 }
1067 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -08001068 Call heldCall = getHeldCall();
1069 if (heldCall != null) {
1070 Log.v(this, "Disconnecting held call %s before holding active call.",
1071 heldCall);
1072 heldCall.disconnect();
1073 }
1074
Santos Cordonf193ba42014-09-12 06:37:39 -07001075 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
Hall Liue091ab92015-12-18 17:05:30 -08001076 foregroundCall, call);
1077 foregroundCall.hold();
Santos Cordonf193ba42014-09-12 06:37:39 -07001078 }
Santos Cordondf399862014-08-06 04:39:15 -07001079 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -07001080 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -07001081 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -07001082 }
1083
Santos Cordona56f2762014-03-24 15:55:53 -07001084 for (CallsManagerListener listener : mListeners) {
1085 listener.onIncomingCallAnswered(call);
1086 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001087
Santos Cordon61d0f702014-02-19 02:52:23 -08001088 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -07001089 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -07001090 call.answer(videoState);
Hall Liu9696c212016-06-24 16:09:02 -07001091 if (isSpeakerphoneAutoEnabledForVideoCalls(videoState)) {
Rekha Kumard240fe82015-04-01 21:45:57 -07001092 call.setStartWithSpeakerphoneOn(true);
1093 }
Santos Cordon61d0f702014-02-19 02:52:23 -08001094 }
Santos Cordone3d76ab2014-01-28 17:25:20 -08001095 }
1096
Tyler Gunnb360f0d2015-12-01 14:34:03 -08001097 /**
1098 * Determines if the speakerphone should be automatically enabled for the call. Speakerphone
1099 * should be enabled if the call is a video call and bluetooth or the wired headset are not in
1100 * use.
1101 *
1102 * @param videoState The video state of the call.
1103 * @return {@code true} if the speakerphone should be enabled.
1104 */
Hall Liu9696c212016-06-24 16:09:02 -07001105 public boolean isSpeakerphoneAutoEnabledForVideoCalls(int videoState) {
Tyler Gunnb360f0d2015-12-01 14:34:03 -08001106 return VideoProfile.isVideo(videoState) &&
1107 !mWiredHeadsetManager.isPluggedIn() &&
1108 !mBluetoothManager.isBluetoothAvailable() &&
1109 isSpeakerEnabledForVideoCalls();
1110 }
1111
1112 /**
John Eckerdal63486002015-11-20 13:17:29 +01001113 * Determines if the speakerphone should be enabled for when docked. Speakerphone
1114 * should be enabled if the device is docked and bluetooth or the wired headset are
1115 * not in use.
1116 *
1117 * @return {@code true} if the speakerphone should be enabled for the dock.
1118 */
1119 private boolean isSpeakerphoneEnabledForDock() {
1120 return mDockManager.isDocked() &&
1121 !mWiredHeadsetManager.isPluggedIn() &&
1122 !mBluetoothManager.isBluetoothAvailable();
1123 }
1124
1125 /**
Tyler Gunnb360f0d2015-12-01 14:34:03 -08001126 * Determines if the speakerphone should be automatically enabled for video calls.
1127 *
1128 * @return {@code true} if the speakerphone should automatically be enabled.
1129 */
Rekha Kumard240fe82015-04-01 21:45:57 -07001130 private static boolean isSpeakerEnabledForVideoCalls() {
1131 return (SystemProperties.getInt(TelephonyProperties.PROPERTY_VIDEOCALL_AUDIO_OUTPUT,
1132 PhoneConstants.AUDIO_OUTPUT_DEFAULT) ==
1133 PhoneConstants.AUDIO_OUTPUT_ENABLE_SPEAKER);
1134 }
1135
Santos Cordone3d76ab2014-01-28 17:25:20 -08001136 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001137 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
1138 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -08001139 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -08001140 */
Brad Ebinger53855132015-10-30 10:58:19 -07001141 @VisibleForTesting
1142 public void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001143 if (!mCalls.contains(call)) {
1144 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -08001145 } else {
Santos Cordona56f2762014-03-24 15:55:53 -07001146 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -07001147 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -07001148 }
Ihab Awadff7493a2014-06-10 13:47:44 -07001149 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -08001150 }
Santos Cordone3d76ab2014-01-28 17:25:20 -08001151 }
1152
1153 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001154 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -07001155 *
Ihab Awad74549ec2014-03-10 15:33:25 -07001156 * @param digit The DTMF digit to play.
1157 */
Brad Ebinger53855132015-10-30 10:58:19 -07001158 @VisibleForTesting
1159 public void playDtmfTone(Call call, char digit) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001160 if (!mCalls.contains(call)) {
1161 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -07001162 } else {
1163 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -07001164 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -07001165 }
1166 }
1167
1168 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001169 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -07001170 */
Brad Ebinger53855132015-10-30 10:58:19 -07001171 @VisibleForTesting
1172 public void stopDtmfTone(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001173 if (!mCalls.contains(call)) {
1174 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -07001175 } else {
1176 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -07001177 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -07001178 }
1179 }
1180
1181 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001182 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -07001183 */
Evan Charlton352105c2014-06-03 14:10:54 -07001184 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001185 if (!mCalls.contains(call)) {
1186 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -07001187 } else {
Evan Charlton352105c2014-06-03 14:10:54 -07001188 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -07001189 }
1190 }
1191
1192 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001193 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -08001194 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
1195 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -08001196 */
Brad Ebinger53855132015-10-30 10:58:19 -07001197 @VisibleForTesting
1198 public void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -07001199 Log.v(this, "disconnectCall %s", call);
1200
Sailesh Nepale59bb192014-04-01 18:33:59 -07001201 if (!mCalls.contains(call)) {
1202 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -08001203 } else {
Ihab Awad5b281322014-09-25 18:25:29 -07001204 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -08001205 call.disconnect();
1206 }
Santos Cordone3d76ab2014-01-28 17:25:20 -08001207 }
Santos Cordon681663d2014-01-30 04:32:15 -08001208
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001209 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001210 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -07001211 */
1212 void disconnectAllCalls() {
1213 Log.v(this, "disconnectAllCalls");
1214
1215 for (Call call : mCalls) {
1216 disconnectCall(call);
1217 }
1218 }
1219
Nancy Chenbc9ef172014-08-15 17:11:57 -07001220
Tyler Gunn61b92102014-08-19 07:42:20 -07001221 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001222 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001223 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
1224 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001225 */
Brad Ebinger53855132015-10-30 10:58:19 -07001226 @VisibleForTesting
1227 public void holdCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001228 if (!mCalls.contains(call)) {
1229 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001230 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001231 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001232 call.hold();
1233 }
1234 }
1235
1236 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001237 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001238 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
1239 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001240 */
Brad Ebinger53855132015-10-30 10:58:19 -07001241 @VisibleForTesting
1242 public void unholdCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -07001243 if (!mCalls.contains(call)) {
1244 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001245 } else {
Hall Liu874c0f82016-04-29 18:13:18 -07001246 boolean otherCallHeld = false;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001247 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001248 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -08001249 // Only attempt to hold parent calls and not the individual children.
1250 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Hall Liu874c0f82016-04-29 18:13:18 -07001251 otherCallHeld = true;
1252 Log.event(c, Log.Events.SWAP);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -07001253 c.hold();
1254 }
1255 }
Hall Liu874c0f82016-04-29 18:13:18 -07001256 if (otherCallHeld) {
1257 Log.event(call, Log.Events.SWAP);
1258 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001259 call.unhold();
1260 }
1261 }
1262
Hall Liu32587202015-11-18 11:10:08 -08001263 @Override
Tyler Gunn961694a2016-03-21 16:01:40 -07001264 public void onExtrasChanged(Call c, int source, Bundle extras) {
1265 if (source != Call.SOURCE_CONNECTION_SERVICE) {
1266 return;
1267 }
1268 handleCallTechnologyChange(c);
Brad Ebingerbaf52ba2016-03-21 16:38:32 -07001269 handleChildAddressChange(c);
Hall Liufc130b22016-06-15 17:54:48 -07001270 updateCanAddCall();
Tyler Gunn961694a2016-03-21 16:01:40 -07001271 }
1272
Brad Ebinger3da57642016-04-04 17:32:19 -07001273 // Construct the list of possible PhoneAccounts that the outgoing call can use based on the
1274 // active calls in CallsManager. If any of the active calls are on a SIM based PhoneAccount,
1275 // then include only that SIM based PhoneAccount and any non-SIM PhoneAccounts, such as SIP.
1276 private List<PhoneAccountHandle> constructPossiblePhoneAccounts(Uri handle, UserHandle user) {
1277 if (handle == null) {
1278 return Collections.emptyList();
1279 }
1280 List<PhoneAccountHandle> allAccounts =
1281 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme(), false, user);
1282 // First check the Radio SIM Technology
1283 if(mRadioSimVariants == null) {
1284 TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
1285 Context.TELEPHONY_SERVICE);
1286 // Cache Sim Variants
1287 mRadioSimVariants = tm.getMultiSimConfiguration();
1288 }
1289 // Only one SIM PhoneAccount can be active at one time for DSDS. Only that SIM PhoneAccount
1290 // Should be available if a call is already active on the SIM account.
1291 if(mRadioSimVariants != TelephonyManager.MultiSimVariants.DSDA) {
1292 List<PhoneAccountHandle> simAccounts =
1293 mPhoneAccountRegistrar.getSimPhoneAccountsOfCurrentUser();
1294 PhoneAccountHandle ongoingCallAccount = null;
1295 for (Call c : mCalls) {
1296 if (!c.isDisconnected() && !c.isNew() && simAccounts.contains(
1297 c.getTargetPhoneAccount())) {
1298 ongoingCallAccount = c.getTargetPhoneAccount();
1299 break;
1300 }
1301 }
1302 if (ongoingCallAccount != null) {
1303 // Remove all SIM accounts that are not the active SIM from the list.
1304 simAccounts.remove(ongoingCallAccount);
1305 allAccounts.removeAll(simAccounts);
1306 }
1307 }
1308 return allAccounts;
1309 }
1310
Tyler Gunn1a40c4f2016-04-14 14:29:45 -07001311 /**
1312 * Informs listeners (notably {@link CallAudioManager} of a change to the call's external
1313 * property.
1314 * .
1315 * @param call The call whose external property changed.
1316 * @param isExternalCall {@code True} if the call is now external, {@code false} otherwise.
1317 */
1318 @Override
1319 public void onExternalCallChanged(Call call, boolean isExternalCall) {
1320 Log.v(this, "onConnectionPropertiesChanged: %b", isExternalCall);
1321 for (CallsManagerListener listener : mListeners) {
1322 listener.onExternalCallChanged(call, isExternalCall);
1323 }
1324 }
1325
Tyler Gunn961694a2016-03-21 16:01:40 -07001326 private void handleCallTechnologyChange(Call call) {
Hall Liu32587202015-11-18 11:10:08 -08001327 if (call.getExtras() != null
1328 && call.getExtras().containsKey(TelecomManager.EXTRA_CALL_TECHNOLOGY_TYPE)) {
1329
1330 Integer analyticsCallTechnology = sAnalyticsTechnologyMap.get(
1331 call.getExtras().getInt(TelecomManager.EXTRA_CALL_TECHNOLOGY_TYPE));
1332 if (analyticsCallTechnology == null) {
1333 analyticsCallTechnology = Analytics.THIRD_PARTY_PHONE;
1334 }
1335 call.getAnalytics().addCallTechnology(analyticsCallTechnology);
1336 }
1337 }
1338
Brad Ebingerbaf52ba2016-03-21 16:38:32 -07001339 public void handleChildAddressChange(Call call) {
1340 if (call.getExtras() != null
1341 && call.getExtras().containsKey(Connection.EXTRA_CHILD_ADDRESS)) {
1342
1343 String viaNumber = call.getExtras().getString(Connection.EXTRA_CHILD_ADDRESS);
1344 call.setViaNumber(viaNumber);
1345 }
1346 }
1347
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001348 /** Called by the in-call UI to change the mute state. */
1349 void mute(boolean shouldMute) {
1350 mCallAudioManager.mute(shouldMute);
1351 }
1352
1353 /**
1354 * Called by the in-call UI to change the audio route, for example to change from earpiece to
1355 * speaker phone.
1356 */
1357 void setAudioRoute(int route) {
1358 mCallAudioManager.setAudioRoute(route);
1359 }
1360
Yorke Leed1346872014-07-28 14:38:45 -07001361 /** Called by the in-call UI to turn the proximity sensor on. */
1362 void turnOnProximitySensor() {
1363 mProximitySensorManager.turnOn();
1364 }
1365
1366 /**
1367 * Called by the in-call UI to turn the proximity sensor off.
1368 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
1369 * the screen will be kept off until the proximity sensor goes negative.
1370 */
1371 void turnOffProximitySensor(boolean screenOnImmediately) {
1372 mProximitySensorManager.turnOff(screenOnImmediately);
1373 }
1374
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001375 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -07001376 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001377 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -07001378 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -07001379 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -07001380
Hall Liu4b1759d2016-02-02 18:17:40 -08001381 if (!call.isNewOutgoingCallIntentBroadcastDone()) {
1382 return;
1383 }
1384
Santos Cordonf193ba42014-09-12 06:37:39 -07001385 // Note: emergency calls never go through account selection dialog so they never
1386 // arrive here.
1387 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001388 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -07001389 } else {
1390 call.disconnect();
1391 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001392
1393 if (setDefault) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001394 mPhoneAccountRegistrar
1395 .setUserSelectedOutgoingPhoneAccount(account, call.getInitiatingUser());
Nancy Chenf5e5d3c2014-10-21 18:45:56 -07001396 }
Nancy Chen53ceedc2014-07-08 18:56:51 -07001397 }
1398 }
1399
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001400 /** Called when the audio state changes. */
Hall Liuf62630a2015-10-27 14:53:39 -07001401 @VisibleForTesting
1402 public void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState
1403 newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001404 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001405 for (CallsManagerListener listener : mListeners) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07001406 listener.onCallAudioStateChanged(oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -07001407 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001408 }
1409
Sailesh Nepale59bb192014-04-01 18:33:59 -07001410 void markCallAsRinging(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001411 setCallState(call, CallState.RINGING, "ringing set explicitly");
Santos Cordon681663d2014-01-30 04:32:15 -08001412 }
1413
Sailesh Nepale59bb192014-04-01 18:33:59 -07001414 void markCallAsDialing(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001415 setCallState(call, CallState.DIALING, "dialing set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -07001416 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -08001417 }
1418
Tyler Gunn1e37be52016-07-11 08:54:23 -07001419 void markCallAsPulling(Call call) {
1420 setCallState(call, CallState.PULLING, "pulling set explicitly");
1421 maybeMoveToSpeakerPhone(call);
1422 }
1423
Sailesh Nepale59bb192014-04-01 18:33:59 -07001424 void markCallAsActive(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001425 setCallState(call, CallState.ACTIVE, "active set explicitly");
Santos Cordond6782cd2015-04-16 09:57:50 -07001426 maybeMoveToSpeakerPhone(call);
Santos Cordon681663d2014-01-30 04:32:15 -08001427 }
1428
Sailesh Nepale59bb192014-04-01 18:33:59 -07001429 void markCallAsOnHold(Call call) {
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001430 setCallState(call, CallState.ON_HOLD, "on-hold set explicitly");
Yorke Leecdf3ebd2014-03-12 18:31:41 -07001431 }
1432
Santos Cordon049b7b62014-01-30 05:34:26 -08001433 /**
Santos Cordonf193ba42014-09-12 06:37:39 -07001434 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
1435 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -08001436 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001437 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -08001438 */
Andrew Lee701dc002014-09-11 21:29:12 -07001439 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
1440 call.setDisconnectCause(disconnectCause);
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001441 setCallState(call, CallState.DISCONNECTED, "disconnected set explicitly");
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -07001442 }
1443
Ben Gilada0d9f752014-02-26 11:49:03 -08001444 /**
Ihab Awada02bef52014-08-13 18:18:42 -07001445 * Removes an existing disconnected call, and notifies the in-call app.
1446 */
1447 void markCallAsRemoved(Call call) {
1448 removeCall(call);
Tyler Gunn7d58ba52016-07-25 15:05:48 -07001449 Call foregroundCall = mCallAudioManager.getPossiblyHeldForegroundCall();
Ihab Awad5b281322014-09-25 18:25:29 -07001450 if (mLocallyDisconnectingCalls.contains(call)) {
1451 mLocallyDisconnectingCalls.remove(call);
Hall Liue091ab92015-12-18 17:05:30 -08001452 if (foregroundCall != null && foregroundCall.getState() == CallState.ON_HOLD) {
1453 foregroundCall.unhold();
Ihab Awad5b281322014-09-25 18:25:29 -07001454 }
Tyler Gunn7d58ba52016-07-25 15:05:48 -07001455 } else if (foregroundCall != null &&
1456 !foregroundCall.can(Connection.CAPABILITY_SUPPORT_HOLD) &&
1457 foregroundCall.getState() == CallState.ON_HOLD) {
1458
1459 // The new foreground call is on hold, however the carrier does not display the hold
1460 // button in the UI. Therefore, we need to auto unhold the held call since the user has
1461 // no means of unholding it themselves.
1462 Log.i(this, "Auto-unholding held foreground call (call doesn't support hold)");
1463 foregroundCall.unhold();
Ihab Awad5b281322014-09-25 18:25:29 -07001464 }
Ihab Awada02bef52014-08-13 18:18:42 -07001465 }
1466
1467 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001468 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001469 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001470 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001471 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -07001472 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001473 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001474 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -07001475 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001476 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -08001477 if (call.getState() != CallState.DISCONNECTED) {
1478 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
1479 }
1480 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001481 }
Santos Cordon4b2c1192014-03-19 18:15:38 -07001482 }
1483 }
1484 }
1485
Tyler Gunn1a40c4f2016-04-14 14:29:45 -07001486 /**
1487 * Determines if the {@link CallsManager} has any non-external calls.
1488 *
1489 * @return {@code True} if there are any non-external calls, {@code false} otherwise.
1490 */
Santos Cordondeb8c892014-05-30 01:38:03 -07001491 boolean hasAnyCalls() {
Tyler Gunn1a40c4f2016-04-14 14:29:45 -07001492 if (mCalls.isEmpty()) {
1493 return false;
1494 }
1495
1496 for (Call call : mCalls) {
1497 if (!call.isExternalCall()) {
1498 return true;
1499 }
1500 }
1501 return false;
Santos Cordondeb8c892014-05-30 01:38:03 -07001502 }
1503
Santos Cordonc7e85d42014-05-22 02:51:48 -07001504 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001505 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001506 }
1507
1508 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -07001509 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -07001510 }
1511
Santos Cordondeb8c892014-05-30 01:38:03 -07001512 boolean onMediaButton(int type) {
1513 if (hasAnyCalls()) {
1514 if (HeadsetMediaButton.SHORT_PRESS == type) {
1515 Call ringingCall = getFirstCallWithState(CallState.RINGING);
1516 if (ringingCall == null) {
1517 mCallAudioManager.toggleMute();
1518 return true;
1519 } else {
Hall Liu3777a7e2016-07-25 14:46:48 -07001520 ringingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
Santos Cordondeb8c892014-05-30 01:38:03 -07001521 return true;
1522 }
1523 } else if (HeadsetMediaButton.LONG_PRESS == type) {
1524 Log.d(this, "handleHeadsetHook: longpress -> hangup");
1525 Call callToHangup = getFirstCallWithState(
Tyler Gunn1e37be52016-07-11 08:54:23 -07001526 CallState.RINGING, CallState.DIALING, CallState.PULLING, CallState.ACTIVE,
1527 CallState.ON_HOLD);
Santos Cordondeb8c892014-05-30 01:38:03 -07001528 if (callToHangup != null) {
1529 callToHangup.disconnect();
1530 return true;
1531 }
1532 }
1533 }
1534 return false;
1535 }
1536
1537 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -08001538 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -07001539 */
Hall Liu28b82f02016-07-26 17:38:56 -07001540 @VisibleForTesting
1541 public boolean canAddCall() {
Hall Liuf1422e72016-01-27 11:07:07 -08001542 boolean isDeviceProvisioned = Settings.Global.getInt(mContext.getContentResolver(),
1543 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1544 if (!isDeviceProvisioned) {
1545 Log.d(TAG, "Device not provisioned, canAddCall is false.");
1546 return false;
1547 }
1548
Andrew Lee113ad622014-12-01 18:33:10 -08001549 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
1550 return false;
1551 }
1552
Santos Cordon91bd74c2014-11-07 16:10:22 -08001553 int count = 0;
1554 for (Call call : mCalls) {
1555 if (call.isEmergencyCall()) {
1556 // We never support add call if one of the calls is an emergency call.
1557 return false;
Tyler Gunn1e37be52016-07-11 08:54:23 -07001558 } else if (call.isExternalCall()) {
1559 // External calls don't count.
1560 continue;
Santos Cordon91bd74c2014-11-07 16:10:22 -08001561 } else if (call.getParentCall() == null) {
1562 count++;
1563 }
Hall Liufc130b22016-06-15 17:54:48 -07001564 Bundle extras = call.getExtras();
1565 if (extras != null) {
Hall Liu140c8402016-07-06 16:30:46 -07001566 if (extras.getBoolean(Connection.EXTRA_DISABLE_ADD_CALL, false)) {
1567 return false;
1568 }
Hall Liufc130b22016-06-15 17:54:48 -07001569 }
Santos Cordon10838c22014-06-11 17:36:04 -07001570
Santos Cordon91bd74c2014-11-07 16:10:22 -08001571 // We do not check states for canAddCall. We treat disconnected calls the same
1572 // and wait until they are removed instead. If we didn't count disconnected calls,
1573 // we could put InCallServices into a state where they are showing two calls but
1574 // also support add-call. Technically it's right, but overall looks better (UI-wise)
1575 // and acts better if we wait until the call is removed.
1576 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -07001577 return false;
1578 }
1579 }
Hall Liufc130b22016-06-15 17:54:48 -07001580
Santos Cordon10838c22014-06-11 17:36:04 -07001581 return true;
1582 }
1583
Ihab Awada3653902015-01-23 13:44:46 -08001584 @VisibleForTesting
P.Y. Laligandd35be752015-03-06 14:12:43 -08001585 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001586 return getFirstCallWithState(CallState.RINGING);
1587 }
1588
Brad Ebinger53855132015-10-30 10:58:19 -07001589 @VisibleForTesting
1590 public Call getActiveCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001591 return getFirstCallWithState(CallState.ACTIVE);
1592 }
1593
Nancy Chen05a9e402014-09-26 14:14:32 -07001594 Call getDialingCall() {
1595 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001596 }
1597
Brad Ebinger53855132015-10-30 10:58:19 -07001598 @VisibleForTesting
1599 public Call getHeldCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001600 return getFirstCallWithState(CallState.ON_HOLD);
1601 }
1602
Brad Ebinger53855132015-10-30 10:58:19 -07001603 @VisibleForTesting
1604 public int getNumHeldCalls() {
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001605 int count = 0;
1606 for (Call call : mCalls) {
1607 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1608 count++;
1609 }
1610 }
1611 return count;
1612 }
1613
Brad Ebinger53855132015-10-30 10:58:19 -07001614 @VisibleForTesting
1615 public Call getOutgoingCall() {
Roshan Pius7d7cf272015-08-28 11:10:56 -07001616 return getFirstCallWithState(OUTGOING_CALL_STATES);
1617 }
1618
Hall Liu0a6dd302015-12-16 15:06:49 -08001619 @VisibleForTesting
1620 public Call getFirstCallWithState(int... states) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001621 return getFirstCallWithState(null, states);
1622 }
1623
Brad Ebinger6e8f3d72016-06-20 11:35:42 -07001624 @VisibleForTesting
1625 public PhoneNumberUtilsAdapter getPhoneNumberUtilsAdapter() {
1626 return mPhoneNumberUtilsAdapter;
1627 }
1628
Santos Cordon10838c22014-06-11 17:36:04 -07001629 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001630 * Returns the first call that it finds with the given states. The states are treated as having
1631 * priority order so that any call with the first state will be returned before any call with
1632 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001633 *
1634 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001635 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001636 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001637 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001638 // check the foreground first
Hall Liue091ab92015-12-18 17:05:30 -08001639 Call foregroundCall = getForegroundCall();
1640 if (foregroundCall != null && foregroundCall.getState() == currentState) {
1641 return foregroundCall;
Santos Cordon23baed32014-06-27 14:45:39 -07001642 }
1643
Santos Cordondeb8c892014-05-30 01:38:03 -07001644 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001645 if (Objects.equals(callToSkip, call)) {
1646 continue;
1647 }
1648
1649 // Only operate on top-level calls
1650 if (call.getParentCall() != null) {
1651 continue;
1652 }
1653
Tyler Gunnf15dc332016-06-07 16:01:41 -07001654 if (call.isExternalCall()) {
1655 continue;
1656 }
1657
Santos Cordondeb8c892014-05-30 01:38:03 -07001658 if (currentState == call.getState()) {
1659 return call;
1660 }
1661 }
1662 }
1663 return null;
1664 }
1665
Santos Cordon0fbe6322014-08-14 04:04:25 -07001666 Call createConferenceCall(
Tyler Gunn8452be02015-09-17 09:57:02 -07001667 String callId,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001668 PhoneAccountHandle phoneAccount,
1669 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001670
1671 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1672 // which is the default value for new Calls.
1673 long connectTime =
1674 parcelableConference.getConnectTimeMillis() ==
1675 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1676 parcelableConference.getConnectTimeMillis();
1677
Santos Cordon0fbe6322014-08-14 04:04:25 -07001678 Call call = new Call(
Tyler Gunn8452be02015-09-17 09:57:02 -07001679 callId,
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001680 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001681 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07001682 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001683 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001684 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07001685 mCallerInfoAsyncQueryFactory,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -07001686 mPhoneNumberUtilsAdapter,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001687 null /* handle */,
1688 null /* gatewayInfo */,
1689 null /* connectionManagerPhoneAccount */,
1690 phoneAccount,
Hall Liu32587202015-11-18 11:10:08 -08001691 Call.CALL_DIRECTION_UNDEFINED /* callDirection */,
1692 false /* forceAttachToExistingConnection */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001693 true /* isConference */,
1694 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001695
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001696 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()),
1697 "new conference call");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001698 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Tyler Gunn571d5e62016-03-15 15:55:18 -07001699 call.setConnectionProperties(parcelableConference.getConnectionProperties());
Tyler Gunn13e8a692016-08-03 18:38:00 -07001700 call.setVideoState(parcelableConference.getVideoState());
Rekha Kumard240fe82015-04-01 21:45:57 -07001701 call.setVideoProvider(parcelableConference.getVideoProvider());
Andrew Lee57412d32015-04-14 13:38:44 -07001702 call.setStatusHints(parcelableConference.getStatusHints());
Tyler Gunn961694a2016-03-21 16:01:40 -07001703 call.putExtras(Call.SOURCE_CONNECTION_SERVICE, parcelableConference.getExtras());
Tyler Gunn9b618b82016-10-17 15:54:35 -07001704 // In case this Conference was added via a ConnectionManager, keep track of the original
1705 // Connection ID as created by the originating ConnectionService.
1706 Bundle extras = parcelableConference.getExtras();
1707 if (extras != null && extras.containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
1708 call.setOriginalConnectionId(extras.getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID));
1709 }
Santos Cordon0fbe6322014-08-14 04:04:25 -07001710
1711 // TODO: Move this to be a part of addCall()
1712 call.addListener(this);
1713 addCall(call);
1714 return call;
1715 }
1716
Yorke Leef86db2e2014-09-12 17:56:48 -07001717 /**
1718 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1719 */
1720 int getCallState() {
1721 return mPhoneStateBroadcaster.getCallState();
1722 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001723
Santos Cordon4b2c1192014-03-19 18:15:38 -07001724 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001725 * Retrieves the {@link PhoneAccountRegistrar}.
1726 *
1727 * @return The {@link PhoneAccountRegistrar}.
1728 */
1729 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1730 return mPhoneAccountRegistrar;
1731 }
1732
1733 /**
1734 * Retrieves the {@link MissedCallNotifier}
1735 * @return The {@link MissedCallNotifier}.
1736 */
1737 MissedCallNotifier getMissedCallNotifier() {
1738 return mMissedCallNotifier;
1739 }
1740
1741 /**
Brad Ebinger3165d502015-12-15 17:22:29 -08001742 * Reject an incoming call and manually add it to the Call Log.
1743 * @param incomingCall Incoming call that has been rejected
1744 */
1745 private void rejectCallAndLog(Call incomingCall) {
Tyler Gunn165ee752016-09-06 13:40:10 -07001746 if (incomingCall.getConnectionService() != null) {
1747 // Only reject the call if it has not already been destroyed. If a call ends while
1748 // incoming call filtering is taking place, it is possible that the call has already
1749 // been destroyed, and as such it will be impossible to send the reject to the
1750 // associated ConnectionService.
1751 incomingCall.reject(false, null);
1752 } else {
1753 Log.i(this, "rejectCallAndLog - call already destroyed.");
1754 }
1755
Brad Ebinger3165d502015-12-15 17:22:29 -08001756 // Since the call was not added to the list of calls, we have to call the missed
1757 // call notifier and the call logger manually.
1758 // Do we need missed call notification for direct to Voicemail calls?
Ta-wei Yen982c0bd2016-04-14 13:59:54 -07001759 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE,
1760 true /*showNotificationForMissedCall*/);
Brad Ebinger3165d502015-12-15 17:22:29 -08001761 }
1762
1763 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001764 * Adds the specified call to the main list of live calls.
1765 *
1766 * @param call The call to add.
1767 */
1768 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001769 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001770 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001771 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001772 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001773
Sailesh Nepal703a1af2016-04-14 20:10:12 -07001774 // Specifies the time telecom finished routing the call. This is used by the dialer for
1775 // analytics.
1776 Bundle extras = call.getIntentExtras();
1777 extras.putLong(TelecomManager.EXTRA_CALL_TELECOM_ROUTING_END_TIME_MILLIS,
1778 SystemClock.elapsedRealtime());
1779
Hall Liufc130b22016-06-15 17:54:48 -07001780 updateCanAddCall();
Santos Cordon40f78c22014-04-07 02:11:42 -07001781 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001782 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001783 if (Log.SYSTRACE_DEBUG) {
1784 Trace.beginSection(listener.getClass().toString() + " addCall");
1785 }
Santos Cordona56f2762014-03-24 15:55:53 -07001786 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001787 if (Log.SYSTRACE_DEBUG) {
1788 Trace.endSection();
1789 }
Santos Cordona56f2762014-03-24 15:55:53 -07001790 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001791 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001792 }
1793
Sailesh Nepal810735e2014-03-18 18:15:46 -07001794 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001795 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001796 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001797
Santos Cordon672321e2014-08-21 14:38:58 -07001798 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001799 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001800 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001801
1802 boolean shouldNotify = false;
1803 if (mCalls.contains(call)) {
1804 mCalls.remove(call);
1805 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001806 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001807
Santos Cordon2685dab2015-04-17 17:12:09 -07001808 call.destroy();
1809
Sailesh Nepale59bb192014-04-01 18:33:59 -07001810 // Only broadcast changes for calls that are being tracked.
1811 if (shouldNotify) {
Hall Liufc130b22016-06-15 17:54:48 -07001812 updateCanAddCall();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001813 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001814 if (Log.SYSTRACE_DEBUG) {
1815 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1816 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001817 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001818 if (Log.SYSTRACE_DEBUG) {
1819 Trace.endSection();
1820 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001821 }
Yorke Lee604a90f2014-10-20 12:13:32 -07001822 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001823 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001824 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001825
Sailesh Nepal810735e2014-03-18 18:15:46 -07001826 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001827 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001828 *
1829 * @param call The call.
1830 * @param newState The new state of the call.
1831 */
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001832 private void setCallState(Call call, int newState, String tag) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001833 if (call == null) {
1834 return;
1835 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001836 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001837 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1838 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001839 if (newState != oldState) {
1840 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1841 // 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 -07001842 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001843 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001844 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001845 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001846 // unexpected transition occurs.
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07001847 call.setState(newState, tag);
Xueren Zhangc0e8e9d2015-03-06 14:16:55 +01001848 maybeShowErrorDialogOnDisconnect(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001849
Yorke Leee4a9c412014-11-14 16:59:42 -08001850 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001851 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001852 if (mCalls.contains(call)) {
Hall Liufc130b22016-06-15 17:54:48 -07001853 updateCanAddCall();
Santos Cordona56f2762014-03-24 15:55:53 -07001854 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001855 if (Log.SYSTRACE_DEBUG) {
1856 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1857 }
Santos Cordona56f2762014-03-24 15:55:53 -07001858 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001859 if (Log.SYSTRACE_DEBUG) {
1860 Trace.endSection();
1861 }
Santos Cordona56f2762014-03-24 15:55:53 -07001862 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001863 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001864 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001865 }
1866 }
1867
Santos Cordon91bd74c2014-11-07 16:10:22 -08001868 private void updateCanAddCall() {
1869 boolean newCanAddCall = canAddCall();
1870 if (newCanAddCall != mCanAddCall) {
1871 mCanAddCall = newCanAddCall;
1872 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001873 if (Log.SYSTRACE_DEBUG) {
1874 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1875 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001876 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001877 if (Log.SYSTRACE_DEBUG) {
1878 Trace.endSection();
1879 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001880 }
1881 }
1882 }
1883
Yorke Leeb701f6d2014-08-12 14:04:19 -07001884 private boolean isPotentialMMICode(Uri handle) {
1885 return (handle != null && handle.getSchemeSpecificPart() != null
1886 && handle.getSchemeSpecificPart().contains("#"));
1887 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001888
Tyler Gunn5b452df2014-10-01 15:02:58 -07001889 /**
1890 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1891 * MMI codes which can be dialed when one or more calls are in progress.
1892 * <P>
1893 * Checks for numbers formatted similar to the MMI codes defined in:
Amit Mahajan8c4e22a2015-12-14 13:24:15 -08001894 * {@link com.android.internal.telephony.Phone#handleInCallMmiCommands(String)}
Tyler Gunn5b452df2014-10-01 15:02:58 -07001895 *
1896 * @param handle The URI to call.
1897 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1898 */
1899 private boolean isPotentialInCallMMICode(Uri handle) {
1900 if (handle != null && handle.getSchemeSpecificPart() != null &&
Xueren Zhangc0e8e9d2015-03-06 14:16:55 +01001901 handle.getScheme() != null &&
Tyler Gunn5b452df2014-10-01 15:02:58 -07001902 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1903
1904 String dialedNumber = handle.getSchemeSpecificPart();
1905 return (dialedNumber.equals("0") ||
1906 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1907 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1908 dialedNumber.equals("3") ||
1909 dialedNumber.equals("4") ||
1910 dialedNumber.equals("5"));
1911 }
1912 return false;
1913 }
1914
Santos Cordonf193ba42014-09-12 06:37:39 -07001915 private int getNumCallsWithState(int... states) {
1916 int count = 0;
1917 for (int state : states) {
1918 for (Call call : mCalls) {
Tyler Gunnf15dc332016-06-07 16:01:41 -07001919 if (call.getParentCall() == null && call.getState() == state &&
1920 !call.isExternalCall()) {
1921
Santos Cordonf193ba42014-09-12 06:37:39 -07001922 count++;
1923 }
1924 }
1925 }
1926 return count;
1927 }
1928
1929 private boolean hasMaximumLiveCalls() {
1930 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1931 }
1932
1933 private boolean hasMaximumHoldingCalls() {
1934 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1935 }
1936
1937 private boolean hasMaximumRingingCalls() {
1938 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1939 }
1940
1941 private boolean hasMaximumOutgoingCalls() {
1942 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1943 }
1944
Roshan Pius4de4a892015-08-24 11:06:58 -07001945 private boolean hasMaximumDialingCalls() {
Tyler Gunn1e37be52016-07-11 08:54:23 -07001946 return MAXIMUM_DIALING_CALLS <= getNumCallsWithState(CallState.DIALING, CallState.PULLING);
Roshan Pius4de4a892015-08-24 11:06:58 -07001947 }
1948
Santos Cordonf193ba42014-09-12 06:37:39 -07001949 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1950 if (hasMaximumLiveCalls()) {
1951 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1952 // have to change.
Tony Makf5a32e42016-02-02 12:10:26 +00001953 Call liveCall = getFirstCallWithState(LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301954 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1955 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001956
Santos Cordond1766502014-09-26 15:45:39 -07001957 if (call == liveCall) {
1958 // If the call is already the foreground call, then we are golden.
Santos Cordon92694512015-04-23 14:47:28 -07001959 // This can happen after the user selects an account in the SELECT_PHONE_ACCOUNT
Santos Cordond1766502014-09-26 15:45:39 -07001960 // state since the call was already populated into the list.
1961 return true;
1962 }
1963
Andrew Leee6595182014-09-23 18:43:05 -07001964 if (hasMaximumOutgoingCalls()) {
Yorke Lee867907d2015-06-03 12:24:43 -07001965 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1966 if (isEmergency && !outgoingCall.isEmergencyCall()) {
1967 // Disconnect the current outgoing call if it's not an emergency call. If the
1968 // user tries to make two outgoing calls to different emergency call numbers,
1969 // we will try to connect the first outgoing call.
Hall Liu32587202015-11-18 11:10:08 -08001970 call.getAnalytics().setCallIsAdditional(true);
1971 outgoingCall.getAnalytics().setCallIsInterrupted(true);
Yorke Lee867907d2015-06-03 12:24:43 -07001972 outgoingCall.disconnect();
1973 return true;
1974 }
1975 if (outgoingCall.getState() == CallState.SELECT_PHONE_ACCOUNT) {
1976 // If there is an orphaned call in the {@link CallState#SELECT_PHONE_ACCOUNT}
1977 // state, just disconnect it since the user has explicitly started a new call.
Hall Liu32587202015-11-18 11:10:08 -08001978 call.getAnalytics().setCallIsAdditional(true);
1979 outgoingCall.getAnalytics().setCallIsInterrupted(true);
Yorke Lee867907d2015-06-03 12:24:43 -07001980 outgoingCall.disconnect();
1981 return true;
Andrew Leee6595182014-09-23 18:43:05 -07001982 }
1983 return false;
1984 }
1985
Santos Cordonf193ba42014-09-12 06:37:39 -07001986 if (hasMaximumHoldingCalls()) {
1987 // There is no more room for any more calls, unless it's an emergency.
1988 if (isEmergency) {
1989 // Kill the current active call, this is easier then trying to disconnect a
1990 // holding call and hold an active call.
Hall Liu32587202015-11-18 11:10:08 -08001991 call.getAnalytics().setCallIsAdditional(true);
1992 liveCall.getAnalytics().setCallIsInterrupted(true);
Santos Cordonf193ba42014-09-12 06:37:39 -07001993 liveCall.disconnect();
1994 return true;
1995 }
1996 return false; // No more room!
1997 }
1998
1999 // We have room for at least one more holding call at this point.
2000
Tyler Gunne4cd9162015-08-11 15:36:04 -07002001 // TODO: Remove once b/23035408 has been corrected.
2002 // If the live call is a conference, it will not have a target phone account set. This
2003 // means the check to see if the live call has the same target phone account as the new
2004 // call will not cause us to bail early. As a result, we'll end up holding the
2005 // ongoing conference call. However, the ConnectionService is already doing that. This
2006 // has caused problems with some carriers. As a workaround until b/23035408 is
2007 // corrected, we will try and get the target phone account for one of the conference's
2008 // children and use that instead.
2009 PhoneAccountHandle liveCallPhoneAccount = liveCall.getTargetPhoneAccount();
2010 if (liveCallPhoneAccount == null && liveCall.isConference() &&
2011 !liveCall.getChildCalls().isEmpty()) {
2012 liveCallPhoneAccount = getFirstChildPhoneAccount(liveCall);
2013 Log.i(this, "makeRoomForOutgoingCall: using child call PhoneAccount = " +
2014 liveCallPhoneAccount);
2015 }
2016
Santos Cordonf193ba42014-09-12 06:37:39 -07002017 // First thing, if we are trying to make a call with the same phone account as the live
2018 // call, then allow it so that the connection service can make its own decision about
2019 // how to handle the new call relative to the current one.
Tyler Gunne4cd9162015-08-11 15:36:04 -07002020 if (Objects.equals(liveCallPhoneAccount, call.getTargetPhoneAccount())) {
2021 Log.i(this, "makeRoomForOutgoingCall: phoneAccount matches.");
Hall Liu32587202015-11-18 11:10:08 -08002022 call.getAnalytics().setCallIsAdditional(true);
2023 liveCall.getAnalytics().setCallIsInterrupted(true);
Santos Cordonf193ba42014-09-12 06:37:39 -07002024 return true;
2025 } else if (call.getTargetPhoneAccount() == null) {
2026 // Without a phone account, we can't say reliably that the call will fail.
2027 // If the user chooses the same phone account as the live call, then it's
2028 // still possible that the call can be made (like with CDMA calls not supporting
2029 // hold but they still support adding a call by going immediately into conference
2030 // mode). Return true here and we'll run this code again after user chooses an
2031 // account.
2032 return true;
2033 }
2034
2035 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08002036 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Tyler Gunne4cd9162015-08-11 15:36:04 -07002037 Log.i(this, "makeRoomForOutgoingCall: holding live call.");
Hall Liu32587202015-11-18 11:10:08 -08002038 call.getAnalytics().setCallIsAdditional(true);
2039 liveCall.getAnalytics().setCallIsInterrupted(true);
Santos Cordonf193ba42014-09-12 06:37:39 -07002040 liveCall.hold();
2041 return true;
2042 }
2043
2044 // The live call cannot be held so we're out of luck here. There's no room.
2045 return false;
2046 }
2047 return true;
2048 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002049
2050 /**
Tyler Gunne4cd9162015-08-11 15:36:04 -07002051 * Given a call, find the first non-null phone account handle of its children.
2052 *
2053 * @param parentCall The parent call.
2054 * @return The first non-null phone account handle of the children, or {@code null} if none.
2055 */
2056 private PhoneAccountHandle getFirstChildPhoneAccount(Call parentCall) {
2057 for (Call childCall : parentCall.getChildCalls()) {
2058 PhoneAccountHandle childPhoneAccount = childCall.getTargetPhoneAccount();
2059 if (childPhoneAccount != null) {
2060 return childPhoneAccount;
2061 }
2062 }
2063 return null;
2064 }
2065
2066 /**
Santos Cordond6782cd2015-04-16 09:57:50 -07002067 * Checks to see if the call should be on speakerphone and if so, set it.
2068 */
2069 private void maybeMoveToSpeakerPhone(Call call) {
2070 if (call.getStartWithSpeakerphoneOn()) {
Yorke Lee2a66f7b2015-05-13 14:21:19 -07002071 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Santos Cordond6782cd2015-04-16 09:57:50 -07002072 call.setStartWithSpeakerphoneOn(false);
2073 }
2074 }
2075
2076 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002077 * Creates a new call for an existing connection.
2078 *
2079 * @param callId The id of the new call.
2080 * @param connection The connection information.
2081 * @return The new call.
2082 */
2083 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
Hall Liu4b9e5562016-07-14 15:34:56 -07002084 boolean isDowngradedConference = (connection.getConnectionProperties()
2085 & Connection.PROPERTY_IS_DOWNGRADED_CONFERENCE) != 0;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002086 Call call = new Call(
Tyler Gunn234d5422015-12-08 14:27:49 -08002087 callId,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002088 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08002089 this,
Ihab Awade6dbc9d2015-03-26 10:33:44 -07002090 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002091 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07002092 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -07002093 mCallerInfoAsyncQueryFactory,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -07002094 mPhoneNumberUtilsAdapter,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002095 connection.getHandle() /* handle */,
2096 null /* gatewayInfo */,
2097 null /* connectionManagerPhoneAccount */,
2098 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
Hall Liu32587202015-11-18 11:10:08 -08002099 Call.CALL_DIRECTION_UNDEFINED /* callDirection */,
2100 false /* forceAttachToExistingConnection */,
Hall Liu4b9e5562016-07-14 15:34:56 -07002101 isDowngradedConference /* isConference */,
Roshan Pius084ab082015-07-15 12:17:04 -07002102 connection.getConnectTimeMillis() /* connectTimeMillis */);
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002103
Hall Liu32587202015-11-18 11:10:08 -08002104 call.initAnalytics();
2105 call.getAnalytics().setCreatedFromExistingConnection(true);
2106
Santos Cordon5fa4e4f2015-06-10 14:56:01 -07002107 setCallState(call, Call.getStateFromConnectionState(connection.getState()),
2108 "existing connection");
Ihab Awad07bc5ee2014-11-12 13:42:52 -08002109 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn571d5e62016-03-15 15:55:18 -07002110 call.setConnectionProperties(connection.getConnectionProperties());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002111 call.setCallerDisplayName(connection.getCallerDisplayName(),
2112 connection.getCallerDisplayNamePresentation());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002113 call.addListener(this);
Tyler Gunn9b618b82016-10-17 15:54:35 -07002114
2115 // In case this connection was added via a ConnectionManager, keep track of the original
2116 // Connection ID as created by the originating ConnectionService.
2117 Bundle extras = connection.getExtras();
2118 if (extras != null && extras.containsKey(Connection.EXTRA_ORIGINAL_CONNECTION_ID)) {
2119 call.setOriginalConnectionId(extras.getString(Connection.EXTRA_ORIGINAL_CONNECTION_ID));
2120 }
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07002121 addCall(call);
2122
2123 return call;
2124 }
2125
2126 /**
Tyler Gunn9b618b82016-10-17 15:54:35 -07002127 * Determines whether Telecom already knows about a Connection added via the
2128 * {@link android.telecom.ConnectionService#addExistingConnection(PhoneAccountHandle,
2129 * Connection)} API via a ConnectionManager.
2130 *
2131 * See {@link Connection#EXTRA_ORIGINAL_CONNECTION_ID}.
2132 * @param originalConnectionId The new connection ID to check.
2133 * @return {@code true} if this connection is already known by Telecom.
2134 */
2135 Call getAlreadyAddedConnection(String originalConnectionId) {
2136 Optional<Call> existingCall = mCalls.stream()
2137 .filter(call -> originalConnectionId.equals(call.getOriginalConnectionId()) ||
2138 originalConnectionId.equals(call.getId()))
2139 .findFirst();
2140
2141 if (existingCall.isPresent()) {
2142 Log.i(this, "isExistingConnectionAlreadyAdded - call %s already added with id %s",
2143 originalConnectionId, existingCall.get().getId());
2144 return existingCall.get();
2145 }
2146
2147 return null;
2148 }
2149
2150 /**
Tyler Gunn8452be02015-09-17 09:57:02 -07002151 * @return A new unique telecom call Id.
2152 */
2153 private String getNextCallId() {
2154 synchronized(mLock) {
2155 return TELECOM_CALL_ID_PREFIX + (++mCallId);
2156 }
2157 }
2158
2159 /**
Tony Maka9930942016-01-15 10:57:14 +00002160 * Callback when foreground user is switched. We will reload missed call in all profiles
2161 * including the user itself. There may be chances that profiles are not started yet.
2162 */
2163 void onUserSwitch(UserHandle userHandle) {
Santos Cordonf0f99f32016-02-18 16:13:57 -08002164 mCurrentUserHandle = userHandle;
Tony Maka9930942016-01-15 10:57:14 +00002165 mMissedCallNotifier.setCurrentUserHandle(userHandle);
2166 final UserManager userManager = UserManager.get(mContext);
2167 List<UserInfo> profiles = userManager.getEnabledProfiles(userHandle.getIdentifier());
2168 for (UserInfo profile : profiles) {
2169 reloadMissedCallsOfUser(profile.getUserHandle());
2170 }
2171 }
2172
2173 /**
2174 * Because there may be chances that profiles are not started yet though its parent user is
2175 * switched, we reload missed calls of profile that are just started here.
2176 */
2177 void onUserStarting(UserHandle userHandle) {
2178 if (UserUtil.isProfile(mContext, userHandle)) {
2179 reloadMissedCallsOfUser(userHandle);
2180 }
2181 }
2182
Hall Liuc9cf5442016-06-29 10:08:10 -07002183 public TelecomSystem.SyncRoot getLock() {
2184 return mLock;
2185 }
2186
Tony Maka9930942016-01-15 10:57:14 +00002187 private void reloadMissedCallsOfUser(UserHandle userHandle) {
2188 mMissedCallNotifier.reloadFromDatabase(
2189 mLock, this, mContactsAsyncHelper, mCallerInfoAsyncQueryFactory, userHandle);
2190 }
2191
2192 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002193 * Dumps the state of the {@link CallsManager}.
2194 *
2195 * @param pw The {@code IndentingPrintWriter} to write the state to.
2196 */
2197 public void dump(IndentingPrintWriter pw) {
2198 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002199 if (mCalls != null) {
2200 pw.println("mCalls: ");
2201 pw.increaseIndent();
2202 for (Call call : mCalls) {
2203 pw.println(call);
2204 }
2205 pw.decreaseIndent();
2206 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07002207
2208 if (mCallAudioManager != null) {
2209 pw.println("mCallAudioManager:");
2210 pw.increaseIndent();
2211 mCallAudioManager.dump(pw);
2212 pw.decreaseIndent();
2213 }
2214
2215 if (mTtyManager != null) {
2216 pw.println("mTtyManager:");
2217 pw.increaseIndent();
2218 mTtyManager.dump(pw);
2219 pw.decreaseIndent();
2220 }
2221
2222 if (mInCallController != null) {
2223 pw.println("mInCallController:");
2224 pw.increaseIndent();
2225 mInCallController.dump(pw);
2226 pw.decreaseIndent();
2227 }
2228
2229 if (mConnectionServiceRepository != null) {
2230 pw.println("mConnectionServiceRepository:");
2231 pw.increaseIndent();
2232 mConnectionServiceRepository.dump(pw);
2233 pw.decreaseIndent();
2234 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07002235 }
Xueren Zhangc0e8e9d2015-03-06 14:16:55 +01002236
2237 /**
2238 * For some disconnected causes, we show a dialog when it's a mmi code or potential mmi code.
2239 *
2240 * @param call The call.
2241 */
2242 private void maybeShowErrorDialogOnDisconnect(Call call) {
2243 if (call.getState() == CallState.DISCONNECTED && (isPotentialMMICode(call.getHandle())
2244 || isPotentialInCallMMICode(call.getHandle()))) {
2245 DisconnectCause disconnectCause = call.getDisconnectCause();
2246 if (!TextUtils.isEmpty(disconnectCause.getDescription()) && (disconnectCause.getCode()
2247 == DisconnectCause.ERROR)) {
2248 Intent errorIntent = new Intent(mContext, ErrorDialogActivity.class);
2249 errorIntent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_STRING_EXTRA,
2250 disconnectCause.getDescription());
2251 errorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2252 mContext.startActivityAsUser(errorIntent, UserHandle.CURRENT);
2253 }
2254 }
2255 }
Sailesh Nepal703a1af2016-04-14 20:10:12 -07002256
2257 private void setIntentExtrasAndStartTime(Call call, Bundle extras) {
2258 // Create our own instance to modify (since extras may be Bundle.EMPTY)
2259 extras = new Bundle(extras);
2260
2261 // Specifies the time telecom began routing the call. This is used by the dialer for
2262 // analytics.
2263 extras.putLong(TelecomManager.EXTRA_CALL_TELECOM_ROUTING_START_TIME_MILLIS,
2264 SystemClock.elapsedRealtime());
2265
2266 call.setIntentExtras(extras);
2267 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08002268}