blob: aaa075ab16fdcb2732dddd95611dbc2cd327bb1a [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Sailesh Nepalce704b92014-03-17 18:31:43 -070020import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
Santos Cordoncf5b2912014-11-05 21:57:54 -080022import android.os.Handler;
Yorke Leee4a9c412014-11-14 16:59:42 -080023import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070024import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070025import android.telecom.AudioState;
26import android.telecom.CallState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080027import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080028import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070029import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070030import android.telecom.GatewayInfo;
31import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070032import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070033import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070034import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070035import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070036import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080037import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070038import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080039
Ihab Awada3653902015-01-23 13:44:46 -080040import com.android.internal.annotations.VisibleForTesting;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070041import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080042
Santos Cordon4bc02452014-11-19 15:51:12 -080043import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070044import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070045import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070046import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070047import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070048import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070049import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080050
Ben Giladdd8c6082013-12-30 14:44:08 -080051/**
52 * Singleton.
53 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070054 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080055 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070056 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080057 */
Santos Cordon64c7e962014-07-02 15:15:27 -070058public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080059
Santos Cordondf399862014-08-06 04:39:15 -070060 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070061 interface CallsManagerListener {
62 void onCallAdded(Call call);
63 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070064 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070066 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070067 ConnectionServiceWrapper oldService,
68 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070069 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070070 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070071 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070072 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070073 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070074 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070075 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070076 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080077 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070078 }
79
Tyler Gunn91d43cf2014-09-17 12:19:39 -070080 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080081
Santos Cordonf193ba42014-09-12 06:37:39 -070082 private static final int MAXIMUM_LIVE_CALLS = 1;
83 private static final int MAXIMUM_HOLD_CALLS = 1;
84 private static final int MAXIMUM_RINGING_CALLS = 1;
85 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080086 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070087
88 private static final int[] OUTGOING_CALL_STATES =
89 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
90
Santos Cordon91bd74c2014-11-07 16:10:22 -080091 private static final int[] LIVE_CALL_STATES =
92 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
93
Santos Cordon8e8b8d22013-12-19 14:14:05 -080094 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070095 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
96 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -080097 *
Jay Shraunera82c8f72014-08-14 15:49:16 -070098 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
99 * load factor before resizing, 1 means we only expect a single thread to
100 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800101 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700102 private final Set<Call> mCalls = Collections.newSetFromMap(
103 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800104
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700105 private final ConnectionServiceRepository mConnectionServiceRepository;
106 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
107 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700108 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800109 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700110 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800111 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700112 // For this set initial table size to 16 because we add 13 listeners in
113 // the CallsManager constructor.
114 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
115 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700116 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700117 private final WiredHeadsetManager mWiredHeadsetManager;
118 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700119 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700120 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700121 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700122 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700123 private final TelecomSystem.SyncRoot mLock;
124 private final ContactsAsyncHelper mContactsAsyncHelper;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700125 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
126 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700127 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800128 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
129 /* Handler tied to thread in which CallManager was initialized. */
130 private final Handler mHandler = new Handler();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700131
Santos Cordon91bd74c2014-11-07 16:10:22 -0800132 private boolean mCanAddCall = true;
133
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700134 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 * The call the user is currently interacting with. This is the call that should have audio
136 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800137 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800139
Nancy Chena469f762014-12-09 18:29:20 -0800140 private Runnable mStopTone;
141
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700142 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700143 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800144 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700145 CallsManager(
146 Context context,
147 TelecomSystem.SyncRoot lock,
148 ContactsAsyncHelper contactsAsyncHelper,
149 MissedCallNotifier missedCallNotifier,
150 PhoneAccountRegistrar phoneAccountRegistrar,
151 HeadsetMediaButtonFactory headsetMediaButtonFactory,
152 ProximitySensorManagerFactory proximitySensorManagerFactory,
153 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700154 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700155 mLock = lock;
156 mContactsAsyncHelper = contactsAsyncHelper;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700157 mPhoneAccountRegistrar = phoneAccountRegistrar;
158 mMissedCallNotifier = missedCallNotifier;
159 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
160 mWiredHeadsetManager = new WiredHeadsetManager(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800161 mCallAudioManager = new CallAudioManager(
162 context, statusBarNotifier, mWiredHeadsetManager, this);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700163 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700164 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad8de76912015-02-17 12:25:52 -0800165 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700166 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800167 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
168 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700169 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700170 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700171 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800172 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700173 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800174 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700175
Santos Cordondeb8c892014-05-30 01:38:03 -0700176 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700177 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700178 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700179 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700180 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700181 mListeners.add(new RingbackPlayer(this, playerFactory));
182 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700183 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700184 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700185 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700186 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700187 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700188
189 mMissedCallNotifier.updateOnStartup(mLock, this, mContactsAsyncHelper);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800190 }
191
Ihab Awad8de76912015-02-17 12:25:52 -0800192 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
193 if (mRespondViaSmsManager != null) {
194 mListeners.remove(mRespondViaSmsManager);
195 }
196 mRespondViaSmsManager = respondViaSmsManager;
197 mListeners.add(respondViaSmsManager);
198 }
199
200 public RespondViaSmsManager getRespondViaSmsManager() {
201 return mRespondViaSmsManager;
202 }
203
Santos Cordon766d04f2014-05-06 10:28:25 -0700204 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700205 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700206 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700207
Tyler Gunncde2e502014-08-12 14:35:58 -0700208 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700209 if (!mCalls.contains(call)) {
210 // Call was not added previously in startOutgoingCall due to it being a potential MMI
211 // code, so add it now.
212 addCall(call);
213 }
214
215 // The call's ConnectionService has been updated.
216 for (CallsManagerListener listener : mListeners) {
217 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700218 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700219
220 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700221 }
222
223 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700224 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700225 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700226
Andrew Leee6288a72014-10-01 13:50:02 -0700227 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700228 }
229
230 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700231 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700232 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700233 setCallState(incomingCall, CallState.RINGING);
234
235 if (hasMaximumRingingCalls()) {
236 incomingCall.reject(false, null);
237 // since the call was not added to the list of calls, we have to call the missed
238 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700239 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700240 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
241 } else {
242 addCall(incomingCall);
243 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700244 }
245
246 @Override
247 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700248 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700249 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800250 }
251
Ihab Awadcb387ac2014-05-28 16:49:38 -0700252 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700253 public void onSuccessfulUnknownCall(Call call, int callState) {
254 setCallState(call, callState);
255 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
256 addCall(call);
257 }
258
259 @Override
260 public void onFailedUnknownCall(Call call) {
261 Log.i(this, "onFailedUnknownCall for call %s", call);
262 setCallState(call, CallState.DISCONNECTED);
263 call.removeListener(this);
264 }
265
266 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700267 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700268 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700269 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700270 }
271 }
272
Evan Charlton352105c2014-06-03 14:10:54 -0700273 @Override
274 public void onPostDialWait(Call call, String remaining) {
275 mInCallController.onPostDialWait(call, remaining);
276 }
277
Santos Cordona1610702014-06-04 20:22:56 -0700278 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800279 public void onPostDialChar(final Call call, char nextChar) {
280 if (PhoneNumberUtils.is12Key(nextChar)) {
281 // Play tone if it is one of the dialpad digits, canceling out the previously queued
282 // up stopTone runnable since playing a new tone automatically stops the previous tone.
283 if (mStopTone != null) {
284 mHandler.removeCallbacks(mStopTone);
285 }
286
287 mDtmfLocalTonePlayer.playTone(call, nextChar);
288
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700289 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800290 mStopTone = new Runnable() {
291 @Override
292 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700293 synchronized (mLock) {
294 // Set a timeout to stop the tone in case there isn't another tone to follow.
295 mDtmfLocalTonePlayer.stopTone(call);
296 }
Nancy Chena469f762014-12-09 18:29:20 -0800297 }
298 };
299 mHandler.postDelayed(
300 mStopTone,
301 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
302 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
303 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
304 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
305 // the previous tone is being stopped anyway.
306 if (mStopTone != null) {
307 mHandler.removeCallbacks(mStopTone);
308 }
309 mDtmfLocalTonePlayer.stopTone(call);
310 } else {
311 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
312 }
313 }
314
315 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700316 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700317 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800318 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700319 for (CallsManagerListener listener : mListeners) {
320 listener.onIsConferencedChanged(call);
321 }
322 }
323
324 @Override
325 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700326 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800327 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700328 for (CallsManagerListener listener : mListeners) {
329 listener.onIsConferencedChanged(call);
330 }
331 }
332
Ihab Awadff7493a2014-06-10 13:47:44 -0700333 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700334 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700335 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700336 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700337 }
338 }
339
Andrew Lee4a796602014-07-11 17:23:03 -0700340 @Override
341 public void onVideoStateChanged(Call call) {
342 for (CallsManagerListener listener : mListeners) {
343 listener.onVideoStateChanged(call);
344 }
345 }
346
Santos Cordoncf5b2912014-11-05 21:57:54 -0800347 @Override
348 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
349 mPendingCallsToDisconnect.add(call);
350 mHandler.postDelayed(new Runnable() {
351 @Override
352 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700353 synchronized (mLock) {
354 if (mPendingCallsToDisconnect.remove(call)) {
355 Log.i(this, "Delayed disconnection of call: %s", call);
356 call.disconnect();
357 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800358 }
359 }
360 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
361
362 return true;
363 }
364
Santos Cordon4bc02452014-11-19 15:51:12 -0800365 Collection<Call> getCalls() {
366 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700367 }
368
369 Call getForegroundCall() {
370 return mForegroundCall;
371 }
372
Santos Cordonae193062014-05-21 21:21:49 -0700373 Ringer getRinger() {
374 return mRinger;
375 }
376
Santos Cordonf3671a62014-05-29 21:51:53 -0700377 InCallController getInCallController() {
378 return mInCallController;
379 }
380
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700381 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700382 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700383 if (call.isEmergencyCall()) {
384 return true;
385 }
386 }
387 return false;
388 }
389
Andrew Lee45506232014-10-22 17:54:33 -0700390 boolean hasVideoCall() {
391 for (Call call : mCalls) {
392 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
393 return true;
394 }
395 }
396 return false;
397 }
398
Ihab Awad6fb37c82014-08-07 19:48:57 -0700399 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700400 return mCallAudioManager.getAudioState();
401 }
402
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700403 boolean isTtySupported() {
404 return mTtyManager.isTtySupported();
405 }
406
407 int getCurrentTtyMode() {
408 return mTtyManager.getCurrentTtyMode();
409 }
410
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700411 void addListener(CallsManagerListener listener) {
412 mListeners.add(listener);
413 }
414
415 void removeListener(CallsManagerListener listener) {
416 mListeners.remove(listener);
417 }
418
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800419 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700420 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800421 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700422 * @param phoneAccountHandle The phone account which contains the component name of the
423 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800424 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800425 */
Evan Charlton89176372014-07-19 18:23:09 -0700426 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800427 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700428 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700429 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700430 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800431 this,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700432 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700433 mContactsAsyncHelper,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700434 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700435 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700436 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700437 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700438 true /* isIncoming */,
439 false /* isConference */);
440
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700441 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700442 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700443 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700444 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800445 }
446
Yorke Lee9250e5f2014-10-01 13:39:09 -0700447 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
448 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
449 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
450 Call call = new Call(
451 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800452 this,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700453 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700454 mContactsAsyncHelper,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700455 handle,
456 null /* gatewayInfo */,
457 null /* connectionManagerPhoneAccount */,
458 phoneAccountHandle,
459 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
460 // to the existing connection instead of trying to create a new one.
461 true /* isIncoming */,
462 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700463 call.setIsUnknown(true);
464 call.setExtras(extras);
465 call.addListener(this);
466 call.startCreateConnection(mPhoneAccountRegistrar);
467 }
468
Santos Cordoncf5b2912014-11-05 21:57:54 -0800469 private Call getNewOutgoingCall(Uri handle) {
470 // First check to see if we can reuse any of the calls that are waiting to disconnect.
471 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800472 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800473 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800474 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800475 mPendingCallsToDisconnect.remove(pendingCall);
476 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800477 reusedCall = pendingCall;
478 } else {
479 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800480 }
481 }
Yorke Lee59979512014-12-02 01:03:40 -0800482 if (reusedCall != null) {
483 return reusedCall;
484 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800485
486 // Create a call with original handle. The handle may be changed when the call is attached
487 // to a connection service, but in most cases will remain the same.
488 return new Call(
489 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800490 this,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800491 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700492 mContactsAsyncHelper,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800493 handle,
494 null /* gatewayInfo */,
495 null /* connectionManagerPhoneAccount */,
496 null /* phoneAccountHandle */,
497 false /* isIncoming */,
498 false /* isConference */);
499 }
500
Nancy Chen0d3076c2014-07-30 14:45:44 -0700501 /**
502 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
503 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700504 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700505 * @param phoneAccountHandle The phone account which contains the component name of the
506 * connection service to use for this call.
507 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700508 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700509 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800510 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700511
512 List<PhoneAccountHandle> accounts =
513 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
514
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800515 Log.v(this, "startOutgoingCall found accounts = " + accounts);
516
Nancy Chenbc95db92014-11-24 13:19:18 -0800517 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
518 // If there is an ongoing call, use the same phone account to place this new call.
519 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
520 }
521
Nancy Chenbc9ef172014-08-15 17:11:57 -0700522 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
523 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700524 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700525 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700526 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700527 phoneAccountHandle = null;
528 }
529 }
530
531 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700532 // No preset account, check if default exists that supports the URI scheme for the
533 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700534 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700535 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700536 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700537 if (defaultAccountHandle != null) {
538 phoneAccountHandle = defaultAccountHandle;
539 }
540 }
541
Nancy Chen1c5926f2014-09-17 14:44:14 -0700542 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700543
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700544 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
545 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700546 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700547
548 // Do not support any more live calls. Our options are to move a call to hold, disconnect
549 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700550 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700551 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800552 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800553 if (mCalls.contains(call)) {
554 // This call can already exist if it is a reused call,
555 // See {@link #getNewOutgoingCall}.
556 call.disconnect();
557 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700558 return null;
559 }
560
Nancy Chen8d92f452014-10-20 21:43:44 -0700561 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
562 !isEmergencyCall;
563
564 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700565 // This is the state where the user is expected to select an account
566 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700567 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700568 } else {
569 call.setState(CallState.CONNECTING);
570 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700571
Nancy Chen1c5926f2014-09-17 14:44:14 -0700572 call.setExtras(extras);
573
Tyler Gunn504eb832014-10-13 14:15:27 -0700574 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700575 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700576 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800577 } else if (!mCalls.contains(call)) {
578 // We check if mCalls already contains the call because we could potentially be reusing
579 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700580 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700581 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700582
583 return call;
584 }
585
Ben Gilada0d9f752014-02-26 11:49:03 -0800586 /**
Yorke Lee33501632014-03-17 19:24:12 -0700587 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800588 *
Yorke Lee33501632014-03-17 19:24:12 -0700589 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700590 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700591 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700592 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700593 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800594 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700595 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
596 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700597 if (call == null) {
598 // don't do anything if the call no longer exists
599 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700600 return;
601 }
602
Nancy Chen201b4372014-09-08 14:18:24 -0700603 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700604
605 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700606 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700607 } else {
608 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
609 Log.pii(uriHandle), Log.pii(handle));
610 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700611
Nancy Chen0d3076c2014-07-30 14:45:44 -0700612 call.setHandle(uriHandle);
613 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700614 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700615 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700616
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700617 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
618 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700619 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700620 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700621 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700622 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700623
Santos Cordonf193ba42014-09-12 06:37:39 -0700624 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700625 // If the account has been set, proceed to place the outgoing call.
626 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700627 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700628 }
Yorke Leef98fb572014-03-05 10:56:55 -0800629 }
630
631 /**
Santos Cordona1610702014-06-04 20:22:56 -0700632 * Attempts to start a conference call for the specified call.
633 *
Santos Cordon12d61822014-07-29 16:02:20 -0700634 * @param call The call to conference.
635 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700636 */
Santos Cordon12d61822014-07-29 16:02:20 -0700637 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700638 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700639 }
640
641 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700642 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
643 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800644 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700645 *
646 * @param call The call to answer.
647 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800648 */
Andrew Lee38931d02014-07-16 10:17:36 -0700649 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700650 if (!mCalls.contains(call)) {
651 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800652 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700653 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700654 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700655 if (mForegroundCall != null && mForegroundCall != call &&
656 (mForegroundCall.isActive() ||
657 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800658 if (0 == (mForegroundCall.getConnectionCapabilities()
659 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700660 // This call does not support hold. If it is from a different connection
661 // service, then disconnect it, otherwise allow the connection service to
662 // figure out the right states.
663 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
664 mForegroundCall.disconnect();
665 }
666 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800667 Call heldCall = getHeldCall();
668 if (heldCall != null) {
669 Log.v(this, "Disconnecting held call %s before holding active call.",
670 heldCall);
671 heldCall.disconnect();
672 }
673
Santos Cordonf193ba42014-09-12 06:37:39 -0700674 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
675 mForegroundCall, call);
676 mForegroundCall.hold();
677 }
Santos Cordondf399862014-08-06 04:39:15 -0700678 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700679 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700680 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700681 }
682
Santos Cordona56f2762014-03-24 15:55:53 -0700683 for (CallsManagerListener listener : mListeners) {
684 listener.onIncomingCallAnswered(call);
685 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800686
Santos Cordon61d0f702014-02-19 02:52:23 -0800687 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700688 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700689 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800690 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800691 }
692
693 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700694 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
695 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800696 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800697 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700698 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700699 if (!mCalls.contains(call)) {
700 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800701 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700702 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700703 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700704 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700705 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800706 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800707 }
708
709 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700710 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700711 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700712 * @param digit The DTMF digit to play.
713 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700714 void playDtmfTone(Call call, char digit) {
715 if (!mCalls.contains(call)) {
716 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700717 } else {
718 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700719 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700720 }
721 }
722
723 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700724 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700725 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700726 void stopDtmfTone(Call call) {
727 if (!mCalls.contains(call)) {
728 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700729 } else {
730 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700731 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700732 }
733 }
734
735 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700736 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700737 */
Evan Charlton352105c2014-06-03 14:10:54 -0700738 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700739 if (!mCalls.contains(call)) {
740 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700741 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700742 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700743 }
744 }
745
746 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700747 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800748 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
749 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800750 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700751 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700752 Log.v(this, "disconnectCall %s", call);
753
Sailesh Nepale59bb192014-04-01 18:33:59 -0700754 if (!mCalls.contains(call)) {
755 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800756 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700757 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800758 call.disconnect();
759 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800760 }
Santos Cordon681663d2014-01-30 04:32:15 -0800761
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700762 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700763 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700764 */
765 void disconnectAllCalls() {
766 Log.v(this, "disconnectAllCalls");
767
768 for (Call call : mCalls) {
769 disconnectCall(call);
770 }
771 }
772
Nancy Chenbc9ef172014-08-15 17:11:57 -0700773
Tyler Gunn61b92102014-08-19 07:42:20 -0700774 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700775 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700776 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
777 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700778 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700779 void holdCall(Call call) {
780 if (!mCalls.contains(call)) {
781 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700782 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700783 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700784 call.hold();
785 }
786 }
787
788 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700789 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700790 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
791 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700792 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700793 void unholdCall(Call call) {
794 if (!mCalls.contains(call)) {
795 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700796 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700797 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700798 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800799 // Only attempt to hold parent calls and not the individual children.
800 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700801 c.hold();
802 }
803 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700804 call.unhold();
805 }
806 }
807
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700808 /** Called by the in-call UI to change the mute state. */
809 void mute(boolean shouldMute) {
810 mCallAudioManager.mute(shouldMute);
811 }
812
813 /**
814 * Called by the in-call UI to change the audio route, for example to change from earpiece to
815 * speaker phone.
816 */
817 void setAudioRoute(int route) {
818 mCallAudioManager.setAudioRoute(route);
819 }
820
Yorke Leed1346872014-07-28 14:38:45 -0700821 /** Called by the in-call UI to turn the proximity sensor on. */
822 void turnOnProximitySensor() {
823 mProximitySensorManager.turnOn();
824 }
825
826 /**
827 * Called by the in-call UI to turn the proximity sensor off.
828 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
829 * the screen will be kept off until the proximity sensor goes negative.
830 */
831 void turnOffProximitySensor(boolean screenOnImmediately) {
832 mProximitySensorManager.turnOff(screenOnImmediately);
833 }
834
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700835 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700836 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700837 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700838 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700839 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
840 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
841 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
842 // respecting a rewritten number or a canceled number. This is unlikely since
843 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
844 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700845 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700846
847 // Note: emergency calls never go through account selection dialog so they never
848 // arrive here.
849 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700850 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700851 } else {
852 call.disconnect();
853 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700854
855 if (setDefault) {
856 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
857 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700858 }
859 }
860
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700861 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700862 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700863 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700864 for (CallsManagerListener listener : mListeners) {
865 listener.onAudioStateChanged(oldAudioState, newAudioState);
866 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700867 }
868
Sailesh Nepale59bb192014-04-01 18:33:59 -0700869 void markCallAsRinging(Call call) {
870 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800871 }
872
Sailesh Nepale59bb192014-04-01 18:33:59 -0700873 void markCallAsDialing(Call call) {
874 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800875 }
876
Sailesh Nepale59bb192014-04-01 18:33:59 -0700877 void markCallAsActive(Call call) {
878 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700879
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700880 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700881 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700882 }
Santos Cordon681663d2014-01-30 04:32:15 -0800883 }
884
Sailesh Nepale59bb192014-04-01 18:33:59 -0700885 void markCallAsOnHold(Call call) {
886 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700887 }
888
Santos Cordon049b7b62014-01-30 05:34:26 -0800889 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700890 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
891 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800892 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800893 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800894 */
Andrew Lee701dc002014-09-11 21:29:12 -0700895 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
896 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700897 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700898 }
899
Ben Gilada0d9f752014-02-26 11:49:03 -0800900 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700901 * Removes an existing disconnected call, and notifies the in-call app.
902 */
903 void markCallAsRemoved(Call call) {
904 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700905 if (mLocallyDisconnectingCalls.contains(call)) {
906 mLocallyDisconnectingCalls.remove(call);
907 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
908 mForegroundCall.unhold();
909 }
910 }
Ihab Awada02bef52014-08-13 18:18:42 -0700911 }
912
913 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700914 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700915 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700916 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700917 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700918 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700919 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700920 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700921 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700922 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800923 if (call.getState() != CallState.DISCONNECTED) {
924 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
925 }
926 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700927 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700928 }
929 }
930 }
931
Santos Cordondeb8c892014-05-30 01:38:03 -0700932 boolean hasAnyCalls() {
933 return !mCalls.isEmpty();
934 }
935
Santos Cordonc7e85d42014-05-22 02:51:48 -0700936 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700937 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700938 }
939
940 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700941 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700942 }
943
Santos Cordondeb8c892014-05-30 01:38:03 -0700944 boolean onMediaButton(int type) {
945 if (hasAnyCalls()) {
946 if (HeadsetMediaButton.SHORT_PRESS == type) {
947 Call ringingCall = getFirstCallWithState(CallState.RINGING);
948 if (ringingCall == null) {
949 mCallAudioManager.toggleMute();
950 return true;
951 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700952 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700953 return true;
954 }
955 } else if (HeadsetMediaButton.LONG_PRESS == type) {
956 Log.d(this, "handleHeadsetHook: longpress -> hangup");
957 Call callToHangup = getFirstCallWithState(
958 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
959 if (callToHangup != null) {
960 callToHangup.disconnect();
961 return true;
962 }
963 }
964 }
965 return false;
966 }
967
968 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800969 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700970 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800971 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -0800972 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
973 return false;
974 }
975
Santos Cordon91bd74c2014-11-07 16:10:22 -0800976 int count = 0;
977 for (Call call : mCalls) {
978 if (call.isEmergencyCall()) {
979 // We never support add call if one of the calls is an emergency call.
980 return false;
981 } else if (call.getParentCall() == null) {
982 count++;
983 }
Santos Cordon10838c22014-06-11 17:36:04 -0700984
Santos Cordon91bd74c2014-11-07 16:10:22 -0800985 // We do not check states for canAddCall. We treat disconnected calls the same
986 // and wait until they are removed instead. If we didn't count disconnected calls,
987 // we could put InCallServices into a state where they are showing two calls but
988 // also support add-call. Technically it's right, but overall looks better (UI-wise)
989 // and acts better if we wait until the call is removed.
990 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700991 return false;
992 }
993 }
994 return true;
995 }
996
Ihab Awada3653902015-01-23 13:44:46 -0800997 @VisibleForTesting
998 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700999 return getFirstCallWithState(CallState.RINGING);
1000 }
1001
1002 Call getActiveCall() {
1003 return getFirstCallWithState(CallState.ACTIVE);
1004 }
1005
Nancy Chen05a9e402014-09-26 14:14:32 -07001006 Call getDialingCall() {
1007 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001008 }
1009
1010 Call getHeldCall() {
1011 return getFirstCallWithState(CallState.ON_HOLD);
1012 }
1013
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001014 int getNumHeldCalls() {
1015 int count = 0;
1016 for (Call call : mCalls) {
1017 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1018 count++;
1019 }
1020 }
1021 return count;
1022 }
1023
Santos Cordonf193ba42014-09-12 06:37:39 -07001024 Call getFirstCallWithState(int... states) {
1025 return getFirstCallWithState(null, states);
1026 }
1027
Santos Cordon10838c22014-06-11 17:36:04 -07001028 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001029 * Returns the first call that it finds with the given states. The states are treated as having
1030 * priority order so that any call with the first state will be returned before any call with
1031 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001032 *
1033 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001034 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001035 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001036 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001037 // check the foreground first
1038 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1039 return mForegroundCall;
1040 }
1041
Santos Cordondeb8c892014-05-30 01:38:03 -07001042 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001043 if (Objects.equals(callToSkip, call)) {
1044 continue;
1045 }
1046
1047 // Only operate on top-level calls
1048 if (call.getParentCall() != null) {
1049 continue;
1050 }
1051
Santos Cordondeb8c892014-05-30 01:38:03 -07001052 if (currentState == call.getState()) {
1053 return call;
1054 }
1055 }
1056 }
1057 return null;
1058 }
1059
Santos Cordon0fbe6322014-08-14 04:04:25 -07001060 Call createConferenceCall(
1061 PhoneAccountHandle phoneAccount,
1062 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001063
1064 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1065 // which is the default value for new Calls.
1066 long connectTime =
1067 parcelableConference.getConnectTimeMillis() ==
1068 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1069 parcelableConference.getConnectTimeMillis();
1070
Santos Cordon0fbe6322014-08-14 04:04:25 -07001071 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001072 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001073 this,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001074 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001075 mContactsAsyncHelper,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001076 null /* handle */,
1077 null /* gatewayInfo */,
1078 null /* connectionManagerPhoneAccount */,
1079 phoneAccount,
1080 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001081 true /* isConference */,
1082 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001083
1084 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001085 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001086
1087 // TODO: Move this to be a part of addCall()
1088 call.addListener(this);
1089 addCall(call);
1090 return call;
1091 }
1092
Yorke Leef86db2e2014-09-12 17:56:48 -07001093 /**
1094 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1095 */
1096 int getCallState() {
1097 return mPhoneStateBroadcaster.getCallState();
1098 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001099
Santos Cordon4b2c1192014-03-19 18:15:38 -07001100 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001101 * Retrieves the {@link PhoneAccountRegistrar}.
1102 *
1103 * @return The {@link PhoneAccountRegistrar}.
1104 */
1105 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1106 return mPhoneAccountRegistrar;
1107 }
1108
1109 /**
1110 * Retrieves the {@link MissedCallNotifier}
1111 * @return The {@link MissedCallNotifier}.
1112 */
1113 MissedCallNotifier getMissedCallNotifier() {
1114 return mMissedCallNotifier;
1115 }
1116
1117 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001118 * Adds the specified call to the main list of live calls.
1119 *
1120 * @param call The call to add.
1121 */
1122 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001123 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001124 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001125 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001126 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001127
Santos Cordondf399862014-08-06 04:39:15 -07001128 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001129 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001130 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001131 if (Log.SYSTRACE_DEBUG) {
1132 Trace.beginSection(listener.getClass().toString() + " addCall");
1133 }
Santos Cordona56f2762014-03-24 15:55:53 -07001134 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001135 if (Log.SYSTRACE_DEBUG) {
1136 Trace.endSection();
1137 }
Santos Cordona56f2762014-03-24 15:55:53 -07001138 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001139 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001140 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001141 }
1142
Sailesh Nepal810735e2014-03-18 18:15:46 -07001143 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001144 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001145 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001146
Santos Cordon672321e2014-08-21 14:38:58 -07001147 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001148 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001149 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001150
1151 boolean shouldNotify = false;
1152 if (mCalls.contains(call)) {
1153 mCalls.remove(call);
1154 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001155 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001156
Sailesh Nepale59bb192014-04-01 18:33:59 -07001157 // Only broadcast changes for calls that are being tracked.
1158 if (shouldNotify) {
1159 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001160 if (Log.SYSTRACE_DEBUG) {
1161 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1162 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001163 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001164 if (Log.SYSTRACE_DEBUG) {
1165 Trace.endSection();
1166 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001167 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001168 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001169 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001170 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001171 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001172
Sailesh Nepal810735e2014-03-18 18:15:46 -07001173 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001174 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001175 *
1176 * @param call The call.
1177 * @param newState The new state of the call.
1178 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001179 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001180 if (call == null) {
1181 return;
1182 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001183 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001184 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1185 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001186 if (newState != oldState) {
1187 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1188 // 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 -07001189 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001190 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001191 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001192 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001193 // unexpected transition occurs.
1194 call.setState(newState);
1195
Yorke Leee4a9c412014-11-14 16:59:42 -08001196 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001197 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001198 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001199 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001200 if (Log.SYSTRACE_DEBUG) {
1201 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1202 }
Santos Cordona56f2762014-03-24 15:55:53 -07001203 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001204 if (Log.SYSTRACE_DEBUG) {
1205 Trace.endSection();
1206 }
Santos Cordona56f2762014-03-24 15:55:53 -07001207 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001208 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001209 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001210 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001211 }
1212 }
1213
1214 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001215 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001216 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001217 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001218 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001219 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001220 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001221 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001222 // of its state will be foreground by default and instead the connection service should
1223 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001224 // the call should play audio and listen to microphone if it wants.
1225
Santos Cordonf193ba42014-09-12 06:37:39 -07001226 // Only top-level calls can be in foreground
1227 if (call.getParentCall() != null) {
1228 continue;
1229 }
1230
Santos Cordon40f78c22014-04-07 02:11:42 -07001231 // Active calls have priority.
1232 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001233 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001234 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001235 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001236
1237 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001238 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001239 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001240 }
1241 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001242
Sailesh Nepal810735e2014-03-18 18:15:46 -07001243 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001244 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001245 Call oldForegroundCall = mForegroundCall;
1246 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001247
Santos Cordona56f2762014-03-24 15:55:53 -07001248 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001249 if (Log.SYSTRACE_DEBUG) {
1250 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1251 }
Santos Cordona56f2762014-03-24 15:55:53 -07001252 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001253 if (Log.SYSTRACE_DEBUG) {
1254 Trace.endSection();
1255 }
Santos Cordona56f2762014-03-24 15:55:53 -07001256 }
Santos Cordon681663d2014-01-30 04:32:15 -08001257 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001258 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001259 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001260
Santos Cordon91bd74c2014-11-07 16:10:22 -08001261 private void updateCanAddCall() {
1262 boolean newCanAddCall = canAddCall();
1263 if (newCanAddCall != mCanAddCall) {
1264 mCanAddCall = newCanAddCall;
1265 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001266 if (Log.SYSTRACE_DEBUG) {
1267 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1268 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001269 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001270 if (Log.SYSTRACE_DEBUG) {
1271 Trace.endSection();
1272 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001273 }
1274 }
1275 }
1276
1277 private void updateCallsManagerState() {
1278 updateForegroundCall();
1279 updateCanAddCall();
1280 }
1281
Yorke Leeb701f6d2014-08-12 14:04:19 -07001282 private boolean isPotentialMMICode(Uri handle) {
1283 return (handle != null && handle.getSchemeSpecificPart() != null
1284 && handle.getSchemeSpecificPart().contains("#"));
1285 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001286
Tyler Gunn5b452df2014-10-01 15:02:58 -07001287 /**
1288 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1289 * MMI codes which can be dialed when one or more calls are in progress.
1290 * <P>
1291 * Checks for numbers formatted similar to the MMI codes defined in:
1292 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1293 * and
1294 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1295 *
1296 * @param handle The URI to call.
1297 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1298 */
1299 private boolean isPotentialInCallMMICode(Uri handle) {
1300 if (handle != null && handle.getSchemeSpecificPart() != null &&
1301 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1302
1303 String dialedNumber = handle.getSchemeSpecificPart();
1304 return (dialedNumber.equals("0") ||
1305 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1306 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1307 dialedNumber.equals("3") ||
1308 dialedNumber.equals("4") ||
1309 dialedNumber.equals("5"));
1310 }
1311 return false;
1312 }
1313
Santos Cordonf193ba42014-09-12 06:37:39 -07001314 private int getNumCallsWithState(int... states) {
1315 int count = 0;
1316 for (int state : states) {
1317 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001318 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001319 count++;
1320 }
1321 }
1322 }
1323 return count;
1324 }
1325
1326 private boolean hasMaximumLiveCalls() {
1327 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1328 }
1329
1330 private boolean hasMaximumHoldingCalls() {
1331 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1332 }
1333
1334 private boolean hasMaximumRingingCalls() {
1335 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1336 }
1337
1338 private boolean hasMaximumOutgoingCalls() {
1339 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1340 }
1341
1342 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1343 if (hasMaximumLiveCalls()) {
1344 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1345 // have to change.
1346 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301347 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1348 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001349
Santos Cordond1766502014-09-26 15:45:39 -07001350 if (call == liveCall) {
1351 // If the call is already the foreground call, then we are golden.
1352 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1353 // state since the call was already populated into the list.
1354 return true;
1355 }
1356
Andrew Leee6595182014-09-23 18:43:05 -07001357 if (hasMaximumOutgoingCalls()) {
1358 // Disconnect the current outgoing call if it's not an emergency call. If the user
1359 // tries to make two outgoing calls to different emergency call numbers, we will try
1360 // to connect the first outgoing call.
1361 if (isEmergency) {
1362 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1363 if (!outgoingCall.isEmergencyCall()) {
1364 outgoingCall.disconnect();
1365 return true;
1366 }
1367 }
1368 return false;
1369 }
1370
Santos Cordonf193ba42014-09-12 06:37:39 -07001371 if (hasMaximumHoldingCalls()) {
1372 // There is no more room for any more calls, unless it's an emergency.
1373 if (isEmergency) {
1374 // Kill the current active call, this is easier then trying to disconnect a
1375 // holding call and hold an active call.
1376 liveCall.disconnect();
1377 return true;
1378 }
1379 return false; // No more room!
1380 }
1381
1382 // We have room for at least one more holding call at this point.
1383
1384 // First thing, if we are trying to make a call with the same phone account as the live
1385 // call, then allow it so that the connection service can make its own decision about
1386 // how to handle the new call relative to the current one.
1387 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1388 return true;
1389 } else if (call.getTargetPhoneAccount() == null) {
1390 // Without a phone account, we can't say reliably that the call will fail.
1391 // If the user chooses the same phone account as the live call, then it's
1392 // still possible that the call can be made (like with CDMA calls not supporting
1393 // hold but they still support adding a call by going immediately into conference
1394 // mode). Return true here and we'll run this code again after user chooses an
1395 // account.
1396 return true;
1397 }
1398
1399 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001400 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001401 liveCall.hold();
1402 return true;
1403 }
1404
1405 // The live call cannot be held so we're out of luck here. There's no room.
1406 return false;
1407 }
1408 return true;
1409 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001410
1411 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001412 * Creates a new call for an existing connection.
1413 *
1414 * @param callId The id of the new call.
1415 * @param connection The connection information.
1416 * @return The new call.
1417 */
1418 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1419 Call call = new Call(
1420 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001421 this,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001422 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001423 mContactsAsyncHelper,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001424 connection.getHandle() /* handle */,
1425 null /* gatewayInfo */,
1426 null /* connectionManagerPhoneAccount */,
1427 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1428 false /* isIncoming */,
1429 false /* isConference */);
1430
1431 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001432 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001433 call.setCallerDisplayName(connection.getCallerDisplayName(),
1434 connection.getCallerDisplayNamePresentation());
1435
1436 call.addListener(this);
1437 addCall(call);
1438
1439 return call;
1440 }
1441
1442 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001443 * Dumps the state of the {@link CallsManager}.
1444 *
1445 * @param pw The {@code IndentingPrintWriter} to write the state to.
1446 */
1447 public void dump(IndentingPrintWriter pw) {
1448 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001449 if (mCalls != null) {
1450 pw.println("mCalls: ");
1451 pw.increaseIndent();
1452 for (Call call : mCalls) {
1453 pw.println(call);
1454 }
1455 pw.decreaseIndent();
1456 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001457 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1458
1459 if (mCallAudioManager != null) {
1460 pw.println("mCallAudioManager:");
1461 pw.increaseIndent();
1462 mCallAudioManager.dump(pw);
1463 pw.decreaseIndent();
1464 }
1465
1466 if (mTtyManager != null) {
1467 pw.println("mTtyManager:");
1468 pw.increaseIndent();
1469 mTtyManager.dump(pw);
1470 pw.decreaseIndent();
1471 }
1472
1473 if (mInCallController != null) {
1474 pw.println("mInCallController:");
1475 pw.increaseIndent();
1476 mInCallController.dump(pw);
1477 pw.decreaseIndent();
1478 }
1479
1480 if (mConnectionServiceRepository != null) {
1481 pw.println("mConnectionServiceRepository:");
1482 pw.increaseIndent();
1483 mConnectionServiceRepository.dump(pw);
1484 pw.decreaseIndent();
1485 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001486 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001487}