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 | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 19 | import android.os.RemoteException; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 20 | import android.telecomm.CallInfo; |
| 21 | import android.telecomm.CallState; |
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 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 58 | * The call service which is currently connecting this call, null as long as the call is not |
| 59 | * connected. |
| 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 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 63 | /** Read-only and parcelable version of this call. */ |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 64 | private CallInfo mCallInfo; |
| 65 | |
| 66 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 67 | * Persists the specified parameters and initializes the new instance. |
| 68 | * |
| 69 | * @param handle The handle to dial. |
| 70 | * @param contactInfo Information about the entity being called. |
| 71 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 72 | Call(String handle, ContactInfo contactInfo) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 73 | mId = UUID.randomUUID().toString(); // UUIDs should provide sufficient uniqueness. |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 74 | mState = CallState.NEW; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 75 | mHandle = handle; |
| 76 | mContactInfo = contactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 77 | mCreationTime = new Date(); |
| 78 | } |
| 79 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 80 | /** {@inheritDoc} */ |
| 81 | @Override public String toString() { |
| 82 | return "[" + mId + ", " + mState + ", " + mCallService.getComponentName() + "]"; |
| 83 | } |
| 84 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 85 | String getId() { |
| 86 | return mId; |
| 87 | } |
| 88 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 89 | CallState getState() { |
| 90 | return mState; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 95 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 96 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 97 | * and instead keep the code resilient to unexpected state changes. |
| 98 | */ |
| 99 | void setState(CallState state) { |
| 100 | mState = state; |
| 101 | clearCallInfo(); |
| 102 | } |
| 103 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 104 | String getHandle() { |
| 105 | return mHandle; |
| 106 | } |
| 107 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 108 | void setHandle(String handle) { |
| 109 | mHandle = handle; |
| 110 | } |
| 111 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 112 | ContactInfo getContactInfo() { |
| 113 | return mContactInfo; |
| 114 | } |
| 115 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 116 | /** |
| 117 | * @return The "age" of this call object in milliseconds, which typically also represents the |
| 118 | * period since this call was added to the set pending outgoing calls, see mCreationTime. |
| 119 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 120 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 121 | return new Date().getTime() - mCreationTime.getTime(); |
| 122 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 123 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 124 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 125 | return mCallService; |
| 126 | } |
| 127 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 128 | void setCallService(CallServiceWrapper callService) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 129 | mCallService = callService; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Clears the associated call service. |
| 134 | */ |
| 135 | void clearCallService() { |
| 136 | setCallService(null); |
| 137 | } |
| 138 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 139 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 140 | * Attempts to disconnect the call through the call service. |
| 141 | */ |
| 142 | void disconnect() { |
| 143 | if (mCallService == null) { |
| 144 | Log.w(TAG, "disconnect() request on a call without a call service."); |
| 145 | } else { |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 146 | Log.i(TAG, "Send disconnect to call service for call with id " + mId); |
| 147 | // The call isn't officially disconnected until the call service confirms that the call |
| 148 | // was actually disconnected. Only then is the association between call and call service |
| 149 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
| 150 | mCallService.disconnect(mId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 154 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 155 | * Answers the call if it is ringing. |
| 156 | */ |
| 157 | void answer() { |
| 158 | Preconditions.checkNotNull(mCallService); |
| 159 | |
| 160 | // Check to verify that the call is still in the ringing state. A call can change states |
| 161 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 162 | if (isRinging("answer")) { |
| 163 | // At this point, we are asking the call service to answer but we don't assume that |
| 164 | // it will work. Instead, we wait until confirmation from the call service that the |
| 165 | // call is in a non-RINGING state before changing the UI. See |
| 166 | // {@link CallServiceAdapter#setActive} and other set* methods. |
| 167 | mCallService.answer(mId); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Rejects the call if it is ringing. |
| 173 | */ |
| 174 | void reject() { |
| 175 | Preconditions.checkNotNull(mCallService); |
| 176 | |
| 177 | // Check to verify that the call is still in the ringing state. A call can change states |
| 178 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 179 | if (isRinging("reject")) { |
| 180 | mCallService.reject(mId); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 185 | * @return An object containing read-only information about this call. |
| 186 | */ |
| 187 | CallInfo toCallInfo() { |
| 188 | if (mCallInfo == null) { |
| 189 | mCallInfo = new CallInfo(mId, mState, mHandle); |
| 190 | } |
| 191 | return mCallInfo; |
| 192 | } |
| 193 | |
| 194 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 195 | * @return True if the call is ringing, else logs the action name. |
| 196 | */ |
| 197 | private boolean isRinging(String actionName) { |
| 198 | if (mState == CallState.RINGING) { |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | Log.i(TAG, "Request to " + actionName + " a non-ringing call " + this); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 207 | * Resets the cached read-only version of this call. |
| 208 | */ |
| 209 | private void clearCallInfo() { |
| 210 | mCallInfo = null; |
| 211 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 212 | } |