blob: 5c8f24ffaca93967e8845c7cd4cf1cfb1e787cb5 [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;
Tyler Gunn504eb832014-10-13 14:15:27 -070020import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Sailesh Nepalce704b92014-03-17 18:31:43 -070023import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080024import android.os.Bundle;
Santos Cordoncf5b2912014-11-05 21:57:54 -080025import android.os.Handler;
Yorke Leee4a9c412014-11-14 16:59:42 -080026import android.os.Trace;
Santos Cordonf193ba42014-09-12 06:37:39 -070027import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070028import android.telecom.AudioState;
29import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070030import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070031import android.telecom.GatewayInfo;
32import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070033import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070034import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070035import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070036import android.telecom.PhoneCapabilities;
Tyler Gunn5b452df2014-10-01 15:02:58 -070037import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070038import android.telecom.VideoProfile;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070039import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080040
Tyler Gunn91d43cf2014-09-17 12:19:39 -070041import com.android.internal.util.IndentingPrintWriter;
Yorke Leee4a9c412014-11-14 16:59:42 -080042
Sailesh Nepal810735e2014-03-18 18:15:46 -070043import com.google.common.collect.ImmutableCollection;
44import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080045
Jay Shraunera82c8f72014-08-14 15:49:16 -070046import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070047import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070048import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070049import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070050import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070051import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080052
Ben Giladdd8c6082013-12-30 14:44:08 -080053/**
54 * Singleton.
55 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070056 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080057 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070058 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080059 */
Santos Cordon64c7e962014-07-02 15:15:27 -070060public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080061
Santos Cordondf399862014-08-06 04:39:15 -070062 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070063 interface CallsManagerListener {
64 void onCallAdded(Call call);
65 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070066 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070067 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070068 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070069 ConnectionServiceWrapper oldService,
70 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070071 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070072 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070073 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070074 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070075 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070076 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070077 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070078 void onVideoStateChanged(Call call);
Santos Cordon91bd74c2014-11-07 16:10:22 -080079 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070080 }
81
Tyler Gunn91d43cf2014-09-17 12:19:39 -070082 /**
83 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
84 */
85 private static CallsManager INSTANCE = null;
86
87 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080088
Santos Cordonf193ba42014-09-12 06:37:39 -070089 private static final int MAXIMUM_LIVE_CALLS = 1;
90 private static final int MAXIMUM_HOLD_CALLS = 1;
91 private static final int MAXIMUM_RINGING_CALLS = 1;
92 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080093 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070094
95 private static final int[] OUTGOING_CALL_STATES =
96 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
97
Santos Cordon91bd74c2014-11-07 16:10:22 -080098 private static final int[] LIVE_CALL_STATES =
99 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
100
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700102 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
103 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800104 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700105 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
106 * load factor before resizing, 1 means we only expect a single thread to
107 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800108 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700109 private final Set<Call> mCalls = Collections.newSetFromMap(
110 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800111
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700112 private final ConnectionServiceRepository mConnectionServiceRepository;
113 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
114 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700115 private final CallAudioManager mCallAudioManager;
116 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700117 // For this set initial table size to 16 because we add 13 listeners in
118 // the CallsManager constructor.
119 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
120 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700121 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700122 private final WiredHeadsetManager mWiredHeadsetManager;
123 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700124 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700125 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700126 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700127 private final Context mContext;
128 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
129 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700130 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800131 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
132 /* Handler tied to thread in which CallManager was initialized. */
133 private final Handler mHandler = new Handler();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700134
Santos Cordon91bd74c2014-11-07 16:10:22 -0800135 private boolean mCanAddCall = true;
136
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700137 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 * The call the user is currently interacting with. This is the call that should have audio
139 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800140 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700141 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800142
Santos Cordon766d04f2014-05-06 10:28:25 -0700143 /** Singleton accessor. */
144 static CallsManager getInstance() {
145 return INSTANCE;
146 }
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) {
154 INSTANCE = instance;
155 }
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 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
Santos Cordona1610702014-06-04 20:22:56 -0700270 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700271 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800272 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700273 for (CallsManagerListener listener : mListeners) {
274 listener.onIsConferencedChanged(call);
275 }
276 }
277
278 @Override
279 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700280 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800281 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700282 for (CallsManagerListener listener : mListeners) {
283 listener.onIsConferencedChanged(call);
284 }
285 }
286
Ihab Awadff7493a2014-06-10 13:47:44 -0700287 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700288 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700289 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700290 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700291 }
292 }
293
Andrew Lee4a796602014-07-11 17:23:03 -0700294 @Override
295 public void onVideoStateChanged(Call call) {
296 for (CallsManagerListener listener : mListeners) {
297 listener.onVideoStateChanged(call);
298 }
299 }
300
Santos Cordoncf5b2912014-11-05 21:57:54 -0800301 @Override
302 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
303 mPendingCallsToDisconnect.add(call);
304 mHandler.postDelayed(new Runnable() {
305 @Override
306 public void run() {
307 if (mPendingCallsToDisconnect.remove(call)) {
308 Log.i(this, "Delayed disconnection of call: %s", call);
309 call.disconnect();
310 }
311 }
312 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
313
314 return true;
315 }
316
Sailesh Nepal810735e2014-03-18 18:15:46 -0700317 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700318 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700319 }
320
321 Call getForegroundCall() {
322 return mForegroundCall;
323 }
324
Santos Cordonae193062014-05-21 21:21:49 -0700325 Ringer getRinger() {
326 return mRinger;
327 }
328
Santos Cordonf3671a62014-05-29 21:51:53 -0700329 InCallController getInCallController() {
330 return mInCallController;
331 }
332
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700333 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700334 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700335 if (call.isEmergencyCall()) {
336 return true;
337 }
338 }
339 return false;
340 }
341
Andrew Lee45506232014-10-22 17:54:33 -0700342 boolean hasVideoCall() {
343 for (Call call : mCalls) {
344 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
345 return true;
346 }
347 }
348 return false;
349 }
350
Ihab Awad6fb37c82014-08-07 19:48:57 -0700351 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700352 return mCallAudioManager.getAudioState();
353 }
354
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700355 boolean isTtySupported() {
356 return mTtyManager.isTtySupported();
357 }
358
359 int getCurrentTtyMode() {
360 return mTtyManager.getCurrentTtyMode();
361 }
362
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700363 void addListener(CallsManagerListener listener) {
364 mListeners.add(listener);
365 }
366
367 void removeListener(CallsManagerListener listener) {
368 mListeners.remove(listener);
369 }
370
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800371 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700372 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800373 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700374 * @param phoneAccountHandle The phone account which contains the component name of the
375 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800376 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800377 */
Evan Charlton89176372014-07-19 18:23:09 -0700378 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800379 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700380 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700381 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700382 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700383 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700384 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700385 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700386 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700387 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700388 true /* isIncoming */,
389 false /* isConference */);
390
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700391 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700392 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700393 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700394 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800395 }
396
Yorke Lee9250e5f2014-10-01 13:39:09 -0700397 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
398 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
399 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
400 Call call = new Call(
401 mContext,
402 mConnectionServiceRepository,
403 handle,
404 null /* gatewayInfo */,
405 null /* connectionManagerPhoneAccount */,
406 phoneAccountHandle,
407 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
408 // to the existing connection instead of trying to create a new one.
409 true /* isIncoming */,
410 false /* isConference */);
411 call.setConnectTimeMillis(System.currentTimeMillis());
412 call.setIsUnknown(true);
413 call.setExtras(extras);
414 call.addListener(this);
415 call.startCreateConnection(mPhoneAccountRegistrar);
416 }
417
Santos Cordoncf5b2912014-11-05 21:57:54 -0800418 private Call getNewOutgoingCall(Uri handle) {
419 // First check to see if we can reuse any of the calls that are waiting to disconnect.
420 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
421 for (Call pendingCall : mPendingCallsToDisconnect) {
422 if (Objects.equals(pendingCall.getHandle(), handle)) {
423 mPendingCallsToDisconnect.remove(pendingCall);
424 Log.i(this, "Reusing disconnected call %s", pendingCall);
425 return pendingCall;
426 }
427 }
428
429 // Create a call with original handle. The handle may be changed when the call is attached
430 // to a connection service, but in most cases will remain the same.
431 return new Call(
432 mContext,
433 mConnectionServiceRepository,
434 handle,
435 null /* gatewayInfo */,
436 null /* connectionManagerPhoneAccount */,
437 null /* phoneAccountHandle */,
438 false /* isIncoming */,
439 false /* isConference */);
440 }
441
Nancy Chen0d3076c2014-07-30 14:45:44 -0700442 /**
443 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
444 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700445 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700446 * @param phoneAccountHandle The phone account which contains the component name of the
447 * connection service to use for this call.
448 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700449 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700450 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800451 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700452
453 List<PhoneAccountHandle> accounts =
454 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
455
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800456 Log.v(this, "startOutgoingCall found accounts = " + accounts);
457
Nancy Chenbc9ef172014-08-15 17:11:57 -0700458 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
459 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700460 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700461 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700462 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700463 phoneAccountHandle = null;
464 }
465 }
466
467 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700468 // No preset account, check if default exists that supports the URI scheme for the
469 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700470 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700471 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700472 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700473 if (defaultAccountHandle != null) {
474 phoneAccountHandle = defaultAccountHandle;
475 }
476 }
477
Nancy Chen1c5926f2014-09-17 14:44:14 -0700478 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700479
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700480 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
481 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700482 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700483
484 // Do not support any more live calls. Our options are to move a call to hold, disconnect
485 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700486 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700487 // just cancel at this point.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800488 if (mCalls.contains(call)) {
489 // This call can already exist if it is a reused call,
490 // See {@link #getNewOutgoingCall}.
491 call.disconnect();
492 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700493 return null;
494 }
495
Nancy Chen8d92f452014-10-20 21:43:44 -0700496 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
497 !isEmergencyCall;
498
499 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700500 // This is the state where the user is expected to select an account
501 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700502 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700503 } else {
504 call.setState(CallState.CONNECTING);
505 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700506
Nancy Chen1c5926f2014-09-17 14:44:14 -0700507 call.setExtras(extras);
508
Tyler Gunn504eb832014-10-13 14:15:27 -0700509 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700510 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700511 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800512 } else if (!mCalls.contains(call)) {
513 // We check if mCalls already contains the call because we could potentially be reusing
514 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700515 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700516 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700517
518 return call;
519 }
520
Ben Gilada0d9f752014-02-26 11:49:03 -0800521 /**
Yorke Lee33501632014-03-17 19:24:12 -0700522 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800523 *
Yorke Lee33501632014-03-17 19:24:12 -0700524 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700525 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700526 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700527 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700528 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800529 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700530 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
531 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700532 if (call == null) {
533 // don't do anything if the call no longer exists
534 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700535 return;
536 }
537
Nancy Chen201b4372014-09-08 14:18:24 -0700538 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700539
540 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700541 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700542 } else {
543 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
544 Log.pii(uriHandle), Log.pii(handle));
545 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700546
Nancy Chen0d3076c2014-07-30 14:45:44 -0700547 call.setHandle(uriHandle);
548 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700549 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700550 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700551
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700552 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
553 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700554 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700555 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700556 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700557 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700558
Santos Cordonf193ba42014-09-12 06:37:39 -0700559 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700560 // If the account has been set, proceed to place the outgoing call.
561 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700562 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700563 }
Yorke Leef98fb572014-03-05 10:56:55 -0800564 }
565
566 /**
Santos Cordona1610702014-06-04 20:22:56 -0700567 * Attempts to start a conference call for the specified call.
568 *
Santos Cordon12d61822014-07-29 16:02:20 -0700569 * @param call The call to conference.
570 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700571 */
Santos Cordon12d61822014-07-29 16:02:20 -0700572 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700573 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700574 }
575
576 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700577 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
578 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800579 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700580 *
581 * @param call The call to answer.
582 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800583 */
Andrew Lee38931d02014-07-16 10:17:36 -0700584 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700585 if (!mCalls.contains(call)) {
586 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800587 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700588 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700589 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700590 if (mForegroundCall != null && mForegroundCall != call &&
591 (mForegroundCall.isActive() ||
592 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700593 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
594 // This call does not support hold. If it is from a different connection
595 // service, then disconnect it, otherwise allow the connection service to
596 // figure out the right states.
597 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
598 mForegroundCall.disconnect();
599 }
600 } else {
601 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
602 mForegroundCall, call);
603 mForegroundCall.hold();
604 }
Santos Cordondf399862014-08-06 04:39:15 -0700605 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700606 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700607 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700608 }
609
Santos Cordona56f2762014-03-24 15:55:53 -0700610 for (CallsManagerListener listener : mListeners) {
611 listener.onIncomingCallAnswered(call);
612 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800613
Santos Cordon61d0f702014-02-19 02:52:23 -0800614 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700615 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700616 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800617 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800618 }
619
620 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700621 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
622 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800623 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800624 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700625 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700626 if (!mCalls.contains(call)) {
627 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800628 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700629 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700630 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700631 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700632 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800633 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800634 }
635
636 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700637 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700638 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700639 * @param digit The DTMF digit to play.
640 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700641 void playDtmfTone(Call call, char digit) {
642 if (!mCalls.contains(call)) {
643 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700644 } else {
645 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700646 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700647 }
648 }
649
650 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700651 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700652 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700653 void stopDtmfTone(Call call) {
654 if (!mCalls.contains(call)) {
655 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700656 } else {
657 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700658 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700659 }
660 }
661
662 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700663 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700664 */
Evan Charlton352105c2014-06-03 14:10:54 -0700665 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700666 if (!mCalls.contains(call)) {
667 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700668 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700669 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700670 }
671 }
672
673 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700674 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800675 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
676 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800677 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700678 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700679 Log.v(this, "disconnectCall %s", call);
680
Sailesh Nepale59bb192014-04-01 18:33:59 -0700681 if (!mCalls.contains(call)) {
682 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800683 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700684 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800685 call.disconnect();
686 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800687 }
Santos Cordon681663d2014-01-30 04:32:15 -0800688
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700689 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700690 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700691 */
692 void disconnectAllCalls() {
693 Log.v(this, "disconnectAllCalls");
694
695 for (Call call : mCalls) {
696 disconnectCall(call);
697 }
698 }
699
Nancy Chenbc9ef172014-08-15 17:11:57 -0700700
Tyler Gunn61b92102014-08-19 07:42:20 -0700701 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700702 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700703 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
704 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700705 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700706 void holdCall(Call call) {
707 if (!mCalls.contains(call)) {
708 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700709 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700710 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700711 call.hold();
712 }
713 }
714
715 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700716 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700717 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
718 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700719 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700720 void unholdCall(Call call) {
721 if (!mCalls.contains(call)) {
722 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700723 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700724 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700725 for (Call c : mCalls) {
726 if (c != null && c.isAlive() && c != call) {
727 c.hold();
728 }
729 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700730 call.unhold();
731 }
732 }
733
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700734 /** Called by the in-call UI to change the mute state. */
735 void mute(boolean shouldMute) {
736 mCallAudioManager.mute(shouldMute);
737 }
738
739 /**
740 * Called by the in-call UI to change the audio route, for example to change from earpiece to
741 * speaker phone.
742 */
743 void setAudioRoute(int route) {
744 mCallAudioManager.setAudioRoute(route);
745 }
746
Yorke Leed1346872014-07-28 14:38:45 -0700747 /** Called by the in-call UI to turn the proximity sensor on. */
748 void turnOnProximitySensor() {
749 mProximitySensorManager.turnOn();
750 }
751
752 /**
753 * Called by the in-call UI to turn the proximity sensor off.
754 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
755 * the screen will be kept off until the proximity sensor goes negative.
756 */
757 void turnOffProximitySensor(boolean screenOnImmediately) {
758 mProximitySensorManager.turnOff(screenOnImmediately);
759 }
760
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700761 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700762 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700763 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700764 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700765 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
766 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
767 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
768 // respecting a rewritten number or a canceled number. This is unlikely since
769 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
770 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700771 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700772
773 // Note: emergency calls never go through account selection dialog so they never
774 // arrive here.
775 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700776 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700777 } else {
778 call.disconnect();
779 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700780
781 if (setDefault) {
782 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
783 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700784 }
785 }
786
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700787 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700788 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700789 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700790 for (CallsManagerListener listener : mListeners) {
791 listener.onAudioStateChanged(oldAudioState, newAudioState);
792 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700793 }
794
Sailesh Nepale59bb192014-04-01 18:33:59 -0700795 void markCallAsRinging(Call call) {
796 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800797 }
798
Sailesh Nepale59bb192014-04-01 18:33:59 -0700799 void markCallAsDialing(Call call) {
800 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800801 }
802
Sailesh Nepale59bb192014-04-01 18:33:59 -0700803 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700804 if (call.getConnectTimeMillis() == 0) {
805 call.setConnectTimeMillis(System.currentTimeMillis());
806 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700807 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700808
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700809 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700810 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700811 }
Santos Cordon681663d2014-01-30 04:32:15 -0800812 }
813
Sailesh Nepale59bb192014-04-01 18:33:59 -0700814 void markCallAsOnHold(Call call) {
815 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700816 }
817
Santos Cordon049b7b62014-01-30 05:34:26 -0800818 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700819 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
820 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800821 *
Andrew Lee701dc002014-09-11 21:29:12 -0700822 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800823 */
Andrew Lee701dc002014-09-11 21:29:12 -0700824 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
825 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700826 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700827 }
828
Ben Gilada0d9f752014-02-26 11:49:03 -0800829 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700830 * Removes an existing disconnected call, and notifies the in-call app.
831 */
832 void markCallAsRemoved(Call call) {
833 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700834 if (mLocallyDisconnectingCalls.contains(call)) {
835 mLocallyDisconnectingCalls.remove(call);
836 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
837 mForegroundCall.unhold();
838 }
839 }
Ihab Awada02bef52014-08-13 18:18:42 -0700840 }
841
842 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700843 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700844 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700845 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700846 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700847 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700848 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700849 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700850 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700851 if (call.getConnectionService() == service) {
Yorke Lee2af16b62014-11-11 17:36:57 -0800852 if (call.getState() != CallState.DISCONNECTED) {
853 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
854 }
855 markCallAsRemoved(call);
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700856 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700857 }
858 }
859 }
860
Santos Cordondeb8c892014-05-30 01:38:03 -0700861 boolean hasAnyCalls() {
862 return !mCalls.isEmpty();
863 }
864
Santos Cordonc7e85d42014-05-22 02:51:48 -0700865 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700866 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700867 }
868
869 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700870 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700871 }
872
Santos Cordondeb8c892014-05-30 01:38:03 -0700873 boolean onMediaButton(int type) {
874 if (hasAnyCalls()) {
875 if (HeadsetMediaButton.SHORT_PRESS == type) {
876 Call ringingCall = getFirstCallWithState(CallState.RINGING);
877 if (ringingCall == null) {
878 mCallAudioManager.toggleMute();
879 return true;
880 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700881 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700882 return true;
883 }
884 } else if (HeadsetMediaButton.LONG_PRESS == type) {
885 Log.d(this, "handleHeadsetHook: longpress -> hangup");
886 Call callToHangup = getFirstCallWithState(
887 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
888 if (callToHangup != null) {
889 callToHangup.disconnect();
890 return true;
891 }
892 }
893 }
894 return false;
895 }
896
897 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800898 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700899 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800900 boolean canAddCall() {
901 int count = 0;
902 for (Call call : mCalls) {
903 if (call.isEmergencyCall()) {
904 // We never support add call if one of the calls is an emergency call.
905 return false;
906 } else if (call.getParentCall() == null) {
907 count++;
908 }
Santos Cordon10838c22014-06-11 17:36:04 -0700909
Santos Cordon91bd74c2014-11-07 16:10:22 -0800910 // We do not check states for canAddCall. We treat disconnected calls the same
911 // and wait until they are removed instead. If we didn't count disconnected calls,
912 // we could put InCallServices into a state where they are showing two calls but
913 // also support add-call. Technically it's right, but overall looks better (UI-wise)
914 // and acts better if we wait until the call is removed.
915 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700916 return false;
917 }
918 }
919 return true;
920 }
921
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700922 Call getRingingCall() {
923 return getFirstCallWithState(CallState.RINGING);
924 }
925
926 Call getActiveCall() {
927 return getFirstCallWithState(CallState.ACTIVE);
928 }
929
Nancy Chen05a9e402014-09-26 14:14:32 -0700930 Call getDialingCall() {
931 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700932 }
933
934 Call getHeldCall() {
935 return getFirstCallWithState(CallState.ON_HOLD);
936 }
937
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700938 int getNumHeldCalls() {
939 int count = 0;
940 for (Call call : mCalls) {
941 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
942 count++;
943 }
944 }
945 return count;
946 }
947
Santos Cordonf193ba42014-09-12 06:37:39 -0700948 Call getFirstCallWithState(int... states) {
949 return getFirstCallWithState(null, states);
950 }
951
Santos Cordon10838c22014-06-11 17:36:04 -0700952 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700953 * Returns the first call that it finds with the given states. The states are treated as having
954 * priority order so that any call with the first state will be returned before any call with
955 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700956 *
957 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700958 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700959 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700960 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700961 // check the foreground first
962 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
963 return mForegroundCall;
964 }
965
Santos Cordondeb8c892014-05-30 01:38:03 -0700966 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700967 if (Objects.equals(callToSkip, call)) {
968 continue;
969 }
970
971 // Only operate on top-level calls
972 if (call.getParentCall() != null) {
973 continue;
974 }
975
Santos Cordondeb8c892014-05-30 01:38:03 -0700976 if (currentState == call.getState()) {
977 return call;
978 }
979 }
980 }
981 return null;
982 }
983
Santos Cordon0fbe6322014-08-14 04:04:25 -0700984 Call createConferenceCall(
985 PhoneAccountHandle phoneAccount,
986 ParcelableConference parcelableConference) {
987 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700988 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700989 mConnectionServiceRepository,
990 null /* handle */,
991 null /* gatewayInfo */,
992 null /* connectionManagerPhoneAccount */,
993 phoneAccount,
994 false /* isIncoming */,
995 true /* isConference */);
996
997 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700998 if (call.getState() == CallState.ACTIVE) {
999 call.setConnectTimeMillis(System.currentTimeMillis());
1000 }
Santos Cordon0fbe6322014-08-14 04:04:25 -07001001 call.setCallCapabilities(parcelableConference.getCapabilities());
1002
1003 // TODO: Move this to be a part of addCall()
1004 call.addListener(this);
1005 addCall(call);
1006 return call;
1007 }
1008
Yorke Leef86db2e2014-09-12 17:56:48 -07001009 /**
1010 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1011 */
1012 int getCallState() {
1013 return mPhoneStateBroadcaster.getCallState();
1014 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001015
Santos Cordon4b2c1192014-03-19 18:15:38 -07001016 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001017 * Retrieves the {@link PhoneAccountRegistrar}.
1018 *
1019 * @return The {@link PhoneAccountRegistrar}.
1020 */
1021 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1022 return mPhoneAccountRegistrar;
1023 }
1024
1025 /**
1026 * Retrieves the {@link MissedCallNotifier}
1027 * @return The {@link MissedCallNotifier}.
1028 */
1029 MissedCallNotifier getMissedCallNotifier() {
1030 return mMissedCallNotifier;
1031 }
1032
1033 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001034 * Adds the specified call to the main list of live calls.
1035 *
1036 * @param call The call to add.
1037 */
1038 private void addCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001039 Trace.beginSection("addCall");
Yorke Leeb701f6d2014-08-12 14:04:19 -07001040 Log.v(this, "addCall(%s)", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -07001041 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001042 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001043
Santos Cordondf399862014-08-06 04:39:15 -07001044 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001045 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001046 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001047 if (Log.SYSTRACE_DEBUG) {
1048 Trace.beginSection(listener.getClass().toString() + " addCall");
1049 }
Santos Cordona56f2762014-03-24 15:55:53 -07001050 listener.onCallAdded(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001051 if (Log.SYSTRACE_DEBUG) {
1052 Trace.endSection();
1053 }
Santos Cordona56f2762014-03-24 15:55:53 -07001054 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001055 updateCallsManagerState();
Yorke Leee4a9c412014-11-14 16:59:42 -08001056 Trace.endSection();
Ben Gilada0d9f752014-02-26 11:49:03 -08001057 }
1058
Sailesh Nepal810735e2014-03-18 18:15:46 -07001059 private void removeCall(Call call) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001060 Trace.beginSection("removeCall");
Santos Cordonc499c1c2014-04-14 17:13:14 -07001061 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001062
Santos Cordon672321e2014-08-21 14:38:58 -07001063 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001064 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001065 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001066
1067 boolean shouldNotify = false;
1068 if (mCalls.contains(call)) {
1069 mCalls.remove(call);
1070 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001071 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001072
Sailesh Nepale59bb192014-04-01 18:33:59 -07001073 // Only broadcast changes for calls that are being tracked.
1074 if (shouldNotify) {
1075 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001076 if (Log.SYSTRACE_DEBUG) {
1077 Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
1078 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001079 listener.onCallRemoved(call);
Yorke Leee4a9c412014-11-14 16:59:42 -08001080 if (Log.SYSTRACE_DEBUG) {
1081 Trace.endSection();
1082 }
Sailesh Nepale59bb192014-04-01 18:33:59 -07001083 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001084 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001085 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001086 Trace.endSection();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001087 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001088
Sailesh Nepal810735e2014-03-18 18:15:46 -07001089 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001090 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001091 *
1092 * @param call The call.
1093 * @param newState The new state of the call.
1094 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001095 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001096 if (call == null) {
1097 return;
1098 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001099 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001100 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1101 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001102 if (newState != oldState) {
1103 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1104 // 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 -07001105 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001106 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001107 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001108 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001109 // unexpected transition occurs.
1110 call.setState(newState);
1111
Yorke Leee4a9c412014-11-14 16:59:42 -08001112 Trace.beginSection("onCallStateChanged");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001113 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001114 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001115 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001116 if (Log.SYSTRACE_DEBUG) {
1117 Trace.beginSection(listener.getClass().toString() + " onCallStateChanged");
1118 }
Santos Cordona56f2762014-03-24 15:55:53 -07001119 listener.onCallStateChanged(call, oldState, newState);
Yorke Leee4a9c412014-11-14 16:59:42 -08001120 if (Log.SYSTRACE_DEBUG) {
1121 Trace.endSection();
1122 }
Santos Cordona56f2762014-03-24 15:55:53 -07001123 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001124 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001125 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001126 Trace.endSection();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001127 }
1128 }
1129
1130 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001131 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001132 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001133 private void updateForegroundCall() {
Yorke Leee4a9c412014-11-14 16:59:42 -08001134 Trace.beginSection("updateForegroundCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -07001135 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001136 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001137 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001138 // of its state will be foreground by default and instead the connection service should
1139 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001140 // the call should play audio and listen to microphone if it wants.
1141
Santos Cordonf193ba42014-09-12 06:37:39 -07001142 // Only top-level calls can be in foreground
1143 if (call.getParentCall() != null) {
1144 continue;
1145 }
1146
Santos Cordon40f78c22014-04-07 02:11:42 -07001147 // Active calls have priority.
1148 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001149 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001150 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001151 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001152
1153 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001154 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001155 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001156 }
1157 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001158
Sailesh Nepal810735e2014-03-18 18:15:46 -07001159 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001160 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001161 Call oldForegroundCall = mForegroundCall;
1162 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001163
Santos Cordona56f2762014-03-24 15:55:53 -07001164 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001165 if (Log.SYSTRACE_DEBUG) {
1166 Trace.beginSection(listener.getClass().toString() + " updateForegroundCall");
1167 }
Santos Cordona56f2762014-03-24 15:55:53 -07001168 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001169 if (Log.SYSTRACE_DEBUG) {
1170 Trace.endSection();
1171 }
Santos Cordona56f2762014-03-24 15:55:53 -07001172 }
Santos Cordon681663d2014-01-30 04:32:15 -08001173 }
Yorke Leee4a9c412014-11-14 16:59:42 -08001174 Trace.endSection();
Santos Cordon681663d2014-01-30 04:32:15 -08001175 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001176
Santos Cordon91bd74c2014-11-07 16:10:22 -08001177 private void updateCanAddCall() {
1178 boolean newCanAddCall = canAddCall();
1179 if (newCanAddCall != mCanAddCall) {
1180 mCanAddCall = newCanAddCall;
1181 for (CallsManagerListener listener : mListeners) {
Yorke Leee4a9c412014-11-14 16:59:42 -08001182 if (Log.SYSTRACE_DEBUG) {
1183 Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
1184 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001185 listener.onCanAddCallChanged(mCanAddCall);
Yorke Leee4a9c412014-11-14 16:59:42 -08001186 if (Log.SYSTRACE_DEBUG) {
1187 Trace.endSection();
1188 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001189 }
1190 }
1191 }
1192
1193 private void updateCallsManagerState() {
1194 updateForegroundCall();
1195 updateCanAddCall();
1196 }
1197
Yorke Leeb701f6d2014-08-12 14:04:19 -07001198 private boolean isPotentialMMICode(Uri handle) {
1199 return (handle != null && handle.getSchemeSpecificPart() != null
1200 && handle.getSchemeSpecificPart().contains("#"));
1201 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001202
Tyler Gunn5b452df2014-10-01 15:02:58 -07001203 /**
1204 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1205 * MMI codes which can be dialed when one or more calls are in progress.
1206 * <P>
1207 * Checks for numbers formatted similar to the MMI codes defined in:
1208 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1209 * and
1210 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1211 *
1212 * @param handle The URI to call.
1213 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1214 */
1215 private boolean isPotentialInCallMMICode(Uri handle) {
1216 if (handle != null && handle.getSchemeSpecificPart() != null &&
1217 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1218
1219 String dialedNumber = handle.getSchemeSpecificPart();
1220 return (dialedNumber.equals("0") ||
1221 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1222 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1223 dialedNumber.equals("3") ||
1224 dialedNumber.equals("4") ||
1225 dialedNumber.equals("5"));
1226 }
1227 return false;
1228 }
1229
Santos Cordonf193ba42014-09-12 06:37:39 -07001230 private int getNumCallsWithState(int... states) {
1231 int count = 0;
1232 for (int state : states) {
1233 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001234 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001235 count++;
1236 }
1237 }
1238 }
1239 return count;
1240 }
1241
1242 private boolean hasMaximumLiveCalls() {
1243 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1244 }
1245
1246 private boolean hasMaximumHoldingCalls() {
1247 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1248 }
1249
1250 private boolean hasMaximumRingingCalls() {
1251 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1252 }
1253
1254 private boolean hasMaximumOutgoingCalls() {
1255 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1256 }
1257
1258 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1259 if (hasMaximumLiveCalls()) {
1260 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1261 // have to change.
1262 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1263
Santos Cordond1766502014-09-26 15:45:39 -07001264 if (call == liveCall) {
1265 // If the call is already the foreground call, then we are golden.
1266 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1267 // state since the call was already populated into the list.
1268 return true;
1269 }
1270
Andrew Leee6595182014-09-23 18:43:05 -07001271 if (hasMaximumOutgoingCalls()) {
1272 // Disconnect the current outgoing call if it's not an emergency call. If the user
1273 // tries to make two outgoing calls to different emergency call numbers, we will try
1274 // to connect the first outgoing call.
1275 if (isEmergency) {
1276 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1277 if (!outgoingCall.isEmergencyCall()) {
1278 outgoingCall.disconnect();
1279 return true;
1280 }
1281 }
1282 return false;
1283 }
1284
Santos Cordonf193ba42014-09-12 06:37:39 -07001285 if (hasMaximumHoldingCalls()) {
1286 // There is no more room for any more calls, unless it's an emergency.
1287 if (isEmergency) {
1288 // Kill the current active call, this is easier then trying to disconnect a
1289 // holding call and hold an active call.
1290 liveCall.disconnect();
1291 return true;
1292 }
1293 return false; // No more room!
1294 }
1295
1296 // We have room for at least one more holding call at this point.
1297
1298 // First thing, if we are trying to make a call with the same phone account as the live
1299 // call, then allow it so that the connection service can make its own decision about
1300 // how to handle the new call relative to the current one.
1301 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1302 return true;
1303 } else if (call.getTargetPhoneAccount() == null) {
1304 // Without a phone account, we can't say reliably that the call will fail.
1305 // If the user chooses the same phone account as the live call, then it's
1306 // still possible that the call can be made (like with CDMA calls not supporting
1307 // hold but they still support adding a call by going immediately into conference
1308 // mode). Return true here and we'll run this code again after user chooses an
1309 // account.
1310 return true;
1311 }
1312
1313 // Try to hold the live call before attempting the new outgoing call.
1314 if (liveCall.can(PhoneCapabilities.HOLD)) {
1315 liveCall.hold();
1316 return true;
1317 }
1318
1319 // The live call cannot be held so we're out of luck here. There's no room.
1320 return false;
1321 }
1322 return true;
1323 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001324
1325 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001326 * Creates a new call for an existing connection.
1327 *
1328 * @param callId The id of the new call.
1329 * @param connection The connection information.
1330 * @return The new call.
1331 */
1332 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1333 Call call = new Call(
1334 mContext,
1335 mConnectionServiceRepository,
1336 connection.getHandle() /* handle */,
1337 null /* gatewayInfo */,
1338 null /* connectionManagerPhoneAccount */,
1339 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1340 false /* isIncoming */,
1341 false /* isConference */);
1342
1343 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
1344 call.setConnectTimeMillis(System.currentTimeMillis());
1345 call.setCallCapabilities(connection.getCapabilities());
1346 call.setCallerDisplayName(connection.getCallerDisplayName(),
1347 connection.getCallerDisplayNamePresentation());
1348
1349 call.addListener(this);
1350 addCall(call);
1351
1352 return call;
1353 }
1354
1355 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001356 * Dumps the state of the {@link CallsManager}.
1357 *
1358 * @param pw The {@code IndentingPrintWriter} to write the state to.
1359 */
1360 public void dump(IndentingPrintWriter pw) {
1361 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001362 if (mCalls != null) {
1363 pw.println("mCalls: ");
1364 pw.increaseIndent();
1365 for (Call call : mCalls) {
1366 pw.println(call);
1367 }
1368 pw.decreaseIndent();
1369 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001370 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1371
1372 if (mCallAudioManager != null) {
1373 pw.println("mCallAudioManager:");
1374 pw.increaseIndent();
1375 mCallAudioManager.dump(pw);
1376 pw.decreaseIndent();
1377 }
1378
1379 if (mTtyManager != null) {
1380 pw.println("mTtyManager:");
1381 pw.increaseIndent();
1382 mTtyManager.dump(pw);
1383 pw.decreaseIndent();
1384 }
1385
1386 if (mInCallController != null) {
1387 pw.println("mInCallController:");
1388 pw.increaseIndent();
1389 mInCallController.dump(pw);
1390 pw.decreaseIndent();
1391 }
1392
1393 if (mConnectionServiceRepository != null) {
1394 pw.println("mConnectionServiceRepository:");
1395 pw.increaseIndent();
1396 mConnectionServiceRepository.dump(pw);
1397 pw.decreaseIndent();
1398 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001399 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001400}