blob: 3e4895b7f98970248846f8040fede2422f1f9537 [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;
Santos Cordon61d0f702014-02-19 02:52:23 -080032import java.util.UUID;
Ben Gilad0407fb22014-01-09 16:18:41 -080033
Ben Gilad2495d572014-01-09 17:26:19 -080034/**
35 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
36 * from the time the call intent was received by Telecomm (vs. the time the call was
37 * connected etc).
38 */
Ben Gilad0407fb22014-01-09 16:18:41 -080039final class Call {
Santos Cordon61d0f702014-02-19 02:52:23 -080040 /** Unique identifier for the call as a UUID string. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080041 private final String mId;
42
Ben Gilad0407fb22014-01-09 16:18:41 -080043 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080044 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080045
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 /** True if this is an incoming call. */
47 private final boolean mIsIncoming;
48
Ben Gilad0407fb22014-01-09 16:18:41 -080049 /**
50 * The time this call was created, typically also the time this call was added to the set
51 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
52 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
53 * certain call attempts as failed attempts.
54 */
55 private final Date mCreationTime;
56
Santos Cordon61d0f702014-02-19 02:52:23 -080057 /** The state of the call. */
58 private CallState mState;
59
60 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070061 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080062
Yorke Lee33501632014-03-17 19:24:12 -070063 /** The gateway information associated with this call. This stores the original call handle
64 * that the user is attempting to connect to via the gateway, the actual handle to dial in
65 * order to connect the call via the gateway, as well as the package name of the gateway
66 * service. */
67 private final GatewayInfo mGatewayInfo;
68
Ben Gilad0407fb22014-01-09 16:18:41 -080069 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080070 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080071 */
Santos Cordonc195e362014-02-11 17:05:31 -080072 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080073
Ben Gilad8e55d1d2014-02-26 16:25:56 -080074 /**
75 * The call-service selector for this call.
Ben Gilad8e55d1d2014-02-26 16:25:56 -080076 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070077 private CallServiceSelectorWrapper mCallServiceSelector;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080078
Santos Cordon0b03b4b2014-01-29 18:01:59 -080079 /**
Ben Gilad61925612014-03-11 19:06:36 -070080 * The set of call services that were attempted in the process of placing/switching this call
81 * but turned out unsuitable. Only used in the context of call switching.
82 */
83 private Set<CallServiceWrapper> mIncompatibleCallServices;
84
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070085 private boolean mIsEmergencyCall;
86
Ben Gilad61925612014-03-11 19:06:36 -070087 /**
Santos Cordon79ff2bc2014-03-27 15:31:27 -070088 * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED.
89 * See {@link android.telephony.DisconnectCause}.
90 */
91 private int mDisconnectCause;
92
93 /**
94 * Additional disconnect information provided by the call service.
95 */
96 private String mDisconnectMessage;
97
98 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080099 * Creates an empty call object with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700100 *
101 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -0800102 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700103 Call(boolean isIncoming) {
Yorke Lee33501632014-03-17 19:24:12 -0700104 this(null, null, null, isIncoming);
Santos Cordon493e8f22014-02-19 03:15:12 -0800105 }
106
107 /**
Ben Gilad0407fb22014-01-09 16:18:41 -0800108 * Persists the specified parameters and initializes the new instance.
109 *
110 * @param handle The handle to dial.
111 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700112 * @param gatewayInfo Gateway information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700113 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800114 */
Yorke Lee33501632014-03-17 19:24:12 -0700115 Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800116 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800117 mState = CallState.NEW;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700118 setHandle(handle);
Ben Gilad0407fb22014-01-09 16:18:41 -0800119 mContactInfo = contactInfo;
Yorke Lee33501632014-03-17 19:24:12 -0700120 mGatewayInfo = gatewayInfo;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700121 mIsIncoming = isIncoming;
Ben Gilad0407fb22014-01-09 16:18:41 -0800122 mCreationTime = new Date();
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700123 mDisconnectCause = DisconnectCause.NOT_VALID;
Ben Gilad0407fb22014-01-09 16:18:41 -0800124 }
125
Santos Cordon61d0f702014-02-19 02:52:23 -0800126 /** {@inheritDoc} */
127 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -0800128 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
Sailesh Nepal810735e2014-03-18 18:15:46 -0700129 mCallService == null ? "<null>" : mCallService.getComponentName(),
130 Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800131 }
132
Ben Gilad0bf5b912014-01-28 17:55:57 -0800133 String getId() {
134 return mId;
135 }
136
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800137 CallState getState() {
138 return mState;
139 }
140
141 /**
142 * Sets the call state. Although there exists the notion of appropriate state transitions
143 * (see {@link CallState}), in practice those expectations break down when cellular systems
144 * misbehave and they do this very often. The result is that we do not enforce state transitions
145 * and instead keep the code resilient to unexpected state changes.
146 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700147 void setState(CallState newState) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700148 Preconditions.checkState(newState != CallState.DISCONNECTED ||
149 mDisconnectCause != DisconnectCause.NOT_VALID);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700150 if (mState != newState) {
151 Log.v(this, "setState %s -> %s", mState, newState);
152 mState = newState;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700153 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800154 }
155
Sailesh Nepalce704b92014-03-17 18:31:43 -0700156 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800157 return mHandle;
158 }
159
Sailesh Nepalce704b92014-03-17 18:31:43 -0700160 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800161 mHandle = handle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700162 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
163 mHandle.getSchemeSpecificPart(), TelecommApp.getInstance());
164 }
165
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700166 /**
167 * @param disconnectCause The reason for the disconnection, any of
168 * {@link android.telephony.DisconnectCause}.
169 * @param disconnectMessage Optional call-service-provided message about the disconnect.
170 */
171 void setDisconnectCause(int disconnectCause, String disconnectMessage) {
172 // TODO: Consider combining this method with a setDisconnected() method that is totally
173 // separate from setState.
174 mDisconnectCause = disconnectCause;
175 mDisconnectMessage = disconnectMessage;
176 }
177
178 int getDisconnectCause() {
179 return mDisconnectCause;
180 }
181
182 String getDisconnectMessage() {
183 return mDisconnectMessage;
184 }
185
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700186 boolean isEmergencyCall() {
187 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800188 }
189
Yorke Lee33501632014-03-17 19:24:12 -0700190 /**
191 * @return The original handle this call is associated with. In-call services should use this
192 * handle when indicating in their UI the handle that is being called.
193 */
194 public Uri getOriginalHandle() {
195 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
196 return mGatewayInfo.getOriginalHandle();
197 }
198 return getHandle();
199 }
200
201 GatewayInfo getGatewayInfo() {
202 return mGatewayInfo;
203 }
204
Ben Gilad0bf5b912014-01-28 17:55:57 -0800205 ContactInfo getContactInfo() {
206 return mContactInfo;
207 }
208
Sailesh Nepal810735e2014-03-18 18:15:46 -0700209 boolean isIncoming() {
210 return mIsIncoming;
211 }
212
Ben Gilad0407fb22014-01-09 16:18:41 -0800213 /**
214 * @return The "age" of this call object in milliseconds, which typically also represents the
215 * period since this call was added to the set pending outgoing calls, see mCreationTime.
216 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800217 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800218 return new Date().getTime() - mCreationTime.getTime();
219 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800220
Yorke Leef98fb572014-03-05 10:56:55 -0800221 /**
222 * @return The time when this call object was created and added to the set of pending outgoing
223 * calls.
224 */
225 long getCreationTimeInMilliseconds() {
226 return mCreationTime.getTime();
227 }
228
Santos Cordonc195e362014-02-11 17:05:31 -0800229 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800230 return mCallService;
231 }
232
Santos Cordonc195e362014-02-11 17:05:31 -0800233 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800234 Preconditions.checkNotNull(callService);
235
Yorke Leeadee12d2014-03-13 12:08:30 -0700236 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800237
238 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800239 mCallService = callService;
240 }
241
242 /**
243 * Clears the associated call service.
244 */
245 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700246 if (mCallService != null) {
247 decrementAssociatedCallCount(mCallService);
248 mCallService.cancelOutgoingCall(getId());
249 mCallService = null;
250 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800251 }
252
Sailesh Nepal18386a82014-03-19 10:22:40 -0700253 void setCallServiceSelector(CallServiceSelectorWrapper selector) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800254 Preconditions.checkNotNull(selector);
255 mCallServiceSelector = selector;
256 }
257
258 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800259 // TODO(gilad): Un-comment once selectors are converted into wrappers.
260 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800261
262 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800263 }
264
265 /**
Ben Gilad61925612014-03-11 19:06:36 -0700266 * Adds the specified call service to the list of incompatible services. The set is used when
267 * attempting to switch a phone call between call services such that incompatible services can
268 * be avoided.
269 *
270 * @param callService The incompatible call service.
271 */
272 void addIncompatibleCallService(CallServiceWrapper callService) {
273 if (mIncompatibleCallServices == null) {
274 mIncompatibleCallServices = Sets.newHashSet();
275 }
276 mIncompatibleCallServices.add(callService);
277 }
278
279 /**
280 * Checks whether or not the specified callService was identified as incompatible in the
281 * context of this call.
282 *
283 * @param callService The call service to evaluate.
284 * @return True upon incompatible call services and false otherwise.
285 */
286 boolean isIncompatibleCallService(CallServiceWrapper callService) {
287 return mIncompatibleCallServices != null &&
288 mIncompatibleCallServices.contains(callService);
289 }
290
291 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800292 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
293 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800294 */
295 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800296 if (mState == CallState.NEW) {
297 if (mCallService != null) {
298 mCallService.abort(mId);
299 }
Ben Gilad9c234112014-03-04 16:07:33 -0800300 clearCallService();
301 clearCallServiceSelector();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800302 }
Santos Cordon681663d2014-01-30 04:32:15 -0800303 }
304
Santos Cordonc195e362014-02-11 17:05:31 -0800305 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700306 * Plays the specified DTMF tone.
307 */
308 void playDtmfTone(char digit) {
309 if (mCallService == null) {
310 Log.w(this, "playDtmfTone() request on a call without a call service.");
311 } else {
312 Log.i(this, "Send playDtmfTone to call service for call with id %s", mId);
313 mCallService.playDtmfTone(mId, digit);
314 }
315 }
316
317 /**
318 * Stops playing any currently playing DTMF tone.
319 */
320 void stopDtmfTone() {
321 if (mCallService == null) {
322 Log.w(this, "stopDtmfTone() request on a call without a call service.");
323 } else {
324 Log.i(this, "Send stopDtmfTone to call service for call with id %s", mId);
325 mCallService.stopDtmfTone(mId);
326 }
327 }
328
329 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800330 * Attempts to disconnect the call through the call service.
331 */
332 void disconnect() {
333 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800334 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800335 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800336 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800337 // The call isn't officially disconnected until the call service confirms that the call
338 // was actually disconnected. Only then is the association between call and call service
339 // severed, see {@link CallsManager#markCallAsDisconnected}.
340 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800341 }
342 }
343
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800344 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800345 * Answers the call if it is ringing.
346 */
347 void answer() {
348 Preconditions.checkNotNull(mCallService);
349
350 // Check to verify that the call is still in the ringing state. A call can change states
351 // between the time the user hits 'answer' and Telecomm receives the command.
352 if (isRinging("answer")) {
353 // At this point, we are asking the call service to answer but we don't assume that
354 // it will work. Instead, we wait until confirmation from the call service that the
355 // call is in a non-RINGING state before changing the UI. See
356 // {@link CallServiceAdapter#setActive} and other set* methods.
357 mCallService.answer(mId);
358 }
359 }
360
361 /**
362 * Rejects the call if it is ringing.
363 */
364 void reject() {
365 Preconditions.checkNotNull(mCallService);
366
367 // Check to verify that the call is still in the ringing state. A call can change states
368 // between the time the user hits 'reject' and Telecomm receives the command.
369 if (isRinging("reject")) {
370 mCallService.reject(mId);
371 }
372 }
373
374 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700375 * Puts the call on hold if it is currently active.
376 */
377 void hold() {
378 Preconditions.checkNotNull(mCallService);
379
380 if (mState == CallState.ACTIVE) {
381 mCallService.hold(mId);
382 }
383 }
384
385 /**
386 * Releases the call from hold if it is currently active.
387 */
388 void unhold() {
389 Preconditions.checkNotNull(mCallService);
390
391 if (mState == CallState.ON_HOLD) {
392 mCallService.unhold(mId);
393 }
394 }
395
396 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800397 * @return An object containing read-only information about this call.
398 */
399 CallInfo toCallInfo() {
Yorke Lee33501632014-03-17 19:24:12 -0700400 return new CallInfo(mId, mState, mHandle, mGatewayInfo);
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800401 }
402
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700403 /** Checks if this is a live call or not. */
404 boolean isAlive() {
405 switch (mState) {
406 case NEW:
407 case RINGING:
408 case DISCONNECTED:
409 case ABORTED:
410 return false;
411 default:
412 return true;
413 }
414 }
415
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800416 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800417 * @return True if the call is ringing, else logs the action name.
418 */
419 private boolean isRinging(String actionName) {
420 if (mState == CallState.RINGING) {
421 return true;
422 }
423
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800424 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800425 return false;
426 }
427
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800428 @SuppressWarnings("rawtypes")
429 private void decrementAssociatedCallCount(ServiceBinder binder) {
430 if (binder != null) {
431 binder.decrementAssociatedCallCount();
432 }
433 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800434}