blob: 07891359a4dc9b952342e6118a3d59fc361f1e1c [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;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070023import android.telephony.PhoneNumberUtils;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080024
Ben Gilad61925612014-03-11 19:06:36 -070025import com.google.android.collect.Sets;
Santos Cordon61d0f702014-02-19 02:52:23 -080026import com.google.common.base.Preconditions;
27
Ben Gilad0407fb22014-01-09 16:18:41 -080028import java.util.Date;
Sailesh Nepal91990782014-03-08 17:45:52 -080029import java.util.Locale;
Ben Gilad61925612014-03-11 19:06:36 -070030import java.util.Set;
Santos Cordon61d0f702014-02-19 02:52:23 -080031import java.util.UUID;
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 {
Santos Cordon61d0f702014-02-19 02:52:23 -080039 /** Unique identifier for the call as a UUID string. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080040 private final String mId;
41
Ben Gilad0407fb22014-01-09 16:18:41 -080042 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080043 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080044
Sailesh Nepal810735e2014-03-18 18:15:46 -070045 /** True if this is an incoming call. */
46 private final boolean mIsIncoming;
47
Ben Gilad0407fb22014-01-09 16:18:41 -080048 /**
49 * The time this call was created, typically also the time this call was added to the set
50 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
51 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
52 * certain call attempts as failed attempts.
53 */
54 private final Date mCreationTime;
55
Santos Cordon61d0f702014-02-19 02:52:23 -080056 /** The state of the call. */
57 private CallState mState;
58
59 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070060 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080061
Yorke Lee33501632014-03-17 19:24:12 -070062 /** The gateway information associated with this call. This stores the original call handle
63 * that the user is attempting to connect to via the gateway, the actual handle to dial in
64 * order to connect the call via the gateway, as well as the package name of the gateway
65 * service. */
66 private final GatewayInfo mGatewayInfo;
67
Ben Gilad0407fb22014-01-09 16:18:41 -080068 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080069 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080070 */
Santos Cordonc195e362014-02-11 17:05:31 -080071 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080072
Ben Gilad8e55d1d2014-02-26 16:25:56 -080073 /**
74 * The call-service selector for this call.
Ben Gilad8e55d1d2014-02-26 16:25:56 -080075 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070076 private CallServiceSelectorWrapper mCallServiceSelector;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080077
Santos Cordon0b03b4b2014-01-29 18:01:59 -080078 /**
Ben Gilad61925612014-03-11 19:06:36 -070079 * The set of call services that were attempted in the process of placing/switching this call
80 * but turned out unsuitable. Only used in the context of call switching.
81 */
82 private Set<CallServiceWrapper> mIncompatibleCallServices;
83
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070084 private boolean mIsEmergencyCall;
85
Ben Gilad61925612014-03-11 19:06:36 -070086 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080087 * Creates an empty call object with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -070088 *
89 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -080090 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070091 Call(boolean isIncoming) {
Yorke Lee33501632014-03-17 19:24:12 -070092 this(null, null, null, isIncoming);
Santos Cordon493e8f22014-02-19 03:15:12 -080093 }
94
95 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080096 * Persists the specified parameters and initializes the new instance.
97 *
98 * @param handle The handle to dial.
99 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700100 * @param gatewayInfo Gateway information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700101 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800102 */
Yorke Lee33501632014-03-17 19:24:12 -0700103 Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800104 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800105 mState = CallState.NEW;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700106 setHandle(handle);
Ben Gilad0407fb22014-01-09 16:18:41 -0800107 mContactInfo = contactInfo;
Yorke Lee33501632014-03-17 19:24:12 -0700108 mGatewayInfo = gatewayInfo;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700109 mIsIncoming = isIncoming;
Ben Gilad0407fb22014-01-09 16:18:41 -0800110 mCreationTime = new Date();
111 }
112
Santos Cordon61d0f702014-02-19 02:52:23 -0800113 /** {@inheritDoc} */
114 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -0800115 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
Sailesh Nepal810735e2014-03-18 18:15:46 -0700116 mCallService == null ? "<null>" : mCallService.getComponentName(),
117 Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800118 }
119
Ben Gilad0bf5b912014-01-28 17:55:57 -0800120 String getId() {
121 return mId;
122 }
123
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800124 CallState getState() {
125 return mState;
126 }
127
128 /**
129 * Sets the call state. Although there exists the notion of appropriate state transitions
130 * (see {@link CallState}), in practice those expectations break down when cellular systems
131 * misbehave and they do this very often. The result is that we do not enforce state transitions
132 * and instead keep the code resilient to unexpected state changes.
133 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700134 void setState(CallState newState) {
135 if (mState != newState) {
136 Log.v(this, "setState %s -> %s", mState, newState);
137 mState = newState;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800139 }
140
Sailesh Nepalce704b92014-03-17 18:31:43 -0700141 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800142 return mHandle;
143 }
144
Sailesh Nepalce704b92014-03-17 18:31:43 -0700145 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800146 mHandle = handle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700147 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
148 mHandle.getSchemeSpecificPart(), TelecommApp.getInstance());
149 }
150
151 boolean isEmergencyCall() {
152 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800153 }
154
Yorke Lee33501632014-03-17 19:24:12 -0700155 /**
156 * @return The original handle this call is associated with. In-call services should use this
157 * handle when indicating in their UI the handle that is being called.
158 */
159 public Uri getOriginalHandle() {
160 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
161 return mGatewayInfo.getOriginalHandle();
162 }
163 return getHandle();
164 }
165
166 GatewayInfo getGatewayInfo() {
167 return mGatewayInfo;
168 }
169
Ben Gilad0bf5b912014-01-28 17:55:57 -0800170 ContactInfo getContactInfo() {
171 return mContactInfo;
172 }
173
Sailesh Nepal810735e2014-03-18 18:15:46 -0700174 boolean isIncoming() {
175 return mIsIncoming;
176 }
177
Ben Gilad0407fb22014-01-09 16:18:41 -0800178 /**
179 * @return The "age" of this call object in milliseconds, which typically also represents the
180 * period since this call was added to the set pending outgoing calls, see mCreationTime.
181 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800182 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800183 return new Date().getTime() - mCreationTime.getTime();
184 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800185
Yorke Leef98fb572014-03-05 10:56:55 -0800186 /**
187 * @return The time when this call object was created and added to the set of pending outgoing
188 * calls.
189 */
190 long getCreationTimeInMilliseconds() {
191 return mCreationTime.getTime();
192 }
193
Santos Cordonc195e362014-02-11 17:05:31 -0800194 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800195 return mCallService;
196 }
197
Santos Cordonc195e362014-02-11 17:05:31 -0800198 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800199 Preconditions.checkNotNull(callService);
200
Yorke Leeadee12d2014-03-13 12:08:30 -0700201 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800202
203 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800204 mCallService = callService;
205 }
206
207 /**
208 * Clears the associated call service.
209 */
210 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700211 if (mCallService != null) {
212 decrementAssociatedCallCount(mCallService);
213 mCallService.cancelOutgoingCall(getId());
214 mCallService = null;
215 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800216 }
217
Sailesh Nepal18386a82014-03-19 10:22:40 -0700218 void setCallServiceSelector(CallServiceSelectorWrapper selector) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800219 Preconditions.checkNotNull(selector);
220 mCallServiceSelector = selector;
221 }
222
223 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800224 // TODO(gilad): Un-comment once selectors are converted into wrappers.
225 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800226
227 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800228 }
229
230 /**
Ben Gilad61925612014-03-11 19:06:36 -0700231 * Adds the specified call service to the list of incompatible services. The set is used when
232 * attempting to switch a phone call between call services such that incompatible services can
233 * be avoided.
234 *
235 * @param callService The incompatible call service.
236 */
237 void addIncompatibleCallService(CallServiceWrapper callService) {
238 if (mIncompatibleCallServices == null) {
239 mIncompatibleCallServices = Sets.newHashSet();
240 }
241 mIncompatibleCallServices.add(callService);
242 }
243
244 /**
245 * Checks whether or not the specified callService was identified as incompatible in the
246 * context of this call.
247 *
248 * @param callService The call service to evaluate.
249 * @return True upon incompatible call services and false otherwise.
250 */
251 boolean isIncompatibleCallService(CallServiceWrapper callService) {
252 return mIncompatibleCallServices != null &&
253 mIncompatibleCallServices.contains(callService);
254 }
255
256 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800257 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
258 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800259 */
260 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800261 if (mState == CallState.NEW) {
262 if (mCallService != null) {
263 mCallService.abort(mId);
264 }
Ben Gilad9c234112014-03-04 16:07:33 -0800265 clearCallService();
266 clearCallServiceSelector();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800267 }
Santos Cordon681663d2014-01-30 04:32:15 -0800268 }
269
Santos Cordonc195e362014-02-11 17:05:31 -0800270 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800271 * Attempts to disconnect the call through the call service.
272 */
273 void disconnect() {
274 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800275 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800276 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800277 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800278 // The call isn't officially disconnected until the call service confirms that the call
279 // was actually disconnected. Only then is the association between call and call service
280 // severed, see {@link CallsManager#markCallAsDisconnected}.
281 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800282 }
283 }
284
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800285 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800286 * Answers the call if it is ringing.
287 */
288 void answer() {
289 Preconditions.checkNotNull(mCallService);
290
291 // Check to verify that the call is still in the ringing state. A call can change states
292 // between the time the user hits 'answer' and Telecomm receives the command.
293 if (isRinging("answer")) {
294 // At this point, we are asking the call service to answer but we don't assume that
295 // it will work. Instead, we wait until confirmation from the call service that the
296 // call is in a non-RINGING state before changing the UI. See
297 // {@link CallServiceAdapter#setActive} and other set* methods.
298 mCallService.answer(mId);
299 }
300 }
301
302 /**
303 * Rejects the call if it is ringing.
304 */
305 void reject() {
306 Preconditions.checkNotNull(mCallService);
307
308 // Check to verify that the call is still in the ringing state. A call can change states
309 // between the time the user hits 'reject' and Telecomm receives the command.
310 if (isRinging("reject")) {
311 mCallService.reject(mId);
312 }
313 }
314
315 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700316 * Puts the call on hold if it is currently active.
317 */
318 void hold() {
319 Preconditions.checkNotNull(mCallService);
320
321 if (mState == CallState.ACTIVE) {
322 mCallService.hold(mId);
323 }
324 }
325
326 /**
327 * Releases the call from hold if it is currently active.
328 */
329 void unhold() {
330 Preconditions.checkNotNull(mCallService);
331
332 if (mState == CallState.ON_HOLD) {
333 mCallService.unhold(mId);
334 }
335 }
336
337 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800338 * @return An object containing read-only information about this call.
339 */
340 CallInfo toCallInfo() {
Yorke Lee33501632014-03-17 19:24:12 -0700341 return new CallInfo(mId, mState, mHandle, mGatewayInfo);
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800342 }
343
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700344 /** Checks if this is a live call or not. */
345 boolean isAlive() {
346 switch (mState) {
347 case NEW:
348 case RINGING:
349 case DISCONNECTED:
350 case ABORTED:
351 return false;
352 default:
353 return true;
354 }
355 }
356
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800357 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800358 * @return True if the call is ringing, else logs the action name.
359 */
360 private boolean isRinging(String actionName) {
361 if (mState == CallState.RINGING) {
362 return true;
363 }
364
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800365 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800366 return false;
367 }
368
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800369 @SuppressWarnings("rawtypes")
370 private void decrementAssociatedCallCount(ServiceBinder binder) {
371 if (binder != null) {
372 binder.decrementAssociatedCallCount();
373 }
374 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800375}