Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 1 | /* |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 19 | import android.telecomm.CallInfo; |
| 20 | import android.telecomm.CallState; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 21 | import android.telecomm.ICallServiceSelector; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 22 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 23 | import com.google.common.base.Preconditions; |
| 24 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 25 | import java.util.Date; |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 26 | import java.util.Locale; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 27 | import java.util.UUID; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 28 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 29 | /** |
| 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 34 | final class Call { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 35 | /** Unique identifier for the call as a UUID string. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 36 | private final String mId; |
| 37 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 38 | /** Additional contact information beyond handle above, optional. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 39 | private final ContactInfo mContactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 40 | |
| 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 Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 49 | /** 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 55 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 56 | * The call service which is attempted or already connecting this call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 57 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 58 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 59 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 60 | /** |
| 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 Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 66 | /** Read-only and parcelable version of this call. */ |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 67 | private CallInfo mCallInfo; |
| 68 | |
| 69 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 70 | * Creates an empty call object with a unique call ID. |
| 71 | */ |
| 72 | Call() { |
| 73 | this(null, null); |
| 74 | } |
| 75 | |
| 76 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 77 | * 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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 82 | Call(String handle, ContactInfo contactInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 83 | mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness. |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 84 | mState = CallState.NEW; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 85 | mHandle = handle; |
| 86 | mContactInfo = contactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 87 | mCreationTime = new Date(); |
| 88 | } |
| 89 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 90 | /** {@inheritDoc} */ |
| 91 | @Override public String toString() { |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 92 | return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState, |
| 93 | mCallService.getComponentName(), Log.pii(mHandle)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 96 | String getId() { |
| 97 | return mId; |
| 98 | } |
| 99 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 100 | 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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 115 | String getHandle() { |
| 116 | return mHandle; |
| 117 | } |
| 118 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 119 | void setHandle(String handle) { |
| 120 | mHandle = handle; |
| 121 | } |
| 122 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 123 | ContactInfo getContactInfo() { |
| 124 | return mContactInfo; |
| 125 | } |
| 126 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 127 | /** |
| 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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 131 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 132 | return new Date().getTime() - mCreationTime.getTime(); |
| 133 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 134 | |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame^] | 135 | /** |
| 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 Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 143 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 144 | return mCallService; |
| 145 | } |
| 146 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 147 | void setCallService(CallServiceWrapper callService) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 148 | 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 Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 156 | mCallService = callService; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Clears the associated call service. |
| 161 | */ |
| 162 | void clearCallService() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 163 | 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 Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 173 | // TODO(gilad): Un-comment once selectors are converted into wrappers. |
| 174 | // decrementAssociatedCallCount(mCallServiceSelector); |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 175 | |
| 176 | mCallServiceSelector = null; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 180 | * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW} |
| 181 | * outgoing calls. See {@link #disconnect} for already-connected calls. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 182 | */ |
| 183 | void abort() { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 184 | if (mState == CallState.NEW) { |
| 185 | if (mCallService != null) { |
| 186 | mCallService.abort(mId); |
| 187 | } |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 188 | clearCallService(); |
| 189 | clearCallServiceSelector(); |
| 190 | mState = CallState.ABORTED; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 191 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 194 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 195 | * Attempts to disconnect the call through the call service. |
| 196 | */ |
| 197 | void disconnect() { |
| 198 | if (mCallService == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 199 | Log.w(this, "disconnect() request on a call without a call service."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 200 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 201 | Log.i(this, "Send disconnect to call service for call with id %s", mId); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 202 | // 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 Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 209 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 210 | * 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 Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 240 | * @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 Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 250 | * @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 Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 257 | Log.i(this, "Request to %s a non-ringing call %s", actionName, this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
| 261 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 262 | * Resets the cached read-only version of this call. |
| 263 | */ |
| 264 | private void clearCallInfo() { |
| 265 | mCallInfo = null; |
| 266 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 267 | |
| 268 | @SuppressWarnings("rawtypes") |
| 269 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 270 | if (binder != null) { |
| 271 | binder.decrementAssociatedCallCount(); |
| 272 | } |
| 273 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 274 | } |