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