blob: 568d7c64b68b5ddd011d35b95b686040d52558d5 [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 Cordon0b03b4b2014-01-29 18:01:59 -080023
Ben Gilad61925612014-03-11 19:06:36 -070024import com.google.android.collect.Sets;
Santos Cordon61d0f702014-02-19 02:52:23 -080025import com.google.common.base.Preconditions;
26
Ben Gilad0407fb22014-01-09 16:18:41 -080027import java.util.Date;
Sailesh Nepal91990782014-03-08 17:45:52 -080028import java.util.Locale;
Ben Gilad61925612014-03-11 19:06:36 -070029import java.util.Set;
Santos Cordon61d0f702014-02-19 02:52:23 -080030import java.util.UUID;
Ben Gilad0407fb22014-01-09 16:18:41 -080031
Ben Gilad2495d572014-01-09 17:26:19 -080032/**
33 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
34 * from the time the call intent was received by Telecomm (vs. the time the call was
35 * connected etc).
36 */
Ben Gilad0407fb22014-01-09 16:18:41 -080037final class Call {
Santos Cordon61d0f702014-02-19 02:52:23 -080038 /** Unique identifier for the call as a UUID string. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080039 private final String mId;
40
Ben Gilad0407fb22014-01-09 16:18:41 -080041 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080042 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080043
Sailesh Nepal810735e2014-03-18 18:15:46 -070044 /** True if this is an incoming call. */
45 private final boolean mIsIncoming;
46
Ben Gilad0407fb22014-01-09 16:18:41 -080047 /**
48 * The time this call was created, typically also the time this call was added to the set
49 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
50 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
51 * certain call attempts as failed attempts.
52 */
53 private final Date mCreationTime;
54
Santos Cordon61d0f702014-02-19 02:52:23 -080055 /** The state of the call. */
56 private CallState mState;
57
58 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070059 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080060
Yorke Lee33501632014-03-17 19:24:12 -070061 /** The gateway information associated with this call. This stores the original call handle
62 * that the user is attempting to connect to via the gateway, the actual handle to dial in
63 * order to connect the call via the gateway, as well as the package name of the gateway
64 * service. */
65 private final GatewayInfo mGatewayInfo;
66
Ben Gilad0407fb22014-01-09 16:18:41 -080067 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080068 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080069 */
Santos Cordonc195e362014-02-11 17:05:31 -080070 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080071
Ben Gilad8e55d1d2014-02-26 16:25:56 -080072 /**
73 * The call-service selector for this call.
Ben Gilad8e55d1d2014-02-26 16:25:56 -080074 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070075 private CallServiceSelectorWrapper mCallServiceSelector;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080076
Santos Cordon0b03b4b2014-01-29 18:01:59 -080077 /**
Ben Gilad61925612014-03-11 19:06:36 -070078 * The set of call services that were attempted in the process of placing/switching this call
79 * but turned out unsuitable. Only used in the context of call switching.
80 */
81 private Set<CallServiceWrapper> mIncompatibleCallServices;
82
83 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080084 * Creates an empty call object with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -070085 *
86 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -080087 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070088 Call(boolean isIncoming) {
Yorke Lee33501632014-03-17 19:24:12 -070089 this(null, null, null, isIncoming);
Santos Cordon493e8f22014-02-19 03:15:12 -080090 }
91
92 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080093 * Persists the specified parameters and initializes the new instance.
94 *
95 * @param handle The handle to dial.
96 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -070097 * @param gatewayInfo Gateway information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -070098 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -080099 */
Yorke Lee33501632014-03-17 19:24:12 -0700100 Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800101 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800102 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -0800103 mHandle = handle;
104 mContactInfo = contactInfo;
Yorke Lee33501632014-03-17 19:24:12 -0700105 mGatewayInfo = gatewayInfo;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700106 mIsIncoming = isIncoming;
Ben Gilad0407fb22014-01-09 16:18:41 -0800107 mCreationTime = new Date();
108 }
109
Santos Cordon61d0f702014-02-19 02:52:23 -0800110 /** {@inheritDoc} */
111 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -0800112 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
Sailesh Nepal810735e2014-03-18 18:15:46 -0700113 mCallService == null ? "<null>" : mCallService.getComponentName(),
114 Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800115 }
116
Ben Gilad0bf5b912014-01-28 17:55:57 -0800117 String getId() {
118 return mId;
119 }
120
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800121 CallState getState() {
122 return mState;
123 }
124
125 /**
126 * Sets the call state. Although there exists the notion of appropriate state transitions
127 * (see {@link CallState}), in practice those expectations break down when cellular systems
128 * misbehave and they do this very often. The result is that we do not enforce state transitions
129 * and instead keep the code resilient to unexpected state changes.
130 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700131 void setState(CallState newState) {
132 if (mState != newState) {
133 Log.v(this, "setState %s -> %s", mState, newState);
134 mState = newState;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800136 }
137
Sailesh Nepalce704b92014-03-17 18:31:43 -0700138 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800139 return mHandle;
140 }
141
Sailesh Nepalce704b92014-03-17 18:31:43 -0700142 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800143 mHandle = handle;
144 }
145
Yorke Lee33501632014-03-17 19:24:12 -0700146 /**
147 * @return The original handle this call is associated with. In-call services should use this
148 * handle when indicating in their UI the handle that is being called.
149 */
150 public Uri getOriginalHandle() {
151 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
152 return mGatewayInfo.getOriginalHandle();
153 }
154 return getHandle();
155 }
156
157 GatewayInfo getGatewayInfo() {
158 return mGatewayInfo;
159 }
160
Ben Gilad0bf5b912014-01-28 17:55:57 -0800161 ContactInfo getContactInfo() {
162 return mContactInfo;
163 }
164
Sailesh Nepal810735e2014-03-18 18:15:46 -0700165 boolean isIncoming() {
166 return mIsIncoming;
167 }
168
Ben Gilad0407fb22014-01-09 16:18:41 -0800169 /**
170 * @return The "age" of this call object in milliseconds, which typically also represents the
171 * period since this call was added to the set pending outgoing calls, see mCreationTime.
172 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800173 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800174 return new Date().getTime() - mCreationTime.getTime();
175 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800176
Yorke Leef98fb572014-03-05 10:56:55 -0800177 /**
178 * @return The time when this call object was created and added to the set of pending outgoing
179 * calls.
180 */
181 long getCreationTimeInMilliseconds() {
182 return mCreationTime.getTime();
183 }
184
Santos Cordonc195e362014-02-11 17:05:31 -0800185 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800186 return mCallService;
187 }
188
Santos Cordonc195e362014-02-11 17:05:31 -0800189 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800190 Preconditions.checkNotNull(callService);
191
Yorke Leeadee12d2014-03-13 12:08:30 -0700192 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800193
194 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800195 mCallService = callService;
196 }
197
198 /**
199 * Clears the associated call service.
200 */
201 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700202 if (mCallService != null) {
203 decrementAssociatedCallCount(mCallService);
204 mCallService.cancelOutgoingCall(getId());
205 mCallService = null;
206 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800207 }
208
Sailesh Nepal18386a82014-03-19 10:22:40 -0700209 void setCallServiceSelector(CallServiceSelectorWrapper selector) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800210 Preconditions.checkNotNull(selector);
211 mCallServiceSelector = selector;
212 }
213
214 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800215 // TODO(gilad): Un-comment once selectors are converted into wrappers.
216 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800217
218 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800219 }
220
221 /**
Ben Gilad61925612014-03-11 19:06:36 -0700222 * Adds the specified call service to the list of incompatible services. The set is used when
223 * attempting to switch a phone call between call services such that incompatible services can
224 * be avoided.
225 *
226 * @param callService The incompatible call service.
227 */
228 void addIncompatibleCallService(CallServiceWrapper callService) {
229 if (mIncompatibleCallServices == null) {
230 mIncompatibleCallServices = Sets.newHashSet();
231 }
232 mIncompatibleCallServices.add(callService);
233 }
234
235 /**
236 * Checks whether or not the specified callService was identified as incompatible in the
237 * context of this call.
238 *
239 * @param callService The call service to evaluate.
240 * @return True upon incompatible call services and false otherwise.
241 */
242 boolean isIncompatibleCallService(CallServiceWrapper callService) {
243 return mIncompatibleCallServices != null &&
244 mIncompatibleCallServices.contains(callService);
245 }
246
247 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800248 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
249 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800250 */
251 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800252 if (mState == CallState.NEW) {
253 if (mCallService != null) {
254 mCallService.abort(mId);
255 }
Ben Gilad9c234112014-03-04 16:07:33 -0800256 clearCallService();
257 clearCallServiceSelector();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800258 }
Santos Cordon681663d2014-01-30 04:32:15 -0800259 }
260
Santos Cordonc195e362014-02-11 17:05:31 -0800261 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800262 * Attempts to disconnect the call through the call service.
263 */
264 void disconnect() {
265 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800266 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800267 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800268 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800269 // The call isn't officially disconnected until the call service confirms that the call
270 // was actually disconnected. Only then is the association between call and call service
271 // severed, see {@link CallsManager#markCallAsDisconnected}.
272 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800273 }
274 }
275
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800276 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800277 * Answers the call if it is ringing.
278 */
279 void answer() {
280 Preconditions.checkNotNull(mCallService);
281
282 // Check to verify that the call is still in the ringing state. A call can change states
283 // between the time the user hits 'answer' and Telecomm receives the command.
284 if (isRinging("answer")) {
285 // At this point, we are asking the call service to answer but we don't assume that
286 // it will work. Instead, we wait until confirmation from the call service that the
287 // call is in a non-RINGING state before changing the UI. See
288 // {@link CallServiceAdapter#setActive} and other set* methods.
289 mCallService.answer(mId);
290 }
291 }
292
293 /**
294 * Rejects the call if it is ringing.
295 */
296 void reject() {
297 Preconditions.checkNotNull(mCallService);
298
299 // Check to verify that the call is still in the ringing state. A call can change states
300 // between the time the user hits 'reject' and Telecomm receives the command.
301 if (isRinging("reject")) {
302 mCallService.reject(mId);
303 }
304 }
305
306 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700307 * Puts the call on hold if it is currently active.
308 */
309 void hold() {
310 Preconditions.checkNotNull(mCallService);
311
312 if (mState == CallState.ACTIVE) {
313 mCallService.hold(mId);
314 }
315 }
316
317 /**
318 * Releases the call from hold if it is currently active.
319 */
320 void unhold() {
321 Preconditions.checkNotNull(mCallService);
322
323 if (mState == CallState.ON_HOLD) {
324 mCallService.unhold(mId);
325 }
326 }
327
328 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800329 * @return An object containing read-only information about this call.
330 */
331 CallInfo toCallInfo() {
Yorke Lee33501632014-03-17 19:24:12 -0700332 return new CallInfo(mId, mState, mHandle, mGatewayInfo);
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800333 }
334
335 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800336 * @return True if the call is ringing, else logs the action name.
337 */
338 private boolean isRinging(String actionName) {
339 if (mState == CallState.RINGING) {
340 return true;
341 }
342
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800343 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800344 return false;
345 }
346
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800347 @SuppressWarnings("rawtypes")
348 private void decrementAssociatedCallCount(ServiceBinder binder) {
349 if (binder != null) {
350 binder.decrementAssociatedCallCount();
351 }
352 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800353}