blob: 1463c5171adb99f17885ef27404e73f1b2772e21 [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;
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
Santos Cordonc195e362014-02-11 17:05:31 -0800135 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800136 return mCallService;
137 }
138
Santos Cordonc195e362014-02-11 17:05:31 -0800139 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800140 Preconditions.checkNotNull(callService);
141
142 if (mCallService != null) {
143 // Should never be the case, basically covering for potential programming errors.
144 decrementAssociatedCallCount(mCallService);
145 }
146
147 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800148 mCallService = callService;
149 }
150
151 /**
152 * Clears the associated call service.
153 */
154 void clearCallService() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800155 decrementAssociatedCallCount(mCallService);
156 mCallService = null;
157 }
158
159 void setCallServiceSelector(ICallServiceSelector selector) {
160 Preconditions.checkNotNull(selector);
161 mCallServiceSelector = selector;
162 }
163
164 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800165 // TODO(gilad): Un-comment once selectors are converted into wrappers.
166 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800167
168 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800169 }
170
171 /**
Ben Gilad28e8ad62014-03-06 17:01:54 -0800172 * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW}
173 * outgoing calls. See {@link #disconnect} for already-connected calls.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800174 */
175 void abort() {
Ben Gilad28e8ad62014-03-06 17:01:54 -0800176 if (mState == CallState.NEW) {
177 if (mCallService != null) {
178 mCallService.abort(mId);
179 }
Ben Gilad9c234112014-03-04 16:07:33 -0800180 clearCallService();
181 clearCallServiceSelector();
182 mState = CallState.ABORTED;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800183 }
Santos Cordon681663d2014-01-30 04:32:15 -0800184 }
185
Santos Cordonc195e362014-02-11 17:05:31 -0800186 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800187 * Attempts to disconnect the call through the call service.
188 */
189 void disconnect() {
190 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800191 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800192 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800193 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800194 // The call isn't officially disconnected until the call service confirms that the call
195 // was actually disconnected. Only then is the association between call and call service
196 // severed, see {@link CallsManager#markCallAsDisconnected}.
197 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800198 }
199 }
200
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800201 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800202 * Answers the call if it is ringing.
203 */
204 void answer() {
205 Preconditions.checkNotNull(mCallService);
206
207 // Check to verify that the call is still in the ringing state. A call can change states
208 // between the time the user hits 'answer' and Telecomm receives the command.
209 if (isRinging("answer")) {
210 // At this point, we are asking the call service to answer but we don't assume that
211 // it will work. Instead, we wait until confirmation from the call service that the
212 // call is in a non-RINGING state before changing the UI. See
213 // {@link CallServiceAdapter#setActive} and other set* methods.
214 mCallService.answer(mId);
215 }
216 }
217
218 /**
219 * Rejects the call if it is ringing.
220 */
221 void reject() {
222 Preconditions.checkNotNull(mCallService);
223
224 // Check to verify that the call is still in the ringing state. A call can change states
225 // between the time the user hits 'reject' and Telecomm receives the command.
226 if (isRinging("reject")) {
227 mCallService.reject(mId);
228 }
229 }
230
231 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800232 * @return An object containing read-only information about this call.
233 */
234 CallInfo toCallInfo() {
235 if (mCallInfo == null) {
236 mCallInfo = new CallInfo(mId, mState, mHandle);
237 }
238 return mCallInfo;
239 }
240
241 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800242 * @return True if the call is ringing, else logs the action name.
243 */
244 private boolean isRinging(String actionName) {
245 if (mState == CallState.RINGING) {
246 return true;
247 }
248
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800249 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800250 return false;
251 }
252
253 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800254 * Resets the cached read-only version of this call.
255 */
256 private void clearCallInfo() {
257 mCallInfo = null;
258 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800259
260 @SuppressWarnings("rawtypes")
261 private void decrementAssociatedCallCount(ServiceBinder binder) {
262 if (binder != null) {
263 binder.decrementAssociatedCallCount();
264 }
265 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800266}