blob: 57074b720ee18c524eadbdea59b133d56af673f9 [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;
22
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
Sailesh Nepal810735e2014-03-18 18:15:46 -070043 /** True if this is an incoming call. */
44 private final boolean mIsIncoming;
45
Ben Gilad0407fb22014-01-09 16:18:41 -080046 /**
47 * The time this call was created, typically also the time this call was added to the set
48 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
49 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
50 * certain call attempts as failed attempts.
51 */
52 private final Date mCreationTime;
53
Santos Cordon61d0f702014-02-19 02:52:23 -080054 /** The state of the call. */
55 private CallState mState;
56
57 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070058 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080059
Ben Gilad0407fb22014-01-09 16:18:41 -080060 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080061 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080062 */
Santos Cordonc195e362014-02-11 17:05:31 -080063 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080064
Ben Gilad8e55d1d2014-02-26 16:25:56 -080065 /**
66 * The call-service selector for this call.
Ben Gilad8e55d1d2014-02-26 16:25:56 -080067 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070068 private CallServiceSelectorWrapper mCallServiceSelector;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080069
Santos Cordon61d0f702014-02-19 02:52:23 -080070 /** Read-only and parcelable version of this call. */
Santos Cordon0b03b4b2014-01-29 18:01:59 -080071 private CallInfo mCallInfo;
72
73 /**
Ben Gilad61925612014-03-11 19:06:36 -070074 * The set of call services that were attempted in the process of placing/switching this call
75 * but turned out unsuitable. Only used in the context of call switching.
76 */
77 private Set<CallServiceWrapper> mIncompatibleCallServices;
78
79 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080080 * Creates an empty call object with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -070081 *
82 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -080083 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070084 Call(boolean isIncoming) {
85 this(null, null, isIncoming);
Santos Cordon493e8f22014-02-19 03:15:12 -080086 }
87
88 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080089 * Persists the specified parameters and initializes the new instance.
90 *
91 * @param handle The handle to dial.
92 * @param contactInfo Information about the entity being called.
Sailesh Nepal810735e2014-03-18 18:15:46 -070093 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -080094 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070095 Call(Uri handle, ContactInfo contactInfo, boolean isIncoming) {
Santos Cordon61d0f702014-02-19 02:52:23 -080096 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -080097 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080098 mHandle = handle;
99 mContactInfo = contactInfo;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700100 mIsIncoming = isIncoming;
Ben Gilad0407fb22014-01-09 16:18:41 -0800101 mCreationTime = new Date();
102 }
103
Santos Cordon61d0f702014-02-19 02:52:23 -0800104 /** {@inheritDoc} */
105 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -0800106 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
Sailesh Nepal810735e2014-03-18 18:15:46 -0700107 mCallService == null ? "<null>" : mCallService.getComponentName(),
108 Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800109 }
110
Ben Gilad0bf5b912014-01-28 17:55:57 -0800111 String getId() {
112 return mId;
113 }
114
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800115 CallState getState() {
116 return mState;
117 }
118
119 /**
120 * Sets the call state. Although there exists the notion of appropriate state transitions
121 * (see {@link CallState}), in practice those expectations break down when cellular systems
122 * misbehave and they do this very often. The result is that we do not enforce state transitions
123 * and instead keep the code resilient to unexpected state changes.
124 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700125 void setState(CallState newState) {
126 if (mState != newState) {
127 Log.v(this, "setState %s -> %s", mState, newState);
128 mState = newState;
129 clearCallInfo();
130 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800131 }
132
Sailesh Nepalce704b92014-03-17 18:31:43 -0700133 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800134 return mHandle;
135 }
136
Sailesh Nepalce704b92014-03-17 18:31:43 -0700137 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800138 mHandle = handle;
139 }
140
Ben Gilad0bf5b912014-01-28 17:55:57 -0800141 ContactInfo getContactInfo() {
142 return mContactInfo;
143 }
144
Sailesh Nepal810735e2014-03-18 18:15:46 -0700145 boolean isIncoming() {
146 return mIsIncoming;
147 }
148
Ben Gilad0407fb22014-01-09 16:18:41 -0800149 /**
150 * @return The "age" of this call object in milliseconds, which typically also represents the
151 * period since this call was added to the set pending outgoing calls, see mCreationTime.
152 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800153 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800154 return new Date().getTime() - mCreationTime.getTime();
155 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800156
Yorke Leef98fb572014-03-05 10:56:55 -0800157 /**
158 * @return The time when this call object was created and added to the set of pending outgoing
159 * calls.
160 */
161 long getCreationTimeInMilliseconds() {
162 return mCreationTime.getTime();
163 }
164
Santos Cordonc195e362014-02-11 17:05:31 -0800165 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800166 return mCallService;
167 }
168
Santos Cordonc195e362014-02-11 17:05:31 -0800169 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800170 Preconditions.checkNotNull(callService);
171
Yorke Leeadee12d2014-03-13 12:08:30 -0700172 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800173
174 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800175 mCallService = callService;
176 }
177
178 /**
179 * Clears the associated call service.
180 */
181 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700182 if (mCallService != null) {
183 decrementAssociatedCallCount(mCallService);
184 mCallService.cancelOutgoingCall(getId());
185 mCallService = null;
186 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800187 }
188
Sailesh Nepal18386a82014-03-19 10:22:40 -0700189 void setCallServiceSelector(CallServiceSelectorWrapper selector) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800190 Preconditions.checkNotNull(selector);
191 mCallServiceSelector = selector;
192 }
193
194 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800195 // TODO(gilad): Un-comment once selectors are converted into wrappers.
196 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800197
198 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800199 }
200
201 /**
Ben Gilad61925612014-03-11 19:06:36 -0700202 * Adds the specified call service to the list of incompatible services. The set is used when
203 * attempting to switch a phone call between call services such that incompatible services can
204 * be avoided.
205 *
206 * @param callService The incompatible call service.
207 */
208 void addIncompatibleCallService(CallServiceWrapper callService) {
209 if (mIncompatibleCallServices == null) {
210 mIncompatibleCallServices = Sets.newHashSet();
211 }
212 mIncompatibleCallServices.add(callService);
213 }
214
215 /**
216 * Checks whether or not the specified callService was identified as incompatible in the
217 * context of this call.
218 *
219 * @param callService The call service to evaluate.
220 * @return True upon incompatible call services and false otherwise.
221 */
222 boolean isIncompatibleCallService(CallServiceWrapper callService) {
223 return mIncompatibleCallServices != null &&
224 mIncompatibleCallServices.contains(callService);
225 }
226
227 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800228 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
229 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800230 */
231 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800232 if (mState == CallState.NEW) {
233 if (mCallService != null) {
234 mCallService.abort(mId);
235 }
Ben Gilad9c234112014-03-04 16:07:33 -0800236 clearCallService();
237 clearCallServiceSelector();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800238 }
Santos Cordon681663d2014-01-30 04:32:15 -0800239 }
240
Santos Cordonc195e362014-02-11 17:05:31 -0800241 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800242 * Attempts to disconnect the call through the call service.
243 */
244 void disconnect() {
245 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800246 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800247 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800248 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800249 // The call isn't officially disconnected until the call service confirms that the call
250 // was actually disconnected. Only then is the association between call and call service
251 // severed, see {@link CallsManager#markCallAsDisconnected}.
252 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800253 }
254 }
255
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800256 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800257 * Answers the call if it is ringing.
258 */
259 void answer() {
260 Preconditions.checkNotNull(mCallService);
261
262 // Check to verify that the call is still in the ringing state. A call can change states
263 // between the time the user hits 'answer' and Telecomm receives the command.
264 if (isRinging("answer")) {
265 // At this point, we are asking the call service to answer but we don't assume that
266 // it will work. Instead, we wait until confirmation from the call service that the
267 // call is in a non-RINGING state before changing the UI. See
268 // {@link CallServiceAdapter#setActive} and other set* methods.
269 mCallService.answer(mId);
270 }
271 }
272
273 /**
274 * Rejects the call if it is ringing.
275 */
276 void reject() {
277 Preconditions.checkNotNull(mCallService);
278
279 // Check to verify that the call is still in the ringing state. A call can change states
280 // between the time the user hits 'reject' and Telecomm receives the command.
281 if (isRinging("reject")) {
282 mCallService.reject(mId);
283 }
284 }
285
286 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700287 * Puts the call on hold if it is currently active.
288 */
289 void hold() {
290 Preconditions.checkNotNull(mCallService);
291
292 if (mState == CallState.ACTIVE) {
293 mCallService.hold(mId);
294 }
295 }
296
297 /**
298 * Releases the call from hold if it is currently active.
299 */
300 void unhold() {
301 Preconditions.checkNotNull(mCallService);
302
303 if (mState == CallState.ON_HOLD) {
304 mCallService.unhold(mId);
305 }
306 }
307
308 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800309 * @return An object containing read-only information about this call.
310 */
311 CallInfo toCallInfo() {
312 if (mCallInfo == null) {
313 mCallInfo = new CallInfo(mId, mState, mHandle);
314 }
315 return mCallInfo;
316 }
317
318 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800319 * @return True if the call is ringing, else logs the action name.
320 */
321 private boolean isRinging(String actionName) {
322 if (mState == CallState.RINGING) {
323 return true;
324 }
325
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800326 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800327 return false;
328 }
329
330 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800331 * Resets the cached read-only version of this call.
332 */
333 private void clearCallInfo() {
334 mCallInfo = null;
335 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800336
337 @SuppressWarnings("rawtypes")
338 private void decrementAssociatedCallCount(ServiceBinder binder) {
339 if (binder != null) {
340 binder.decrementAssociatedCallCount();
341 }
342 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800343}