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