blob: 7049742f2cf79adabd940f963bc26d3de6a8fa63 [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 Cordon049b7b62014-01-30 05:34:26 -080019import android.os.RemoteException;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080020import android.telecomm.CallInfo;
21import android.telecomm.CallState;
Santos Cordon049b7b62014-01-30 05:34:26 -080022import android.util.Log;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080023
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;
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 Cordon049b7b62014-01-30 05:34:26 -080035 private static final String TAG = Call.class.getSimpleName();
Ben Gilad0407fb22014-01-09 16:18:41 -080036
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
43 /**
44 * The time this call was created, typically also the time this call was added to the set
45 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
46 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
47 * certain call attempts as failed attempts.
48 */
49 private final Date mCreationTime;
50
Santos Cordon61d0f702014-02-19 02:52:23 -080051 /** The state of the call. */
52 private CallState mState;
53
54 /** The handle with which to establish this call. */
55 private String mHandle;
56
Ben Gilad0407fb22014-01-09 16:18:41 -080057 /**
Santos Cordon681663d2014-01-30 04:32:15 -080058 * The call service which is currently connecting this call, null as long as the call is not
59 * connected.
60 */
Santos Cordonc195e362014-02-11 17:05:31 -080061 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080062
Santos Cordon61d0f702014-02-19 02:52:23 -080063 /** Read-only and parcelable version of this call. */
Santos Cordon0b03b4b2014-01-29 18:01:59 -080064 private CallInfo mCallInfo;
65
66 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080067 * Creates an empty call object with a unique call ID.
68 */
69 Call() {
70 this(null, null);
71 }
72
73 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080074 * Persists the specified parameters and initializes the new instance.
75 *
76 * @param handle The handle to dial.
77 * @param contactInfo Information about the entity being called.
78 */
Ben Gilad0bf5b912014-01-28 17:55:57 -080079 Call(String handle, ContactInfo contactInfo) {
Santos Cordon61d0f702014-02-19 02:52:23 -080080 mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness.
Santos Cordon0b03b4b2014-01-29 18:01:59 -080081 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080082 mHandle = handle;
83 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080084 mCreationTime = new Date();
85 }
86
Santos Cordon61d0f702014-02-19 02:52:23 -080087 /** {@inheritDoc} */
88 @Override public String toString() {
89 return "[" + mId + ", " + mState + ", " + mCallService.getComponentName() + "]";
90 }
91
Ben Gilad0bf5b912014-01-28 17:55:57 -080092 String getId() {
93 return mId;
94 }
95
Santos Cordon0b03b4b2014-01-29 18:01:59 -080096 CallState getState() {
97 return mState;
98 }
99
100 /**
101 * Sets the call state. Although there exists the notion of appropriate state transitions
102 * (see {@link CallState}), in practice those expectations break down when cellular systems
103 * misbehave and they do this very often. The result is that we do not enforce state transitions
104 * and instead keep the code resilient to unexpected state changes.
105 */
106 void setState(CallState state) {
107 mState = state;
108 clearCallInfo();
109 }
110
Ben Gilad0bf5b912014-01-28 17:55:57 -0800111 String getHandle() {
112 return mHandle;
113 }
114
Santos Cordon61d0f702014-02-19 02:52:23 -0800115 void setHandle(String handle) {
116 mHandle = handle;
117 }
118
Ben Gilad0bf5b912014-01-28 17:55:57 -0800119 ContactInfo getContactInfo() {
120 return mContactInfo;
121 }
122
Ben Gilad0407fb22014-01-09 16:18:41 -0800123 /**
124 * @return The "age" of this call object in milliseconds, which typically also represents the
125 * period since this call was added to the set pending outgoing calls, see mCreationTime.
126 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800127 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800128 return new Date().getTime() - mCreationTime.getTime();
129 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800130
Santos Cordonc195e362014-02-11 17:05:31 -0800131 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800132 return mCallService;
133 }
134
Santos Cordonc195e362014-02-11 17:05:31 -0800135 void setCallService(CallServiceWrapper callService) {
Santos Cordon681663d2014-01-30 04:32:15 -0800136 mCallService = callService;
137 }
138
139 /**
140 * Clears the associated call service.
141 */
142 void clearCallService() {
143 setCallService(null);
144 }
145
Santos Cordonc195e362014-02-11 17:05:31 -0800146 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800147 * Attempts to disconnect the call through the call service.
148 */
149 void disconnect() {
150 if (mCallService == null) {
151 Log.w(TAG, "disconnect() request on a call without a call service.");
152 } else {
Santos Cordonc195e362014-02-11 17:05:31 -0800153 Log.i(TAG, "Send disconnect to call service for call with id " + mId);
154 // The call isn't officially disconnected until the call service confirms that the call
155 // was actually disconnected. Only then is the association between call and call service
156 // severed, see {@link CallsManager#markCallAsDisconnected}.
157 mCallService.disconnect(mId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800158 }
159 }
160
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800161 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800162 * Answers the call if it is ringing.
163 */
164 void answer() {
165 Preconditions.checkNotNull(mCallService);
166
167 // Check to verify that the call is still in the ringing state. A call can change states
168 // between the time the user hits 'answer' and Telecomm receives the command.
169 if (isRinging("answer")) {
170 // At this point, we are asking the call service to answer but we don't assume that
171 // it will work. Instead, we wait until confirmation from the call service that the
172 // call is in a non-RINGING state before changing the UI. See
173 // {@link CallServiceAdapter#setActive} and other set* methods.
174 mCallService.answer(mId);
175 }
176 }
177
178 /**
179 * Rejects the call if it is ringing.
180 */
181 void reject() {
182 Preconditions.checkNotNull(mCallService);
183
184 // Check to verify that the call is still in the ringing state. A call can change states
185 // between the time the user hits 'reject' and Telecomm receives the command.
186 if (isRinging("reject")) {
187 mCallService.reject(mId);
188 }
189 }
190
191 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800192 * @return An object containing read-only information about this call.
193 */
194 CallInfo toCallInfo() {
195 if (mCallInfo == null) {
196 mCallInfo = new CallInfo(mId, mState, mHandle);
197 }
198 return mCallInfo;
199 }
200
201 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800202 * @return True if the call is ringing, else logs the action name.
203 */
204 private boolean isRinging(String actionName) {
205 if (mState == CallState.RINGING) {
206 return true;
207 }
208
209 Log.i(TAG, "Request to " + actionName + " a non-ringing call " + this);
210 return false;
211 }
212
213 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800214 * Resets the cached read-only version of this call.
215 */
216 private void clearCallInfo() {
217 mCallInfo = null;
218 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800219}