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