blob: c1c7eb42e20d7aceb78873a322d5028bd2cbc2b2 [file] [log] [blame]
Ben Gilad0407fb22014-01-09 16:18:41 -08001/*
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 Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Santos Cordon99c8a6f2014-05-28 18:28:47 -070019import android.content.ContentUris;
20import android.graphics.Bitmap;
21import android.graphics.drawable.Drawable;
Sailesh Nepalce704b92014-03-17 18:31:43 -070022import android.net.Uri;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070023import android.os.Bundle;
Santos Cordonfd71f4a2014-05-28 13:59:49 -070024import android.os.Handler;
Santos Cordon99c8a6f2014-05-28 18:28:47 -070025import android.provider.ContactsContract.Contacts;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070026import android.telecomm.CallPropertyPresentation;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070027import android.telecomm.CallServiceDescriptor;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080028import android.telecomm.CallState;
Sailesh Nepalc92c4362014-07-04 18:33:21 -070029import android.telecomm.ConnectionRequest;
Yorke Lee33501632014-03-17 19:24:12 -070030import android.telecomm.GatewayInfo;
Ihab Awad98a55602014-06-30 21:27:28 -070031import android.telecomm.PhoneAccount;
Ihab Awadff7493a2014-06-10 13:47:44 -070032import android.telecomm.Response;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070033import android.telecomm.StatusHints;
Santos Cordon766d04f2014-05-06 10:28:25 -070034import android.telecomm.TelecommConstants;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070035import android.telephony.DisconnectCause;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070036import android.telephony.PhoneNumberUtils;
Santos Cordonfd71f4a2014-05-28 13:59:49 -070037import android.text.TextUtils;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080038
Nancy Chena65d41f2014-06-24 12:06:03 -070039import com.android.internal.telecomm.ICallVideoProvider;
Santos Cordonfd71f4a2014-05-28 13:59:49 -070040import com.android.internal.telephony.CallerInfo;
41import com.android.internal.telephony.CallerInfoAsyncQuery;
42import com.android.internal.telephony.CallerInfoAsyncQuery.OnQueryCompleteListener;
Ihab Awadff7493a2014-06-10 13:47:44 -070043import com.android.internal.telephony.SmsApplication;
Santos Cordon99c8a6f2014-05-28 18:28:47 -070044import com.android.telecomm.ContactsAsyncHelper.OnImageLoadCompleteListener;
Santos Cordon61d0f702014-02-19 02:52:23 -080045import com.google.common.base.Preconditions;
Ihab Awadff7493a2014-06-10 13:47:44 -070046import com.google.common.collect.Sets;
Santos Cordon61d0f702014-02-19 02:52:23 -080047
Ihab Awadff7493a2014-06-10 13:47:44 -070048import java.util.Collections;
Santos Cordona1610702014-06-04 20:22:56 -070049import java.util.LinkedList;
50import java.util.List;
Sailesh Nepal91990782014-03-08 17:45:52 -080051import java.util.Locale;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070052import java.util.Objects;
Ben Gilad61925612014-03-11 19:06:36 -070053import java.util.Set;
Ben Gilad0407fb22014-01-09 16:18:41 -080054
Ben Gilad2495d572014-01-09 17:26:19 -080055/**
56 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
57 * from the time the call intent was received by Telecomm (vs. the time the call was
58 * connected etc).
59 */
Sailesh Nepal5a73b032014-06-25 15:53:21 -070060final class Call implements OutgoingCallResponse {
Santos Cordon766d04f2014-05-06 10:28:25 -070061 /**
62 * Listener for events on the call.
63 */
64 interface Listener {
65 void onSuccessfulOutgoingCall(Call call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -070066 void onFailedOutgoingCall(Call call, int errorCode, String errorMsg);
67 void onCancelledOutgoingCall(Call call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070068 void onSuccessfulIncomingCall(Call call);
Santos Cordon766d04f2014-05-06 10:28:25 -070069 void onFailedIncomingCall(Call call);
Ihab Awadcb387ac2014-05-28 16:49:38 -070070 void onRequestingRingback(Call call, boolean requestingRingback);
Evan Charlton352105c2014-06-03 14:10:54 -070071 void onPostDialWait(Call call, String remaining);
Sailesh Nepale20bc972014-07-09 21:22:36 -070072 void onCallCapabilitiesChanged(Call call);
Santos Cordona1610702014-06-04 20:22:56 -070073 void onExpiredConferenceCall(Call call);
74 void onConfirmedConferenceCall(Call call);
75 void onParentChanged(Call call);
76 void onChildrenChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070077 void onCannedSmsResponsesLoaded(Call call);
Nancy Chena65d41f2014-06-24 12:06:03 -070078 void onCallVideoProviderChanged(Call call);
Santos Cordon64c7e962014-07-02 15:15:27 -070079 void onCallerInfoChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070080 void onAudioModeIsVoipChanged(Call call);
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070081 void onStatusHintsChanged(Call call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -070082 void onHandleChanged(Call call);
83 void onCallerDisplayNameChanged(Call call);
Santos Cordon64c7e962014-07-02 15:15:27 -070084 }
85
86 abstract static class ListenerBase implements Listener {
87 @Override
88 public void onSuccessfulOutgoingCall(Call call) {}
89 @Override
90 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {}
91 @Override
92 public void onCancelledOutgoingCall(Call call) {}
93 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -070094 public void onSuccessfulIncomingCall(Call call) {}
Santos Cordon64c7e962014-07-02 15:15:27 -070095 @Override
96 public void onFailedIncomingCall(Call call) {}
97 @Override
98 public void onRequestingRingback(Call call, boolean requestingRingback) {}
99 @Override
100 public void onPostDialWait(Call call, String remaining) {}
101 @Override
Sailesh Nepale20bc972014-07-09 21:22:36 -0700102 public void onCallCapabilitiesChanged(Call call) {}
Santos Cordon64c7e962014-07-02 15:15:27 -0700103 @Override
104 public void onExpiredConferenceCall(Call call) {}
105 @Override
106 public void onConfirmedConferenceCall(Call call) {}
107 @Override
108 public void onParentChanged(Call call) {}
109 @Override
110 public void onChildrenChanged(Call call) {}
111 @Override
112 public void onCannedSmsResponsesLoaded(Call call) {}
113 @Override
114 public void onCallVideoProviderChanged(Call call) {}
115 @Override
Santos Cordon64c7e962014-07-02 15:15:27 -0700116 public void onCallerInfoChanged(Call call) {}
Sailesh Nepal7e669572014-07-08 21:29:12 -0700117 @Override
118 public void onAudioModeIsVoipChanged(Call call) {}
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700119 @Override
120 public void onStatusHintsChanged(Call call) {}
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700121 @Override
122 public void onHandleChanged(Call call) {}
123 @Override
124 public void onCallerDisplayNameChanged(Call call) {}
Santos Cordon766d04f2014-05-06 10:28:25 -0700125 }
126
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700127 private static final OnQueryCompleteListener sCallerInfoQueryListener =
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700128 new OnQueryCompleteListener() {
129 /** ${inheritDoc} */
130 @Override
131 public void onQueryComplete(int token, Object cookie, CallerInfo callerInfo) {
132 if (cookie != null) {
133 ((Call) cookie).setCallerInfo(callerInfo, token);
134 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700135 }
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700136 };
137
138 private static final OnImageLoadCompleteListener sPhotoLoadListener =
139 new OnImageLoadCompleteListener() {
140 /** ${inheritDoc} */
141 @Override
142 public void onImageLoadComplete(
143 int token, Drawable photo, Bitmap photoIcon, Object cookie) {
144 if (cookie != null) {
145 ((Call) cookie).setPhoto(photo, photoIcon, token);
146 }
147 }
148 };
Ben Gilad0407fb22014-01-09 16:18:41 -0800149
Sailesh Nepal810735e2014-03-18 18:15:46 -0700150 /** True if this is an incoming call. */
151 private final boolean mIsIncoming;
152
Ben Gilad0407fb22014-01-09 16:18:41 -0800153 /**
154 * The time this call was created, typically also the time this call was added to the set
155 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
156 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
157 * certain call attempts as failed attempts.
158 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700159 private final long mCreationTimeMillis = System.currentTimeMillis();
160
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700161 /** The gateway information associated with this call. This stores the original call handle
162 * that the user is attempting to connect to via the gateway, the actual handle to dial in
163 * order to connect the call via the gateway, as well as the package name of the gateway
164 * service. */
165 private final GatewayInfo mGatewayInfo;
166
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700167 private PhoneAccount mPhoneAccount;
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700168
Santos Cordon2174fb52014-05-29 08:22:56 -0700169 private final Handler mHandler = new Handler();
170
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700171 private long mConnectTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -0800172
Santos Cordon61d0f702014-02-19 02:52:23 -0800173 /** The state of the call. */
174 private CallState mState;
175
176 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -0700177 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -0800178
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700179 /** The {@link CallPropertyPresentation} that controls how the handle is shown. */
180 private int mHandlePresentation;
181
182 /** The caller display name (CNAP) set by the connection service. */
183 private String mCallerDisplayName;
184
185 /** The {@link CallPropertyPresentation} that controls how the caller display name is shown. */
186 private int mCallerDisplayNamePresentation;
187
Ben Gilad0407fb22014-01-09 16:18:41 -0800188 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700189 * The connection service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -0800190 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700191 private ConnectionServiceWrapper mConnectionService;
Ben Gilad61925612014-03-11 19:06:36 -0700192
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700193 private boolean mIsEmergencyCall;
194
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700195 private boolean mSpeakerphoneOn;
196
Tyler Gunnc4abd912014-07-08 14:22:10 -0700197 private int mVideoState;
198
Ben Gilad61925612014-03-11 19:06:36 -0700199 /**
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700200 * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED.
201 * See {@link android.telephony.DisconnectCause}.
202 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700203 private int mDisconnectCause = DisconnectCause.NOT_VALID;
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700204
205 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700206 * Additional disconnect information provided by the connection service.
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700207 */
208 private String mDisconnectMessage;
209
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700210 /** Info used by the connection services. */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700211 private Bundle mExtras = Bundle.EMPTY;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700212
Santos Cordon766d04f2014-05-06 10:28:25 -0700213 /** Set of listeners on this call. */
214 private Set<Listener> mListeners = Sets.newHashSet();
215
Santos Cordon682fe6b2014-05-20 08:56:39 -0700216 private OutgoingCallProcessor mOutgoingCallProcessor;
217
218 // TODO(santoscordon): The repositories should be changed into singleton types.
219 private CallServiceRepository mCallServiceRepository;
Santos Cordon682fe6b2014-05-20 08:56:39 -0700220
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700221 /** Caller information retrieved from the latest contact query. */
222 private CallerInfo mCallerInfo;
223
224 /** The latest token used with a contact info query. */
225 private int mQueryToken = 0;
226
Ihab Awadcb387ac2014-05-28 16:49:38 -0700227 /** Whether this call is requesting that Telecomm play the ringback tone on its behalf. */
228 private boolean mRequestingRingback = false;
229
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700230 /** Whether direct-to-voicemail query is pending. */
231 private boolean mDirectToVoicemailQueryPending;
Santos Cordon2174fb52014-05-29 08:22:56 -0700232
Sailesh Nepale20bc972014-07-09 21:22:36 -0700233 private int mCallCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700234
235 private boolean mIsConference = false;
236
237 private Call mParentCall = null;
238
239 private List<Call> mChildCalls = new LinkedList<>();
240
Ihab Awadff7493a2014-06-10 13:47:44 -0700241 /** Set of text message responses allowed for this call, if applicable. */
242 private List<String> mCannedSmsResponses = Collections.EMPTY_LIST;
243
244 /** Whether an attempt has been made to load the text message responses. */
245 private boolean mCannedSmsResponsesLoadingStarted = false;
246
Nancy Chena65d41f2014-06-24 12:06:03 -0700247 private ICallVideoProvider mCallVideoProvider;
248
Sailesh Nepal7e669572014-07-08 21:29:12 -0700249 private boolean mAudioModeIsVoip;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700250 private StatusHints mStatusHints;
Sailesh Nepal7e669572014-07-08 21:29:12 -0700251
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700252 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700253 * Creates an empty call object.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700254 *
255 * @param isIncoming True if this is an incoming call.
Santos Cordon493e8f22014-02-19 03:15:12 -0800256 */
Santos Cordona1610702014-06-04 20:22:56 -0700257 Call(boolean isIncoming, boolean isConference) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700258 this(null, null, null, isIncoming, isConference);
Santos Cordon493e8f22014-02-19 03:15:12 -0800259 }
260
261 /**
Ben Gilad0407fb22014-01-09 16:18:41 -0800262 * Persists the specified parameters and initializes the new instance.
263 *
264 * @param handle The handle to dial.
Yorke Lee33501632014-03-17 19:24:12 -0700265 * @param gatewayInfo Gateway information to use for the call.
Ihab Awad98a55602014-06-30 21:27:28 -0700266 * @param account Account information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700267 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800268 */
Ihab Awad98a55602014-06-30 21:27:28 -0700269 Call(Uri handle, GatewayInfo gatewayInfo, PhoneAccount account,
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700270 boolean isIncoming, boolean isConference) {
Santos Cordona1610702014-06-04 20:22:56 -0700271 mState = isConference ? CallState.ACTIVE : CallState.NEW;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700272 setHandle(handle, CallPropertyPresentation.ALLOWED);
Yorke Lee33501632014-03-17 19:24:12 -0700273 mGatewayInfo = gatewayInfo;
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700274 mPhoneAccount = account;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700275 mIsIncoming = isIncoming;
Santos Cordona1610702014-06-04 20:22:56 -0700276 mIsConference = isConference;
Ihab Awadff7493a2014-06-10 13:47:44 -0700277 maybeLoadCannedSmsResponses();
Ben Gilad0407fb22014-01-09 16:18:41 -0800278 }
279
Santos Cordon766d04f2014-05-06 10:28:25 -0700280 void addListener(Listener listener) {
281 mListeners.add(listener);
282 }
283
284 void removeListener(Listener listener) {
285 mListeners.remove(listener);
286 }
287
Santos Cordon61d0f702014-02-19 02:52:23 -0800288 /** {@inheritDoc} */
289 @Override public String toString() {
Sailesh Nepal4538f012014-04-15 11:40:33 -0700290 String component = null;
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700291 if (mConnectionService != null && mConnectionService.getComponentName() != null) {
292 component = mConnectionService.getComponentName().flattenToShortString();
Sailesh Nepal4538f012014-04-15 11:40:33 -0700293 }
294 return String.format(Locale.US, "[%s, %s, %s]", mState, component, Log.piiHandle(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800295 }
296
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800297 CallState getState() {
Santos Cordona1610702014-06-04 20:22:56 -0700298 if (mIsConference) {
299 if (!mChildCalls.isEmpty()) {
300 // If we have child calls, just return the child call.
301 return mChildCalls.get(0).getState();
302 }
303 return CallState.ACTIVE;
304 } else {
305 return mState;
306 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800307 }
308
309 /**
310 * Sets the call state. Although there exists the notion of appropriate state transitions
311 * (see {@link CallState}), in practice those expectations break down when cellular systems
312 * misbehave and they do this very often. The result is that we do not enforce state transitions
313 * and instead keep the code resilient to unexpected state changes.
314 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700315 void setState(CallState newState) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700316 Preconditions.checkState(newState != CallState.DISCONNECTED ||
317 mDisconnectCause != DisconnectCause.NOT_VALID);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700318 if (mState != newState) {
319 Log.v(this, "setState %s -> %s", mState, newState);
320 mState = newState;
Ihab Awadff7493a2014-06-10 13:47:44 -0700321 maybeLoadCannedSmsResponses();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700322 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800323 }
324
Ihab Awadcb387ac2014-05-28 16:49:38 -0700325 void setRequestingRingback(boolean requestingRingback) {
326 mRequestingRingback = requestingRingback;
327 for (Listener l : mListeners) {
328 l.onRequestingRingback(this, mRequestingRingback);
329 }
330 }
331
332 boolean isRequestingRingback() {
333 return mRequestingRingback;
334 }
335
Sailesh Nepalce704b92014-03-17 18:31:43 -0700336 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800337 return mHandle;
338 }
339
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700340 int getHandlePresentation() {
341 return mHandlePresentation;
342 }
343
344 void setHandle(Uri handle, int presentation) {
345 if (!Objects.equals(handle, mHandle) || presentation != mHandlePresentation) {
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700346 mHandle = handle;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700347 mHandlePresentation = presentation;
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700348 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
Yorke Lee66255452014-06-05 08:09:24 -0700349 TelecommApp.getInstance(), mHandle.getSchemeSpecificPart());
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700350 startCallerInfoLookup();
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700351 for (Listener l : mListeners) {
352 l.onHandleChanged(this);
353 }
354 }
355 }
356
357 String getCallerDisplayName() {
358 return mCallerDisplayName;
359 }
360
361 int getCallerDisplayNamePresentation() {
362 return mCallerDisplayNamePresentation;
363 }
364
365 void setCallerDisplayName(String callerDisplayName, int presentation) {
366 if (!TextUtils.equals(callerDisplayName, mCallerDisplayName) ||
367 presentation != mCallerDisplayNamePresentation) {
368 mCallerDisplayName = callerDisplayName;
369 mCallerDisplayNamePresentation = presentation;
370 for (Listener l : mListeners) {
371 l.onCallerDisplayNameChanged(this);
372 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700373 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700374 }
375
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700376 String getName() {
377 return mCallerInfo == null ? null : mCallerInfo.name;
378 }
379
380 Bitmap getPhotoIcon() {
381 return mCallerInfo == null ? null : mCallerInfo.cachedPhotoIcon;
382 }
383
384 Drawable getPhoto() {
385 return mCallerInfo == null ? null : mCallerInfo.cachedPhoto;
386 }
387
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700388 /**
389 * @param disconnectCause The reason for the disconnection, any of
390 * {@link android.telephony.DisconnectCause}.
391 * @param disconnectMessage Optional call-service-provided message about the disconnect.
392 */
393 void setDisconnectCause(int disconnectCause, String disconnectMessage) {
394 // TODO: Consider combining this method with a setDisconnected() method that is totally
395 // separate from setState.
396 mDisconnectCause = disconnectCause;
397 mDisconnectMessage = disconnectMessage;
398 }
399
400 int getDisconnectCause() {
401 return mDisconnectCause;
402 }
403
404 String getDisconnectMessage() {
405 return mDisconnectMessage;
406 }
407
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700408 boolean isEmergencyCall() {
409 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800410 }
411
Yorke Lee33501632014-03-17 19:24:12 -0700412 /**
413 * @return The original handle this call is associated with. In-call services should use this
414 * handle when indicating in their UI the handle that is being called.
415 */
416 public Uri getOriginalHandle() {
417 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
418 return mGatewayInfo.getOriginalHandle();
419 }
420 return getHandle();
421 }
422
423 GatewayInfo getGatewayInfo() {
424 return mGatewayInfo;
425 }
426
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700427 PhoneAccount getPhoneAccount() {
428 return mPhoneAccount;
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700429 }
430
Sailesh Nepal810735e2014-03-18 18:15:46 -0700431 boolean isIncoming() {
432 return mIsIncoming;
433 }
434
Ben Gilad0407fb22014-01-09 16:18:41 -0800435 /**
436 * @return The "age" of this call object in milliseconds, which typically also represents the
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700437 * period since this call was added to the set pending outgoing calls, see
438 * mCreationTimeMillis.
Ben Gilad0407fb22014-01-09 16:18:41 -0800439 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700440 long getAgeMillis() {
441 return System.currentTimeMillis() - mCreationTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -0800442 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800443
Yorke Leef98fb572014-03-05 10:56:55 -0800444 /**
445 * @return The time when this call object was created and added to the set of pending outgoing
446 * calls.
447 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700448 long getCreationTimeMillis() {
449 return mCreationTimeMillis;
450 }
451
452 long getConnectTimeMillis() {
453 return mConnectTimeMillis;
454 }
455
456 void setConnectTimeMillis(long connectTimeMillis) {
457 mConnectTimeMillis = connectTimeMillis;
Yorke Leef98fb572014-03-05 10:56:55 -0800458 }
459
Sailesh Nepale20bc972014-07-09 21:22:36 -0700460 int getCallCapabilities() {
461 return mCallCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700462 }
463
Sailesh Nepale20bc972014-07-09 21:22:36 -0700464 void setCallCapabilities(int callCapabilities) {
465 if (mCallCapabilities != callCapabilities) {
466 mCallCapabilities = callCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700467 for (Listener l : mListeners) {
Sailesh Nepale20bc972014-07-09 21:22:36 -0700468 l.onCallCapabilitiesChanged(this);
Santos Cordona1610702014-06-04 20:22:56 -0700469 }
470 }
471 }
472
473 Call getParentCall() {
474 return mParentCall;
475 }
476
477 List<Call> getChildCalls() {
478 return mChildCalls;
479 }
480
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700481 ConnectionServiceWrapper getConnectionService() {
482 return mConnectionService;
Santos Cordon681663d2014-01-30 04:32:15 -0800483 }
484
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700485 void setConnectionService(ConnectionServiceWrapper service) {
486 Preconditions.checkNotNull(service);
487
488 clearConnectionService();
489
490 service.incrementAssociatedCallCount();
491 mConnectionService = service;
492 mConnectionService.addCall(this);
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700493 }
494
495 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700496 * Clears the associated connection service.
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700497 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700498 void clearConnectionService() {
499 if (mConnectionService != null) {
500 ConnectionServiceWrapper serviceTemp = mConnectionService;
501 mConnectionService = null;
502 serviceTemp.removeCall(this);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700503
504 // Decrementing the count can cause the service to unbind, which itself can trigger the
505 // service-death code. Since the service death code tries to clean up any associated
506 // calls, we need to make sure to remove that information (e.g., removeCall()) before
507 // we decrement. Technically, invoking removeCall() prior to decrementing is all that is
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700508 // necessary, but cleaning up mConnectionService prior to triggering an unbind is good
509 // to do.
510 decrementAssociatedCallCount(serviceTemp);
Yorke Leeadee12d2014-03-13 12:08:30 -0700511 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800512 }
513
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800514 /**
Santos Cordon766d04f2014-05-06 10:28:25 -0700515 * Starts the incoming call flow through the switchboard. When switchboard completes, it will
516 * invoke handle[Un]SuccessfulIncomingCall.
517 *
518 * @param descriptor The relevant call-service descriptor.
519 * @param extras The optional extras passed via
520 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}.
521 */
522 void startIncoming(CallServiceDescriptor descriptor, Bundle extras) {
523 Switchboard.getInstance().retrieveIncomingCall(this, descriptor, extras);
524 }
525
Santos Cordon2174fb52014-05-29 08:22:56 -0700526 /**
527 * Takes a verified incoming call and uses the handle to lookup direct-to-voicemail property
528 * from the contacts provider. The call is not yet exposed to the user at this point and
529 * the result of the query will determine if the call is rejected or passed through to the
530 * in-call UI.
531 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700532 void handleVerifiedIncoming(ConnectionRequest request) {
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700533 mPhoneAccount = request.getAccount();
534
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700535 // We do not handle incoming calls immediately when they are verified by the connection
536 // service. We allow the caller-info-query code to execute first so that we can read the
Santos Cordon2174fb52014-05-29 08:22:56 -0700537 // direct-to-voicemail property before deciding if we want to show the incoming call to the
538 // user or if we want to reject the call.
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700539 mDirectToVoicemailQueryPending = true;
Santos Cordon2174fb52014-05-29 08:22:56 -0700540
541 // Setting the handle triggers the caller info lookup code.
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700542 setHandle(request.getHandle(), CallPropertyPresentation.ALLOWED);
Santos Cordon766d04f2014-05-06 10:28:25 -0700543
Santos Cordon2174fb52014-05-29 08:22:56 -0700544 // Timeout the direct-to-voicemail lookup execution so that we dont wait too long before
545 // showing the user the incoming call screen.
546 mHandler.postDelayed(new Runnable() {
547 @Override
548 public void run() {
549 processDirectToVoicemail();
550 }
Santos Cordona1610702014-06-04 20:22:56 -0700551 }, Timeouts.getDirectToVoicemailMillis());
Santos Cordon2174fb52014-05-29 08:22:56 -0700552 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700553
Santos Cordon2174fb52014-05-29 08:22:56 -0700554 void processDirectToVoicemail() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700555 if (mDirectToVoicemailQueryPending) {
Santos Cordon2174fb52014-05-29 08:22:56 -0700556 if (mCallerInfo != null && mCallerInfo.shouldSendToVoicemail) {
557 Log.i(this, "Directing call to voicemail: %s.", this);
558 // TODO(santoscordon): Once we move State handling from CallsManager to Call, we
559 // will not need to set RINGING state prior to calling reject.
560 setState(CallState.RINGING);
Ihab Awadff7493a2014-06-10 13:47:44 -0700561 reject(false, null);
Santos Cordon2174fb52014-05-29 08:22:56 -0700562 } else {
563 // TODO(santoscordon): Make this class (not CallsManager) responsible for changing
564 // the call state to RINGING.
565
566 // TODO(santoscordon): Replace this with state transition to RINGING.
567 for (Listener l : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700568 l.onSuccessfulIncomingCall(this);
Santos Cordon2174fb52014-05-29 08:22:56 -0700569 }
570 }
571
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700572 mDirectToVoicemailQueryPending = false;
Santos Cordon766d04f2014-05-06 10:28:25 -0700573 }
574 }
575
576 void handleFailedIncoming() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700577 clearConnectionService();
Santos Cordon766d04f2014-05-06 10:28:25 -0700578
579 // TODO: Needs more specific disconnect error for this case.
580 setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
581 setState(CallState.DISCONNECTED);
582
583 // TODO(santoscordon): Replace this with state transitions related to "connecting".
584 for (Listener l : mListeners) {
585 l.onFailedIncomingCall(this);
586 }
587 }
588
589 /**
Santos Cordon682fe6b2014-05-20 08:56:39 -0700590 * Starts the outgoing call sequence. Upon completion, there should exist an active connection
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700591 * through a connection service (or the call will have failed).
Santos Cordon766d04f2014-05-06 10:28:25 -0700592 */
593 void startOutgoing() {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700594 Preconditions.checkState(mOutgoingCallProcessor == null);
595
596 mOutgoingCallProcessor = new OutgoingCallProcessor(
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700597 this, Switchboard.getInstance().getCallServiceRepository(), this);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700598 mOutgoingCallProcessor.process();
Santos Cordon766d04f2014-05-06 10:28:25 -0700599 }
600
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700601 @Override
602 public void onOutgoingCallSuccess() {
Santos Cordon766d04f2014-05-06 10:28:25 -0700603 // TODO(santoscordon): Replace this with state transitions related to "connecting".
604 for (Listener l : mListeners) {
605 l.onSuccessfulOutgoingCall(this);
606 }
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700607 mOutgoingCallProcessor = null;
Santos Cordon766d04f2014-05-06 10:28:25 -0700608 }
609
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700610 @Override
611 public void onOutgoingCallFailure(int code, String msg) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700612 // TODO(santoscordon): Replace this with state transitions related to "connecting".
613 for (Listener l : mListeners) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700614 l.onFailedOutgoingCall(this, code, msg);
Santos Cordon766d04f2014-05-06 10:28:25 -0700615 }
Santos Cordon682fe6b2014-05-20 08:56:39 -0700616
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700617 clearConnectionService();
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700618 mOutgoingCallProcessor = null;
619 }
620
621 @Override
622 public void onOutgoingCallCancel() {
623 // TODO(santoscordon): Replace this with state transitions related to "connecting".
624 for (Listener l : mListeners) {
625 l.onCancelledOutgoingCall(this);
626 }
627
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700628 clearConnectionService();
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700629 mOutgoingCallProcessor = null;
Santos Cordon766d04f2014-05-06 10:28:25 -0700630 }
631
632 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700633 * Plays the specified DTMF tone.
634 */
635 void playDtmfTone(char digit) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700636 if (mConnectionService == null) {
637 Log.w(this, "playDtmfTone() request on a call without a connection service.");
Ihab Awad74549ec2014-03-10 15:33:25 -0700638 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700639 Log.i(this, "Send playDtmfTone to connection service for call %s", this);
640 mConnectionService.playDtmfTone(this, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700641 }
642 }
643
644 /**
645 * Stops playing any currently playing DTMF tone.
646 */
647 void stopDtmfTone() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700648 if (mConnectionService == null) {
649 Log.w(this, "stopDtmfTone() request on a call without a connectino service.");
Ihab Awad74549ec2014-03-10 15:33:25 -0700650 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700651 Log.i(this, "Send stopDtmfTone to connection service for call %s", this);
652 mConnectionService.stopDtmfTone(this);
Ihab Awad74549ec2014-03-10 15:33:25 -0700653 }
654 }
655
656 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700657 * Attempts to disconnect the call through the connection service.
Santos Cordon049b7b62014-01-30 05:34:26 -0800658 */
659 void disconnect() {
Santos Cordon766d04f2014-05-06 10:28:25 -0700660 if (mState == CallState.NEW) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700661 Log.v(this, "Aborting call %s", this);
662 abort();
Santos Cordon74d420b2014-05-07 14:38:47 -0700663 } else if (mState != CallState.ABORTED && mState != CallState.DISCONNECTED) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700664 Preconditions.checkNotNull(mConnectionService);
Santos Cordon766d04f2014-05-06 10:28:25 -0700665
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700666 Log.i(this, "Send disconnect to connection service for call: %s", this);
667 // The call isn't officially disconnected until the connection service confirms that the
668 // call was actually disconnected. Only then is the association between call and
669 // connection service severed, see {@link CallsManager#markCallAsDisconnected}.
670 mConnectionService.disconnect(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800671 }
672 }
673
Santos Cordon682fe6b2014-05-20 08:56:39 -0700674 void abort() {
675 if (mOutgoingCallProcessor != null) {
676 mOutgoingCallProcessor.abort();
677 }
678 }
679
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800680 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800681 * Answers the call if it is ringing.
682 */
683 void answer() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700684 Preconditions.checkNotNull(mConnectionService);
Santos Cordon61d0f702014-02-19 02:52:23 -0800685
686 // Check to verify that the call is still in the ringing state. A call can change states
687 // between the time the user hits 'answer' and Telecomm receives the command.
688 if (isRinging("answer")) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700689 // At this point, we are asking the connection service to answer but we don't assume
690 // that it will work. Instead, we wait until confirmation from the connectino service
691 // that the call is in a non-RINGING state before changing the UI. See
692 // {@link ConnectionServiceAdapter#setActive} and other set* methods.
693 mConnectionService.answer(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800694 }
695 }
696
697 /**
698 * Rejects the call if it is ringing.
Ihab Awadff7493a2014-06-10 13:47:44 -0700699 *
700 * @param rejectWithMessage Whether to send a text message as part of the call rejection.
701 * @param textMessage An optional text message to send as part of the rejection.
Santos Cordon61d0f702014-02-19 02:52:23 -0800702 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700703 void reject(boolean rejectWithMessage, String textMessage) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700704 Preconditions.checkNotNull(mConnectionService);
Santos Cordon61d0f702014-02-19 02:52:23 -0800705
706 // Check to verify that the call is still in the ringing state. A call can change states
707 // between the time the user hits 'reject' and Telecomm receives the command.
708 if (isRinging("reject")) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700709 mConnectionService.reject(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800710 }
711 }
712
713 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700714 * Puts the call on hold if it is currently active.
715 */
716 void hold() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700717 Preconditions.checkNotNull(mConnectionService);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700718
719 if (mState == CallState.ACTIVE) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700720 mConnectionService.hold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700721 }
722 }
723
724 /**
725 * Releases the call from hold if it is currently active.
726 */
727 void unhold() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700728 Preconditions.checkNotNull(mConnectionService);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700729
730 if (mState == CallState.ON_HOLD) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700731 mConnectionService.unhold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700732 }
733 }
734
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700735 /** Checks if this is a live call or not. */
736 boolean isAlive() {
737 switch (mState) {
738 case NEW:
739 case RINGING:
740 case DISCONNECTED:
741 case ABORTED:
742 return false;
743 default:
744 return true;
745 }
746 }
747
Santos Cordon40f78c22014-04-07 02:11:42 -0700748 boolean isActive() {
Ihab Awad84bfe472014-07-13 17:11:57 -0700749 return mState == CallState.ACTIVE;
Santos Cordon40f78c22014-04-07 02:11:42 -0700750 }
751
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700752 Bundle getExtras() {
753 return mExtras;
754 }
755
756 void setExtras(Bundle extras) {
757 mExtras = extras;
758 }
759
Santos Cordon5ba7f272014-05-28 13:59:49 -0700760 Uri getRingtone() {
761 return mCallerInfo == null ? null : mCallerInfo.contactRingtoneUri;
762 }
763
Evan Charlton352105c2014-06-03 14:10:54 -0700764 void onPostDialWait(String remaining) {
765 for (Listener l : mListeners) {
766 l.onPostDialWait(this, remaining);
767 }
768 }
769
770 void postDialContinue(boolean proceed) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700771 mConnectionService.onPostDialContinue(this, proceed);
Evan Charlton352105c2014-06-03 14:10:54 -0700772 }
773
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700774 void phoneAccountClicked() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700775 mConnectionService.onPhoneAccountClicked(this);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700776 }
777
Santos Cordona1610702014-06-04 20:22:56 -0700778 void conferenceInto(Call conferenceCall) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700779 if (mConnectionService == null) {
780 Log.w(this, "conference requested on a call without a connection service.");
Santos Cordona1610702014-06-04 20:22:56 -0700781 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700782 mConnectionService.conference(conferenceCall, this);
Santos Cordona1610702014-06-04 20:22:56 -0700783 }
784 }
785
786 void expireConference() {
787 // The conference call expired before we got a confirmation of the conference from the
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700788 // connection service...so start shutting down.
789 clearConnectionService();
Santos Cordona1610702014-06-04 20:22:56 -0700790 for (Listener l : mListeners) {
791 l.onExpiredConferenceCall(this);
792 }
793 }
794
795 void confirmConference() {
796 Log.v(this, "confirming Conf call %s", mListeners);
797 for (Listener l : mListeners) {
798 l.onConfirmedConferenceCall(this);
799 }
800 }
801
802 void splitFromConference() {
803 // TODO(santoscordon): todo
804 }
805
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700806 void swapWithBackgroundCall() {
807 mConnectionService.swapWithBackgroundCall(this);
808 }
809
Santos Cordona1610702014-06-04 20:22:56 -0700810 void setParentCall(Call parentCall) {
811 if (parentCall == this) {
812 Log.e(this, new Exception(), "setting the parent to self");
813 return;
814 }
815 Preconditions.checkState(parentCall == null || mParentCall == null);
816
817 Call oldParent = mParentCall;
818 if (mParentCall != null) {
819 mParentCall.removeChildCall(this);
820 }
821 mParentCall = parentCall;
822 if (mParentCall != null) {
823 mParentCall.addChildCall(this);
824 }
825
826 for (Listener l : mListeners) {
827 l.onParentChanged(this);
828 }
829 }
830
831 private void addChildCall(Call call) {
832 if (!mChildCalls.contains(call)) {
833 mChildCalls.add(call);
834
835 for (Listener l : mListeners) {
836 l.onChildrenChanged(this);
837 }
838 }
839 }
840
841 private void removeChildCall(Call call) {
842 if (mChildCalls.remove(call)) {
843 for (Listener l : mListeners) {
844 l.onChildrenChanged(this);
845 }
846 }
847 }
848
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800849 /**
Ihab Awadff7493a2014-06-10 13:47:44 -0700850 * Return whether the user can respond to this {@code Call} via an SMS message.
851 *
852 * @return true if the "Respond via SMS" feature should be enabled
853 * for this incoming call.
854 *
855 * The general rule is that we *do* allow "Respond via SMS" except for
856 * the few (relatively rare) cases where we know for sure it won't
857 * work, namely:
858 * - a bogus or blank incoming number
859 * - a call from a SIP address
860 * - a "call presentation" that doesn't allow the number to be revealed
861 *
862 * In all other cases, we allow the user to respond via SMS.
863 *
864 * Note that this behavior isn't perfect; for example we have no way
865 * to detect whether the incoming call is from a landline (with most
866 * networks at least), so we still enable this feature even though
867 * SMSes to that number will silently fail.
868 */
869 boolean isRespondViaSmsCapable() {
870 if (mState != CallState.RINGING) {
871 return false;
872 }
873
874 if (getHandle() == null) {
875 // No incoming number known or call presentation is "PRESENTATION_RESTRICTED", in
876 // other words, the user should not be able to see the incoming phone number.
877 return false;
878 }
879
880 if (PhoneNumberUtils.isUriNumber(getHandle().toString())) {
881 // The incoming number is actually a URI (i.e. a SIP address),
882 // not a regular PSTN phone number, and we can't send SMSes to
883 // SIP addresses.
884 // (TODO: That might still be possible eventually, though. Is
885 // there some SIP-specific equivalent to sending a text message?)
886 return false;
887 }
888
889 // Is there a valid SMS application on the phone?
890 if (SmsApplication.getDefaultRespondViaMessageApplication(TelecommApp.getInstance(),
891 true /*updateIfNeeded*/) == null) {
892 return false;
893 }
894
895 // TODO: with some carriers (in certain countries) you *can* actually
896 // tell whether a given number is a mobile phone or not. So in that
897 // case we could potentially return false here if the incoming call is
898 // from a land line.
899
900 // If none of the above special cases apply, it's OK to enable the
901 // "Respond via SMS" feature.
902 return true;
903 }
904
905 List<String> getCannedSmsResponses() {
906 return mCannedSmsResponses;
907 }
908
909 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800910 * @return True if the call is ringing, else logs the action name.
911 */
912 private boolean isRinging(String actionName) {
913 if (mState == CallState.RINGING) {
914 return true;
915 }
916
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800917 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800918 return false;
919 }
920
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800921 @SuppressWarnings("rawtypes")
922 private void decrementAssociatedCallCount(ServiceBinder binder) {
923 if (binder != null) {
924 binder.decrementAssociatedCallCount();
925 }
926 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700927
928 /**
929 * Looks up contact information based on the current handle.
930 */
931 private void startCallerInfoLookup() {
932 String number = mHandle == null ? null : mHandle.getSchemeSpecificPart();
933
934 mQueryToken++; // Updated so that previous queries can no longer set the information.
935 mCallerInfo = null;
936 if (!TextUtils.isEmpty(number)) {
Santos Cordon5ba7f272014-05-28 13:59:49 -0700937 Log.v(this, "Looking up information for: %s.", Log.piiHandle(number));
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700938 CallerInfoAsyncQuery.startQuery(
939 mQueryToken,
940 TelecommApp.getInstance(),
941 number,
942 sCallerInfoQueryListener,
943 this);
944 }
945 }
946
947 /**
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700948 * Saves the specified caller info if the specified token matches that of the last query
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700949 * that was made.
950 *
951 * @param callerInfo The new caller information to set.
952 * @param token The token used with this query.
953 */
954 private void setCallerInfo(CallerInfo callerInfo, int token) {
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700955 Preconditions.checkNotNull(callerInfo);
956
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700957 if (mQueryToken == token) {
958 mCallerInfo = callerInfo;
Santos Cordon5ba7f272014-05-28 13:59:49 -0700959 Log.i(this, "CallerInfo received for %s: %s", Log.piiHandle(mHandle), callerInfo);
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700960
Makoto Onukia1662d02014-07-10 15:31:59 -0700961 if (mCallerInfo.contactDisplayPhotoUri != null) {
962 Log.d(this, "Searching person uri %s for call %s",
963 mCallerInfo.contactDisplayPhotoUri, this);
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700964 ContactsAsyncHelper.startObtainPhotoAsync(
965 token,
966 TelecommApp.getInstance(),
Makoto Onukia1662d02014-07-10 15:31:59 -0700967 mCallerInfo.contactDisplayPhotoUri,
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700968 sPhotoLoadListener,
969 this);
Makoto Onukia1662d02014-07-10 15:31:59 -0700970 // Do not call onCallerInfoChanged yet in this case. We call it in setPhoto().
Santos Cordon64c7e962014-07-02 15:15:27 -0700971 } else {
972 for (Listener l : mListeners) {
973 l.onCallerInfoChanged(this);
974 }
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700975 }
Santos Cordon2174fb52014-05-29 08:22:56 -0700976
977 processDirectToVoicemail();
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700978 }
979 }
980
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700981 CallerInfo getCallerInfo() {
982 return mCallerInfo;
983 }
984
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700985 /**
986 * Saves the specified photo information if the specified token matches that of the last query.
987 *
988 * @param photo The photo as a drawable.
989 * @param photoIcon The photo as a small icon.
990 * @param token The token used with this query.
991 */
992 private void setPhoto(Drawable photo, Bitmap photoIcon, int token) {
993 if (mQueryToken == token) {
994 mCallerInfo.cachedPhoto = photo;
995 mCallerInfo.cachedPhotoIcon = photoIcon;
Santos Cordon64c7e962014-07-02 15:15:27 -0700996
997 for (Listener l : mListeners) {
998 l.onCallerInfoChanged(this);
999 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -07001000 }
1001 }
Ihab Awadff7493a2014-06-10 13:47:44 -07001002
1003 private void maybeLoadCannedSmsResponses() {
1004 if (mIsIncoming && isRespondViaSmsCapable() && !mCannedSmsResponsesLoadingStarted) {
1005 Log.d(this, "maybeLoadCannedSmsResponses: starting task to load messages");
1006 mCannedSmsResponsesLoadingStarted = true;
1007 RespondViaSmsManager.getInstance().loadCannedTextMessages(
1008 new Response<Void, List<String>>() {
1009 @Override
1010 public void onResult(Void request, List<String>... result) {
1011 if (result.length > 0) {
1012 Log.d(this, "maybeLoadCannedSmsResponses: got %s", result[0]);
1013 mCannedSmsResponses = result[0];
1014 for (Listener l : mListeners) {
1015 l.onCannedSmsResponsesLoaded(Call.this);
1016 }
1017 }
1018 }
1019
1020 @Override
1021 public void onError(Void request, int code, String msg) {
1022 Log.w(Call.this, "Error obtaining canned SMS responses: %d %s", code,
1023 msg);
1024 }
1025 }
1026 );
1027 } else {
1028 Log.d(this, "maybeLoadCannedSmsResponses: doing nothing");
1029 }
1030 }
Sai Cheemalapatib7157e92014-06-11 17:51:55 -07001031
1032 /**
1033 * Sets speakerphone option on when call begins.
1034 */
1035 public void setStartWithSpeakerphoneOn(boolean startWithSpeakerphone) {
1036 mSpeakerphoneOn = startWithSpeakerphone;
1037 }
1038
1039 /**
1040 * Returns speakerphone option.
1041 *
1042 * @return Whether or not speakerphone should be set automatically when call begins.
1043 */
1044 public boolean getStartWithSpeakerphoneOn() {
1045 return mSpeakerphoneOn;
1046 }
Andrew Leee9a77652014-06-26 13:07:57 -07001047
1048 /**
1049 * Sets a call video provider for the call.
1050 */
Nancy Chena65d41f2014-06-24 12:06:03 -07001051 public void setCallVideoProvider(ICallVideoProvider callVideoProvider) {
1052 mCallVideoProvider = callVideoProvider;
1053 for (Listener l : mListeners) {
1054 l.onCallVideoProviderChanged(Call.this);
1055 }
1056 }
1057
1058 /**
1059 * @return Return the call video Provider binder.
1060 */
1061 public ICallVideoProvider getCallVideoProvider() {
1062 return mCallVideoProvider;
Andrew Leee9a77652014-06-26 13:07:57 -07001063 }
Tyler Gunne19cc002014-07-01 11:32:53 -07001064
1065 /**
Tyler Gunnc4abd912014-07-08 14:22:10 -07001066 * The current video state for the call.
1067 * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
1068 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
1069 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
1070 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
1071 *
1072 * @return True if video is enabled.
1073 */
1074 public int getVideoState() {
1075 return mVideoState;
1076 }
1077
1078 /**
1079 * At the start of the call, determines the desired video state for the call.
1080 * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
1081 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
1082 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
1083 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
1084 *
1085 * @param videoState The desired video state for the call.
1086 */
1087 public void setVideoState(int videoState) {
1088 mVideoState = videoState;
1089 }
Sailesh Nepal7e669572014-07-08 21:29:12 -07001090
1091 public boolean getAudioModeIsVoip() {
1092 return mAudioModeIsVoip;
1093 }
1094
1095 public void setAudioModeIsVoip(boolean audioModeIsVoip) {
1096 mAudioModeIsVoip = audioModeIsVoip;
1097 for (Listener l : mListeners) {
Sailesh Nepal35faf8c2014-07-08 22:02:34 -07001098 l.onAudioModeIsVoipChanged(this);
1099 }
1100 }
1101
1102 public StatusHints getStatusHints() {
1103 return mStatusHints;
1104 }
1105
1106 public void setStatusHints(StatusHints statusHints) {
1107 mStatusHints = statusHints;
1108 for (Listener l : mListeners) {
1109 l.onStatusHintsChanged(this);
Sailesh Nepal7e669572014-07-08 21:29:12 -07001110 }
1111 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001112}