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