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; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 26 | import java.util.UUID; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 27 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Encapsulates all aspects of a given phone call throughout its lifecycle, starting |
| 30 | * from the time the call intent was received by Telecomm (vs. the time the call was |
| 31 | * connected etc). |
| 32 | */ |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 33 | final class Call { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 34 | /** Unique identifier for the call as a UUID string. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 35 | private final String mId; |
| 36 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 37 | /** Additional contact information beyond handle above, optional. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 38 | private final ContactInfo mContactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * The time this call was created, typically also the time this call was added to the set |
| 42 | * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard. |
| 43 | * Beyond logging and such, may also be used for bookkeeping and specifically for marking |
| 44 | * certain call attempts as failed attempts. |
| 45 | */ |
| 46 | private final Date mCreationTime; |
| 47 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 48 | /** The state of the call. */ |
| 49 | private CallState mState; |
| 50 | |
| 51 | /** The handle with which to establish this call. */ |
| 52 | private String mHandle; |
| 53 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 54 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 55 | * The call service which is attempted or already connecting this call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 56 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 57 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 58 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 59 | /** |
| 60 | * The call-service selector for this call. |
| 61 | * TODO(gilad): Switch to using a wrapper object, see {@link #mCallService}. |
| 62 | */ |
| 63 | private ICallServiceSelector mCallServiceSelector; |
| 64 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 65 | /** Read-only and parcelable version of this call. */ |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 66 | private CallInfo mCallInfo; |
| 67 | |
| 68 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 69 | * Creates an empty call object with a unique call ID. |
| 70 | */ |
| 71 | Call() { |
| 72 | this(null, null); |
| 73 | } |
| 74 | |
| 75 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 76 | * Persists the specified parameters and initializes the new instance. |
| 77 | * |
| 78 | * @param handle The handle to dial. |
| 79 | * @param contactInfo Information about the entity being called. |
| 80 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 81 | Call(String handle, ContactInfo contactInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 82 | mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness. |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 83 | mState = CallState.NEW; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 84 | mHandle = handle; |
| 85 | mContactInfo = contactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 86 | mCreationTime = new Date(); |
| 87 | } |
| 88 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 89 | /** {@inheritDoc} */ |
| 90 | @Override public String toString() { |
| 91 | return "[" + mId + ", " + mState + ", " + mCallService.getComponentName() + "]"; |
| 92 | } |
| 93 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 94 | String getId() { |
| 95 | return mId; |
| 96 | } |
| 97 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 98 | CallState getState() { |
| 99 | return mState; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 104 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 105 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 106 | * and instead keep the code resilient to unexpected state changes. |
| 107 | */ |
| 108 | void setState(CallState state) { |
| 109 | mState = state; |
| 110 | clearCallInfo(); |
| 111 | } |
| 112 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 113 | String getHandle() { |
| 114 | return mHandle; |
| 115 | } |
| 116 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 117 | void setHandle(String handle) { |
| 118 | mHandle = handle; |
| 119 | } |
| 120 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 121 | ContactInfo getContactInfo() { |
| 122 | return mContactInfo; |
| 123 | } |
| 124 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 125 | /** |
| 126 | * @return The "age" of this call object in milliseconds, which typically also represents the |
| 127 | * period since this call was added to the set pending outgoing calls, see mCreationTime. |
| 128 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 129 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 130 | return new Date().getTime() - mCreationTime.getTime(); |
| 131 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 132 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 133 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 134 | return mCallService; |
| 135 | } |
| 136 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 137 | void setCallService(CallServiceWrapper callService) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 138 | Preconditions.checkNotNull(callService); |
| 139 | |
| 140 | if (mCallService != null) { |
| 141 | // Should never be the case, basically covering for potential programming errors. |
| 142 | decrementAssociatedCallCount(mCallService); |
| 143 | } |
| 144 | |
| 145 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 146 | mCallService = callService; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Clears the associated call service. |
| 151 | */ |
| 152 | void clearCallService() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 153 | decrementAssociatedCallCount(mCallService); |
| 154 | mCallService = null; |
| 155 | } |
| 156 | |
| 157 | void setCallServiceSelector(ICallServiceSelector selector) { |
| 158 | Preconditions.checkNotNull(selector); |
| 159 | mCallServiceSelector = selector; |
| 160 | } |
| 161 | |
| 162 | void clearCallServiceSelector() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 163 | // TODO(gilad): Un-comment once selectors are converted into wrappers. |
| 164 | // decrementAssociatedCallCount(mCallServiceSelector); |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 165 | |
| 166 | mCallServiceSelector = null; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Aborts ongoing attempts to connect this call. No-op once the call is connected or has been |
| 171 | * disconnected. See {@link #disconnect} for already-connected calls. |
| 172 | */ |
| 173 | void abort() { |
| 174 | if (mState == CallState.NEW || |
| 175 | mState == CallState.DIALING || |
| 176 | mState == CallState.RINGING) { |
| 177 | |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 178 | clearCallService(); |
| 179 | clearCallServiceSelector(); |
| 180 | mState = CallState.ABORTED; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 181 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 184 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 185 | * Attempts to disconnect the call through the call service. |
| 186 | */ |
| 187 | void disconnect() { |
| 188 | if (mCallService == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 189 | Log.w(this, "disconnect() request on a call without a call service."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 190 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 191 | 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] | 192 | // The call isn't officially disconnected until the call service confirms that the call |
| 193 | // was actually disconnected. Only then is the association between call and call service |
| 194 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
| 195 | mCallService.disconnect(mId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 199 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 200 | * Answers the call if it is ringing. |
| 201 | */ |
| 202 | void answer() { |
| 203 | Preconditions.checkNotNull(mCallService); |
| 204 | |
| 205 | // Check to verify that the call is still in the ringing state. A call can change states |
| 206 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 207 | if (isRinging("answer")) { |
| 208 | // At this point, we are asking the call service to answer but we don't assume that |
| 209 | // it will work. Instead, we wait until confirmation from the call service that the |
| 210 | // call is in a non-RINGING state before changing the UI. See |
| 211 | // {@link CallServiceAdapter#setActive} and other set* methods. |
| 212 | mCallService.answer(mId); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Rejects the call if it is ringing. |
| 218 | */ |
| 219 | void reject() { |
| 220 | Preconditions.checkNotNull(mCallService); |
| 221 | |
| 222 | // Check to verify that the call is still in the ringing state. A call can change states |
| 223 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 224 | if (isRinging("reject")) { |
| 225 | mCallService.reject(mId); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 230 | * @return An object containing read-only information about this call. |
| 231 | */ |
| 232 | CallInfo toCallInfo() { |
| 233 | if (mCallInfo == null) { |
| 234 | mCallInfo = new CallInfo(mId, mState, mHandle); |
| 235 | } |
| 236 | return mCallInfo; |
| 237 | } |
| 238 | |
| 239 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 240 | * @return True if the call is ringing, else logs the action name. |
| 241 | */ |
| 242 | private boolean isRinging(String actionName) { |
| 243 | if (mState == CallState.RINGING) { |
| 244 | return true; |
| 245 | } |
| 246 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 247 | 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] | 248 | return false; |
| 249 | } |
| 250 | |
| 251 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 252 | * Resets the cached read-only version of this call. |
| 253 | */ |
| 254 | private void clearCallInfo() { |
| 255 | mCallInfo = null; |
| 256 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 257 | |
| 258 | @SuppressWarnings("rawtypes") |
| 259 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 260 | if (binder != null) { |
| 261 | binder.decrementAssociatedCallCount(); |
| 262 | } |
| 263 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 264 | } |