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