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; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 20 | import android.telecomm.CallInfo; |
| 21 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 22 | import android.telecomm.GatewayInfo; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 23 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 24 | import android.telephony.PhoneNumberUtils; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 25 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 26 | import com.google.android.collect.Sets; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 27 | import com.google.common.base.Preconditions; |
| 28 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 29 | import java.util.Date; |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 30 | import java.util.Locale; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 31 | import java.util.Set; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 32 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Encapsulates all aspects of a given phone call throughout its lifecycle, starting |
| 35 | * from the time the call intent was received by Telecomm (vs. the time the call was |
| 36 | * connected etc). |
| 37 | */ |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 38 | final class Call { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 39 | /** Additional contact information beyond handle above, optional. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 40 | private final ContactInfo mContactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 41 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 42 | /** True if this is an incoming call. */ |
| 43 | private final boolean mIsIncoming; |
| 44 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 45 | /** |
| 46 | * The time this call was created, typically also the time this call was added to the set |
| 47 | * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard. |
| 48 | * Beyond logging and such, may also be used for bookkeeping and specifically for marking |
| 49 | * certain call attempts as failed attempts. |
| 50 | */ |
| 51 | private final Date mCreationTime; |
| 52 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 53 | /** The state of the call. */ |
| 54 | private CallState mState; |
| 55 | |
| 56 | /** The handle with which to establish this call. */ |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 57 | private Uri mHandle; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 58 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 59 | /** The gateway information associated with this call. This stores the original call handle |
| 60 | * that the user is attempting to connect to via the gateway, the actual handle to dial in |
| 61 | * order to connect the call via the gateway, as well as the package name of the gateway |
| 62 | * service. */ |
| 63 | private final GatewayInfo mGatewayInfo; |
| 64 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 65 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 66 | * The call service which is attempted or already connecting this call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 67 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 68 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 69 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 70 | /** |
| 71 | * The call-service selector for this call. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 72 | */ |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 73 | private CallServiceSelectorWrapper mCallServiceSelector; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 74 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 75 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 76 | * The set of call services that were attempted in the process of placing/switching this call |
| 77 | * but turned out unsuitable. Only used in the context of call switching. |
| 78 | */ |
| 79 | private Set<CallServiceWrapper> mIncompatibleCallServices; |
| 80 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 81 | private boolean mIsEmergencyCall; |
| 82 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 83 | /** |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 84 | * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED. |
| 85 | * See {@link android.telephony.DisconnectCause}. |
| 86 | */ |
| 87 | private int mDisconnectCause; |
| 88 | |
| 89 | /** |
| 90 | * Additional disconnect information provided by the call service. |
| 91 | */ |
| 92 | private String mDisconnectMessage; |
| 93 | |
| 94 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 95 | * Creates an empty call object. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 96 | * |
| 97 | * @param isIncoming True if this is an incoming call. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 98 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 99 | Call(boolean isIncoming) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 100 | this(null, null, null, isIncoming); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 104 | * Persists the specified parameters and initializes the new instance. |
| 105 | * |
| 106 | * @param handle The handle to dial. |
| 107 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 108 | * @param gatewayInfo Gateway information to use for the call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 109 | * @param isIncoming True if this is an incoming call. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 110 | */ |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 111 | Call(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, boolean isIncoming) { |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 112 | mState = CallState.NEW; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 113 | setHandle(handle); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 114 | mContactInfo = contactInfo; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 115 | mGatewayInfo = gatewayInfo; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 116 | mIsIncoming = isIncoming; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 117 | mCreationTime = new Date(); |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 118 | mDisconnectCause = DisconnectCause.NOT_VALID; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 121 | /** {@inheritDoc} */ |
| 122 | @Override public String toString() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 123 | return String.format(Locale.US, "[%s, %s, %s]", mState, |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 124 | mCallService == null ? "<null>" : mCallService.getComponentName(), |
| 125 | Log.pii(mHandle)); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 128 | CallState getState() { |
| 129 | return mState; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 134 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 135 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 136 | * and instead keep the code resilient to unexpected state changes. |
| 137 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 138 | void setState(CallState newState) { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 139 | Preconditions.checkState(newState != CallState.DISCONNECTED || |
| 140 | mDisconnectCause != DisconnectCause.NOT_VALID); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 141 | if (mState != newState) { |
| 142 | Log.v(this, "setState %s -> %s", mState, newState); |
| 143 | mState = newState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 144 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 147 | Uri getHandle() { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 148 | return mHandle; |
| 149 | } |
| 150 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 151 | void setHandle(Uri handle) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 152 | mHandle = handle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 153 | mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber( |
| 154 | mHandle.getSchemeSpecificPart(), TelecommApp.getInstance()); |
| 155 | } |
| 156 | |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 157 | /** |
| 158 | * @param disconnectCause The reason for the disconnection, any of |
| 159 | * {@link android.telephony.DisconnectCause}. |
| 160 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
| 161 | */ |
| 162 | void setDisconnectCause(int disconnectCause, String disconnectMessage) { |
| 163 | // TODO: Consider combining this method with a setDisconnected() method that is totally |
| 164 | // separate from setState. |
| 165 | mDisconnectCause = disconnectCause; |
| 166 | mDisconnectMessage = disconnectMessage; |
| 167 | } |
| 168 | |
| 169 | int getDisconnectCause() { |
| 170 | return mDisconnectCause; |
| 171 | } |
| 172 | |
| 173 | String getDisconnectMessage() { |
| 174 | return mDisconnectMessage; |
| 175 | } |
| 176 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 177 | boolean isEmergencyCall() { |
| 178 | return mIsEmergencyCall; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 181 | /** |
| 182 | * @return The original handle this call is associated with. In-call services should use this |
| 183 | * handle when indicating in their UI the handle that is being called. |
| 184 | */ |
| 185 | public Uri getOriginalHandle() { |
| 186 | if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) { |
| 187 | return mGatewayInfo.getOriginalHandle(); |
| 188 | } |
| 189 | return getHandle(); |
| 190 | } |
| 191 | |
| 192 | GatewayInfo getGatewayInfo() { |
| 193 | return mGatewayInfo; |
| 194 | } |
| 195 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 196 | ContactInfo getContactInfo() { |
| 197 | return mContactInfo; |
| 198 | } |
| 199 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 200 | boolean isIncoming() { |
| 201 | return mIsIncoming; |
| 202 | } |
| 203 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 204 | /** |
| 205 | * @return The "age" of this call object in milliseconds, which typically also represents the |
| 206 | * period since this call was added to the set pending outgoing calls, see mCreationTime. |
| 207 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 208 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 209 | return new Date().getTime() - mCreationTime.getTime(); |
| 210 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 211 | |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 212 | /** |
| 213 | * @return The time when this call object was created and added to the set of pending outgoing |
| 214 | * calls. |
| 215 | */ |
| 216 | long getCreationTimeInMilliseconds() { |
| 217 | return mCreationTime.getTime(); |
| 218 | } |
| 219 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 220 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 221 | return mCallService; |
| 222 | } |
| 223 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 224 | void setCallService(CallServiceWrapper callService) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 225 | Preconditions.checkNotNull(callService); |
| 226 | |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 227 | clearCallService(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 228 | |
| 229 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 230 | mCallService = callService; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 231 | mCallService.addCall(this); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Clears the associated call service. |
| 236 | */ |
| 237 | void clearCallService() { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 238 | if (mCallService != null) { |
| 239 | decrementAssociatedCallCount(mCallService); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 240 | mCallService.removeCall(this); |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 241 | mCallService = null; |
| 242 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 245 | void setCallServiceSelector(CallServiceSelectorWrapper selector) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 246 | Preconditions.checkNotNull(selector); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 247 | |
| 248 | clearCallServiceSelector(); |
| 249 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 250 | mCallServiceSelector = selector; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 251 | mCallServiceSelector.addCall(this); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void clearCallServiceSelector() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 255 | if (mCallServiceSelector != null) { |
| 256 | // TODO(sail): Stop leaking selectors. |
| 257 | // decrementAssociatedCallCount(mCallServiceSelector); |
| 258 | mCallServiceSelector.removeCall(this); |
| 259 | mCallServiceSelector = null; |
| 260 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 264 | * Adds the specified call service to the list of incompatible services. The set is used when |
| 265 | * attempting to switch a phone call between call services such that incompatible services can |
| 266 | * be avoided. |
| 267 | * |
| 268 | * @param callService The incompatible call service. |
| 269 | */ |
| 270 | void addIncompatibleCallService(CallServiceWrapper callService) { |
| 271 | if (mIncompatibleCallServices == null) { |
| 272 | mIncompatibleCallServices = Sets.newHashSet(); |
| 273 | } |
| 274 | mIncompatibleCallServices.add(callService); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Checks whether or not the specified callService was identified as incompatible in the |
| 279 | * context of this call. |
| 280 | * |
| 281 | * @param callService The call service to evaluate. |
| 282 | * @return True upon incompatible call services and false otherwise. |
| 283 | */ |
| 284 | boolean isIncompatibleCallService(CallServiceWrapper callService) { |
| 285 | return mIncompatibleCallServices != null && |
| 286 | mIncompatibleCallServices.contains(callService); |
| 287 | } |
| 288 | |
| 289 | /** |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 290 | * Aborts ongoing attempts to connect this call. Only applicable to {@link CallState#NEW} |
| 291 | * outgoing calls. See {@link #disconnect} for already-connected calls. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 292 | */ |
| 293 | void abort() { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 294 | if (mState == CallState.NEW) { |
| 295 | if (mCallService != null) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 296 | mCallService.abort(this); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 297 | } |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 298 | clearCallService(); |
| 299 | clearCallServiceSelector(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 300 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 303 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 304 | * Plays the specified DTMF tone. |
| 305 | */ |
| 306 | void playDtmfTone(char digit) { |
| 307 | if (mCallService == null) { |
| 308 | Log.w(this, "playDtmfTone() request on a call without a call service."); |
| 309 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 310 | Log.i(this, "Send playDtmfTone to call service for call %s", this); |
| 311 | mCallService.playDtmfTone(this, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Stops playing any currently playing DTMF tone. |
| 317 | */ |
| 318 | void stopDtmfTone() { |
| 319 | if (mCallService == null) { |
| 320 | Log.w(this, "stopDtmfTone() request on a call without a call service."); |
| 321 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 322 | Log.i(this, "Send stopDtmfTone to call service for call %s", this); |
| 323 | mCallService.stopDtmfTone(this); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 328 | * Attempts to disconnect the call through the call service. |
| 329 | */ |
| 330 | void disconnect() { |
| 331 | if (mCallService == null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 332 | Log.w(this, "disconnect() request on a call without a call service."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 333 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 334 | Log.i(this, "Send disconnect to call service for call: %s", this); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 335 | // The call isn't officially disconnected until the call service confirms that the call |
| 336 | // was actually disconnected. Only then is the association between call and call service |
| 337 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 338 | mCallService.disconnect(this); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 342 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 343 | * Answers the call if it is ringing. |
| 344 | */ |
| 345 | void answer() { |
| 346 | Preconditions.checkNotNull(mCallService); |
| 347 | |
| 348 | // Check to verify that the call is still in the ringing state. A call can change states |
| 349 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 350 | if (isRinging("answer")) { |
| 351 | // At this point, we are asking the call service to answer but we don't assume that |
| 352 | // it will work. Instead, we wait until confirmation from the call service that the |
| 353 | // call is in a non-RINGING state before changing the UI. See |
| 354 | // {@link CallServiceAdapter#setActive} and other set* methods. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 355 | mCallService.answer(this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Rejects the call if it is ringing. |
| 361 | */ |
| 362 | void reject() { |
| 363 | Preconditions.checkNotNull(mCallService); |
| 364 | |
| 365 | // Check to verify that the call is still in the ringing state. A call can change states |
| 366 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 367 | if (isRinging("reject")) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 368 | mCallService.reject(this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 373 | * Puts the call on hold if it is currently active. |
| 374 | */ |
| 375 | void hold() { |
| 376 | Preconditions.checkNotNull(mCallService); |
| 377 | |
| 378 | if (mState == CallState.ACTIVE) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 379 | mCallService.hold(this); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Releases the call from hold if it is currently active. |
| 385 | */ |
| 386 | void unhold() { |
| 387 | Preconditions.checkNotNull(mCallService); |
| 388 | |
| 389 | if (mState == CallState.ON_HOLD) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 390 | mCallService.unhold(this); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 395 | * @return An object containing read-only information about this call. |
| 396 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 397 | CallInfo toCallInfo(String callId) { |
| 398 | return new CallInfo(callId, mState, mHandle, mGatewayInfo); |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 401 | /** Checks if this is a live call or not. */ |
| 402 | boolean isAlive() { |
| 403 | switch (mState) { |
| 404 | case NEW: |
| 405 | case RINGING: |
| 406 | case DISCONNECTED: |
| 407 | case ABORTED: |
| 408 | return false; |
| 409 | default: |
| 410 | return true; |
| 411 | } |
| 412 | } |
| 413 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 414 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 415 | * @return True if the call is ringing, else logs the action name. |
| 416 | */ |
| 417 | private boolean isRinging(String actionName) { |
| 418 | if (mState == CallState.RINGING) { |
| 419 | return true; |
| 420 | } |
| 421 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 422 | 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] | 423 | return false; |
| 424 | } |
| 425 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 426 | @SuppressWarnings("rawtypes") |
| 427 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 428 | if (binder != null) { |
| 429 | binder.decrementAssociatedCallCount(); |
| 430 | } |
| 431 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 432 | } |