blob: 57b9e8e121c228ccd2476f1f329a5c8245116e2c [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);
Santos Cordon91bd74c2014-11-07 16:10:22 -080077 void onCanAddCallChanged(boolean canAddCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -070078 }
79
Tyler Gunn91d43cf2014-09-17 12:19:39 -070080 /**
81 * Singleton instance of the {@link CallsManager}, initialized from {@link TelecomService}.
82 */
83 private static CallsManager INSTANCE = null;
84
85 private static final String TAG = "CallsManager";
Ben Gilad9f2bed32013-12-12 17:43:26 -080086
Santos Cordonf193ba42014-09-12 06:37:39 -070087 private static final int MAXIMUM_LIVE_CALLS = 1;
88 private static final int MAXIMUM_HOLD_CALLS = 1;
89 private static final int MAXIMUM_RINGING_CALLS = 1;
90 private static final int MAXIMUM_OUTGOING_CALLS = 1;
Santos Cordon91bd74c2014-11-07 16:10:22 -080091 private static final int MAXIMUM_TOP_LEVEL_CALLS = 2;
Santos Cordonf193ba42014-09-12 06:37:39 -070092
93 private static final int[] OUTGOING_CALL_STATES =
94 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING};
95
Santos Cordon91bd74c2014-11-07 16:10:22 -080096 private static final int[] LIVE_CALL_STATES =
97 {CallState.CONNECTING, CallState.PRE_DIAL_WAIT, CallState.DIALING, CallState.ACTIVE};
98
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700100 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
101 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800102 *
Jay Shraunera82c8f72014-08-14 15:49:16 -0700103 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
104 * load factor before resizing, 1 means we only expect a single thread to
105 * access the map so make only a single shard
Santos Cordon681663d2014-01-30 04:32:15 -0800106 */
Jay Shraunera82c8f72014-08-14 15:49:16 -0700107 private final Set<Call> mCalls = Collections.newSetFromMap(
108 new ConcurrentHashMap<Call, Boolean>(8, 0.9f, 1));
Santos Cordon681663d2014-01-30 04:32:15 -0800109
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700110 private final ConnectionServiceRepository mConnectionServiceRepository;
111 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer;
112 private final InCallController mInCallController;
Santos Cordonf3671a62014-05-29 21:51:53 -0700113 private final CallAudioManager mCallAudioManager;
114 private final Ringer mRinger;
Jay Shraunera82c8f72014-08-14 15:49:16 -0700115 // For this set initial table size to 16 because we add 13 listeners in
116 // the CallsManager constructor.
117 private final Set<CallsManagerListener> mListeners = Collections.newSetFromMap(
118 new ConcurrentHashMap<CallsManagerListener, Boolean>(16, 0.9f, 1));
Santos Cordondeb8c892014-05-30 01:38:03 -0700119 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700120 private final WiredHeadsetManager mWiredHeadsetManager;
121 private final TtyManager mTtyManager;
Yorke Leed1346872014-07-28 14:38:45 -0700122 private final ProximitySensorManager mProximitySensorManager;
Yorke Leef86db2e2014-09-12 17:56:48 -0700123 private final PhoneStateBroadcaster mPhoneStateBroadcaster;
Santos Cordonf193ba42014-09-12 06:37:39 -0700124 private final CallLogManager mCallLogManager;
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700125 private final Context mContext;
126 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
127 private final MissedCallNotifier mMissedCallNotifier;
Ihab Awad5b281322014-09-25 18:25:29 -0700128 private final Set<Call> mLocallyDisconnectingCalls = new HashSet<>();
Santos Cordoncf5b2912014-11-05 21:57:54 -0800129 private final Set<Call> mPendingCallsToDisconnect = new HashSet<>();
130 /* Handler tied to thread in which CallManager was initialized. */
131 private final Handler mHandler = new Handler();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700132
Santos Cordon91bd74c2014-11-07 16:10:22 -0800133 private boolean mCanAddCall = true;
134
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700135 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700136 * The call the user is currently interacting with. This is the call that should have audio
137 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800138 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700139 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800140
Santos Cordon766d04f2014-05-06 10:28:25 -0700141 /** Singleton accessor. */
142 static CallsManager getInstance() {
143 return INSTANCE;
144 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800145
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800146 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700147 * Sets the static singleton instance.
148 *
149 * @param instance The instance to set.
150 */
151 static void initialize(CallsManager instance) {
152 INSTANCE = instance;
153 }
154
155 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700156 * Initializes the required Telecom components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800157 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700158 CallsManager(Context context, MissedCallNotifier missedCallNotifier,
159 PhoneAccountRegistrar phoneAccountRegistrar) {
160 mContext = context;
161 mPhoneAccountRegistrar = phoneAccountRegistrar;
162 mMissedCallNotifier = missedCallNotifier;
163 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(context, this);
164 mWiredHeadsetManager = new WiredHeadsetManager(context);
165 mCallAudioManager = new CallAudioManager(context, statusBarNotifier, mWiredHeadsetManager);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700166 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700167 mRinger = new Ringer(mCallAudioManager, this, playerFactory, context);
168 mHeadsetMediaButton = new HeadsetMediaButton(context, this);
169 mTtyManager = new TtyManager(context, mWiredHeadsetManager);
170 mProximitySensorManager = new ProximitySensorManager(context);
Yorke Leef86db2e2014-09-12 17:56:48 -0700171 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700172 mCallLogManager = new CallLogManager(context);
173 mInCallController = new InCallController(context);
174 mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(context);
175 mConnectionServiceRepository = new ConnectionServiceRepository(mPhoneAccountRegistrar,
176 context);
Santos Cordonae193062014-05-21 21:21:49 -0700177
Santos Cordondeb8c892014-05-30 01:38:03 -0700178 mListeners.add(statusBarNotifier);
Santos Cordonf193ba42014-09-12 06:37:39 -0700179 mListeners.add(mCallLogManager);
Yorke Leef86db2e2014-09-12 17:56:48 -0700180 mListeners.add(mPhoneStateBroadcaster);
Santos Cordonf3671a62014-05-29 21:51:53 -0700181 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700182 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700183 mListeners.add(new RingbackPlayer(this, playerFactory));
184 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700185 mListeners.add(mCallAudioManager);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700186 mListeners.add(missedCallNotifier);
Santos Cordon92a2d812014-04-30 15:19:01 -0700187 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700188 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700189 mListeners.add(RespondViaSmsManager.getInstance());
Yorke Leed1346872014-07-28 14:38:45 -0700190 mListeners.add(mProximitySensorManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800191 }
192
Santos Cordon766d04f2014-05-06 10:28:25 -0700193 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700194 public void onSuccessfulOutgoingCall(Call call, int callState) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700195 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700196
Tyler Gunncde2e502014-08-12 14:35:58 -0700197 setCallState(call, callState);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700198 if (!mCalls.contains(call)) {
199 // Call was not added previously in startOutgoingCall due to it being a potential MMI
200 // code, so add it now.
201 addCall(call);
202 }
203
204 // The call's ConnectionService has been updated.
205 for (CallsManagerListener listener : mListeners) {
206 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700207 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700208
209 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700210 }
211
212 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700213 public void onFailedOutgoingCall(Call call, DisconnectCause disconnectCause) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700214 Log.v(this, "onFailedOutgoingCall, call: %s", call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700215
Andrew Leee6288a72014-10-01 13:50:02 -0700216 markCallAsRemoved(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700217 }
218
219 @Override
Santos Cordonf193ba42014-09-12 06:37:39 -0700220 public void onSuccessfulIncomingCall(Call incomingCall) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700221 Log.d(this, "onSuccessfulIncomingCall");
Santos Cordonf193ba42014-09-12 06:37:39 -0700222 setCallState(incomingCall, CallState.RINGING);
223
224 if (hasMaximumRingingCalls()) {
225 incomingCall.reject(false, null);
226 // since the call was not added to the list of calls, we have to call the missed
227 // call notifier and the call logger manually.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700228 mMissedCallNotifier.showMissedCallNotification(incomingCall);
Santos Cordonf193ba42014-09-12 06:37:39 -0700229 mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE);
230 } else {
231 addCall(incomingCall);
232 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700233 }
234
235 @Override
236 public void onFailedIncomingCall(Call call) {
Tyler Gunncde2e502014-08-12 14:35:58 -0700237 setCallState(call, CallState.DISCONNECTED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700238 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800239 }
240
Ihab Awadcb387ac2014-05-28 16:49:38 -0700241 @Override
Yorke Lee9250e5f2014-10-01 13:39:09 -0700242 public void onSuccessfulUnknownCall(Call call, int callState) {
243 setCallState(call, callState);
244 Log.i(this, "onSuccessfulUnknownCall for call %s", call);
245 addCall(call);
246 }
247
248 @Override
249 public void onFailedUnknownCall(Call call) {
250 Log.i(this, "onFailedUnknownCall for call %s", call);
251 setCallState(call, CallState.DISCONNECTED);
252 call.removeListener(this);
253 }
254
255 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700256 public void onRingbackRequested(Call call, boolean ringback) {
Ihab Awadcb387ac2014-05-28 16:49:38 -0700257 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700258 listener.onRingbackRequested(call, ringback);
Ihab Awadcb387ac2014-05-28 16:49:38 -0700259 }
260 }
261
Evan Charlton352105c2014-06-03 14:10:54 -0700262 @Override
263 public void onPostDialWait(Call call, String remaining) {
264 mInCallController.onPostDialWait(call, remaining);
265 }
266
Santos Cordona1610702014-06-04 20:22:56 -0700267 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700268 public void onParentChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700269 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800270 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700271 for (CallsManagerListener listener : mListeners) {
272 listener.onIsConferencedChanged(call);
273 }
274 }
275
276 @Override
277 public void onChildrenChanged(Call call) {
Santos Cordon66fe8822014-10-10 16:10:58 -0700278 // parent-child relationship affects which call should be foreground, so do an update.
Santos Cordon91bd74c2014-11-07 16:10:22 -0800279 updateCallsManagerState();
Santos Cordona1610702014-06-04 20:22:56 -0700280 for (CallsManagerListener listener : mListeners) {
281 listener.onIsConferencedChanged(call);
282 }
283 }
284
Ihab Awadff7493a2014-06-10 13:47:44 -0700285 @Override
Andrew Lee5be64bc2014-09-08 18:35:15 -0700286 public void onIsVoipAudioModeChanged(Call call) {
Sailesh Nepal7e669572014-07-08 21:29:12 -0700287 for (CallsManagerListener listener : mListeners) {
Andrew Lee5be64bc2014-09-08 18:35:15 -0700288 listener.onIsVoipAudioModeChanged(call);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700289 }
290 }
291
Andrew Lee4a796602014-07-11 17:23:03 -0700292 @Override
293 public void onVideoStateChanged(Call call) {
294 for (CallsManagerListener listener : mListeners) {
295 listener.onVideoStateChanged(call);
296 }
297 }
298
Santos Cordoncf5b2912014-11-05 21:57:54 -0800299 @Override
300 public boolean onCanceledViaNewOutgoingCallBroadcast(final Call call) {
301 mPendingCallsToDisconnect.add(call);
302 mHandler.postDelayed(new Runnable() {
303 @Override
304 public void run() {
305 if (mPendingCallsToDisconnect.remove(call)) {
306 Log.i(this, "Delayed disconnection of call: %s", call);
307 call.disconnect();
308 }
309 }
310 }, Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()));
311
312 return true;
313 }
314
Sailesh Nepal810735e2014-03-18 18:15:46 -0700315 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700316 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700317 }
318
319 Call getForegroundCall() {
320 return mForegroundCall;
321 }
322
Santos Cordonae193062014-05-21 21:21:49 -0700323 Ringer getRinger() {
324 return mRinger;
325 }
326
Santos Cordonf3671a62014-05-29 21:51:53 -0700327 InCallController getInCallController() {
328 return mInCallController;
329 }
330
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700331 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700332 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700333 if (call.isEmergencyCall()) {
334 return true;
335 }
336 }
337 return false;
338 }
339
Andrew Lee45506232014-10-22 17:54:33 -0700340 boolean hasVideoCall() {
341 for (Call call : mCalls) {
342 if (call.getVideoState() != VideoProfile.VideoState.AUDIO_ONLY) {
343 return true;
344 }
345 }
346 return false;
347 }
348
Ihab Awad6fb37c82014-08-07 19:48:57 -0700349 AudioState getAudioState() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700350 return mCallAudioManager.getAudioState();
351 }
352
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700353 boolean isTtySupported() {
354 return mTtyManager.isTtySupported();
355 }
356
357 int getCurrentTtyMode() {
358 return mTtyManager.getCurrentTtyMode();
359 }
360
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700361 void addListener(CallsManagerListener listener) {
362 mListeners.add(listener);
363 }
364
365 void removeListener(CallsManagerListener listener) {
366 mListeners.remove(listener);
367 }
368
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800369 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700370 * Starts the process to attach the call to a connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800371 *
Nancy Chenbc9ef172014-08-15 17:11:57 -0700372 * @param phoneAccountHandle The phone account which contains the component name of the
373 * connection service to use for this call.
Evan Charltona05805b2014-03-05 08:21:46 -0800374 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800375 */
Evan Charlton89176372014-07-19 18:23:09 -0700376 void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800377 Log.d(this, "processIncomingCallIntent");
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700378 Uri handle = extras.getParcelable(TelephonyManager.EXTRA_INCOMING_NUMBER);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700379 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700380 mContext,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700381 mConnectionServiceRepository,
Sailesh Nepal8aa6a002014-09-12 10:52:49 -0700382 handle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700383 null /* gatewayInfo */,
Ihab Awadb78b2762014-07-25 15:16:23 -0700384 null /* connectionManagerPhoneAccount */,
Evan Charlton89176372014-07-19 18:23:09 -0700385 phoneAccountHandle,
Sailesh Nepal664837f2014-07-14 16:31:51 -0700386 true /* isIncoming */,
387 false /* isConference */);
388
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700389 call.setExtras(extras);
Santos Cordondf399862014-08-06 04:39:15 -0700390 // TODO: Move this to be a part of addCall()
Santos Cordon766d04f2014-05-06 10:28:25 -0700391 call.addListener(this);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700392 call.startCreateConnection(mPhoneAccountRegistrar);
Ben Gilada0d9f752014-02-26 11:49:03 -0800393 }
394
Yorke Lee9250e5f2014-10-01 13:39:09 -0700395 void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
396 Uri handle = extras.getParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE);
397 Log.i(this, "addNewUnknownCall with handle: %s", Log.pii(handle));
398 Call call = new Call(
399 mContext,
400 mConnectionServiceRepository,
401 handle,
402 null /* gatewayInfo */,
403 null /* connectionManagerPhoneAccount */,
404 phoneAccountHandle,
405 // Use onCreateIncomingConnection in TelephonyConnectionService, so that we attach
406 // to the existing connection instead of trying to create a new one.
407 true /* isIncoming */,
408 false /* isConference */);
409 call.setConnectTimeMillis(System.currentTimeMillis());
410 call.setIsUnknown(true);
411 call.setExtras(extras);
412 call.addListener(this);
413 call.startCreateConnection(mPhoneAccountRegistrar);
414 }
415
Santos Cordoncf5b2912014-11-05 21:57:54 -0800416 private Call getNewOutgoingCall(Uri handle) {
417 // First check to see if we can reuse any of the calls that are waiting to disconnect.
418 // See {@link Call#abort} and {@link #onCanceledViaNewOutgoingCall} for more information.
419 for (Call pendingCall : mPendingCallsToDisconnect) {
420 if (Objects.equals(pendingCall.getHandle(), handle)) {
421 mPendingCallsToDisconnect.remove(pendingCall);
422 Log.i(this, "Reusing disconnected call %s", pendingCall);
423 return pendingCall;
424 }
425 }
426
427 // Create a call with original handle. The handle may be changed when the call is attached
428 // to a connection service, but in most cases will remain the same.
429 return new Call(
430 mContext,
431 mConnectionServiceRepository,
432 handle,
433 null /* gatewayInfo */,
434 null /* connectionManagerPhoneAccount */,
435 null /* phoneAccountHandle */,
436 false /* isIncoming */,
437 false /* isConference */);
438 }
439
Nancy Chen0d3076c2014-07-30 14:45:44 -0700440 /**
441 * Kicks off the first steps to creating an outgoing call so that InCallUI can launch.
442 *
Nancy Chena9d91da2014-08-12 14:31:06 -0700443 * @param handle Handle to connect the call with.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700444 * @param phoneAccountHandle The phone account which contains the component name of the
445 * connection service to use for this call.
446 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Nancy Chen0d3076c2014-07-30 14:45:44 -0700447 */
Nancy Chena9d91da2014-08-12 14:31:06 -0700448 Call startOutgoingCall(Uri handle, PhoneAccountHandle phoneAccountHandle, Bundle extras) {
Santos Cordoncf5b2912014-11-05 21:57:54 -0800449 Call call = getNewOutgoingCall(handle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700450
451 List<PhoneAccountHandle> accounts =
452 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
453
Nancy Chenbc9ef172014-08-15 17:11:57 -0700454 // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
455 // as if a phoneAccount was not specified (does the default behavior instead).
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700456 // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700457 if (phoneAccountHandle != null) {
Nancy Chen309198e2014-09-15 18:02:49 -0700458 if (!accounts.contains(phoneAccountHandle)) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700459 phoneAccountHandle = null;
460 }
461 }
462
463 if (phoneAccountHandle == null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700464 // No preset account, check if default exists that supports the URI scheme for the
465 // handle.
Nancy Chenbc9ef172014-08-15 17:11:57 -0700466 PhoneAccountHandle defaultAccountHandle =
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700467 mPhoneAccountRegistrar.getDefaultOutgoingPhoneAccount(
Tyler Gunn84253572014-09-02 14:50:05 -0700468 handle.getScheme());
Nancy Chenbc9ef172014-08-15 17:11:57 -0700469 if (defaultAccountHandle != null) {
470 phoneAccountHandle = defaultAccountHandle;
471 }
472 }
473
Nancy Chen1c5926f2014-09-17 14:44:14 -0700474 call.setTargetPhoneAccount(phoneAccountHandle);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700475
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700476 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
477 call.getHandle());
Tyler Gunn5b452df2014-10-01 15:02:58 -0700478 boolean isPotentialInCallMMICode = isPotentialInCallMMICode(handle);
Santos Cordonf193ba42014-09-12 06:37:39 -0700479
480 // Do not support any more live calls. Our options are to move a call to hold, disconnect
481 // a call, or cancel this call altogether.
Tyler Gunn5b452df2014-10-01 15:02:58 -0700482 if (!isPotentialInCallMMICode && !makeRoomForOutgoingCall(call, isEmergencyCall)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700483 // just cancel at this point.
Santos Cordoncf5b2912014-11-05 21:57:54 -0800484 if (mCalls.contains(call)) {
485 // This call can already exist if it is a reused call,
486 // See {@link #getNewOutgoingCall}.
487 call.disconnect();
488 }
Santos Cordonf193ba42014-09-12 06:37:39 -0700489 return null;
490 }
491
Nancy Chen8d92f452014-10-20 21:43:44 -0700492 boolean needsAccountSelection = phoneAccountHandle == null && accounts.size() > 1 &&
493 !isEmergencyCall;
494
495 if (needsAccountSelection) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700496 // This is the state where the user is expected to select an account
497 call.setState(CallState.PRE_DIAL_WAIT);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700498 extras.putParcelableList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS, accounts);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700499 } else {
500 call.setState(CallState.CONNECTING);
501 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700502
Nancy Chen1c5926f2014-09-17 14:44:14 -0700503 call.setExtras(extras);
504
Tyler Gunn504eb832014-10-13 14:15:27 -0700505 // Do not add the call if it is a potential MMI code.
Nancy Chen8d92f452014-10-20 21:43:44 -0700506 if ((isPotentialMMICode(handle) || isPotentialInCallMMICode) && !needsAccountSelection) {
Yorke Leeb701f6d2014-08-12 14:04:19 -0700507 call.addListener(this);
Santos Cordoncf5b2912014-11-05 21:57:54 -0800508 } else if (!mCalls.contains(call)) {
509 // We check if mCalls already contains the call because we could potentially be reusing
510 // a call which was previously added (See {@link #getNewOutgoingCall}).
Tyler Gunn5b452df2014-10-01 15:02:58 -0700511 addCall(call);
Yorke Leeb701f6d2014-08-12 14:04:19 -0700512 }
Nancy Chen0d3076c2014-07-30 14:45:44 -0700513
514 return call;
515 }
516
Ben Gilada0d9f752014-02-26 11:49:03 -0800517 /**
Yorke Lee33501632014-03-17 19:24:12 -0700518 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800519 *
Yorke Lee33501632014-03-17 19:24:12 -0700520 * @param handle Handle to connect the call with.
Yorke Lee33501632014-03-17 19:24:12 -0700521 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Nancy Chen53ceedc2014-07-08 18:56:51 -0700522 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700523 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700524 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800525 */
Nancy Chenbc9ef172014-08-15 17:11:57 -0700526 void placeOutgoingCall(Call call, Uri handle, GatewayInfo gatewayInfo, boolean speakerphoneOn,
527 int videoState) {
Nancy Chen0d3076c2014-07-30 14:45:44 -0700528 if (call == null) {
529 // don't do anything if the call no longer exists
530 Log.i(this, "Canceling unknown call.");
Santos Cordonb7af09e2014-08-06 04:30:01 -0700531 return;
532 }
533
Nancy Chen201b4372014-09-08 14:18:24 -0700534 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayAddress();
Yorke Lee33501632014-03-17 19:24:12 -0700535
536 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700537 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700538 } else {
539 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
540 Log.pii(uriHandle), Log.pii(handle));
541 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700542
Nancy Chen0d3076c2014-07-30 14:45:44 -0700543 call.setHandle(uriHandle);
544 call.setGatewayInfo(gatewayInfo);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700545 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700546 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700547
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700548 boolean isEmergencyCall = TelephonyUtil.shouldProcessAsEmergency(mContext,
549 call.getHandle());
Santos Cordonf193ba42014-09-12 06:37:39 -0700550 if (isEmergencyCall) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700551 // Emergency -- CreateConnectionProcessor will choose accounts automatically
Ihab Awadb78b2762014-07-25 15:16:23 -0700552 call.setTargetPhoneAccount(null);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700553 }
Nancy Chend9de92c2014-07-21 16:09:13 -0700554
Santos Cordonf193ba42014-09-12 06:37:39 -0700555 if (call.getTargetPhoneAccount() != null || isEmergencyCall) {
Nancy Chenbc9ef172014-08-15 17:11:57 -0700556 // If the account has been set, proceed to place the outgoing call.
557 // Otherwise the connection will be initiated when the account is set by the user.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700558 call.startCreateConnection(mPhoneAccountRegistrar);
Nancy Chend9de92c2014-07-21 16:09:13 -0700559 }
Yorke Leef98fb572014-03-05 10:56:55 -0800560 }
561
562 /**
Santos Cordona1610702014-06-04 20:22:56 -0700563 * Attempts to start a conference call for the specified call.
564 *
Santos Cordon12d61822014-07-29 16:02:20 -0700565 * @param call The call to conference.
566 * @param otherCall The other call to conference with.
Santos Cordona1610702014-06-04 20:22:56 -0700567 */
Santos Cordon12d61822014-07-29 16:02:20 -0700568 void conference(Call call, Call otherCall) {
Santos Cordon0fbe6322014-08-14 04:04:25 -0700569 call.conferenceWith(otherCall);
Santos Cordona1610702014-06-04 20:22:56 -0700570 }
571
572 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700573 * Instructs Telecom to answer the specified call. Intended to be invoked by the in-call
574 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800575 * the user opting to answer said call.
Andrew Lee38931d02014-07-16 10:17:36 -0700576 *
577 * @param call The call to answer.
578 * @param videoState The video state in which to answer the call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800579 */
Andrew Lee38931d02014-07-16 10:17:36 -0700580 void answerCall(Call call, int videoState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700581 if (!mCalls.contains(call)) {
582 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800583 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700584 // If the foreground call is not the ringing call and it is currently isActive() or
Ihab Awad6fb37c82014-08-07 19:48:57 -0700585 // STATE_DIALING, put it on hold before answering the call.
Santos Cordon40f78c22014-04-07 02:11:42 -0700586 if (mForegroundCall != null && mForegroundCall != call &&
587 (mForegroundCall.isActive() ||
588 mForegroundCall.getState() == CallState.DIALING)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700589 if (0 == (mForegroundCall.getCallCapabilities() & PhoneCapabilities.HOLD)) {
590 // This call does not support hold. If it is from a different connection
591 // service, then disconnect it, otherwise allow the connection service to
592 // figure out the right states.
593 if (mForegroundCall.getConnectionService() != call.getConnectionService()) {
594 mForegroundCall.disconnect();
595 }
596 } else {
597 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
598 mForegroundCall, call);
599 mForegroundCall.hold();
600 }
Santos Cordondf399862014-08-06 04:39:15 -0700601 // TODO: Wait until we get confirmation of the active call being
Santos Cordon40f78c22014-04-07 02:11:42 -0700602 // on-hold before answering the new call.
Santos Cordondf399862014-08-06 04:39:15 -0700603 // TODO: Import logic from CallManager.acceptCall()
Santos Cordon40f78c22014-04-07 02:11:42 -0700604 }
605
Santos Cordona56f2762014-03-24 15:55:53 -0700606 for (CallsManagerListener listener : mListeners) {
607 listener.onIncomingCallAnswered(call);
608 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800609
Santos Cordon61d0f702014-02-19 02:52:23 -0800610 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700611 // {@link #markCallAsActive}.
Andrew Lee38931d02014-07-16 10:17:36 -0700612 call.answer(videoState);
Santos Cordon61d0f702014-02-19 02:52:23 -0800613 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800614 }
615
616 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700617 * Instructs Telecom to reject the specified call. Intended to be invoked by the in-call
618 * app through {@link InCallAdapter} after Telecom notifies it of an incoming call followed by
Santos Cordone3d76ab2014-01-28 17:25:20 -0800619 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800620 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700621 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700622 if (!mCalls.contains(call)) {
623 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800624 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700625 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700626 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700627 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700628 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800629 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800630 }
631
632 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700633 * Instructs Telecom to play the specified DTMF tone within the specified call.
Ihab Awad74549ec2014-03-10 15:33:25 -0700634 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700635 * @param digit The DTMF digit to play.
636 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700637 void playDtmfTone(Call call, char digit) {
638 if (!mCalls.contains(call)) {
639 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700640 } else {
641 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700642 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700643 }
644 }
645
646 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700647 * Instructs Telecom to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700648 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700649 void stopDtmfTone(Call call) {
650 if (!mCalls.contains(call)) {
651 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700652 } else {
653 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700654 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700655 }
656 }
657
658 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700659 * Instructs Telecom to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700660 */
Evan Charlton352105c2014-06-03 14:10:54 -0700661 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700662 if (!mCalls.contains(call)) {
663 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700664 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700665 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700666 }
667 }
668
669 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700670 * Instructs Telecom to disconnect the specified call. Intended to be invoked by the
Santos Cordone3d76ab2014-01-28 17:25:20 -0800671 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
672 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800673 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700674 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700675 Log.v(this, "disconnectCall %s", call);
676
Sailesh Nepale59bb192014-04-01 18:33:59 -0700677 if (!mCalls.contains(call)) {
678 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800679 } else {
Ihab Awad5b281322014-09-25 18:25:29 -0700680 mLocallyDisconnectingCalls.add(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800681 call.disconnect();
682 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800683 }
Santos Cordon681663d2014-01-30 04:32:15 -0800684
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700685 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700686 * Instructs Telecom to disconnect all calls.
Tyler Gunn61b92102014-08-19 07:42:20 -0700687 */
688 void disconnectAllCalls() {
689 Log.v(this, "disconnectAllCalls");
690
691 for (Call call : mCalls) {
692 disconnectCall(call);
693 }
694 }
695
Nancy Chenbc9ef172014-08-15 17:11:57 -0700696
Tyler Gunn61b92102014-08-19 07:42:20 -0700697 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700698 * Instructs Telecom to put the specified call on hold. Intended to be invoked by the
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700699 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
700 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700701 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700702 void holdCall(Call call) {
703 if (!mCalls.contains(call)) {
704 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700705 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700706 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700707 call.hold();
708 }
709 }
710
711 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700712 * Instructs Telecom to release the specified call from hold. Intended to be invoked by
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700713 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
714 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700715 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700716 void unholdCall(Call call) {
717 if (!mCalls.contains(call)) {
718 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700719 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700720 Log.d(this, "unholding call: (%s)", call);
Sai Cheemalapati45b3e3f2014-08-15 09:33:00 -0700721 for (Call c : mCalls) {
722 if (c != null && c.isAlive() && c != call) {
723 c.hold();
724 }
725 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700726 call.unhold();
727 }
728 }
729
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700730 /** Called by the in-call UI to change the mute state. */
731 void mute(boolean shouldMute) {
732 mCallAudioManager.mute(shouldMute);
733 }
734
735 /**
736 * Called by the in-call UI to change the audio route, for example to change from earpiece to
737 * speaker phone.
738 */
739 void setAudioRoute(int route) {
740 mCallAudioManager.setAudioRoute(route);
741 }
742
Yorke Leed1346872014-07-28 14:38:45 -0700743 /** Called by the in-call UI to turn the proximity sensor on. */
744 void turnOnProximitySensor() {
745 mProximitySensorManager.turnOn();
746 }
747
748 /**
749 * Called by the in-call UI to turn the proximity sensor off.
750 * @param screenOnImmediately If true, the screen will be turned on immediately. Otherwise,
751 * the screen will be kept off until the proximity sensor goes negative.
752 */
753 void turnOffProximitySensor(boolean screenOnImmediately) {
754 mProximitySensorManager.turnOff(screenOnImmediately);
755 }
756
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700757 void phoneAccountSelected(Call call, PhoneAccountHandle account, boolean setDefault) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700758 if (!mCalls.contains(call)) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700759 Log.i(this, "Attempted to add account to unknown call %s", call);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700760 } else {
Santos Cordonf193ba42014-09-12 06:37:39 -0700761 // TODO: There is an odd race condition here. Since NewOutgoingCallIntentBroadcaster and
762 // the PRE_DIAL_WAIT sequence run in parallel, if the user selects an account before the
763 // NEW_OUTGOING_CALL sequence finishes, we'll start the call immediately without
764 // respecting a rewritten number or a canceled number. This is unlikely since
765 // NEW_OUTGOING_CALL sequence, in practice, runs a lot faster than the user selecting
766 // a phone account from the in-call UI.
Ihab Awadb78b2762014-07-25 15:16:23 -0700767 call.setTargetPhoneAccount(account);
Santos Cordonf193ba42014-09-12 06:37:39 -0700768
769 // Note: emergency calls never go through account selection dialog so they never
770 // arrive here.
771 if (makeRoomForOutgoingCall(call, false /* isEmergencyCall */)) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700772 call.startCreateConnection(mPhoneAccountRegistrar);
Santos Cordonf193ba42014-09-12 06:37:39 -0700773 } else {
774 call.disconnect();
775 }
Nancy Chenf5e5d3c2014-10-21 18:45:56 -0700776
777 if (setDefault) {
778 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount(account);
779 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700780 }
781 }
782
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700783 /** Called when the audio state changes. */
Ihab Awad6fb37c82014-08-07 19:48:57 -0700784 void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700785 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700786 for (CallsManagerListener listener : mListeners) {
787 listener.onAudioStateChanged(oldAudioState, newAudioState);
788 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700789 }
790
Sailesh Nepale59bb192014-04-01 18:33:59 -0700791 void markCallAsRinging(Call call) {
792 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800793 }
794
Sailesh Nepale59bb192014-04-01 18:33:59 -0700795 void markCallAsDialing(Call call) {
796 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800797 }
798
Sailesh Nepale59bb192014-04-01 18:33:59 -0700799 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700800 if (call.getConnectTimeMillis() == 0) {
801 call.setConnectTimeMillis(System.currentTimeMillis());
802 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700803 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700804
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700805 if (call.getStartWithSpeakerphoneOn()) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700806 setAudioRoute(AudioState.ROUTE_SPEAKER);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700807 }
Santos Cordon681663d2014-01-30 04:32:15 -0800808 }
809
Sailesh Nepale59bb192014-04-01 18:33:59 -0700810 void markCallAsOnHold(Call call) {
811 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700812 }
813
Santos Cordon049b7b62014-01-30 05:34:26 -0800814 /**
Santos Cordonf193ba42014-09-12 06:37:39 -0700815 * Marks the specified call as STATE_DISCONNECTED and notifies the in-call app. If this was the
816 * last live call, then also disconnect from the in-call controller.
Santos Cordon049b7b62014-01-30 05:34:26 -0800817 *
Andrew Lee701dc002014-09-11 21:29:12 -0700818 * @param disconnectCause The disconnect cause, see {@link android.telecomm.DisconnectCause}.
Santos Cordon049b7b62014-01-30 05:34:26 -0800819 */
Andrew Lee701dc002014-09-11 21:29:12 -0700820 void markCallAsDisconnected(Call call, DisconnectCause disconnectCause) {
821 call.setDisconnectCause(disconnectCause);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700822 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700823 }
824
Ben Gilada0d9f752014-02-26 11:49:03 -0800825 /**
Ihab Awada02bef52014-08-13 18:18:42 -0700826 * Removes an existing disconnected call, and notifies the in-call app.
827 */
828 void markCallAsRemoved(Call call) {
829 removeCall(call);
Ihab Awad5b281322014-09-25 18:25:29 -0700830 if (mLocallyDisconnectingCalls.contains(call)) {
831 mLocallyDisconnectingCalls.remove(call);
832 if (mForegroundCall != null && mForegroundCall.getState() == CallState.ON_HOLD) {
833 mForegroundCall.unhold();
834 }
835 }
Ihab Awada02bef52014-08-13 18:18:42 -0700836 }
837
838 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700839 * Cleans up any calls currently associated with the specified connection service when the
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700840 * service binder disconnects unexpectedly.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700841 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700842 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700843 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700844 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700845 if (service != null) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700846 for (Call call : mCalls) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700847 if (call.getConnectionService() == service) {
Andrew Lee701dc002014-09-11 21:29:12 -0700848 markCallAsDisconnected(call, new DisconnectCause(DisconnectCause.ERROR));
Jay Shrauneracb91eb2014-08-08 16:04:53 -0700849 }
Santos Cordon4b2c1192014-03-19 18:15:38 -0700850 }
851 }
852 }
853
Santos Cordondeb8c892014-05-30 01:38:03 -0700854 boolean hasAnyCalls() {
855 return !mCalls.isEmpty();
856 }
857
Santos Cordonc7e85d42014-05-22 02:51:48 -0700858 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700859 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700860 }
861
862 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700863 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700864 }
865
Santos Cordondeb8c892014-05-30 01:38:03 -0700866 boolean onMediaButton(int type) {
867 if (hasAnyCalls()) {
868 if (HeadsetMediaButton.SHORT_PRESS == type) {
869 Call ringingCall = getFirstCallWithState(CallState.RINGING);
870 if (ringingCall == null) {
871 mCallAudioManager.toggleMute();
872 return true;
873 } else {
Andrew Lee38931d02014-07-16 10:17:36 -0700874 ringingCall.answer(ringingCall.getVideoState());
Santos Cordondeb8c892014-05-30 01:38:03 -0700875 return true;
876 }
877 } else if (HeadsetMediaButton.LONG_PRESS == type) {
878 Log.d(this, "handleHeadsetHook: longpress -> hangup");
879 Call callToHangup = getFirstCallWithState(
880 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
881 if (callToHangup != null) {
882 callToHangup.disconnect();
883 return true;
884 }
885 }
886 }
887 return false;
888 }
889
890 /**
Santos Cordon91bd74c2014-11-07 16:10:22 -0800891 * Returns true if telecom supports adding another top-level call.
Santos Cordon10838c22014-06-11 17:36:04 -0700892 */
Santos Cordon91bd74c2014-11-07 16:10:22 -0800893 boolean canAddCall() {
894 int count = 0;
895 for (Call call : mCalls) {
896 if (call.isEmergencyCall()) {
897 // We never support add call if one of the calls is an emergency call.
898 return false;
899 } else if (call.getParentCall() == null) {
900 count++;
901 }
Santos Cordon10838c22014-06-11 17:36:04 -0700902
Santos Cordon91bd74c2014-11-07 16:10:22 -0800903 // We do not check states for canAddCall. We treat disconnected calls the same
904 // and wait until they are removed instead. If we didn't count disconnected calls,
905 // we could put InCallServices into a state where they are showing two calls but
906 // also support add-call. Technically it's right, but overall looks better (UI-wise)
907 // and acts better if we wait until the call is removed.
908 if (count >= MAXIMUM_TOP_LEVEL_CALLS) {
Santos Cordon10838c22014-06-11 17:36:04 -0700909 return false;
910 }
911 }
912 return true;
913 }
914
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700915 Call getRingingCall() {
916 return getFirstCallWithState(CallState.RINGING);
917 }
918
919 Call getActiveCall() {
920 return getFirstCallWithState(CallState.ACTIVE);
921 }
922
Nancy Chen05a9e402014-09-26 14:14:32 -0700923 Call getDialingCall() {
924 return getFirstCallWithState(CallState.DIALING);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700925 }
926
927 Call getHeldCall() {
928 return getFirstCallWithState(CallState.ON_HOLD);
929 }
930
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700931 int getNumHeldCalls() {
932 int count = 0;
933 for (Call call : mCalls) {
934 if (call.getParentCall() == null && call.getState() == CallState.ON_HOLD) {
935 count++;
936 }
937 }
938 return count;
939 }
940
Santos Cordonf193ba42014-09-12 06:37:39 -0700941 Call getFirstCallWithState(int... states) {
942 return getFirstCallWithState(null, states);
943 }
944
Santos Cordon10838c22014-06-11 17:36:04 -0700945 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700946 * Returns the first call that it finds with the given states. The states are treated as having
947 * priority order so that any call with the first state will be returned before any call with
948 * states listed later in the parameter list.
Santos Cordonf193ba42014-09-12 06:37:39 -0700949 *
950 * @param callToSkip Call that this method should skip while searching
Santos Cordondeb8c892014-05-30 01:38:03 -0700951 */
Santos Cordonf193ba42014-09-12 06:37:39 -0700952 Call getFirstCallWithState(Call callToSkip, int... states) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700953 for (int currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700954 // check the foreground first
955 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
956 return mForegroundCall;
957 }
958
Santos Cordondeb8c892014-05-30 01:38:03 -0700959 for (Call call : mCalls) {
Santos Cordonf193ba42014-09-12 06:37:39 -0700960 if (Objects.equals(callToSkip, call)) {
961 continue;
962 }
963
964 // Only operate on top-level calls
965 if (call.getParentCall() != null) {
966 continue;
967 }
968
Santos Cordondeb8c892014-05-30 01:38:03 -0700969 if (currentState == call.getState()) {
970 return call;
971 }
972 }
973 }
974 return null;
975 }
976
Santos Cordon0fbe6322014-08-14 04:04:25 -0700977 Call createConferenceCall(
978 PhoneAccountHandle phoneAccount,
979 ParcelableConference parcelableConference) {
980 Call call = new Call(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700981 mContext,
Santos Cordon0fbe6322014-08-14 04:04:25 -0700982 mConnectionServiceRepository,
983 null /* handle */,
984 null /* gatewayInfo */,
985 null /* connectionManagerPhoneAccount */,
986 phoneAccount,
987 false /* isIncoming */,
988 true /* isConference */);
989
990 setCallState(call, Call.getStateFromConnectionState(parcelableConference.getState()));
Yorke Lee03f51282014-09-11 09:49:26 -0700991 if (call.getState() == CallState.ACTIVE) {
992 call.setConnectTimeMillis(System.currentTimeMillis());
993 }
Santos Cordon0fbe6322014-08-14 04:04:25 -0700994 call.setCallCapabilities(parcelableConference.getCapabilities());
995
996 // TODO: Move this to be a part of addCall()
997 call.addListener(this);
998 addCall(call);
999 return call;
1000 }
1001
Yorke Leef86db2e2014-09-12 17:56:48 -07001002 /**
1003 * @return the call state currently tracked by {@link PhoneStateBroadcaster}
1004 */
1005 int getCallState() {
1006 return mPhoneStateBroadcaster.getCallState();
1007 }
Nancy Chenbc9ef172014-08-15 17:11:57 -07001008
Santos Cordon4b2c1192014-03-19 18:15:38 -07001009 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001010 * Retrieves the {@link PhoneAccountRegistrar}.
1011 *
1012 * @return The {@link PhoneAccountRegistrar}.
1013 */
1014 PhoneAccountRegistrar getPhoneAccountRegistrar() {
1015 return mPhoneAccountRegistrar;
1016 }
1017
1018 /**
1019 * Retrieves the {@link MissedCallNotifier}
1020 * @return The {@link MissedCallNotifier}.
1021 */
1022 MissedCallNotifier getMissedCallNotifier() {
1023 return mMissedCallNotifier;
1024 }
1025
1026 /**
Ben Gilada0d9f752014-02-26 11:49:03 -08001027 * Adds the specified call to the main list of live calls.
1028 *
1029 * @param call The call to add.
1030 */
1031 private void addCall(Call call) {
Yorke Leeb701f6d2014-08-12 14:04:19 -07001032 Log.v(this, "addCall(%s)", call);
1033
1034 call.addListener(this);
Sailesh Nepale59bb192014-04-01 18:33:59 -07001035 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -07001036
Santos Cordondf399862014-08-06 04:39:15 -07001037 // TODO: Update mForegroundCall prior to invoking
Santos Cordon40f78c22014-04-07 02:11:42 -07001038 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -07001039 for (CallsManagerListener listener : mListeners) {
1040 listener.onCallAdded(call);
1041 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001042 updateCallsManagerState();
Ben Gilada0d9f752014-02-26 11:49:03 -08001043 }
1044
Sailesh Nepal810735e2014-03-18 18:15:46 -07001045 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -07001046 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -07001047
Santos Cordon672321e2014-08-21 14:38:58 -07001048 call.setParentCall(null); // need to clean up parent relationship before destroying.
Santos Cordon766d04f2014-05-06 10:28:25 -07001049 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001050 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -07001051
1052 boolean shouldNotify = false;
1053 if (mCalls.contains(call)) {
1054 mCalls.remove(call);
1055 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -07001056 }
Ben Gilada0d9f752014-02-26 11:49:03 -08001057
Sailesh Nepale59bb192014-04-01 18:33:59 -07001058 // Only broadcast changes for calls that are being tracked.
1059 if (shouldNotify) {
1060 for (CallsManagerListener listener : mListeners) {
1061 listener.onCallRemoved(call);
1062 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001063 updateCallsManagerState();
Yorke Lee604a90f2014-10-20 12:13:32 -07001064 }
Sailesh Nepal810735e2014-03-18 18:15:46 -07001065 }
Evan Charlton5c670a92014-03-06 14:58:20 -08001066
Sailesh Nepal810735e2014-03-18 18:15:46 -07001067 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -07001068 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -07001069 *
1070 * @param call The call.
1071 * @param newState The new state of the call.
1072 */
Ihab Awad6fb37c82014-08-07 19:48:57 -07001073 private void setCallState(Call call, int newState) {
Jay Shrauneracb91eb2014-08-08 16:04:53 -07001074 if (call == null) {
1075 return;
1076 }
Ihab Awad6fb37c82014-08-07 19:48:57 -07001077 int oldState = call.getState();
Nancy Chen308ab8b2014-09-02 16:18:30 -07001078 Log.i(this, "setCallState %s -> %s, call: %s", CallState.toString(oldState),
1079 CallState.toString(newState), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001080 if (newState != oldState) {
1081 // Unfortunately, in the telephony world the radio is king. So if the call notifies
1082 // 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 -07001083 // sense (e.g., STATE_ACTIVE -> STATE_RINGING).
Santos Cordondf399862014-08-06 04:39:15 -07001084 // TODO: Consider putting a stop to the above and turning CallState
Sailesh Nepal810735e2014-03-18 18:15:46 -07001085 // into a well-defined state machine.
Santos Cordondf399862014-08-06 04:39:15 -07001086 // TODO: Define expected state transitions here, and log when an
Sailesh Nepal810735e2014-03-18 18:15:46 -07001087 // unexpected transition occurs.
1088 call.setState(newState);
1089
1090 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -07001091 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -07001092 for (CallsManagerListener listener : mListeners) {
1093 listener.onCallStateChanged(call, oldState, newState);
1094 }
Santos Cordon91bd74c2014-11-07 16:10:22 -08001095 updateCallsManagerState();
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001096 }
1097 }
1098 }
1099
1100 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -07001101 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -08001102 */
Sailesh Nepal810735e2014-03-18 18:15:46 -07001103 private void updateForegroundCall() {
1104 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -07001105 for (Call call : mCalls) {
Santos Cordondf399862014-08-06 04:39:15 -07001106 // TODO: Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001107 // of its state will be foreground by default and instead the connection service should
1108 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -07001109 // the call should play audio and listen to microphone if it wants.
1110
Santos Cordonf193ba42014-09-12 06:37:39 -07001111 // Only top-level calls can be in foreground
1112 if (call.getParentCall() != null) {
1113 continue;
1114 }
1115
Santos Cordon40f78c22014-04-07 02:11:42 -07001116 // Active calls have priority.
1117 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001118 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -08001119 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -07001120 }
Santos Cordon40f78c22014-04-07 02:11:42 -07001121
1122 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -07001123 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -07001124 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001125 }
1126 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -08001127
Sailesh Nepal810735e2014-03-18 18:15:46 -07001128 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -07001129 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -07001130 Call oldForegroundCall = mForegroundCall;
1131 mForegroundCall = newForegroundCall;
Ihab Awad5b281322014-09-25 18:25:29 -07001132
Santos Cordona56f2762014-03-24 15:55:53 -07001133 for (CallsManagerListener listener : mListeners) {
1134 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
1135 }
Santos Cordon681663d2014-01-30 04:32:15 -08001136 }
1137 }
Yorke Leeb701f6d2014-08-12 14:04:19 -07001138
Santos Cordon91bd74c2014-11-07 16:10:22 -08001139 private void updateCanAddCall() {
1140 boolean newCanAddCall = canAddCall();
1141 if (newCanAddCall != mCanAddCall) {
1142 mCanAddCall = newCanAddCall;
1143 for (CallsManagerListener listener : mListeners) {
1144 listener.onCanAddCallChanged(mCanAddCall);
1145 }
1146 }
1147 }
1148
1149 private void updateCallsManagerState() {
1150 updateForegroundCall();
1151 updateCanAddCall();
1152 }
1153
Yorke Leeb701f6d2014-08-12 14:04:19 -07001154 private boolean isPotentialMMICode(Uri handle) {
1155 return (handle != null && handle.getSchemeSpecificPart() != null
1156 && handle.getSchemeSpecificPart().contains("#"));
1157 }
Santos Cordonf193ba42014-09-12 06:37:39 -07001158
Tyler Gunn5b452df2014-10-01 15:02:58 -07001159 /**
1160 * Determines if a dialed number is potentially an In-Call MMI code. In-Call MMI codes are
1161 * MMI codes which can be dialed when one or more calls are in progress.
1162 * <P>
1163 * Checks for numbers formatted similar to the MMI codes defined in:
1164 * {@link com.android.internal.telephony.gsm.GSMPhone#handleInCallMmiCommands(String)}
1165 * and
1166 * {@link com.android.internal.telephony.imsphone.ImsPhone#handleInCallMmiCommands(String)}
1167 *
1168 * @param handle The URI to call.
1169 * @return {@code True} if the URI represents a number which could be an in-call MMI code.
1170 */
1171 private boolean isPotentialInCallMMICode(Uri handle) {
1172 if (handle != null && handle.getSchemeSpecificPart() != null &&
1173 handle.getScheme().equals(PhoneAccount.SCHEME_TEL)) {
1174
1175 String dialedNumber = handle.getSchemeSpecificPart();
1176 return (dialedNumber.equals("0") ||
1177 (dialedNumber.startsWith("1") && dialedNumber.length() <= 2) ||
1178 (dialedNumber.startsWith("2") && dialedNumber.length() <= 2) ||
1179 dialedNumber.equals("3") ||
1180 dialedNumber.equals("4") ||
1181 dialedNumber.equals("5"));
1182 }
1183 return false;
1184 }
1185
Santos Cordonf193ba42014-09-12 06:37:39 -07001186 private int getNumCallsWithState(int... states) {
1187 int count = 0;
1188 for (int state : states) {
1189 for (Call call : mCalls) {
Santos Cordon91bd74c2014-11-07 16:10:22 -08001190 if (call.getParentCall() == null && call.getState() == state) {
Santos Cordonf193ba42014-09-12 06:37:39 -07001191 count++;
1192 }
1193 }
1194 }
1195 return count;
1196 }
1197
1198 private boolean hasMaximumLiveCalls() {
1199 return MAXIMUM_LIVE_CALLS <= getNumCallsWithState(LIVE_CALL_STATES);
1200 }
1201
1202 private boolean hasMaximumHoldingCalls() {
1203 return MAXIMUM_HOLD_CALLS <= getNumCallsWithState(CallState.ON_HOLD);
1204 }
1205
1206 private boolean hasMaximumRingingCalls() {
1207 return MAXIMUM_RINGING_CALLS <= getNumCallsWithState(CallState.RINGING);
1208 }
1209
1210 private boolean hasMaximumOutgoingCalls() {
1211 return MAXIMUM_OUTGOING_CALLS <= getNumCallsWithState(OUTGOING_CALL_STATES);
1212 }
1213
1214 private boolean makeRoomForOutgoingCall(Call call, boolean isEmergency) {
1215 if (hasMaximumLiveCalls()) {
1216 // NOTE: If the amount of live calls changes beyond 1, this logic will probably
1217 // have to change.
1218 Call liveCall = getFirstCallWithState(call, LIVE_CALL_STATES);
1219
Santos Cordond1766502014-09-26 15:45:39 -07001220 if (call == liveCall) {
1221 // If the call is already the foreground call, then we are golden.
1222 // This can happen after the user selects an account in the PRE_DIAL_WAIT
1223 // state since the call was already populated into the list.
1224 return true;
1225 }
1226
Andrew Leee6595182014-09-23 18:43:05 -07001227 if (hasMaximumOutgoingCalls()) {
1228 // Disconnect the current outgoing call if it's not an emergency call. If the user
1229 // tries to make two outgoing calls to different emergency call numbers, we will try
1230 // to connect the first outgoing call.
1231 if (isEmergency) {
1232 Call outgoingCall = getFirstCallWithState(OUTGOING_CALL_STATES);
1233 if (!outgoingCall.isEmergencyCall()) {
1234 outgoingCall.disconnect();
1235 return true;
1236 }
1237 }
1238 return false;
1239 }
1240
Santos Cordonf193ba42014-09-12 06:37:39 -07001241 if (hasMaximumHoldingCalls()) {
1242 // There is no more room for any more calls, unless it's an emergency.
1243 if (isEmergency) {
1244 // Kill the current active call, this is easier then trying to disconnect a
1245 // holding call and hold an active call.
1246 liveCall.disconnect();
1247 return true;
1248 }
1249 return false; // No more room!
1250 }
1251
1252 // We have room for at least one more holding call at this point.
1253
1254 // First thing, if we are trying to make a call with the same phone account as the live
1255 // call, then allow it so that the connection service can make its own decision about
1256 // how to handle the new call relative to the current one.
1257 if (Objects.equals(liveCall.getTargetPhoneAccount(), call.getTargetPhoneAccount())) {
1258 return true;
1259 } else if (call.getTargetPhoneAccount() == null) {
1260 // Without a phone account, we can't say reliably that the call will fail.
1261 // If the user chooses the same phone account as the live call, then it's
1262 // still possible that the call can be made (like with CDMA calls not supporting
1263 // hold but they still support adding a call by going immediately into conference
1264 // mode). Return true here and we'll run this code again after user chooses an
1265 // account.
1266 return true;
1267 }
1268
1269 // Try to hold the live call before attempting the new outgoing call.
1270 if (liveCall.can(PhoneCapabilities.HOLD)) {
1271 liveCall.hold();
1272 return true;
1273 }
1274
1275 // The live call cannot be held so we're out of luck here. There's no room.
1276 return false;
1277 }
1278 return true;
1279 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001280
1281 /**
Tyler Gunn6e2b94e2014-10-30 11:05:22 -07001282 * Creates a new call for an existing connection.
1283 *
1284 * @param callId The id of the new call.
1285 * @param connection The connection information.
1286 * @return The new call.
1287 */
1288 Call createCallForExistingConnection(String callId, ParcelableConnection connection) {
1289 Call call = new Call(
1290 mContext,
1291 mConnectionServiceRepository,
1292 connection.getHandle() /* handle */,
1293 null /* gatewayInfo */,
1294 null /* connectionManagerPhoneAccount */,
1295 connection.getPhoneAccount(), /* targetPhoneAccountHandle */
1296 false /* isIncoming */,
1297 false /* isConference */);
1298
1299 setCallState(call, Call.getStateFromConnectionState(connection.getState()));
1300 call.setConnectTimeMillis(System.currentTimeMillis());
1301 call.setCallCapabilities(connection.getCapabilities());
1302 call.setCallerDisplayName(connection.getCallerDisplayName(),
1303 connection.getCallerDisplayNamePresentation());
1304
1305 call.addListener(this);
1306 addCall(call);
1307
1308 return call;
1309 }
1310
1311 /**
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001312 * Dumps the state of the {@link CallsManager}.
1313 *
1314 * @param pw The {@code IndentingPrintWriter} to write the state to.
1315 */
1316 public void dump(IndentingPrintWriter pw) {
1317 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001318 if (mCalls != null) {
1319 pw.println("mCalls: ");
1320 pw.increaseIndent();
1321 for (Call call : mCalls) {
1322 pw.println(call);
1323 }
1324 pw.decreaseIndent();
1325 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001326 pw.println("mForegroundCall: " + (mForegroundCall == null ? "none" : mForegroundCall));
1327
1328 if (mCallAudioManager != null) {
1329 pw.println("mCallAudioManager:");
1330 pw.increaseIndent();
1331 mCallAudioManager.dump(pw);
1332 pw.decreaseIndent();
1333 }
1334
1335 if (mTtyManager != null) {
1336 pw.println("mTtyManager:");
1337 pw.increaseIndent();
1338 mTtyManager.dump(pw);
1339 pw.decreaseIndent();
1340 }
1341
1342 if (mInCallController != null) {
1343 pw.println("mInCallController:");
1344 pw.increaseIndent();
1345 mInCallController.dump(pw);
1346 pw.decreaseIndent();
1347 }
1348
1349 if (mConnectionServiceRepository != null) {
1350 pw.println("mConnectionServiceRepository:");
1351 pw.increaseIndent();
1352 mConnectionServiceRepository.dump(pw);
1353 pw.decreaseIndent();
1354 }
Tyler Gunn91d43cf2014-09-17 12:19:39 -07001355 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001356}