blob: 3732a4fcd476af433f23d5851f8d8dca621c8b14 [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
Tyler Gunn91d43cf2014-09-17 12:19:39 -070040import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080041
Santos Cordon4bc02452014-11-19 15:51:12 -080042import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070043import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070044import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070045import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070046import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070047import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070048import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080049
Ben Giladdd8c6082013-12-30 14:44:08 -080050/**
51 * Singleton.
52 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070053 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080054 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070055 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080056 */
Santos Cordon64c7e962014-07-02 15:15:27 -070057public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080058
Santos Cordondf399862014-08-06 04:39:15 -070059 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070060 interface CallsManagerListener {
61 void onCallAdded(Call call);
62 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070063 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070064 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070065 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070066 ConnectionServiceWrapper oldService,
67 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070069 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070070 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070071 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070072 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070073 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070074 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070075 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080076 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070077 }
78
Tyler Gunn91d43cf2014-09-17 12:19:39 -070079 /**
80 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
81 */
Santos Cordone6a33f22014-12-02 13:19:00 -080082 private static CallsManager sInstance = null;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070083
84 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Santos Cordonf193ba42014-09-12 06:37:39 -070086 private static final int MAXIMUM_LIVE_CALLS = 1;
87 private static final int MAXIMUM_HOLD_CALLS = 1;
88 private static final int MAXIMUM_RINGING_CALLS = 1;
89 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080090 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070091
92 private static final int[] OUTGOING_CALL_STATES =
93 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
94
Santos Cordon91bd74c2014-11-07 16:10:22 -080095 private static final int[] LIVE_CALL_STATES =
96 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
97
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070099 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
100 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800101 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700102 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
103 * load factor before resizing, 1 means we only expect a single thread to
104 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800105 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700106 private final Set<Call> mCalls = Collections.newSetFromMap(
107 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800108
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700109 private final ConnectionServiceRepository mConnectionServiceRepository;
110 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
111 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700112 private final CallAudioManager mCallAudioManager;
113 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800114 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700115 // For this set initial table size to 16 because we add 13 listeners in
116 // the CallsManager constructor.
117 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
118 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700119 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700120 private final WiredHeadsetManager mWiredHeadsetManager;
121 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700122 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700123 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700124 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700125 private final Context mContext;
126 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. */
131 private final Handler mHandler = new Handler();
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
Santos Cordon766d04f2014-05-06 10:28:25 -0700143 /** Singleton accessor. */
144 static CallsManager getInstance() {
Santos Cordone6a33f22014-12-02 13:19:00 -0800145 return sInstance;
Santos Cordon766d04f2014-05-06 10:28:25 -0700146 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800147
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800148 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700149 * Sets the static singleton instance.
150 *
151 * @param instance The instance to set.
152 */
153 static void initialize(CallsManager instance) {
Santos Cordone6a33f22014-12-02 13:19:00 -0800154 sInstance = instance;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700155 }
156
157 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700158 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800159 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700160 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
161 PhoneAccountRegistrar phoneAccountRegistrar) {
162 mContext = context;
163 mPhoneAccountRegistrar = phoneAccountRegistrar;
164 mMissedCallNotifier = missedCallNotifier;
165 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
166 mWiredHeadsetManager = new WiredHeadsetManager(context);
167 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700168 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700169 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
170 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
171 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
172 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700173 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700174 mCallLogManager = new CallLogManager(context);
175 mInCallController = new InCallController(context);
176 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
177 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
178 context);
Santos Cordon61e6f352014-12-03 02:53:34 -0800179 mInCallWakeLockController = new InCallWakeLockController(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700180
Santos Cordondeb8c892014-05-30 01:38:03 -0700181 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700182 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700183 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700184 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700185 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700186 mListeners.add(new RingbackPlayer(this, playerFactory));
187 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700188 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700189 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700190 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700191 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700192 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700193 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800194 }
195
Santos Cordon766d04f2014-05-06 10:28:25 -0700196 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700197 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700198 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700199
Tyler Gunncde2e502014-08-12 14:35:58 -0700200 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700201 if (!mCalls.contains(call)) {
202 // Call was not added previously in startOutgoingCall due to it being a potential MMI
203 // code, so add it now.
204 addCall(call);
205 }
206
207 // The call's ConnectionService has been updated.
208 for (CallsManagerListener listener : mListeners) {
209 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700210 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700211
212 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700213 }
214
215 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700216 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700217 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700218
Andrew Leee6288a72014-10-01 13:50:02 -0700219 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700220 }
221
222 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700223 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700224 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700225 setCallState(incomingCall, CallState.RINGING);
226
227 if (hasMaximumRingingCalls()) {
228 incomingCall.reject(false, null);
229 // since the call was not added to the list of calls, we have to call the missed
230 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700231 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700232 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
233 } else {
234 addCall(incomingCall);
235 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700236 }
237
238 @Override
239 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700240 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700241 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800242 }
243
Ihab Awadcb387ac2014-05-28 16:49:38 -0700244 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700245 public void onSuccessfulUnknownCall(Call call, int callState) {
246 setCallState(call, callState);
247 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
248 addCall(call);
249 }
250
251 @Override
252 public void onFailedUnknownCall(Call call) {
253 Log.i(this, "onFailedUnknownCall for call %s", call);
254 setCallState(call, CallState.DISCONNECTED);
255 call.removeListener(this);
256 }
257
258 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700259 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700260 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700261 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700262 }
263 }
264
Evan Charlton352105c2014-06-03 14:10:54 -0700265 @Override
266 public void onPostDialWait(Call call, String remaining) {
267 mInCallController.onPostDialWait(call, remaining);
268 }
269
Santos Cordona1610702014-06-04 20:22:56 -0700270 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800271 public void onPostDialChar(final Call call, char nextChar) {
272 if (PhoneNumberUtils.is12Key(nextChar)) {
273 // Play tone if it is one of the dialpad digits, canceling out the previously queued
274 // up stopTone runnable since playing a new tone automatically stops the previous tone.
275 if (mStopTone != null) {
276 mHandler.removeCallbacks(mStopTone);
277 }
278
279 mDtmfLocalTonePlayer.playTone(call, nextChar);
280
281 mStopTone = new Runnable() {
282 @Override
283 public void run() {
284 // Set a timeout to stop the tone in case there isn't another tone to follow.
285 mDtmfLocalTonePlayer.stopTone(call);
286 }
287 };
288 mHandler.postDelayed(
289 mStopTone,
290 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
291 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
292 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
293 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
294 // the previous tone is being stopped anyway.
295 if (mStopTone != null) {
296 mHandler.removeCallbacks(mStopTone);
297 }
298 mDtmfLocalTonePlayer.stopTone(call);
299 } else {
300 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
301 }
302 }
303
304 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700305 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700306 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800307 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700308 for (CallsManagerListener listener : mListeners) {
309 listener.onIsConferencedChanged(call);
310 }
311 }
312
313 @Override
314 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700315 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800316 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700317 for (CallsManagerListener listener : mListeners) {
318 listener.onIsConferencedChanged(call);
319 }
320 }
321
Ihab Awadff7493a2014-06-10 13:47:44 -0700322 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700323 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700324 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700325 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700326 }
327 }
328
Andrew Lee4a796602014-07-11 17:23:03 -0700329 @Override
330 public void onVideoStateChanged(Call call) {
331 for (CallsManagerListener listener : mListeners) {
332 listener.onVideoStateChanged(call);
333 }
334 }
335
Santos Cordoncf5b2912014-11-05 21:57:54 -0800336 @Override
337 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
338 mPendingCallsToDisconnect.add(call);
339 mHandler.postDelayed(new Runnable() {
340 @Override
341 public void run() {
342 if (mPendingCallsToDisconnect.remove(call)) {
343 Log.i(this, "Delayed disconnection of call: %s", call);
344 call.disconnect();
345 }
346 }
347 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
348
349 return true;
350 }
351
Santos Cordon4bc02452014-11-19 15:51:12 -0800352 Collection<Call> getCalls() {
353 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700354 }
355
356 Call getForegroundCall() {
357 return mForegroundCall;
358 }
359
Santos Cordonae193062014-05-21 21:21:49 -0700360 Ringer getRinger() {
361 return mRinger;
362 }
363
Santos Cordonf3671a62014-05-29 21:51:53 -0700364 InCallController getInCallController() {
365 return mInCallController;
366 }
367
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700368 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700369 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700370 if (call.isEmergencyCall()) {
371 return true;
372 }
373 }
374 return false;
375 }
376
Andrew Lee45506232014-10-22 17:54:33 -0700377 boolean hasVideoCall() {
378 for (Call call : mCalls) {
379 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
380 return true;
381 }
382 }
383 return false;
384 }
385
Ihab Awad6fb37c82014-08-07 19:48:57 -0700386 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700387 return mCallAudioManager.getAudioState();
388 }
389
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700390 boolean isTtySupported() {
391 return mTtyManager.isTtySupported();
392 }
393
394 int getCurrentTtyMode() {
395 return mTtyManager.getCurrentTtyMode();
396 }
397
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700398 void addListener(CallsManagerListener listener) {
399 mListeners.add(listener);
400 }
401
402 void removeListener(CallsManagerListener listener) {
403 mListeners.remove(listener);
404 }
405
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800406 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700407 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800408 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700409 * @param phoneAccountHandle The phone account which contains the component name of the
410 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800411 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800412 */
Evan Charlton89176372014-07-19 18:23:09 -0700413 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800414 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700415 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700416 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700417 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700418 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700419 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700420 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700421 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700422 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700423 true /* isIncoming */,
424 false /* isConference */);
425
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700426 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700427 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700428 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700429 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800430 }
431
Yorke Lee9250e5f2014-10-01 13:39:09 -0700432 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
433 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
434 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
435 Call call = new Call(
436 mContext,
437 mConnectionServiceRepository,
438 handle,
439 null /* gatewayInfo */,
440 null /* connectionManagerPhoneAccount */,
441 phoneAccountHandle,
442 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
443 // to the existing connection instead of trying to create a new one.
444 true /* isIncoming */,
445 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700446 call.setIsUnknown(true);
447 call.setExtras(extras);
448 call.addListener(this);
449 call.startCreateConnection(mPhoneAccountRegistrar);
450 }
451
Santos Cordoncf5b2912014-11-05 21:57:54 -0800452 private Call getNewOutgoingCall(Uri handle) {
453 // First check to see if we can reuse any of the calls that are waiting to disconnect.
454 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800455 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800456 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800457 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800458 mPendingCallsToDisconnect.remove(pendingCall);
459 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800460 reusedCall = pendingCall;
461 } else {
462 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800463 }
464 }
Yorke Lee59979512014-12-02 01:03:40 -0800465 if (reusedCall != null) {
466 return reusedCall;
467 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800468
469 // Create a call with original handle. The handle may be changed when the call is attached
470 // to a connection service, but in most cases will remain the same.
471 return new Call(
472 mContext,
473 mConnectionServiceRepository,
474 handle,
475 null /* gatewayInfo */,
476 null /* connectionManagerPhoneAccount */,
477 null /* phoneAccountHandle */,
478 false /* isIncoming */,
479 false /* isConference */);
480 }
481
Nancy Chen0d3076c2014-07-30 14:45:44 -0700482 /**
483 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
484 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700485 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700486 * @param phoneAccountHandle The phone account which contains the component name of the
487 * connection service to use for this call.
488 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700489 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700490 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800491 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700492
493 List<PhoneAccountHandle> accounts =
494 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
495
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800496 Log.v(this, "startOutgoingCall found accounts = " + accounts);
497
Nancy Chenbc95db92014-11-24 13:19:18 -0800498 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
499 // If there is an ongoing call, use the same phone account to place this new call.
500 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
501 }
502
Nancy Chenbc9ef172014-08-15 17:11:57 -0700503 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
504 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700505 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700506 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700507 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700508 phoneAccountHandle = null;
509 }
510 }
511
512 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700513 // No preset account, check if default exists that supports the URI scheme for the
514 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700515 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700516 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700517 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700518 if (defaultAccountHandle != null) {
519 phoneAccountHandle = defaultAccountHandle;
520 }
521 }
522
Nancy Chen1c5926f2014-09-17 14:44:14 -0700523 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700524
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700525 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
526 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700527 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700528
529 // Do not support any more live calls. Our options are to move a call to hold, disconnect
530 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700531 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700532 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800533 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800534 if (mCalls.contains(call)) {
535 // This call can already exist if it is a reused call,
536 // See {@link #getNewOutgoingCall}.
537 call.disconnect();
538 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700539 return null;
540 }
541
Nancy Chen8d92f452014-10-20 21:43:44 -0700542 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
543 !isEmergencyCall;
544
545 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700546 // This is the state where the user is expected to select an account
547 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700548 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700549 } else {
550 call.setState(CallState.CONNECTING);
551 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700552
Nancy Chen1c5926f2014-09-17 14:44:14 -0700553 call.setExtras(extras);
554
Tyler Gunn504eb832014-10-13 14:15:27 -0700555 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700556 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700557 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800558 } else if (!mCalls.contains(call)) {
559 // We check if mCalls already contains the call because we could potentially be reusing
560 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700561 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700562 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700563
564 return call;
565 }
566
Ben Gilada0d9f752014-02-26 11:49:03 -0800567 /**
Yorke Lee33501632014-03-17 19:24:12 -0700568 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800569 *
Yorke Lee33501632014-03-17 19:24:12 -0700570 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700571 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700572 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700573 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700574 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800575 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700576 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
577 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700578 if (call == null) {
579 // don't do anything if the call no longer exists
580 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700581 return;
582 }
583
Nancy Chen201b4372014-09-08 14:18:24 -0700584 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700585
586 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700587 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700588 } else {
589 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
590 Log.pii(uriHandle), Log.pii(handle));
591 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700592
Nancy Chen0d3076c2014-07-30 14:45:44 -0700593 call.setHandle(uriHandle);
594 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700595 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700596 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700597
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700598 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
599 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700600 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700601 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700602 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700603 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700604
Santos Cordonf193ba42014-09-12 06:37:39 -0700605 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700606 // If the account has been set, proceed to place the outgoing call.
607 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700608 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700609 }
Yorke Leef98fb572014-03-05 10:56:55 -0800610 }
611
612 /**
Santos Cordona1610702014-06-04 20:22:56 -0700613 * Attempts to start a conference call for the specified call.
614 *
Santos Cordon12d61822014-07-29 16:02:20 -0700615 * @param call The call to conference.
616 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700617 */
Santos Cordon12d61822014-07-29 16:02:20 -0700618 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700619 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700620 }
621
622 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700623 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
624 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800625 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700626 *
627 * @param call The call to answer.
628 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800629 */
Andrew Lee38931d02014-07-16 10:17:36 -0700630 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700631 if (!mCalls.contains(call)) {
632 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800633 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700634 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700635 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700636 if (mForegroundCall != null && mForegroundCall != call &&
637 (mForegroundCall.isActive() ||
638 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800639 if (0 == (mForegroundCall.getConnectionCapabilities()
640 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700641 // This call does not support hold. If it is from a different connection
642 // service, then disconnect it, otherwise allow the connection service to
643 // figure out the right states.
644 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
645 mForegroundCall.disconnect();
646 }
647 } else {
Andrew Leee94a8f12014-12-19 16:19:51 -0800648 Call heldCall = getHeldCall();
649 if (heldCall != null) {
650 Log.v(this, "Disconnecting held call %s before holding active call.",
651 heldCall);
652 heldCall.disconnect();
653 }
654
Santos Cordonf193ba42014-09-12 06:37:39 -0700655 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
656 mForegroundCall, call);
657 mForegroundCall.hold();
658 }
Santos Cordondf399862014-08-06 04:39:15 -0700659 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700660 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700661 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700662 }
663
Santos Cordona56f2762014-03-24 15:55:53 -0700664 for (CallsManagerListener listener : mListeners) {
665 listener.onIncomingCallAnswered(call);
666 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800667
Santos Cordon61d0f702014-02-19 02:52:23 -0800668 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700669 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700670 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800671 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800672 }
673
674 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700675 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
676 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800677 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800678 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700679 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700680 if (!mCalls.contains(call)) {
681 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800682 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700683 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700684 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700685 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700686 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800687 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800688 }
689
690 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700691 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700692 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700693 * @param digit The DTMF digit to play.
694 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700695 void playDtmfTone(Call call, char digit) {
696 if (!mCalls.contains(call)) {
697 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700698 } else {
699 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700700 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700701 }
702 }
703
704 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700705 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700706 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700707 void stopDtmfTone(Call call) {
708 if (!mCalls.contains(call)) {
709 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700710 } else {
711 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700712 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700713 }
714 }
715
716 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700717 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700718 */
Evan Charlton352105c2014-06-03 14:10:54 -0700719 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700720 if (!mCalls.contains(call)) {
721 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700722 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700723 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700724 }
725 }
726
727 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700728 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800729 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
730 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800731 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700732 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700733 Log.v(this, "disconnectCall %s", call);
734
Sailesh Nepale59bb192014-04-01 18:33:59 -0700735 if (!mCalls.contains(call)) {
736 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800737 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700738 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800739 call.disconnect();
740 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800741 }
Santos Cordon681663d2014-01-30 04:32:15 -0800742
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700743 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700744 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700745 */
746 void disconnectAllCalls() {
747 Log.v(this, "disconnectAllCalls");
748
749 for (Call call : mCalls) {
750 disconnectCall(call);
751 }
752 }
753
Nancy Chenbc9ef172014-08-15 17:11:57 -0700754
Tyler Gunn61b92102014-08-19 07:42:20 -0700755 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700756 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700757 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
758 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700759 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700760 void holdCall(Call call) {
761 if (!mCalls.contains(call)) {
762 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700763 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700764 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700765 call.hold();
766 }
767 }
768
769 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700770 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700771 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
772 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700773 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700774 void unholdCall(Call call) {
775 if (!mCalls.contains(call)) {
776 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700777 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700778 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700779 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800780 // Only attempt to hold parent calls and not the individual children.
781 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700782 c.hold();
783 }
784 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700785 call.unhold();
786 }
787 }
788
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700789 /** Called by the in-call UI to change the mute state. */
790 void mute(boolean shouldMute) {
791 mCallAudioManager.mute(shouldMute);
792 }
793
794 /**
795 * Called by the in-call UI to change the audio route, for example to change from earpiece to
796 * speaker phone.
797 */
798 void setAudioRoute(int route) {
799 mCallAudioManager.setAudioRoute(route);
800 }
801
Yorke Leed1346872014-07-28 14:38:45 -0700802 /** Called by the in-call UI to turn the proximity sensor on. */
803 void turnOnProximitySensor() {
804 mProximitySensorManager.turnOn();
805 }
806
807 /**
808 * Called by the in-call UI to turn the proximity sensor off.
809 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
810 * the screen will be kept off until the proximity sensor goes negative.
811 */
812 void turnOffProximitySensor(boolean screenOnImmediately) {
813 mProximitySensorManager.turnOff(screenOnImmediately);
814 }
815
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700816 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700817 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700818 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700819 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700820 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
821 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
822 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
823 // respecting a rewritten number or a canceled number. This is unlikely since
824 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
825 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700826 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700827
828 // Note: emergency calls never go through account selection dialog so they never
829 // arrive here.
830 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700831 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700832 } else {
833 call.disconnect();
834 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700835
836 if (setDefault) {
837 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
838 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700839 }
840 }
841
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700842 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700843 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700844 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700845 for (CallsManagerListener listener : mListeners) {
846 listener.onAudioStateChanged(oldAudioState, newAudioState);
847 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700848 }
849
Sailesh Nepale59bb192014-04-01 18:33:59 -0700850 void markCallAsRinging(Call call) {
851 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800852 }
853
Sailesh Nepale59bb192014-04-01 18:33:59 -0700854 void markCallAsDialing(Call call) {
855 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800856 }
857
Sailesh Nepale59bb192014-04-01 18:33:59 -0700858 void markCallAsActive(Call call) {
859 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700860
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700861 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700862 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700863 }
Santos Cordon681663d2014-01-30 04:32:15 -0800864 }
865
Sailesh Nepale59bb192014-04-01 18:33:59 -0700866 void markCallAsOnHold(Call call) {
867 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700868 }
869
Santos Cordon049b7b62014-01-30 05:34:26 -0800870 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700871 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
872 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800873 *
Andrew Lee701dc002014-09-11 21:29:12 -0700874 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800875 */
Andrew Lee701dc002014-09-11 21:29:12 -0700876 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
877 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700878 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700879 }
880
Ben Gilada0d9f752014-02-26 11:49:03 -0800881 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700882 * Removes an existing disconnected call, and notifies the in-call app.
883 */
884 void markCallAsRemoved(Call call) {
885 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700886 if (mLocallyDisconnectingCalls.contains(call)) {
887 mLocallyDisconnectingCalls.remove(call);
888 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
889 mForegroundCall.unhold();
890 }
891 }
Ihab Awada02bef52014-08-13 18:18:42 -0700892 }
893
894 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700895 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700896 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700897 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700898 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700899 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700900 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700901 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700902 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700903 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800904 if (call.getState() != CallState.DISCONNECTED) {
905 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
906 }
907 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700908 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700909 }
910 }
911 }
912
Santos Cordondeb8c892014-05-30 01:38:03 -0700913 boolean hasAnyCalls() {
914 return !mCalls.isEmpty();
915 }
916
Santos Cordonc7e85d42014-05-22 02:51:48 -0700917 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700918 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700919 }
920
921 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700922 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700923 }
924
Santos Cordondeb8c892014-05-30 01:38:03 -0700925 boolean onMediaButton(int type) {
926 if (hasAnyCalls()) {
927 if (HeadsetMediaButton.SHORT_PRESS == type) {
928 Call ringingCall = getFirstCallWithState(CallState.RINGING);
929 if (ringingCall == null) {
930 mCallAudioManager.toggleMute();
931 return true;
932 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700933 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700934 return true;
935 }
936 } else if (HeadsetMediaButton.LONG_PRESS == type) {
937 Log.d(this, "handleHeadsetHook: longpress -> hangup");
938 Call callToHangup = getFirstCallWithState(
939 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
940 if (callToHangup != null) {
941 callToHangup.disconnect();
942 return true;
943 }
944 }
945 }
946 return false;
947 }
948
949 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800950 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700951 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800952 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -0800953 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
954 return false;
955 }
956
Santos Cordon91bd74c2014-11-07 16:10:22 -0800957 int count = 0;
958 for (Call call : mCalls) {
959 if (call.isEmergencyCall()) {
960 // We never support add call if one of the calls is an emergency call.
961 return false;
962 } else if (call.getParentCall() == null) {
963 count++;
964 }
Santos Cordon10838c22014-06-11 17:36:04 -0700965
Santos Cordon91bd74c2014-11-07 16:10:22 -0800966 // We do not check states for canAddCall. We treat disconnected calls the same
967 // and wait until they are removed instead. If we didn't count disconnected calls,
968 // we could put InCallServices into a state where they are showing two calls but
969 // also support add-call. Technically it's right, but overall looks better (UI-wise)
970 // and acts better if we wait until the call is removed.
971 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700972 return false;
973 }
974 }
975 return true;
976 }
977
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700978 Call getRingingCall() {
979 return getFirstCallWithState(CallState.RINGING);
980 }
981
982 Call getActiveCall() {
983 return getFirstCallWithState(CallState.ACTIVE);
984 }
985
Nancy Chen05a9e402014-09-26 14:14:32 -0700986 Call getDialingCall() {
987 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700988 }
989
990 Call getHeldCall() {
991 return getFirstCallWithState(CallState.ON_HOLD);
992 }
993
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700994 int getNumHeldCalls() {
995 int count = 0;
996 for (Call call : mCalls) {
997 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
998 count++;
999 }
1000 }
1001 return count;
1002 }
1003
Santos Cordonf193ba42014-09-12 06:37:39 -07001004 Call getFirstCallWithState(int... states) {
1005 return getFirstCallWithState(null, states);
1006 }
1007
Santos Cordon10838c22014-06-11 17:36:04 -07001008 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001009 * Returns the first call that it finds with the given states. The states are treated as having
1010 * priority order so that any call with the first state will be returned before any call with
1011 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001012 *
1013 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001014 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001015 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001016 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001017 // check the foreground first
1018 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1019 return mForegroundCall;
1020 }
1021
Santos Cordondeb8c892014-05-30 01:38:03 -07001022 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001023 if (Objects.equals(callToSkip, call)) {
1024 continue;
1025 }
1026
1027 // Only operate on top-level calls
1028 if (call.getParentCall() != null) {
1029 continue;
1030 }
1031
Santos Cordondeb8c892014-05-30 01:38:03 -07001032 if (currentState == call.getState()) {
1033 return call;
1034 }
1035 }
1036 }
1037 return null;
1038 }
1039
Santos Cordon0fbe6322014-08-14 04:04:25 -07001040 Call createConferenceCall(
1041 PhoneAccountHandle phoneAccount,
1042 ParcelableConference parcelableConference) {
Tyler Gunnd92e7372015-01-09 15:59:45 -08001043
1044 // If the parceled conference specifies a connect time, use it; otherwise default to 0,
1045 // which is the default value for new Calls.
1046 long connectTime =
1047 parcelableConference.getConnectTimeMillis() ==
1048 Conference.CONNECT_TIME_NOT_SPECIFIED ? 0 :
1049 parcelableConference.getConnectTimeMillis();
1050
Santos Cordon0fbe6322014-08-14 04:04:25 -07001051 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001052 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001053 mConnectionServiceRepository,
1054 null /* handle */,
1055 null /* gatewayInfo */,
1056 null /* connectionManagerPhoneAccount */,
1057 phoneAccount,
1058 false /* isIncoming */,
Tyler Gunnd92e7372015-01-09 15:59:45 -08001059 true /* isConference */,
1060 connectTime);
Santos Cordon0fbe6322014-08-14 04:04:25 -07001061
1062 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001063 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001064
1065 // TODO: Move this to be a part of addCall()
1066 call.addListener(this);
1067 addCall(call);
1068 return call;
1069 }
1070
Yorke Leef86db2e2014-09-12 17:56:48 -07001071 /**
1072 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1073 */
1074 int getCallState() {
1075 return mPhoneStateBroadcaster.getCallState();
1076 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001077
Santos Cordon4b2c1192014-03-19 18:15:38 -07001078 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001079 * Retrieves the {@link PhoneAccountRegistrar}.
1080 *
1081 * @return The {@link PhoneAccountRegistrar}.
1082 */
1083 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1084 return mPhoneAccountRegistrar;
1085 }
1086
1087 /**
1088 * Retrieves the {@link MissedCallNotifier}
1089 * @return The {@link MissedCallNotifier}.
1090 */
1091 MissedCallNotifier getMissedCallNotifier() {
1092 return mMissedCallNotifier;
1093 }
1094
1095 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001096 * Adds the specified call to the main list of live calls.
1097 *
1098 * @param call The call to add.
1099 */
1100 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001101 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001102 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001103 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001104 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001105
Santos Cordondf399862014-08-06 04:39:15 -07001106 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001107 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001108 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001109 if (Log.SYSTRACE_DEBUG) {
1110 Trace.beginSection(listener.getClass().toString() + " addCall");
1111 }
Santos Cordona56f2762014-03-24 15:55:53 -07001112 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001113 if (Log.SYSTRACE_DEBUG) {
1114 Trace.endSection();
1115 }
Santos Cordona56f2762014-03-24 15:55:53 -07001116 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001117 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001118 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001119 }
1120
Sailesh Nepal810735e2014-03-18 18:15:46 -07001121 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001122 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001123 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001124
Santos Cordon672321e2014-08-21 14:38:58 -07001125 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001126 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001127 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001128
1129 boolean shouldNotify = false;
1130 if (mCalls.contains(call)) {
1131 mCalls.remove(call);
1132 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001133 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001134
Sailesh Nepale59bb192014-04-01 18:33:59 -07001135 // Only broadcast changes for calls that are being tracked.
1136 if (shouldNotify) {
1137 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001138 if (Log.SYSTRACE_DEBUG) {
1139 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1140 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001141 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001142 if (Log.SYSTRACE_DEBUG) {
1143 Trace.endSection();
1144 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001145 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001146 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001147 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001148 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001149 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001150
Sailesh Nepal810735e2014-03-18 18:15:46 -07001151 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001152 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001153 *
1154 * @param call The call.
1155 * @param newState The new state of the call.
1156 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001157 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001158 if (call == null) {
1159 return;
1160 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001161 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001162 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1163 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001164 if (newState != oldState) {
1165 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1166 // 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 -07001167 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001168 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001169 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001170 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001171 // unexpected transition occurs.
1172 call.setState(newState);
1173
Yorke Leee4a9c412014-11-14 16:59:42 -08001174 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001175 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001176 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001177 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001178 if (Log.SYSTRACE_DEBUG) {
1179 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1180 }
Santos Cordona56f2762014-03-24 15:55:53 -07001181 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001182 if (Log.SYSTRACE_DEBUG) {
1183 Trace.endSection();
1184 }
Santos Cordona56f2762014-03-24 15:55:53 -07001185 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001186 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001187 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001188 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001189 }
1190 }
1191
1192 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001193 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001194 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001195 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001196 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001197 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001198 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001199 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001200 // of its state will be foreground by default and instead the connection service should
1201 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001202 // the call should play audio and listen to microphone if it wants.
1203
Santos Cordonf193ba42014-09-12 06:37:39 -07001204 // Only top-level calls can be in foreground
1205 if (call.getParentCall() != null) {
1206 continue;
1207 }
1208
Santos Cordon40f78c22014-04-07 02:11:42 -07001209 // Active calls have priority.
1210 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001211 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001212 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001213 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001214
1215 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001216 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001217 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001218 }
1219 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001220
Sailesh Nepal810735e2014-03-18 18:15:46 -07001221 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001222 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001223 Call oldForegroundCall = mForegroundCall;
1224 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001225
Santos Cordona56f2762014-03-24 15:55:53 -07001226 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001227 if (Log.SYSTRACE_DEBUG) {
1228 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1229 }
Santos Cordona56f2762014-03-24 15:55:53 -07001230 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001231 if (Log.SYSTRACE_DEBUG) {
1232 Trace.endSection();
1233 }
Santos Cordona56f2762014-03-24 15:55:53 -07001234 }
Santos Cordon681663d2014-01-30 04:32:15 -08001235 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001236 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001237 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001238
Santos Cordon91bd74c2014-11-07 16:10:22 -08001239 private void updateCanAddCall() {
1240 boolean newCanAddCall = canAddCall();
1241 if (newCanAddCall != mCanAddCall) {
1242 mCanAddCall = newCanAddCall;
1243 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001244 if (Log.SYSTRACE_DEBUG) {
1245 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1246 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001247 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001248 if (Log.SYSTRACE_DEBUG) {
1249 Trace.endSection();
1250 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001251 }
1252 }
1253 }
1254
1255 private void updateCallsManagerState() {
1256 updateForegroundCall();
1257 updateCanAddCall();
1258 }
1259
Yorke Leeb701f6d2014-08-12 14:04:19 -07001260 private boolean isPotentialMMICode(Uri handle) {
1261 return (handle != null && handle.getSchemeSpecificPart() != null
1262 && handle.getSchemeSpecificPart().contains("#"));
1263 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001264
Tyler Gunn5b452df2014-10-01 15:02:58 -07001265 /**
1266 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1267 * MMI codes which can be dialed when one or more calls are in progress.
1268 * <P>
1269 * Checks for numbers formatted similar to the MMI codes defined in:
1270 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1271 * and
1272 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1273 *
1274 * @param handle The URI to call.
1275 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1276 */
1277 private boolean isPotentialInCallMMICode(Uri handle) {
1278 if (handle != null && handle.getSchemeSpecificPart() != null &&
1279 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1280
1281 String dialedNumber = handle.getSchemeSpecificPart();
1282 return (dialedNumber.equals("0") ||
1283 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1284 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1285 dialedNumber.equals("3") ||
1286 dialedNumber.equals("4") ||
1287 dialedNumber.equals("5"));
1288 }
1289 return false;
1290 }
1291
Santos Cordonf193ba42014-09-12 06:37:39 -07001292 private int getNumCallsWithState(int... states) {
1293 int count = 0;
1294 for (int state : states) {
1295 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001296 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001297 count++;
1298 }
1299 }
1300 }
1301 return count;
1302 }
1303
1304 private boolean hasMaximumLiveCalls() {
1305 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1306 }
1307
1308 private boolean hasMaximumHoldingCalls() {
1309 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1310 }
1311
1312 private boolean hasMaximumRingingCalls() {
1313 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1314 }
1315
1316 private boolean hasMaximumOutgoingCalls() {
1317 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1318 }
1319
1320 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1321 if (hasMaximumLiveCalls()) {
1322 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1323 // have to change.
1324 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301325 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1326 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001327
Santos Cordond1766502014-09-26 15:45:39 -07001328 if (call == liveCall) {
1329 // If the call is already the foreground call, then we are golden.
1330 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1331 // state since the call was already populated into the list.
1332 return true;
1333 }
1334
Andrew Leee6595182014-09-23 18:43:05 -07001335 if (hasMaximumOutgoingCalls()) {
1336 // Disconnect the current outgoing call if it's not an emergency call. If the user
1337 // tries to make two outgoing calls to different emergency call numbers, we will try
1338 // to connect the first outgoing call.
1339 if (isEmergency) {
1340 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1341 if (!outgoingCall.isEmergencyCall()) {
1342 outgoingCall.disconnect();
1343 return true;
1344 }
1345 }
1346 return false;
1347 }
1348
Santos Cordonf193ba42014-09-12 06:37:39 -07001349 if (hasMaximumHoldingCalls()) {
1350 // There is no more room for any more calls, unless it's an emergency.
1351 if (isEmergency) {
1352 // Kill the current active call, this is easier then trying to disconnect a
1353 // holding call and hold an active call.
1354 liveCall.disconnect();
1355 return true;
1356 }
1357 return false; // No more room!
1358 }
1359
1360 // We have room for at least one more holding call at this point.
1361
1362 // First thing, if we are trying to make a call with the same phone account as the live
1363 // call, then allow it so that the connection service can make its own decision about
1364 // how to handle the new call relative to the current one.
1365 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1366 return true;
1367 } else if (call.getTargetPhoneAccount() == null) {
1368 // Without a phone account, we can't say reliably that the call will fail.
1369 // If the user chooses the same phone account as the live call, then it's
1370 // still possible that the call can be made (like with CDMA calls not supporting
1371 // hold but they still support adding a call by going immediately into conference
1372 // mode). Return true here and we'll run this code again after user chooses an
1373 // account.
1374 return true;
1375 }
1376
1377 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001378 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001379 liveCall.hold();
1380 return true;
1381 }
1382
1383 // The live call cannot be held so we're out of luck here. There's no room.
1384 return false;
1385 }
1386 return true;
1387 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001388
1389 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001390 * Creates a new call for an existing connection.
1391 *
1392 * @param callId The id of the new call.
1393 * @param connection The connection information.
1394 * @return The new call.
1395 */
1396 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1397 Call call = new Call(
1398 mContext,
1399 mConnectionServiceRepository,
1400 connection.getHandle() /* handle */,
1401 null /* gatewayInfo */,
1402 null /* connectionManagerPhoneAccount */,
1403 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1404 false /* isIncoming */,
1405 false /* isConference */);
1406
1407 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001408 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001409 call.setCallerDisplayName(connection.getCallerDisplayName(),
1410 connection.getCallerDisplayNamePresentation());
1411
1412 call.addListener(this);
1413 addCall(call);
1414
1415 return call;
1416 }
1417
1418 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001419 * Dumps the state of the {@link CallsManager}.
1420 *
1421 * @param pw The {@code IndentingPrintWriter} to write the state to.
1422 */
1423 public void dump(IndentingPrintWriter pw) {
1424 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001425 if (mCalls != null) {
1426 pw.println("mCalls: ");
1427 pw.increaseIndent();
1428 for (Call call : mCalls) {
1429 pw.println(call);
1430 }
1431 pw.decreaseIndent();
1432 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001433 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1434
1435 if (mCallAudioManager != null) {
1436 pw.println("mCallAudioManager:");
1437 pw.increaseIndent();
1438 mCallAudioManager.dump(pw);
1439 pw.decreaseIndent();
1440 }
1441
1442 if (mTtyManager != null) {
1443 pw.println("mTtyManager:");
1444 pw.increaseIndent();
1445 mTtyManager.dump(pw);
1446 pw.decreaseIndent();
1447 }
1448
1449 if (mInCallController != null) {
1450 pw.println("mInCallController:");
1451 pw.increaseIndent();
1452 mInCallController.dump(pw);
1453 pw.decreaseIndent();
1454 }
1455
1456 if (mConnectionServiceRepository != null) {
1457 pw.println("mConnectionServiceRepository:");
1458 pw.increaseIndent();
1459 mConnectionServiceRepository.dump(pw);
1460 pw.decreaseIndent();
1461 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001462 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001463}