blob: b677b850afc02ccd0911bb8c5628bb1726dbd4e1 [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;
Santos Cordonf193ba42014-09-12 06:37:39 -070026import android.provider.CallLog.Calls;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070027import android.telecom.AudioState;
28import android.telecom.CallState;
Andrew Lee701dc002014-09-11 21:29:12 -070029import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070030import android.telecom.GatewayInfo;
31import android.telecom.ParcelableConference;
Tyler Gunn6e2b94e2014-10-30 11:05:22 -070032import android.telecom.ParcelableConnection;
Tyler Gunn5b452df2014-10-01 15:02:58 -070033import android.telecom.PhoneAccount;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070034import android.telecom.PhoneAccountHandle;
Santos Cordonf193ba42014-09-12 06:37:39 -070035import android.telecom.PhoneCapabilities;
Tyler Gunn5b452df2014-10-01 15:02:58 -070036import android.telecom.TelecomManager;
Andrew Lee45506232014-10-22 17:54:33 -070037import android.telecom.VideoProfile;
Sailesh Nepal8aa6a002014-09-12 10:52:49 -070038import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080039
Tyler Gunn91d43cf2014-09-17 12:19:39 -070040import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal810735e2014-03-18 18:15:46 -070041import com.google.common.collect.ImmutableCollection;
42import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080043
Jay Shraunera82c8f72014-08-14 15:49:16 -070044import java.util.Collections;
Ihab Awad5b281322014-09-25 18:25:29 -070045import java.util.HashSet;
Santos Cordon7cdb9092014-07-24 21:33:33 -070046import java.util.List;
Santos Cordonf193ba42014-09-12 06:37:39 -070047import java.util.Objects;
Santos Cordona56f2762014-03-24 15:55:53 -070048import java.util.Set;
Jay Shraunera82c8f72014-08-14 15:49:16 -070049import java.util.concurrent.ConcurrentHashMap;
Ben Gilad9f2bed32013-12-12 17:43:26 -080050
Ben Giladdd8c6082013-12-30 14:44:08 -080051/**
52 * Singleton.
53 *
Jay Shrauneracb91eb2014-08-08 16:04:53 -070054 * NOTE: by design most APIs are package private, use the relevant adapter/s to allow
Ben Giladdd8c6082013-12-30 14:44:08 -080055 * access from other packages specifically refraining from passing the CallsManager instance
Tyler Gunn7cc70b42014-09-12 22:17:27 -070056 * beyond the com.android.server.telecom package boundary.
Ben Giladdd8c6082013-12-30 14:44:08 -080057 */
Santos Cordon64c7e962014-07-02 15:15:27 -070058public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080059
Santos Cordondf399862014-08-06 04:39:15 -070060 // TODO: Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070061 interface CallsManagerListener {
62 void onCallAdded(Call call);
63 void onCallRemoved(Call call);
Ihab Awad6fb37c82014-08-07 19:48:57 -070064 void onCallStateChanged(Call call, int oldState, int newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070066 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070067 ConnectionServiceWrapper oldService,
68 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070069 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070070 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070071 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Ihab Awad6fb37c82014-08-07 19:48:57 -070072 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
Andrew Lee5be64bc2014-09-08 18:35:15 -070073 void onRingbackRequested(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070074 void onIsConferencedChanged(Call call);
Andrew Lee5be64bc2014-09-08 18:35:15 -070075 void onIsVoipAudioModeChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070076 void onVideoStateChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070077 }
78
Tyler Gunn91d43cf2014-09-17 12:19:39 -070079 /**
80 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
81 */
82 private static CallsManager INSTANCE = null;
83
84 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Santos Cordonf193ba42014-09-12 06:37:39 -070086 private static final int MAXIMUM_LIVE_CALLS = 1;
87 private static final int MAXIMUM_HOLD_CALLS = 1;
88 private static final int MAXIMUM_RINGING_CALLS = 1;
89 private static final int MAXIMUM_OUTGOING_CALLS = 1;
90
91 private static final int[] LIVE_CALL_STATES =
92 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
93
94 private static final int[] OUTGOING_CALL_STATES =
95 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
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;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700113 // For this set initial table size to 16 because we add 13 listeners in
114 // the CallsManager constructor.
115 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
116 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700117 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700118 private final WiredHeadsetManager mWiredHeadsetManager;
119 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700120 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700121 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700122 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700123 private final Context mContext;
124 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
125 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700126 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800127 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
128 /* Handler tied to thread in which CallManager was initialized. */
129 private final Handler mHandler = new Handler();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700130
131 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700132 * The call the user is currently interacting with. This is the call that should have audio
133 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800134 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800136
Santos Cordon766d04f2014-05-06 10:28:25 -0700137 /** Singleton accessor. */
138 static CallsManager getInstance() {
139 return INSTANCE;
140 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800141
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800142 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700143 * Sets the static singleton instance.
144 *
145 * @param instance The instance to set.
146 */
147 static void initialize(CallsManager instance) {
148 INSTANCE = instance;
149 }
150
151 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700152 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800153 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700154 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
155 PhoneAccountRegistrar phoneAccountRegistrar) {
156 mContext = context;
157 mPhoneAccountRegistrar = phoneAccountRegistrar;
158 mMissedCallNotifier = missedCallNotifier;
159 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
160 mWiredHeadsetManager = new WiredHeadsetManager(context);
161 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700162 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700163 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
164 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
165 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
166 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700167 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700168 mCallLogManager = new CallLogManager(context);
169 mInCallController = new InCallController(context);
170 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
171 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
172 context);
Santos Cordonae193062014-05-21 21:21:49 -0700173
Santos Cordondeb8c892014-05-30 01:38:03 -0700174 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700175 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700176 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700177 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700178 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700179 mListeners.add(new RingbackPlayer(this, playerFactory));
180 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700181 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700182 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700183 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700184 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700185 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700186 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800187 }
188
Santos Cordon766d04f2014-05-06 10:28:25 -0700189 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700190 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700191 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700192
Tyler Gunncde2e502014-08-12 14:35:58 -0700193 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700194 if (!mCalls.contains(call)) {
195 // Call was not added previously in startOutgoingCall due to it being a potential MMI
196 // code, so add it now.
197 addCall(call);
198 }
199
200 // The call's ConnectionService has been updated.
201 for (CallsManagerListener listener : mListeners) {
202 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700203 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700204
205 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700206 }
207
208 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700209 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700210 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700211
Andrew Leee6288a72014-10-01 13:50:02 -0700212 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700213 }
214
215 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700216 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700217 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700218 setCallState(incomingCall, CallState.RINGING);
219
220 if (hasMaximumRingingCalls()) {
221 incomingCall.reject(false, null);
222 // since the call was not added to the list of calls, we have to call the missed
223 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700224 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700225 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
226 } else {
227 addCall(incomingCall);
228 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700229 }
230
231 @Override
232 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700233 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700234 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800235 }
236
Ihab Awadcb387ac2014-05-28 16:49:38 -0700237 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700238 public void onSuccessfulUnknownCall(Call call, int callState) {
239 setCallState(call, callState);
240 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
241 addCall(call);
242 }
243
244 @Override
245 public void onFailedUnknownCall(Call call) {
246 Log.i(this, "onFailedUnknownCall for call %s", call);
247 setCallState(call, CallState.DISCONNECTED);
248 call.removeListener(this);
249 }
250
251 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700252 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700253 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700254 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700255 }
256 }
257
Evan Charlton352105c2014-06-03 14:10:54 -0700258 @Override
259 public void onPostDialWait(Call call, String remaining) {
260 mInCallController.onPostDialWait(call, remaining);
261 }
262
Santos Cordona1610702014-06-04 20:22:56 -0700263 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700264 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700265 // parent-child relationship affects which call should be foreground, so do an update.
266 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700267 for (CallsManagerListener listener : mListeners) {
268 listener.onIsConferencedChanged(call);
269 }
270 }
271
272 @Override
273 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700274 // parent-child relationship affects which call should be foreground, so do an update.
275 updateForegroundCall();
Santos Cordona1610702014-06-04 20:22:56 -0700276 for (CallsManagerListener listener : mListeners) {
277 listener.onIsConferencedChanged(call);
278 }
279 }
280
Ihab Awadff7493a2014-06-10 13:47:44 -0700281 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700282 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700283 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700284 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700285 }
286 }
287
Andrew Lee4a796602014-07-11 17:23:03 -0700288 @Override
289 public void onVideoStateChanged(Call call) {
290 for (CallsManagerListener listener : mListeners) {
291 listener.onVideoStateChanged(call);
292 }
293 }
294
Santos Cordoncf5b2912014-11-05 21:57:54 -0800295 @Override
296 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
297 mPendingCallsToDisconnect.add(call);
298 mHandler.postDelayed(new Runnable() {
299 @Override
300 public void run() {
301 if (mPendingCallsToDisconnect.remove(call)) {
302 Log.i(this, "Delayed disconnection of call: %s", call);
303 call.disconnect();
304 }
305 }
306 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
307
308 return true;
309 }
310
Sailesh Nepal810735e2014-03-18 18:15:46 -0700311 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700312 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700313 }
314
315 Call getForegroundCall() {
316 return mForegroundCall;
317 }
318
Santos Cordonae193062014-05-21 21:21:49 -0700319 Ringer getRinger() {
320 return mRinger;
321 }
322
Santos Cordonf3671a62014-05-29 21:51:53 -0700323 InCallController getInCallController() {
324 return mInCallController;
325 }
326
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700327 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700328 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700329 if (call.isEmergencyCall()) {
330 return true;
331 }
332 }
333 return false;
334 }
335
Andrew Lee45506232014-10-22 17:54:33 -0700336 boolean hasVideoCall() {
337 for (Call call : mCalls) {
338 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
339 return true;
340 }
341 }
342 return false;
343 }
344
Ihab Awad6fb37c82014-08-07 19:48:57 -0700345 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700346 return mCallAudioManager.getAudioState();
347 }
348
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700349 boolean isTtySupported() {
350 return mTtyManager.isTtySupported();
351 }
352
353 int getCurrentTtyMode() {
354 return mTtyManager.getCurrentTtyMode();
355 }
356
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700357 void addListener(CallsManagerListener listener) {
358 mListeners.add(listener);
359 }
360
361 void removeListener(CallsManagerListener listener) {
362 mListeners.remove(listener);
363 }
364
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800365 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700366 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800367 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700368 * @param phoneAccountHandle The phone account which contains the component name of the
369 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800370 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800371 */
Evan Charlton89176372014-07-19 18:23:09 -0700372 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800373 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700374 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700375 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700376 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700377 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700378 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700379 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700380 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700381 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700382 true /* isIncoming */,
383 false /* isConference */);
384
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700385 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700386 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700387 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700388 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800389 }
390
Yorke Lee9250e5f2014-10-01 13:39:09 -0700391 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
392 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
393 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
394 Call call = new Call(
395 mContext,
396 mConnectionServiceRepository,
397 handle,
398 null /* gatewayInfo */,
399 null /* connectionManagerPhoneAccount */,
400 phoneAccountHandle,
401 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
402 // to the existing connection instead of trying to create a new one.
403 true /* isIncoming */,
404 false /* isConference */);
405 call.setConnectTimeMillis(System.currentTimeMillis());
406 call.setIsUnknown(true);
407 call.setExtras(extras);
408 call.addListener(this);
409 call.startCreateConnection(mPhoneAccountRegistrar);
410 }
411
Santos Cordoncf5b2912014-11-05 21:57:54 -0800412 private Call getNewOutgoingCall(Uri handle) {
413 // First check to see if we can reuse any of the calls that are waiting to disconnect.
414 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
415 for (Call pendingCall : mPendingCallsToDisconnect) {
416 if (Objects.equals(pendingCall.getHandle(), handle)) {
417 mPendingCallsToDisconnect.remove(pendingCall);
418 Log.i(this, "Reusing disconnected call %s", pendingCall);
419 return pendingCall;
420 }
421 }
422
423 // Create a call with original handle. The handle may be changed when the call is attached
424 // to a connection service, but in most cases will remain the same.
425 return new Call(
426 mContext,
427 mConnectionServiceRepository,
428 handle,
429 null /* gatewayInfo */,
430 null /* connectionManagerPhoneAccount */,
431 null /* phoneAccountHandle */,
432 false /* isIncoming */,
433 false /* isConference */);
434 }
435
Nancy Chen0d3076c2014-07-30 14:45:44 -0700436 /**
437 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
438 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700439 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700440 * @param phoneAccountHandle The phone account which contains the component name of the
441 * connection service to use for this call.
442 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700443 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700444 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800445 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700446
447 List<PhoneAccountHandle> accounts =
448 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
449
Nancy Chenbc9ef172014-08-15 17:11:57 -0700450 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
451 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700452 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700453 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700454 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700455 phoneAccountHandle = null;
456 }
457 }
458
459 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700460 // No preset account, check if default exists that supports the URI scheme for the
461 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700462 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700463 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700464 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700465 if (defaultAccountHandle != null) {
466 phoneAccountHandle = defaultAccountHandle;
467 }
468 }
469
Nancy Chen1c5926f2014-09-17 14:44:14 -0700470 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700471
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700472 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
473 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700474 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700475
476 // Do not support any more live calls. Our options are to move a call to hold, disconnect
477 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700478 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700479 // just cancel at this point.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800480 if (mCalls.contains(call)) {
481 // This call can already exist if it is a reused call,
482 // See {@link #getNewOutgoingCall}.
483 call.disconnect();
484 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700485 return null;
486 }
487
Nancy Chen8d92f452014-10-20 21:43:44 -0700488 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
489 !isEmergencyCall;
490
491 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700492 // This is the state where the user is expected to select an account
493 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700494 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700495 } else {
496 call.setState(CallState.CONNECTING);
497 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700498
Nancy Chen1c5926f2014-09-17 14:44:14 -0700499 call.setExtras(extras);
500
Tyler Gunn504eb832014-10-13 14:15:27 -0700501 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700502 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700503 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800504 } else if (!mCalls.contains(call)) {
505 // We check if mCalls already contains the call because we could potentially be reusing
506 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700507 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700508 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700509
510 return call;
511 }
512
Ben Gilada0d9f752014-02-26 11:49:03 -0800513 /**
Yorke Lee33501632014-03-17 19:24:12 -0700514 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800515 *
Yorke Lee33501632014-03-17 19:24:12 -0700516 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700517 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700518 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700519 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700520 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800521 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700522 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
523 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700524 if (call == null) {
525 // don't do anything if the call no longer exists
526 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700527 return;
528 }
529
Nancy Chen201b4372014-09-08 14:18:24 -0700530 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700531
532 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700533 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700534 } else {
535 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
536 Log.pii(uriHandle), Log.pii(handle));
537 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700538
Nancy Chen0d3076c2014-07-30 14:45:44 -0700539 call.setHandle(uriHandle);
540 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700541 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700542 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700543
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700544 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
545 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700546 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700547 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700548 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700549 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700550
Santos Cordonf193ba42014-09-12 06:37:39 -0700551 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700552 // If the account has been set, proceed to place the outgoing call.
553 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700554 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700555 }
Yorke Leef98fb572014-03-05 10:56:55 -0800556 }
557
558 /**
Santos Cordona1610702014-06-04 20:22:56 -0700559 * Attempts to start a conference call for the specified call.
560 *
Santos Cordon12d61822014-07-29 16:02:20 -0700561 * @param call The call to conference.
562 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700563 */
Santos Cordon12d61822014-07-29 16:02:20 -0700564 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700565 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700566 }
567
568 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700569 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
570 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800571 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700572 *
573 * @param call The call to answer.
574 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800575 */
Andrew Lee38931d02014-07-16 10:17:36 -0700576 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700577 if (!mCalls.contains(call)) {
578 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800579 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700580 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700581 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700582 if (mForegroundCall != null && mForegroundCall != call &&
583 (mForegroundCall.isActive() ||
584 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700585 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
586 // This call does not support hold. If it is from a different connection
587 // service, then disconnect it, otherwise allow the connection service to
588 // figure out the right states.
589 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
590 mForegroundCall.disconnect();
591 }
592 } else {
593 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
594 mForegroundCall, call);
595 mForegroundCall.hold();
596 }
Santos Cordondf399862014-08-06 04:39:15 -0700597 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700598 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700599 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700600 }
601
Santos Cordona56f2762014-03-24 15:55:53 -0700602 for (CallsManagerListener listener : mListeners) {
603 listener.onIncomingCallAnswered(call);
604 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800605
Santos Cordon61d0f702014-02-19 02:52:23 -0800606 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700607 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700608 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800609 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800610 }
611
612 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700613 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
614 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800615 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800616 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700617 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700618 if (!mCalls.contains(call)) {
619 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800620 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700621 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700622 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700623 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700624 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800625 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800626 }
627
628 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700629 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700630 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700631 * @param digit The DTMF digit to play.
632 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700633 void playDtmfTone(Call call, char digit) {
634 if (!mCalls.contains(call)) {
635 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700636 } else {
637 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700638 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700639 }
640 }
641
642 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700643 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700644 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700645 void stopDtmfTone(Call call) {
646 if (!mCalls.contains(call)) {
647 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700648 } else {
649 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700650 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700651 }
652 }
653
654 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700655 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700656 */
Evan Charlton352105c2014-06-03 14:10:54 -0700657 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700658 if (!mCalls.contains(call)) {
659 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700660 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700661 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700662 }
663 }
664
665 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700666 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800667 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
668 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800669 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700670 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700671 Log.v(this, "disconnectCall %s", call);
672
Sailesh Nepale59bb192014-04-01 18:33:59 -0700673 if (!mCalls.contains(call)) {
674 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800675 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700676 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800677 call.disconnect();
678 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800679 }
Santos Cordon681663d2014-01-30 04:32:15 -0800680
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700681 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700682 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700683 */
684 void disconnectAllCalls() {
685 Log.v(this, "disconnectAllCalls");
686
687 for (Call call : mCalls) {
688 disconnectCall(call);
689 }
690 }
691
Nancy Chenbc9ef172014-08-15 17:11:57 -0700692
Tyler Gunn61b92102014-08-19 07:42:20 -0700693 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700694 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700695 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
696 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700697 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700698 void holdCall(Call call) {
699 if (!mCalls.contains(call)) {
700 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700701 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700702 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700703 call.hold();
704 }
705 }
706
707 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700708 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700709 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
710 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700711 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700712 void unholdCall(Call call) {
713 if (!mCalls.contains(call)) {
714 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700715 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700716 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700717 for (Call c : mCalls) {
718 if (c != null && c.isAlive() && c != call) {
719 c.hold();
720 }
721 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700722 call.unhold();
723 }
724 }
725
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700726 /** Called by the in-call UI to change the mute state. */
727 void mute(boolean shouldMute) {
728 mCallAudioManager.mute(shouldMute);
729 }
730
731 /**
732 * Called by the in-call UI to change the audio route, for example to change from earpiece to
733 * speaker phone.
734 */
735 void setAudioRoute(int route) {
736 mCallAudioManager.setAudioRoute(route);
737 }
738
Yorke Leed1346872014-07-28 14:38:45 -0700739 /** Called by the in-call UI to turn the proximity sensor on. */
740 void turnOnProximitySensor() {
741 mProximitySensorManager.turnOn();
742 }
743
744 /**
745 * Called by the in-call UI to turn the proximity sensor off.
746 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
747 * the screen will be kept off until the proximity sensor goes negative.
748 */
749 void turnOffProximitySensor(boolean screenOnImmediately) {
750 mProximitySensorManager.turnOff(screenOnImmediately);
751 }
752
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700753 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700754 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700755 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700756 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700757 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
758 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
759 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
760 // respecting a rewritten number or a canceled number. This is unlikely since
761 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
762 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700763 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700764
765 // Note: emergency calls never go through account selection dialog so they never
766 // arrive here.
767 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700768 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700769 } else {
770 call.disconnect();
771 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700772
773 if (setDefault) {
774 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
775 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700776 }
777 }
778
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700779 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700780 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700781 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700782 for (CallsManagerListener listener : mListeners) {
783 listener.onAudioStateChanged(oldAudioState, newAudioState);
784 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700785 }
786
Sailesh Nepale59bb192014-04-01 18:33:59 -0700787 void markCallAsRinging(Call call) {
788 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800789 }
790
Sailesh Nepale59bb192014-04-01 18:33:59 -0700791 void markCallAsDialing(Call call) {
792 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800793 }
794
Sailesh Nepale59bb192014-04-01 18:33:59 -0700795 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700796 if (call.getConnectTimeMillis() == 0) {
797 call.setConnectTimeMillis(System.currentTimeMillis());
798 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700799 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700800
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700801 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700802 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700803 }
Santos Cordon681663d2014-01-30 04:32:15 -0800804 }
805
Sailesh Nepale59bb192014-04-01 18:33:59 -0700806 void markCallAsOnHold(Call call) {
807 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700808 }
809
Santos Cordon049b7b62014-01-30 05:34:26 -0800810 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700811 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
812 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800813 *
Andrew Lee701dc002014-09-11 21:29:12 -0700814 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800815 */
Andrew Lee701dc002014-09-11 21:29:12 -0700816 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
817 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700818 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700819 }
820
Ben Gilada0d9f752014-02-26 11:49:03 -0800821 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700822 * Removes an existing disconnected call, and notifies the in-call app.
823 */
824 void markCallAsRemoved(Call call) {
825 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700826 if (mLocallyDisconnectingCalls.contains(call)) {
827 mLocallyDisconnectingCalls.remove(call);
828 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
829 mForegroundCall.unhold();
830 }
831 }
Ihab Awada02bef52014-08-13 18:18:42 -0700832 }
833
834 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700835 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700836 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700837 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700838 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700839 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700840 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700841 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700842 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700843 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700844 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700845 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700846 }
847 }
848 }
849
Santos Cordondeb8c892014-05-30 01:38:03 -0700850 boolean hasAnyCalls() {
851 return !mCalls.isEmpty();
852 }
853
Santos Cordonc7e85d42014-05-22 02:51:48 -0700854 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700855 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700856 }
857
858 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700859 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700860 }
861
Santos Cordondeb8c892014-05-30 01:38:03 -0700862 boolean onMediaButton(int type) {
863 if (hasAnyCalls()) {
864 if (HeadsetMediaButton.SHORT_PRESS == type) {
865 Call ringingCall = getFirstCallWithState(CallState.RINGING);
866 if (ringingCall == null) {
867 mCallAudioManager.toggleMute();
868 return true;
869 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700870 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700871 return true;
872 }
873 } else if (HeadsetMediaButton.LONG_PRESS == type) {
874 Log.d(this, "handleHeadsetHook: longpress -> hangup");
875 Call callToHangup = getFirstCallWithState(
876 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
877 if (callToHangup != null) {
878 callToHangup.disconnect();
879 return true;
880 }
881 }
882 }
883 return false;
884 }
885
886 /**
Andrew Lee235a52f2014-10-21 14:27:41 -0700887 * Checks to see if the specified call is the only top level call. If it is not, we should
888 * remove the ADD_CALL capability. We allow you to add a second call but not a third or beyond.
Santos Cordon10838c22014-06-11 17:36:04 -0700889 *
Andrew Lee235a52f2014-10-21 14:27:41 -0700890 * @param call The call to check to see if it is the only top level call.
891 * @return Whether the call is the only top level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700892 */
Andrew Lee235a52f2014-10-21 14:27:41 -0700893 protected boolean isOnlyTopLevelCall(Call call) {
Santos Cordon10838c22014-06-11 17:36:04 -0700894 if (call.getParentCall() != null) {
895 // Never true for child calls.
896 return false;
897 }
898
899 // Loop through all the other calls and there exists a top level (has no parent) call
900 // that is not the specified call, return false.
901 for (Call otherCall : mCalls) {
902 if (call != otherCall && otherCall.getParentCall() == null) {
903 return false;
904 }
905 }
906 return true;
907 }
908
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700909 Call getRingingCall() {
910 return getFirstCallWithState(CallState.RINGING);
911 }
912
913 Call getActiveCall() {
914 return getFirstCallWithState(CallState.ACTIVE);
915 }
916
Nancy Chen05a9e402014-09-26 14:14:32 -0700917 Call getDialingCall() {
918 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700919 }
920
921 Call getHeldCall() {
922 return getFirstCallWithState(CallState.ON_HOLD);
923 }
924
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700925 int getNumHeldCalls() {
926 int count = 0;
927 for (Call call : mCalls) {
928 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
929 count++;
930 }
931 }
932 return count;
933 }
934
Santos Cordonf193ba42014-09-12 06:37:39 -0700935 Call getFirstCallWithState(int... states) {
936 return getFirstCallWithState(null, states);
937 }
938
Santos Cordon10838c22014-06-11 17:36:04 -0700939 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700940 * Returns the first call that it finds with the given states. The states are treated as having
941 * priority order so that any call with the first state will be returned before any call with
942 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700943 *
944 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700945 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700946 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700947 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700948 // check the foreground first
949 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
950 return mForegroundCall;
951 }
952
Santos Cordondeb8c892014-05-30 01:38:03 -0700953 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700954 if (Objects.equals(callToSkip, call)) {
955 continue;
956 }
957
958 // Only operate on top-level calls
959 if (call.getParentCall() != null) {
960 continue;
961 }
962
Santos Cordondeb8c892014-05-30 01:38:03 -0700963 if (currentState == call.getState()) {
964 return call;
965 }
966 }
967 }
968 return null;
969 }
970
Santos Cordon0fbe6322014-08-14 04:04:25 -0700971 Call createConferenceCall(
972 PhoneAccountHandle phoneAccount,
973 ParcelableConference parcelableConference) {
974 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700975 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700976 mConnectionServiceRepository,
977 null /* handle */,
978 null /* gatewayInfo */,
979 null /* connectionManagerPhoneAccount */,
980 phoneAccount,
981 false /* isIncoming */,
982 true /* isConference */);
983
984 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700985 if (call.getState() == CallState.ACTIVE) {
986 call.setConnectTimeMillis(System.currentTimeMillis());
987 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700988 call.setCallCapabilities(parcelableConference.getCapabilities());
989
990 // TODO: Move this to be a part of addCall()
991 call.addListener(this);
992 addCall(call);
993 return call;
994 }
995
Yorke Leef86db2e2014-09-12 17:56:48 -0700996 /**
997 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
998 */
999 int getCallState() {
1000 return mPhoneStateBroadcaster.getCallState();
1001 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001002
Santos Cordon4b2c1192014-03-19 18:15:38 -07001003 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001004 * Retrieves the {@link PhoneAccountRegistrar}.
1005 *
1006 * @return The {@link PhoneAccountRegistrar}.
1007 */
1008 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1009 return mPhoneAccountRegistrar;
1010 }
1011
1012 /**
1013 * Retrieves the {@link MissedCallNotifier}
1014 * @return The {@link MissedCallNotifier}.
1015 */
1016 MissedCallNotifier getMissedCallNotifier() {
1017 return mMissedCallNotifier;
1018 }
1019
1020 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001021 * Adds the specified call to the main list of live calls.
1022 *
1023 * @param call The call to add.
1024 */
1025 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -07001026 Log.v(this, "addCall(%s)", call);
1027
1028 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001029 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001030
Santos Cordondf399862014-08-06 04:39:15 -07001031 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001032 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001033 for (CallsManagerListener listener : mListeners) {
1034 listener.onCallAdded(call);
1035 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001036 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -08001037 }
1038
Sailesh Nepal810735e2014-03-18 18:15:46 -07001039 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -07001040 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001041
Santos Cordon672321e2014-08-21 14:38:58 -07001042 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001043 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001044 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001045
1046 boolean shouldNotify = false;
1047 if (mCalls.contains(call)) {
1048 mCalls.remove(call);
1049 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001050 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001051
Sailesh Nepale59bb192014-04-01 18:33:59 -07001052 // Only broadcast changes for calls that are being tracked.
1053 if (shouldNotify) {
1054 for (CallsManagerListener listener : mListeners) {
1055 listener.onCallRemoved(call);
1056 }
1057 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -07001058 }
Yorke Lee604a90f2014-10-20 12:13:32 -07001059
1060 // Now that a call has been removed, other calls may gain new call capabilities (for
1061 // example, if only one call is left, it is now add-call capable again). Trigger the
1062 // recalculation of the call's current capabilities by forcing an update. (See
1063 // InCallController.toParcelableCall()).
1064 for (Call otherCall : mCalls) {
Yorke Lee7ae9eed2014-10-21 13:26:51 -07001065 otherCall.setCallCapabilities(otherCall.getCallCapabilities(), true /* forceUpdate */);
Yorke Lee604a90f2014-10-20 12:13:32 -07001066 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001067 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001068
Sailesh Nepal810735e2014-03-18 18:15:46 -07001069 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001070 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001071 *
1072 * @param call The call.
1073 * @param newState The new state of the call.
1074 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001075 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001076 if (call == null) {
1077 return;
1078 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001079 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001080 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1081 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001082 if (newState != oldState) {
1083 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1084 // 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 -07001085 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001086 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001087 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001088 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001089 // unexpected transition occurs.
1090 call.setState(newState);
1091
1092 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001093 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001094 for (CallsManagerListener listener : mListeners) {
1095 listener.onCallStateChanged(call, oldState, newState);
1096 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001097 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001098 }
1099 }
1100 }
1101
1102 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001103 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001104 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001105 private void updateForegroundCall() {
1106 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001107 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001108 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001109 // of its state will be foreground by default and instead the connection service should
1110 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001111 // the call should play audio and listen to microphone if it wants.
1112
Santos Cordonf193ba42014-09-12 06:37:39 -07001113 // Only top-level calls can be in foreground
1114 if (call.getParentCall() != null) {
1115 continue;
1116 }
1117
Santos Cordon40f78c22014-04-07 02:11:42 -07001118 // Active calls have priority.
1119 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001120 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001121 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001122 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001123
1124 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001125 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001126 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001127 }
1128 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001129
Sailesh Nepal810735e2014-03-18 18:15:46 -07001130 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001131 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001132 Call oldForegroundCall = mForegroundCall;
1133 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001134
Santos Cordona56f2762014-03-24 15:55:53 -07001135 for (CallsManagerListener listener : mListeners) {
1136 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1137 }
Santos Cordon681663d2014-01-30 04:32:15 -08001138 }
1139 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001140
1141 private boolean isPotentialMMICode(Uri handle) {
1142 return (handle != null && handle.getSchemeSpecificPart() != null
1143 && handle.getSchemeSpecificPart().contains("#"));
1144 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001145
Tyler Gunn5b452df2014-10-01 15:02:58 -07001146 /**
1147 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1148 * MMI codes which can be dialed when one or more calls are in progress.
1149 * <P>
1150 * Checks for numbers formatted similar to the MMI codes defined in:
1151 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1152 * and
1153 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1154 *
1155 * @param handle The URI to call.
1156 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1157 */
1158 private boolean isPotentialInCallMMICode(Uri handle) {
1159 if (handle != null && handle.getSchemeSpecificPart() != null &&
1160 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1161
1162 String dialedNumber = handle.getSchemeSpecificPart();
1163 return (dialedNumber.equals("0") ||
1164 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1165 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1166 dialedNumber.equals("3") ||
1167 dialedNumber.equals("4") ||
1168 dialedNumber.equals("5"));
1169 }
1170 return false;
1171 }
1172
Santos Cordonf193ba42014-09-12 06:37:39 -07001173 private int getNumCallsWithState(int... states) {
1174 int count = 0;
1175 for (int state : states) {
1176 for (Call call : mCalls) {
1177 if (call.getState() == state) {
1178 count++;
1179 }
1180 }
1181 }
1182 return count;
1183 }
1184
1185 private boolean hasMaximumLiveCalls() {
1186 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1187 }
1188
1189 private boolean hasMaximumHoldingCalls() {
1190 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1191 }
1192
1193 private boolean hasMaximumRingingCalls() {
1194 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1195 }
1196
1197 private boolean hasMaximumOutgoingCalls() {
1198 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1199 }
1200
1201 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1202 if (hasMaximumLiveCalls()) {
1203 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1204 // have to change.
1205 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1206
Santos Cordond1766502014-09-26 15:45:39 -07001207 if (call == liveCall) {
1208 // If the call is already the foreground call, then we are golden.
1209 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1210 // state since the call was already populated into the list.
1211 return true;
1212 }
1213
Andrew Leee6595182014-09-23 18:43:05 -07001214 if (hasMaximumOutgoingCalls()) {
1215 // Disconnect the current outgoing call if it's not an emergency call. If the user
1216 // tries to make two outgoing calls to different emergency call numbers, we will try
1217 // to connect the first outgoing call.
1218 if (isEmergency) {
1219 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1220 if (!outgoingCall.isEmergencyCall()) {
1221 outgoingCall.disconnect();
1222 return true;
1223 }
1224 }
1225 return false;
1226 }
1227
Santos Cordonf193ba42014-09-12 06:37:39 -07001228 if (hasMaximumHoldingCalls()) {
1229 // There is no more room for any more calls, unless it's an emergency.
1230 if (isEmergency) {
1231 // Kill the current active call, this is easier then trying to disconnect a
1232 // holding call and hold an active call.
1233 liveCall.disconnect();
1234 return true;
1235 }
1236 return false; // No more room!
1237 }
1238
1239 // We have room for at least one more holding call at this point.
1240
1241 // First thing, if we are trying to make a call with the same phone account as the live
1242 // call, then allow it so that the connection service can make its own decision about
1243 // how to handle the new call relative to the current one.
1244 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1245 return true;
1246 } else if (call.getTargetPhoneAccount() == null) {
1247 // Without a phone account, we can't say reliably that the call will fail.
1248 // If the user chooses the same phone account as the live call, then it's
1249 // still possible that the call can be made (like with CDMA calls not supporting
1250 // hold but they still support adding a call by going immediately into conference
1251 // mode). Return true here and we'll run this code again after user chooses an
1252 // account.
1253 return true;
1254 }
1255
1256 // Try to hold the live call before attempting the new outgoing call.
1257 if (liveCall.can(PhoneCapabilities.HOLD)) {
1258 liveCall.hold();
1259 return true;
1260 }
1261
1262 // The live call cannot be held so we're out of luck here. There's no room.
1263 return false;
1264 }
1265 return true;
1266 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001267
1268 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001269 * Creates a new call for an existing connection.
1270 *
1271 * @param callId The id of the new call.
1272 * @param connection The connection information.
1273 * @return The new call.
1274 */
1275 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1276 Call call = new Call(
1277 mContext,
1278 mConnectionServiceRepository,
1279 connection.getHandle() /* handle */,
1280 null /* gatewayInfo */,
1281 null /* connectionManagerPhoneAccount */,
1282 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1283 false /* isIncoming */,
1284 false /* isConference */);
1285
1286 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
1287 call.setConnectTimeMillis(System.currentTimeMillis());
1288 call.setCallCapabilities(connection.getCapabilities());
1289 call.setCallerDisplayName(connection.getCallerDisplayName(),
1290 connection.getCallerDisplayNamePresentation());
1291
1292 call.addListener(this);
1293 addCall(call);
1294
1295 return call;
1296 }
1297
1298 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001299 * Dumps the state of the {@link CallsManager}.
1300 *
1301 * @param pw The {@code IndentingPrintWriter} to write the state to.
1302 */
1303 public void dump(IndentingPrintWriter pw) {
1304 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001305 if (mCalls != null) {
1306 pw.println("mCalls: ");
1307 pw.increaseIndent();
1308 for (Call call : mCalls) {
1309 pw.println(call);
1310 }
1311 pw.decreaseIndent();
1312 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001313 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1314
1315 if (mCallAudioManager != null) {
1316 pw.println("mCallAudioManager:");
1317 pw.increaseIndent();
1318 mCallAudioManager.dump(pw);
1319 pw.decreaseIndent();
1320 }
1321
1322 if (mTtyManager != null) {
1323 pw.println("mTtyManager:");
1324 pw.increaseIndent();
1325 mTtyManager.dump(pw);
1326 pw.decreaseIndent();
1327 }
1328
1329 if (mInCallController != null) {
1330 pw.println("mInCallController:");
1331 pw.increaseIndent();
1332 mInCallController.dump(pw);
1333 pw.decreaseIndent();
1334 }
1335
1336 if (mConnectionServiceRepository != null) {
1337 pw.println("mConnectionServiceRepository:");
1338 pw.increaseIndent();
1339 mConnectionServiceRepository.dump(pw);
1340 pw.decreaseIndent();
1341 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001342 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001343}