blob: b830499cf44ba1af5d8143ace6b69275b347890b [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
Sailesh Nepal905dfba2014-07-14 08:20:41 -070019import android.content.ComponentName;
Santos Cordon99c8a6f2014-05-28 18:28:47 -070020import android.content.ContentUris;
21import android.graphics.Bitmap;
22import android.graphics.drawable.Drawable;
Sailesh Nepalce704b92014-03-17 18:31:43 -070023import android.net.Uri;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070024import android.os.Bundle;
Santos Cordonfd71f4a2014-05-28 13:59:49 -070025import android.os.Handler;
Santos Cordon99c8a6f2014-05-28 18:28:47 -070026import android.provider.ContactsContract.Contacts;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070027import android.telecomm.CallPropertyPresentation;
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;
46
Ihab Awadff7493a2014-06-10 13:47:44 -070047import java.util.Collections;
Santos Cordona1610702014-06-04 20:22:56 -070048import java.util.LinkedList;
49import java.util.List;
Sailesh Nepal91990782014-03-08 17:45:52 -080050import java.util.Locale;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070051import java.util.Objects;
Ben Gilad61925612014-03-11 19:06:36 -070052import java.util.Set;
Santos Cordondfc66012014-07-15 13:41:40 -070053import java.util.concurrent.CopyOnWriteArraySet;
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 Nepal664837f2014-07-14 16:31:51 -070060final class Call implements CreateConnectionResponse {
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);
Andrew Lee4a796602014-07-11 17:23:03 -070084 void onVideoStateChanged(Call call);
Santos Cordon64c7e962014-07-02 15:15:27 -070085 }
86
87 abstract static class ListenerBase implements Listener {
88 @Override
89 public void onSuccessfulOutgoingCall(Call call) {}
90 @Override
91 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {}
92 @Override
93 public void onCancelledOutgoingCall(Call call) {}
94 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -070095 public void onSuccessfulIncomingCall(Call call) {}
Santos Cordon64c7e962014-07-02 15:15:27 -070096 @Override
97 public void onFailedIncomingCall(Call call) {}
98 @Override
99 public void onRequestingRingback(Call call, boolean requestingRingback) {}
100 @Override
101 public void onPostDialWait(Call call, String remaining) {}
102 @Override
Sailesh Nepale20bc972014-07-09 21:22:36 -0700103 public void onCallCapabilitiesChanged(Call call) {}
Santos Cordon64c7e962014-07-02 15:15:27 -0700104 @Override
105 public void onExpiredConferenceCall(Call call) {}
106 @Override
107 public void onConfirmedConferenceCall(Call call) {}
108 @Override
109 public void onParentChanged(Call call) {}
110 @Override
111 public void onChildrenChanged(Call call) {}
112 @Override
113 public void onCannedSmsResponsesLoaded(Call call) {}
114 @Override
115 public void onCallVideoProviderChanged(Call call) {}
116 @Override
Santos Cordon64c7e962014-07-02 15:15:27 -0700117 public void onCallerInfoChanged(Call call) {}
Sailesh Nepal7e669572014-07-08 21:29:12 -0700118 @Override
119 public void onAudioModeIsVoipChanged(Call call) {}
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700120 @Override
121 public void onStatusHintsChanged(Call call) {}
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700122 @Override
123 public void onHandleChanged(Call call) {}
124 @Override
125 public void onCallerDisplayNameChanged(Call call) {}
Andrew Lee4a796602014-07-11 17:23:03 -0700126 @Override
127 public void onVideoStateChanged(Call call) {}
Santos Cordon766d04f2014-05-06 10:28:25 -0700128 }
129
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700130 private static final OnQueryCompleteListener sCallerInfoQueryListener =
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700131 new OnQueryCompleteListener() {
132 /** ${inheritDoc} */
133 @Override
134 public void onQueryComplete(int token, Object cookie, CallerInfo callerInfo) {
135 if (cookie != null) {
136 ((Call) cookie).setCallerInfo(callerInfo, token);
137 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700138 }
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700139 };
140
141 private static final OnImageLoadCompleteListener sPhotoLoadListener =
142 new OnImageLoadCompleteListener() {
143 /** ${inheritDoc} */
144 @Override
145 public void onImageLoadComplete(
146 int token, Drawable photo, Bitmap photoIcon, Object cookie) {
147 if (cookie != null) {
148 ((Call) cookie).setPhoto(photo, photoIcon, token);
149 }
150 }
151 };
Ben Gilad0407fb22014-01-09 16:18:41 -0800152
Sailesh Nepal664837f2014-07-14 16:31:51 -0700153 private final Runnable mDirectToVoicemailRunnable = new Runnable() {
154 @Override
155 public void run() {
156 processDirectToVoicemail();
157 }
158 };
159
Sailesh Nepal810735e2014-03-18 18:15:46 -0700160 /** True if this is an incoming call. */
161 private final boolean mIsIncoming;
162
Ben Gilad0407fb22014-01-09 16:18:41 -0800163 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700164 * The time this call was created. Beyond logging and such, may also be used for bookkeeping
165 * and specifically for marking certain call attempts as failed attempts.
Ben Gilad0407fb22014-01-09 16:18:41 -0800166 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700167 private final long mCreationTimeMillis = System.currentTimeMillis();
168
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700169 /** The gateway information associated with this call. This stores the original call handle
170 * that the user is attempting to connect to via the gateway, the actual handle to dial in
171 * order to connect the call via the gateway, as well as the package name of the gateway
172 * service. */
173 private final GatewayInfo mGatewayInfo;
174
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700175 private PhoneAccount mPhoneAccount;
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700176
Santos Cordon2174fb52014-05-29 08:22:56 -0700177 private final Handler mHandler = new Handler();
178
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700179 private long mConnectTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -0800180
Santos Cordon61d0f702014-02-19 02:52:23 -0800181 /** The state of the call. */
182 private CallState mState;
183
184 /** The handle with which to establish this call. */
Sailesh Nepalce704b92014-03-17 18:31:43 -0700185 private Uri mHandle;
Santos Cordon61d0f702014-02-19 02:52:23 -0800186
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700187 /** The {@link CallPropertyPresentation} that controls how the handle is shown. */
188 private int mHandlePresentation;
189
190 /** The caller display name (CNAP) set by the connection service. */
191 private String mCallerDisplayName;
192
193 /** The {@link CallPropertyPresentation} that controls how the caller display name is shown. */
194 private int mCallerDisplayNamePresentation;
195
Ben Gilad0407fb22014-01-09 16:18:41 -0800196 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700197 * The connection service which is attempted or already connecting this call.
Santos Cordon681663d2014-01-30 04:32:15 -0800198 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700199 private ConnectionServiceWrapper mConnectionService;
Ben Gilad61925612014-03-11 19:06:36 -0700200
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700201 private boolean mIsEmergencyCall;
202
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700203 private boolean mSpeakerphoneOn;
204
Tyler Gunnc4abd912014-07-08 14:22:10 -0700205 private int mVideoState;
206
Ben Gilad61925612014-03-11 19:06:36 -0700207 /**
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700208 * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED.
209 * See {@link android.telephony.DisconnectCause}.
210 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700211 private int mDisconnectCause = DisconnectCause.NOT_VALID;
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700212
213 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700214 * Additional disconnect information provided by the connection service.
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700215 */
216 private String mDisconnectMessage;
217
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700218 /** Info used by the connection services. */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700219 private Bundle mExtras = Bundle.EMPTY;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700220
Santos Cordon766d04f2014-05-06 10:28:25 -0700221 /** Set of listeners on this call. */
Santos Cordondfc66012014-07-15 13:41:40 -0700222 private Set<Listener> mListeners = new CopyOnWriteArraySet<>();
Santos Cordon766d04f2014-05-06 10:28:25 -0700223
Sailesh Nepal664837f2014-07-14 16:31:51 -0700224 private CreateConnectionProcessor mCreateConnectionProcessor;
Santos Cordon682fe6b2014-05-20 08:56:39 -0700225
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700226 /** Caller information retrieved from the latest contact query. */
227 private CallerInfo mCallerInfo;
228
229 /** The latest token used with a contact info query. */
230 private int mQueryToken = 0;
231
Ihab Awadcb387ac2014-05-28 16:49:38 -0700232 /** Whether this call is requesting that Telecomm play the ringback tone on its behalf. */
233 private boolean mRequestingRingback = false;
234
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700235 /** Whether direct-to-voicemail query is pending. */
236 private boolean mDirectToVoicemailQueryPending;
Santos Cordon2174fb52014-05-29 08:22:56 -0700237
Sailesh Nepale20bc972014-07-09 21:22:36 -0700238 private int mCallCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700239
240 private boolean mIsConference = false;
241
242 private Call mParentCall = null;
243
244 private List<Call> mChildCalls = new LinkedList<>();
245
Ihab Awadff7493a2014-06-10 13:47:44 -0700246 /** Set of text message responses allowed for this call, if applicable. */
247 private List<String> mCannedSmsResponses = Collections.EMPTY_LIST;
248
249 /** Whether an attempt has been made to load the text message responses. */
250 private boolean mCannedSmsResponsesLoadingStarted = false;
251
Nancy Chena65d41f2014-06-24 12:06:03 -0700252 private ICallVideoProvider mCallVideoProvider;
253
Sailesh Nepal7e669572014-07-08 21:29:12 -0700254 private boolean mAudioModeIsVoip;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700255 private StatusHints mStatusHints;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700256 private final ConnectionServiceRepository mRepository;
Sailesh Nepal7e669572014-07-08 21:29:12 -0700257
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700258 /**
Ben Gilad0407fb22014-01-09 16:18:41 -0800259 * Persists the specified parameters and initializes the new instance.
260 *
261 * @param handle The handle to dial.
Yorke Lee33501632014-03-17 19:24:12 -0700262 * @param gatewayInfo Gateway information to use for the call.
Ihab Awad98a55602014-06-30 21:27:28 -0700263 * @param account Account information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700264 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800265 */
Sailesh Nepal664837f2014-07-14 16:31:51 -0700266 Call(
267 ConnectionServiceRepository repository,
268 Uri handle,
269 GatewayInfo gatewayInfo,
270 PhoneAccount account,
271 boolean isIncoming,
272 boolean isConference) {
Santos Cordona1610702014-06-04 20:22:56 -0700273 mState = isConference ? CallState.ACTIVE : CallState.NEW;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700274 mRepository = repository;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700275 setHandle(handle, CallPropertyPresentation.ALLOWED);
Yorke Lee33501632014-03-17 19:24:12 -0700276 mGatewayInfo = gatewayInfo;
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700277 mPhoneAccount = account;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700278 mIsIncoming = isIncoming;
Santos Cordona1610702014-06-04 20:22:56 -0700279 mIsConference = isConference;
Ihab Awadff7493a2014-06-10 13:47:44 -0700280 maybeLoadCannedSmsResponses();
Ben Gilad0407fb22014-01-09 16:18:41 -0800281 }
282
Santos Cordon766d04f2014-05-06 10:28:25 -0700283 void addListener(Listener listener) {
284 mListeners.add(listener);
285 }
286
287 void removeListener(Listener listener) {
288 mListeners.remove(listener);
289 }
290
Santos Cordon61d0f702014-02-19 02:52:23 -0800291 /** {@inheritDoc} */
292 @Override public String toString() {
Sailesh Nepal4538f012014-04-15 11:40:33 -0700293 String component = null;
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700294 if (mConnectionService != null && mConnectionService.getComponentName() != null) {
295 component = mConnectionService.getComponentName().flattenToShortString();
Sailesh Nepal4538f012014-04-15 11:40:33 -0700296 }
297 return String.format(Locale.US, "[%s, %s, %s]", mState, component, Log.piiHandle(mHandle));
Santos Cordon61d0f702014-02-19 02:52:23 -0800298 }
299
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800300 CallState getState() {
Santos Cordona1610702014-06-04 20:22:56 -0700301 if (mIsConference) {
302 if (!mChildCalls.isEmpty()) {
303 // If we have child calls, just return the child call.
304 return mChildCalls.get(0).getState();
305 }
306 return CallState.ACTIVE;
307 } else {
308 return mState;
309 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800310 }
311
312 /**
313 * Sets the call state. Although there exists the notion of appropriate state transitions
314 * (see {@link CallState}), in practice those expectations break down when cellular systems
315 * misbehave and they do this very often. The result is that we do not enforce state transitions
316 * and instead keep the code resilient to unexpected state changes.
317 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700318 void setState(CallState newState) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700319 Preconditions.checkState(newState != CallState.DISCONNECTED ||
320 mDisconnectCause != DisconnectCause.NOT_VALID);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700321 if (mState != newState) {
322 Log.v(this, "setState %s -> %s", mState, newState);
323 mState = newState;
Ihab Awadff7493a2014-06-10 13:47:44 -0700324 maybeLoadCannedSmsResponses();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700325 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800326 }
327
Ihab Awadcb387ac2014-05-28 16:49:38 -0700328 void setRequestingRingback(boolean requestingRingback) {
329 mRequestingRingback = requestingRingback;
330 for (Listener l : mListeners) {
331 l.onRequestingRingback(this, mRequestingRingback);
332 }
333 }
334
335 boolean isRequestingRingback() {
336 return mRequestingRingback;
337 }
338
Sailesh Nepalce704b92014-03-17 18:31:43 -0700339 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800340 return mHandle;
341 }
342
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700343 int getHandlePresentation() {
344 return mHandlePresentation;
345 }
346
347 void setHandle(Uri handle, int presentation) {
348 if (!Objects.equals(handle, mHandle) || presentation != mHandlePresentation) {
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700349 mHandle = handle;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700350 mHandlePresentation = presentation;
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700351 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
Yorke Lee66255452014-06-05 08:09:24 -0700352 TelecommApp.getInstance(), mHandle.getSchemeSpecificPart());
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700353 startCallerInfoLookup();
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700354 for (Listener l : mListeners) {
355 l.onHandleChanged(this);
356 }
357 }
358 }
359
360 String getCallerDisplayName() {
361 return mCallerDisplayName;
362 }
363
364 int getCallerDisplayNamePresentation() {
365 return mCallerDisplayNamePresentation;
366 }
367
368 void setCallerDisplayName(String callerDisplayName, int presentation) {
369 if (!TextUtils.equals(callerDisplayName, mCallerDisplayName) ||
370 presentation != mCallerDisplayNamePresentation) {
371 mCallerDisplayName = callerDisplayName;
372 mCallerDisplayNamePresentation = presentation;
373 for (Listener l : mListeners) {
374 l.onCallerDisplayNameChanged(this);
375 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700376 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700377 }
378
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700379 String getName() {
380 return mCallerInfo == null ? null : mCallerInfo.name;
381 }
382
383 Bitmap getPhotoIcon() {
384 return mCallerInfo == null ? null : mCallerInfo.cachedPhotoIcon;
385 }
386
387 Drawable getPhoto() {
388 return mCallerInfo == null ? null : mCallerInfo.cachedPhoto;
389 }
390
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700391 /**
392 * @param disconnectCause The reason for the disconnection, any of
393 * {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700394 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700395 */
396 void setDisconnectCause(int disconnectCause, String disconnectMessage) {
397 // TODO: Consider combining this method with a setDisconnected() method that is totally
398 // separate from setState.
399 mDisconnectCause = disconnectCause;
400 mDisconnectMessage = disconnectMessage;
401 }
402
403 int getDisconnectCause() {
404 return mDisconnectCause;
405 }
406
407 String getDisconnectMessage() {
408 return mDisconnectMessage;
409 }
410
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700411 boolean isEmergencyCall() {
412 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800413 }
414
Yorke Lee33501632014-03-17 19:24:12 -0700415 /**
416 * @return The original handle this call is associated with. In-call services should use this
417 * handle when indicating in their UI the handle that is being called.
418 */
419 public Uri getOriginalHandle() {
420 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
421 return mGatewayInfo.getOriginalHandle();
422 }
423 return getHandle();
424 }
425
426 GatewayInfo getGatewayInfo() {
427 return mGatewayInfo;
428 }
429
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700430 PhoneAccount getPhoneAccount() {
431 return mPhoneAccount;
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700432 }
433
Sailesh Nepal810735e2014-03-18 18:15:46 -0700434 boolean isIncoming() {
435 return mIsIncoming;
436 }
437
Ben Gilad0407fb22014-01-09 16:18:41 -0800438 /**
439 * @return The "age" of this call object in milliseconds, which typically also represents the
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700440 * period since this call was added to the set pending outgoing calls, see
441 * mCreationTimeMillis.
Ben Gilad0407fb22014-01-09 16:18:41 -0800442 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700443 long getAgeMillis() {
444 return System.currentTimeMillis() - mCreationTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -0800445 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800446
Yorke Leef98fb572014-03-05 10:56:55 -0800447 /**
448 * @return The time when this call object was created and added to the set of pending outgoing
449 * calls.
450 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700451 long getCreationTimeMillis() {
452 return mCreationTimeMillis;
453 }
454
455 long getConnectTimeMillis() {
456 return mConnectTimeMillis;
457 }
458
459 void setConnectTimeMillis(long connectTimeMillis) {
460 mConnectTimeMillis = connectTimeMillis;
Yorke Leef98fb572014-03-05 10:56:55 -0800461 }
462
Sailesh Nepale20bc972014-07-09 21:22:36 -0700463 int getCallCapabilities() {
464 return mCallCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700465 }
466
Sailesh Nepale20bc972014-07-09 21:22:36 -0700467 void setCallCapabilities(int callCapabilities) {
468 if (mCallCapabilities != callCapabilities) {
469 mCallCapabilities = callCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700470 for (Listener l : mListeners) {
Sailesh Nepale20bc972014-07-09 21:22:36 -0700471 l.onCallCapabilitiesChanged(this);
Santos Cordona1610702014-06-04 20:22:56 -0700472 }
473 }
474 }
475
476 Call getParentCall() {
477 return mParentCall;
478 }
479
480 List<Call> getChildCalls() {
481 return mChildCalls;
482 }
483
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700484 ConnectionServiceWrapper getConnectionService() {
485 return mConnectionService;
Santos Cordon681663d2014-01-30 04:32:15 -0800486 }
487
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700488 void setConnectionService(ConnectionServiceWrapper service) {
489 Preconditions.checkNotNull(service);
490
491 clearConnectionService();
492
493 service.incrementAssociatedCallCount();
494 mConnectionService = service;
495 mConnectionService.addCall(this);
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700496 }
497
498 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700499 * Clears the associated connection service.
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700500 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700501 void clearConnectionService() {
502 if (mConnectionService != null) {
503 ConnectionServiceWrapper serviceTemp = mConnectionService;
504 mConnectionService = null;
505 serviceTemp.removeCall(this);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700506
507 // Decrementing the count can cause the service to unbind, which itself can trigger the
508 // service-death code. Since the service death code tries to clean up any associated
509 // calls, we need to make sure to remove that information (e.g., removeCall()) before
510 // we decrement. Technically, invoking removeCall() prior to decrementing is all that is
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700511 // necessary, but cleaning up mConnectionService prior to triggering an unbind is good
512 // to do.
513 decrementAssociatedCallCount(serviceTemp);
Yorke Leeadee12d2014-03-13 12:08:30 -0700514 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800515 }
516
Sailesh Nepal664837f2014-07-14 16:31:51 -0700517 private void processDirectToVoicemail() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700518 if (mDirectToVoicemailQueryPending) {
Santos Cordon2174fb52014-05-29 08:22:56 -0700519 if (mCallerInfo != null && mCallerInfo.shouldSendToVoicemail) {
520 Log.i(this, "Directing call to voicemail: %s.", this);
521 // TODO(santoscordon): Once we move State handling from CallsManager to Call, we
522 // will not need to set RINGING state prior to calling reject.
523 setState(CallState.RINGING);
Ihab Awadff7493a2014-06-10 13:47:44 -0700524 reject(false, null);
Santos Cordon2174fb52014-05-29 08:22:56 -0700525 } else {
526 // TODO(santoscordon): Make this class (not CallsManager) responsible for changing
527 // the call state to RINGING.
528
529 // TODO(santoscordon): Replace this with state transition to RINGING.
530 for (Listener l : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700531 l.onSuccessfulIncomingCall(this);
Santos Cordon2174fb52014-05-29 08:22:56 -0700532 }
533 }
534
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700535 mDirectToVoicemailQueryPending = false;
Santos Cordon766d04f2014-05-06 10:28:25 -0700536 }
537 }
538
Santos Cordon766d04f2014-05-06 10:28:25 -0700539 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700540 * Starts the create connection sequence. Upon completion, there should exist an active
541 * connection through a connection service (or the call will have failed).
Santos Cordon766d04f2014-05-06 10:28:25 -0700542 */
Sailesh Nepal664837f2014-07-14 16:31:51 -0700543 void startCreateConnection() {
544 Preconditions.checkState(mCreateConnectionProcessor == null);
545 mCreateConnectionProcessor = new CreateConnectionProcessor(this, mRepository, this);
546 mCreateConnectionProcessor.process();
Santos Cordon766d04f2014-05-06 10:28:25 -0700547 }
548
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700549 @Override
Sailesh Nepal664837f2014-07-14 16:31:51 -0700550 public void handleCreateConnectionSuccessful(ConnectionRequest request) {
551 mCreateConnectionProcessor = null;
552 mPhoneAccount = request.getAccount();
553
554 if (mIsIncoming) {
555 // We do not handle incoming calls immediately when they are verified by the connection
556 // service. We allow the caller-info-query code to execute first so that we can read the
557 // direct-to-voicemail property before deciding if we want to show the incoming call to
558 // the user or if we want to reject the call.
559 mDirectToVoicemailQueryPending = true;
560
561 // Setting the handle triggers the caller info lookup code.
562 setHandle(request.getHandle(), request.getHandlePresentation());
563
564 // Timeout the direct-to-voicemail lookup execution so that we dont wait too long before
565 // showing the user the incoming call screen.
566 mHandler.postDelayed(mDirectToVoicemailRunnable, Timeouts.getDirectToVoicemailMillis());
567 } else {
568 for (Listener l : mListeners) {
569 l.onSuccessfulOutgoingCall(this);
570 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700571 }
572 }
573
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700574 @Override
Sailesh Nepal664837f2014-07-14 16:31:51 -0700575 public void handleCreateConnectionFailed(int code, String msg) {
576 mCreateConnectionProcessor = null;
577 if (mIsIncoming) {
578 clearConnectionService();
579 setDisconnectCause(code, null);
580 setState(CallState.DISCONNECTED);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700581
Sailesh Nepal664837f2014-07-14 16:31:51 -0700582 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
583 for (int i = 0; i < listeners.length; i++) {
584 listeners[i].onFailedIncomingCall(this);
585 }
586 } else {
587 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
588 for (int i = 0; i < listeners.length; i++) {
589 listeners[i].onFailedOutgoingCall(this, code, msg);
590 }
591 clearConnectionService();
592 }
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700593 }
594
595 @Override
Sailesh Nepal664837f2014-07-14 16:31:51 -0700596 public void handleCreateConnectionCancelled() {
597 mCreateConnectionProcessor = null;
598 if (mIsIncoming) {
599 clearConnectionService();
600 setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
601 setState(CallState.DISCONNECTED);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700602
Sailesh Nepal664837f2014-07-14 16:31:51 -0700603 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
604 for (int i = 0; i < listeners.length; i++) {
605 listeners[i].onFailedIncomingCall(this);
606 }
607 } else {
608 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
609 for (int i = 0; i < listeners.length; i++) {
610 listeners[i].onCancelledOutgoingCall(this);
611 }
612 clearConnectionService();
613 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700614 }
615
616 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700617 * Plays the specified DTMF tone.
618 */
619 void playDtmfTone(char digit) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700620 if (mConnectionService == null) {
621 Log.w(this, "playDtmfTone() request on a call without a connection service.");
Ihab Awad74549ec2014-03-10 15:33:25 -0700622 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700623 Log.i(this, "Send playDtmfTone to connection service for call %s", this);
624 mConnectionService.playDtmfTone(this, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700625 }
626 }
627
628 /**
629 * Stops playing any currently playing DTMF tone.
630 */
631 void stopDtmfTone() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700632 if (mConnectionService == null) {
633 Log.w(this, "stopDtmfTone() request on a call without a connectino service.");
Ihab Awad74549ec2014-03-10 15:33:25 -0700634 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700635 Log.i(this, "Send stopDtmfTone to connection service for call %s", this);
636 mConnectionService.stopDtmfTone(this);
Ihab Awad74549ec2014-03-10 15:33:25 -0700637 }
638 }
639
640 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700641 * Attempts to disconnect the call through the connection service.
Santos Cordon049b7b62014-01-30 05:34:26 -0800642 */
643 void disconnect() {
Santos Cordon766d04f2014-05-06 10:28:25 -0700644 if (mState == CallState.NEW) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700645 Log.v(this, "Aborting call %s", this);
646 abort();
Santos Cordon74d420b2014-05-07 14:38:47 -0700647 } else if (mState != CallState.ABORTED && mState != CallState.DISCONNECTED) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700648 Preconditions.checkNotNull(mConnectionService);
Santos Cordon766d04f2014-05-06 10:28:25 -0700649
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700650 Log.i(this, "Send disconnect to connection service for call: %s", this);
651 // The call isn't officially disconnected until the connection service confirms that the
652 // call was actually disconnected. Only then is the association between call and
653 // connection service severed, see {@link CallsManager#markCallAsDisconnected}.
654 mConnectionService.disconnect(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800655 }
656 }
657
Santos Cordon682fe6b2014-05-20 08:56:39 -0700658 void abort() {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700659 if (mCreateConnectionProcessor != null) {
660 mCreateConnectionProcessor.abort();
Santos Cordon682fe6b2014-05-20 08:56:39 -0700661 }
662 }
663
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800664 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800665 * Answers the call if it is ringing.
666 */
667 void answer() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700668 Preconditions.checkNotNull(mConnectionService);
Santos Cordon61d0f702014-02-19 02:52:23 -0800669
670 // Check to verify that the call is still in the ringing state. A call can change states
671 // between the time the user hits 'answer' and Telecomm receives the command.
672 if (isRinging("answer")) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700673 // At this point, we are asking the connection service to answer but we don't assume
674 // that it will work. Instead, we wait until confirmation from the connectino service
675 // that the call is in a non-RINGING state before changing the UI. See
676 // {@link ConnectionServiceAdapter#setActive} and other set* methods.
677 mConnectionService.answer(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800678 }
679 }
680
681 /**
682 * Rejects the call if it is ringing.
Ihab Awadff7493a2014-06-10 13:47:44 -0700683 *
684 * @param rejectWithMessage Whether to send a text message as part of the call rejection.
685 * @param textMessage An optional text message to send as part of the rejection.
Santos Cordon61d0f702014-02-19 02:52:23 -0800686 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700687 void reject(boolean rejectWithMessage, String textMessage) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700688 Preconditions.checkNotNull(mConnectionService);
Santos Cordon61d0f702014-02-19 02:52:23 -0800689
690 // Check to verify that the call is still in the ringing state. A call can change states
691 // between the time the user hits 'reject' and Telecomm receives the command.
692 if (isRinging("reject")) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700693 mConnectionService.reject(this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800694 }
695 }
696
697 /**
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700698 * Puts the call on hold if it is currently active.
699 */
700 void hold() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700701 Preconditions.checkNotNull(mConnectionService);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700702
703 if (mState == CallState.ACTIVE) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700704 mConnectionService.hold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700705 }
706 }
707
708 /**
709 * Releases the call from hold if it is currently active.
710 */
711 void unhold() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700712 Preconditions.checkNotNull(mConnectionService);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700713
714 if (mState == CallState.ON_HOLD) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700715 mConnectionService.unhold(this);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700716 }
717 }
718
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700719 /** Checks if this is a live call or not. */
720 boolean isAlive() {
721 switch (mState) {
722 case NEW:
723 case RINGING:
724 case DISCONNECTED:
725 case ABORTED:
726 return false;
727 default:
728 return true;
729 }
730 }
731
Santos Cordon40f78c22014-04-07 02:11:42 -0700732 boolean isActive() {
Ihab Awad84bfe472014-07-13 17:11:57 -0700733 return mState == CallState.ACTIVE;
Santos Cordon40f78c22014-04-07 02:11:42 -0700734 }
735
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700736 Bundle getExtras() {
737 return mExtras;
738 }
739
740 void setExtras(Bundle extras) {
741 mExtras = extras;
742 }
743
Santos Cordon5ba7f272014-05-28 13:59:49 -0700744 Uri getRingtone() {
745 return mCallerInfo == null ? null : mCallerInfo.contactRingtoneUri;
746 }
747
Evan Charlton352105c2014-06-03 14:10:54 -0700748 void onPostDialWait(String remaining) {
749 for (Listener l : mListeners) {
750 l.onPostDialWait(this, remaining);
751 }
752 }
753
754 void postDialContinue(boolean proceed) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700755 mConnectionService.onPostDialContinue(this, proceed);
Evan Charlton352105c2014-06-03 14:10:54 -0700756 }
757
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700758 void phoneAccountClicked() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700759 mConnectionService.onPhoneAccountClicked(this);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700760 }
761
Santos Cordona1610702014-06-04 20:22:56 -0700762 void conferenceInto(Call conferenceCall) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700763 if (mConnectionService == null) {
764 Log.w(this, "conference requested on a call without a connection service.");
Santos Cordona1610702014-06-04 20:22:56 -0700765 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700766 mConnectionService.conference(conferenceCall, this);
Santos Cordona1610702014-06-04 20:22:56 -0700767 }
768 }
769
770 void expireConference() {
771 // The conference call expired before we got a confirmation of the conference from the
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700772 // connection service...so start shutting down.
773 clearConnectionService();
Santos Cordona1610702014-06-04 20:22:56 -0700774 for (Listener l : mListeners) {
775 l.onExpiredConferenceCall(this);
776 }
777 }
778
779 void confirmConference() {
780 Log.v(this, "confirming Conf call %s", mListeners);
781 for (Listener l : mListeners) {
782 l.onConfirmedConferenceCall(this);
783 }
784 }
785
786 void splitFromConference() {
787 // TODO(santoscordon): todo
788 }
789
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700790 void swapWithBackgroundCall() {
791 mConnectionService.swapWithBackgroundCall(this);
792 }
793
Santos Cordona1610702014-06-04 20:22:56 -0700794 void setParentCall(Call parentCall) {
795 if (parentCall == this) {
796 Log.e(this, new Exception(), "setting the parent to self");
797 return;
798 }
799 Preconditions.checkState(parentCall == null || mParentCall == null);
800
801 Call oldParent = mParentCall;
802 if (mParentCall != null) {
803 mParentCall.removeChildCall(this);
804 }
805 mParentCall = parentCall;
806 if (mParentCall != null) {
807 mParentCall.addChildCall(this);
808 }
809
810 for (Listener l : mListeners) {
811 l.onParentChanged(this);
812 }
813 }
814
815 private void addChildCall(Call call) {
816 if (!mChildCalls.contains(call)) {
817 mChildCalls.add(call);
818
819 for (Listener l : mListeners) {
820 l.onChildrenChanged(this);
821 }
822 }
823 }
824
825 private void removeChildCall(Call call) {
826 if (mChildCalls.remove(call)) {
827 for (Listener l : mListeners) {
828 l.onChildrenChanged(this);
829 }
830 }
831 }
832
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800833 /**
Ihab Awadff7493a2014-06-10 13:47:44 -0700834 * Return whether the user can respond to this {@code Call} via an SMS message.
835 *
836 * @return true if the "Respond via SMS" feature should be enabled
837 * for this incoming call.
838 *
839 * The general rule is that we *do* allow "Respond via SMS" except for
840 * the few (relatively rare) cases where we know for sure it won't
841 * work, namely:
842 * - a bogus or blank incoming number
843 * - a call from a SIP address
844 * - a "call presentation" that doesn't allow the number to be revealed
845 *
846 * In all other cases, we allow the user to respond via SMS.
847 *
848 * Note that this behavior isn't perfect; for example we have no way
849 * to detect whether the incoming call is from a landline (with most
850 * networks at least), so we still enable this feature even though
851 * SMSes to that number will silently fail.
852 */
853 boolean isRespondViaSmsCapable() {
854 if (mState != CallState.RINGING) {
855 return false;
856 }
857
858 if (getHandle() == null) {
859 // No incoming number known or call presentation is "PRESENTATION_RESTRICTED", in
860 // other words, the user should not be able to see the incoming phone number.
861 return false;
862 }
863
864 if (PhoneNumberUtils.isUriNumber(getHandle().toString())) {
865 // The incoming number is actually a URI (i.e. a SIP address),
866 // not a regular PSTN phone number, and we can't send SMSes to
867 // SIP addresses.
868 // (TODO: That might still be possible eventually, though. Is
869 // there some SIP-specific equivalent to sending a text message?)
870 return false;
871 }
872
873 // Is there a valid SMS application on the phone?
874 if (SmsApplication.getDefaultRespondViaMessageApplication(TelecommApp.getInstance(),
875 true /*updateIfNeeded*/) == null) {
876 return false;
877 }
878
879 // TODO: with some carriers (in certain countries) you *can* actually
880 // tell whether a given number is a mobile phone or not. So in that
881 // case we could potentially return false here if the incoming call is
882 // from a land line.
883
884 // If none of the above special cases apply, it's OK to enable the
885 // "Respond via SMS" feature.
886 return true;
887 }
888
889 List<String> getCannedSmsResponses() {
890 return mCannedSmsResponses;
891 }
892
893 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800894 * @return True if the call is ringing, else logs the action name.
895 */
896 private boolean isRinging(String actionName) {
897 if (mState == CallState.RINGING) {
898 return true;
899 }
900
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800901 Log.i(this, "Request to %s a non-ringing call %s", actionName, this);
Santos Cordon61d0f702014-02-19 02:52:23 -0800902 return false;
903 }
904
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800905 @SuppressWarnings("rawtypes")
906 private void decrementAssociatedCallCount(ServiceBinder binder) {
907 if (binder != null) {
908 binder.decrementAssociatedCallCount();
909 }
910 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700911
912 /**
913 * Looks up contact information based on the current handle.
914 */
915 private void startCallerInfoLookup() {
916 String number = mHandle == null ? null : mHandle.getSchemeSpecificPart();
917
918 mQueryToken++; // Updated so that previous queries can no longer set the information.
919 mCallerInfo = null;
920 if (!TextUtils.isEmpty(number)) {
Santos Cordon5ba7f272014-05-28 13:59:49 -0700921 Log.v(this, "Looking up information for: %s.", Log.piiHandle(number));
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700922 CallerInfoAsyncQuery.startQuery(
923 mQueryToken,
924 TelecommApp.getInstance(),
925 number,
926 sCallerInfoQueryListener,
927 this);
928 }
929 }
930
931 /**
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700932 * Saves the specified caller info if the specified token matches that of the last query
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700933 * that was made.
934 *
935 * @param callerInfo The new caller information to set.
936 * @param token The token used with this query.
937 */
938 private void setCallerInfo(CallerInfo callerInfo, int token) {
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700939 Preconditions.checkNotNull(callerInfo);
940
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700941 if (mQueryToken == token) {
942 mCallerInfo = callerInfo;
Santos Cordon5ba7f272014-05-28 13:59:49 -0700943 Log.i(this, "CallerInfo received for %s: %s", Log.piiHandle(mHandle), callerInfo);
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700944
Makoto Onukia1662d02014-07-10 15:31:59 -0700945 if (mCallerInfo.contactDisplayPhotoUri != null) {
946 Log.d(this, "Searching person uri %s for call %s",
947 mCallerInfo.contactDisplayPhotoUri, this);
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700948 ContactsAsyncHelper.startObtainPhotoAsync(
949 token,
950 TelecommApp.getInstance(),
Makoto Onukia1662d02014-07-10 15:31:59 -0700951 mCallerInfo.contactDisplayPhotoUri,
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700952 sPhotoLoadListener,
953 this);
Makoto Onukia1662d02014-07-10 15:31:59 -0700954 // Do not call onCallerInfoChanged yet in this case. We call it in setPhoto().
Santos Cordon64c7e962014-07-02 15:15:27 -0700955 } else {
956 for (Listener l : mListeners) {
957 l.onCallerInfoChanged(this);
958 }
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700959 }
Santos Cordon2174fb52014-05-29 08:22:56 -0700960
961 processDirectToVoicemail();
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700962 }
963 }
964
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700965 CallerInfo getCallerInfo() {
966 return mCallerInfo;
967 }
968
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700969 /**
970 * Saves the specified photo information if the specified token matches that of the last query.
971 *
972 * @param photo The photo as a drawable.
973 * @param photoIcon The photo as a small icon.
974 * @param token The token used with this query.
975 */
976 private void setPhoto(Drawable photo, Bitmap photoIcon, int token) {
977 if (mQueryToken == token) {
978 mCallerInfo.cachedPhoto = photo;
979 mCallerInfo.cachedPhotoIcon = photoIcon;
Santos Cordon64c7e962014-07-02 15:15:27 -0700980
981 for (Listener l : mListeners) {
982 l.onCallerInfoChanged(this);
983 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700984 }
985 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700986
987 private void maybeLoadCannedSmsResponses() {
988 if (mIsIncoming && isRespondViaSmsCapable() && !mCannedSmsResponsesLoadingStarted) {
989 Log.d(this, "maybeLoadCannedSmsResponses: starting task to load messages");
990 mCannedSmsResponsesLoadingStarted = true;
991 RespondViaSmsManager.getInstance().loadCannedTextMessages(
992 new Response<Void, List<String>>() {
993 @Override
994 public void onResult(Void request, List<String>... result) {
995 if (result.length > 0) {
996 Log.d(this, "maybeLoadCannedSmsResponses: got %s", result[0]);
997 mCannedSmsResponses = result[0];
998 for (Listener l : mListeners) {
999 l.onCannedSmsResponsesLoaded(Call.this);
1000 }
1001 }
1002 }
1003
1004 @Override
1005 public void onError(Void request, int code, String msg) {
1006 Log.w(Call.this, "Error obtaining canned SMS responses: %d %s", code,
1007 msg);
1008 }
1009 }
1010 );
1011 } else {
1012 Log.d(this, "maybeLoadCannedSmsResponses: doing nothing");
1013 }
1014 }
Sai Cheemalapatib7157e92014-06-11 17:51:55 -07001015
1016 /**
1017 * Sets speakerphone option on when call begins.
1018 */
1019 public void setStartWithSpeakerphoneOn(boolean startWithSpeakerphone) {
1020 mSpeakerphoneOn = startWithSpeakerphone;
1021 }
1022
1023 /**
1024 * Returns speakerphone option.
1025 *
1026 * @return Whether or not speakerphone should be set automatically when call begins.
1027 */
1028 public boolean getStartWithSpeakerphoneOn() {
1029 return mSpeakerphoneOn;
1030 }
Andrew Leee9a77652014-06-26 13:07:57 -07001031
1032 /**
1033 * Sets a call video provider for the call.
1034 */
Nancy Chena65d41f2014-06-24 12:06:03 -07001035 public void setCallVideoProvider(ICallVideoProvider callVideoProvider) {
1036 mCallVideoProvider = callVideoProvider;
1037 for (Listener l : mListeners) {
1038 l.onCallVideoProviderChanged(Call.this);
1039 }
1040 }
1041
1042 /**
1043 * @return Return the call video Provider binder.
1044 */
1045 public ICallVideoProvider getCallVideoProvider() {
1046 return mCallVideoProvider;
Andrew Leee9a77652014-06-26 13:07:57 -07001047 }
Tyler Gunne19cc002014-07-01 11:32:53 -07001048
1049 /**
Tyler Gunnc4abd912014-07-08 14:22:10 -07001050 * The current video state for the call.
1051 * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
1052 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
1053 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
1054 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
1055 *
1056 * @return True if video is enabled.
1057 */
1058 public int getVideoState() {
1059 return mVideoState;
1060 }
1061
1062 /**
1063 * At the start of the call, determines the desired video state for the call.
1064 * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
1065 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
1066 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
1067 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
1068 *
1069 * @param videoState The desired video state for the call.
1070 */
1071 public void setVideoState(int videoState) {
1072 mVideoState = videoState;
Andrew Lee4a796602014-07-11 17:23:03 -07001073 for (Listener l : mListeners) {
1074 l.onVideoStateChanged(this);
1075 }
Tyler Gunnc4abd912014-07-08 14:22:10 -07001076 }
Sailesh Nepal7e669572014-07-08 21:29:12 -07001077
1078 public boolean getAudioModeIsVoip() {
1079 return mAudioModeIsVoip;
1080 }
1081
1082 public void setAudioModeIsVoip(boolean audioModeIsVoip) {
1083 mAudioModeIsVoip = audioModeIsVoip;
1084 for (Listener l : mListeners) {
Sailesh Nepal35faf8c2014-07-08 22:02:34 -07001085 l.onAudioModeIsVoipChanged(this);
1086 }
1087 }
1088
1089 public StatusHints getStatusHints() {
1090 return mStatusHints;
1091 }
1092
1093 public void setStatusHints(StatusHints statusHints) {
1094 mStatusHints = statusHints;
1095 for (Listener l : mListeners) {
1096 l.onStatusHintsChanged(this);
Sailesh Nepal7e669572014-07-08 21:29:12 -07001097 }
1098 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001099}