blob: 7eb4a6fc198a0593d9afe1f6b0613259d34c976b [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
Santos Cordon0b03b4b2014-01-29 18:01:59 -080019import android.telecomm.CallInfo;
20import android.telecomm.CallState;
21
Sailesh Nepala439e1b2014-03-11 18:19:58 -070022import com.android.internal.telecomm.ICallServiceSelector;
Ben Gilad61925612014-03-11 19:06:36 -070023import com.google.android.collect.Sets;
Santos Cordon61d0f702014-02-19 02:52:23 -080024import com.google.common.base.Preconditions;
25
Ben Gilad0407fb22014-01-09 16:18:41 -080026import java.util.Date;
Sailesh Nepal91990782014-03-08 17:45:52 -080027import java.util.Locale;
Ben Gilad61925612014-03-11 19:06:36 -070028import java.util.Set;
Santos Cordon61d0f702014-02-19 02:52:23 -080029import java.util.UUID;
Ben Gilad0407fb22014-01-09 16:18:41 -080030
Ben Gilad2495d572014-01-09 17:26:19 -080031/**
32 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
33 * from the time the call intent was received by Telecomm (vs. the time the call was
34 * connected etc).
35 */
Ben Gilad0407fb22014-01-09 16:18:41 -080036final class Call {
Santos Cordon61d0f702014-02-19 02:52:23 -080037 /** Unique identifier for the call as a UUID string. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080038 private final String mId;
39
Ben Gilad0407fb22014-01-09 16:18:41 -080040 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080041 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080042
43 /**
44 * The time this call was created, typically also the time this call was added to the set
45 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
46 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
47 * certain call attempts as failed attempts.
48 */
49 private final Date mCreationTime;
50
Santos Cordon61d0f702014-02-19 02:52:23 -080051 /** The state of the call. */
52 private CallState mState;
53
54 /** The handle with which to establish this call. */
55 private String mHandle;
56
Ben Gilad0407fb22014-01-09 16:18:41 -080057 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080058 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080059 */
Santos Cordonc195e362014-02-11 17:05:31 -080060 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080061
Ben Gilad8e55d1d2014-02-26 16:25:56 -080062 /**
63 * The call-service selector for this call.
64 * TODO(gilad): Switch to using a wrapper object, see {@link #mCallService}.
65 */
66 private ICallServiceSelector mCallServiceSelector;
67
Santos Cordon61d0f702014-02-19 02:52:23 -080068 /** Read-only and parcelable version of this call. */
Santos Cordon0b03b4b2014-01-29 18:01:59 -080069 private CallInfo mCallInfo;
70
71 /**
Ben Gilad61925612014-03-11 19:06:36 -070072 * The set of call services that were attempted in the process of placing/switching this call
73 * but turned out unsuitable. Only used in the context of call switching.
74 */
75 private Set<CallServiceWrapper> mIncompatibleCallServices;
76
77 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080078 * Creates an empty call object with a unique call ID.
79 */
80 Call() {
81 this(null, null);
82 }
83
84 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080085 * Persists the specified parameters and initializes the new instance.
86 *
87 * @param handle The handle to dial.
88 * @param contactInfo Information about the entity being called.
89 */
Ben Gilad0bf5b912014-01-28 17:55:57 -080090 Call(String handle, ContactInfo contactInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080091 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -080092 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080093 mHandle = handle;
94 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080095 mCreationTime = new Date();
96 }
97
Santos Cordon61d0f702014-02-19 02:52:23 -080098 /** {@inheritDoc} */
99 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -0800100 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
101 mCallService.getComponentName(), Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800102 }
103
Ben Gilad0bf5b912014-01-28 17:55:57 -0800104 String getId() {
105 return mId;
106 }
107
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800108 CallState getState() {
109 return mState;
110 }
111
112 /**
113 * Sets the call state. Although there exists the notion of appropriate state transitions
114 * (see {@link CallState}), in practice those expectations break down when cellular systems
115 * misbehave and they do this very often. The result is that we do not enforce state transitions
116 * and instead keep the code resilient to unexpected state changes.
117 */
118 void setState(CallState state) {
119 mState = state;
120 clearCallInfo();
121 }
122
Ben Gilad0bf5b912014-01-28 17:55:57 -0800123 String getHandle() {
124 return mHandle;
125 }
126
Santos Cordon61d0f702014-02-19 02:52:23 -0800127 void setHandle(String handle) {
128 mHandle = handle;
129 }
130
Ben Gilad0bf5b912014-01-28 17:55:57 -0800131 ContactInfo getContactInfo() {
132 return mContactInfo;
133 }
134
Ben Gilad0407fb22014-01-09 16:18:41 -0800135 /**
136 * @return The "age" of this call object in milliseconds, which typically also represents the
137 * period since this call was added to the set pending outgoing calls, see mCreationTime.
138 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800139 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800140 return new Date().getTime() - mCreationTime.getTime();
141 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800142
Yorke Leef98fb572014-03-05 10:56:55 -0800143 /**
144 * @return The time when this call object was created and added to the set of pending outgoing
145 * calls.
146 */
147 long getCreationTimeInMilliseconds() {
148 return mCreationTime.getTime();
149 }
150
Santos Cordonc195e362014-02-11 17:05:31 -0800151 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800152 return mCallService;
153 }
154
Santos Cordonc195e362014-02-11 17:05:31 -0800155 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800156 Preconditions.checkNotNull(callService);
157
Yorke Leeadee12d2014-03-13 12:08:30 -0700158 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800159
160 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800161 mCallService = callService;
162 }
163
164 /**
165 * Clears the associated call service.
166 */
167 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700168 if (mCallService != null) {
169 decrementAssociatedCallCount(mCallService);
170 mCallService.cancelOutgoingCall(getId());
171 mCallService = null;
172 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800173 }
174
175 void setCallServiceSelector(ICallServiceSelector selector) {
176 Preconditions.checkNotNull(selector);
177 mCallServiceSelector = selector;
178 }
179
180 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800181 // TODO(gilad): Un-comment once selectors are converted into wrappers.
182 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800183
184 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800185 }
186
187 /**
Ben Gilad61925612014-03-11 19:06:36 -0700188 * Adds the specified call service to the list of incompatible services. The set is used when
189 * attempting to switch a phone call between call services such that incompatible services can
190 * be avoided.
191 *
192 * @param callService The incompatible call service.
193 */
194 void addIncompatibleCallService(CallServiceWrapper callService) {
195 if (mIncompatibleCallServices == null) {
196 mIncompatibleCallServices = Sets.newHashSet();
197 }
198 mIncompatibleCallServices.add(callService);
199 }
200
201 /**
202 * Checks whether or not the specified callService was identified as incompatible in the
203 * context of this call.
204 *
205 * @param callService The call service to evaluate.
206 * @return True upon incompatible call services and false otherwise.
207 */
208 boolean isIncompatibleCallService(CallServiceWrapper callService) {
209 return mIncompatibleCallServices != null &&
210 mIncompatibleCallServices.contains(callService);
211 }
212
213 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800214 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
215 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800216 */
217 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800218 if (mState == CallState.NEW) {
219 if (mCallService != null) {
220 mCallService.abort(mId);
221 }
Ben Gilad9c234112014-03-04 16:07:33 -0800222 clearCallService();
223 clearCallServiceSelector();
224 mState = CallState.ABORTED;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800225 }
Santos Cordon681663d2014-01-30 04:32:15 -0800226 }
227
Santos Cordonc195e362014-02-11 17:05:31 -0800228 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800229 * Attempts to disconnect the call through the call service.
230 */
231 void disconnect() {
232 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800233 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800234 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800235 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800236 // The call isn't officially disconnected until the call service confirms that the call
237 // was actually disconnected. Only then is the association between call and call service
238 // severed, see {@link CallsManager#markCallAsDisconnected}.
239 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800240 }
241 }
242
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800243 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800244 * Answers the call if it is ringing.
245 */
246 void answer() {
247 Preconditions.checkNotNull(mCallService);
248
249 // Check to verify that the call is still in the ringing state. A call can change states
250 // between the time the user hits 'answer' and Telecomm receives the command.
251 if (isRinging("answer")) {
252 // At this point, we are asking the call service to answer but we don't assume that
253 // it will work. Instead, we wait until confirmation from the call service that the
254 // call is in a non-RINGING state before changing the UI. See
255 // {@link CallServiceAdapter#setActive} and other set* methods.
256 mCallService.answer(mId);
257 }
258 }
259
260 /**
261 * Rejects the call if it is ringing.
262 */
263 void reject() {
264 Preconditions.checkNotNull(mCallService);
265
266 // Check to verify that the call is still in the ringing state. A call can change states
267 // between the time the user hits 'reject' and Telecomm receives the command.
268 if (isRinging("reject")) {
269 mCallService.reject(mId);
270 }
271 }
272
273 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700274 * Puts the call on hold if it is currently active.
275 */
276 void hold() {
277 Preconditions.checkNotNull(mCallService);
278
279 if (mState == CallState.ACTIVE) {
280 mCallService.hold(mId);
281 }
282 }
283
284 /**
285 * Releases the call from hold if it is currently active.
286 */
287 void unhold() {
288 Preconditions.checkNotNull(mCallService);
289
290 if (mState == CallState.ON_HOLD) {
291 mCallService.unhold(mId);
292 }
293 }
294
295 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800296 * @return An object containing read-only information about this call.
297 */
298 CallInfo toCallInfo() {
299 if (mCallInfo == null) {
300 mCallInfo = new CallInfo(mId, mState, mHandle);
301 }
302 return mCallInfo;
303 }
304
305 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800306 * @return True if the call is ringing, else logs the action name.
307 */
308 private boolean isRinging(String actionName) {
309 if (mState == CallState.RINGING) {
310 return true;
311 }
312
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800313 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800314 return false;
315 }
316
317 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800318 * Resets the cached read-only version of this call.
319 */
320 private void clearCallInfo() {
321 mCallInfo = null;
322 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800323
324 @SuppressWarnings("rawtypes")
325 private void decrementAssociatedCallCount(ServiceBinder binder) {
326 if (binder != null) {
327 binder.decrementAssociatedCallCount();
328 }
329 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800330}