blob: 52dd55d04918bc3ce9ea772de8dbcf6cb8c8d0af [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Sailesh Nepalce704b92014-03-17 18:31:43 -070020import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
Santos Cordoncf5b2912014-11-05 21:57:54 -080022import android.os.Handler;
Ihab Awadd1d63e12015-03-26 10:33:44 -070023import android.os.Looper;
Yorke Leee4a9c412014-11-14 16:59:42 -080024import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070025import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070026import android.telecom.AudioState;
27import android.telecom.CallState;
Tyler Gunnd92e7372015-01-09 15:59:45 -080028import android.telecom.Conference;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080029import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070030import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070031import android.telecom.GatewayInfo;
32import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070033import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070035import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070036import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070037import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080038import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070039import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080040
Ihab Awada3653902015-01-23 13:44:46 -080041import com.android.internal.annotations.VisibleForTesting;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070042import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080043
Santos Cordon4bc02452014-11-19 15:51:12 -080044import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070045import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070046import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070047import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070048import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070049import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070050import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080051
Ben Giladdd8c6082013-12-30 14:44:08 -080052/**
53 * Singleton.
54 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070055 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080056 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070057 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080058 */
Santos Cordon64c7e962014-07-02 15:15:27 -070059public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080060
Santos Cordondf399862014-08-06 04:39:15 -070061 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070062 interface CallsManagerListener {
63 void onCallAdded(Call call);
64 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070065 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070066 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070067 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070068 ConnectionServiceWrapper oldService,
69 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070070 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070071 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070072 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070073 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070074 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070075 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070076 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070077 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080078 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070079 }
80
Tyler Gunn91d43cf2014-09-17 12:19:39 -070081 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080082
Santos Cordonf193ba42014-09-12 06:37:39 -070083 private static final int MAXIMUM_LIVE_CALLS = 1;
84 private static final int MAXIMUM_HOLD_CALLS = 1;
85 private static final int MAXIMUM_RINGING_CALLS = 1;
86 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080087 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070088
89 private static final int[] OUTGOING_CALL_STATES =
90 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
91
Santos Cordon91bd74c2014-11-07 16:10:22 -080092 private static final int[] LIVE_CALL_STATES =
93 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
94
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070096 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
97 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -080098 *
Jay Shraunera82c8f72014-08-14 15:49:16 -070099 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
100 * load factor before resizing, 1 means we only expect a single thread to
101 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800102 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700103 private final Set<Call> mCalls = Collections.newSetFromMap(
104 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800105
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700106 private final ConnectionServiceRepository mConnectionServiceRepository;
107 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
108 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700109 private final CallAudioManager mCallAudioManager;
Ihab Awad8de76912015-02-17 12:25:52 -0800110 private RespondViaSmsManager mRespondViaSmsManager;
Santos Cordonf3671a62014-05-29 21:51:53 -0700111 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800112 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700113 // For this set initial table size to 16 because we add 13 listeners in
114 // the CallsManager constructor.
115 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
116 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700117 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700118 private final WiredHeadsetManager mWiredHeadsetManager;
119 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700120 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700121 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700122 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700123 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700124 private final TelecomSystem.SyncRoot mLock;
125 private final ContactsAsyncHelper mContactsAsyncHelper;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700126 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
127 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700128 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800129 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
130 /* Handler tied to thread in which CallManager was initialized. */
Ihab Awadd1d63e12015-03-26 10:33:44 -0700131 private final Handler mHandler = new Handler(Looper.getMainLooper());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700132
Santos Cordon91bd74c2014-11-07 16:10:22 -0800133 private boolean mCanAddCall = true;
134
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700135 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700136 * The call the user is currently interacting with. This is the call that should have audio
137 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800138 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700139 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800140
Nancy Chena469f762014-12-09 18:29:20 -0800141 private Runnable mStopTone;
142
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700143 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700144 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 */
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700146 CallsManager(
147 Context context,
148 TelecomSystem.SyncRoot lock,
149 ContactsAsyncHelper contactsAsyncHelper,
150 MissedCallNotifier missedCallNotifier,
151 PhoneAccountRegistrar phoneAccountRegistrar,
152 HeadsetMediaButtonFactory headsetMediaButtonFactory,
153 ProximitySensorManagerFactory proximitySensorManagerFactory,
154 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700155 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700156 mLock = lock;
157 mContactsAsyncHelper = contactsAsyncHelper;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700158 mPhoneAccountRegistrar = phoneAccountRegistrar;
159 mMissedCallNotifier = missedCallNotifier;
160 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
161 mWiredHeadsetManager = new WiredHeadsetManager(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800162 mCallAudioManager = new CallAudioManager(
163 context, statusBarNotifier, mWiredHeadsetManager, this);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700164 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700165 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
Ihab Awad8de76912015-02-17 12:25:52 -0800166 mHeadsetMediaButton = headsetMediaButtonFactory.create(context, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700167 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
Ihab Awad8de76912015-02-17 12:25:52 -0800168 mProximitySensorManager = proximitySensorManagerFactory.create(context, this);
169 mPhoneStateBroadcaster = new PhoneStateBroadcaster(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700170 mCallLogManager = new CallLogManager(context);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700171 mInCallController = new InCallController(context, mLock, this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700172 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
Ihab Awad8de76912015-02-17 12:25:52 -0800173 mConnectionServiceRepository =
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700174 new ConnectionServiceRepository(mPhoneAccountRegistrar, mContext, mLock, this);
Ihab Awad8de76912015-02-17 12:25:52 -0800175 mInCallWakeLockController = inCallWakeLockControllerFactory.create(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700176
Santos Cordondeb8c892014-05-30 01:38:03 -0700177 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700178 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700179 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700180 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700181 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700182 mListeners.add(new RingbackPlayer(this, playerFactory));
183 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700184 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700185 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700186 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700187 mListeners.add(mHeadsetMediaButton);
Yorke Leed1346872014-07-28 14:38:45 -0700188 mListeners.add(mProximitySensorManager);
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700189
190 mMissedCallNotifier.updateOnStartup(mLock, this, mContactsAsyncHelper);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800191 }
192
Ihab Awad8de76912015-02-17 12:25:52 -0800193 public void setRespondViaSmsManager(RespondViaSmsManager respondViaSmsManager) {
194 if (mRespondViaSmsManager != null) {
195 mListeners.remove(mRespondViaSmsManager);
196 }
197 mRespondViaSmsManager = respondViaSmsManager;
198 mListeners.add(respondViaSmsManager);
199 }
200
201 public RespondViaSmsManager getRespondViaSmsManager() {
202 return mRespondViaSmsManager;
203 }
204
Santos Cordon766d04f2014-05-06 10:28:25 -0700205 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700206 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700207 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700208
Tyler Gunncde2e502014-08-12 14:35:58 -0700209 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700210 if (!mCalls.contains(call)) {
211 // Call was not added previously in startOutgoingCall due to it being a potential MMI
212 // code, so add it now.
213 addCall(call);
214 }
215
216 // The call's ConnectionService has been updated.
217 for (CallsManagerListener listener : mListeners) {
218 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700219 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700220
221 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700222 }
223
224 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700225 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700226 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700227
Andrew Leee6288a72014-10-01 13:50:02 -0700228 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700229 }
230
231 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700232 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700233 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700234 setCallState(incomingCall, CallState.RINGING);
235
236 if (hasMaximumRingingCalls()) {
237 incomingCall.reject(false, null);
238 // since the call was not added to the list of calls, we have to call the missed
239 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700240 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700241 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
242 } else {
243 addCall(incomingCall);
244 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700245 }
246
247 @Override
248 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700249 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700250 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800251 }
252
Ihab Awadcb387ac2014-05-28 16:49:38 -0700253 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700254 public void onSuccessfulUnknownCall(Call call, int callState) {
255 setCallState(call, callState);
256 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
257 addCall(call);
258 }
259
260 @Override
261 public void onFailedUnknownCall(Call call) {
262 Log.i(this, "onFailedUnknownCall for call %s", call);
263 setCallState(call, CallState.DISCONNECTED);
264 call.removeListener(this);
265 }
266
267 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700268 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700269 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700270 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700271 }
272 }
273
Evan Charlton352105c2014-06-03 14:10:54 -0700274 @Override
275 public void onPostDialWait(Call call, String remaining) {
276 mInCallController.onPostDialWait(call, remaining);
277 }
278
Santos Cordona1610702014-06-04 20:22:56 -0700279 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800280 public void onPostDialChar(final Call call, char nextChar) {
281 if (PhoneNumberUtils.is12Key(nextChar)) {
282 // Play tone if it is one of the dialpad digits, canceling out the previously queued
283 // up stopTone runnable since playing a new tone automatically stops the previous tone.
284 if (mStopTone != null) {
285 mHandler.removeCallbacks(mStopTone);
286 }
287
288 mDtmfLocalTonePlayer.playTone(call, nextChar);
289
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700290 // TODO: Create a LockedRunnable class that does the synchronization automatically.
Nancy Chena469f762014-12-09 18:29:20 -0800291 mStopTone = new Runnable() {
292 @Override
293 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700294 synchronized (mLock) {
295 // Set a timeout to stop the tone in case there isn't another tone to follow.
296 mDtmfLocalTonePlayer.stopTone(call);
297 }
Nancy Chena469f762014-12-09 18:29:20 -0800298 }
299 };
300 mHandler.postDelayed(
301 mStopTone,
302 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
303 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
304 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
305 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
306 // the previous tone is being stopped anyway.
307 if (mStopTone != null) {
308 mHandler.removeCallbacks(mStopTone);
309 }
310 mDtmfLocalTonePlayer.stopTone(call);
311 } else {
312 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
313 }
314 }
315
316 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700317 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700318 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800319 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700320 for (CallsManagerListener listener : mListeners) {
321 listener.onIsConferencedChanged(call);
322 }
323 }
324
325 @Override
326 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700327 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800328 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700329 for (CallsManagerListener listener : mListeners) {
330 listener.onIsConferencedChanged(call);
331 }
332 }
333
Ihab Awadff7493a2014-06-10 13:47:44 -0700334 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700335 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700336 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700337 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700338 }
339 }
340
Andrew Lee4a796602014-07-11 17:23:03 -0700341 @Override
342 public void onVideoStateChanged(Call call) {
343 for (CallsManagerListener listener : mListeners) {
344 listener.onVideoStateChanged(call);
345 }
346 }
347
Santos Cordoncf5b2912014-11-05 21:57:54 -0800348 @Override
349 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
350 mPendingCallsToDisconnect.add(call);
351 mHandler.postDelayed(new Runnable() {
352 @Override
353 public void run() {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700354 synchronized (mLock) {
355 if (mPendingCallsToDisconnect.remove(call)) {
356 Log.i(this, "Delayed disconnection of call: %s", call);
357 call.disconnect();
358 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800359 }
360 }
361 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
362
363 return true;
364 }
365
Santos Cordon4bc02452014-11-19 15:51:12 -0800366 Collection<Call> getCalls() {
367 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700368 }
369
370 Call getForegroundCall() {
371 return mForegroundCall;
372 }
373
Santos Cordonae193062014-05-21 21:21:49 -0700374 Ringer getRinger() {
375 return mRinger;
376 }
377
Santos Cordonf3671a62014-05-29 21:51:53 -0700378 InCallController getInCallController() {
379 return mInCallController;
380 }
381
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700382 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700383 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700384 if (call.isEmergencyCall()) {
385 return true;
386 }
387 }
388 return false;
389 }
390
Andrew Lee45506232014-10-22 17:54:33 -0700391 boolean hasVideoCall() {
392 for (Call call : mCalls) {
393 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
394 return true;
395 }
396 }
397 return false;
398 }
399
Ihab Awad6fb37c82014-08-07 19:48:57 -0700400 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700401 return mCallAudioManager.getAudioState();
402 }
403
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700404 boolean isTtySupported() {
405 return mTtyManager.isTtySupported();
406 }
407
408 int getCurrentTtyMode() {
409 return mTtyManager.getCurrentTtyMode();
410 }
411
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700412 void addListener(CallsManagerListener listener) {
413 mListeners.add(listener);
414 }
415
416 void removeListener(CallsManagerListener listener) {
417 mListeners.remove(listener);
418 }
419
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800420 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700421 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800422 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700423 * @param phoneAccountHandle The phone account which contains the component name of the
424 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800425 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800426 */
Evan Charlton89176372014-07-19 18:23:09 -0700427 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800428 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700429 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700430 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700431 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800432 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -0700433 mLock,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700434 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700435 mContactsAsyncHelper,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700436 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700437 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700438 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700439 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700440 true /* isIncoming */,
441 false /* isConference */);
442
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700443 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700444 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700445 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700446 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800447 }
448
Yorke Lee9250e5f2014-10-01 13:39:09 -0700449 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
450 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
451 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
452 Call call = new Call(
453 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800454 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -0700455 mLock,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700456 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700457 mContactsAsyncHelper,
Yorke Lee9250e5f2014-10-01 13:39:09 -0700458 handle,
459 null /* gatewayInfo */,
460 null /* connectionManagerPhoneAccount */,
461 phoneAccountHandle,
462 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
463 // to the existing connection instead of trying to create a new one.
464 true /* isIncoming */,
465 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700466 call.setIsUnknown(true);
467 call.setExtras(extras);
468 call.addListener(this);
469 call.startCreateConnection(mPhoneAccountRegistrar);
470 }
471
Santos Cordoncf5b2912014-11-05 21:57:54 -0800472 private Call getNewOutgoingCall(Uri handle) {
473 // First check to see if we can reuse any of the calls that are waiting to disconnect.
474 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800475 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800476 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800477 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800478 mPendingCallsToDisconnect.remove(pendingCall);
479 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800480 reusedCall = pendingCall;
481 } else {
482 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800483 }
484 }
Yorke Lee59979512014-12-02 01:03:40 -0800485 if (reusedCall != null) {
486 return reusedCall;
487 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800488
489 // Create a call with original handle. The handle may be changed when the call is attached
490 // to a connection service, but in most cases will remain the same.
491 return new Call(
492 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800493 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -0700494 mLock,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800495 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700496 mContactsAsyncHelper,
Santos Cordoncf5b2912014-11-05 21:57:54 -0800497 handle,
498 null /* gatewayInfo */,
499 null /* connectionManagerPhoneAccount */,
500 null /* phoneAccountHandle */,
501 false /* isIncoming */,
502 false /* isConference */);
503 }
504
Nancy Chen0d3076c2014-07-30 14:45:44 -0700505 /**
506 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
507 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700508 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700509 * @param phoneAccountHandle The phone account which contains the component name of the
510 * connection service to use for this call.
511 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700512 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700513 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800514 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700515
516 List<PhoneAccountHandle> accounts =
517 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
518
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800519 Log.v(this, "startOutgoingCall found accounts = " + accounts);
520
Nancy Chenbc95db92014-11-24 13:19:18 -0800521 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
522 // If there is an ongoing call, use the same phone account to place this new call.
523 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
524 }
525
Nancy Chenbc9ef172014-08-15 17:11:57 -0700526 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
527 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700528 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700529 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700530 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700531 phoneAccountHandle = null;
532 }
533 }
534
535 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700536 // No preset account, check if default exists that supports the URI scheme for the
537 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700538 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700539 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700540 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700541 if (defaultAccountHandle != null) {
542 phoneAccountHandle = defaultAccountHandle;
543 }
544 }
545
Nancy Chen1c5926f2014-09-17 14:44:14 -0700546 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700547
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700548 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
549 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700550 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700551
552 // Do not support any more live calls. Our options are to move a call to hold, disconnect
553 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700554 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700555 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800556 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800557 if (mCalls.contains(call)) {
558 // This call can already exist if it is a reused call,
559 // See {@link #getNewOutgoingCall}.
560 call.disconnect();
561 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700562 return null;
563 }
564
Nancy Chen8d92f452014-10-20 21:43:44 -0700565 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
566 !isEmergencyCall;
567
568 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700569 // This is the state where the user is expected to select an account
570 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700571 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700572 } else {
573 call.setState(CallState.CONNECTING);
574 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700575
Nancy Chen1c5926f2014-09-17 14:44:14 -0700576 call.setExtras(extras);
577
Tyler Gunn504eb832014-10-13 14:15:27 -0700578 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700579 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700580 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800581 } else if (!mCalls.contains(call)) {
582 // We check if mCalls already contains the call because we could potentially be reusing
583 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700584 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700585 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700586
587 return call;
588 }
589
Ben Gilada0d9f752014-02-26 11:49:03 -0800590 /**
Yorke Lee33501632014-03-17 19:24:12 -0700591 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800592 *
Yorke Lee33501632014-03-17 19:24:12 -0700593 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700594 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700595 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700596 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700597 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800598 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700599 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
600 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700601 if (call == null) {
602 // don't do anything if the call no longer exists
603 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700604 return;
605 }
606
Nancy Chen201b4372014-09-08 14:18:24 -0700607 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700608
609 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700610 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700611 } else {
612 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
613 Log.pii(uriHandle), Log.pii(handle));
614 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700615
Nancy Chen0d3076c2014-07-30 14:45:44 -0700616 call.setHandle(uriHandle);
617 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700618 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700619 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700620
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700621 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
622 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700623 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700624 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700625 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700626 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700627
Santos Cordonf193ba42014-09-12 06:37:39 -0700628 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700629 // If the account has been set, proceed to place the outgoing call.
630 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700631 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700632 }
Yorke Leef98fb572014-03-05 10:56:55 -0800633 }
634
635 /**
Santos Cordona1610702014-06-04 20:22:56 -0700636 * Attempts to start a conference call for the specified call.
637 *
Santos Cordon12d61822014-07-29 16:02:20 -0700638 * @param call The call to conference.
639 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700640 */
Santos Cordon12d61822014-07-29 16:02:20 -0700641 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700642 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700643 }
644
645 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700646 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
647 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800648 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700649 *
650 * @param call The call to answer.
651 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800652 */
Andrew Lee38931d02014-07-16 10:17:36 -0700653 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700654 if (!mCalls.contains(call)) {
655 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800656 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700657 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700658 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700659 if (mForegroundCall != null && mForegroundCall != call &&
660 (mForegroundCall.isActive() ||
661 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800662 if (0 == (mForegroundCall.getConnectionCapabilities()
663 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700664 // This call does not support hold. If it is from a different connection
665 // service, then disconnect it, otherwise allow the connection service to
666 // figure out the right states.
667 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
668 mForegroundCall.disconnect();
669 }
670 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800671 Call heldCall = getHeldCall();
672 if (heldCall != null) {
673 Log.v(this, "Disconnecting held call %s before holding active call.",
674 heldCall);
675 heldCall.disconnect();
676 }
677
Santos Cordonf193ba42014-09-12 06:37:39 -0700678 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
679 mForegroundCall, call);
680 mForegroundCall.hold();
681 }
Santos Cordondf399862014-08-06 04:39:15 -0700682 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700683 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700684 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700685 }
686
Santos Cordona56f2762014-03-24 15:55:53 -0700687 for (CallsManagerListener listener : mListeners) {
688 listener.onIncomingCallAnswered(call);
689 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800690
Santos Cordon61d0f702014-02-19 02:52:23 -0800691 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700692 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700693 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800694 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800695 }
696
697 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700698 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
699 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800700 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800701 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700702 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700703 if (!mCalls.contains(call)) {
704 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800705 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700706 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700707 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700708 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700709 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800710 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800711 }
712
713 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700714 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700715 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700716 * @param digit The DTMF digit to play.
717 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700718 void playDtmfTone(Call call, char digit) {
719 if (!mCalls.contains(call)) {
720 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700721 } else {
722 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700723 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700724 }
725 }
726
727 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700728 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700729 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700730 void stopDtmfTone(Call call) {
731 if (!mCalls.contains(call)) {
732 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700733 } else {
734 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700735 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700736 }
737 }
738
739 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700740 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700741 */
Evan Charlton352105c2014-06-03 14:10:54 -0700742 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700743 if (!mCalls.contains(call)) {
744 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700745 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700746 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700747 }
748 }
749
750 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700751 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800752 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
753 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800754 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700755 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700756 Log.v(this, "disconnectCall %s", call);
757
Sailesh Nepale59bb192014-04-01 18:33:59 -0700758 if (!mCalls.contains(call)) {
759 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800760 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700761 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800762 call.disconnect();
763 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800764 }
Santos Cordon681663d2014-01-30 04:32:15 -0800765
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700766 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700767 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700768 */
769 void disconnectAllCalls() {
770 Log.v(this, "disconnectAllCalls");
771
772 for (Call call : mCalls) {
773 disconnectCall(call);
774 }
775 }
776
Nancy Chenbc9ef172014-08-15 17:11:57 -0700777
Tyler Gunn61b92102014-08-19 07:42:20 -0700778 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700779 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700780 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
781 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700782 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700783 void holdCall(Call call) {
784 if (!mCalls.contains(call)) {
785 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700786 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700787 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700788 call.hold();
789 }
790 }
791
792 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700793 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700794 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
795 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700796 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700797 void unholdCall(Call call) {
798 if (!mCalls.contains(call)) {
799 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700800 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700801 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700802 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800803 // Only attempt to hold parent calls and not the individual children.
804 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700805 c.hold();
806 }
807 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700808 call.unhold();
809 }
810 }
811
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700812 /** Called by the in-call UI to change the mute state. */
813 void mute(boolean shouldMute) {
814 mCallAudioManager.mute(shouldMute);
815 }
816
817 /**
818 * Called by the in-call UI to change the audio route, for example to change from earpiece to
819 * speaker phone.
820 */
821 void setAudioRoute(int route) {
822 mCallAudioManager.setAudioRoute(route);
823 }
824
Yorke Leed1346872014-07-28 14:38:45 -0700825 /** Called by the in-call UI to turn the proximity sensor on. */
826 void turnOnProximitySensor() {
827 mProximitySensorManager.turnOn();
828 }
829
830 /**
831 * Called by the in-call UI to turn the proximity sensor off.
832 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
833 * the screen will be kept off until the proximity sensor goes negative.
834 */
835 void turnOffProximitySensor(boolean screenOnImmediately) {
836 mProximitySensorManager.turnOff(screenOnImmediately);
837 }
838
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700839 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700840 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700841 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700842 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700843 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
844 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
845 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
846 // respecting a rewritten number or a canceled number. This is unlikely since
847 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
848 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700849 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700850
851 // Note: emergency calls never go through account selection dialog so they never
852 // arrive here.
853 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700854 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700855 } else {
856 call.disconnect();
857 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700858
859 if (setDefault) {
860 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
861 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700862 }
863 }
864
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700865 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700866 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700867 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700868 for (CallsManagerListener listener : mListeners) {
869 listener.onAudioStateChanged(oldAudioState, newAudioState);
870 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700871 }
872
Sailesh Nepale59bb192014-04-01 18:33:59 -0700873 void markCallAsRinging(Call call) {
874 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800875 }
876
Sailesh Nepale59bb192014-04-01 18:33:59 -0700877 void markCallAsDialing(Call call) {
878 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800879 }
880
Sailesh Nepale59bb192014-04-01 18:33:59 -0700881 void markCallAsActive(Call call) {
882 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700883
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700884 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700885 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700886 }
Santos Cordon681663d2014-01-30 04:32:15 -0800887 }
888
Sailesh Nepale59bb192014-04-01 18:33:59 -0700889 void markCallAsOnHold(Call call) {
890 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700891 }
892
Santos Cordon049b7b62014-01-30 05:34:26 -0800893 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700894 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
895 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800896 *
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800897 * @param disconnectCause The disconnect cause, see {@link android.telecom.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800898 */
Andrew Lee701dc002014-09-11 21:29:12 -0700899 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
900 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700901 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700902 }
903
Ben Gilada0d9f752014-02-26 11:49:03 -0800904 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700905 * Removes an existing disconnected call, and notifies the in-call app.
906 */
907 void markCallAsRemoved(Call call) {
908 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700909 if (mLocallyDisconnectingCalls.contains(call)) {
910 mLocallyDisconnectingCalls.remove(call);
911 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
912 mForegroundCall.unhold();
913 }
914 }
Ihab Awada02bef52014-08-13 18:18:42 -0700915 }
916
917 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700918 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700919 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700920 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700921 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700922 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700923 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700924 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700925 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700926 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800927 if (call.getState() != CallState.DISCONNECTED) {
928 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
929 }
930 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700931 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700932 }
933 }
934 }
935
Santos Cordondeb8c892014-05-30 01:38:03 -0700936 boolean hasAnyCalls() {
937 return !mCalls.isEmpty();
938 }
939
Santos Cordonc7e85d42014-05-22 02:51:48 -0700940 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700941 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700942 }
943
944 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700945 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700946 }
947
Santos Cordondeb8c892014-05-30 01:38:03 -0700948 boolean onMediaButton(int type) {
949 if (hasAnyCalls()) {
950 if (HeadsetMediaButton.SHORT_PRESS == type) {
951 Call ringingCall = getFirstCallWithState(CallState.RINGING);
952 if (ringingCall == null) {
953 mCallAudioManager.toggleMute();
954 return true;
955 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700956 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700957 return true;
958 }
959 } else if (HeadsetMediaButton.LONG_PRESS == type) {
960 Log.d(this, "handleHeadsetHook: longpress -> hangup");
961 Call callToHangup = getFirstCallWithState(
962 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
963 if (callToHangup != null) {
964 callToHangup.disconnect();
965 return true;
966 }
967 }
968 }
969 return false;
970 }
971
972 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800973 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700974 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800975 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -0800976 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
977 return false;
978 }
979
Santos Cordon91bd74c2014-11-07 16:10:22 -0800980 int count = 0;
981 for (Call call : mCalls) {
982 if (call.isEmergencyCall()) {
983 // We never support add call if one of the calls is an emergency call.
984 return false;
985 } else if (call.getParentCall() == null) {
986 count++;
987 }
Santos Cordon10838c22014-06-11 17:36:04 -0700988
Santos Cordon91bd74c2014-11-07 16:10:22 -0800989 // We do not check states for canAddCall. We treat disconnected calls the same
990 // and wait until they are removed instead. If we didn't count disconnected calls,
991 // we could put InCallServices into a state where they are showing two calls but
992 // also support add-call. Technically it's right, but overall looks better (UI-wise)
993 // and acts better if we wait until the call is removed.
994 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700995 return false;
996 }
997 }
998 return true;
999 }
1000
Ihab Awada3653902015-01-23 13:44:46 -08001001 @VisibleForTesting
1002 public Call getRingingCall() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001003 return getFirstCallWithState(CallState.RINGING);
1004 }
1005
1006 Call getActiveCall() {
1007 return getFirstCallWithState(CallState.ACTIVE);
1008 }
1009
Nancy Chen05a9e402014-09-26 14:14:32 -07001010 Call getDialingCall() {
1011 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001012 }
1013
1014 Call getHeldCall() {
1015 return getFirstCallWithState(CallState.ON_HOLD);
1016 }
1017
Santos Cordonc0ca76e2014-10-21 15:54:19 -07001018 int getNumHeldCalls() {
1019 int count = 0;
1020 for (Call call : mCalls) {
1021 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
1022 count++;
1023 }
1024 }
1025 return count;
1026 }
1027
Santos Cordonf193ba42014-09-12 06:37:39 -07001028 Call getFirstCallWithState(int... states) {
1029 return getFirstCallWithState(null, states);
1030 }
1031
Santos Cordon10838c22014-06-11 17:36:04 -07001032 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001033 * Returns the first call that it finds with the given states. The states are treated as having
1034 * priority order so that any call with the first state will be returned before any call with
1035 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001036 *
1037 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001038 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001039 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001040 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001041 // check the foreground first
1042 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1043 return mForegroundCall;
1044 }
1045
Santos Cordondeb8c892014-05-30 01:38:03 -07001046 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001047 if (Objects.equals(callToSkip, call)) {
1048 continue;
1049 }
1050
1051 // Only operate on top-level calls
1052 if (call.getParentCall() != null) {
1053 continue;
1054 }
1055
Santos Cordondeb8c892014-05-30 01:38:03 -07001056 if (currentState == call.getState()) {
1057 return call;
1058 }
1059 }
1060 }
1061 return null;
1062 }
1063
Santos Cordon0fbe6322014-08-14 04:04:25 -07001064 Call createConferenceCall(
1065 PhoneAccountHandle phoneAccount,
1066 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001067
1068 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1069 // which is the default value for new Calls.
1070 long connectTime =
1071 parcelableConference.getConnectTimeMillis() ==
1072 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1073 parcelableConference.getConnectTimeMillis();
1074
Santos Cordon0fbe6322014-08-14 04:04:25 -07001075 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001076 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001077 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -07001078 mLock,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001079 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001080 mContactsAsyncHelper,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001081 null /* handle */,
1082 null /* gatewayInfo */,
1083 null /* connectionManagerPhoneAccount */,
1084 phoneAccount,
1085 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001086 true /* isConference */,
1087 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001088
1089 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001090 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001091
1092 // TODO: Move this to be a part of addCall()
1093 call.addListener(this);
1094 addCall(call);
1095 return call;
1096 }
1097
Yorke Leef86db2e2014-09-12 17:56:48 -07001098 /**
1099 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1100 */
1101 int getCallState() {
1102 return mPhoneStateBroadcaster.getCallState();
1103 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001104
Santos Cordon4b2c1192014-03-19 18:15:38 -07001105 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001106 * Retrieves the {@link PhoneAccountRegistrar}.
1107 *
1108 * @return The {@link PhoneAccountRegistrar}.
1109 */
1110 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1111 return mPhoneAccountRegistrar;
1112 }
1113
1114 /**
1115 * Retrieves the {@link MissedCallNotifier}
1116 * @return The {@link MissedCallNotifier}.
1117 */
1118 MissedCallNotifier getMissedCallNotifier() {
1119 return mMissedCallNotifier;
1120 }
1121
1122 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001123 * Adds the specified call to the main list of live calls.
1124 *
1125 * @param call The call to add.
1126 */
1127 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001128 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001129 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001130 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001131 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001132
Santos Cordondf399862014-08-06 04:39:15 -07001133 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001134 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001135 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001136 if (Log.SYSTRACE_DEBUG) {
1137 Trace.beginSection(listener.getClass().toString() + " addCall");
1138 }
Santos Cordona56f2762014-03-24 15:55:53 -07001139 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001140 if (Log.SYSTRACE_DEBUG) {
1141 Trace.endSection();
1142 }
Santos Cordona56f2762014-03-24 15:55:53 -07001143 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001144 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001145 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001146 }
1147
Sailesh Nepal810735e2014-03-18 18:15:46 -07001148 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001149 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001150 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001151
Santos Cordon672321e2014-08-21 14:38:58 -07001152 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001153 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001154 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001155
1156 boolean shouldNotify = false;
1157 if (mCalls.contains(call)) {
1158 mCalls.remove(call);
1159 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001160 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001161
Sailesh Nepale59bb192014-04-01 18:33:59 -07001162 // Only broadcast changes for calls that are being tracked.
1163 if (shouldNotify) {
1164 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001165 if (Log.SYSTRACE_DEBUG) {
1166 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1167 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001168 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001169 if (Log.SYSTRACE_DEBUG) {
1170 Trace.endSection();
1171 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001172 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001173 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001174 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001175 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001176 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001177
Sailesh Nepal810735e2014-03-18 18:15:46 -07001178 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001179 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001180 *
1181 * @param call The call.
1182 * @param newState The new state of the call.
1183 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001184 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001185 if (call == null) {
1186 return;
1187 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001188 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001189 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1190 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001191 if (newState != oldState) {
1192 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1193 // 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 -07001194 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001195 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001196 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001197 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001198 // unexpected transition occurs.
1199 call.setState(newState);
1200
Yorke Leee4a9c412014-11-14 16:59:42 -08001201 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001202 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001203 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001204 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001205 if (Log.SYSTRACE_DEBUG) {
1206 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1207 }
Santos Cordona56f2762014-03-24 15:55:53 -07001208 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001209 if (Log.SYSTRACE_DEBUG) {
1210 Trace.endSection();
1211 }
Santos Cordona56f2762014-03-24 15:55:53 -07001212 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001213 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001214 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001215 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001216 }
1217 }
1218
1219 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001220 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001221 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001222 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001223 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001224 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001225 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001226 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001227 // of its state will be foreground by default and instead the connection service should
1228 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001229 // the call should play audio and listen to microphone if it wants.
1230
Santos Cordonf193ba42014-09-12 06:37:39 -07001231 // Only top-level calls can be in foreground
1232 if (call.getParentCall() != null) {
1233 continue;
1234 }
1235
Santos Cordon40f78c22014-04-07 02:11:42 -07001236 // Active calls have priority.
1237 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001238 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001239 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001240 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001241
1242 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001243 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001244 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001245 }
1246 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001247
Sailesh Nepal810735e2014-03-18 18:15:46 -07001248 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001249 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001250 Call oldForegroundCall = mForegroundCall;
1251 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001252
Santos Cordona56f2762014-03-24 15:55:53 -07001253 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001254 if (Log.SYSTRACE_DEBUG) {
1255 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1256 }
Santos Cordona56f2762014-03-24 15:55:53 -07001257 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001258 if (Log.SYSTRACE_DEBUG) {
1259 Trace.endSection();
1260 }
Santos Cordona56f2762014-03-24 15:55:53 -07001261 }
Santos Cordon681663d2014-01-30 04:32:15 -08001262 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001263 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001264 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001265
Santos Cordon91bd74c2014-11-07 16:10:22 -08001266 private void updateCanAddCall() {
1267 boolean newCanAddCall = canAddCall();
1268 if (newCanAddCall != mCanAddCall) {
1269 mCanAddCall = newCanAddCall;
1270 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001271 if (Log.SYSTRACE_DEBUG) {
1272 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1273 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001274 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001275 if (Log.SYSTRACE_DEBUG) {
1276 Trace.endSection();
1277 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001278 }
1279 }
1280 }
1281
1282 private void updateCallsManagerState() {
1283 updateForegroundCall();
1284 updateCanAddCall();
1285 }
1286
Yorke Leeb701f6d2014-08-12 14:04:19 -07001287 private boolean isPotentialMMICode(Uri handle) {
1288 return (handle != null && handle.getSchemeSpecificPart() != null
1289 && handle.getSchemeSpecificPart().contains("#"));
1290 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001291
Tyler Gunn5b452df2014-10-01 15:02:58 -07001292 /**
1293 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1294 * MMI codes which can be dialed when one or more calls are in progress.
1295 * <P>
1296 * Checks for numbers formatted similar to the MMI codes defined in:
1297 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1298 * and
1299 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1300 *
1301 * @param handle The URI to call.
1302 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1303 */
1304 private boolean isPotentialInCallMMICode(Uri handle) {
1305 if (handle != null && handle.getSchemeSpecificPart() != null &&
1306 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1307
1308 String dialedNumber = handle.getSchemeSpecificPart();
1309 return (dialedNumber.equals("0") ||
1310 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1311 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1312 dialedNumber.equals("3") ||
1313 dialedNumber.equals("4") ||
1314 dialedNumber.equals("5"));
1315 }
1316 return false;
1317 }
1318
Santos Cordonf193ba42014-09-12 06:37:39 -07001319 private int getNumCallsWithState(int... states) {
1320 int count = 0;
1321 for (int state : states) {
1322 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001323 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001324 count++;
1325 }
1326 }
1327 }
1328 return count;
1329 }
1330
1331 private boolean hasMaximumLiveCalls() {
1332 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1333 }
1334
1335 private boolean hasMaximumHoldingCalls() {
1336 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1337 }
1338
1339 private boolean hasMaximumRingingCalls() {
1340 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1341 }
1342
1343 private boolean hasMaximumOutgoingCalls() {
1344 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1345 }
1346
1347 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1348 if (hasMaximumLiveCalls()) {
1349 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1350 // have to change.
1351 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301352 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1353 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001354
Santos Cordond1766502014-09-26 15:45:39 -07001355 if (call == liveCall) {
1356 // If the call is already the foreground call, then we are golden.
1357 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1358 // state since the call was already populated into the list.
1359 return true;
1360 }
1361
Andrew Leee6595182014-09-23 18:43:05 -07001362 if (hasMaximumOutgoingCalls()) {
1363 // Disconnect the current outgoing call if it's not an emergency call. If the user
1364 // tries to make two outgoing calls to different emergency call numbers, we will try
1365 // to connect the first outgoing call.
1366 if (isEmergency) {
1367 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1368 if (!outgoingCall.isEmergencyCall()) {
1369 outgoingCall.disconnect();
1370 return true;
1371 }
1372 }
1373 return false;
1374 }
1375
Santos Cordonf193ba42014-09-12 06:37:39 -07001376 if (hasMaximumHoldingCalls()) {
1377 // There is no more room for any more calls, unless it's an emergency.
1378 if (isEmergency) {
1379 // Kill the current active call, this is easier then trying to disconnect a
1380 // holding call and hold an active call.
1381 liveCall.disconnect();
1382 return true;
1383 }
1384 return false; // No more room!
1385 }
1386
1387 // We have room for at least one more holding call at this point.
1388
1389 // First thing, if we are trying to make a call with the same phone account as the live
1390 // call, then allow it so that the connection service can make its own decision about
1391 // how to handle the new call relative to the current one.
1392 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1393 return true;
1394 } else if (call.getTargetPhoneAccount() == null) {
1395 // Without a phone account, we can't say reliably that the call will fail.
1396 // If the user chooses the same phone account as the live call, then it's
1397 // still possible that the call can be made (like with CDMA calls not supporting
1398 // hold but they still support adding a call by going immediately into conference
1399 // mode). Return true here and we'll run this code again after user chooses an
1400 // account.
1401 return true;
1402 }
1403
1404 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001405 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001406 liveCall.hold();
1407 return true;
1408 }
1409
1410 // The live call cannot be held so we're out of luck here. There's no room.
1411 return false;
1412 }
1413 return true;
1414 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001415
1416 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001417 * Creates a new call for an existing connection.
1418 *
1419 * @param callId The id of the new call.
1420 * @param connection The connection information.
1421 * @return The new call.
1422 */
1423 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1424 Call call = new Call(
1425 mContext,
Ihab Awad78a5e6b2015-02-06 10:13:05 -08001426 this,
Ihab Awadd1d63e12015-03-26 10:33:44 -07001427 mLock,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001428 mConnectionServiceRepository,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -07001429 mContactsAsyncHelper,
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001430 connection.getHandle() /* handle */,
1431 null /* gatewayInfo */,
1432 null /* connectionManagerPhoneAccount */,
1433 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1434 false /* isIncoming */,
1435 false /* isConference */);
1436
1437 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001438 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001439 call.setCallerDisplayName(connection.getCallerDisplayName(),
1440 connection.getCallerDisplayNamePresentation());
1441
1442 call.addListener(this);
1443 addCall(call);
1444
1445 return call;
1446 }
1447
1448 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001449 * Dumps the state of the {@link CallsManager}.
1450 *
1451 * @param pw The {@code IndentingPrintWriter} to write the state to.
1452 */
1453 public void dump(IndentingPrintWriter pw) {
1454 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001455 if (mCalls != null) {
1456 pw.println("mCalls: ");
1457 pw.increaseIndent();
1458 for (Call call : mCalls) {
1459 pw.println(call);
1460 }
1461 pw.decreaseIndent();
1462 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001463 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1464
1465 if (mCallAudioManager != null) {
1466 pw.println("mCallAudioManager:");
1467 pw.increaseIndent();
1468 mCallAudioManager.dump(pw);
1469 pw.decreaseIndent();
1470 }
1471
1472 if (mTtyManager != null) {
1473 pw.println("mTtyManager:");
1474 pw.increaseIndent();
1475 mTtyManager.dump(pw);
1476 pw.decreaseIndent();
1477 }
1478
1479 if (mInCallController != null) {
1480 pw.println("mInCallController:");
1481 pw.increaseIndent();
1482 mInCallController.dump(pw);
1483 pw.decreaseIndent();
1484 }
1485
1486 if (mConnectionServiceRepository != null) {
1487 pw.println("mConnectionServiceRepository:");
1488 pw.increaseIndent();
1489 mConnectionServiceRepository.dump(pw);
1490 pw.decreaseIndent();
1491 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001492 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001493}