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; |
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 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 135 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 136 | return mCallService; |
| 137 | } |
| 138 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 139 | void setCallService(CallServiceWrapper callService) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 140 | Preconditions.checkNotNull(callService); |
| 141 | |
| 142 | if (mCallService != null) { |
| 143 | // Should never be the case, basically covering for potential programming errors. |
| 144 | decrementAssociatedCallCount(mCallService); |
| 145 | } |
| 146 | |
| 147 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 148 | mCallService = callService; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Clears the associated call service. |
| 153 | */ |
| 154 | void clearCallService() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 155 | decrementAssociatedCallCount(mCallService); |
| 156 | mCallService = null; |
| 157 | } |
| 158 | |
| 159 | void setCallServiceSelector(ICallServiceSelector selector) { |
| 160 | Preconditions.checkNotNull(selector); |
| 161 | mCallServiceSelector = selector; |
| 162 | } |
| 163 | |
| 164 | void clearCallServiceSelector() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 165 | // TODO(gilad): Un-comment once selectors are converted into wrappers. |
| 166 | // decrementAssociatedCallCount(mCallServiceSelector); |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 167 | |
| 168 | mCallServiceSelector = null; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 172 | * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW} |
| 173 | * outgoing calls. See {@link #disconnect} for already-connected calls. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 174 | */ |
| 175 | void abort() { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 176 | if (mState == CallState.NEW) { |
| 177 | if (mCallService != null) { |
| 178 | mCallService.abort(mId); |
| 179 | } |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 180 | clearCallService(); |
| 181 | clearCallServiceSelector(); |
| 182 | mState = CallState.ABORTED; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 183 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 186 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 187 | * Attempts to disconnect the call through the call service. |
| 188 | */ |
| 189 | void disconnect() { |
| 190 | if (mCallService == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 191 | Log.w(this, "disconnect() request on a call without a call service."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 192 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 193 | 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] | 194 | // The call isn't officially disconnected until the call service confirms that the call |
| 195 | // was actually disconnected. Only then is the association between call and call service |
| 196 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
| 197 | mCallService.disconnect(mId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 201 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 202 | * Answers the call if it is ringing. |
| 203 | */ |
| 204 | void answer() { |
| 205 | Preconditions.checkNotNull(mCallService); |
| 206 | |
| 207 | // Check to verify that the call is still in the ringing state. A call can change states |
| 208 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 209 | if (isRinging("answer")) { |
| 210 | // At this point, we are asking the call service to answer but we don't assume that |
| 211 | // it will work. Instead, we wait until confirmation from the call service that the |
| 212 | // call is in a non-RINGING state before changing the UI. See |
| 213 | // {@link CallServiceAdapter#setActive} and other set* methods. |
| 214 | mCallService.answer(mId); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Rejects the call if it is ringing. |
| 220 | */ |
| 221 | void reject() { |
| 222 | Preconditions.checkNotNull(mCallService); |
| 223 | |
| 224 | // Check to verify that the call is still in the ringing state. A call can change states |
| 225 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 226 | if (isRinging("reject")) { |
| 227 | mCallService.reject(mId); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 232 | * @return An object containing read-only information about this call. |
| 233 | */ |
| 234 | CallInfo toCallInfo() { |
| 235 | if (mCallInfo == null) { |
| 236 | mCallInfo = new CallInfo(mId, mState, mHandle); |
| 237 | } |
| 238 | return mCallInfo; |
| 239 | } |
| 240 | |
| 241 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 242 | * @return True if the call is ringing, else logs the action name. |
| 243 | */ |
| 244 | private boolean isRinging(String actionName) { |
| 245 | if (mState == CallState.RINGING) { |
| 246 | return true; |
| 247 | } |
| 248 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 249 | 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] | 250 | return false; |
| 251 | } |
| 252 | |
| 253 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 254 | * Resets the cached read-only version of this call. |
| 255 | */ |
| 256 | private void clearCallInfo() { |
| 257 | mCallInfo = null; |
| 258 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 259 | |
| 260 | @SuppressWarnings("rawtypes") |
| 261 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 262 | if (binder != null) { |
| 263 | binder.decrementAssociatedCallCount(); |
| 264 | } |
| 265 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 266 | } |