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