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; |
| 21 | |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 22 | import com.android.internal.telecomm.ICallServiceSelector; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 23 | import com.google.android.collect.Sets; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 24 | import com.google.common.base.Preconditions; |
| 25 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 26 | import java.util.Date; |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 27 | import java.util.Locale; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 28 | import java.util.Set; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 29 | import java.util.UUID; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 30 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Encapsulates all aspects of a given phone call throughout its lifecycle, starting |
| 33 | * from the time the call intent was received by Telecomm (vs. the time the call was |
| 34 | * connected etc). |
| 35 | */ |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 36 | final class Call { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 37 | /** Unique identifier for the call as a UUID string. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 38 | private final String mId; |
| 39 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 40 | /** Additional contact information beyond handle above, optional. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 41 | private final ContactInfo mContactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 42 | |
| 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 Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 51 | /** 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 57 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 58 | * The call service which is attempted or already connecting this call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 59 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 60 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 61 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 62 | /** |
| 63 | * The call-service selector for this call. |
| 64 | * TODO(gilad): Switch to using a wrapper object, see {@link #mCallService}. |
| 65 | */ |
| 66 | private ICallServiceSelector mCallServiceSelector; |
| 67 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 68 | /** Read-only and parcelable version of this call. */ |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 69 | private CallInfo mCallInfo; |
| 70 | |
| 71 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 72 | * The set of call services that were attempted in the process of placing/switching this call |
| 73 | * but turned out unsuitable. Only used in the context of call switching. |
| 74 | */ |
| 75 | private Set<CallServiceWrapper> mIncompatibleCallServices; |
| 76 | |
| 77 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 78 | * Creates an empty call object with a unique call ID. |
| 79 | */ |
| 80 | Call() { |
| 81 | this(null, null); |
| 82 | } |
| 83 | |
| 84 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 85 | * Persists the specified parameters and initializes the new instance. |
| 86 | * |
| 87 | * @param handle The handle to dial. |
| 88 | * @param contactInfo Information about the entity being called. |
| 89 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 90 | Call(String handle, ContactInfo contactInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 91 | mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness. |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 92 | mState = CallState.NEW; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 93 | mHandle = handle; |
| 94 | mContactInfo = contactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 95 | mCreationTime = new Date(); |
| 96 | } |
| 97 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 98 | /** {@inheritDoc} */ |
| 99 | @Override public String toString() { |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 100 | return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState, |
| 101 | mCallService.getComponentName(), Log.pii(mHandle)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 104 | String getId() { |
| 105 | return mId; |
| 106 | } |
| 107 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 108 | CallState getState() { |
| 109 | return mState; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 114 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 115 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 116 | * and instead keep the code resilient to unexpected state changes. |
| 117 | */ |
| 118 | void setState(CallState state) { |
| 119 | mState = state; |
| 120 | clearCallInfo(); |
| 121 | } |
| 122 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 123 | String getHandle() { |
| 124 | return mHandle; |
| 125 | } |
| 126 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 127 | void setHandle(String handle) { |
| 128 | mHandle = handle; |
| 129 | } |
| 130 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 131 | ContactInfo getContactInfo() { |
| 132 | return mContactInfo; |
| 133 | } |
| 134 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 135 | /** |
| 136 | * @return The "age" of this call object in milliseconds, which typically also represents the |
| 137 | * period since this call was added to the set pending outgoing calls, see mCreationTime. |
| 138 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 139 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 140 | return new Date().getTime() - mCreationTime.getTime(); |
| 141 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 142 | |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 143 | /** |
| 144 | * @return The time when this call object was created and added to the set of pending outgoing |
| 145 | * calls. |
| 146 | */ |
| 147 | long getCreationTimeInMilliseconds() { |
| 148 | return mCreationTime.getTime(); |
| 149 | } |
| 150 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 151 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 152 | return mCallService; |
| 153 | } |
| 154 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 155 | void setCallService(CallServiceWrapper callService) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 156 | Preconditions.checkNotNull(callService); |
| 157 | |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 158 | clearCallService(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 159 | |
| 160 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 161 | mCallService = callService; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Clears the associated call service. |
| 166 | */ |
| 167 | void clearCallService() { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 168 | if (mCallService != null) { |
| 169 | decrementAssociatedCallCount(mCallService); |
| 170 | mCallService.cancelOutgoingCall(getId()); |
| 171 | mCallService = null; |
| 172 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void setCallServiceSelector(ICallServiceSelector selector) { |
| 176 | Preconditions.checkNotNull(selector); |
| 177 | mCallServiceSelector = selector; |
| 178 | } |
| 179 | |
| 180 | void clearCallServiceSelector() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 181 | // TODO(gilad): Un-comment once selectors are converted into wrappers. |
| 182 | // decrementAssociatedCallCount(mCallServiceSelector); |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 183 | |
| 184 | mCallServiceSelector = null; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 188 | * Adds the specified call service to the list of incompatible services. The set is used when |
| 189 | * attempting to switch a phone call between call services such that incompatible services can |
| 190 | * be avoided. |
| 191 | * |
| 192 | * @param callService The incompatible call service. |
| 193 | */ |
| 194 | void addIncompatibleCallService(CallServiceWrapper callService) { |
| 195 | if (mIncompatibleCallServices == null) { |
| 196 | mIncompatibleCallServices = Sets.newHashSet(); |
| 197 | } |
| 198 | mIncompatibleCallServices.add(callService); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Checks whether or not the specified callService was identified as incompatible in the |
| 203 | * context of this call. |
| 204 | * |
| 205 | * @param callService The call service to evaluate. |
| 206 | * @return True upon incompatible call services and false otherwise. |
| 207 | */ |
| 208 | boolean isIncompatibleCallService(CallServiceWrapper callService) { |
| 209 | return mIncompatibleCallServices != null && |
| 210 | mIncompatibleCallServices.contains(callService); |
| 211 | } |
| 212 | |
| 213 | /** |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 214 | * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW} |
| 215 | * outgoing calls. See {@link #disconnect} for already-connected calls. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 216 | */ |
| 217 | void abort() { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 218 | if (mState == CallState.NEW) { |
| 219 | if (mCallService != null) { |
| 220 | mCallService.abort(mId); |
| 221 | } |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 222 | clearCallService(); |
| 223 | clearCallServiceSelector(); |
| 224 | mState = CallState.ABORTED; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 225 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 228 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 229 | * Attempts to disconnect the call through the call service. |
| 230 | */ |
| 231 | void disconnect() { |
| 232 | if (mCallService == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 233 | Log.w(this, "disconnect() request on a call without a call service."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 234 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 235 | 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] | 236 | // The call isn't officially disconnected until the call service confirms that the call |
| 237 | // was actually disconnected. Only then is the association between call and call service |
| 238 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
| 239 | mCallService.disconnect(mId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 243 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 244 | * Answers the call if it is ringing. |
| 245 | */ |
| 246 | void answer() { |
| 247 | Preconditions.checkNotNull(mCallService); |
| 248 | |
| 249 | // Check to verify that the call is still in the ringing state. A call can change states |
| 250 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 251 | if (isRinging("answer")) { |
| 252 | // At this point, we are asking the call service to answer but we don't assume that |
| 253 | // it will work. Instead, we wait until confirmation from the call service that the |
| 254 | // call is in a non-RINGING state before changing the UI. See |
| 255 | // {@link CallServiceAdapter#setActive} and other set* methods. |
| 256 | mCallService.answer(mId); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Rejects the call if it is ringing. |
| 262 | */ |
| 263 | void reject() { |
| 264 | Preconditions.checkNotNull(mCallService); |
| 265 | |
| 266 | // Check to verify that the call is still in the ringing state. A call can change states |
| 267 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 268 | if (isRinging("reject")) { |
| 269 | mCallService.reject(mId); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame^] | 274 | * Puts the call on hold if it is currently active. |
| 275 | */ |
| 276 | void hold() { |
| 277 | Preconditions.checkNotNull(mCallService); |
| 278 | |
| 279 | if (mState == CallState.ACTIVE) { |
| 280 | mCallService.hold(mId); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Releases the call from hold if it is currently active. |
| 286 | */ |
| 287 | void unhold() { |
| 288 | Preconditions.checkNotNull(mCallService); |
| 289 | |
| 290 | if (mState == CallState.ON_HOLD) { |
| 291 | mCallService.unhold(mId); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 296 | * @return An object containing read-only information about this call. |
| 297 | */ |
| 298 | CallInfo toCallInfo() { |
| 299 | if (mCallInfo == null) { |
| 300 | mCallInfo = new CallInfo(mId, mState, mHandle); |
| 301 | } |
| 302 | return mCallInfo; |
| 303 | } |
| 304 | |
| 305 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 306 | * @return True if the call is ringing, else logs the action name. |
| 307 | */ |
| 308 | private boolean isRinging(String actionName) { |
| 309 | if (mState == CallState.RINGING) { |
| 310 | return true; |
| 311 | } |
| 312 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 313 | 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] | 314 | return false; |
| 315 | } |
| 316 | |
| 317 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 318 | * Resets the cached read-only version of this call. |
| 319 | */ |
| 320 | private void clearCallInfo() { |
| 321 | mCallInfo = null; |
| 322 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 323 | |
| 324 | @SuppressWarnings("rawtypes") |
| 325 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 326 | if (binder != null) { |
| 327 | binder.decrementAssociatedCallCount(); |
| 328 | } |
| 329 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 330 | } |