blob: 96d83745d44b3bd8f5be6e122546849935050eba [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;
Santos Cordon61d0f702014-02-19 02:52:23 -080026import java.util.UUID;
Ben Gilad0407fb22014-01-09 16:18:41 -080027
Ben Gilad2495d572014-01-09 17:26:19 -080028/**
29 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
30 * from the time the call intent was received by Telecomm (vs. the time the call was
31 * connected etc).
32 */
Ben Gilad0407fb22014-01-09 16:18:41 -080033final class Call {
Santos Cordon61d0f702014-02-19 02:52:23 -080034 /** Unique identifier for the call as a UUID string. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080035 private final String mId;
36
Ben Gilad0407fb22014-01-09 16:18:41 -080037 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080038 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080039
40 /**
41 * The time this call was created, typically also the time this call was added to the set
42 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
43 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
44 * certain call attempts as failed attempts.
45 */
46 private final Date mCreationTime;
47
Santos Cordon61d0f702014-02-19 02:52:23 -080048 /** The state of the call. */
49 private CallState mState;
50
51 /** The handle with which to establish this call. */
52 private String mHandle;
53
Ben Gilad0407fb22014-01-09 16:18:41 -080054 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -080055 * The call service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -080056 */
Santos Cordonc195e362014-02-11 17:05:31 -080057 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080058
Ben Gilad8e55d1d2014-02-26 16:25:56 -080059 /**
60 * The call-service selector for this call.
61 * TODO(gilad): Switch to using a wrapper object, see {@link #mCallService}.
62 */
63 private ICallServiceSelector mCallServiceSelector;
64
Santos Cordon61d0f702014-02-19 02:52:23 -080065 /** Read-only and parcelable version of this call. */
Santos Cordon0b03b4b2014-01-29 18:01:59 -080066 private CallInfo mCallInfo;
67
68 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080069 * Creates an empty call object with a unique call ID.
70 */
71 Call() {
72 this(null, null);
73 }
74
75 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080076 * Persists the specified parameters and initializes the new instance.
77 *
78 * @param handle The handle to dial.
79 * @param contactInfo Information about the entity being called.
80 */
Ben Gilad0bf5b912014-01-28 17:55:57 -080081 Call(String handle, ContactInfo contactInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080082 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -080083 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080084 mHandle = handle;
85 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080086 mCreationTime = new Date();
87 }
88
Santos Cordon61d0f702014-02-19 02:52:23 -080089 /** {@inheritDoc} */
90 @Override public String toString() {
91 return "[" + mId + ", " + mState + ", " + mCallService.getComponentName() + "]";
92 }
93
Ben Gilad0bf5b912014-01-28 17:55:57 -080094 String getId() {
95 return mId;
96 }
97
Santos Cordon0b03b4b2014-01-29 18:01:59 -080098 CallState getState() {
99 return mState;
100 }
101
102 /**
103 * Sets the call state. Although there exists the notion of appropriate state transitions
104 * (see {@link CallState}), in practice those expectations break down when cellular systems
105 * misbehave and they do this very often. The result is that we do not enforce state transitions
106 * and instead keep the code resilient to unexpected state changes.
107 */
108 void setState(CallState state) {
109 mState = state;
110 clearCallInfo();
111 }
112
Ben Gilad0bf5b912014-01-28 17:55:57 -0800113 String getHandle() {
114 return mHandle;
115 }
116
Santos Cordon61d0f702014-02-19 02:52:23 -0800117 void setHandle(String handle) {
118 mHandle = handle;
119 }
120
Ben Gilad0bf5b912014-01-28 17:55:57 -0800121 ContactInfo getContactInfo() {
122 return mContactInfo;
123 }
124
Ben Gilad0407fb22014-01-09 16:18:41 -0800125 /**
126 * @return The "age" of this call object in milliseconds, which typically also represents the
127 * period since this call was added to the set pending outgoing calls, see mCreationTime.
128 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800129 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800130 return new Date().getTime() - mCreationTime.getTime();
131 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800132
Santos Cordonc195e362014-02-11 17:05:31 -0800133 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800134 return mCallService;
135 }
136
Santos Cordonc195e362014-02-11 17:05:31 -0800137 void setCallService(CallServiceWrapper callService) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800138 Preconditions.checkNotNull(callService);
139
140 if (mCallService != null) {
141 // Should never be the case, basically covering for potential programming errors.
142 decrementAssociatedCallCount(mCallService);
143 }
144
145 callService.incrementAssociatedCallCount();
Santos Cordon681663d2014-01-30 04:32:15 -0800146 mCallService = callService;
147 }
148
149 /**
150 * Clears the associated call service.
151 */
152 void clearCallService() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800153 decrementAssociatedCallCount(mCallService);
154 mCallService = null;
155 }
156
157 void setCallServiceSelector(ICallServiceSelector selector) {
158 Preconditions.checkNotNull(selector);
159 mCallServiceSelector = selector;
160 }
161
162 void clearCallServiceSelector() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800163 // TODO(gilad): Un-comment once selectors are converted into wrappers.
164 // decrementAssociatedCallCount(mCallServiceSelector);
Ben Gilad9c234112014-03-04 16:07:33 -0800165
166 mCallServiceSelector = null;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800167 }
168
169 /**
170 * Aborts ongoing attempts to connect this call. No-op once the call is connected or has been
171 * disconnected. See {@link #disconnect} for already-connected calls.
172 */
173 void abort() {
174 if (mState == CallState.NEW ||
175 mState == CallState.DIALING ||
176 mState == CallState.RINGING) {
177
Ben Gilad9c234112014-03-04 16:07:33 -0800178 clearCallService();
179 clearCallServiceSelector();
180 mState = CallState.ABORTED;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800181 }
Santos Cordon681663d2014-01-30 04:32:15 -0800182 }
183
Santos Cordonc195e362014-02-11 17:05:31 -0800184 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800185 * Attempts to disconnect the call through the call service.
186 */
187 void disconnect() {
188 if (mCallService == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800189 Log.w(this, "disconnect() request on a call without a call service.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800190 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800191 Log.i(this, "Send disconnect to call service for call with id %s", mId);
Santos Cordonc195e362014-02-11 17:05:31 -0800192 // The call isn't officially disconnected until the call service confirms that the call
193 // was actually disconnected. Only then is the association between call and call service
194 // severed, see {@link CallsManager#markCallAsDisconnected}.
195 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800196 }
197 }
198
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800199 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800200 * Answers the call if it is ringing.
201 */
202 void answer() {
203 Preconditions.checkNotNull(mCallService);
204
205 // Check to verify that the call is still in the ringing state. A call can change states
206 // between the time the user hits 'answer' and Telecomm receives the command.
207 if (isRinging("answer")) {
208 // At this point, we are asking the call service to answer but we don't assume that
209 // it will work. Instead, we wait until confirmation from the call service that the
210 // call is in a non-RINGING state before changing the UI. See
211 // {@link CallServiceAdapter#setActive} and other set* methods.
212 mCallService.answer(mId);
213 }
214 }
215
216 /**
217 * Rejects the call if it is ringing.
218 */
219 void reject() {
220 Preconditions.checkNotNull(mCallService);
221
222 // Check to verify that the call is still in the ringing state. A call can change states
223 // between the time the user hits 'reject' and Telecomm receives the command.
224 if (isRinging("reject")) {
225 mCallService.reject(mId);
226 }
227 }
228
229 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800230 * @return An object containing read-only information about this call.
231 */
232 CallInfo toCallInfo() {
233 if (mCallInfo == null) {
234 mCallInfo = new CallInfo(mId, mState, mHandle);
235 }
236 return mCallInfo;
237 }
238
239 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800240 * @return True if the call is ringing, else logs the action name.
241 */
242 private boolean isRinging(String actionName) {
243 if (mState == CallState.RINGING) {
244 return true;
245 }
246
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800247 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800248 return false;
249 }
250
251 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800252 * Resets the cached read-only version of this call.
253 */
254 private void clearCallInfo() {
255 mCallInfo = null;
256 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800257
258 @SuppressWarnings("rawtypes")
259 private void decrementAssociatedCallCount(ServiceBinder binder) {
260 if (binder != null) {
261 binder.decrementAssociatedCallCount();
262 }
263 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800264}