blob: d86a8f9768410519f8ca8192c0787527f77e6de9 [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) {
Santos Cordon74d420b2014-05-07 14:38:47 -0700397 if (isAborted) {
398 finalizeAbort();
399 }
400
Santos Cordon766d04f2014-05-06 10:28:25 -0700401 // TODO(santoscordon): Replace this with state transitions related to "connecting".
402 for (Listener l : mListeners) {
403 l.onFailedOutgoingCall(this, isAborted);
404 }
405 }
406
407 /**
Ben Gilad61925612014-03-11 19:06:36 -0700408 * Adds the specified call service to the list of incompatible services. The set is used when
409 * attempting to switch a phone call between call services such that incompatible services can
410 * be avoided.
411 *
412 * @param callService The incompatible call service.
413 */
414 void addIncompatibleCallService(CallServiceWrapper callService) {
415 if (mIncompatibleCallServices == null) {
416 mIncompatibleCallServices = Sets.newHashSet();
417 }
418 mIncompatibleCallServices.add(callService);
419 }
420
421 /**
422 * Checks whether or not the specified callService was identified as incompatible in the
423 * context of this call.
424 *
425 * @param callService The call service to evaluate.
426 * @return True upon incompatible call services and false otherwise.
427 */
428 boolean isIncompatibleCallService(CallServiceWrapper callService) {
429 return mIncompatibleCallServices != null &&
430 mIncompatibleCallServices.contains(callService);
431 }
432
433 /**
Santos Cordon766d04f2014-05-06 10:28:25 -0700434 * Issues an abort signal to the associated call service and clears the current call service
435 * and call-service selector.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800436 */
Santos Cordon766d04f2014-05-06 10:28:25 -0700437 void finalizeAbort() {
438 Preconditions.checkState(mState == CallState.NEW);
439
440 if (mCallService != null) {
441 mCallService.abort(this);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800442 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700443 clearCallService();
444 clearCallServiceSelector();
Santos Cordon681663d2014-01-30 04:32:15 -0800445 }
446
Santos Cordonc195e362014-02-11 17:05:31 -0800447 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700448 * Plays the specified DTMF tone.
449 */
450 void playDtmfTone(char digit) {
451 if (mCallService == null) {
452 Log.w(this, "playDtmfTone() request on a call without a call service.");
453 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700454 Log.i(this, "Send playDtmfTone to call service for call %s", this);
455 mCallService.playDtmfTone(this, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700456 }
457 }
458
459 /**
460 * Stops playing any currently playing DTMF tone.
461 */
462 void stopDtmfTone() {
463 if (mCallService == null) {
464 Log.w(this, "stopDtmfTone() request on a call without a call service.");
465 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700466 Log.i(this, "Send stopDtmfTone to call service for call %s", this);
467 mCallService.stopDtmfTone(this);
Ihab Awad74549ec2014-03-10 15:33:25 -0700468 }
469 }
470
471 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800472 * Attempts to disconnect the call through the call service.
473 */
474 void disconnect() {
Santos Cordon766d04f2014-05-06 10:28:25 -0700475 if (mState == CallState.NEW) {
476 // There is a very strange indirection here. When we are told to disconnect, we issue
477 // an 'abort' to the switchboard if we are still in the NEW (or "connecting") state.
478 // The switchboard will then cancel the outgoing call process and ask this class to
479 // finalize the abort procedure via {@link #finalizeAbort}. The issue is that
480 // Switchboard edits the state of the call as part of the process and then this class
481 // is responsible for undoing it, and that is all kinds of backwards.
482 // TODO(santoscordon): Remove Switchboard's requirement to edit the state of the Call
483 // objects and remove any multi-class shared state of incoming and outgoing call
484 // processing.
485 Switchboard.getInstance().abortCall(this);
Santos Cordon74d420b2014-05-07 14:38:47 -0700486 } else if (mState != CallState.ABORTED && mState != CallState.DISCONNECTED) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700487 Preconditions.checkNotNull(mCallService);
488
Sailesh Nepale59bb192014-04-01 18:33:59 -0700489 Log.i(this, "Send disconnect to call service for call: %s", this);
Santos Cordonc195e362014-02-11 17:05:31 -0800490 // The call isn't officially disconnected until the call service confirms that the call
491 // was actually disconnected. Only then is the association between call and call service
492 // severed, see {@link CallsManager#markCallAsDisconnected}.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700493 mCallService.disconnect(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800494 }
495 }
496
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800497 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800498 * Answers the call if it is ringing.
499 */
500 void answer() {
501 Preconditions.checkNotNull(mCallService);
502
503 // Check to verify that the call is still in the ringing state. A call can change states
504 // between the time the user hits 'answer' and Telecomm receives the command.
505 if (isRinging("answer")) {
506 // At this point, we are asking the call service to answer but we don't assume that
507 // it will work. Instead, we wait until confirmation from the call service that the
508 // call is in a non-RINGING state before changing the UI. See
509 // {@link CallServiceAdapter#setActive} and other set* methods.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700510 mCallService.answer(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800511 }
512 }
513
514 /**
515 * Rejects the call if it is ringing.
516 */
517 void reject() {
518 Preconditions.checkNotNull(mCallService);
519
520 // Check to verify that the call is still in the ringing state. A call can change states
521 // between the time the user hits 'reject' and Telecomm receives the command.
522 if (isRinging("reject")) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700523 mCallService.reject(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800524 }
525 }
526
527 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700528 * Puts the call on hold if it is currently active.
529 */
530 void hold() {
531 Preconditions.checkNotNull(mCallService);
532
533 if (mState == CallState.ACTIVE) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700534 mCallService.hold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700535 }
536 }
537
538 /**
539 * Releases the call from hold if it is currently active.
540 */
541 void unhold() {
542 Preconditions.checkNotNull(mCallService);
543
544 if (mState == CallState.ON_HOLD) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700545 mCallService.unhold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700546 }
547 }
548
549 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800550 * @return An object containing read-only information about this call.
551 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700552 CallInfo toCallInfo(String callId) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700553 CallServiceDescriptor descriptor = null;
554 if (mCallService != null) {
555 descriptor = mCallService.getDescriptor();
556 } else if (mOriginalCall != null && mOriginalCall.mCallService != null) {
557 descriptor = mOriginalCall.mCallService.getDescriptor();
558 }
559 return new CallInfo(callId, mState, mHandle, mGatewayInfo, mExtras, descriptor);
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800560 }
561
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700562 /** Checks if this is a live call or not. */
563 boolean isAlive() {
564 switch (mState) {
565 case NEW:
566 case RINGING:
567 case DISCONNECTED:
568 case ABORTED:
569 return false;
570 default:
571 return true;
572 }
573 }
574
Santos Cordon40f78c22014-04-07 02:11:42 -0700575 boolean isActive() {
576 switch (mState) {
577 case ACTIVE:
578 case POST_DIAL:
579 case POST_DIAL_WAIT:
580 return true;
581 default:
582 return false;
583 }
584 }
585
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700586 Bundle getExtras() {
587 return mExtras;
588 }
589
590 void setExtras(Bundle extras) {
591 mExtras = extras;
592 }
593
594 Uri getHandoffHandle() {
595 return mHandoffHandle;
596 }
597
598 void setHandoffHandle(Uri handoffHandle) {
599 mHandoffHandle = handoffHandle;
600 }
601
602 Call getOriginalCall() {
603 return mOriginalCall;
604 }
605
606 void setOriginalCall(Call originalCall) {
607 mOriginalCall = originalCall;
608 }
609
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700610 CallServiceDescriptor getHandoffCallServiceDescriptor() {
611 return mHandoffCallServiceDescriptor;
612 }
613
614 void setHandoffCallServiceDescriptor(CallServiceDescriptor descriptor) {
615 mHandoffCallServiceDescriptor = descriptor;
616 }
617
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800618 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800619 * @return True if the call is ringing, else logs the action name.
620 */
621 private boolean isRinging(String actionName) {
622 if (mState == CallState.RINGING) {
623 return true;
624 }
625
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800626 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800627 return false;
628 }
629
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800630 @SuppressWarnings("rawtypes")
631 private void decrementAssociatedCallCount(ServiceBinder binder) {
632 if (binder != null) {
633 binder.decrementAssociatedCallCount();
634 }
635 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800636}