blob: 597155b3016bed007b36f532621f892e552d9f49 [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;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080027import android.telecom.Connection;
Andrew Lee701dc002014-09-11 21:29:12 -070028import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070029import android.telecom.GatewayInfo;
30import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070031import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070032import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070033import android.telecom.PhoneAccountHandle;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070035import android.telecom.VideoProfile;
Nancy Chena469f762014-12-09 18:29:20 -080036import android.telephony.PhoneNumberUtils;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070037import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080038
Tyler Gunn91d43cf2014-09-17 12:19:39 -070039import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080040
Santos Cordon4bc02452014-11-19 15:51:12 -080041import java.util.Collection;
Jay Shraunera82c8f72014-08-14 15:49:16 -070042import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070043import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070044import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070045import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070046import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070047import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080048
Ben Giladdd8c6082013-12-30 14:44:08 -080049/**
50 * Singleton.
51 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070052 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080053 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070054 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080055 */
Santos Cordon64c7e962014-07-02 15:15:27 -070056public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080057
Santos Cordondf399862014-08-06 04:39:15 -070058 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 interface CallsManagerListener {
60 void onCallAdded(Call call);
61 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070062 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070063 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070064 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 ConnectionServiceWrapper oldService,
66 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070068 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070069 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070070 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070071 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070072 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070073 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070074 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080075 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070076 }
77
Tyler Gunn91d43cf2014-09-17 12:19:39 -070078 /**
79 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
80 */
Santos Cordone6a33f22014-12-02 13:19:00 -080081 private static CallsManager sInstance = null;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070082
83 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080084
Santos Cordonf193ba42014-09-12 06:37:39 -070085 private static final int MAXIMUM_LIVE_CALLS = 1;
86 private static final int MAXIMUM_HOLD_CALLS = 1;
87 private static final int MAXIMUM_RINGING_CALLS = 1;
88 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080089 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070090
91 private static final int[] OUTGOING_CALL_STATES =
92 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
93
Santos Cordon91bd74c2014-11-07 16:10:22 -080094 private static final int[] LIVE_CALL_STATES =
95 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
96
Santos Cordon8e8b8d22013-12-19 14:14:05 -080097 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070098 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
99 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800100 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700101 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
102 * load factor before resizing, 1 means we only expect a single thread to
103 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800104 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700105 private final Set<Call> mCalls = Collections.newSetFromMap(
106 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800107
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700108 private final ConnectionServiceRepository mConnectionServiceRepository;
109 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
110 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700111 private final CallAudioManager mCallAudioManager;
112 private final Ringer mRinger;
Santos Cordon61e6f352014-12-03 02:53:34 -0800113 private final InCallWakeLockController mInCallWakeLockController;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700114 // For this set initial table size to 16 because we add 13 listeners in
115 // the CallsManager constructor.
116 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
117 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700118 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700119 private final WiredHeadsetManager mWiredHeadsetManager;
120 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700121 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700122 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700123 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700124 private final Context mContext;
125 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
126 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700127 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800128 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
129 /* Handler tied to thread in which CallManager was initialized. */
130 private final Handler mHandler = new Handler();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700131
Santos Cordon91bd74c2014-11-07 16:10:22 -0800132 private boolean mCanAddCall = true;
133
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700134 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 * The call the user is currently interacting with. This is the call that should have audio
136 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800137 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800139
Nancy Chena469f762014-12-09 18:29:20 -0800140 private Runnable mStopTone;
141
Santos Cordon766d04f2014-05-06 10:28:25 -0700142 /** Singleton accessor. */
143 static CallsManager getInstance() {
Santos Cordone6a33f22014-12-02 13:19:00 -0800144 return sInstance;
Santos Cordon766d04f2014-05-06 10:28:25 -0700145 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800146
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800147 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700148 * Sets the static singleton instance.
149 *
150 * @param instance The instance to set.
151 */
152 static void initialize(CallsManager instance) {
Santos Cordone6a33f22014-12-02 13:19:00 -0800153 sInstance = instance;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700154 }
155
156 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700157 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800158 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700159 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
160 PhoneAccountRegistrar phoneAccountRegistrar) {
161 mContext = context;
162 mPhoneAccountRegistrar = phoneAccountRegistrar;
163 mMissedCallNotifier = missedCallNotifier;
164 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
165 mWiredHeadsetManager = new WiredHeadsetManager(context);
166 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700167 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700168 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
169 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
170 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
171 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700172 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700173 mCallLogManager = new CallLogManager(context);
174 mInCallController = new InCallController(context);
175 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
176 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
177 context);
Santos Cordon61e6f352014-12-03 02:53:34 -0800178 mInCallWakeLockController = new InCallWakeLockController(context, this);
Santos Cordonae193062014-05-21 21:21:49 -0700179
Santos Cordondeb8c892014-05-30 01:38:03 -0700180 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700181 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700182 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700183 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700184 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700185 mListeners.add(new RingbackPlayer(this, playerFactory));
186 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700187 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700188 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700189 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700190 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700191 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700192 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800193 }
194
Santos Cordon766d04f2014-05-06 10:28:25 -0700195 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700196 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700197 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700198
Tyler Gunncde2e502014-08-12 14:35:58 -0700199 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700200 if (!mCalls.contains(call)) {
201 // Call was not added previously in startOutgoingCall due to it being a potential MMI
202 // code, so add it now.
203 addCall(call);
204 }
205
206 // The call's ConnectionService has been updated.
207 for (CallsManagerListener listener : mListeners) {
208 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700209 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700210
211 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700212 }
213
214 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700215 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700216 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700217
Andrew Leee6288a72014-10-01 13:50:02 -0700218 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700219 }
220
221 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700222 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700223 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700224 setCallState(incomingCall, CallState.RINGING);
225
226 if (hasMaximumRingingCalls()) {
227 incomingCall.reject(false, null);
228 // since the call was not added to the list of calls, we have to call the missed
229 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700230 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700231 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
232 } else {
233 addCall(incomingCall);
234 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700235 }
236
237 @Override
238 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700239 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700240 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800241 }
242
Ihab Awadcb387ac2014-05-28 16:49:38 -0700243 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700244 public void onSuccessfulUnknownCall(Call call, int callState) {
245 setCallState(call, callState);
246 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
247 addCall(call);
248 }
249
250 @Override
251 public void onFailedUnknownCall(Call call) {
252 Log.i(this, "onFailedUnknownCall for call %s", call);
253 setCallState(call, CallState.DISCONNECTED);
254 call.removeListener(this);
255 }
256
257 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700258 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700259 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700260 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700261 }
262 }
263
Evan Charlton352105c2014-06-03 14:10:54 -0700264 @Override
265 public void onPostDialWait(Call call, String remaining) {
266 mInCallController.onPostDialWait(call, remaining);
267 }
268
Santos Cordona1610702014-06-04 20:22:56 -0700269 @Override
Nancy Chena469f762014-12-09 18:29:20 -0800270 public void onPostDialChar(final Call call, char nextChar) {
271 if (PhoneNumberUtils.is12Key(nextChar)) {
272 // Play tone if it is one of the dialpad digits, canceling out the previously queued
273 // up stopTone runnable since playing a new tone automatically stops the previous tone.
274 if (mStopTone != null) {
275 mHandler.removeCallbacks(mStopTone);
276 }
277
278 mDtmfLocalTonePlayer.playTone(call, nextChar);
279
280 mStopTone = new Runnable() {
281 @Override
282 public void run() {
283 // Set a timeout to stop the tone in case there isn't another tone to follow.
284 mDtmfLocalTonePlayer.stopTone(call);
285 }
286 };
287 mHandler.postDelayed(
288 mStopTone,
289 Timeouts.getDelayBetweenDtmfTonesMillis(mContext.getContentResolver()));
290 } else if (nextChar == 0 || nextChar == TelecomManager.DTMF_CHARACTER_WAIT ||
291 nextChar == TelecomManager.DTMF_CHARACTER_PAUSE) {
292 // Stop the tone if a tone is playing, removing any other stopTone callbacks since
293 // the previous tone is being stopped anyway.
294 if (mStopTone != null) {
295 mHandler.removeCallbacks(mStopTone);
296 }
297 mDtmfLocalTonePlayer.stopTone(call);
298 } else {
299 Log.w(this, "onPostDialChar: invalid value %d", nextChar);
300 }
301 }
302
303 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700304 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700305 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800306 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700307 for (CallsManagerListener listener : mListeners) {
308 listener.onIsConferencedChanged(call);
309 }
310 }
311
312 @Override
313 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700314 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800315 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700316 for (CallsManagerListener listener : mListeners) {
317 listener.onIsConferencedChanged(call);
318 }
319 }
320
Ihab Awadff7493a2014-06-10 13:47:44 -0700321 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700322 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700323 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700324 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700325 }
326 }
327
Andrew Lee4a796602014-07-11 17:23:03 -0700328 @Override
329 public void onVideoStateChanged(Call call) {
330 for (CallsManagerListener listener : mListeners) {
331 listener.onVideoStateChanged(call);
332 }
333 }
334
Santos Cordoncf5b2912014-11-05 21:57:54 -0800335 @Override
336 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
337 mPendingCallsToDisconnect.add(call);
338 mHandler.postDelayed(new Runnable() {
339 @Override
340 public void run() {
341 if (mPendingCallsToDisconnect.remove(call)) {
342 Log.i(this, "Delayed disconnection of call: %s", call);
343 call.disconnect();
344 }
345 }
346 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
347
348 return true;
349 }
350
Santos Cordon4bc02452014-11-19 15:51:12 -0800351 Collection<Call> getCalls() {
352 return Collections.unmodifiableCollection(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700353 }
354
355 Call getForegroundCall() {
356 return mForegroundCall;
357 }
358
Santos Cordonae193062014-05-21 21:21:49 -0700359 Ringer getRinger() {
360 return mRinger;
361 }
362
Santos Cordonf3671a62014-05-29 21:51:53 -0700363 InCallController getInCallController() {
364 return mInCallController;
365 }
366
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700367 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700368 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700369 if (call.isEmergencyCall()) {
370 return true;
371 }
372 }
373 return false;
374 }
375
Andrew Lee45506232014-10-22 17:54:33 -0700376 boolean hasVideoCall() {
377 for (Call call : mCalls) {
378 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
379 return true;
380 }
381 }
382 return false;
383 }
384
Ihab Awad6fb37c82014-08-07 19:48:57 -0700385 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700386 return mCallAudioManager.getAudioState();
387 }
388
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700389 boolean isTtySupported() {
390 return mTtyManager.isTtySupported();
391 }
392
393 int getCurrentTtyMode() {
394 return mTtyManager.getCurrentTtyMode();
395 }
396
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700397 void addListener(CallsManagerListener listener) {
398 mListeners.add(listener);
399 }
400
401 void removeListener(CallsManagerListener listener) {
402 mListeners.remove(listener);
403 }
404
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800405 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700406 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800407 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700408 * @param phoneAccountHandle The phone account which contains the component name of the
409 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800410 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800411 */
Evan Charlton89176372014-07-19 18:23:09 -0700412 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800413 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700414 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700415 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700416 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700417 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700418 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700419 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700420 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700421 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700422 true /* isIncoming */,
423 false /* isConference */);
424
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700425 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700426 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700427 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700428 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800429 }
430
Yorke Lee9250e5f2014-10-01 13:39:09 -0700431 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
432 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
433 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
434 Call call = new Call(
435 mContext,
436 mConnectionServiceRepository,
437 handle,
438 null /* gatewayInfo */,
439 null /* connectionManagerPhoneAccount */,
440 phoneAccountHandle,
441 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
442 // to the existing connection instead of trying to create a new one.
443 true /* isIncoming */,
444 false /* isConference */);
Yorke Lee9250e5f2014-10-01 13:39:09 -0700445 call.setIsUnknown(true);
446 call.setExtras(extras);
447 call.addListener(this);
448 call.startCreateConnection(mPhoneAccountRegistrar);
449 }
450
Santos Cordoncf5b2912014-11-05 21:57:54 -0800451 private Call getNewOutgoingCall(Uri handle) {
452 // First check to see if we can reuse any of the calls that are waiting to disconnect.
453 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
Yorke Lee59979512014-12-02 01:03:40 -0800454 Call reusedCall = null;
Santos Cordoncf5b2912014-11-05 21:57:54 -0800455 for (Call pendingCall : mPendingCallsToDisconnect) {
Yorke Lee59979512014-12-02 01:03:40 -0800456 if (reusedCall == null && Objects.equals(pendingCall.getHandle(), handle)) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800457 mPendingCallsToDisconnect.remove(pendingCall);
458 Log.i(this, "Reusing disconnected call %s", pendingCall);
Yorke Lee59979512014-12-02 01:03:40 -0800459 reusedCall = pendingCall;
460 } else {
461 pendingCall.disconnect();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800462 }
463 }
Yorke Lee59979512014-12-02 01:03:40 -0800464 if (reusedCall != null) {
465 return reusedCall;
466 }
Santos Cordoncf5b2912014-11-05 21:57:54 -0800467
468 // Create a call with original handle. The handle may be changed when the call is attached
469 // to a connection service, but in most cases will remain the same.
470 return new Call(
471 mContext,
472 mConnectionServiceRepository,
473 handle,
474 null /* gatewayInfo */,
475 null /* connectionManagerPhoneAccount */,
476 null /* phoneAccountHandle */,
477 false /* isIncoming */,
478 false /* isConference */);
479 }
480
Nancy Chen0d3076c2014-07-30 14:45:44 -0700481 /**
482 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
483 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700484 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700485 * @param phoneAccountHandle The phone account which contains the component name of the
486 * connection service to use for this call.
487 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700488 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700489 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800490 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700491
492 List<PhoneAccountHandle> accounts =
493 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
494
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800495 Log.v(this, "startOutgoingCall found accounts = " + accounts);
496
Nancy Chenbc95db92014-11-24 13:19:18 -0800497 if (mForegroundCall != null && mForegroundCall.getTargetPhoneAccount() != null) {
498 // If there is an ongoing call, use the same phone account to place this new call.
499 phoneAccountHandle = mForegroundCall.getTargetPhoneAccount();
500 }
501
Nancy Chenbc9ef172014-08-15 17:11:57 -0700502 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
503 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700504 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700505 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700506 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700507 phoneAccountHandle = null;
508 }
509 }
510
511 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700512 // No preset account, check if default exists that supports the URI scheme for the
513 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700514 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700515 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700516 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700517 if (defaultAccountHandle != null) {
518 phoneAccountHandle = defaultAccountHandle;
519 }
520 }
521
Nancy Chen1c5926f2014-09-17 14:44:14 -0700522 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700523
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700524 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
525 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700526 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700527
528 // Do not support any more live calls. Our options are to move a call to hold, disconnect
529 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700530 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700531 // just cancel at this point.
Yorke Lee59979512014-12-02 01:03:40 -0800532 Log.i(this, "No remaining room for outgoing call: %s", call);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800533 if (mCalls.contains(call)) {
534 // This call can already exist if it is a reused call,
535 // See {@link #getNewOutgoingCall}.
536 call.disconnect();
537 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700538 return null;
539 }
540
Nancy Chen8d92f452014-10-20 21:43:44 -0700541 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
542 !isEmergencyCall;
543
544 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700545 // This is the state where the user is expected to select an account
546 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700547 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700548 } else {
549 call.setState(CallState.CONNECTING);
550 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700551
Nancy Chen1c5926f2014-09-17 14:44:14 -0700552 call.setExtras(extras);
553
Tyler Gunn504eb832014-10-13 14:15:27 -0700554 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700555 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700556 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800557 } else if (!mCalls.contains(call)) {
558 // We check if mCalls already contains the call because we could potentially be reusing
559 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700560 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700561 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700562
563 return call;
564 }
565
Ben Gilada0d9f752014-02-26 11:49:03 -0800566 /**
Yorke Lee33501632014-03-17 19:24:12 -0700567 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800568 *
Yorke Lee33501632014-03-17 19:24:12 -0700569 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700570 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700571 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700572 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700573 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800574 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700575 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
576 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700577 if (call == null) {
578 // don't do anything if the call no longer exists
579 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700580 return;
581 }
582
Nancy Chen201b4372014-09-08 14:18:24 -0700583 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700584
585 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700586 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700587 } else {
588 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
589 Log.pii(uriHandle), Log.pii(handle));
590 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700591
Nancy Chen0d3076c2014-07-30 14:45:44 -0700592 call.setHandle(uriHandle);
593 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700594 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700595 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700596
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700597 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
598 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700599 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700600 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700601 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700602 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700603
Santos Cordonf193ba42014-09-12 06:37:39 -0700604 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700605 // If the account has been set, proceed to place the outgoing call.
606 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700607 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700608 }
Yorke Leef98fb572014-03-05 10:56:55 -0800609 }
610
611 /**
Santos Cordona1610702014-06-04 20:22:56 -0700612 * Attempts to start a conference call for the specified call.
613 *
Santos Cordon12d61822014-07-29 16:02:20 -0700614 * @param call The call to conference.
615 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700616 */
Santos Cordon12d61822014-07-29 16:02:20 -0700617 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700618 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700619 }
620
621 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700622 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
623 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800624 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700625 *
626 * @param call The call to answer.
627 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800628 */
Andrew Lee38931d02014-07-16 10:17:36 -0700629 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700630 if (!mCalls.contains(call)) {
631 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800632 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700633 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700634 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700635 if (mForegroundCall != null && mForegroundCall != call &&
636 (mForegroundCall.isActive() ||
637 mForegroundCall.getState() == CallState.DIALING)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800638 if (0 == (mForegroundCall.getConnectionCapabilities()
639 & Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700640 // This call does not support hold. If it is from a different connection
641 // service, then disconnect it, otherwise allow the connection service to
642 // figure out the right states.
643 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
644 mForegroundCall.disconnect();
645 }
646 } else {
647 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
648 mForegroundCall, call);
649 mForegroundCall.hold();
650 }
Santos Cordondf399862014-08-06 04:39:15 -0700651 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700652 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700653 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700654 }
655
Santos Cordona56f2762014-03-24 15:55:53 -0700656 for (CallsManagerListener listener : mListeners) {
657 listener.onIncomingCallAnswered(call);
658 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800659
Santos Cordon61d0f702014-02-19 02:52:23 -0800660 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700661 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700662 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800663 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800664 }
665
666 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700667 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
668 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800669 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800670 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700671 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700672 if (!mCalls.contains(call)) {
673 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800674 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700675 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700676 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700677 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700678 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800679 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800680 }
681
682 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700683 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700684 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700685 * @param digit The DTMF digit to play.
686 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700687 void playDtmfTone(Call call, char digit) {
688 if (!mCalls.contains(call)) {
689 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700690 } else {
691 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700692 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700693 }
694 }
695
696 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700697 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700698 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700699 void stopDtmfTone(Call call) {
700 if (!mCalls.contains(call)) {
701 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700702 } else {
703 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700704 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700705 }
706 }
707
708 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700709 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700710 */
Evan Charlton352105c2014-06-03 14:10:54 -0700711 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700712 if (!mCalls.contains(call)) {
713 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700714 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700715 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700716 }
717 }
718
719 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700720 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800721 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
722 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800723 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700724 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700725 Log.v(this, "disconnectCall %s", call);
726
Sailesh Nepale59bb192014-04-01 18:33:59 -0700727 if (!mCalls.contains(call)) {
728 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800729 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700730 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800731 call.disconnect();
732 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800733 }
Santos Cordon681663d2014-01-30 04:32:15 -0800734
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700735 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700736 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700737 */
738 void disconnectAllCalls() {
739 Log.v(this, "disconnectAllCalls");
740
741 for (Call call : mCalls) {
742 disconnectCall(call);
743 }
744 }
745
Nancy Chenbc9ef172014-08-15 17:11:57 -0700746
Tyler Gunn61b92102014-08-19 07:42:20 -0700747 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700748 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700749 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
750 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700751 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700752 void holdCall(Call call) {
753 if (!mCalls.contains(call)) {
754 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700755 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700756 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700757 call.hold();
758 }
759 }
760
761 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700762 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700763 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
764 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700765 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700766 void unholdCall(Call call) {
767 if (!mCalls.contains(call)) {
768 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700769 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700770 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700771 for (Call c : mCalls) {
Tyler Gunne44adb42015-01-02 10:55:35 -0800772 // Only attempt to hold parent calls and not the individual children.
773 if (c != null && c.isAlive() && c != call && c.getParentCall() == null) {
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700774 c.hold();
775 }
776 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700777 call.unhold();
778 }
779 }
780
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700781 /** Called by the in-call UI to change the mute state. */
782 void mute(boolean shouldMute) {
783 mCallAudioManager.mute(shouldMute);
784 }
785
786 /**
787 * Called by the in-call UI to change the audio route, for example to change from earpiece to
788 * speaker phone.
789 */
790 void setAudioRoute(int route) {
791 mCallAudioManager.setAudioRoute(route);
792 }
793
Yorke Leed1346872014-07-28 14:38:45 -0700794 /** Called by the in-call UI to turn the proximity sensor on. */
795 void turnOnProximitySensor() {
796 mProximitySensorManager.turnOn();
797 }
798
799 /**
800 * Called by the in-call UI to turn the proximity sensor off.
801 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
802 * the screen will be kept off until the proximity sensor goes negative.
803 */
804 void turnOffProximitySensor(boolean screenOnImmediately) {
805 mProximitySensorManager.turnOff(screenOnImmediately);
806 }
807
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700808 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700809 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700810 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700811 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700812 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
813 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
814 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
815 // respecting a rewritten number or a canceled number. This is unlikely since
816 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
817 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700818 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700819
820 // Note: emergency calls never go through account selection dialog so they never
821 // arrive here.
822 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700823 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700824 } else {
825 call.disconnect();
826 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700827
828 if (setDefault) {
829 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
830 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700831 }
832 }
833
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700834 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700835 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700836 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700837 for (CallsManagerListener listener : mListeners) {
838 listener.onAudioStateChanged(oldAudioState, newAudioState);
839 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700840 }
841
Sailesh Nepale59bb192014-04-01 18:33:59 -0700842 void markCallAsRinging(Call call) {
843 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800844 }
845
Sailesh Nepale59bb192014-04-01 18:33:59 -0700846 void markCallAsDialing(Call call) {
847 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800848 }
849
Sailesh Nepale59bb192014-04-01 18:33:59 -0700850 void markCallAsActive(Call call) {
851 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700852
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700853 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700854 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700855 }
Santos Cordon681663d2014-01-30 04:32:15 -0800856 }
857
Sailesh Nepale59bb192014-04-01 18:33:59 -0700858 void markCallAsOnHold(Call call) {
859 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700860 }
861
Santos Cordon049b7b62014-01-30 05:34:26 -0800862 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700863 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
864 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800865 *
Andrew Lee701dc002014-09-11 21:29:12 -0700866 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800867 */
Andrew Lee701dc002014-09-11 21:29:12 -0700868 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
869 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700870 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700871 }
872
Ben Gilada0d9f752014-02-26 11:49:03 -0800873 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700874 * Removes an existing disconnected call, and notifies the in-call app.
875 */
876 void markCallAsRemoved(Call call) {
877 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700878 if (mLocallyDisconnectingCalls.contains(call)) {
879 mLocallyDisconnectingCalls.remove(call);
880 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
881 mForegroundCall.unhold();
882 }
883 }
Ihab Awada02bef52014-08-13 18:18:42 -0700884 }
885
886 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700887 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700888 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700889 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700890 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700891 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700892 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700893 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700894 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700895 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800896 if (call.getState() != CallState.DISCONNECTED) {
897 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
898 }
899 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700900 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700901 }
902 }
903 }
904
Santos Cordondeb8c892014-05-30 01:38:03 -0700905 boolean hasAnyCalls() {
906 return !mCalls.isEmpty();
907 }
908
Santos Cordonc7e85d42014-05-22 02:51:48 -0700909 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700910 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700911 }
912
913 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700914 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700915 }
916
Santos Cordondeb8c892014-05-30 01:38:03 -0700917 boolean onMediaButton(int type) {
918 if (hasAnyCalls()) {
919 if (HeadsetMediaButton.SHORT_PRESS == type) {
920 Call ringingCall = getFirstCallWithState(CallState.RINGING);
921 if (ringingCall == null) {
922 mCallAudioManager.toggleMute();
923 return true;
924 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700925 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700926 return true;
927 }
928 } else if (HeadsetMediaButton.LONG_PRESS == type) {
929 Log.d(this, "handleHeadsetHook: longpress -> hangup");
930 Call callToHangup = getFirstCallWithState(
931 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
932 if (callToHangup != null) {
933 callToHangup.disconnect();
934 return true;
935 }
936 }
937 }
938 return false;
939 }
940
941 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800942 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700943 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800944 boolean canAddCall() {
Andrew Lee113ad622014-12-01 18:33:10 -0800945 if (getFirstCallWithState(OUTGOING_CALL_STATES) != null) {
946 return false;
947 }
948
Santos Cordon91bd74c2014-11-07 16:10:22 -0800949 int count = 0;
950 for (Call call : mCalls) {
951 if (call.isEmergencyCall()) {
952 // We never support add call if one of the calls is an emergency call.
953 return false;
954 } else if (call.getParentCall() == null) {
955 count++;
956 }
Santos Cordon10838c22014-06-11 17:36:04 -0700957
Santos Cordon91bd74c2014-11-07 16:10:22 -0800958 // We do not check states for canAddCall. We treat disconnected calls the same
959 // and wait until they are removed instead. If we didn't count disconnected calls,
960 // we could put InCallServices into a state where they are showing two calls but
961 // also support add-call. Technically it's right, but overall looks better (UI-wise)
962 // and acts better if we wait until the call is removed.
963 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700964 return false;
965 }
966 }
967 return true;
968 }
969
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700970 Call getRingingCall() {
971 return getFirstCallWithState(CallState.RINGING);
972 }
973
974 Call getActiveCall() {
975 return getFirstCallWithState(CallState.ACTIVE);
976 }
977
Nancy Chen05a9e402014-09-26 14:14:32 -0700978 Call getDialingCall() {
979 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700980 }
981
982 Call getHeldCall() {
983 return getFirstCallWithState(CallState.ON_HOLD);
984 }
985
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700986 int getNumHeldCalls() {
987 int count = 0;
988 for (Call call : mCalls) {
989 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
990 count++;
991 }
992 }
993 return count;
994 }
995
Santos Cordonf193ba42014-09-12 06:37:39 -0700996 Call getFirstCallWithState(int... states) {
997 return getFirstCallWithState(null, states);
998 }
999
Santos Cordon10838c22014-06-11 17:36:04 -07001000 /**
Santos Cordondeb8c892014-05-30 01:38:03 -07001001 * Returns the first call that it finds with the given states. The states are treated as having
1002 * priority order so that any call with the first state will be returned before any call with
1003 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -07001004 *
1005 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -07001006 */
Santos Cordonf193ba42014-09-12 06:37:39 -07001007 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -07001008 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -07001009 // check the foreground first
1010 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
1011 return mForegroundCall;
1012 }
1013
Santos Cordondeb8c892014-05-30 01:38:03 -07001014 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001015 if (Objects.equals(callToSkip, call)) {
1016 continue;
1017 }
1018
1019 // Only operate on top-level calls
1020 if (call.getParentCall() != null) {
1021 continue;
1022 }
1023
Santos Cordondeb8c892014-05-30 01:38:03 -07001024 if (currentState == call.getState()) {
1025 return call;
1026 }
1027 }
1028 }
1029 return null;
1030 }
1031
Santos Cordon0fbe6322014-08-14 04:04:25 -07001032 Call createConferenceCall(
1033 PhoneAccountHandle phoneAccount,
1034 ParcelableConference parcelableConference) {
1035 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001036 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -07001037 mConnectionServiceRepository,
1038 null /* handle */,
1039 null /* gatewayInfo */,
1040 null /* connectionManagerPhoneAccount */,
1041 phoneAccount,
1042 false /* isIncoming */,
1043 true /* isConference */);
1044
1045 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001046 call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
Santos Cordon0fbe6322014-08-14 04:04:25 -07001047
1048 // TODO: Move this to be a part of addCall()
1049 call.addListener(this);
1050 addCall(call);
1051 return call;
1052 }
1053
Yorke Leef86db2e2014-09-12 17:56:48 -07001054 /**
1055 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1056 */
1057 int getCallState() {
1058 return mPhoneStateBroadcaster.getCallState();
1059 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001060
Santos Cordon4b2c1192014-03-19 18:15:38 -07001061 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001062 * Retrieves the {@link PhoneAccountRegistrar}.
1063 *
1064 * @return The {@link PhoneAccountRegistrar}.
1065 */
1066 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1067 return mPhoneAccountRegistrar;
1068 }
1069
1070 /**
1071 * Retrieves the {@link MissedCallNotifier}
1072 * @return The {@link MissedCallNotifier}.
1073 */
1074 MissedCallNotifier getMissedCallNotifier() {
1075 return mMissedCallNotifier;
1076 }
1077
1078 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001079 * Adds the specified call to the main list of live calls.
1080 *
1081 * @param call The call to add.
1082 */
1083 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001084 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001085 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001086 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001087 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001088
Santos Cordondf399862014-08-06 04:39:15 -07001089 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001090 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001091 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001092 if (Log.SYSTRACE_DEBUG) {
1093 Trace.beginSection(listener.getClass().toString() + " addCall");
1094 }
Santos Cordona56f2762014-03-24 15:55:53 -07001095 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001096 if (Log.SYSTRACE_DEBUG) {
1097 Trace.endSection();
1098 }
Santos Cordona56f2762014-03-24 15:55:53 -07001099 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001100 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001101 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001102 }
1103
Sailesh Nepal810735e2014-03-18 18:15:46 -07001104 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001105 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001106 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001107
Santos Cordon672321e2014-08-21 14:38:58 -07001108 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001109 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001110 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001111
1112 boolean shouldNotify = false;
1113 if (mCalls.contains(call)) {
1114 mCalls.remove(call);
1115 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001116 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001117
Sailesh Nepale59bb192014-04-01 18:33:59 -07001118 // Only broadcast changes for calls that are being tracked.
1119 if (shouldNotify) {
1120 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001121 if (Log.SYSTRACE_DEBUG) {
1122 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1123 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001124 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001125 if (Log.SYSTRACE_DEBUG) {
1126 Trace.endSection();
1127 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001128 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001129 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001130 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001131 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001132 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001133
Sailesh Nepal810735e2014-03-18 18:15:46 -07001134 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001135 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001136 *
1137 * @param call The call.
1138 * @param newState The new state of the call.
1139 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001140 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001141 if (call == null) {
1142 return;
1143 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001144 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001145 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1146 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001147 if (newState != oldState) {
1148 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1149 // 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 -07001150 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001151 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001152 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001153 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001154 // unexpected transition occurs.
1155 call.setState(newState);
1156
Yorke Leee4a9c412014-11-14 16:59:42 -08001157 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001158 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001159 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001160 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001161 if (Log.SYSTRACE_DEBUG) {
1162 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1163 }
Santos Cordona56f2762014-03-24 15:55:53 -07001164 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001165 if (Log.SYSTRACE_DEBUG) {
1166 Trace.endSection();
1167 }
Santos Cordona56f2762014-03-24 15:55:53 -07001168 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001169 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001170 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001171 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001172 }
1173 }
1174
1175 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001176 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001177 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001178 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001179 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001180 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001181 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001182 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001183 // of its state will be foreground by default and instead the connection service should
1184 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001185 // the call should play audio and listen to microphone if it wants.
1186
Santos Cordonf193ba42014-09-12 06:37:39 -07001187 // Only top-level calls can be in foreground
1188 if (call.getParentCall() != null) {
1189 continue;
1190 }
1191
Santos Cordon40f78c22014-04-07 02:11:42 -07001192 // Active calls have priority.
1193 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001194 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001195 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001196 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001197
1198 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001199 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001200 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001201 }
1202 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001203
Sailesh Nepal810735e2014-03-18 18:15:46 -07001204 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001205 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001206 Call oldForegroundCall = mForegroundCall;
1207 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001208
Santos Cordona56f2762014-03-24 15:55:53 -07001209 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001210 if (Log.SYSTRACE_DEBUG) {
1211 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1212 }
Santos Cordona56f2762014-03-24 15:55:53 -07001213 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001214 if (Log.SYSTRACE_DEBUG) {
1215 Trace.endSection();
1216 }
Santos Cordona56f2762014-03-24 15:55:53 -07001217 }
Santos Cordon681663d2014-01-30 04:32:15 -08001218 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001219 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001220 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001221
Santos Cordon91bd74c2014-11-07 16:10:22 -08001222 private void updateCanAddCall() {
1223 boolean newCanAddCall = canAddCall();
1224 if (newCanAddCall != mCanAddCall) {
1225 mCanAddCall = newCanAddCall;
1226 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001227 if (Log.SYSTRACE_DEBUG) {
1228 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1229 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001230 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001231 if (Log.SYSTRACE_DEBUG) {
1232 Trace.endSection();
1233 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001234 }
1235 }
1236 }
1237
1238 private void updateCallsManagerState() {
1239 updateForegroundCall();
1240 updateCanAddCall();
1241 }
1242
Yorke Leeb701f6d2014-08-12 14:04:19 -07001243 private boolean isPotentialMMICode(Uri handle) {
1244 return (handle != null && handle.getSchemeSpecificPart() != null
1245 && handle.getSchemeSpecificPart().contains("#"));
1246 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001247
Tyler Gunn5b452df2014-10-01 15:02:58 -07001248 /**
1249 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1250 * MMI codes which can be dialed when one or more calls are in progress.
1251 * <P>
1252 * Checks for numbers formatted similar to the MMI codes defined in:
1253 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1254 * and
1255 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1256 *
1257 * @param handle The URI to call.
1258 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1259 */
1260 private boolean isPotentialInCallMMICode(Uri handle) {
1261 if (handle != null && handle.getSchemeSpecificPart() != null &&
1262 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1263
1264 String dialedNumber = handle.getSchemeSpecificPart();
1265 return (dialedNumber.equals("0") ||
1266 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1267 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1268 dialedNumber.equals("3") ||
1269 dialedNumber.equals("4") ||
1270 dialedNumber.equals("5"));
1271 }
1272 return false;
1273 }
1274
Santos Cordonf193ba42014-09-12 06:37:39 -07001275 private int getNumCallsWithState(int... states) {
1276 int count = 0;
1277 for (int state : states) {
1278 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001279 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001280 count++;
1281 }
1282 }
1283 }
1284 return count;
1285 }
1286
1287 private boolean hasMaximumLiveCalls() {
1288 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1289 }
1290
1291 private boolean hasMaximumHoldingCalls() {
1292 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1293 }
1294
1295 private boolean hasMaximumRingingCalls() {
1296 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1297 }
1298
1299 private boolean hasMaximumOutgoingCalls() {
1300 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1301 }
1302
1303 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1304 if (hasMaximumLiveCalls()) {
1305 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1306 // have to change.
1307 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
Anju Mathapatic88574c2014-12-16 14:46:19 +05301308 Log.i(this, "makeRoomForOutgoingCall call = " + call + " livecall = " +
1309 liveCall);
Santos Cordonf193ba42014-09-12 06:37:39 -07001310
Santos Cordond1766502014-09-26 15:45:39 -07001311 if (call == liveCall) {
1312 // If the call is already the foreground call, then we are golden.
1313 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1314 // state since the call was already populated into the list.
1315 return true;
1316 }
1317
Andrew Leee6595182014-09-23 18:43:05 -07001318 if (hasMaximumOutgoingCalls()) {
1319 // Disconnect the current outgoing call if it's not an emergency call. If the user
1320 // tries to make two outgoing calls to different emergency call numbers, we will try
1321 // to connect the first outgoing call.
1322 if (isEmergency) {
1323 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1324 if (!outgoingCall.isEmergencyCall()) {
1325 outgoingCall.disconnect();
1326 return true;
1327 }
1328 }
1329 return false;
1330 }
1331
Santos Cordonf193ba42014-09-12 06:37:39 -07001332 if (hasMaximumHoldingCalls()) {
1333 // There is no more room for any more calls, unless it's an emergency.
1334 if (isEmergency) {
1335 // Kill the current active call, this is easier then trying to disconnect a
1336 // holding call and hold an active call.
1337 liveCall.disconnect();
1338 return true;
1339 }
1340 return false; // No more room!
1341 }
1342
1343 // We have room for at least one more holding call at this point.
1344
1345 // First thing, if we are trying to make a call with the same phone account as the live
1346 // call, then allow it so that the connection service can make its own decision about
1347 // how to handle the new call relative to the current one.
1348 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1349 return true;
1350 } else if (call.getTargetPhoneAccount() == null) {
1351 // Without a phone account, we can't say reliably that the call will fail.
1352 // If the user chooses the same phone account as the live call, then it's
1353 // still possible that the call can be made (like with CDMA calls not supporting
1354 // hold but they still support adding a call by going immediately into conference
1355 // mode). Return true here and we'll run this code again after user chooses an
1356 // account.
1357 return true;
1358 }
1359
1360 // Try to hold the live call before attempting the new outgoing call.
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001361 if (liveCall.can(Connection.CAPABILITY_HOLD)) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001362 liveCall.hold();
1363 return true;
1364 }
1365
1366 // The live call cannot be held so we're out of luck here. There's no room.
1367 return false;
1368 }
1369 return true;
1370 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001371
1372 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001373 * Creates a new call for an existing connection.
1374 *
1375 * @param callId The id of the new call.
1376 * @param connection The connection information.
1377 * @return The new call.
1378 */
1379 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1380 Call call = new Call(
1381 mContext,
1382 mConnectionServiceRepository,
1383 connection.getHandle() /* handle */,
1384 null /* gatewayInfo */,
1385 null /* connectionManagerPhoneAccount */,
1386 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1387 false /* isIncoming */,
1388 false /* isConference */);
1389
1390 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001391 call.setConnectionCapabilities(connection.getConnectionCapabilities());
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001392 call.setCallerDisplayName(connection.getCallerDisplayName(),
1393 connection.getCallerDisplayNamePresentation());
1394
1395 call.addListener(this);
1396 addCall(call);
1397
1398 return call;
1399 }
1400
1401 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001402 * Dumps the state of the {@link CallsManager}.
1403 *
1404 * @param pw The {@code IndentingPrintWriter} to write the state to.
1405 */
1406 public void dump(IndentingPrintWriter pw) {
1407 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001408 if (mCalls != null) {
1409 pw.println("mCalls: ");
1410 pw.increaseIndent();
1411 for (Call call : mCalls) {
1412 pw.println(call);
1413 }
1414 pw.decreaseIndent();
1415 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001416 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1417
1418 if (mCallAudioManager != null) {
1419 pw.println("mCallAudioManager:");
1420 pw.increaseIndent();
1421 mCallAudioManager.dump(pw);
1422 pw.decreaseIndent();
1423 }
1424
1425 if (mTtyManager != null) {
1426 pw.println("mTtyManager:");
1427 pw.increaseIndent();
1428 mTtyManager.dump(pw);
1429 pw.decreaseIndent();
1430 }
1431
1432 if (mInCallController != null) {
1433 pw.println("mInCallController:");
1434 pw.increaseIndent();
1435 mInCallController.dump(pw);
1436 pw.decreaseIndent();
1437 }
1438
1439 if (mConnectionServiceRepository != null) {
1440 pw.println("mConnectionServiceRepository:");
1441 pw.increaseIndent();
1442 mConnectionServiceRepository.dump(pw);
1443 pw.decreaseIndent();
1444 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001445 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001446}