blob: fe984cd2cabd446ab6d000f95eb3dc4cbcd8c6a4 [file] [log] [blame]
Ben Gilad0407fb22014-01-09 16:18:41 -08001/*
2 * Copyright (C) 2014 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
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Sailesh Nepalce704b92014-03-17 18:31:43 -070019import android.net.Uri;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070020import android.os.Bundle;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080021import android.telecomm.CallInfo;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070022import android.telecomm.CallServiceDescriptor;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080023import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070024import android.telecomm.GatewayInfo;
Santos Cordon766d04f2014-05-06 10:28:25 -070025import android.telecomm.TelecommConstants;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070027import android.telephony.PhoneNumberUtils;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080028
Ben Gilad61925612014-03-11 19:06:36 -070029import com.google.android.collect.Sets;
Santos Cordon61d0f702014-02-19 02:52:23 -080030import com.google.common.base.Preconditions;
31
Sailesh Nepal91990782014-03-08 17:45:52 -080032import java.util.Locale;
Ben Gilad61925612014-03-11 19:06:36 -070033import java.util.Set;
Ben Gilad0407fb22014-01-09 16:18:41 -080034
Ben Gilad2495d572014-01-09 17:26:19 -080035/**
36 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
37 * from the time the call intent was received by Telecomm (vs. the time the call was
38 * connected etc).
39 */
Ben Gilad0407fb22014-01-09 16:18:41 -080040final class Call {
Santos Cordon766d04f2014-05-06 10:28:25 -070041
42 /**
43 * Listener for events on the call.
44 */
45 interface Listener {
46 void onSuccessfulOutgoingCall(Call call);
47 void onFailedOutgoingCall(Call call, boolean isAborted);
48 void onSuccessfulIncomingCall(Call call, CallInfo callInfo);
49 void onFailedIncomingCall(Call call);
50 }
51
Ben Gilad0407fb22014-01-09 16:18:41 -080052 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080053 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080054
Sailesh Nepal810735e2014-03-18 18:15:46 -070055 /** True if this is an incoming call. */
56 private final boolean mIsIncoming;
57
Ben Gilad0407fb22014-01-09 16:18:41 -080058 /**
59 * The time this call was created, typically also the time this call was added to the set
60 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
61 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
62 * certain call attempts as failed attempts.
63 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070064 private final long mCreationTimeMillis = System.currentTimeMillis();
65
66 private long mConnectTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -080067
Santos Cordon61d0f702014-02-19 02:52:23 -080068 /** The state of the call. */
69 private CallState mState;
70
71 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070072 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080073
Yorke Lee33501632014-03-17 19:24:12 -070074 /** The gateway information associated with this call. This stores the original call handle
75 * that the user is attempting to connect to via the gateway, the actual handle to dial in
76 * order to connect the call via the gateway, as well as the package name of the gateway
77 * service. */
78 private final GatewayInfo mGatewayInfo;
79
Ben Gilad0407fb22014-01-09 16:18:41 -080080 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080081 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080082 */
Santos Cordonc195e362014-02-11 17:05:31 -080083 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080084
Ben Gilad8e55d1d2014-02-26 16:25:56 -080085 /**
86 * The call-service selector for this call.
Ben Gilad8e55d1d2014-02-26 16:25:56 -080087 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070088 private CallServiceSelectorWrapper mCallServiceSelector;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080089
Santos Cordon0b03b4b2014-01-29 18:01:59 -080090 /**
Ben Gilad61925612014-03-11 19:06:36 -070091 * The set of call services that were attempted in the process of placing/switching this call
92 * but turned out unsuitable. Only used in the context of call switching.
93 */
94 private Set<CallServiceWrapper> mIncompatibleCallServices;
95
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070096 private boolean mIsEmergencyCall;
97
Ben Gilad61925612014-03-11 19:06:36 -070098 /**
Santos Cordon79ff2bc2014-03-27 15:31:27 -070099 * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED.
100 * See {@link android.telephony.DisconnectCause}.
101 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700102 private int mDisconnectCause = DisconnectCause.NOT_VALID;
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700103
104 /**
105 * Additional disconnect information provided by the call service.
106 */
107 private String mDisconnectMessage;
108
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700109 /** Info used by the call services. */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700110 private Bundle mExtras = Bundle.EMPTY;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700111
112 /** The Uri to dial to perform the handoff. If this is null then handoff is not supported. */
113 private Uri mHandoffHandle;
114
115 /**
116 * References the call that is being handed off. This value is non-null for untracked calls
117 * that are being used to perform a handoff.
118 */
119 private Call mOriginalCall;
120
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700121 /**
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700122 * The descriptor for the call service that this call is being switched to, null if handoff is
123 * not in progress.
124 */
125 private CallServiceDescriptor mHandoffCallServiceDescriptor;
126
Santos Cordon766d04f2014-05-06 10:28:25 -0700127 /** Set of listeners on this call. */
128 private Set<Listener> mListeners = Sets.newHashSet();
129
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700130 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700131 * Creates an empty call object.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700132 *
133 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -0800134 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 Call(boolean isIncoming) {
Yorke Lee33501632014-03-17 19:24:12 -0700136 this(null, null, null, isIncoming);
Santos Cordon493e8f22014-02-19 03:15:12 -0800137 }
138
139 /**
Ben Gilad0407fb22014-01-09 16:18:41 -0800140 * Persists the specified parameters and initializes the new instance.
141 *
142 * @param handle The handle to dial.
143 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700144 * @param gatewayInfo Gateway information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700145 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800146 */
Yorke Lee33501632014-03-17 19:24:12 -0700147 Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) {
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800148 mState = CallState.NEW;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700149 setHandle(handle);
Ben Gilad0407fb22014-01-09 16:18:41 -0800150 mContactInfo = contactInfo;
Yorke Lee33501632014-03-17 19:24:12 -0700151 mGatewayInfo = gatewayInfo;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700152 mIsIncoming = isIncoming;
Ben Gilad0407fb22014-01-09 16:18:41 -0800153 }
154
Santos Cordon766d04f2014-05-06 10:28:25 -0700155 void addListener(Listener listener) {
156 mListeners.add(listener);
157 }
158
159 void removeListener(Listener listener) {
160 mListeners.remove(listener);
161 }
162
Santos Cordon61d0f702014-02-19 02:52:23 -0800163 /** {@inheritDoc} */
164 @Override public String toString() {
Sailesh Nepal4538f012014-04-15 11:40:33 -0700165 String component = null;
166 if (mCallService != null && mCallService.getComponentName() != null) {
167 component = mCallService.getComponentName().flattenToShortString();
168 }
169 return String.format(Locale.US, "[%s, %s, %s]", mState, component, Log.piiHandle(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800170 }
171
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800172 CallState getState() {
173 return mState;
174 }
175
176 /**
177 * Sets the call state. Although there exists the notion of appropriate state transitions
178 * (see {@link CallState}), in practice those expectations break down when cellular systems
179 * misbehave and they do this very often. The result is that we do not enforce state transitions
180 * and instead keep the code resilient to unexpected state changes.
181 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700182 void setState(CallState newState) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700183 Preconditions.checkState(newState != CallState.DISCONNECTED ||
184 mDisconnectCause != DisconnectCause.NOT_VALID);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700185 if (mState != newState) {
186 Log.v(this, "setState %s -> %s", mState, newState);
187 mState = newState;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700188 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800189 }
190
Sailesh Nepalce704b92014-03-17 18:31:43 -0700191 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800192 return mHandle;
193 }
194
Sailesh Nepalce704b92014-03-17 18:31:43 -0700195 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800196 mHandle = handle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700197 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
198 mHandle.getSchemeSpecificPart(), TelecommApp.getInstance());
199 }
200
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700201 /**
202 * @param disconnectCause The reason for the disconnection, any of
203 * {@link android.telephony.DisconnectCause}.
204 * @param disconnectMessage Optional call-service-provided message about the disconnect.
205 */
206 void setDisconnectCause(int disconnectCause, String disconnectMessage) {
207 // TODO: Consider combining this method with a setDisconnected() method that is totally
208 // separate from setState.
209 mDisconnectCause = disconnectCause;
210 mDisconnectMessage = disconnectMessage;
211 }
212
213 int getDisconnectCause() {
214 return mDisconnectCause;
215 }
216
217 String getDisconnectMessage() {
218 return mDisconnectMessage;
219 }
220
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700221 boolean isEmergencyCall() {
222 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800223 }
224
Yorke Lee33501632014-03-17 19:24:12 -0700225 /**
226 * @return The original handle this call is associated with. In-call services should use this
227 * handle when indicating in their UI the handle that is being called.
228 */
229 public Uri getOriginalHandle() {
230 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
231 return mGatewayInfo.getOriginalHandle();
232 }
233 return getHandle();
234 }
235
236 GatewayInfo getGatewayInfo() {
237 return mGatewayInfo;
238 }
239
Ben Gilad0bf5b912014-01-28 17:55:57 -0800240 ContactInfo getContactInfo() {
241 return mContactInfo;
242 }
243
Sailesh Nepal810735e2014-03-18 18:15:46 -0700244 boolean isIncoming() {
245 return mIsIncoming;
246 }
247
Ben Gilad0407fb22014-01-09 16:18:41 -0800248 /**
249 * @return The "age" of this call object in milliseconds, which typically also represents the
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700250 * period since this call was added to the set pending outgoing calls, see
251 * mCreationTimeMillis.
Ben Gilad0407fb22014-01-09 16:18:41 -0800252 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700253 long getAgeMillis() {
254 return System.currentTimeMillis() - mCreationTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -0800255 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800256
Yorke Leef98fb572014-03-05 10:56:55 -0800257 /**
258 * @return The time when this call object was created and added to the set of pending outgoing
259 * calls.
260 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700261 long getCreationTimeMillis() {
262 return mCreationTimeMillis;
263 }
264
265 long getConnectTimeMillis() {
266 return mConnectTimeMillis;
267 }
268
269 void setConnectTimeMillis(long connectTimeMillis) {
270 mConnectTimeMillis = connectTimeMillis;
Yorke Leef98fb572014-03-05 10:56:55 -0800271 }
272
Santos Cordonc195e362014-02-11 17:05:31 -0800273 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800274 return mCallService;
275 }
276
Santos Cordonc195e362014-02-11 17:05:31 -0800277 void setCallService(CallServiceWrapper callService) {
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700278 setCallService(callService, null);
279 }
280
281 /**
282 * Changes the call service this call is associated with. If callToReplace is non-null then this
283 * call takes its place within the call service.
284 */
285 void setCallService(CallServiceWrapper callService, Call callToReplace) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800286 Preconditions.checkNotNull(callService);
287
Yorke Leeadee12d2014-03-13 12:08:30 -0700288 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800289
290 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800291 mCallService = callService;
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700292 if (callToReplace == null) {
293 mCallService.addCall(this);
294 } else {
295 mCallService.replaceCall(this, callToReplace);
296 }
Santos Cordon681663d2014-01-30 04:32:15 -0800297 }
298
299 /**
300 * Clears the associated call service.
301 */
302 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700303 if (mCallService != null) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700304 CallServiceWrapper callServiceTemp = mCallService;
Yorke Leeadee12d2014-03-13 12:08:30 -0700305 mCallService = null;
Santos Cordonc499c1c2014-04-14 17:13:14 -0700306 callServiceTemp.removeCall(this);
307
308 // Decrementing the count can cause the service to unbind, which itself can trigger the
309 // service-death code. Since the service death code tries to clean up any associated
310 // calls, we need to make sure to remove that information (e.g., removeCall()) before
311 // we decrement. Technically, invoking removeCall() prior to decrementing is all that is
312 // necessary, but cleaning up mCallService prior to triggering an unbind is good to do.
313 // If you change this, make sure to update {@link clearCallServiceSelector} as well.
314 decrementAssociatedCallCount(callServiceTemp);
Yorke Leeadee12d2014-03-13 12:08:30 -0700315 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800316 }
317
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700318 CallServiceSelectorWrapper getCallServiceSelector() {
319 return mCallServiceSelector;
320 }
321
Sailesh Nepal18386a82014-03-19 10:22:40 -0700322 void setCallServiceSelector(CallServiceSelectorWrapper selector) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800323 Preconditions.checkNotNull(selector);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700324
325 clearCallServiceSelector();
326
Santos Cordonc499c1c2014-04-14 17:13:14 -0700327 selector.incrementAssociatedCallCount();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800328 mCallServiceSelector = selector;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700329 mCallServiceSelector.addCall(this);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800330 }
331
332 void clearCallServiceSelector() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700333 if (mCallServiceSelector != null) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700334 CallServiceSelectorWrapper selectorTemp = mCallServiceSelector;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700335 mCallServiceSelector = null;
Santos Cordonc499c1c2014-04-14 17:13:14 -0700336 selectorTemp.removeCall(this);
337
338 // See comment on {@link #clearCallService}.
339 decrementAssociatedCallCount(selectorTemp);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700340 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800341 }
342
343 /**
Santos Cordon766d04f2014-05-06 10:28:25 -0700344 * Starts the incoming call flow through the switchboard. When switchboard completes, it will
345 * invoke handle[Un]SuccessfulIncomingCall.
346 *
347 * @param descriptor The relevant call-service descriptor.
348 * @param extras The optional extras passed via
349 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}.
350 */
351 void startIncoming(CallServiceDescriptor descriptor, Bundle extras) {
352 Switchboard.getInstance().retrieveIncomingCall(this, descriptor, extras);
353 }
354
355 void handleSuccessfulIncoming(CallInfo callInfo) {
356 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
357 setHandle(callInfo.getHandle());
358
359 // TODO(santoscordon): Make this class (not CallsManager) responsible for changing the call
360 // state to RINGING.
361
362 // TODO(santoscordon): Replace this with state transitions related to "connecting".
363 for (Listener l : mListeners) {
364 l.onSuccessfulIncomingCall(this, callInfo);
365 }
366 }
367
368 void handleFailedIncoming() {
369 clearCallService();
370
371 // TODO: Needs more specific disconnect error for this case.
372 setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
373 setState(CallState.DISCONNECTED);
374
375 // TODO(santoscordon): Replace this with state transitions related to "connecting".
376 for (Listener l : mListeners) {
377 l.onFailedIncomingCall(this);
378 }
379 }
380
381 /**
382 * Starts the outgoing call flow through the switchboard. When switchboard completes, it will
383 * invoke handleSuccessful/FailedOutgoingCall.
384 */
385 void startOutgoing() {
386 Switchboard.getInstance().placeOutgoingCall(this);
387 }
388
389 void handleSuccessfulOutgoing() {
390 // TODO(santoscordon): Replace this with state transitions related to "connecting".
391 for (Listener l : mListeners) {
392 l.onSuccessfulOutgoingCall(this);
393 }
394 }
395
396 void handleFailedOutgoing(boolean isAborted) {
397 // TODO(santoscordon): Replace this with state transitions related to "connecting".
398 for (Listener l : mListeners) {
399 l.onFailedOutgoingCall(this, isAborted);
400 }
401 }
402
403 /**
Ben Gilad61925612014-03-11 19:06:36 -0700404 * Adds the specified call service to the list of incompatible services. The set is used when
405 * attempting to switch a phone call between call services such that incompatible services can
406 * be avoided.
407 *
408 * @param callService The incompatible call service.
409 */
410 void addIncompatibleCallService(CallServiceWrapper callService) {
411 if (mIncompatibleCallServices == null) {
412 mIncompatibleCallServices = Sets.newHashSet();
413 }
414 mIncompatibleCallServices.add(callService);
415 }
416
417 /**
418 * Checks whether or not the specified callService was identified as incompatible in the
419 * context of this call.
420 *
421 * @param callService The call service to evaluate.
422 * @return True upon incompatible call services and false otherwise.
423 */
424 boolean isIncompatibleCallService(CallServiceWrapper callService) {
425 return mIncompatibleCallServices != null &&
426 mIncompatibleCallServices.contains(callService);
427 }
428
429 /**
Santos Cordon766d04f2014-05-06 10:28:25 -0700430 * Issues an abort signal to the associated call service and clears the current call service
431 * and call-service selector.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800432 */
Santos Cordon766d04f2014-05-06 10:28:25 -0700433 void finalizeAbort() {
434 Preconditions.checkState(mState == CallState.NEW);
435
436 if (mCallService != null) {
437 mCallService.abort(this);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800438 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700439 clearCallService();
440 clearCallServiceSelector();
Santos Cordon681663d2014-01-30 04:32:15 -0800441 }
442
Santos Cordonc195e362014-02-11 17:05:31 -0800443 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700444 * Plays the specified DTMF tone.
445 */
446 void playDtmfTone(char digit) {
447 if (mCallService == null) {
448 Log.w(this, "playDtmfTone() request on a call without a call service.");
449 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700450 Log.i(this, "Send playDtmfTone to call service for call %s", this);
451 mCallService.playDtmfTone(this, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700452 }
453 }
454
455 /**
456 * Stops playing any currently playing DTMF tone.
457 */
458 void stopDtmfTone() {
459 if (mCallService == null) {
460 Log.w(this, "stopDtmfTone() request on a call without a call service.");
461 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700462 Log.i(this, "Send stopDtmfTone to call service for call %s", this);
463 mCallService.stopDtmfTone(this);
Ihab Awad74549ec2014-03-10 15:33:25 -0700464 }
465 }
466
467 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800468 * Attempts to disconnect the call through the call service.
469 */
470 void disconnect() {
Santos Cordon766d04f2014-05-06 10:28:25 -0700471 if (mState == CallState.NEW) {
472 // There is a very strange indirection here. When we are told to disconnect, we issue
473 // an 'abort' to the switchboard if we are still in the NEW (or "connecting") state.
474 // The switchboard will then cancel the outgoing call process and ask this class to
475 // finalize the abort procedure via {@link #finalizeAbort}. The issue is that
476 // Switchboard edits the state of the call as part of the process and then this class
477 // is responsible for undoing it, and that is all kinds of backwards.
478 // TODO(santoscordon): Remove Switchboard's requirement to edit the state of the Call
479 // objects and remove any multi-class shared state of incoming and outgoing call
480 // processing.
481 Switchboard.getInstance().abortCall(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800482 } else {
Santos Cordon766d04f2014-05-06 10:28:25 -0700483 Preconditions.checkNotNull(mCallService);
484
Sailesh Nepale59bb192014-04-01 18:33:59 -0700485 Log.i(this, "Send disconnect to call service for call: %s", this);
Santos Cordonc195e362014-02-11 17:05:31 -0800486 // The call isn't officially disconnected until the call service confirms that the call
487 // was actually disconnected. Only then is the association between call and call service
488 // severed, see {@link CallsManager#markCallAsDisconnected}.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700489 mCallService.disconnect(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800490 }
491 }
492
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800493 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800494 * Answers the call if it is ringing.
495 */
496 void answer() {
497 Preconditions.checkNotNull(mCallService);
498
499 // Check to verify that the call is still in the ringing state. A call can change states
500 // between the time the user hits 'answer' and Telecomm receives the command.
501 if (isRinging("answer")) {
502 // At this point, we are asking the call service to answer but we don't assume that
503 // it will work. Instead, we wait until confirmation from the call service that the
504 // call is in a non-RINGING state before changing the UI. See
505 // {@link CallServiceAdapter#setActive} and other set* methods.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700506 mCallService.answer(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800507 }
508 }
509
510 /**
511 * Rejects the call if it is ringing.
512 */
513 void reject() {
514 Preconditions.checkNotNull(mCallService);
515
516 // Check to verify that the call is still in the ringing state. A call can change states
517 // between the time the user hits 'reject' and Telecomm receives the command.
518 if (isRinging("reject")) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700519 mCallService.reject(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800520 }
521 }
522
523 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700524 * Puts the call on hold if it is currently active.
525 */
526 void hold() {
527 Preconditions.checkNotNull(mCallService);
528
529 if (mState == CallState.ACTIVE) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700530 mCallService.hold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700531 }
532 }
533
534 /**
535 * Releases the call from hold if it is currently active.
536 */
537 void unhold() {
538 Preconditions.checkNotNull(mCallService);
539
540 if (mState == CallState.ON_HOLD) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700541 mCallService.unhold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700542 }
543 }
544
545 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800546 * @return An object containing read-only information about this call.
547 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700548 CallInfo toCallInfo(String callId) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700549 CallServiceDescriptor descriptor = null;
550 if (mCallService != null) {
551 descriptor = mCallService.getDescriptor();
552 } else if (mOriginalCall != null && mOriginalCall.mCallService != null) {
553 descriptor = mOriginalCall.mCallService.getDescriptor();
554 }
555 return new CallInfo(callId, mState, mHandle, mGatewayInfo, mExtras, descriptor);
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800556 }
557
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700558 /** Checks if this is a live call or not. */
559 boolean isAlive() {
560 switch (mState) {
561 case NEW:
562 case RINGING:
563 case DISCONNECTED:
564 case ABORTED:
565 return false;
566 default:
567 return true;
568 }
569 }
570
Santos Cordon40f78c22014-04-07 02:11:42 -0700571 boolean isActive() {
572 switch (mState) {
573 case ACTIVE:
574 case POST_DIAL:
575 case POST_DIAL_WAIT:
576 return true;
577 default:
578 return false;
579 }
580 }
581
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700582 Bundle getExtras() {
583 return mExtras;
584 }
585
586 void setExtras(Bundle extras) {
587 mExtras = extras;
588 }
589
590 Uri getHandoffHandle() {
591 return mHandoffHandle;
592 }
593
594 void setHandoffHandle(Uri handoffHandle) {
595 mHandoffHandle = handoffHandle;
596 }
597
598 Call getOriginalCall() {
599 return mOriginalCall;
600 }
601
602 void setOriginalCall(Call originalCall) {
603 mOriginalCall = originalCall;
604 }
605
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700606 CallServiceDescriptor getHandoffCallServiceDescriptor() {
607 return mHandoffCallServiceDescriptor;
608 }
609
610 void setHandoffCallServiceDescriptor(CallServiceDescriptor descriptor) {
611 mHandoffCallServiceDescriptor = descriptor;
612 }
613
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800614 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800615 * @return True if the call is ringing, else logs the action name.
616 */
617 private boolean isRinging(String actionName) {
618 if (mState == CallState.RINGING) {
619 return true;
620 }
621
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800622 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800623 return false;
624 }
625
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800626 @SuppressWarnings("rawtypes")
627 private void decrementAssociatedCallCount(ServiceBinder binder) {
628 if (binder != null) {
629 binder.decrementAssociatedCallCount();
630 }
631 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800632}