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 | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 23 | |
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; |
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 | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 35 | private static final String TAG = Call.class.getSimpleName(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 36 | |
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 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 72 | * Creates an empty call object with a unique call ID. |
| 73 | */ |
| 74 | Call() { |
| 75 | this(null, null); |
| 76 | } |
| 77 | |
| 78 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 79 | * Persists the specified parameters and initializes the new instance. |
| 80 | * |
| 81 | * @param handle The handle to dial. |
| 82 | * @param contactInfo Information about the entity being called. |
| 83 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 84 | Call(String handle, ContactInfo contactInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 85 | mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness. |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 86 | mState = CallState.NEW; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 87 | mHandle = handle; |
| 88 | mContactInfo = contactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 89 | mCreationTime = new Date(); |
| 90 | } |
| 91 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 92 | /** {@inheritDoc} */ |
| 93 | @Override public String toString() { |
| 94 | return "[" + mId + ", " + mState + ", " + mCallService.getComponentName() + "]"; |
| 95 | } |
| 96 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 97 | String getId() { |
| 98 | return mId; |
| 99 | } |
| 100 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 101 | CallState getState() { |
| 102 | return mState; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 107 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 108 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 109 | * and instead keep the code resilient to unexpected state changes. |
| 110 | */ |
| 111 | void setState(CallState state) { |
| 112 | mState = state; |
| 113 | clearCallInfo(); |
| 114 | } |
| 115 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 116 | String getHandle() { |
| 117 | return mHandle; |
| 118 | } |
| 119 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 120 | void setHandle(String handle) { |
| 121 | mHandle = handle; |
| 122 | } |
| 123 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 124 | ContactInfo getContactInfo() { |
| 125 | return mContactInfo; |
| 126 | } |
| 127 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 128 | /** |
| 129 | * @return The "age" of this call object in milliseconds, which typically also represents the |
| 130 | * period since this call was added to the set pending outgoing calls, see mCreationTime. |
| 131 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 132 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 133 | return new Date().getTime() - mCreationTime.getTime(); |
| 134 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 135 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 136 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 137 | return mCallService; |
| 138 | } |
| 139 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 140 | void setCallService(CallServiceWrapper callService) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 141 | Preconditions.checkNotNull(callService); |
| 142 | |
| 143 | if (mCallService != null) { |
| 144 | // Should never be the case, basically covering for potential programming errors. |
| 145 | decrementAssociatedCallCount(mCallService); |
| 146 | } |
| 147 | |
| 148 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 149 | mCallService = callService; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Clears the associated call service. |
| 154 | */ |
| 155 | void clearCallService() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 156 | decrementAssociatedCallCount(mCallService); |
| 157 | mCallService = null; |
| 158 | } |
| 159 | |
| 160 | void setCallServiceSelector(ICallServiceSelector selector) { |
| 161 | Preconditions.checkNotNull(selector); |
| 162 | mCallServiceSelector = selector; |
| 163 | } |
| 164 | |
| 165 | void clearCallServiceSelector() { |
| 166 | mCallServiceSelector = null; |
| 167 | |
| 168 | // TODO(gilad): Un-comment once selectors are converted into wrappers. |
| 169 | // decrementAssociatedCallCount(mCallServiceSelector); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Aborts ongoing attempts to connect this call. No-op once the call is connected or has been |
| 174 | * disconnected. See {@link #disconnect} for already-connected calls. |
| 175 | */ |
| 176 | void abort() { |
| 177 | if (mState == CallState.NEW || |
| 178 | mState == CallState.DIALING || |
| 179 | mState == CallState.RINGING) { |
| 180 | |
| 181 | // TODO(gilad): Add CallState.ABORTED and set it here. |
| 182 | // mState = CallState.ABORTED; |
| 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) { |
| 191 | Log.w(TAG, "disconnect() request on a call without a call service."); |
| 192 | } else { |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 193 | Log.i(TAG, "Send disconnect to call service for call with id " + mId); |
| 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 | |
| 249 | Log.i(TAG, "Request to " + actionName + " a non-ringing call " + this); |
| 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 | } |