blob: cb7453399072dd2f0a4230d741393a5661a1d52a [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;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080021import android.telecomm.ICallServiceSelector;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080022
Santos Cordon61d0f702014-02-19 02:52:23 -080023import com.google.common.base.Preconditions;
24
Ben Gilad0407fb22014-01-09 16:18:41 -080025import java.util.Date;
Sailesh Nepal91990782014-03-08 17:45:52 -080026import java.util.Locale;
Santos Cordon61d0f702014-02-19 02:52:23 -080027import java.util.UUID;
Ben Gilad0407fb22014-01-09 16:18:41 -080028
Ben Gilad2495d572014-01-09 17:26:19 -080029/**
30 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
31 * from the time the call intent was received by Telecomm (vs. the time the call was
32 * connected etc).
33 */
Ben Gilad0407fb22014-01-09 16:18:41 -080034final class Call {
Santos Cordon61d0f702014-02-19 02:52:23 -080035 /** Unique identifier for the call as a UUID string. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080036 private final String mId;
37
Ben Gilad0407fb22014-01-09 16:18:41 -080038 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080039 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080040
41 /**
42 * The time this call was created, typically also the time this call was added to the set
43 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
44 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
45 * certain call attempts as failed attempts.
46 */
47 private final Date mCreationTime;
48
Santos Cordon61d0f702014-02-19 02:52:23 -080049 /** The state of the call. */
50 private CallState mState;
51
52 /** The handle with which to establish this call. */
53 private String mHandle;
54
Ben Gilad0407fb22014-01-09 16:18:41 -080055 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080056 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080057 */
Santos Cordonc195e362014-02-11 17:05:31 -080058 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080059
Ben Gilad8e55d1d2014-02-26 16:25:56 -080060 /**
61 * The call-service selector for this call.
62 * TODO(gilad): Switch to using a wrapper object, see {@link #mCallService}.
63 */
64 private ICallServiceSelector mCallServiceSelector;
65
Santos Cordon61d0f702014-02-19 02:52:23 -080066 /** Read-only and parcelable version of this call. */
Santos Cordon0b03b4b2014-01-29 18:01:59 -080067 private CallInfo mCallInfo;
68
69 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080070 * Creates an empty call object with a unique call ID.
71 */
72 Call() {
73 this(null, null);
74 }
75
76 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080077 * Persists the specified parameters and initializes the new instance.
78 *
79 * @param handle The handle to dial.
80 * @param contactInfo Information about the entity being called.
81 */
Ben Gilad0bf5b912014-01-28 17:55:57 -080082 Call(String handle, ContactInfo contactInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080083 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -080084 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080085 mHandle = handle;
86 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080087 mCreationTime = new Date();
88 }
89
Santos Cordon61d0f702014-02-19 02:52:23 -080090 /** {@inheritDoc} */
91 @Override public String toString() {
Sailesh Nepal91990782014-03-08 17:45:52 -080092 return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
93 mCallService.getComponentName(), Log.pii(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -080094 }
95
Ben Gilad0bf5b912014-01-28 17:55:57 -080096 String getId() {
97 return mId;
98 }
99
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800100 CallState getState() {
101 return mState;
102 }
103
104 /**
105 * Sets the call state. Although there exists the notion of appropriate state transitions
106 * (see {@link CallState}), in practice those expectations break down when cellular systems
107 * misbehave and they do this very often. The result is that we do not enforce state transitions
108 * and instead keep the code resilient to unexpected state changes.
109 */
110 void setState(CallState state) {
111 mState = state;
112 clearCallInfo();
113 }
114
Ben Gilad0bf5b912014-01-28 17:55:57 -0800115 String getHandle() {
116 return mHandle;
117 }
118
Santos Cordon61d0f702014-02-19 02:52:23 -0800119 void setHandle(String handle) {
120 mHandle = handle;
121 }
122
Ben Gilad0bf5b912014-01-28 17:55:57 -0800123 ContactInfo getContactInfo() {
124 return mContactInfo;
125 }
126
Ben Gilad0407fb22014-01-09 16:18:41 -0800127 /**
128 * @return The "age" of this call object in milliseconds, which typically also represents the
129 * period since this call was added to the set pending outgoing calls, see mCreationTime.
130 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800131 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800132 return new Date().getTime() - mCreationTime.getTime();
133 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800134
Yorke Leef98fb572014-03-05 10:56:55 -0800135 /**
136 * @return The time when this call object was created and added to the set of pending outgoing
137 * calls.
138 */
139 long getCreationTimeInMilliseconds() {
140 return mCreationTime.getTime();
141 }
142
Santos Cordonc195e362014-02-11 17:05:31 -0800143 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800144 return mCallService;
145 }
146
Santos Cordonc195e362014-02-11 17:05:31 -0800147 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800148 Preconditions.checkNotNull(callService);
149
150 if (mCallService != null) {
151 // Should never be the case, basically covering for potential programming errors.
152 decrementAssociatedCallCount(mCallService);
153 }
154
155 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800156 mCallService = callService;
157 }
158
159 /**
160 * Clears the associated call service.
161 */
162 void clearCallService() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800163 decrementAssociatedCallCount(mCallService);
164 mCallService = null;
165 }
166
167 void setCallServiceSelector(ICallServiceSelector selector) {
168 Preconditions.checkNotNull(selector);
169 mCallServiceSelector = selector;
170 }
171
172 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800173 // TODO(gilad): Un-comment once selectors are converted into wrappers.
174 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800175
176 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800177 }
178
179 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800180 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
181 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800182 */
183 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800184 if (mState == CallState.NEW) {
185 if (mCallService != null) {
186 mCallService.abort(mId);
187 }
Ben Gilad9c234112014-03-04 16:07:33 -0800188 clearCallService();
189 clearCallServiceSelector();
190 mState = CallState.ABORTED;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800191 }
Santos Cordon681663d2014-01-30 04:32:15 -0800192 }
193
Santos Cordonc195e362014-02-11 17:05:31 -0800194 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800195 * Attempts to disconnect the call through the call service.
196 */
197 void disconnect() {
198 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800199 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800200 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800201 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800202 // The call isn't officially disconnected until the call service confirms that the call
203 // was actually disconnected. Only then is the association between call and call service
204 // severed, see {@link CallsManager#markCallAsDisconnected}.
205 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800206 }
207 }
208
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800209 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800210 * Answers the call if it is ringing.
211 */
212 void answer() {
213 Preconditions.checkNotNull(mCallService);
214
215 // Check to verify that the call is still in the ringing state. A call can change states
216 // between the time the user hits 'answer' and Telecomm receives the command.
217 if (isRinging("answer")) {
218 // At this point, we are asking the call service to answer but we don't assume that
219 // it will work. Instead, we wait until confirmation from the call service that the
220 // call is in a non-RINGING state before changing the UI. See
221 // {@link CallServiceAdapter#setActive} and other set* methods.
222 mCallService.answer(mId);
223 }
224 }
225
226 /**
227 * Rejects the call if it is ringing.
228 */
229 void reject() {
230 Preconditions.checkNotNull(mCallService);
231
232 // Check to verify that the call is still in the ringing state. A call can change states
233 // between the time the user hits 'reject' and Telecomm receives the command.
234 if (isRinging("reject")) {
235 mCallService.reject(mId);
236 }
237 }
238
239 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800240 * @return An object containing read-only information about this call.
241 */
242 CallInfo toCallInfo() {
243 if (mCallInfo == null) {
244 mCallInfo = new CallInfo(mId, mState, mHandle);
245 }
246 return mCallInfo;
247 }
248
249 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800250 * @return True if the call is ringing, else logs the action name.
251 */
252 private boolean isRinging(String actionName) {
253 if (mState == CallState.RINGING) {
254 return true;
255 }
256
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800257 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800258 return false;
259 }
260
261 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800262 * Resets the cached read-only version of this call.
263 */
264 private void clearCallInfo() {
265 mCallInfo = null;
266 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800267
268 @SuppressWarnings("rawtypes")
269 private void decrementAssociatedCallCount(ServiceBinder binder) {
270 if (binder != null) {
271 binder.decrementAssociatedCallCount();
272 }
273 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800274}