blob: 2a10558d75d24620c068d71cd56b4df2052ef5bb [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 Nepal9d58de52014-07-18 14:53:19 -070019import android.app.PendingIntent;
Santos Cordon99c8a6f2014-05-28 18:28:47 -070020import 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;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070025import android.telecomm.CallPropertyPresentation;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080026import android.telecomm.CallState;
Sailesh Nepalc92c4362014-07-04 18:33:21 -070027import android.telecomm.ConnectionRequest;
Yorke Lee33501632014-03-17 19:24:12 -070028import android.telecomm.GatewayInfo;
Ihab Awad98a55602014-06-30 21:27:28 -070029import android.telecomm.PhoneAccount;
Ihab Awadff7493a2014-06-10 13:47:44 -070030import android.telecomm.Response;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070031import android.telecomm.StatusHints;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070032import android.telephony.DisconnectCause;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070033import android.telephony.PhoneNumberUtils;
Santos Cordonfd71f4a2014-05-28 13:59:49 -070034import android.text.TextUtils;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080035
Nancy Chena65d41f2014-06-24 12:06:03 -070036import com.android.internal.telecomm.ICallVideoProvider;
Santos Cordonfd71f4a2014-05-28 13:59:49 -070037import com.android.internal.telephony.CallerInfo;
38import com.android.internal.telephony.CallerInfoAsyncQuery;
39import com.android.internal.telephony.CallerInfoAsyncQuery.OnQueryCompleteListener;
Ihab Awadff7493a2014-06-10 13:47:44 -070040import com.android.internal.telephony.SmsApplication;
Santos Cordon99c8a6f2014-05-28 18:28:47 -070041import com.android.telecomm.ContactsAsyncHelper.OnImageLoadCompleteListener;
Santos Cordon61d0f702014-02-19 02:52:23 -080042import com.google.common.base.Preconditions;
43
Ihab Awadff7493a2014-06-10 13:47:44 -070044import java.util.Collections;
Santos Cordona1610702014-06-04 20:22:56 -070045import java.util.LinkedList;
46import java.util.List;
Sailesh Nepal91990782014-03-08 17:45:52 -080047import java.util.Locale;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070048import java.util.Objects;
Ben Gilad61925612014-03-11 19:06:36 -070049import java.util.Set;
Santos Cordondfc66012014-07-15 13:41:40 -070050import java.util.concurrent.CopyOnWriteArraySet;
Ben Gilad0407fb22014-01-09 16:18:41 -080051
Ben Gilad2495d572014-01-09 17:26:19 -080052/**
53 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
54 * from the time the call intent was received by Telecomm (vs. the time the call was
55 * connected etc).
56 */
Sailesh Nepal664837f2014-07-14 16:31:51 -070057final class Call implements CreateConnectionResponse {
Santos Cordon766d04f2014-05-06 10:28:25 -070058 /**
59 * Listener for events on the call.
60 */
61 interface Listener {
62 void onSuccessfulOutgoingCall(Call call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -070063 void onFailedOutgoingCall(Call call, int errorCode, String errorMsg);
64 void onCancelledOutgoingCall(Call call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 void onSuccessfulIncomingCall(Call call);
Santos Cordon766d04f2014-05-06 10:28:25 -070066 void onFailedIncomingCall(Call call);
Ihab Awadcb387ac2014-05-28 16:49:38 -070067 void onRequestingRingback(Call call, boolean requestingRingback);
Evan Charlton352105c2014-06-03 14:10:54 -070068 void onPostDialWait(Call call, String remaining);
Sailesh Nepale20bc972014-07-09 21:22:36 -070069 void onCallCapabilitiesChanged(Call call);
Santos Cordona1610702014-06-04 20:22:56 -070070 void onExpiredConferenceCall(Call call);
71 void onConfirmedConferenceCall(Call call);
72 void onParentChanged(Call call);
73 void onChildrenChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070074 void onCannedSmsResponsesLoaded(Call call);
Nancy Chena65d41f2014-06-24 12:06:03 -070075 void onCallVideoProviderChanged(Call call);
Santos Cordon64c7e962014-07-02 15:15:27 -070076 void onCallerInfoChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070077 void onAudioModeIsVoipChanged(Call call);
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070078 void onStatusHintsChanged(Call call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -070079 void onHandleChanged(Call call);
80 void onCallerDisplayNameChanged(Call call);
Andrew Lee4a796602014-07-11 17:23:03 -070081 void onVideoStateChanged(Call call);
Sailesh Nepal9d58de52014-07-18 14:53:19 -070082 void onStartActivityFromInCall(Call call, PendingIntent intent);
Santos Cordon64c7e962014-07-02 15:15:27 -070083 }
84
85 abstract static class ListenerBase implements Listener {
86 @Override
87 public void onSuccessfulOutgoingCall(Call call) {}
88 @Override
89 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {}
90 @Override
91 public void onCancelledOutgoingCall(Call call) {}
92 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -070093 public void onSuccessfulIncomingCall(Call call) {}
Santos Cordon64c7e962014-07-02 15:15:27 -070094 @Override
95 public void onFailedIncomingCall(Call call) {}
96 @Override
97 public void onRequestingRingback(Call call, boolean requestingRingback) {}
98 @Override
99 public void onPostDialWait(Call call, String remaining) {}
100 @Override
Sailesh Nepale20bc972014-07-09 21:22:36 -0700101 public void onCallCapabilitiesChanged(Call call) {}
Santos Cordon64c7e962014-07-02 15:15:27 -0700102 @Override
103 public void onExpiredConferenceCall(Call call) {}
104 @Override
105 public void onConfirmedConferenceCall(Call call) {}
106 @Override
107 public void onParentChanged(Call call) {}
108 @Override
109 public void onChildrenChanged(Call call) {}
110 @Override
111 public void onCannedSmsResponsesLoaded(Call call) {}
112 @Override
113 public void onCallVideoProviderChanged(Call call) {}
114 @Override
Santos Cordon64c7e962014-07-02 15:15:27 -0700115 public void onCallerInfoChanged(Call call) {}
Sailesh Nepal7e669572014-07-08 21:29:12 -0700116 @Override
117 public void onAudioModeIsVoipChanged(Call call) {}
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700118 @Override
119 public void onStatusHintsChanged(Call call) {}
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700120 @Override
121 public void onHandleChanged(Call call) {}
122 @Override
123 public void onCallerDisplayNameChanged(Call call) {}
Andrew Lee4a796602014-07-11 17:23:03 -0700124 @Override
125 public void onVideoStateChanged(Call call) {}
Sailesh Nepal9d58de52014-07-18 14:53:19 -0700126 @Override
127 public void onStartActivityFromInCall(Call call, PendingIntent intent) {}
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 Gunn0a388fc2014-07-17 12:21:17 -0700205 /**
206 * Tracks the video states which were applicable over the duration of a call.
207 * See {@link android.telecomm.VideoCallProfile} for a list of valid video states.
208 */
209 private int mVideoStateHistory;
210
Tyler Gunnc4abd912014-07-08 14:22:10 -0700211 private int mVideoState;
212
Ben Gilad61925612014-03-11 19:06:36 -0700213 /**
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700214 * Disconnect cause for the call. Only valid if the state of the call is DISCONNECTED.
215 * See {@link android.telephony.DisconnectCause}.
216 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700217 private int mDisconnectCause = DisconnectCause.NOT_VALID;
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700218
219 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700220 * Additional disconnect information provided by the connection service.
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700221 */
222 private String mDisconnectMessage;
223
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700224 /** Info used by the connection services. */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700225 private Bundle mExtras = Bundle.EMPTY;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700226
Santos Cordon766d04f2014-05-06 10:28:25 -0700227 /** Set of listeners on this call. */
Santos Cordondfc66012014-07-15 13:41:40 -0700228 private Set<Listener> mListeners = new CopyOnWriteArraySet<>();
Santos Cordon766d04f2014-05-06 10:28:25 -0700229
Sailesh Nepal664837f2014-07-14 16:31:51 -0700230 private CreateConnectionProcessor mCreateConnectionProcessor;
Santos Cordon682fe6b2014-05-20 08:56:39 -0700231
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700232 /** Caller information retrieved from the latest contact query. */
233 private CallerInfo mCallerInfo;
234
235 /** The latest token used with a contact info query. */
236 private int mQueryToken = 0;
237
Ihab Awadcb387ac2014-05-28 16:49:38 -0700238 /** Whether this call is requesting that Telecomm play the ringback tone on its behalf. */
239 private boolean mRequestingRingback = false;
240
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700241 /** Whether direct-to-voicemail query is pending. */
242 private boolean mDirectToVoicemailQueryPending;
Santos Cordon2174fb52014-05-29 08:22:56 -0700243
Sailesh Nepale20bc972014-07-09 21:22:36 -0700244 private int mCallCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700245
246 private boolean mIsConference = false;
247
248 private Call mParentCall = null;
249
250 private List<Call> mChildCalls = new LinkedList<>();
251
Ihab Awadff7493a2014-06-10 13:47:44 -0700252 /** Set of text message responses allowed for this call, if applicable. */
253 private List<String> mCannedSmsResponses = Collections.EMPTY_LIST;
254
255 /** Whether an attempt has been made to load the text message responses. */
256 private boolean mCannedSmsResponsesLoadingStarted = false;
257
Nancy Chena65d41f2014-06-24 12:06:03 -0700258 private ICallVideoProvider mCallVideoProvider;
259
Sailesh Nepal7e669572014-07-08 21:29:12 -0700260 private boolean mAudioModeIsVoip;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700261 private StatusHints mStatusHints;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700262 private final ConnectionServiceRepository mRepository;
Sailesh Nepal7e669572014-07-08 21:29:12 -0700263
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700264 /**
Ben Gilad0407fb22014-01-09 16:18:41 -0800265 * Persists the specified parameters and initializes the new instance.
266 *
267 * @param handle The handle to dial.
Yorke Lee33501632014-03-17 19:24:12 -0700268 * @param gatewayInfo Gateway information to use for the call.
Ihab Awad98a55602014-06-30 21:27:28 -0700269 * @param account Account information to use for the call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700270 * @param isIncoming True if this is an incoming call.
Ben Gilad0407fb22014-01-09 16:18:41 -0800271 */
Sailesh Nepal664837f2014-07-14 16:31:51 -0700272 Call(
273 ConnectionServiceRepository repository,
274 Uri handle,
275 GatewayInfo gatewayInfo,
276 PhoneAccount account,
277 boolean isIncoming,
278 boolean isConference) {
Santos Cordona1610702014-06-04 20:22:56 -0700279 mState = isConference ? CallState.ACTIVE : CallState.NEW;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700280 mRepository = repository;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700281 setHandle(handle, CallPropertyPresentation.ALLOWED);
Yorke Lee33501632014-03-17 19:24:12 -0700282 mGatewayInfo = gatewayInfo;
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700283 mPhoneAccount = account;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700284 mIsIncoming = isIncoming;
Santos Cordona1610702014-06-04 20:22:56 -0700285 mIsConference = isConference;
Ihab Awadff7493a2014-06-10 13:47:44 -0700286 maybeLoadCannedSmsResponses();
Ben Gilad0407fb22014-01-09 16:18:41 -0800287 }
288
Santos Cordon766d04f2014-05-06 10:28:25 -0700289 void addListener(Listener listener) {
290 mListeners.add(listener);
291 }
292
293 void removeListener(Listener listener) {
294 mListeners.remove(listener);
295 }
296
Santos Cordon61d0f702014-02-19 02:52:23 -0800297 /** {@inheritDoc} */
298 @Override public String toString() {
Sailesh Nepal4538f012014-04-15 11:40:33 -0700299 String component = null;
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700300 if (mConnectionService != null && mConnectionService.getComponentName() != null) {
301 component = mConnectionService.getComponentName().flattenToShortString();
Sailesh Nepal4538f012014-04-15 11:40:33 -0700302 }
Tyler Gunn0a388fc2014-07-17 12:21:17 -0700303
304 return String.format(Locale.US, "[%s, %s, %s, %d]", mState, component,
305 Log.piiHandle(mHandle), getVideoState());
Santos Cordon61d0f702014-02-19 02:52:23 -0800306 }
307
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800308 CallState getState() {
Santos Cordona1610702014-06-04 20:22:56 -0700309 if (mIsConference) {
310 if (!mChildCalls.isEmpty()) {
311 // If we have child calls, just return the child call.
312 return mChildCalls.get(0).getState();
313 }
314 return CallState.ACTIVE;
315 } else {
316 return mState;
317 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800318 }
319
320 /**
321 * Sets the call state. Although there exists the notion of appropriate state transitions
322 * (see {@link CallState}), in practice those expectations break down when cellular systems
323 * misbehave and they do this very often. The result is that we do not enforce state transitions
324 * and instead keep the code resilient to unexpected state changes.
325 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700326 void setState(CallState newState) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700327 Preconditions.checkState(newState != CallState.DISCONNECTED ||
328 mDisconnectCause != DisconnectCause.NOT_VALID);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700329 if (mState != newState) {
330 Log.v(this, "setState %s -> %s", mState, newState);
331 mState = newState;
Ihab Awadff7493a2014-06-10 13:47:44 -0700332 maybeLoadCannedSmsResponses();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700333 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800334 }
335
Ihab Awadcb387ac2014-05-28 16:49:38 -0700336 void setRequestingRingback(boolean requestingRingback) {
337 mRequestingRingback = requestingRingback;
338 for (Listener l : mListeners) {
339 l.onRequestingRingback(this, mRequestingRingback);
340 }
341 }
342
343 boolean isRequestingRingback() {
344 return mRequestingRingback;
345 }
346
Sailesh Nepalce704b92014-03-17 18:31:43 -0700347 Uri getHandle() {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800348 return mHandle;
349 }
350
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700351 int getHandlePresentation() {
352 return mHandlePresentation;
353 }
354
355 void setHandle(Uri handle, int presentation) {
356 if (!Objects.equals(handle, mHandle) || presentation != mHandlePresentation) {
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700357 mHandle = handle;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700358 mHandlePresentation = presentation;
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700359 mIsEmergencyCall = mHandle != null && PhoneNumberUtils.isLocalEmergencyNumber(
Yorke Lee66255452014-06-05 08:09:24 -0700360 TelecommApp.getInstance(), mHandle.getSchemeSpecificPart());
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700361 startCallerInfoLookup();
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700362 for (Listener l : mListeners) {
363 l.onHandleChanged(this);
364 }
365 }
366 }
367
368 String getCallerDisplayName() {
369 return mCallerDisplayName;
370 }
371
372 int getCallerDisplayNamePresentation() {
373 return mCallerDisplayNamePresentation;
374 }
375
376 void setCallerDisplayName(String callerDisplayName, int presentation) {
377 if (!TextUtils.equals(callerDisplayName, mCallerDisplayName) ||
378 presentation != mCallerDisplayNamePresentation) {
379 mCallerDisplayName = callerDisplayName;
380 mCallerDisplayNamePresentation = presentation;
381 for (Listener l : mListeners) {
382 l.onCallerDisplayNameChanged(this);
383 }
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700384 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700385 }
386
Santos Cordon99c8a6f2014-05-28 18:28:47 -0700387 String getName() {
388 return mCallerInfo == null ? null : mCallerInfo.name;
389 }
390
391 Bitmap getPhotoIcon() {
392 return mCallerInfo == null ? null : mCallerInfo.cachedPhotoIcon;
393 }
394
395 Drawable getPhoto() {
396 return mCallerInfo == null ? null : mCallerInfo.cachedPhoto;
397 }
398
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700399 /**
400 * @param disconnectCause The reason for the disconnection, any of
401 * {@link android.telephony.DisconnectCause}.
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700402 * @param disconnectMessage Optional message about the disconnect.
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700403 */
404 void setDisconnectCause(int disconnectCause, String disconnectMessage) {
405 // TODO: Consider combining this method with a setDisconnected() method that is totally
406 // separate from setState.
407 mDisconnectCause = disconnectCause;
408 mDisconnectMessage = disconnectMessage;
409 }
410
411 int getDisconnectCause() {
412 return mDisconnectCause;
413 }
414
415 String getDisconnectMessage() {
416 return mDisconnectMessage;
417 }
418
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700419 boolean isEmergencyCall() {
420 return mIsEmergencyCall;
Santos Cordon61d0f702014-02-19 02:52:23 -0800421 }
422
Yorke Lee33501632014-03-17 19:24:12 -0700423 /**
424 * @return The original handle this call is associated with. In-call services should use this
425 * handle when indicating in their UI the handle that is being called.
426 */
427 public Uri getOriginalHandle() {
428 if (mGatewayInfo != null && !mGatewayInfo.isEmpty()) {
429 return mGatewayInfo.getOriginalHandle();
430 }
431 return getHandle();
432 }
433
434 GatewayInfo getGatewayInfo() {
435 return mGatewayInfo;
436 }
437
Sailesh Nepalb0ba0872014-07-08 22:09:50 -0700438 PhoneAccount getPhoneAccount() {
439 return mPhoneAccount;
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700440 }
441
Nancy Chen53ceedc2014-07-08 18:56:51 -0700442 void setPhoneAccount(PhoneAccount account) {
443 mPhoneAccount = account;
444 }
445
Sailesh Nepal810735e2014-03-18 18:15:46 -0700446 boolean isIncoming() {
447 return mIsIncoming;
448 }
449
Ben Gilad0407fb22014-01-09 16:18:41 -0800450 /**
451 * @return The "age" of this call object in milliseconds, which typically also represents the
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700452 * period since this call was added to the set pending outgoing calls, see
453 * mCreationTimeMillis.
Ben Gilad0407fb22014-01-09 16:18:41 -0800454 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700455 long getAgeMillis() {
456 return System.currentTimeMillis() - mCreationTimeMillis;
Ben Gilad0407fb22014-01-09 16:18:41 -0800457 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800458
Yorke Leef98fb572014-03-05 10:56:55 -0800459 /**
460 * @return The time when this call object was created and added to the set of pending outgoing
461 * calls.
462 */
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700463 long getCreationTimeMillis() {
464 return mCreationTimeMillis;
465 }
466
467 long getConnectTimeMillis() {
468 return mConnectTimeMillis;
469 }
470
471 void setConnectTimeMillis(long connectTimeMillis) {
472 mConnectTimeMillis = connectTimeMillis;
Yorke Leef98fb572014-03-05 10:56:55 -0800473 }
474
Sailesh Nepale20bc972014-07-09 21:22:36 -0700475 int getCallCapabilities() {
476 return mCallCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700477 }
478
Sailesh Nepale20bc972014-07-09 21:22:36 -0700479 void setCallCapabilities(int callCapabilities) {
480 if (mCallCapabilities != callCapabilities) {
481 mCallCapabilities = callCapabilities;
Santos Cordona1610702014-06-04 20:22:56 -0700482 for (Listener l : mListeners) {
Sailesh Nepale20bc972014-07-09 21:22:36 -0700483 l.onCallCapabilitiesChanged(this);
Santos Cordona1610702014-06-04 20:22:56 -0700484 }
485 }
486 }
487
488 Call getParentCall() {
489 return mParentCall;
490 }
491
492 List<Call> getChildCalls() {
493 return mChildCalls;
494 }
495
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700496 ConnectionServiceWrapper getConnectionService() {
497 return mConnectionService;
Santos Cordon681663d2014-01-30 04:32:15 -0800498 }
499
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700500 void setConnectionService(ConnectionServiceWrapper service) {
501 Preconditions.checkNotNull(service);
502
503 clearConnectionService();
504
505 service.incrementAssociatedCallCount();
506 mConnectionService = service;
507 mConnectionService.addCall(this);
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700508 }
509
510 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700511 * Clears the associated connection service.
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700512 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700513 void clearConnectionService() {
514 if (mConnectionService != null) {
515 ConnectionServiceWrapper serviceTemp = mConnectionService;
516 mConnectionService = null;
517 serviceTemp.removeCall(this);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700518
519 // Decrementing the count can cause the service to unbind, which itself can trigger the
520 // service-death code. Since the service death code tries to clean up any associated
521 // calls, we need to make sure to remove that information (e.g., removeCall()) before
522 // we decrement. Technically, invoking removeCall() prior to decrementing is all that is
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700523 // necessary, but cleaning up mConnectionService prior to triggering an unbind is good
524 // to do.
525 decrementAssociatedCallCount(serviceTemp);
Yorke Leeadee12d2014-03-13 12:08:30 -0700526 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800527 }
528
Sailesh Nepal664837f2014-07-14 16:31:51 -0700529 private void processDirectToVoicemail() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700530 if (mDirectToVoicemailQueryPending) {
Santos Cordon2174fb52014-05-29 08:22:56 -0700531 if (mCallerInfo != null && mCallerInfo.shouldSendToVoicemail) {
532 Log.i(this, "Directing call to voicemail: %s.", this);
533 // TODO(santoscordon): Once we move State handling from CallsManager to Call, we
534 // will not need to set RINGING state prior to calling reject.
535 setState(CallState.RINGING);
Ihab Awadff7493a2014-06-10 13:47:44 -0700536 reject(false, null);
Santos Cordon2174fb52014-05-29 08:22:56 -0700537 } else {
538 // TODO(santoscordon): Make this class (not CallsManager) responsible for changing
539 // the call state to RINGING.
540
541 // TODO(santoscordon): Replace this with state transition to RINGING.
542 for (Listener l : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700543 l.onSuccessfulIncomingCall(this);
Santos Cordon2174fb52014-05-29 08:22:56 -0700544 }
545 }
546
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700547 mDirectToVoicemailQueryPending = false;
Santos Cordon766d04f2014-05-06 10:28:25 -0700548 }
549 }
550
Santos Cordon766d04f2014-05-06 10:28:25 -0700551 /**
Sailesh Nepal664837f2014-07-14 16:31:51 -0700552 * Starts the create connection sequence. Upon completion, there should exist an active
553 * connection through a connection service (or the call will have failed).
Santos Cordon766d04f2014-05-06 10:28:25 -0700554 */
Sailesh Nepal664837f2014-07-14 16:31:51 -0700555 void startCreateConnection() {
556 Preconditions.checkState(mCreateConnectionProcessor == null);
557 mCreateConnectionProcessor = new CreateConnectionProcessor(this, mRepository, this);
558 mCreateConnectionProcessor.process();
Santos Cordon766d04f2014-05-06 10:28:25 -0700559 }
560
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700561 @Override
Sailesh Nepal664837f2014-07-14 16:31:51 -0700562 public void handleCreateConnectionSuccessful(ConnectionRequest request) {
563 mCreateConnectionProcessor = null;
564 mPhoneAccount = request.getAccount();
565
566 if (mIsIncoming) {
567 // We do not handle incoming calls immediately when they are verified by the connection
568 // service. We allow the caller-info-query code to execute first so that we can read the
569 // direct-to-voicemail property before deciding if we want to show the incoming call to
570 // the user or if we want to reject the call.
571 mDirectToVoicemailQueryPending = true;
572
573 // Setting the handle triggers the caller info lookup code.
574 setHandle(request.getHandle(), request.getHandlePresentation());
575
576 // Timeout the direct-to-voicemail lookup execution so that we dont wait too long before
577 // showing the user the incoming call screen.
578 mHandler.postDelayed(mDirectToVoicemailRunnable, Timeouts.getDirectToVoicemailMillis());
579 } else {
580 for (Listener l : mListeners) {
581 l.onSuccessfulOutgoingCall(this);
582 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700583 }
584 }
585
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700586 @Override
Sailesh Nepal664837f2014-07-14 16:31:51 -0700587 public void handleCreateConnectionFailed(int code, String msg) {
588 mCreateConnectionProcessor = null;
589 if (mIsIncoming) {
590 clearConnectionService();
591 setDisconnectCause(code, null);
592 setState(CallState.DISCONNECTED);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700593
Sailesh Nepal664837f2014-07-14 16:31:51 -0700594 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
595 for (int i = 0; i < listeners.length; i++) {
596 listeners[i].onFailedIncomingCall(this);
597 }
598 } else {
599 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
600 for (int i = 0; i < listeners.length; i++) {
601 listeners[i].onFailedOutgoingCall(this, code, msg);
602 }
603 clearConnectionService();
604 }
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700605 }
606
607 @Override
Sailesh Nepal664837f2014-07-14 16:31:51 -0700608 public void handleCreateConnectionCancelled() {
609 mCreateConnectionProcessor = null;
610 if (mIsIncoming) {
611 clearConnectionService();
612 setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
613 setState(CallState.DISCONNECTED);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700614
Sailesh Nepal664837f2014-07-14 16:31:51 -0700615 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
616 for (int i = 0; i < listeners.length; i++) {
617 listeners[i].onFailedIncomingCall(this);
618 }
619 } else {
620 Listener[] listeners = mListeners.toArray(new Listener[mListeners.size()]);
621 for (int i = 0; i < listeners.length; i++) {
622 listeners[i].onCancelledOutgoingCall(this);
623 }
624 clearConnectionService();
625 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700626 }
627
628 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700629 * Plays the specified DTMF tone.
630 */
631 void playDtmfTone(char digit) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700632 if (mConnectionService == null) {
633 Log.w(this, "playDtmfTone() request on a call without a connection service.");
Ihab Awad74549ec2014-03-10 15:33:25 -0700634 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700635 Log.i(this, "Send playDtmfTone to connection service for call %s", this);
636 mConnectionService.playDtmfTone(this, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700637 }
638 }
639
640 /**
641 * Stops playing any currently playing DTMF tone.
642 */
643 void stopDtmfTone() {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700644 if (mConnectionService == null) {
645 Log.w(this, "stopDtmfTone() request on a call without a connectino service.");
Ihab Awad74549ec2014-03-10 15:33:25 -0700646 } else {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700647 Log.i(this, "Send stopDtmfTone to connection service for call %s", this);
648 mConnectionService.stopDtmfTone(this);
Ihab Awad74549ec2014-03-10 15:33:25 -0700649 }
650 }
651
652 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700653 * Attempts to disconnect the call through the connection service.
Santos Cordon049b7b62014-01-30 05:34:26 -0800654 */
655 void disconnect() {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700656 if (mState == CallState.NEW || mState == CallState.PRE_DIAL_WAIT) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700657 Log.v(this, "Aborting call %s", this);
658 abort();
Santos Cordon74d420b2014-05-07 14:38:47 -0700659 } else if (mState != CallState.ABORTED && mState != CallState.DISCONNECTED) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700660 Preconditions.checkNotNull(mConnectionService);
Santos Cordon766d04f2014-05-06 10:28:25 -0700661
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700662 Log.i(this, "Send disconnect to connection service for call: %s", this);
663 // The call isn't officially disconnected until the connection service confirms that the
664 // call was actually disconnected. Only then is the association between call and
665 // connection service severed, see {@link CallsManager#markCallAsDisconnected}.
666 mConnectionService.disconnect(this);
Santos Cordon049b7b62014-01-30 05:34:26 -0800667 }
668 }
669
Santos Cordon682fe6b2014-05-20 08:56:39 -0700670 void abort() {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700671 if (mCreateConnectionProcessor != null) {
672 mCreateConnectionProcessor.abort();
Nancy Chen53ceedc2014-07-08 18:56:51 -0700673 } else if (mState == CallState.PRE_DIAL_WAIT) {
674 handleCreateConnectionFailed(DisconnectCause.LOCAL, null);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700675 }
676 }
677
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800678 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800679 * Answers the call if it is ringing.
Andrew Lee38931d02014-07-16 10:17:36 -0700680 *
681 * @param videoState The video state in which to answer the call.
Santos Cordon61d0f702014-02-19 02:52:23 -0800682 */
Andrew Lee38931d02014-07-16 10:17:36 -0700683 void answer(int videoState) {
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.
Andrew Lee38931d02014-07-16 10:17:36 -0700693 mConnectionService.answer(this, videoState);
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 /**
Tyler Gunn0a388fc2014-07-17 12:21:17 -07001079 * Returns the video states which were applicable over the duration of a call.
1080 * See {@link android.telecomm.VideoCallProfile} for a list of valid video states.
1081 *
1082 * @return The video states applicable over the duration of the call.
1083 */
1084 public int getVideoStateHistory() {
1085 return mVideoStateHistory;
1086 }
1087
1088 /**
1089 * Determines the current video state for the call.
1090 * For an outgoing call determines the desired video state for the call.
Tyler Gunnc4abd912014-07-08 14:22:10 -07001091 * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
1092 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
1093 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
1094 * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
1095 *
Tyler Gunn0a388fc2014-07-17 12:21:17 -07001096 * @param videoState The video state for the call.
Tyler Gunnc4abd912014-07-08 14:22:10 -07001097 */
1098 public void setVideoState(int videoState) {
Tyler Gunn0a388fc2014-07-17 12:21:17 -07001099 // Track which video states were applicable over the duration of the call.
1100 mVideoStateHistory = mVideoStateHistory | videoState;
1101
Tyler Gunnc4abd912014-07-08 14:22:10 -07001102 mVideoState = videoState;
Andrew Lee4a796602014-07-11 17:23:03 -07001103 for (Listener l : mListeners) {
1104 l.onVideoStateChanged(this);
1105 }
Tyler Gunnc4abd912014-07-08 14:22:10 -07001106 }
Sailesh Nepal7e669572014-07-08 21:29:12 -07001107
1108 public boolean getAudioModeIsVoip() {
1109 return mAudioModeIsVoip;
1110 }
1111
1112 public void setAudioModeIsVoip(boolean audioModeIsVoip) {
1113 mAudioModeIsVoip = audioModeIsVoip;
1114 for (Listener l : mListeners) {
Sailesh Nepal35faf8c2014-07-08 22:02:34 -07001115 l.onAudioModeIsVoipChanged(this);
1116 }
1117 }
1118
1119 public StatusHints getStatusHints() {
1120 return mStatusHints;
1121 }
1122
1123 public void setStatusHints(StatusHints statusHints) {
1124 mStatusHints = statusHints;
1125 for (Listener l : mListeners) {
1126 l.onStatusHintsChanged(this);
Sailesh Nepal7e669572014-07-08 21:29:12 -07001127 }
1128 }
Sailesh Nepal9d58de52014-07-18 14:53:19 -07001129
1130 public void startActivityFromInCall(PendingIntent intent) {
1131 if (intent.isActivity()) {
1132 for (Listener l : mListeners) {
1133 l.onStartActivityFromInCall(this, intent);
1134 }
1135 } else {
1136 Log.w(this, "startActivityFromInCall, activity intent required");
1137 }
1138 }
Ben Gilad9f2bed32013-12-12 17:43:26 -08001139}