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; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 21 | import android.telecomm.CallInfo; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 22 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 23 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 24 | import android.telecomm.GatewayInfo; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 25 | import android.telecomm.TelecommConstants; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 26 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 27 | import android.telephony.PhoneNumberUtils; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 28 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 29 | import com.google.android.collect.Sets; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 30 | import com.google.common.base.Preconditions; |
| 31 | |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 32 | import java.util.Locale; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 33 | import java.util.Set; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 34 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Encapsulates all aspects of a given phone call throughout its lifecycle, starting |
| 37 | * from the time the call intent was received by Telecomm (vs. the time the call was |
| 38 | * connected etc). |
| 39 | */ |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 40 | final class Call { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Listener for events on the call. |
| 44 | */ |
| 45 | interface Listener { |
| 46 | void onSuccessfulOutgoingCall(Call call); |
| 47 | void onFailedOutgoingCall(Call call, boolean isAborted); |
| 48 | void onSuccessfulIncomingCall(Call call, CallInfo callInfo); |
| 49 | void onFailedIncomingCall(Call call); |
| 50 | } |
| 51 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 52 | /** Additional contact information beyond handle above, optional. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 53 | private final ContactInfo mContactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 54 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 55 | /** True if this is an incoming call. */ |
| 56 | private final boolean mIsIncoming; |
| 57 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 58 | /** |
| 59 | * The time this call was created, typically also the time this call was added to the set |
| 60 | * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard. |
| 61 | * Beyond logging and such, may also be used for bookkeeping and specifically for marking |
| 62 | * certain call attempts as failed attempts. |
| 63 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 64 | private final long mCreationTimeMillis = System.currentTimeMillis(); |
| 65 | |
| 66 | private long mConnectTimeMillis; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 67 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 68 | /** The state of the call. */ |
| 69 | private CallState mState; |
| 70 | |
| 71 | /** The handle with which to establish this call. */ |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 72 | private Uri mHandle; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 73 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 74 | /** The gateway information associated with this call. This stores the original call handle |
| 75 | * that the user is attempting to connect to via the gateway, the actual handle to dial in |
| 76 | * order to connect the call via the gateway, as well as the package name of the gateway |
| 77 | * service. */ |
| 78 | private final GatewayInfo mGatewayInfo; |
| 79 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 80 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 81 | * The call service which is attempted or already connecting this call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 82 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 83 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 84 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 85 | /** |
| 86 | * The call-service selector for this call. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 87 | */ |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 88 | private CallServiceSelectorWrapper mCallServiceSelector; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 89 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 90 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 91 | * The set of call services that were attempted in the process of placing/switching this call |
| 92 | * but turned out unsuitable. Only used in the context of call switching. |
| 93 | */ |
| 94 | private Set<CallServiceWrapper> mIncompatibleCallServices; |
| 95 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 96 | private boolean mIsEmergencyCall; |
| 97 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 98 | /** |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 99 | * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED. |
| 100 | * See {@link android.telephony.DisconnectCause}. |
| 101 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 102 | private int mDisconnectCause = DisconnectCause.NOT_VALID; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Additional disconnect information provided by the call service. |
| 106 | */ |
| 107 | private String mDisconnectMessage; |
| 108 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 109 | /** Info used by the call services. */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 110 | private Bundle mExtras = Bundle.EMPTY; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 111 | |
| 112 | /** The Uri to dial to perform the handoff. If this is null then handoff is not supported. */ |
| 113 | private Uri mHandoffHandle; |
| 114 | |
| 115 | /** |
| 116 | * References the call that is being handed off. This value is non-null for untracked calls |
| 117 | * that are being used to perform a handoff. |
| 118 | */ |
| 119 | private Call mOriginalCall; |
| 120 | |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 121 | /** |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 122 | * The descriptor for the call service that this call is being switched to, null if handoff is |
| 123 | * not in progress. |
| 124 | */ |
| 125 | private CallServiceDescriptor mHandoffCallServiceDescriptor; |
| 126 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 127 | /** Set of listeners on this call. */ |
| 128 | private Set<Listener> mListeners = Sets.newHashSet(); |
| 129 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 130 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 131 | * Creates an empty call object. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 132 | * |
| 133 | * @param isIncoming True if this is an incoming call. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 134 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 135 | Call(boolean isIncoming) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 136 | this(null, null, null, isIncoming); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 140 | * Persists the specified parameters and initializes the new instance. |
| 141 | * |
| 142 | * @param handle The handle to dial. |
| 143 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 144 | * @param gatewayInfo Gateway information to use for the call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 145 | * @param isIncoming True if this is an incoming call. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 146 | */ |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 147 | Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) { |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 148 | mState = CallState.NEW; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 149 | setHandle(handle); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 150 | mContactInfo = contactInfo; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 151 | mGatewayInfo = gatewayInfo; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 152 | mIsIncoming = isIncoming; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 155 | void addListener(Listener listener) { |
| 156 | mListeners.add(listener); |
| 157 | } |
| 158 | |
| 159 | void removeListener(Listener listener) { |
| 160 | mListeners.remove(listener); |
| 161 | } |
| 162 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 163 | /** {@inheritDoc} */ |
| 164 | @Override public String toString() { |
Sailesh Nepal | 4538f01 | 2014-04-15 11:40:33 -0700 | [diff] [blame] | 165 | String component = null; |
| 166 | if (mCallService != null && mCallService.getComponentName() != null) { |
| 167 | component = mCallService.getComponentName().flattenToShortString(); |
| 168 | } |
| 169 | return String.format(Locale.US, "[%s, %s, %s]", mState, component, Log.piiHandle(mHandle)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 172 | CallState getState() { |
| 173 | return mState; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 178 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 179 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 180 | * and instead keep the code resilient to unexpected state changes. |
| 181 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 182 | void setState(CallState newState) { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 183 | Preconditions.checkState(newState != CallState.DISCONNECTED || |
| 184 | mDisconnectCause != DisconnectCause.NOT_VALID); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 185 | if (mState != newState) { |
| 186 | Log.v(this, "setState %s -> %s", mState, newState); |
| 187 | mState = newState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 188 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 191 | Uri getHandle() { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 192 | return mHandle; |
| 193 | } |
| 194 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 195 | void setHandle(Uri handle) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 196 | mHandle = handle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 197 | mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber( |
| 198 | mHandle.getSchemeSpecificPart(), TelecommApp.getInstance()); |
| 199 | } |
| 200 | |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 201 | /** |
| 202 | * @param disconnectCause The reason for the disconnection, any of |
| 203 | * {@link android.telephony.DisconnectCause}. |
| 204 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
| 205 | */ |
| 206 | void setDisconnectCause(int disconnectCause, String disconnectMessage) { |
| 207 | // TODO: Consider combining this method with a setDisconnected() method that is totally |
| 208 | // separate from setState. |
| 209 | mDisconnectCause = disconnectCause; |
| 210 | mDisconnectMessage = disconnectMessage; |
| 211 | } |
| 212 | |
| 213 | int getDisconnectCause() { |
| 214 | return mDisconnectCause; |
| 215 | } |
| 216 | |
| 217 | String getDisconnectMessage() { |
| 218 | return mDisconnectMessage; |
| 219 | } |
| 220 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 221 | boolean isEmergencyCall() { |
| 222 | return mIsEmergencyCall; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 225 | /** |
| 226 | * @return The original handle this call is associated with. In-call services should use this |
| 227 | * handle when indicating in their UI the handle that is being called. |
| 228 | */ |
| 229 | public Uri getOriginalHandle() { |
| 230 | if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) { |
| 231 | return mGatewayInfo.getOriginalHandle(); |
| 232 | } |
| 233 | return getHandle(); |
| 234 | } |
| 235 | |
| 236 | GatewayInfo getGatewayInfo() { |
| 237 | return mGatewayInfo; |
| 238 | } |
| 239 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 240 | ContactInfo getContactInfo() { |
| 241 | return mContactInfo; |
| 242 | } |
| 243 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 244 | boolean isIncoming() { |
| 245 | return mIsIncoming; |
| 246 | } |
| 247 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 248 | /** |
| 249 | * @return The "age" of this call object in milliseconds, which typically also represents the |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 250 | * period since this call was added to the set pending outgoing calls, see |
| 251 | * mCreationTimeMillis. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 252 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 253 | long getAgeMillis() { |
| 254 | return System.currentTimeMillis() - mCreationTimeMillis; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 255 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 256 | |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 257 | /** |
| 258 | * @return The time when this call object was created and added to the set of pending outgoing |
| 259 | * calls. |
| 260 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 261 | long getCreationTimeMillis() { |
| 262 | return mCreationTimeMillis; |
| 263 | } |
| 264 | |
| 265 | long getConnectTimeMillis() { |
| 266 | return mConnectTimeMillis; |
| 267 | } |
| 268 | |
| 269 | void setConnectTimeMillis(long connectTimeMillis) { |
| 270 | mConnectTimeMillis = connectTimeMillis; |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 273 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 274 | return mCallService; |
| 275 | } |
| 276 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 277 | void setCallService(CallServiceWrapper callService) { |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 278 | setCallService(callService, null); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Changes the call service this call is associated with. If callToReplace is non-null then this |
| 283 | * call takes its place within the call service. |
| 284 | */ |
| 285 | void setCallService(CallServiceWrapper callService, Call callToReplace) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 286 | Preconditions.checkNotNull(callService); |
| 287 | |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 288 | clearCallService(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 289 | |
| 290 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 291 | mCallService = callService; |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 292 | if (callToReplace == null) { |
| 293 | mCallService.addCall(this); |
| 294 | } else { |
| 295 | mCallService.replaceCall(this, callToReplace); |
| 296 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Clears the associated call service. |
| 301 | */ |
| 302 | void clearCallService() { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 303 | if (mCallService != null) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 304 | CallServiceWrapper callServiceTemp = mCallService; |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 305 | mCallService = null; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 306 | callServiceTemp.removeCall(this); |
| 307 | |
| 308 | // Decrementing the count can cause the service to unbind, which itself can trigger the |
| 309 | // service-death code. Since the service death code tries to clean up any associated |
| 310 | // calls, we need to make sure to remove that information (e.g., removeCall()) before |
| 311 | // we decrement. Technically, invoking removeCall() prior to decrementing is all that is |
| 312 | // necessary, but cleaning up mCallService prior to triggering an unbind is good to do. |
| 313 | // If you change this, make sure to update {@link clearCallServiceSelector} as well. |
| 314 | decrementAssociatedCallCount(callServiceTemp); |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 315 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 318 | CallServiceSelectorWrapper getCallServiceSelector() { |
| 319 | return mCallServiceSelector; |
| 320 | } |
| 321 | |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 322 | void setCallServiceSelector(CallServiceSelectorWrapper selector) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 323 | Preconditions.checkNotNull(selector); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 324 | |
| 325 | clearCallServiceSelector(); |
| 326 | |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 327 | selector.incrementAssociatedCallCount(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 328 | mCallServiceSelector = selector; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 329 | mCallServiceSelector.addCall(this); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void clearCallServiceSelector() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 333 | if (mCallServiceSelector != null) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 334 | CallServiceSelectorWrapper selectorTemp = mCallServiceSelector; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 335 | mCallServiceSelector = null; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 336 | selectorTemp.removeCall(this); |
| 337 | |
| 338 | // See comment on {@link #clearCallService}. |
| 339 | decrementAssociatedCallCount(selectorTemp); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 340 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 344 | * Starts the incoming call flow through the switchboard. When switchboard completes, it will |
| 345 | * invoke handle[Un]SuccessfulIncomingCall. |
| 346 | * |
| 347 | * @param descriptor The relevant call-service descriptor. |
| 348 | * @param extras The optional extras passed via |
| 349 | * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}. |
| 350 | */ |
| 351 | void startIncoming(CallServiceDescriptor descriptor, Bundle extras) { |
| 352 | Switchboard.getInstance().retrieveIncomingCall(this, descriptor, extras); |
| 353 | } |
| 354 | |
| 355 | void handleSuccessfulIncoming(CallInfo callInfo) { |
| 356 | Preconditions.checkState(callInfo.getState() == CallState.RINGING); |
| 357 | setHandle(callInfo.getHandle()); |
| 358 | |
| 359 | // TODO(santoscordon): Make this class (not CallsManager) responsible for changing the call |
| 360 | // state to RINGING. |
| 361 | |
| 362 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 363 | for (Listener l : mListeners) { |
| 364 | l.onSuccessfulIncomingCall(this, callInfo); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void handleFailedIncoming() { |
| 369 | clearCallService(); |
| 370 | |
| 371 | // TODO: Needs more specific disconnect error for this case. |
| 372 | setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null); |
| 373 | setState(CallState.DISCONNECTED); |
| 374 | |
| 375 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 376 | for (Listener l : mListeners) { |
| 377 | l.onFailedIncomingCall(this); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Starts the outgoing call flow through the switchboard. When switchboard completes, it will |
| 383 | * invoke handleSuccessful/FailedOutgoingCall. |
| 384 | */ |
| 385 | void startOutgoing() { |
| 386 | Switchboard.getInstance().placeOutgoingCall(this); |
| 387 | } |
| 388 | |
| 389 | void handleSuccessfulOutgoing() { |
| 390 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 391 | for (Listener l : mListeners) { |
| 392 | l.onSuccessfulOutgoingCall(this); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void handleFailedOutgoing(boolean isAborted) { |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 397 | if (isAborted) { |
| 398 | finalizeAbort(); |
| 399 | } |
| 400 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 401 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 402 | for (Listener l : mListeners) { |
| 403 | l.onFailedOutgoingCall(this, isAborted); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 408 | * Adds the specified call service to the list of incompatible services. The set is used when |
| 409 | * attempting to switch a phone call between call services such that incompatible services can |
| 410 | * be avoided. |
| 411 | * |
| 412 | * @param callService The incompatible call service. |
| 413 | */ |
| 414 | void addIncompatibleCallService(CallServiceWrapper callService) { |
| 415 | if (mIncompatibleCallServices == null) { |
| 416 | mIncompatibleCallServices = Sets.newHashSet(); |
| 417 | } |
| 418 | mIncompatibleCallServices.add(callService); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Checks whether or not the specified callService was identified as incompatible in the |
| 423 | * context of this call. |
| 424 | * |
| 425 | * @param callService The call service to evaluate. |
| 426 | * @return True upon incompatible call services and false otherwise. |
| 427 | */ |
| 428 | boolean isIncompatibleCallService(CallServiceWrapper callService) { |
| 429 | return mIncompatibleCallServices != null && |
| 430 | mIncompatibleCallServices.contains(callService); |
| 431 | } |
| 432 | |
| 433 | /** |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 434 | * Issues an abort signal to the associated call service and clears the current call service |
| 435 | * and call-service selector. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 436 | */ |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 437 | void finalizeAbort() { |
| 438 | Preconditions.checkState(mState == CallState.NEW); |
| 439 | |
| 440 | if (mCallService != null) { |
| 441 | mCallService.abort(this); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 442 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 443 | clearCallService(); |
| 444 | clearCallServiceSelector(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 447 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 448 | * Plays the specified DTMF tone. |
| 449 | */ |
| 450 | void playDtmfTone(char digit) { |
| 451 | if (mCallService == null) { |
| 452 | Log.w(this, "playDtmfTone() request on a call without a call service."); |
| 453 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 454 | Log.i(this, "Send playDtmfTone to call service for call %s", this); |
| 455 | mCallService.playDtmfTone(this, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Stops playing any currently playing DTMF tone. |
| 461 | */ |
| 462 | void stopDtmfTone() { |
| 463 | if (mCallService == null) { |
| 464 | Log.w(this, "stopDtmfTone() request on a call without a call service."); |
| 465 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 466 | Log.i(this, "Send stopDtmfTone to call service for call %s", this); |
| 467 | mCallService.stopDtmfTone(this); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 472 | * Attempts to disconnect the call through the call service. |
| 473 | */ |
| 474 | void disconnect() { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 475 | if (mState == CallState.NEW) { |
| 476 | // There is a very strange indirection here. When we are told to disconnect, we issue |
| 477 | // an 'abort' to the switchboard if we are still in the NEW (or "connecting") state. |
| 478 | // The switchboard will then cancel the outgoing call process and ask this class to |
| 479 | // finalize the abort procedure via {@link #finalizeAbort}. The issue is that |
| 480 | // Switchboard edits the state of the call as part of the process and then this class |
| 481 | // is responsible for undoing it, and that is all kinds of backwards. |
| 482 | // TODO(santoscordon): Remove Switchboard's requirement to edit the state of the Call |
| 483 | // objects and remove any multi-class shared state of incoming and outgoing call |
| 484 | // processing. |
| 485 | Switchboard.getInstance().abortCall(this); |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame^] | 486 | } else if (mState != CallState.ABORTED && mState != CallState.DISCONNECTED) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 487 | Preconditions.checkNotNull(mCallService); |
| 488 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 489 | Log.i(this, "Send disconnect to call service for call: %s", this); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 490 | // The call isn't officially disconnected until the call service confirms that the call |
| 491 | // was actually disconnected. Only then is the association between call and call service |
| 492 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 493 | mCallService.disconnect(this); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 497 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 498 | * Answers the call if it is ringing. |
| 499 | */ |
| 500 | void answer() { |
| 501 | Preconditions.checkNotNull(mCallService); |
| 502 | |
| 503 | // Check to verify that the call is still in the ringing state. A call can change states |
| 504 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 505 | if (isRinging("answer")) { |
| 506 | // At this point, we are asking the call service to answer but we don't assume that |
| 507 | // it will work. Instead, we wait until confirmation from the call service that the |
| 508 | // call is in a non-RINGING state before changing the UI. See |
| 509 | // {@link CallServiceAdapter#setActive} and other set* methods. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 510 | mCallService.answer(this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Rejects the call if it is ringing. |
| 516 | */ |
| 517 | void reject() { |
| 518 | Preconditions.checkNotNull(mCallService); |
| 519 | |
| 520 | // Check to verify that the call is still in the ringing state. A call can change states |
| 521 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 522 | if (isRinging("reject")) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 523 | mCallService.reject(this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 528 | * Puts the call on hold if it is currently active. |
| 529 | */ |
| 530 | void hold() { |
| 531 | Preconditions.checkNotNull(mCallService); |
| 532 | |
| 533 | if (mState == CallState.ACTIVE) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 534 | mCallService.hold(this); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Releases the call from hold if it is currently active. |
| 540 | */ |
| 541 | void unhold() { |
| 542 | Preconditions.checkNotNull(mCallService); |
| 543 | |
| 544 | if (mState == CallState.ON_HOLD) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 545 | mCallService.unhold(this); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 550 | * @return An object containing read-only information about this call. |
| 551 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 552 | CallInfo toCallInfo(String callId) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 553 | CallServiceDescriptor descriptor = null; |
| 554 | if (mCallService != null) { |
| 555 | descriptor = mCallService.getDescriptor(); |
| 556 | } else if (mOriginalCall != null && mOriginalCall.mCallService != null) { |
| 557 | descriptor = mOriginalCall.mCallService.getDescriptor(); |
| 558 | } |
| 559 | return new CallInfo(callId, mState, mHandle, mGatewayInfo, mExtras, descriptor); |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 562 | /** Checks if this is a live call or not. */ |
| 563 | boolean isAlive() { |
| 564 | switch (mState) { |
| 565 | case NEW: |
| 566 | case RINGING: |
| 567 | case DISCONNECTED: |
| 568 | case ABORTED: |
| 569 | return false; |
| 570 | default: |
| 571 | return true; |
| 572 | } |
| 573 | } |
| 574 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 575 | boolean isActive() { |
| 576 | switch (mState) { |
| 577 | case ACTIVE: |
| 578 | case POST_DIAL: |
| 579 | case POST_DIAL_WAIT: |
| 580 | return true; |
| 581 | default: |
| 582 | return false; |
| 583 | } |
| 584 | } |
| 585 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 586 | Bundle getExtras() { |
| 587 | return mExtras; |
| 588 | } |
| 589 | |
| 590 | void setExtras(Bundle extras) { |
| 591 | mExtras = extras; |
| 592 | } |
| 593 | |
| 594 | Uri getHandoffHandle() { |
| 595 | return mHandoffHandle; |
| 596 | } |
| 597 | |
| 598 | void setHandoffHandle(Uri handoffHandle) { |
| 599 | mHandoffHandle = handoffHandle; |
| 600 | } |
| 601 | |
| 602 | Call getOriginalCall() { |
| 603 | return mOriginalCall; |
| 604 | } |
| 605 | |
| 606 | void setOriginalCall(Call originalCall) { |
| 607 | mOriginalCall = originalCall; |
| 608 | } |
| 609 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 610 | CallServiceDescriptor getHandoffCallServiceDescriptor() { |
| 611 | return mHandoffCallServiceDescriptor; |
| 612 | } |
| 613 | |
| 614 | void setHandoffCallServiceDescriptor(CallServiceDescriptor descriptor) { |
| 615 | mHandoffCallServiceDescriptor = descriptor; |
| 616 | } |
| 617 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 618 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 619 | * @return True if the call is ringing, else logs the action name. |
| 620 | */ |
| 621 | private boolean isRinging(String actionName) { |
| 622 | if (mState == CallState.RINGING) { |
| 623 | return true; |
| 624 | } |
| 625 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 626 | 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] | 627 | return false; |
| 628 | } |
| 629 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 630 | @SuppressWarnings("rawtypes") |
| 631 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 632 | if (binder != null) { |
| 633 | binder.decrementAssociatedCallCount(); |
| 634 | } |
| 635 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 636 | } |