blob: b57ec807cb8d14b20cc6c10fc1914990762c7413 [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;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080020import android.telecomm.CallInfo;
21import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070022import android.telecomm.GatewayInfo;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070023import android.telephony.DisconnectCause;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070024import android.telephony.PhoneNumberUtils;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080025
Ben Gilad61925612014-03-11 19:06:36 -070026import com.google.android.collect.Sets;
Santos Cordon61d0f702014-02-19 02:52:23 -080027import com.google.common.base.Preconditions;
28
Ben Gilad0407fb22014-01-09 16:18:41 -080029import java.util.Date;
Sailesh Nepal91990782014-03-08 17:45:52 -080030import java.util.Locale;
Ben Gilad61925612014-03-11 19:06:36 -070031import java.util.Set;
Ben Gilad0407fb22014-01-09 16:18:41 -080032
Ben Gilad2495d572014-01-09 17:26:19 -080033/**
34 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
35 * from the time the call intent was received by Telecomm (vs. the time the call was
36 * connected etc).
37 */
Ben Gilad0407fb22014-01-09 16:18:41 -080038final class Call {
Ben Gilad0407fb22014-01-09 16:18:41 -080039 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080040 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080041
Sailesh Nepal810735e2014-03-18 18:15:46 -070042 /** True if this is an incoming call. */
43 private final boolean mIsIncoming;
44
Ben Gilad0407fb22014-01-09 16:18:41 -080045 /**
46 * The time this call was created, typically also the time this call was added to the set
47 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
48 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
49 * certain call attempts as failed attempts.
50 */
51 private final Date mCreationTime;
52
Santos Cordon61d0f702014-02-19 02:52:23 -080053 /** The state of the call. */
54 private CallState mState;
55
56 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070057 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080058
Yorke Lee33501632014-03-17 19:24:12 -070059 /** The gateway information associated with this call. This stores the original call handle
60 * that the user is attempting to connect to via the gateway, the actual handle to dial in
61 * order to connect the call via the gateway, as well as the package name of the gateway
62 * service. */
63 private final GatewayInfo mGatewayInfo;
64
Ben Gilad0407fb22014-01-09 16:18:41 -080065 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080066 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080067 */
Santos Cordonc195e362014-02-11 17:05:31 -080068 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080069
Ben Gilad8e55d1d2014-02-26 16:25:56 -080070 /**
71 * The call-service selector for this call.
Ben Gilad8e55d1d2014-02-26 16:25:56 -080072 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070073 private CallServiceSelectorWrapper mCallServiceSelector;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080074
Santos Cordon0b03b4b2014-01-29 18:01:59 -080075 /**
Ben Gilad61925612014-03-11 19:06:36 -070076 * The set of call services that were attempted in the process of placing/switching this call
77 * but turned out unsuitable. Only used in the context of call switching.
78 */
79 private Set<CallServiceWrapper> mIncompatibleCallServices;
80
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070081 private boolean mIsEmergencyCall;
82
Ben Gilad61925612014-03-11 19:06:36 -070083 /**
Santos Cordon79ff2bc2014-03-27 15:31:27 -070084 * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED.
85 * See {@link android.telephony.DisconnectCause}.
86 */
87 private int mDisconnectCause;
88
89 /**
90 * Additional disconnect information provided by the call service.
91 */
92 private String mDisconnectMessage;
93
94 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070095 * Creates an empty call object.
Sailesh Nepal810735e2014-03-18 18:15:46 -070096 *
97 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -080098 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070099 Call(boolean isIncoming) {
Yorke Lee33501632014-03-17 19:24:12 -0700100 this(null, null, null, isIncoming);
Santos Cordon493e8f22014-02-19 03:15:12 -0800101 }
102
103 /**
Ben Gilad0407fb22014-01-09 16:18:41 -0800104 * Persists the specified parameters and initializes the new instance.
105 *
106 * @param handle The handle to dial.
107 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700108 * @param gatewayInfo Gateway information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700109 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800110 */
Yorke Lee33501632014-03-17 19:24:12 -0700111 Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) {
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800112 mState = CallState.NEW;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700113 setHandle(handle);
Ben Gilad0407fb22014-01-09 16:18:41 -0800114 mContactInfo = contactInfo;
Yorke Lee33501632014-03-17 19:24:12 -0700115 mGatewayInfo = gatewayInfo;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700116 mIsIncoming = isIncoming;
Ben Gilad0407fb22014-01-09 16:18:41 -0800117 mCreationTime = new Date();
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700118 mDisconnectCause = DisconnectCause.NOT_VALID;
Ben Gilad0407fb22014-01-09 16:18:41 -0800119 }
120
Santos Cordon61d0f702014-02-19 02:52:23 -0800121 /** {@inheritDoc} */
122 @Override public String toString() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700123 return String.format(Locale.US, "[%s, %s, %s]", mState,
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 mCallService == null ? "<null>" : mCallService.getComponentName(),
125 Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800126 }
127
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800128 CallState getState() {
129 return mState;
130 }
131
132 /**
133 * Sets the call state. Although there exists the notion of appropriate state transitions
134 * (see {@link CallState}), in practice those expectations break down when cellular systems
135 * misbehave and they do this very often. The result is that we do not enforce state transitions
136 * and instead keep the code resilient to unexpected state changes.
137 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 void setState(CallState newState) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700139 Preconditions.checkState(newState != CallState.DISCONNECTED ||
140 mDisconnectCause != DisconnectCause.NOT_VALID);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700141 if (mState != newState) {
142 Log.v(this, "setState %s -> %s", mState, newState);
143 mState = newState;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700144 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800145 }
146
Sailesh Nepalce704b92014-03-17 18:31:43 -0700147 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800148 return mHandle;
149 }
150
Sailesh Nepalce704b92014-03-17 18:31:43 -0700151 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800152 mHandle = handle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700153 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
154 mHandle.getSchemeSpecificPart(), TelecommApp.getInstance());
155 }
156
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700157 /**
158 * @param disconnectCause The reason for the disconnection, any of
159 * {@link android.telephony.DisconnectCause}.
160 * @param disconnectMessage Optional call-service-provided message about the disconnect.
161 */
162 void setDisconnectCause(int disconnectCause, String disconnectMessage) {
163 // TODO: Consider combining this method with a setDisconnected() method that is totally
164 // separate from setState.
165 mDisconnectCause = disconnectCause;
166 mDisconnectMessage = disconnectMessage;
167 }
168
169 int getDisconnectCause() {
170 return mDisconnectCause;
171 }
172
173 String getDisconnectMessage() {
174 return mDisconnectMessage;
175 }
176
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700177 boolean isEmergencyCall() {
178 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800179 }
180
Yorke Lee33501632014-03-17 19:24:12 -0700181 /**
182 * @return The original handle this call is associated with. In-call services should use this
183 * handle when indicating in their UI the handle that is being called.
184 */
185 public Uri getOriginalHandle() {
186 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
187 return mGatewayInfo.getOriginalHandle();
188 }
189 return getHandle();
190 }
191
192 GatewayInfo getGatewayInfo() {
193 return mGatewayInfo;
194 }
195
Ben Gilad0bf5b912014-01-28 17:55:57 -0800196 ContactInfo getContactInfo() {
197 return mContactInfo;
198 }
199
Sailesh Nepal810735e2014-03-18 18:15:46 -0700200 boolean isIncoming() {
201 return mIsIncoming;
202 }
203
Ben Gilad0407fb22014-01-09 16:18:41 -0800204 /**
205 * @return The "age" of this call object in milliseconds, which typically also represents the
206 * period since this call was added to the set pending outgoing calls, see mCreationTime.
207 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800208 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800209 return new Date().getTime() - mCreationTime.getTime();
210 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800211
Yorke Leef98fb572014-03-05 10:56:55 -0800212 /**
213 * @return The time when this call object was created and added to the set of pending outgoing
214 * calls.
215 */
216 long getCreationTimeInMilliseconds() {
217 return mCreationTime.getTime();
218 }
219
Santos Cordonc195e362014-02-11 17:05:31 -0800220 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800221 return mCallService;
222 }
223
Santos Cordonc195e362014-02-11 17:05:31 -0800224 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800225 Preconditions.checkNotNull(callService);
226
Yorke Leeadee12d2014-03-13 12:08:30 -0700227 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800228
229 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800230 mCallService = callService;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700231 mCallService.addCall(this);
Santos Cordon681663d2014-01-30 04:32:15 -0800232 }
233
234 /**
235 * Clears the associated call service.
236 */
237 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700238 if (mCallService != null) {
239 decrementAssociatedCallCount(mCallService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700240 mCallService.removeCall(this);
Yorke Leeadee12d2014-03-13 12:08:30 -0700241 mCallService = null;
242 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800243 }
244
Sailesh Nepal18386a82014-03-19 10:22:40 -0700245 void setCallServiceSelector(CallServiceSelectorWrapper selector) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800246 Preconditions.checkNotNull(selector);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700247
248 clearCallServiceSelector();
249
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800250 mCallServiceSelector = selector;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700251 mCallServiceSelector.addCall(this);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800252 }
253
254 void clearCallServiceSelector() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700255 if (mCallServiceSelector != null) {
256 // TODO(sail): Stop leaking selectors.
257 // decrementAssociatedCallCount(mCallServiceSelector);
258 mCallServiceSelector.removeCall(this);
259 mCallServiceSelector = null;
260 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800261 }
262
263 /**
Ben Gilad61925612014-03-11 19:06:36 -0700264 * Adds the specified call service to the list of incompatible services. The set is used when
265 * attempting to switch a phone call between call services such that incompatible services can
266 * be avoided.
267 *
268 * @param callService The incompatible call service.
269 */
270 void addIncompatibleCallService(CallServiceWrapper callService) {
271 if (mIncompatibleCallServices == null) {
272 mIncompatibleCallServices = Sets.newHashSet();
273 }
274 mIncompatibleCallServices.add(callService);
275 }
276
277 /**
278 * Checks whether or not the specified callService was identified as incompatible in the
279 * context of this call.
280 *
281 * @param callService The call service to evaluate.
282 * @return True upon incompatible call services and false otherwise.
283 */
284 boolean isIncompatibleCallService(CallServiceWrapper callService) {
285 return mIncompatibleCallServices != null &&
286 mIncompatibleCallServices.contains(callService);
287 }
288
289 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800290 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
291 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800292 */
293 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800294 if (mState == CallState.NEW) {
295 if (mCallService != null) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700296 mCallService.abort(this);
Ben Gilad28e8ad62014-03-06 17:01:54 -0800297 }
Ben Gilad9c234112014-03-04 16:07:33 -0800298 clearCallService();
299 clearCallServiceSelector();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800300 }
Santos Cordon681663d2014-01-30 04:32:15 -0800301 }
302
Santos Cordonc195e362014-02-11 17:05:31 -0800303 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700304 * Plays the specified DTMF tone.
305 */
306 void playDtmfTone(char digit) {
307 if (mCallService == null) {
308 Log.w(this, "playDtmfTone() request on a call without a call service.");
309 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700310 Log.i(this, "Send playDtmfTone to call service for call %s", this);
311 mCallService.playDtmfTone(this, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700312 }
313 }
314
315 /**
316 * Stops playing any currently playing DTMF tone.
317 */
318 void stopDtmfTone() {
319 if (mCallService == null) {
320 Log.w(this, "stopDtmfTone() request on a call without a call service.");
321 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700322 Log.i(this, "Send stopDtmfTone to call service for call %s", this);
323 mCallService.stopDtmfTone(this);
Ihab Awad74549ec2014-03-10 15:33:25 -0700324 }
325 }
326
327 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800328 * Attempts to disconnect the call through the call service.
329 */
330 void disconnect() {
331 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800332 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800333 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700334 Log.i(this, "Send disconnect to call service for call: %s", this);
Santos Cordonc195e362014-02-11 17:05:31 -0800335 // The call isn't officially disconnected until the call service confirms that the call
336 // was actually disconnected. Only then is the association between call and call service
337 // severed, see {@link CallsManager#markCallAsDisconnected}.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700338 mCallService.disconnect(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800339 }
340 }
341
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800342 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800343 * Answers the call if it is ringing.
344 */
345 void answer() {
346 Preconditions.checkNotNull(mCallService);
347
348 // Check to verify that the call is still in the ringing state. A call can change states
349 // between the time the user hits 'answer' and Telecomm receives the command.
350 if (isRinging("answer")) {
351 // At this point, we are asking the call service to answer but we don't assume that
352 // it will work. Instead, we wait until confirmation from the call service that the
353 // call is in a non-RINGING state before changing the UI. See
354 // {@link CallServiceAdapter#setActive} and other set* methods.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700355 mCallService.answer(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800356 }
357 }
358
359 /**
360 * Rejects the call if it is ringing.
361 */
362 void reject() {
363 Preconditions.checkNotNull(mCallService);
364
365 // Check to verify that the call is still in the ringing state. A call can change states
366 // between the time the user hits 'reject' and Telecomm receives the command.
367 if (isRinging("reject")) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700368 mCallService.reject(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800369 }
370 }
371
372 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700373 * Puts the call on hold if it is currently active.
374 */
375 void hold() {
376 Preconditions.checkNotNull(mCallService);
377
378 if (mState == CallState.ACTIVE) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700379 mCallService.hold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700380 }
381 }
382
383 /**
384 * Releases the call from hold if it is currently active.
385 */
386 void unhold() {
387 Preconditions.checkNotNull(mCallService);
388
389 if (mState == CallState.ON_HOLD) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700390 mCallService.unhold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700391 }
392 }
393
394 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800395 * @return An object containing read-only information about this call.
396 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700397 CallInfo toCallInfo(String callId) {
398 return new CallInfo(callId, mState, mHandle, mGatewayInfo);
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800399 }
400
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700401 /** Checks if this is a live call or not. */
402 boolean isAlive() {
403 switch (mState) {
404 case NEW:
405 case RINGING:
406 case DISCONNECTED:
407 case ABORTED:
408 return false;
409 default:
410 return true;
411 }
412 }
413
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800414 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800415 * @return True if the call is ringing, else logs the action name.
416 */
417 private boolean isRinging(String actionName) {
418 if (mState == CallState.RINGING) {
419 return true;
420 }
421
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800422 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800423 return false;
424 }
425
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800426 @SuppressWarnings("rawtypes")
427 private void decrementAssociatedCallCount(ServiceBinder binder) {
428 if (binder != null) {
429 binder.decrementAssociatedCallCount();
430 }
431 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800432}