blob: 08a50ab0a70522757c107fbf1e30066855e80340 [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
Sailesh Nepala439e1b2014-03-11 18:19:58 -070023import com.android.internal.telecomm.ICallServiceSelector;
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
44 /**
45 * The time this call was created, typically also the time this call was added to the set
46 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
47 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
48 * certain call attempts as failed attempts.
49 */
50 private final Date mCreationTime;
51
Santos Cordon61d0f702014-02-19 02:52:23 -080052 /** The state of the call. */
53 private CallState mState;
54
55 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -070056 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -080057
Ben Gilad0407fb22014-01-09 16:18:41 -080058 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080059 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080060 */
Santos Cordonc195e362014-02-11 17:05:31 -080061 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080062
Ben Gilad8e55d1d2014-02-26 16:25:56 -080063 /**
64 * The call-service selector for this call.
65 * TODO(gilad): Switch to using a wrapper object, see {@link #mCallService}.
66 */
67 private ICallServiceSelector mCallServiceSelector;
68
Santos Cordon61d0f702014-02-19 02:52:23 -080069 /** Read-only and parcelable version of this call. */
Santos Cordon0b03b4b2014-01-29 18:01:59 -080070 private CallInfo mCallInfo;
71
72 /**
Ben Gilad61925612014-03-11 19:06:36 -070073 * The set of call services that were attempted in the process of placing/switching this call
74 * but turned out unsuitable. Only used in the context of call switching.
75 */
76 private Set<CallServiceWrapper> mIncompatibleCallServices;
77
78 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080079 * Creates an empty call object with a unique call ID.
80 */
81 Call() {
82 this(null, null);
83 }
84
85 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080086 * Persists the specified parameters and initializes the new instance.
87 *
88 * @param handle The handle to dial.
89 * @param contactInfo Information about the entity being called.
90 */
Sailesh Nepalce704b92014-03-17 18:31:43 -070091 Call(Uri handle, ContactInfo contactInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080092 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -080093 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080094 mHandle = handle;
95 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080096 mCreationTime = new Date();
97 }
98
Santos Cordon61d0f702014-02-19 02:52:23 -080099 /** {@inheritDoc} */
100 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -0800101 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
102 mCallService.getComponentName(), Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800103 }
104
Ben Gilad0bf5b912014-01-28 17:55:57 -0800105 String getId() {
106 return mId;
107 }
108
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800109 CallState getState() {
110 return mState;
111 }
112
113 /**
114 * Sets the call state. Although there exists the notion of appropriate state transitions
115 * (see {@link CallState}), in practice those expectations break down when cellular systems
116 * misbehave and they do this very often. The result is that we do not enforce state transitions
117 * and instead keep the code resilient to unexpected state changes.
118 */
119 void setState(CallState state) {
120 mState = state;
121 clearCallInfo();
122 }
123
Sailesh Nepalce704b92014-03-17 18:31:43 -0700124 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800125 return mHandle;
126 }
127
Sailesh Nepalce704b92014-03-17 18:31:43 -0700128 void setHandle(Uri handle) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800129 mHandle = handle;
130 }
131
Ben Gilad0bf5b912014-01-28 17:55:57 -0800132 ContactInfo getContactInfo() {
133 return mContactInfo;
134 }
135
Ben Gilad0407fb22014-01-09 16:18:41 -0800136 /**
137 * @return The "age" of this call object in milliseconds, which typically also represents the
138 * period since this call was added to the set pending outgoing calls, see mCreationTime.
139 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800140 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800141 return new Date().getTime() - mCreationTime.getTime();
142 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800143
Yorke Leef98fb572014-03-05 10:56:55 -0800144 /**
145 * @return The time when this call object was created and added to the set of pending outgoing
146 * calls.
147 */
148 long getCreationTimeInMilliseconds() {
149 return mCreationTime.getTime();
150 }
151
Santos Cordonc195e362014-02-11 17:05:31 -0800152 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800153 return mCallService;
154 }
155
Santos Cordonc195e362014-02-11 17:05:31 -0800156 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800157 Preconditions.checkNotNull(callService);
158
Yorke Leeadee12d2014-03-13 12:08:30 -0700159 clearCallService();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800160
161 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800162 mCallService = callService;
163 }
164
165 /**
166 * Clears the associated call service.
167 */
168 void clearCallService() {
Yorke Leeadee12d2014-03-13 12:08:30 -0700169 if (mCallService != null) {
170 decrementAssociatedCallCount(mCallService);
171 mCallService.cancelOutgoingCall(getId());
172 mCallService = null;
173 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800174 }
175
176 void setCallServiceSelector(ICallServiceSelector selector) {
177 Preconditions.checkNotNull(selector);
178 mCallServiceSelector = selector;
179 }
180
181 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800182 // TODO(gilad): Un-comment once selectors are converted into wrappers.
183 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800184
185 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800186 }
187
188 /**
Ben Gilad61925612014-03-11 19:06:36 -0700189 * Adds the specified call service to the list of incompatible services. The set is used when
190 * attempting to switch a phone call between call services such that incompatible services can
191 * be avoided.
192 *
193 * @param callService The incompatible call service.
194 */
195 void addIncompatibleCallService(CallServiceWrapper callService) {
196 if (mIncompatibleCallServices == null) {
197 mIncompatibleCallServices = Sets.newHashSet();
198 }
199 mIncompatibleCallServices.add(callService);
200 }
201
202 /**
203 * Checks whether or not the specified callService was identified as incompatible in the
204 * context of this call.
205 *
206 * @param callService The call service to evaluate.
207 * @return True upon incompatible call services and false otherwise.
208 */
209 boolean isIncompatibleCallService(CallServiceWrapper callService) {
210 return mIncompatibleCallServices != null &&
211 mIncompatibleCallServices.contains(callService);
212 }
213
214 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800215 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
216 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800217 */
218 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800219 if (mState == CallState.NEW) {
220 if (mCallService != null) {
221 mCallService.abort(mId);
222 }
Ben Gilad9c234112014-03-04 16:07:33 -0800223 clearCallService();
224 clearCallServiceSelector();
225 mState = CallState.ABORTED;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800226 }
Santos Cordon681663d2014-01-30 04:32:15 -0800227 }
228
Santos Cordonc195e362014-02-11 17:05:31 -0800229 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800230 * Attempts to disconnect the call through the call service.
231 */
232 void disconnect() {
233 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800234 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800235 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800236 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800237 // The call isn't officially disconnected until the call service confirms that the call
238 // was actually disconnected. Only then is the association between call and call service
239 // severed, see {@link CallsManager#markCallAsDisconnected}.
240 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800241 }
242 }
243
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800244 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800245 * Answers the call if it is ringing.
246 */
247 void answer() {
248 Preconditions.checkNotNull(mCallService);
249
250 // Check to verify that the call is still in the ringing state. A call can change states
251 // between the time the user hits 'answer' and Telecomm receives the command.
252 if (isRinging("answer")) {
253 // At this point, we are asking the call service to answer but we don't assume that
254 // it will work. Instead, we wait until confirmation from the call service that the
255 // call is in a non-RINGING state before changing the UI. See
256 // {@link CallServiceAdapter#setActive} and other set* methods.
257 mCallService.answer(mId);
258 }
259 }
260
261 /**
262 * Rejects the call if it is ringing.
263 */
264 void reject() {
265 Preconditions.checkNotNull(mCallService);
266
267 // Check to verify that the call is still in the ringing state. A call can change states
268 // between the time the user hits 'reject' and Telecomm receives the command.
269 if (isRinging("reject")) {
270 mCallService.reject(mId);
271 }
272 }
273
274 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800275 * @return An object containing read-only information about this call.
276 */
277 CallInfo toCallInfo() {
278 if (mCallInfo == null) {
279 mCallInfo = new CallInfo(mId, mState, mHandle);
280 }
281 return mCallInfo;
282 }
283
284 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800285 * @return True if the call is ringing, else logs the action name.
286 */
287 private boolean isRinging(String actionName) {
288 if (mState == CallState.RINGING) {
289 return true;
290 }
291
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800292 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800293 return false;
294 }
295
296 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800297 * Resets the cached read-only version of this call.
298 */
299 private void clearCallInfo() {
300 mCallInfo = null;
301 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800302
303 @SuppressWarnings("rawtypes")
304 private void decrementAssociatedCallCount(ServiceBinder binder) {
305 if (binder != null) {
306 binder.decrementAssociatedCallCount();
307 }
308 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800309}