Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 19 | import android.content.ContentUris; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.drawable.Drawable; |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 22 | import android.net.Uri; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 24 | import android.os.Handler; |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 25 | import android.provider.ContactsContract.Contacts; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 26 | import android.telecomm.CallInfo; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 27 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 28 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 29 | import android.telecomm.GatewayInfo; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 30 | import android.telecomm.Response; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 31 | import android.telecomm.TelecommConstants; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 32 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 33 | import android.telephony.PhoneNumberUtils; |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 35 | |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 36 | import com.android.internal.telephony.CallerInfo; |
| 37 | import com.android.internal.telephony.CallerInfoAsyncQuery; |
| 38 | import com.android.internal.telephony.CallerInfoAsyncQuery.OnQueryCompleteListener; |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 39 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 40 | import com.android.internal.telephony.SmsApplication; |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 41 | import com.android.telecomm.ContactsAsyncHelper.OnImageLoadCompleteListener; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 42 | import com.google.common.base.Preconditions; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 43 | import com.google.common.collect.Sets; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 44 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 45 | import java.util.Collections; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 46 | import java.util.LinkedList; |
| 47 | import java.util.List; |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 48 | import java.util.Locale; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 49 | import java.util.Set; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 50 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Encapsulates all aspects of a given phone call throughout its lifecycle, starting |
| 53 | * from the time the call intent was received by Telecomm (vs. the time the call was |
| 54 | * connected etc). |
| 55 | */ |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 56 | final class Call { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Listener for events on the call. |
| 60 | */ |
| 61 | interface Listener { |
| 62 | void onSuccessfulOutgoingCall(Call call); |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 63 | void onFailedOutgoingCall(Call call, boolean isAborted, int errorCode, String errorMsg); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 64 | void onSuccessfulIncomingCall(Call call, CallInfo callInfo); |
| 65 | void onFailedIncomingCall(Call call); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 66 | void onRequestingRingback(Call call, boolean requestingRingback); |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 67 | void onPostDialWait(Call call, String remaining); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 68 | void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable); |
| 69 | void onExpiredConferenceCall(Call call); |
| 70 | void onConfirmedConferenceCall(Call call); |
| 71 | void onParentChanged(Call call); |
| 72 | void onChildrenChanged(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 73 | void onCannedSmsResponsesLoaded(Call call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 76 | private static final OnQueryCompleteListener sCallerInfoQueryListener = |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 77 | new OnQueryCompleteListener() { |
| 78 | /** ${inheritDoc} */ |
| 79 | @Override |
| 80 | public void onQueryComplete(int token, Object cookie, CallerInfo callerInfo) { |
| 81 | if (cookie != null) { |
| 82 | ((Call) cookie).setCallerInfo(callerInfo, token); |
| 83 | } |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 84 | } |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | private static final OnImageLoadCompleteListener sPhotoLoadListener = |
| 88 | new OnImageLoadCompleteListener() { |
| 89 | /** ${inheritDoc} */ |
| 90 | @Override |
| 91 | public void onImageLoadComplete( |
| 92 | int token, Drawable photo, Bitmap photoIcon, Object cookie) { |
| 93 | if (cookie != null) { |
| 94 | ((Call) cookie).setPhoto(photo, photoIcon, token); |
| 95 | } |
| 96 | } |
| 97 | }; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 98 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 99 | /** True if this is an incoming call. */ |
| 100 | private final boolean mIsIncoming; |
| 101 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 102 | /** |
| 103 | * The time this call was created, typically also the time this call was added to the set |
| 104 | * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard. |
| 105 | * Beyond logging and such, may also be used for bookkeeping and specifically for marking |
| 106 | * certain call attempts as failed attempts. |
| 107 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 108 | private final long mCreationTimeMillis = System.currentTimeMillis(); |
| 109 | |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 110 | /** The gateway information associated with this call. This stores the original call handle |
| 111 | * that the user is attempting to connect to via the gateway, the actual handle to dial in |
| 112 | * order to connect the call via the gateway, as well as the package name of the gateway |
| 113 | * service. */ |
| 114 | private final GatewayInfo mGatewayInfo; |
| 115 | |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 116 | private final Handler mHandler = new Handler(); |
| 117 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 118 | private long mConnectTimeMillis; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 119 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 120 | /** The state of the call. */ |
| 121 | private CallState mState; |
| 122 | |
| 123 | /** The handle with which to establish this call. */ |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 124 | private Uri mHandle; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 125 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 126 | /** |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 127 | * The call service which is attempted or already connecting this call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 128 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 129 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 130 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 131 | /** |
| 132 | * The call-service selector for this call. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 133 | */ |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 134 | private CallServiceSelectorWrapper mCallServiceSelector; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 135 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 136 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 137 | * The set of call services that were attempted in the process of placing/switching this call |
| 138 | * but turned out unsuitable. Only used in the context of call switching. |
| 139 | */ |
| 140 | private Set<CallServiceWrapper> mIncompatibleCallServices; |
| 141 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 142 | private boolean mIsEmergencyCall; |
| 143 | |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 144 | private boolean mSpeakerphoneOn; |
| 145 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 146 | /** |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 147 | * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED. |
| 148 | * See {@link android.telephony.DisconnectCause}. |
| 149 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 150 | private int mDisconnectCause = DisconnectCause.NOT_VALID; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Additional disconnect information provided by the call service. |
| 154 | */ |
| 155 | private String mDisconnectMessage; |
| 156 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 157 | /** Info used by the call services. */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 158 | private Bundle mExtras = Bundle.EMPTY; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 159 | |
| 160 | /** The Uri to dial to perform the handoff. If this is null then handoff is not supported. */ |
| 161 | private Uri mHandoffHandle; |
| 162 | |
| 163 | /** |
| 164 | * References the call that is being handed off. This value is non-null for untracked calls |
| 165 | * that are being used to perform a handoff. |
| 166 | */ |
| 167 | private Call mOriginalCall; |
| 168 | |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 169 | /** |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 170 | * The descriptor for the call service that this call is being switched to, null if handoff is |
| 171 | * not in progress. |
| 172 | */ |
| 173 | private CallServiceDescriptor mHandoffCallServiceDescriptor; |
| 174 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 175 | /** Set of listeners on this call. */ |
| 176 | private Set<Listener> mListeners = Sets.newHashSet(); |
| 177 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 178 | private OutgoingCallProcessor mOutgoingCallProcessor; |
| 179 | |
| 180 | // TODO(santoscordon): The repositories should be changed into singleton types. |
| 181 | private CallServiceRepository mCallServiceRepository; |
| 182 | private CallServiceSelectorRepository mSelectorRepository; |
| 183 | |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 184 | /** Caller information retrieved from the latest contact query. */ |
| 185 | private CallerInfo mCallerInfo; |
| 186 | |
| 187 | /** The latest token used with a contact info query. */ |
| 188 | private int mQueryToken = 0; |
| 189 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 190 | /** Whether this call is requesting that Telecomm play the ringback tone on its behalf. */ |
| 191 | private boolean mRequestingRingback = false; |
| 192 | |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 193 | /** Incoming call-info to use when direct-to-voicemail query finishes. */ |
| 194 | private CallInfo mPendingDirectToVoicemailCallInfo; |
| 195 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 196 | private boolean mIsConferenceCapable = false; |
| 197 | |
| 198 | private boolean mIsConference = false; |
| 199 | |
| 200 | private Call mParentCall = null; |
| 201 | |
| 202 | private List<Call> mChildCalls = new LinkedList<>(); |
| 203 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 204 | /** Set of text message responses allowed for this call, if applicable. */ |
| 205 | private List<String> mCannedSmsResponses = Collections.EMPTY_LIST; |
| 206 | |
| 207 | /** Whether an attempt has been made to load the text message responses. */ |
| 208 | private boolean mCannedSmsResponsesLoadingStarted = false; |
| 209 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 210 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 211 | * Creates an empty call object. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 212 | * |
| 213 | * @param isIncoming True if this is an incoming call. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 214 | */ |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 215 | Call(boolean isIncoming, boolean isConference) { |
| 216 | this(null, null, isIncoming, isConference); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 220 | * Persists the specified parameters and initializes the new instance. |
| 221 | * |
| 222 | * @param handle The handle to dial. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 223 | * @param gatewayInfo Gateway information to use for the call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 224 | * @param isIncoming True if this is an incoming call. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 225 | */ |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 226 | Call(Uri handle, GatewayInfo gatewayInfo, boolean isIncoming, boolean isConference) { |
| 227 | mState = isConference ? CallState.ACTIVE : CallState.NEW; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 228 | setHandle(handle); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 229 | mGatewayInfo = gatewayInfo; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 230 | mIsIncoming = isIncoming; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 231 | mIsConference = isConference; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 232 | maybeLoadCannedSmsResponses(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 235 | void addListener(Listener listener) { |
| 236 | mListeners.add(listener); |
| 237 | } |
| 238 | |
| 239 | void removeListener(Listener listener) { |
| 240 | mListeners.remove(listener); |
| 241 | } |
| 242 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 243 | /** {@inheritDoc} */ |
| 244 | @Override public String toString() { |
Sailesh Nepal | 4538f01 | 2014-04-15 11:40:33 -0700 | [diff] [blame] | 245 | String component = null; |
| 246 | if (mCallService != null && mCallService.getComponentName() != null) { |
| 247 | component = mCallService.getComponentName().flattenToShortString(); |
| 248 | } |
| 249 | 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] | 250 | } |
| 251 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 252 | CallState getState() { |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 253 | if (mIsConference) { |
| 254 | if (!mChildCalls.isEmpty()) { |
| 255 | // If we have child calls, just return the child call. |
| 256 | return mChildCalls.get(0).getState(); |
| 257 | } |
| 258 | return CallState.ACTIVE; |
| 259 | } else { |
| 260 | return mState; |
| 261 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 266 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 267 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 268 | * and instead keep the code resilient to unexpected state changes. |
| 269 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 270 | void setState(CallState newState) { |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 271 | Preconditions.checkState(newState != CallState.DISCONNECTED || |
| 272 | mDisconnectCause != DisconnectCause.NOT_VALID); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 273 | if (mState != newState) { |
| 274 | Log.v(this, "setState %s -> %s", mState, newState); |
| 275 | mState = newState; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 276 | maybeLoadCannedSmsResponses(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 277 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 280 | void setRequestingRingback(boolean requestingRingback) { |
| 281 | mRequestingRingback = requestingRingback; |
| 282 | for (Listener l : mListeners) { |
| 283 | l.onRequestingRingback(this, mRequestingRingback); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | boolean isRequestingRingback() { |
| 288 | return mRequestingRingback; |
| 289 | } |
| 290 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 291 | Uri getHandle() { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 292 | return mHandle; |
| 293 | } |
| 294 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 295 | void setHandle(Uri handle) { |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 296 | if ((mHandle == null && handle != null) || (mHandle != null && !mHandle.equals(handle))) { |
| 297 | mHandle = handle; |
| 298 | mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber( |
Yorke Lee | 6625545 | 2014-06-05 08:09:24 -0700 | [diff] [blame] | 299 | TelecommApp.getInstance(), mHandle.getSchemeSpecificPart()); |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 300 | startCallerInfoLookup(); |
| 301 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 304 | String getName() { |
| 305 | return mCallerInfo == null ? null : mCallerInfo.name; |
| 306 | } |
| 307 | |
| 308 | Bitmap getPhotoIcon() { |
| 309 | return mCallerInfo == null ? null : mCallerInfo.cachedPhotoIcon; |
| 310 | } |
| 311 | |
| 312 | Drawable getPhoto() { |
| 313 | return mCallerInfo == null ? null : mCallerInfo.cachedPhoto; |
| 314 | } |
| 315 | |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 316 | /** |
| 317 | * @param disconnectCause The reason for the disconnection, any of |
| 318 | * {@link android.telephony.DisconnectCause}. |
| 319 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
| 320 | */ |
| 321 | void setDisconnectCause(int disconnectCause, String disconnectMessage) { |
| 322 | // TODO: Consider combining this method with a setDisconnected() method that is totally |
| 323 | // separate from setState. |
| 324 | mDisconnectCause = disconnectCause; |
| 325 | mDisconnectMessage = disconnectMessage; |
| 326 | } |
| 327 | |
| 328 | int getDisconnectCause() { |
| 329 | return mDisconnectCause; |
| 330 | } |
| 331 | |
| 332 | String getDisconnectMessage() { |
| 333 | return mDisconnectMessage; |
| 334 | } |
| 335 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 336 | boolean isEmergencyCall() { |
| 337 | return mIsEmergencyCall; |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 340 | /** |
| 341 | * @return The original handle this call is associated with. In-call services should use this |
| 342 | * handle when indicating in their UI the handle that is being called. |
| 343 | */ |
| 344 | public Uri getOriginalHandle() { |
| 345 | if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) { |
| 346 | return mGatewayInfo.getOriginalHandle(); |
| 347 | } |
| 348 | return getHandle(); |
| 349 | } |
| 350 | |
| 351 | GatewayInfo getGatewayInfo() { |
| 352 | return mGatewayInfo; |
| 353 | } |
| 354 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 355 | boolean isIncoming() { |
| 356 | return mIsIncoming; |
| 357 | } |
| 358 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 359 | /** |
| 360 | * @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] | 361 | * period since this call was added to the set pending outgoing calls, see |
| 362 | * mCreationTimeMillis. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 363 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 364 | long getAgeMillis() { |
| 365 | return System.currentTimeMillis() - mCreationTimeMillis; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 366 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 367 | |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 368 | /** |
| 369 | * @return The time when this call object was created and added to the set of pending outgoing |
| 370 | * calls. |
| 371 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 372 | long getCreationTimeMillis() { |
| 373 | return mCreationTimeMillis; |
| 374 | } |
| 375 | |
| 376 | long getConnectTimeMillis() { |
| 377 | return mConnectTimeMillis; |
| 378 | } |
| 379 | |
| 380 | void setConnectTimeMillis(long connectTimeMillis) { |
| 381 | mConnectTimeMillis = connectTimeMillis; |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 384 | boolean isConferenceCapable() { |
| 385 | return mIsConferenceCapable; |
| 386 | } |
| 387 | |
| 388 | void setIsConferenceCapable(boolean isConferenceCapable) { |
| 389 | if (mIsConferenceCapable != isConferenceCapable) { |
| 390 | mIsConferenceCapable = isConferenceCapable; |
| 391 | for (Listener l : mListeners) { |
| 392 | l.onIsConferenceCapableChanged(this, mIsConferenceCapable); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | Call getParentCall() { |
| 398 | return mParentCall; |
| 399 | } |
| 400 | |
| 401 | List<Call> getChildCalls() { |
| 402 | return mChildCalls; |
| 403 | } |
| 404 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 405 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 406 | return mCallService; |
| 407 | } |
| 408 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 409 | void setCallService(CallServiceWrapper callService) { |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 410 | setCallService(callService, null); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Changes the call service this call is associated with. If callToReplace is non-null then this |
| 415 | * call takes its place within the call service. |
| 416 | */ |
| 417 | void setCallService(CallServiceWrapper callService, Call callToReplace) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 418 | Preconditions.checkNotNull(callService); |
| 419 | |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 420 | clearCallService(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 421 | |
| 422 | callService.incrementAssociatedCallCount(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 423 | mCallService = callService; |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 424 | if (callToReplace == null) { |
| 425 | mCallService.addCall(this); |
| 426 | } else { |
| 427 | mCallService.replaceCall(this, callToReplace); |
| 428 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Clears the associated call service. |
| 433 | */ |
| 434 | void clearCallService() { |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 435 | if (mCallService != null) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 436 | CallServiceWrapper callServiceTemp = mCallService; |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 437 | mCallService = null; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 438 | callServiceTemp.removeCall(this); |
| 439 | |
| 440 | // Decrementing the count can cause the service to unbind, which itself can trigger the |
| 441 | // service-death code. Since the service death code tries to clean up any associated |
| 442 | // calls, we need to make sure to remove that information (e.g., removeCall()) before |
| 443 | // we decrement. Technically, invoking removeCall() prior to decrementing is all that is |
| 444 | // necessary, but cleaning up mCallService prior to triggering an unbind is good to do. |
| 445 | // If you change this, make sure to update {@link clearCallServiceSelector} as well. |
| 446 | decrementAssociatedCallCount(callServiceTemp); |
Yorke Lee | adee12d | 2014-03-13 12:08:30 -0700 | [diff] [blame] | 447 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 450 | CallServiceSelectorWrapper getCallServiceSelector() { |
| 451 | return mCallServiceSelector; |
| 452 | } |
| 453 | |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 454 | void setCallServiceSelector(CallServiceSelectorWrapper selector) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 455 | Preconditions.checkNotNull(selector); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 456 | |
| 457 | clearCallServiceSelector(); |
| 458 | |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 459 | selector.incrementAssociatedCallCount(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 460 | mCallServiceSelector = selector; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 461 | mCallServiceSelector.addCall(this); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void clearCallServiceSelector() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 465 | if (mCallServiceSelector != null) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 466 | CallServiceSelectorWrapper selectorTemp = mCallServiceSelector; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 467 | mCallServiceSelector = null; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 468 | selectorTemp.removeCall(this); |
| 469 | |
| 470 | // See comment on {@link #clearCallService}. |
| 471 | decrementAssociatedCallCount(selectorTemp); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 472 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 476 | * Starts the incoming call flow through the switchboard. When switchboard completes, it will |
| 477 | * invoke handle[Un]SuccessfulIncomingCall. |
| 478 | * |
| 479 | * @param descriptor The relevant call-service descriptor. |
| 480 | * @param extras The optional extras passed via |
| 481 | * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}. |
| 482 | */ |
| 483 | void startIncoming(CallServiceDescriptor descriptor, Bundle extras) { |
| 484 | Switchboard.getInstance().retrieveIncomingCall(this, descriptor, extras); |
| 485 | } |
| 486 | |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 487 | /** |
| 488 | * Takes a verified incoming call and uses the handle to lookup direct-to-voicemail property |
| 489 | * from the contacts provider. The call is not yet exposed to the user at this point and |
| 490 | * the result of the query will determine if the call is rejected or passed through to the |
| 491 | * in-call UI. |
| 492 | */ |
| 493 | void handleVerifiedIncoming(CallInfo callInfo) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 494 | Preconditions.checkState(callInfo.getState() == CallState.RINGING); |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 495 | |
| 496 | // We do not handle incoming calls immediately when they are verified by the call service. |
| 497 | // We allow the caller-info-query code to execute first so that we can read the |
| 498 | // direct-to-voicemail property before deciding if we want to show the incoming call to the |
| 499 | // user or if we want to reject the call. |
| 500 | mPendingDirectToVoicemailCallInfo = callInfo; |
| 501 | |
| 502 | // Setting the handle triggers the caller info lookup code. |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 503 | setHandle(callInfo.getHandle()); |
| 504 | |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 505 | // Timeout the direct-to-voicemail lookup execution so that we dont wait too long before |
| 506 | // showing the user the incoming call screen. |
| 507 | mHandler.postDelayed(new Runnable() { |
| 508 | @Override |
| 509 | public void run() { |
| 510 | processDirectToVoicemail(); |
| 511 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 512 | }, Timeouts.getDirectToVoicemailMillis()); |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 513 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 514 | |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 515 | void processDirectToVoicemail() { |
| 516 | if (mPendingDirectToVoicemailCallInfo != null) { |
| 517 | if (mCallerInfo != null && mCallerInfo.shouldSendToVoicemail) { |
| 518 | Log.i(this, "Directing call to voicemail: %s.", this); |
| 519 | // TODO(santoscordon): Once we move State handling from CallsManager to Call, we |
| 520 | // will not need to set RINGING state prior to calling reject. |
| 521 | setState(CallState.RINGING); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 522 | reject(false, null); |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 523 | } else { |
| 524 | // TODO(santoscordon): Make this class (not CallsManager) responsible for changing |
| 525 | // the call state to RINGING. |
| 526 | |
| 527 | // TODO(santoscordon): Replace this with state transition to RINGING. |
| 528 | for (Listener l : mListeners) { |
| 529 | l.onSuccessfulIncomingCall(this, mPendingDirectToVoicemailCallInfo); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | mPendingDirectToVoicemailCallInfo = null; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | void handleFailedIncoming() { |
| 538 | clearCallService(); |
| 539 | |
| 540 | // TODO: Needs more specific disconnect error for this case. |
| 541 | setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null); |
| 542 | setState(CallState.DISCONNECTED); |
| 543 | |
| 544 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 545 | for (Listener l : mListeners) { |
| 546 | l.onFailedIncomingCall(this); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 551 | * Starts the outgoing call sequence. Upon completion, there should exist an active connection |
| 552 | * through a call service (or the call will have failed). |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 553 | */ |
| 554 | void startOutgoing() { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 555 | Preconditions.checkState(mOutgoingCallProcessor == null); |
| 556 | |
| 557 | mOutgoingCallProcessor = new OutgoingCallProcessor( |
| 558 | this, |
| 559 | Switchboard.getInstance().getCallServiceRepository(), |
| 560 | Switchboard.getInstance().getSelectorRepository(), |
| 561 | new AsyncResultCallback<Boolean>() { |
| 562 | @Override |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 563 | public void onResult(Boolean wasCallPlaced, int errorCode, String errorMsg) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 564 | if (wasCallPlaced) { |
| 565 | handleSuccessfulOutgoing(); |
| 566 | } else { |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 567 | handleFailedOutgoing( |
| 568 | mOutgoingCallProcessor.isAborted(), errorCode, errorMsg); |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 569 | } |
| 570 | mOutgoingCallProcessor = null; |
| 571 | } |
| 572 | }); |
| 573 | mOutgoingCallProcessor.process(); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void handleSuccessfulOutgoing() { |
| 577 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 578 | for (Listener l : mListeners) { |
| 579 | l.onSuccessfulOutgoingCall(this); |
| 580 | } |
| 581 | } |
| 582 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 583 | void handleFailedOutgoing(boolean isAborted, int errorCode, String errorMsg) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 584 | // TODO(santoscordon): Replace this with state transitions related to "connecting". |
| 585 | for (Listener l : mListeners) { |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 586 | l.onFailedOutgoingCall(this, isAborted, errorCode, errorMsg); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 587 | } |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 588 | |
| 589 | clearCallService(); |
| 590 | clearCallServiceSelector(); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 594 | * Adds the specified call service to the list of incompatible services. The set is used when |
| 595 | * attempting to switch a phone call between call services such that incompatible services can |
| 596 | * be avoided. |
| 597 | * |
| 598 | * @param callService The incompatible call service. |
| 599 | */ |
| 600 | void addIncompatibleCallService(CallServiceWrapper callService) { |
| 601 | if (mIncompatibleCallServices == null) { |
| 602 | mIncompatibleCallServices = Sets.newHashSet(); |
| 603 | } |
| 604 | mIncompatibleCallServices.add(callService); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Checks whether or not the specified callService was identified as incompatible in the |
| 609 | * context of this call. |
| 610 | * |
| 611 | * @param callService The call service to evaluate. |
| 612 | * @return True upon incompatible call services and false otherwise. |
| 613 | */ |
| 614 | boolean isIncompatibleCallService(CallServiceWrapper callService) { |
| 615 | return mIncompatibleCallServices != null && |
| 616 | mIncompatibleCallServices.contains(callService); |
| 617 | } |
| 618 | |
| 619 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 620 | * Plays the specified DTMF tone. |
| 621 | */ |
| 622 | void playDtmfTone(char digit) { |
| 623 | if (mCallService == null) { |
| 624 | Log.w(this, "playDtmfTone() request on a call without a call service."); |
| 625 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 626 | Log.i(this, "Send playDtmfTone to call service for call %s", this); |
| 627 | mCallService.playDtmfTone(this, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Stops playing any currently playing DTMF tone. |
| 633 | */ |
| 634 | void stopDtmfTone() { |
| 635 | if (mCallService == null) { |
| 636 | Log.w(this, "stopDtmfTone() request on a call without a call service."); |
| 637 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 638 | Log.i(this, "Send stopDtmfTone to call service for call %s", this); |
| 639 | mCallService.stopDtmfTone(this); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 644 | * Attempts to disconnect the call through the call service. |
| 645 | */ |
| 646 | void disconnect() { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 647 | if (mState == CallState.NEW) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 648 | Log.v(this, "Aborting call %s", this); |
| 649 | abort(); |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 650 | } else if (mState != CallState.ABORTED && mState != CallState.DISCONNECTED) { |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 651 | Preconditions.checkNotNull(mCallService); |
| 652 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 653 | Log.i(this, "Send disconnect to call service for call: %s", this); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 654 | // The call isn't officially disconnected until the call service confirms that the call |
| 655 | // was actually disconnected. Only then is the association between call and call service |
| 656 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 657 | mCallService.disconnect(this); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 661 | void abort() { |
| 662 | if (mOutgoingCallProcessor != null) { |
| 663 | mOutgoingCallProcessor.abort(); |
| 664 | } |
| 665 | } |
| 666 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 667 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 668 | * Answers the call if it is ringing. |
| 669 | */ |
| 670 | void answer() { |
| 671 | Preconditions.checkNotNull(mCallService); |
| 672 | |
| 673 | // Check to verify that the call is still in the ringing state. A call can change states |
| 674 | // between the time the user hits 'answer' and Telecomm receives the command. |
| 675 | if (isRinging("answer")) { |
| 676 | // At this point, we are asking the call service to answer but we don't assume that |
| 677 | // it will work. Instead, we wait until confirmation from the call service that the |
| 678 | // call is in a non-RINGING state before changing the UI. See |
| 679 | // {@link CallServiceAdapter#setActive} and other set* methods. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 680 | mCallService.answer(this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Rejects the call if it is ringing. |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 686 | * |
| 687 | * @param rejectWithMessage Whether to send a text message as part of the call rejection. |
| 688 | * @param textMessage An optional text message to send as part of the rejection. |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 689 | */ |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 690 | void reject(boolean rejectWithMessage, String textMessage) { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 691 | Preconditions.checkNotNull(mCallService); |
| 692 | |
| 693 | // Check to verify that the call is still in the ringing state. A call can change states |
| 694 | // between the time the user hits 'reject' and Telecomm receives the command. |
| 695 | if (isRinging("reject")) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 696 | mCallService.reject(this); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | /** |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 701 | * Puts the call on hold if it is currently active. |
| 702 | */ |
| 703 | void hold() { |
| 704 | Preconditions.checkNotNull(mCallService); |
| 705 | |
| 706 | if (mState == CallState.ACTIVE) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 707 | mCallService.hold(this); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Releases the call from hold if it is currently active. |
| 713 | */ |
| 714 | void unhold() { |
| 715 | Preconditions.checkNotNull(mCallService); |
| 716 | |
| 717 | if (mState == CallState.ON_HOLD) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 718 | mCallService.unhold(this); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 723 | * @return An object containing read-only information about this call. |
| 724 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 725 | CallInfo toCallInfo(String callId) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 726 | CallServiceDescriptor descriptor = null; |
| 727 | if (mCallService != null) { |
| 728 | descriptor = mCallService.getDescriptor(); |
| 729 | } else if (mOriginalCall != null && mOriginalCall.mCallService != null) { |
| 730 | descriptor = mOriginalCall.mCallService.getDescriptor(); |
| 731 | } |
| 732 | return new CallInfo(callId, mState, mHandle, mGatewayInfo, mExtras, descriptor); |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 735 | /** Checks if this is a live call or not. */ |
| 736 | boolean isAlive() { |
| 737 | switch (mState) { |
| 738 | case NEW: |
| 739 | case RINGING: |
| 740 | case DISCONNECTED: |
| 741 | case ABORTED: |
| 742 | return false; |
| 743 | default: |
| 744 | return true; |
| 745 | } |
| 746 | } |
| 747 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 748 | boolean isActive() { |
| 749 | switch (mState) { |
| 750 | case ACTIVE: |
| 751 | case POST_DIAL: |
| 752 | case POST_DIAL_WAIT: |
| 753 | return true; |
| 754 | default: |
| 755 | return false; |
| 756 | } |
| 757 | } |
| 758 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 759 | Bundle getExtras() { |
| 760 | return mExtras; |
| 761 | } |
| 762 | |
| 763 | void setExtras(Bundle extras) { |
| 764 | mExtras = extras; |
| 765 | } |
| 766 | |
| 767 | Uri getHandoffHandle() { |
| 768 | return mHandoffHandle; |
| 769 | } |
| 770 | |
| 771 | void setHandoffHandle(Uri handoffHandle) { |
| 772 | mHandoffHandle = handoffHandle; |
| 773 | } |
| 774 | |
| 775 | Call getOriginalCall() { |
| 776 | return mOriginalCall; |
| 777 | } |
| 778 | |
| 779 | void setOriginalCall(Call originalCall) { |
| 780 | mOriginalCall = originalCall; |
| 781 | } |
| 782 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 783 | CallServiceDescriptor getHandoffCallServiceDescriptor() { |
| 784 | return mHandoffCallServiceDescriptor; |
| 785 | } |
| 786 | |
| 787 | void setHandoffCallServiceDescriptor(CallServiceDescriptor descriptor) { |
| 788 | mHandoffCallServiceDescriptor = descriptor; |
| 789 | } |
| 790 | |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 791 | Uri getRingtone() { |
| 792 | return mCallerInfo == null ? null : mCallerInfo.contactRingtoneUri; |
| 793 | } |
| 794 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 795 | void onPostDialWait(String remaining) { |
| 796 | for (Listener l : mListeners) { |
| 797 | l.onPostDialWait(this, remaining); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | void postDialContinue(boolean proceed) { |
| 802 | getCallService().onPostDialContinue(this, proceed); |
| 803 | } |
| 804 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 805 | void conferenceInto(Call conferenceCall) { |
| 806 | if (mCallService == null) { |
| 807 | Log.w(this, "conference requested on a call without a call service."); |
| 808 | } else { |
| 809 | mCallService.conference(conferenceCall, this); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | void expireConference() { |
| 814 | // The conference call expired before we got a confirmation of the conference from the |
| 815 | // call service...so start shutting down. |
| 816 | clearCallService(); |
| 817 | for (Listener l : mListeners) { |
| 818 | l.onExpiredConferenceCall(this); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | void confirmConference() { |
| 823 | Log.v(this, "confirming Conf call %s", mListeners); |
| 824 | for (Listener l : mListeners) { |
| 825 | l.onConfirmedConferenceCall(this); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void splitFromConference() { |
| 830 | // TODO(santoscordon): todo |
| 831 | } |
| 832 | |
| 833 | void setParentCall(Call parentCall) { |
| 834 | if (parentCall == this) { |
| 835 | Log.e(this, new Exception(), "setting the parent to self"); |
| 836 | return; |
| 837 | } |
| 838 | Preconditions.checkState(parentCall == null || mParentCall == null); |
| 839 | |
| 840 | Call oldParent = mParentCall; |
| 841 | if (mParentCall != null) { |
| 842 | mParentCall.removeChildCall(this); |
| 843 | } |
| 844 | mParentCall = parentCall; |
| 845 | if (mParentCall != null) { |
| 846 | mParentCall.addChildCall(this); |
| 847 | } |
| 848 | |
| 849 | for (Listener l : mListeners) { |
| 850 | l.onParentChanged(this); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | private void addChildCall(Call call) { |
| 855 | if (!mChildCalls.contains(call)) { |
| 856 | mChildCalls.add(call); |
| 857 | |
| 858 | for (Listener l : mListeners) { |
| 859 | l.onChildrenChanged(this); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | private void removeChildCall(Call call) { |
| 865 | if (mChildCalls.remove(call)) { |
| 866 | for (Listener l : mListeners) { |
| 867 | l.onChildrenChanged(this); |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 872 | /** |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 873 | * Return whether the user can respond to this {@code Call} via an SMS message. |
| 874 | * |
| 875 | * @return true if the "Respond via SMS" feature should be enabled |
| 876 | * for this incoming call. |
| 877 | * |
| 878 | * The general rule is that we *do* allow "Respond via SMS" except for |
| 879 | * the few (relatively rare) cases where we know for sure it won't |
| 880 | * work, namely: |
| 881 | * - a bogus or blank incoming number |
| 882 | * - a call from a SIP address |
| 883 | * - a "call presentation" that doesn't allow the number to be revealed |
| 884 | * |
| 885 | * In all other cases, we allow the user to respond via SMS. |
| 886 | * |
| 887 | * Note that this behavior isn't perfect; for example we have no way |
| 888 | * to detect whether the incoming call is from a landline (with most |
| 889 | * networks at least), so we still enable this feature even though |
| 890 | * SMSes to that number will silently fail. |
| 891 | */ |
| 892 | boolean isRespondViaSmsCapable() { |
| 893 | if (mState != CallState.RINGING) { |
| 894 | return false; |
| 895 | } |
| 896 | |
| 897 | if (getHandle() == null) { |
| 898 | // No incoming number known or call presentation is "PRESENTATION_RESTRICTED", in |
| 899 | // other words, the user should not be able to see the incoming phone number. |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | if (PhoneNumberUtils.isUriNumber(getHandle().toString())) { |
| 904 | // The incoming number is actually a URI (i.e. a SIP address), |
| 905 | // not a regular PSTN phone number, and we can't send SMSes to |
| 906 | // SIP addresses. |
| 907 | // (TODO: That might still be possible eventually, though. Is |
| 908 | // there some SIP-specific equivalent to sending a text message?) |
| 909 | return false; |
| 910 | } |
| 911 | |
| 912 | // Is there a valid SMS application on the phone? |
| 913 | if (SmsApplication.getDefaultRespondViaMessageApplication(TelecommApp.getInstance(), |
| 914 | true /*updateIfNeeded*/) == null) { |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | // TODO: with some carriers (in certain countries) you *can* actually |
| 919 | // tell whether a given number is a mobile phone or not. So in that |
| 920 | // case we could potentially return false here if the incoming call is |
| 921 | // from a land line. |
| 922 | |
| 923 | // If none of the above special cases apply, it's OK to enable the |
| 924 | // "Respond via SMS" feature. |
| 925 | return true; |
| 926 | } |
| 927 | |
| 928 | List<String> getCannedSmsResponses() { |
| 929 | return mCannedSmsResponses; |
| 930 | } |
| 931 | |
| 932 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 933 | * @return True if the call is ringing, else logs the action name. |
| 934 | */ |
| 935 | private boolean isRinging(String actionName) { |
| 936 | if (mState == CallState.RINGING) { |
| 937 | return true; |
| 938 | } |
| 939 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 940 | 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] | 941 | return false; |
| 942 | } |
| 943 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 944 | @SuppressWarnings("rawtypes") |
| 945 | private void decrementAssociatedCallCount(ServiceBinder binder) { |
| 946 | if (binder != null) { |
| 947 | binder.decrementAssociatedCallCount(); |
| 948 | } |
| 949 | } |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 950 | |
| 951 | /** |
| 952 | * Looks up contact information based on the current handle. |
| 953 | */ |
| 954 | private void startCallerInfoLookup() { |
| 955 | String number = mHandle == null ? null : mHandle.getSchemeSpecificPart(); |
| 956 | |
| 957 | mQueryToken++; // Updated so that previous queries can no longer set the information. |
| 958 | mCallerInfo = null; |
| 959 | if (!TextUtils.isEmpty(number)) { |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 960 | Log.v(this, "Looking up information for: %s.", Log.piiHandle(number)); |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 961 | CallerInfoAsyncQuery.startQuery( |
| 962 | mQueryToken, |
| 963 | TelecommApp.getInstance(), |
| 964 | number, |
| 965 | sCallerInfoQueryListener, |
| 966 | this); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | /** |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 971 | * Saves the specified caller info if the specified token matches that of the last query |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 972 | * that was made. |
| 973 | * |
| 974 | * @param callerInfo The new caller information to set. |
| 975 | * @param token The token used with this query. |
| 976 | */ |
| 977 | private void setCallerInfo(CallerInfo callerInfo, int token) { |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 978 | Preconditions.checkNotNull(callerInfo); |
| 979 | |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 980 | if (mQueryToken == token) { |
| 981 | mCallerInfo = callerInfo; |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 982 | Log.i(this, "CallerInfo received for %s: %s", Log.piiHandle(mHandle), callerInfo); |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 983 | |
| 984 | if (mCallerInfo.person_id != 0) { |
| 985 | Uri personUri = |
| 986 | ContentUris.withAppendedId(Contacts.CONTENT_URI, mCallerInfo.person_id); |
| 987 | Log.d(this, "Searching person uri %s for call %s", personUri, this); |
| 988 | ContactsAsyncHelper.startObtainPhotoAsync( |
| 989 | token, |
| 990 | TelecommApp.getInstance(), |
| 991 | personUri, |
| 992 | sPhotoLoadListener, |
| 993 | this); |
| 994 | } |
Santos Cordon | 2174fb5 | 2014-05-29 08:22:56 -0700 | [diff] [blame] | 995 | |
| 996 | processDirectToVoicemail(); |
Santos Cordon | 99c8a6f | 2014-05-28 18:28:47 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Saves the specified photo information if the specified token matches that of the last query. |
| 1002 | * |
| 1003 | * @param photo The photo as a drawable. |
| 1004 | * @param photoIcon The photo as a small icon. |
| 1005 | * @param token The token used with this query. |
| 1006 | */ |
| 1007 | private void setPhoto(Drawable photo, Bitmap photoIcon, int token) { |
| 1008 | if (mQueryToken == token) { |
| 1009 | mCallerInfo.cachedPhoto = photo; |
| 1010 | mCallerInfo.cachedPhotoIcon = photoIcon; |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 1013 | |
| 1014 | private void maybeLoadCannedSmsResponses() { |
| 1015 | if (mIsIncoming && isRespondViaSmsCapable() && !mCannedSmsResponsesLoadingStarted) { |
| 1016 | Log.d(this, "maybeLoadCannedSmsResponses: starting task to load messages"); |
| 1017 | mCannedSmsResponsesLoadingStarted = true; |
| 1018 | RespondViaSmsManager.getInstance().loadCannedTextMessages( |
| 1019 | new Response<Void, List<String>>() { |
| 1020 | @Override |
| 1021 | public void onResult(Void request, List<String>... result) { |
| 1022 | if (result.length > 0) { |
| 1023 | Log.d(this, "maybeLoadCannedSmsResponses: got %s", result[0]); |
| 1024 | mCannedSmsResponses = result[0]; |
| 1025 | for (Listener l : mListeners) { |
| 1026 | l.onCannedSmsResponsesLoaded(Call.this); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | @Override |
| 1032 | public void onError(Void request, int code, String msg) { |
| 1033 | Log.w(Call.this, "Error obtaining canned SMS responses: %d %s", code, |
| 1034 | msg); |
| 1035 | } |
| 1036 | } |
| 1037 | ); |
| 1038 | } else { |
| 1039 | Log.d(this, "maybeLoadCannedSmsResponses: doing nothing"); |
| 1040 | } |
| 1041 | } |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 1042 | |
| 1043 | /** |
| 1044 | * Sets speakerphone option on when call begins. |
| 1045 | */ |
| 1046 | public void setStartWithSpeakerphoneOn(boolean startWithSpeakerphone) { |
| 1047 | mSpeakerphoneOn = startWithSpeakerphone; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * Returns speakerphone option. |
| 1052 | * |
| 1053 | * @return Whether or not speakerphone should be set automatically when call begins. |
| 1054 | */ |
| 1055 | public boolean getStartWithSpeakerphoneOn() { |
| 1056 | return mSpeakerphoneOn; |
| 1057 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 1058 | } |