blob: 82a29ee84fc1ab12944157e651cc5e0d48417a3a [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2013 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
17package com.android.incallui.call;
18
19import android.content.Context;
20import android.hardware.camera2.CameraCharacteristics;
21import android.net.Uri;
22import android.os.Build.VERSION;
23import android.os.Build.VERSION_CODES;
24import android.os.Bundle;
25import android.os.Trace;
26import android.support.annotation.IntDef;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070027import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import android.support.annotation.Nullable;
twyendde01c52017-09-22 10:07:31 -070029import android.support.annotation.VisibleForTesting;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import android.telecom.Call;
31import android.telecom.Call.Details;
Eric Erfanian8369df02017-05-03 10:27:13 -070032import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080033import android.telecom.Connection;
34import android.telecom.DisconnectCause;
35import android.telecom.GatewayInfo;
36import android.telecom.InCallService.VideoCall;
37import android.telecom.PhoneAccount;
38import android.telecom.PhoneAccountHandle;
39import android.telecom.StatusHints;
40import android.telecom.TelecomManager;
41import android.telecom.VideoProfile;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import android.text.TextUtils;
43import com.android.contacts.common.compat.CallCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080044import com.android.contacts.common.compat.telecom.TelecomManagerCompat;
Eric Erfanian8369df02017-05-03 10:27:13 -070045import com.android.dialer.callintent.CallInitiationType;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.dialer.callintent.CallIntentParser;
Eric Erfanian8369df02017-05-03 10:27:13 -070047import com.android.dialer.callintent.CallSpecificAppData;
Eric Erfanianccca3152017-02-22 16:32:36 -080048import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070050import com.android.dialer.compat.telephony.TelephonyManagerCompat;
51import com.android.dialer.configprovider.ConfigProviderBindings;
Eric Erfanian8369df02017-05-03 10:27:13 -070052import com.android.dialer.enrichedcall.EnrichedCallCapabilities;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian2ca43182017-08-31 06:57:16 -070054import com.android.dialer.enrichedcall.EnrichedCallManager;
55import com.android.dialer.enrichedcall.EnrichedCallManager.CapabilitiesListener;
56import com.android.dialer.enrichedcall.EnrichedCallManager.Filter;
57import com.android.dialer.enrichedcall.EnrichedCallManager.StateChangedListener;
Eric Erfanian8369df02017-05-03 10:27:13 -070058import com.android.dialer.enrichedcall.Session;
Eric Erfaniand8046e52017-04-06 09:41:50 -070059import com.android.dialer.lightbringer.LightbringerComponent;
Eric Erfanian8369df02017-05-03 10:27:13 -070060import com.android.dialer.logging.ContactLookupResult;
twyendde01c52017-09-22 10:07:31 -070061import com.android.dialer.logging.ContactLookupResult.Type;
Eric Erfanian8369df02017-05-03 10:27:13 -070062import com.android.dialer.logging.DialerImpression;
63import com.android.dialer.logging.Logger;
Eric Erfanianc857f902017-05-15 14:05:33 -070064import com.android.dialer.theme.R;
Eric Erfanian8369df02017-05-03 10:27:13 -070065import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.incallui.latencyreport.LatencyReport;
67import com.android.incallui.util.TelecomCallUtil;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070068import com.android.incallui.videotech.VideoTech;
69import com.android.incallui.videotech.VideoTech.VideoTechListener;
70import com.android.incallui.videotech.empty.EmptyVideoTech;
71import com.android.incallui.videotech.ims.ImsVideoTech;
Eric Erfaniand8046e52017-04-06 09:41:50 -070072import com.android.incallui.videotech.lightbringer.LightbringerTech;
Eric Erfanian90508232017-03-24 09:31:16 -070073import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080074import java.lang.annotation.Retention;
75import java.lang.annotation.RetentionPolicy;
76import java.util.ArrayList;
77import java.util.List;
78import java.util.Locale;
79import java.util.Objects;
80import java.util.UUID;
81import java.util.concurrent.CopyOnWriteArrayList;
82import java.util.concurrent.TimeUnit;
83
84/** Describes a single call and its state. */
Eric Erfanian2ca43182017-08-31 06:57:16 -070085public class DialerCall implements VideoTechListener, StateChangedListener, CapabilitiesListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080086
87 public static final int CALL_HISTORY_STATUS_UNKNOWN = 0;
88 public static final int CALL_HISTORY_STATUS_PRESENT = 1;
89 public static final int CALL_HISTORY_STATUS_NOT_PRESENT = 2;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070090
91 // Hard coded property for {@code Call}. Upstreamed change from Motorola.
92 // TODO(b/35359461): Move it to Telecom in framework.
93 public static final int PROPERTY_CODEC_KNOWN = 0x04000000;
94
Eric Erfanianccca3152017-02-22 16:32:36 -080095 private static final String ID_PREFIX = "DialerCall_";
96 private static final String CONFIG_EMERGENCY_CALLBACK_WINDOW_MILLIS =
97 "emergency_callback_window_millis";
98 private static int sIdCounter = 0;
99
100 /**
Eric Erfanianc857f902017-05-15 14:05:33 -0700101 * A counter used to append to restricted/private/hidden calls so that users can identify them in
102 * a conversation. This value is reset in {@link CallList#onCallRemoved(Context, Call)} when there
103 * are no live calls.
104 */
105 private static int sHiddenCounter;
106
107 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800108 * The unique call ID for every call. This will help us to identify each call and allow us the
109 * ability to stitch impressions to calls if needed.
110 */
111 private final String uniqueCallId = UUID.randomUUID().toString();
112
113 private final Call mTelecomCall;
114 private final LatencyReport mLatencyReport;
115 private final String mId;
Eric Erfanianc857f902017-05-15 14:05:33 -0700116 private final int mHiddenId;
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 private final List<String> mChildCallIds = new ArrayList<>();
Eric Erfanianccca3152017-02-22 16:32:36 -0800118 private final LogState mLogState = new LogState();
119 private final Context mContext;
120 private final DialerCallDelegate mDialerCallDelegate;
121 private final List<DialerCallListener> mListeners = new CopyOnWriteArrayList<>();
122 private final List<CannedTextResponsesLoadedListener> mCannedTextResponsesLoadedListeners =
123 new CopyOnWriteArrayList<>();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700124 private final VideoTechManager mVideoTechManager;
Eric Erfanianccca3152017-02-22 16:32:36 -0800125
126 private boolean mIsEmergencyCall;
127 private Uri mHandle;
128 private int mState = State.INVALID;
129 private DisconnectCause mDisconnectCause;
130
131 private boolean hasShownWiFiToLteHandoverToast;
132 private boolean doNotShowDialogForHandoffToWifiFailure;
133
Eric Erfanianccca3152017-02-22 16:32:36 -0800134 private String mChildNumber;
135 private String mLastForwardedNumber;
136 private String mCallSubject;
137 private PhoneAccountHandle mPhoneAccountHandle;
138 @CallHistoryStatus private int mCallHistoryStatus = CALL_HISTORY_STATUS_UNKNOWN;
139 private boolean mIsSpam;
140 private boolean mIsBlocked;
141 private boolean isInUserSpamList;
142 private boolean isInUserWhiteList;
143 private boolean isInGlobalSpamList;
144 private boolean didShowCameraPermission;
145 private String callProviderLabel;
146 private String callbackNumber;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700147 private int mCameraDirection = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
Eric Erfanian8369df02017-05-03 10:27:13 -0700148 private EnrichedCallCapabilities mEnrichedCallCapabilities;
149 private Session mEnrichedCallSession;
Eric Erfanianccca3152017-02-22 16:32:36 -0800150
Eric Erfanian2ca43182017-08-31 06:57:16 -0700151 private int answerAndReleaseButtonDisplayedTimes = 0;
152 private boolean releasedByAnsweringSecondCall = false;
153 // Times when a second call is received but AnswerAndRelease button is not shown
154 // since it's not supported.
155 private int secondCallWithoutAnswerAndReleasedButtonTimes = 0;
wangqi4d705e52017-09-28 12:23:35 -0700156 private VideoTech videoTech = new EmptyVideoTech();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700157
wangqi4d705e52017-09-28 12:23:35 -0700158 private com.android.dialer.logging.VideoTech.Type selectedAvailableVideoTechType =
159 com.android.dialer.logging.VideoTech.Type.NONE;
yueg457b3972017-09-18 15:11:47 -0700160
Eric Erfanianccca3152017-02-22 16:32:36 -0800161 public static String getNumberFromHandle(Uri handle) {
162 return handle == null ? "" : handle.getSchemeSpecificPart();
163 }
164
165 /**
166 * Whether the call is put on hold by remote party. This is different than the {@link
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700167 * State#ONHOLD} state which indicates that the call is being held locally on the device.
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 */
169 private boolean isRemotelyHeld;
170
Eric Erfanian2ca43182017-08-31 06:57:16 -0700171 /** Indicates whether this call is currently in the process of being merged into a conference. */
172 private boolean isMergeInProcess;
173
Eric Erfanianccca3152017-02-22 16:32:36 -0800174 /**
175 * Indicates whether the phone account associated with this call supports specifying a call
176 * subject.
177 */
178 private boolean mIsCallSubjectSupported;
179
180 private final Call.Callback mTelecomCallCallback =
181 new Call.Callback() {
182 @Override
183 public void onStateChanged(Call call, int newState) {
184 LogUtil.v("TelecomCallCallback.onStateChanged", "call=" + call + " newState=" + newState);
185 update();
186 }
187
188 @Override
189 public void onParentChanged(Call call, Call newParent) {
190 LogUtil.v(
191 "TelecomCallCallback.onParentChanged", "call=" + call + " newParent=" + newParent);
192 update();
193 }
194
195 @Override
196 public void onChildrenChanged(Call call, List<Call> children) {
197 update();
198 }
199
200 @Override
201 public void onDetailsChanged(Call call, Call.Details details) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700202 LogUtil.v(
203 "TelecomCallCallback.onDetailsChanged", " call=" + call + " details=" + details);
Eric Erfanianccca3152017-02-22 16:32:36 -0800204 update();
205 }
206
207 @Override
208 public void onCannedTextResponsesLoaded(Call call, List<String> cannedTextResponses) {
209 LogUtil.v(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700210 "TelecomCallCallback.onCannedTextResponsesLoaded",
Eric Erfanianccca3152017-02-22 16:32:36 -0800211 "call=" + call + " cannedTextResponses=" + cannedTextResponses);
212 for (CannedTextResponsesLoadedListener listener : mCannedTextResponsesLoadedListeners) {
213 listener.onCannedTextResponsesLoaded(DialerCall.this);
214 }
215 }
216
217 @Override
218 public void onPostDialWait(Call call, String remainingPostDialSequence) {
219 LogUtil.v(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700220 "TelecomCallCallback.onPostDialWait",
Eric Erfanianccca3152017-02-22 16:32:36 -0800221 "call=" + call + " remainingPostDialSequence=" + remainingPostDialSequence);
222 update();
223 }
224
225 @Override
226 public void onVideoCallChanged(Call call, VideoCall videoCall) {
227 LogUtil.v(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700228 "TelecomCallCallback.onVideoCallChanged", "call=" + call + " videoCall=" + videoCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800229 update();
230 }
231
232 @Override
233 public void onCallDestroyed(Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700234 LogUtil.v("TelecomCallCallback.onCallDestroyed", "call=" + call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700235 unregisterCallback();
Eric Erfanianccca3152017-02-22 16:32:36 -0800236 }
237
238 @Override
239 public void onConferenceableCallsChanged(Call call, List<Call> conferenceableCalls) {
240 LogUtil.v(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700241 "TelecomCallCallback.onConferenceableCallsChanged",
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 "call %s, conferenceable calls: %d",
243 call,
244 conferenceableCalls.size());
245 update();
246 }
247
248 @Override
249 public void onConnectionEvent(android.telecom.Call call, String event, Bundle extras) {
250 LogUtil.v(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700251 "TelecomCallCallback.onConnectionEvent",
Eric Erfanianccca3152017-02-22 16:32:36 -0800252 "Call: " + call + ", Event: " + event + ", Extras: " + extras);
253 switch (event) {
254 // The Previous attempt to Merge two calls together has failed in Telecom. We must
255 // now update the UI to possibly re-enable the Merge button based on the number of
256 // currently conferenceable calls available or Connection Capabilities.
257 case android.telecom.Connection.EVENT_CALL_MERGE_FAILED:
258 update();
259 break;
260 case TelephonyManagerCompat.EVENT_HANDOVER_VIDEO_FROM_WIFI_TO_LTE:
261 notifyWiFiToLteHandover();
262 break;
263 case TelephonyManagerCompat.EVENT_HANDOVER_TO_WIFI_FAILED:
264 notifyHandoverToWifiFailed();
265 break;
266 case TelephonyManagerCompat.EVENT_CALL_REMOTELY_HELD:
267 isRemotelyHeld = true;
268 update();
269 break;
270 case TelephonyManagerCompat.EVENT_CALL_REMOTELY_UNHELD:
271 isRemotelyHeld = false;
272 update();
273 break;
Eric Erfanianc857f902017-05-15 14:05:33 -0700274 case TelephonyManagerCompat.EVENT_NOTIFY_INTERNATIONAL_CALL_ON_WFC:
275 notifyInternationalCallOnWifi();
276 break;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700277 case TelephonyManagerCompat.EVENT_MERGE_START:
278 LogUtil.i("DialerCall.onConnectionEvent", "merge start");
279 isMergeInProcess = true;
280 break;
281 case TelephonyManagerCompat.EVENT_MERGE_COMPLETE:
282 LogUtil.i("DialerCall.onConnectionEvent", "merge complete");
283 isMergeInProcess = false;
284 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 default:
286 break;
287 }
288 }
289 };
Eric Erfanianc857f902017-05-15 14:05:33 -0700290
Eric Erfanianccca3152017-02-22 16:32:36 -0800291 private long mTimeAddedMs;
292
293 public DialerCall(
294 Context context,
295 DialerCallDelegate dialerCallDelegate,
296 Call telecomCall,
297 LatencyReport latencyReport,
298 boolean registerCallback) {
299 Assert.isNotNull(context);
300 mContext = context;
301 mDialerCallDelegate = dialerCallDelegate;
302 mTelecomCall = telecomCall;
303 mLatencyReport = latencyReport;
304 mId = ID_PREFIX + Integer.toString(sIdCounter++);
305
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700306 // Must be after assigning mTelecomCall
307 mVideoTechManager = new VideoTechManager(this);
308
309 updateFromTelecomCall();
Eric Erfanianc857f902017-05-15 14:05:33 -0700310 if (isHiddenNumber() && TextUtils.isEmpty(getNumber())) {
311 mHiddenId = ++sHiddenCounter;
312 } else {
313 mHiddenId = 0;
314 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800315
316 if (registerCallback) {
317 mTelecomCall.registerCallback(mTelecomCallCallback);
318 }
319
320 mTimeAddedMs = System.currentTimeMillis();
321 parseCallSpecificAppData();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700322
323 updateEnrichedCallSession();
Eric Erfanianccca3152017-02-22 16:32:36 -0800324 }
325
twyendde01c52017-09-22 10:07:31 -0700326 /** Test only constructor to avoid initializing dependencies. */
327 @VisibleForTesting
328 DialerCall(Context context) {
329 mContext = context;
330 mTelecomCall = null;
331 mLatencyReport = null;
332 mId = null;
333 mHiddenId = 0;
334 mDialerCallDelegate = null;
335 mVideoTechManager = null;
336 }
337
Eric Erfanianccca3152017-02-22 16:32:36 -0800338 private static int translateState(int state) {
339 switch (state) {
340 case Call.STATE_NEW:
341 case Call.STATE_CONNECTING:
342 return DialerCall.State.CONNECTING;
343 case Call.STATE_SELECT_PHONE_ACCOUNT:
344 return DialerCall.State.SELECT_PHONE_ACCOUNT;
345 case Call.STATE_DIALING:
346 return DialerCall.State.DIALING;
347 case Call.STATE_PULLING_CALL:
348 return DialerCall.State.PULLING;
349 case Call.STATE_RINGING:
350 return DialerCall.State.INCOMING;
351 case Call.STATE_ACTIVE:
352 return DialerCall.State.ACTIVE;
353 case Call.STATE_HOLDING:
354 return DialerCall.State.ONHOLD;
355 case Call.STATE_DISCONNECTED:
356 return DialerCall.State.DISCONNECTED;
357 case Call.STATE_DISCONNECTING:
358 return DialerCall.State.DISCONNECTING;
359 default:
360 return DialerCall.State.INVALID;
361 }
362 }
363
364 public static boolean areSame(DialerCall call1, DialerCall call2) {
365 if (call1 == null && call2 == null) {
366 return true;
367 } else if (call1 == null || call2 == null) {
368 return false;
369 }
370
371 // otherwise compare call Ids
372 return call1.getId().equals(call2.getId());
373 }
374
375 public static boolean areSameNumber(DialerCall call1, DialerCall call2) {
376 if (call1 == null && call2 == null) {
377 return true;
378 } else if (call1 == null || call2 == null) {
379 return false;
380 }
381
382 // otherwise compare call Numbers
383 return TextUtils.equals(call1.getNumber(), call2.getNumber());
384 }
385
386 public void addListener(DialerCallListener listener) {
387 Assert.isMainThread();
388 mListeners.add(listener);
389 }
390
391 public void removeListener(DialerCallListener listener) {
392 Assert.isMainThread();
393 mListeners.remove(listener);
394 }
395
396 public void addCannedTextResponsesLoadedListener(CannedTextResponsesLoadedListener listener) {
397 Assert.isMainThread();
398 mCannedTextResponsesLoadedListeners.add(listener);
399 }
400
401 public void removeCannedTextResponsesLoadedListener(CannedTextResponsesLoadedListener listener) {
402 Assert.isMainThread();
403 mCannedTextResponsesLoadedListeners.remove(listener);
404 }
405
406 public void notifyWiFiToLteHandover() {
407 LogUtil.i("DialerCall.notifyWiFiToLteHandover", "");
408 for (DialerCallListener listener : mListeners) {
409 listener.onWiFiToLteHandover();
410 }
411 }
412
413 public void notifyHandoverToWifiFailed() {
414 LogUtil.i("DialerCall.notifyHandoverToWifiFailed", "");
415 for (DialerCallListener listener : mListeners) {
416 listener.onHandoverToWifiFailure();
417 }
418 }
419
Eric Erfanianc857f902017-05-15 14:05:33 -0700420 public void notifyInternationalCallOnWifi() {
421 LogUtil.enterBlock("DialerCall.notifyInternationalCallOnWifi");
422 for (DialerCallListener dialerCallListener : mListeners) {
423 dialerCallListener.onInternationalCallOnWifi();
424 }
425 }
426
Eric Erfanianccca3152017-02-22 16:32:36 -0800427 /* package-private */ Call getTelecomCall() {
428 return mTelecomCall;
429 }
430
431 public StatusHints getStatusHints() {
432 return mTelecomCall.getDetails().getStatusHints();
433 }
434
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700435 public int getCameraDir() {
436 return mCameraDirection;
437 }
438
439 public void setCameraDir(int cameraDir) {
440 if (cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING
441 || cameraDir == CameraDirection.CAMERA_DIRECTION_BACK_FACING) {
442 mCameraDirection = cameraDir;
443 } else {
444 mCameraDirection = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
445 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800446 }
447
Eric Erfanian2ca43182017-08-31 06:57:16 -0700448 public boolean wasParentCall() {
449 return mLogState.conferencedCalls != 0;
450 }
451
Eric Erfanianccca3152017-02-22 16:32:36 -0800452 private void update() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700453 Trace.beginSection("DialerCall.update");
Eric Erfanianccca3152017-02-22 16:32:36 -0800454 int oldState = getState();
455 // We want to potentially register a video call callback here.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700456 updateFromTelecomCall();
wangqi4d705e52017-09-28 12:23:35 -0700457 // Only store the first video tech type found to be available during the life of the call.
458 if (selectedAvailableVideoTechType == com.android.dialer.logging.VideoTech.Type.NONE) {
459 // Update the video tech.
460 videoTech = mVideoTechManager.findBestAvailableVideoTech();
461 videoTech.becomePrimary();
462 selectedAvailableVideoTechType = videoTech.getVideoTechType();
463 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800464 if (oldState != getState() && getState() == DialerCall.State.DISCONNECTED) {
465 for (DialerCallListener listener : mListeners) {
466 listener.onDialerCallDisconnect();
467 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700468 EnrichedCallComponent.get(mContext)
469 .getEnrichedCallManager()
470 .unregisterCapabilitiesListener(this);
471 EnrichedCallComponent.get(mContext)
472 .getEnrichedCallManager()
473 .unregisterStateChangedListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800474 } else {
475 for (DialerCallListener listener : mListeners) {
476 listener.onDialerCallUpdate();
477 }
478 }
479 Trace.endSection();
480 }
481
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700482 private void updateFromTelecomCall() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700483 Trace.beginSection("DialerCall.updateFromTelecomCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800484 LogUtil.v("DialerCall.updateFromTelecomCall", mTelecomCall.toString());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700485
486 mVideoTechManager.dispatchCallStateChanged(mTelecomCall.getState());
487
Eric Erfanianccca3152017-02-22 16:32:36 -0800488 final int translatedState = translateState(mTelecomCall.getState());
489 if (mState != State.BLOCKED) {
490 setState(translatedState);
491 setDisconnectCause(mTelecomCall.getDetails().getDisconnectCause());
Eric Erfanianccca3152017-02-22 16:32:36 -0800492 }
493
494 mChildCallIds.clear();
495 final int numChildCalls = mTelecomCall.getChildren().size();
496 for (int i = 0; i < numChildCalls; i++) {
497 mChildCallIds.add(
498 mDialerCallDelegate
499 .getDialerCallFromTelecomCall(mTelecomCall.getChildren().get(i))
500 .getId());
501 }
502
503 // The number of conferenced calls can change over the course of the call, so use the
504 // maximum number of conferenced child calls as the metric for conference call usage.
505 mLogState.conferencedCalls = Math.max(numChildCalls, mLogState.conferencedCalls);
506
507 updateFromCallExtras(mTelecomCall.getDetails().getExtras());
508
509 // If the handle of the call has changed, update state for the call determining if it is an
510 // emergency call.
511 Uri newHandle = mTelecomCall.getDetails().getHandle();
512 if (!Objects.equals(mHandle, newHandle)) {
513 mHandle = newHandle;
514 updateEmergencyCallState();
515 }
516
517 // If the phone account handle of the call is set, cache capability bit indicating whether
518 // the phone account supports call subjects.
519 PhoneAccountHandle newPhoneAccountHandle = mTelecomCall.getDetails().getAccountHandle();
520 if (!Objects.equals(mPhoneAccountHandle, newPhoneAccountHandle)) {
521 mPhoneAccountHandle = newPhoneAccountHandle;
522
523 if (mPhoneAccountHandle != null) {
524 PhoneAccount phoneAccount =
525 mContext.getSystemService(TelecomManager.class).getPhoneAccount(mPhoneAccountHandle);
526 if (phoneAccount != null) {
527 mIsCallSubjectSupported =
528 phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT);
529 }
530 }
531 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700532 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800533 }
534
535 /**
536 * Tests corruption of the {@code callExtras} bundle by calling {@link
537 * Bundle#containsKey(String)}. If the bundle is corrupted a {@link IllegalArgumentException} will
538 * be thrown and caught by this function.
539 *
540 * @param callExtras the bundle to verify
541 * @return {@code true} if the bundle is corrupted, {@code false} otherwise.
542 */
543 protected boolean areCallExtrasCorrupted(Bundle callExtras) {
544 /**
545 * There's currently a bug in Telephony service (b/25613098) that could corrupt the extras
546 * bundle, resulting in a IllegalArgumentException while validating data under {@link
547 * Bundle#containsKey(String)}.
548 */
549 try {
550 callExtras.containsKey(Connection.EXTRA_CHILD_ADDRESS);
551 return false;
552 } catch (IllegalArgumentException e) {
553 LogUtil.e(
554 "DialerCall.areCallExtrasCorrupted", "callExtras is corrupted, ignoring exception", e);
555 return true;
556 }
557 }
558
559 protected void updateFromCallExtras(Bundle callExtras) {
560 if (callExtras == null || areCallExtrasCorrupted(callExtras)) {
561 /**
562 * If the bundle is corrupted, abandon information update as a work around. These are not
563 * critical for the dialer to function.
564 */
565 return;
566 }
567 // Check for a change in the child address and notify any listeners.
568 if (callExtras.containsKey(Connection.EXTRA_CHILD_ADDRESS)) {
569 String childNumber = callExtras.getString(Connection.EXTRA_CHILD_ADDRESS);
570 if (!Objects.equals(childNumber, mChildNumber)) {
571 mChildNumber = childNumber;
572 for (DialerCallListener listener : mListeners) {
573 listener.onDialerCallChildNumberChange();
574 }
575 }
576 }
577
578 // Last forwarded number comes in as an array of strings. We want to choose the
579 // last item in the array. The forwarding numbers arrive independently of when the
580 // call is originally set up, so we need to notify the the UI of the change.
581 if (callExtras.containsKey(Connection.EXTRA_LAST_FORWARDED_NUMBER)) {
582 ArrayList<String> lastForwardedNumbers =
583 callExtras.getStringArrayList(Connection.EXTRA_LAST_FORWARDED_NUMBER);
584
585 if (lastForwardedNumbers != null) {
586 String lastForwardedNumber = null;
587 if (!lastForwardedNumbers.isEmpty()) {
588 lastForwardedNumber = lastForwardedNumbers.get(lastForwardedNumbers.size() - 1);
589 }
590
591 if (!Objects.equals(lastForwardedNumber, mLastForwardedNumber)) {
592 mLastForwardedNumber = lastForwardedNumber;
593 for (DialerCallListener listener : mListeners) {
594 listener.onDialerCallLastForwardedNumberChange();
595 }
596 }
597 }
598 }
599
600 // DialerCall subject is present in the extras at the start of call, so we do not need to
601 // notify any other listeners of this.
602 if (callExtras.containsKey(Connection.EXTRA_CALL_SUBJECT)) {
603 String callSubject = callExtras.getString(Connection.EXTRA_CALL_SUBJECT);
604 if (!Objects.equals(mCallSubject, callSubject)) {
605 mCallSubject = callSubject;
606 }
607 }
608 }
609
Eric Erfanianccca3152017-02-22 16:32:36 -0800610 public String getId() {
611 return mId;
612 }
613
Eric Erfanianc857f902017-05-15 14:05:33 -0700614 /**
615 * @return name appended with a number if the number is restricted/unknown and the user has
616 * received more than one restricted/unknown call.
617 */
618 @Nullable
619 public String updateNameIfRestricted(@Nullable String name) {
620 if (name != null && isHiddenNumber() && mHiddenId != 0 && sHiddenCounter > 1) {
621 return mContext.getString(R.string.unknown_counter, name, mHiddenId);
622 }
623 return name;
624 }
625
626 public static void clearRestrictedCount() {
627 sHiddenCounter = 0;
628 }
629
630 private boolean isHiddenNumber() {
631 return getNumberPresentation() == TelecomManager.PRESENTATION_RESTRICTED
632 || getNumberPresentation() == TelecomManager.PRESENTATION_UNKNOWN;
633 }
634
Eric Erfanianccca3152017-02-22 16:32:36 -0800635 public boolean hasShownWiFiToLteHandoverToast() {
636 return hasShownWiFiToLteHandoverToast;
637 }
638
639 public void setHasShownWiFiToLteHandoverToast() {
640 hasShownWiFiToLteHandoverToast = true;
641 }
642
643 public boolean showWifiHandoverAlertAsToast() {
644 return doNotShowDialogForHandoffToWifiFailure;
645 }
646
647 public void setDoNotShowDialogForHandoffToWifiFailure(boolean bool) {
648 doNotShowDialogForHandoffToWifiFailure = bool;
649 }
650
651 public long getTimeAddedMs() {
652 return mTimeAddedMs;
653 }
654
655 @Nullable
656 public String getNumber() {
657 return TelecomCallUtil.getNumber(mTelecomCall);
658 }
659
660 public void blockCall() {
661 mTelecomCall.reject(false, null);
662 setState(State.BLOCKED);
663 }
664
665 @Nullable
666 public Uri getHandle() {
667 return mTelecomCall == null ? null : mTelecomCall.getDetails().getHandle();
668 }
669
670 public boolean isEmergencyCall() {
671 return mIsEmergencyCall;
672 }
673
674 public boolean isPotentialEmergencyCallback() {
675 // The property PROPERTY_EMERGENCY_CALLBACK_MODE is only set for CDMA calls when the system
676 // is actually in emergency callback mode (ie data is disabled).
677 if (hasProperty(Details.PROPERTY_EMERGENCY_CALLBACK_MODE)) {
678 return true;
679 }
680 // We want to treat any incoming call that arrives a short time after an outgoing emergency call
681 // as a potential emergency callback.
682 if (getExtras() != null
683 && getExtras().getLong(TelecomManagerCompat.EXTRA_LAST_EMERGENCY_CALLBACK_TIME_MILLIS, 0)
684 > 0) {
685 long lastEmergencyCallMillis =
686 getExtras().getLong(TelecomManagerCompat.EXTRA_LAST_EMERGENCY_CALLBACK_TIME_MILLIS, 0);
687 if (isInEmergencyCallbackWindow(lastEmergencyCallMillis)) {
688 return true;
689 }
690 }
691 return false;
692 }
693
694 boolean isInEmergencyCallbackWindow(long timestampMillis) {
695 long emergencyCallbackWindowMillis =
696 ConfigProviderBindings.get(mContext)
697 .getLong(CONFIG_EMERGENCY_CALLBACK_WINDOW_MILLIS, TimeUnit.MINUTES.toMillis(5));
698 return System.currentTimeMillis() - timestampMillis < emergencyCallbackWindowMillis;
699 }
700
701 public int getState() {
702 if (mTelecomCall != null && mTelecomCall.getParent() != null) {
703 return State.CONFERENCED;
704 } else {
705 return mState;
706 }
707 }
708
709 public void setState(int state) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700710 if (state == State.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800711 mLogState.isIncoming = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700712 } else if (state == State.DISCONNECTED) {
713 long newDuration =
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 getConnectTimeMillis() == 0 ? 0 : System.currentTimeMillis() - getConnectTimeMillis();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700715 if (mState != state) {
716 mLogState.duration = newDuration;
717 } else {
718 LogUtil.i(
719 "DialerCall.setState",
720 "ignoring state transition from DISCONNECTED to DISCONNECTED."
721 + " Duration would have changed from %s to %s",
722 mLogState.duration,
723 newDuration);
724 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800725 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700726 mState = state;
Eric Erfanianccca3152017-02-22 16:32:36 -0800727 }
728
729 public int getNumberPresentation() {
730 return mTelecomCall == null ? -1 : mTelecomCall.getDetails().getHandlePresentation();
731 }
732
733 public int getCnapNamePresentation() {
734 return mTelecomCall == null ? -1 : mTelecomCall.getDetails().getCallerDisplayNamePresentation();
735 }
736
737 @Nullable
738 public String getCnapName() {
739 return mTelecomCall == null ? null : getTelecomCall().getDetails().getCallerDisplayName();
740 }
741
742 public Bundle getIntentExtras() {
743 return mTelecomCall.getDetails().getIntentExtras();
744 }
745
746 @Nullable
747 public Bundle getExtras() {
748 return mTelecomCall == null ? null : mTelecomCall.getDetails().getExtras();
749 }
750
751 /** @return The child number for the call, or {@code null} if none specified. */
752 public String getChildNumber() {
753 return mChildNumber;
754 }
755
756 /** @return The last forwarded number for the call, or {@code null} if none specified. */
757 public String getLastForwardedNumber() {
758 return mLastForwardedNumber;
759 }
760
761 /** @return The call subject, or {@code null} if none specified. */
762 public String getCallSubject() {
763 return mCallSubject;
764 }
765
766 /**
767 * @return {@code true} if the call's phone account supports call subjects, {@code false}
768 * otherwise.
769 */
770 public boolean isCallSubjectSupported() {
771 return mIsCallSubjectSupported;
772 }
773
774 /** Returns call disconnect cause, defined by {@link DisconnectCause}. */
775 public DisconnectCause getDisconnectCause() {
776 if (mState == State.DISCONNECTED || mState == State.IDLE) {
777 return mDisconnectCause;
778 }
779
780 return new DisconnectCause(DisconnectCause.UNKNOWN);
781 }
782
783 public void setDisconnectCause(DisconnectCause disconnectCause) {
784 mDisconnectCause = disconnectCause;
785 mLogState.disconnectCause = mDisconnectCause;
786 }
787
788 /** Returns the possible text message responses. */
789 public List<String> getCannedSmsResponses() {
790 return mTelecomCall.getCannedTextResponses();
791 }
792
793 /** Checks if the call supports the given set of capabilities supplied as a bit mask. */
794 public boolean can(int capabilities) {
795 int supportedCapabilities = mTelecomCall.getDetails().getCallCapabilities();
796
797 if ((capabilities & Call.Details.CAPABILITY_MERGE_CONFERENCE) != 0) {
798 // We allow you to merge if the capabilities allow it or if it is a call with
799 // conferenceable calls.
800 if (mTelecomCall.getConferenceableCalls().isEmpty()
801 && ((Call.Details.CAPABILITY_MERGE_CONFERENCE & supportedCapabilities) == 0)) {
802 // Cannot merge calls if there are no calls to merge with.
803 return false;
804 }
805 capabilities &= ~Call.Details.CAPABILITY_MERGE_CONFERENCE;
806 }
807 return (capabilities == (capabilities & supportedCapabilities));
808 }
809
810 public boolean hasProperty(int property) {
811 return mTelecomCall.getDetails().hasProperty(property);
812 }
813
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 @NonNull
Eric Erfanianccca3152017-02-22 16:32:36 -0800815 public String getUniqueCallId() {
816 return uniqueCallId;
817 }
818
819 /** Gets the time when the call first became active. */
820 public long getConnectTimeMillis() {
821 return mTelecomCall.getDetails().getConnectTimeMillis();
822 }
823
824 public boolean isConferenceCall() {
825 return hasProperty(Call.Details.PROPERTY_CONFERENCE);
826 }
827
828 @Nullable
829 public GatewayInfo getGatewayInfo() {
830 return mTelecomCall == null ? null : mTelecomCall.getDetails().getGatewayInfo();
831 }
832
833 @Nullable
834 public PhoneAccountHandle getAccountHandle() {
835 return mTelecomCall == null ? null : mTelecomCall.getDetails().getAccountHandle();
836 }
837
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700838 /** @return The {@link VideoCall} instance associated with the {@link Call}. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800839 public VideoCall getVideoCall() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700840 return mTelecomCall == null ? null : mTelecomCall.getVideoCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800841 }
842
843 public List<String> getChildCallIds() {
844 return mChildCallIds;
845 }
846
847 public String getParentId() {
848 Call parentCall = mTelecomCall.getParent();
849 if (parentCall != null) {
850 return mDialerCallDelegate.getDialerCallFromTelecomCall(parentCall).getId();
851 }
852 return null;
853 }
854
855 public int getVideoState() {
856 return mTelecomCall.getDetails().getVideoState();
857 }
858
859 public boolean isVideoCall() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700860 return getVideoTech().isTransmittingOrReceiving();
861 }
862
863 public boolean hasReceivedVideoUpgradeRequest() {
864 return VideoUtils.hasReceivedVideoUpgradeRequest(getVideoTech().getSessionModificationState());
865 }
866
867 public boolean hasSentVideoUpgradeRequest() {
868 return VideoUtils.hasSentVideoUpgradeRequest(getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800869 }
870
871 /**
872 * Determines if the call handle is an emergency number or not and caches the result to avoid
873 * repeated calls to isEmergencyNumber.
874 */
875 private void updateEmergencyCallState() {
876 mIsEmergencyCall = TelecomCallUtil.isEmergencyCall(mTelecomCall);
877 }
878
Eric Erfanianccca3152017-02-22 16:32:36 -0800879 public LogState getLogState() {
880 return mLogState;
881 }
882
883 /**
884 * Determines if the call is an external call.
885 *
886 * <p>An external call is one which does not exist locally for the {@link
887 * android.telecom.ConnectionService} it is associated with.
888 *
889 * <p>External calls are only supported in N and higher.
890 *
891 * @return {@code true} if the call is an external call, {@code false} otherwise.
892 */
893 public boolean isExternalCall() {
894 return VERSION.SDK_INT >= VERSION_CODES.N
895 && hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL);
896 }
897
898 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 * Determines if answering this call will cause an ongoing video call to be dropped.
900 *
901 * @return {@code true} if answering this call will drop an ongoing video call, {@code false}
902 * otherwise.
903 */
904 public boolean answeringDisconnectsForegroundVideoCall() {
905 Bundle extras = getExtras();
906 if (extras == null
907 || !extras.containsKey(CallCompat.Details.EXTRA_ANSWERING_DROPS_FOREGROUND_CALL)) {
908 return false;
909 }
910 return extras.getBoolean(CallCompat.Details.EXTRA_ANSWERING_DROPS_FOREGROUND_CALL);
911 }
912
913 private void parseCallSpecificAppData() {
914 if (isExternalCall()) {
915 return;
916 }
917
918 mLogState.callSpecificAppData = CallIntentParser.getCallSpecificAppData(getIntentExtras());
919 if (mLogState.callSpecificAppData == null) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700920
921 mLogState.callSpecificAppData =
922 CallSpecificAppData.newBuilder()
923 .setCallInitiationType(CallInitiationType.Type.EXTERNAL_INITIATION)
924 .build();
Eric Erfanianccca3152017-02-22 16:32:36 -0800925 }
926 if (getState() == State.INCOMING) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700927 mLogState.callSpecificAppData =
928 mLogState
929 .callSpecificAppData
930 .toBuilder()
931 .setCallInitiationType(CallInitiationType.Type.INCOMING_INITIATION)
932 .build();
Eric Erfanianccca3152017-02-22 16:32:36 -0800933 }
934 }
935
936 @Override
937 public String toString() {
938 if (mTelecomCall == null) {
939 // This should happen only in testing since otherwise we would never have a null
940 // Telecom call.
941 return String.valueOf(mId);
942 }
943
944 return String.format(
945 Locale.US,
946 "[%s, %s, %s, %s, children:%s, parent:%s, "
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700947 + "conferenceable:%s, videoState:%s, mSessionModificationState:%d, CameraDir:%s]",
Eric Erfanianccca3152017-02-22 16:32:36 -0800948 mId,
949 State.toString(getState()),
950 Details.capabilitiesToString(mTelecomCall.getDetails().getCallCapabilities()),
951 Details.propertiesToString(mTelecomCall.getDetails().getCallProperties()),
952 mChildCallIds,
953 getParentId(),
954 this.mTelecomCall.getConferenceableCalls(),
955 VideoProfile.videoStateToString(mTelecomCall.getDetails().getVideoState()),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700956 getVideoTech().getSessionModificationState(),
957 getCameraDir());
Eric Erfanianccca3152017-02-22 16:32:36 -0800958 }
959
960 public String toSimpleString() {
961 return super.toString();
962 }
963
964 @CallHistoryStatus
965 public int getCallHistoryStatus() {
966 return mCallHistoryStatus;
967 }
968
969 public void setCallHistoryStatus(@CallHistoryStatus int callHistoryStatus) {
970 mCallHistoryStatus = callHistoryStatus;
971 }
972
973 public boolean didShowCameraPermission() {
974 return didShowCameraPermission;
975 }
976
977 public void setDidShowCameraPermission(boolean didShow) {
978 didShowCameraPermission = didShow;
979 }
980
981 public boolean isInGlobalSpamList() {
982 return isInGlobalSpamList;
983 }
984
985 public void setIsInGlobalSpamList(boolean inSpamList) {
986 isInGlobalSpamList = inSpamList;
987 }
988
989 public boolean isInUserSpamList() {
990 return isInUserSpamList;
991 }
992
993 public void setIsInUserSpamList(boolean inSpamList) {
994 isInUserSpamList = inSpamList;
995 }
996
997 public boolean isInUserWhiteList() {
998 return isInUserWhiteList;
999 }
1000
1001 public void setIsInUserWhiteList(boolean inWhiteList) {
1002 isInUserWhiteList = inWhiteList;
1003 }
1004
1005 public boolean isSpam() {
1006 return mIsSpam;
1007 }
1008
1009 public void setSpam(boolean isSpam) {
1010 mIsSpam = isSpam;
1011 }
1012
1013 public boolean isBlocked() {
1014 return mIsBlocked;
1015 }
1016
1017 public void setBlockedStatus(boolean isBlocked) {
1018 mIsBlocked = isBlocked;
1019 }
1020
1021 public boolean isRemotelyHeld() {
1022 return isRemotelyHeld;
1023 }
1024
Eric Erfanian2ca43182017-08-31 06:57:16 -07001025 public boolean isMergeInProcess() {
1026 return isMergeInProcess;
1027 }
1028
Eric Erfanianccca3152017-02-22 16:32:36 -08001029 public boolean isIncoming() {
1030 return mLogState.isIncoming;
1031 }
1032
Eric Erfanian2ca43182017-08-31 06:57:16 -07001033 public boolean isAssistedDialed() {
1034 if (getIntentExtras() != null) {
1035 return getIntentExtras().getBoolean(TelephonyManagerCompat.IS_ASSISTED_DIALED, false);
1036 }
1037 return false;
1038 }
1039
Eric Erfanianccca3152017-02-22 16:32:36 -08001040 public LatencyReport getLatencyReport() {
1041 return mLatencyReport;
1042 }
1043
Eric Erfanian2ca43182017-08-31 06:57:16 -07001044 public int getAnswerAndReleaseButtonDisplayedTimes() {
1045 return answerAndReleaseButtonDisplayedTimes;
1046 }
1047
1048 public void increaseAnswerAndReleaseButtonDisplayedTimes() {
1049 answerAndReleaseButtonDisplayedTimes++;
1050 }
1051
1052 public boolean getReleasedByAnsweringSecondCall() {
1053 return releasedByAnsweringSecondCall;
1054 }
1055
1056 public void setReleasedByAnsweringSecondCall(boolean releasedByAnsweringSecondCall) {
1057 this.releasedByAnsweringSecondCall = releasedByAnsweringSecondCall;
1058 }
1059
1060 public int getSecondCallWithoutAnswerAndReleasedButtonTimes() {
1061 return secondCallWithoutAnswerAndReleasedButtonTimes;
1062 }
1063
1064 public void increaseSecondCallWithoutAnswerAndReleasedButtonTimes() {
1065 secondCallWithoutAnswerAndReleasedButtonTimes++;
1066 }
1067
Eric Erfanian8369df02017-05-03 10:27:13 -07001068 @Nullable
1069 public EnrichedCallCapabilities getEnrichedCallCapabilities() {
1070 return mEnrichedCallCapabilities;
1071 }
1072
1073 public void setEnrichedCallCapabilities(
1074 @Nullable EnrichedCallCapabilities mEnrichedCallCapabilities) {
1075 this.mEnrichedCallCapabilities = mEnrichedCallCapabilities;
1076 }
1077
1078 @Nullable
1079 public Session getEnrichedCallSession() {
1080 return mEnrichedCallSession;
1081 }
1082
1083 public void setEnrichedCallSession(@Nullable Session mEnrichedCallSession) {
1084 this.mEnrichedCallSession = mEnrichedCallSession;
1085 }
1086
Eric Erfanianccca3152017-02-22 16:32:36 -08001087 public void unregisterCallback() {
1088 mTelecomCall.unregisterCallback(mTelecomCallCallback);
1089 }
1090
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 public void phoneAccountSelected(PhoneAccountHandle accountHandle, boolean setDefault) {
1092 LogUtil.i(
1093 "DialerCall.phoneAccountSelected",
1094 "accountHandle: %s, setDefault: %b",
1095 accountHandle,
1096 setDefault);
1097 mTelecomCall.phoneAccountSelected(accountHandle, setDefault);
1098 }
1099
1100 public void disconnect() {
1101 LogUtil.i("DialerCall.disconnect", "");
1102 setState(DialerCall.State.DISCONNECTING);
1103 for (DialerCallListener listener : mListeners) {
1104 listener.onDialerCallUpdate();
1105 }
1106 mTelecomCall.disconnect();
1107 }
1108
1109 public void hold() {
1110 LogUtil.i("DialerCall.hold", "");
1111 mTelecomCall.hold();
1112 }
1113
1114 public void unhold() {
1115 LogUtil.i("DialerCall.unhold", "");
1116 mTelecomCall.unhold();
1117 }
1118
1119 public void splitFromConference() {
1120 LogUtil.i("DialerCall.splitFromConference", "");
1121 mTelecomCall.splitFromConference();
1122 }
1123
1124 public void answer(int videoState) {
1125 LogUtil.i("DialerCall.answer", "videoState: " + videoState);
1126 mTelecomCall.answer(videoState);
1127 }
1128
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001129 public void answer() {
1130 answer(mTelecomCall.getDetails().getVideoState());
1131 }
1132
Eric Erfanianccca3152017-02-22 16:32:36 -08001133 public void reject(boolean rejectWithMessage, String message) {
1134 LogUtil.i("DialerCall.reject", "");
1135 mTelecomCall.reject(rejectWithMessage, message);
1136 }
1137
1138 /** Return the string label to represent the call provider */
1139 public String getCallProviderLabel() {
1140 if (callProviderLabel == null) {
1141 PhoneAccount account = getPhoneAccount();
1142 if (account != null && !TextUtils.isEmpty(account.getLabel())) {
1143 List<PhoneAccountHandle> accounts =
1144 mContext.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts();
1145 if (accounts != null && accounts.size() > 1) {
1146 callProviderLabel = account.getLabel().toString();
1147 }
1148 }
1149 if (callProviderLabel == null) {
1150 callProviderLabel = "";
1151 }
1152 }
1153 return callProviderLabel;
1154 }
1155
1156 private PhoneAccount getPhoneAccount() {
1157 PhoneAccountHandle accountHandle = getAccountHandle();
1158 if (accountHandle == null) {
1159 return null;
1160 }
1161 return mContext.getSystemService(TelecomManager.class).getPhoneAccount(accountHandle);
1162 }
1163
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001164 public VideoTech getVideoTech() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001165 return videoTech;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001166 }
1167
Eric Erfanianccca3152017-02-22 16:32:36 -08001168 public String getCallbackNumber() {
1169 if (callbackNumber == null) {
1170 // Show the emergency callback number if either:
1171 // 1. This is an emergency call.
1172 // 2. The phone is in Emergency Callback Mode, which means we should show the callback
1173 // number.
1174 boolean showCallbackNumber = hasProperty(Details.PROPERTY_EMERGENCY_CALLBACK_MODE);
1175
1176 if (isEmergencyCall() || showCallbackNumber) {
wangqi339b4f32017-09-18 14:32:09 -07001177 callbackNumber =
1178 mContext.getSystemService(TelecomManager.class).getLine1Number(getAccountHandle());
Eric Erfanianccca3152017-02-22 16:32:36 -08001179 }
1180
Eric Erfanianccca3152017-02-22 16:32:36 -08001181 if (callbackNumber == null) {
1182 callbackNumber = "";
1183 }
1184 }
1185 return callbackNumber;
1186 }
1187
wangqi97539352017-09-25 11:15:16 -07001188 public String getSimCountryIso() {
1189 String simCountryIso =
1190 TelephonyManagerCompat.getTelephonyManagerForPhoneAccountHandle(
1191 mContext, getAccountHandle())
1192 .getSimCountryIso();
1193 if (!TextUtils.isEmpty(simCountryIso)) {
1194 simCountryIso = simCountryIso.toUpperCase(Locale.US);
1195 }
1196 return simCountryIso;
1197 }
1198
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001199 @Override
1200 public void onVideoTechStateChanged() {
1201 update();
1202 }
1203
1204 @Override
1205 public void onSessionModificationStateChanged() {
1206 for (DialerCallListener listener : mListeners) {
1207 listener.onDialerCallSessionModificationStateChange();
1208 }
1209 }
1210
1211 @Override
1212 public void onCameraDimensionsChanged(int width, int height) {
1213 InCallVideoCallCallbackNotifier.getInstance().cameraDimensionsChanged(this, width, height);
1214 }
1215
1216 @Override
1217 public void onPeerDimensionsChanged(int width, int height) {
1218 InCallVideoCallCallbackNotifier.getInstance().peerDimensionsChanged(this, width, height);
1219 }
1220
1221 @Override
1222 public void onVideoUpgradeRequestReceived() {
1223 LogUtil.enterBlock("DialerCall.onVideoUpgradeRequestReceived");
1224
1225 for (DialerCallListener listener : mListeners) {
1226 listener.onDialerCallUpgradeToVideo();
1227 }
1228
1229 update();
Eric Erfanian8369df02017-05-03 10:27:13 -07001230
1231 Logger.get(mContext)
1232 .logCallImpression(
1233 DialerImpression.Type.VIDEO_CALL_REQUEST_RECEIVED, getUniqueCallId(), getTimeAddedMs());
1234 }
1235
1236 @Override
1237 public void onUpgradedToVideo(boolean switchToSpeaker) {
1238 LogUtil.enterBlock("DialerCall.onUpgradedToVideo");
1239
1240 if (!switchToSpeaker) {
1241 return;
1242 }
1243
1244 CallAudioState audioState = AudioModeProvider.getInstance().getAudioState();
1245
1246 if (0 != (CallAudioState.ROUTE_BLUETOOTH & audioState.getSupportedRouteMask())) {
1247 LogUtil.e(
1248 "DialerCall.onUpgradedToVideo",
1249 "toggling speakerphone not allowed when bluetooth supported.");
1250 return;
1251 }
1252
1253 if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
1254 return;
1255 }
1256
1257 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001258 }
1259
Eric Erfanian2ca43182017-08-31 06:57:16 -07001260 @Override
1261 public void onCapabilitiesUpdated() {
1262 if (getNumber() == null) {
1263 return;
1264 }
1265 EnrichedCallCapabilities capabilities =
1266 EnrichedCallComponent.get(mContext).getEnrichedCallManager().getCapabilities(getNumber());
1267 if (capabilities != null) {
1268 setEnrichedCallCapabilities(capabilities);
1269 update();
1270 }
1271 }
1272
1273 @Override
1274 public void onEnrichedCallStateChanged() {
1275 updateEnrichedCallSession();
1276 }
1277
1278 @Override
1279 public void onImpressionLoggingNeeded(DialerImpression.Type impressionType) {
1280 Logger.get(mContext).logCallImpression(impressionType, getUniqueCallId(), getTimeAddedMs());
twyendde01c52017-09-22 10:07:31 -07001281 if (impressionType == DialerImpression.Type.LIGHTBRINGER_UPGRADE_REQUESTED) {
1282 if (getLogState().contactLookupResult == Type.NOT_FOUND) {
1283 Logger.get(mContext)
1284 .logCallImpression(
1285 DialerImpression.Type.LIGHTBRINGER_NON_CONTACT_UPGRADE_REQUESTED,
1286 getUniqueCallId(),
1287 getTimeAddedMs());
1288 }
1289 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001290 }
1291
1292 private void updateEnrichedCallSession() {
1293 if (getNumber() == null) {
1294 return;
1295 }
1296 if (getEnrichedCallSession() != null) {
1297 // State changes to existing sessions are currently handled by the UI components (which have
1298 // their own listeners). Someday instead we could remove those and just call update() here and
1299 // have the usual onDialerCallUpdate update the UI.
1300 dispatchOnEnrichedCallSessionUpdate();
1301 return;
1302 }
1303
1304 EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager();
1305
1306 Filter filter =
1307 isIncoming()
1308 ? manager.createIncomingCallComposerFilter()
1309 : manager.createOutgoingCallComposerFilter();
1310
1311 Session session = manager.getSession(getUniqueCallId(), getNumber(), filter);
1312 if (session == null) {
1313 return;
1314 }
1315
1316 session.setUniqueDialerCallId(getUniqueCallId());
1317 setEnrichedCallSession(session);
1318
1319 LogUtil.i(
1320 "DialerCall.updateEnrichedCallSession",
1321 "setting session %d's dialer id to %s",
1322 session.getSessionId(),
1323 getUniqueCallId());
1324
1325 dispatchOnEnrichedCallSessionUpdate();
1326 }
1327
1328 private void dispatchOnEnrichedCallSessionUpdate() {
1329 for (DialerCallListener listener : mListeners) {
1330 listener.onEnrichedCallSessionUpdate();
1331 }
1332 }
1333
1334 void onRemovedFromCallList() {
1335 // Ensure we clean up when this call is removed.
1336 mVideoTechManager.dispatchRemovedFromCallList();
1337 }
1338
wangqi4d705e52017-09-28 12:23:35 -07001339 public com.android.dialer.logging.VideoTech.Type getSelectedAvailableVideoTechType() {
1340 return selectedAvailableVideoTechType;
yueg457b3972017-09-18 15:11:47 -07001341 }
1342
Eric Erfanianccca3152017-02-22 16:32:36 -08001343 /**
1344 * Specifies whether a number is in the call history or not. {@link #CALL_HISTORY_STATUS_UNKNOWN}
1345 * means there is no result.
1346 */
1347 @IntDef({
1348 CALL_HISTORY_STATUS_UNKNOWN,
1349 CALL_HISTORY_STATUS_PRESENT,
1350 CALL_HISTORY_STATUS_NOT_PRESENT
1351 })
1352 @Retention(RetentionPolicy.SOURCE)
1353 public @interface CallHistoryStatus {}
1354
1355 /* Defines different states of this call */
1356 public static class State {
1357
1358 public static final int INVALID = 0;
1359 public static final int NEW = 1; /* The call is new. */
1360 public static final int IDLE = 2; /* The call is idle. Nothing active */
1361 public static final int ACTIVE = 3; /* There is an active call */
1362 public static final int INCOMING = 4; /* A normal incoming phone call */
1363 public static final int CALL_WAITING = 5; /* Incoming call while another is active */
1364 public static final int DIALING = 6; /* An outgoing call during dial phase */
1365 public static final int REDIALING = 7; /* Subsequent dialing attempt after a failure */
1366 public static final int ONHOLD = 8; /* An active phone call placed on hold */
1367 public static final int DISCONNECTING = 9; /* A call is being ended. */
1368 public static final int DISCONNECTED = 10; /* State after a call disconnects */
1369 public static final int CONFERENCED = 11; /* DialerCall part of a conference call */
1370 public static final int SELECT_PHONE_ACCOUNT = 12; /* Waiting for account selection */
1371 public static final int CONNECTING = 13; /* Waiting for Telecom broadcast to finish */
1372 public static final int BLOCKED = 14; /* The number was found on the block list */
1373 public static final int PULLING = 15; /* An external call being pulled to the device */
Eric Erfanian2ca43182017-08-31 06:57:16 -07001374 public static final int CALL_PENDING = 16; /* A call is pending on a long process to finish */
Eric Erfanianccca3152017-02-22 16:32:36 -08001375
1376 public static boolean isConnectingOrConnected(int state) {
1377 switch (state) {
1378 case ACTIVE:
1379 case INCOMING:
1380 case CALL_WAITING:
1381 case CONNECTING:
1382 case DIALING:
1383 case PULLING:
1384 case REDIALING:
1385 case ONHOLD:
1386 case CONFERENCED:
1387 return true;
1388 default:
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001389 return false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001390 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001391 }
1392
1393 public static boolean isDialing(int state) {
1394 return state == DIALING || state == PULLING || state == REDIALING;
1395 }
1396
1397 public static String toString(int state) {
1398 switch (state) {
1399 case INVALID:
1400 return "INVALID";
1401 case NEW:
1402 return "NEW";
1403 case IDLE:
1404 return "IDLE";
1405 case ACTIVE:
1406 return "ACTIVE";
1407 case INCOMING:
1408 return "INCOMING";
1409 case CALL_WAITING:
1410 return "CALL_WAITING";
1411 case DIALING:
1412 return "DIALING";
1413 case PULLING:
1414 return "PULLING";
1415 case REDIALING:
1416 return "REDIALING";
1417 case ONHOLD:
1418 return "ONHOLD";
1419 case DISCONNECTING:
1420 return "DISCONNECTING";
1421 case DISCONNECTED:
1422 return "DISCONNECTED";
1423 case CONFERENCED:
1424 return "CONFERENCED";
1425 case SELECT_PHONE_ACCOUNT:
1426 return "SELECT_PHONE_ACCOUNT";
1427 case CONNECTING:
1428 return "CONNECTING";
1429 case BLOCKED:
1430 return "BLOCKED";
1431 default:
1432 return "UNKNOWN";
1433 }
1434 }
1435 }
1436
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001437 /** Camera direction constants */
1438 public static class CameraDirection {
Eric Erfanianccca3152017-02-22 16:32:36 -08001439 public static final int CAMERA_DIRECTION_UNKNOWN = -1;
1440 public static final int CAMERA_DIRECTION_FRONT_FACING = CameraCharacteristics.LENS_FACING_FRONT;
1441 public static final int CAMERA_DIRECTION_BACK_FACING = CameraCharacteristics.LENS_FACING_BACK;
Eric Erfanianccca3152017-02-22 16:32:36 -08001442 }
1443
1444 /**
1445 * Tracks any state variables that is useful for logging. There is some amount of overlap with
1446 * existing call member variables, but this duplication helps to ensure that none of these logging
1447 * variables will interface with/and affect call logic.
1448 */
1449 public static class LogState {
1450
1451 public DisconnectCause disconnectCause;
1452 public boolean isIncoming = false;
Eric Erfanian8369df02017-05-03 10:27:13 -07001453 public ContactLookupResult.Type contactLookupResult =
1454 ContactLookupResult.Type.UNKNOWN_LOOKUP_RESULT_TYPE;
Eric Erfanianccca3152017-02-22 16:32:36 -08001455 public CallSpecificAppData callSpecificAppData;
1456 // If this was a conference call, the total number of calls involved in the conference.
1457 public int conferencedCalls = 0;
1458 public long duration = 0;
1459 public boolean isLogged = false;
1460
Eric Erfanian8369df02017-05-03 10:27:13 -07001461 private static String lookupToString(ContactLookupResult.Type lookupType) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001462 switch (lookupType) {
Eric Erfanian8369df02017-05-03 10:27:13 -07001463 case LOCAL_CONTACT:
Eric Erfanianccca3152017-02-22 16:32:36 -08001464 return "Local";
Eric Erfanian8369df02017-05-03 10:27:13 -07001465 case LOCAL_CACHE:
Eric Erfanianccca3152017-02-22 16:32:36 -08001466 return "Cache";
Eric Erfanian8369df02017-05-03 10:27:13 -07001467 case REMOTE:
Eric Erfanianccca3152017-02-22 16:32:36 -08001468 return "Remote";
Eric Erfanian8369df02017-05-03 10:27:13 -07001469 case EMERGENCY:
Eric Erfanianccca3152017-02-22 16:32:36 -08001470 return "Emergency";
Eric Erfanian8369df02017-05-03 10:27:13 -07001471 case VOICEMAIL:
Eric Erfanianccca3152017-02-22 16:32:36 -08001472 return "Voicemail";
1473 default:
1474 return "Not found";
1475 }
1476 }
1477
1478 private static String initiationToString(CallSpecificAppData callSpecificAppData) {
1479 if (callSpecificAppData == null) {
1480 return "null";
1481 }
Eric Erfanian8369df02017-05-03 10:27:13 -07001482 switch (callSpecificAppData.getCallInitiationType()) {
1483 case INCOMING_INITIATION:
Eric Erfanianccca3152017-02-22 16:32:36 -08001484 return "Incoming";
Eric Erfanian8369df02017-05-03 10:27:13 -07001485 case DIALPAD:
Eric Erfanianccca3152017-02-22 16:32:36 -08001486 return "Dialpad";
Eric Erfanian8369df02017-05-03 10:27:13 -07001487 case SPEED_DIAL:
Eric Erfanianccca3152017-02-22 16:32:36 -08001488 return "Speed Dial";
Eric Erfanian8369df02017-05-03 10:27:13 -07001489 case REMOTE_DIRECTORY:
Eric Erfanianccca3152017-02-22 16:32:36 -08001490 return "Remote Directory";
Eric Erfanian8369df02017-05-03 10:27:13 -07001491 case SMART_DIAL:
Eric Erfanianccca3152017-02-22 16:32:36 -08001492 return "Smart Dial";
Eric Erfanian8369df02017-05-03 10:27:13 -07001493 case REGULAR_SEARCH:
Eric Erfanianccca3152017-02-22 16:32:36 -08001494 return "Regular Search";
Eric Erfanian8369df02017-05-03 10:27:13 -07001495 case CALL_LOG:
Eric Erfanianccca3152017-02-22 16:32:36 -08001496 return "DialerCall Log";
Eric Erfanian8369df02017-05-03 10:27:13 -07001497 case CALL_LOG_FILTER:
Eric Erfanianccca3152017-02-22 16:32:36 -08001498 return "DialerCall Log Filter";
Eric Erfanian8369df02017-05-03 10:27:13 -07001499 case VOICEMAIL_LOG:
Eric Erfanianccca3152017-02-22 16:32:36 -08001500 return "Voicemail Log";
Eric Erfanian8369df02017-05-03 10:27:13 -07001501 case CALL_DETAILS:
Eric Erfanianccca3152017-02-22 16:32:36 -08001502 return "DialerCall Details";
Eric Erfanian8369df02017-05-03 10:27:13 -07001503 case QUICK_CONTACTS:
Eric Erfanianccca3152017-02-22 16:32:36 -08001504 return "Quick Contacts";
Eric Erfanian8369df02017-05-03 10:27:13 -07001505 case EXTERNAL_INITIATION:
Eric Erfanianccca3152017-02-22 16:32:36 -08001506 return "External";
Eric Erfanian8369df02017-05-03 10:27:13 -07001507 case LAUNCHER_SHORTCUT:
Eric Erfanianccca3152017-02-22 16:32:36 -08001508 return "Launcher Shortcut";
1509 default:
Eric Erfanian8369df02017-05-03 10:27:13 -07001510 return "Unknown: " + callSpecificAppData.getCallInitiationType();
Eric Erfanianccca3152017-02-22 16:32:36 -08001511 }
1512 }
1513
1514 @Override
1515 public String toString() {
1516 return String.format(
1517 Locale.US,
1518 "["
1519 + "%s, " // DisconnectCause toString already describes the object type
1520 + "isIncoming: %s, "
1521 + "contactLookup: %s, "
1522 + "callInitiation: %s, "
1523 + "duration: %s"
1524 + "]",
1525 disconnectCause,
1526 isIncoming,
1527 lookupToString(contactLookupResult),
1528 initiationToString(callSpecificAppData),
1529 duration);
1530 }
1531 }
1532
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001533 private static class VideoTechManager {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001534 private final Context context;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001535 private final EmptyVideoTech emptyVideoTech = new EmptyVideoTech();
Eric Erfanian90508232017-03-24 09:31:16 -07001536 private final List<VideoTech> videoTechs;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001537
1538 VideoTechManager(DialerCall call) {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001539 this.context = call.mContext;
1540
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001541 String phoneNumber = call.getNumber();
Eric Erfaniand8046e52017-04-06 09:41:50 -07001542 phoneNumber = phoneNumber != null ? phoneNumber : "";
Eric Erfanian2ca43182017-08-31 06:57:16 -07001543 phoneNumber = phoneNumber.replaceAll("[^+0-9]", "");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001544
1545 // Insert order here determines the priority of that video tech option
Eric Erfanian8369df02017-05-03 10:27:13 -07001546 videoTechs = new ArrayList<>();
yueg457b3972017-09-18 15:11:47 -07001547
wangqi4d705e52017-09-28 12:23:35 -07001548 videoTechs.add(new ImsVideoTech(Logger.get(call.mContext), call, call.mTelecomCall));
Eric Erfanian90508232017-03-24 09:31:16 -07001549
1550 VideoTech rcsVideoTech =
1551 EnrichedCallComponent.get(call.mContext)
1552 .getRcsVideoShareFactory()
1553 .newRcsVideoShare(
1554 EnrichedCallComponent.get(call.mContext).getEnrichedCallManager(),
1555 call,
Eric Erfaniand8046e52017-04-06 09:41:50 -07001556 phoneNumber);
Eric Erfanian90508232017-03-24 09:31:16 -07001557 if (rcsVideoTech != null) {
1558 videoTechs.add(rcsVideoTech);
1559 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001560
1561 videoTechs.add(
1562 new LightbringerTech(
Eric Erfanian2ca43182017-08-31 06:57:16 -07001563 LightbringerComponent.get(call.mContext).getLightbringer(),
1564 call,
1565 call.mTelecomCall,
1566 phoneNumber));
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001567 }
1568
wangqi4d705e52017-09-28 12:23:35 -07001569 VideoTech findBestAvailableVideoTech() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001570 for (VideoTech tech : videoTechs) {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001571 if (tech.isAvailable(context)) {
wangqi4d705e52017-09-28 12:23:35 -07001572 return tech;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001573 }
1574 }
1575
1576 return emptyVideoTech;
1577 }
1578
1579 void dispatchCallStateChanged(int newState) {
1580 for (VideoTech videoTech : videoTechs) {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001581 videoTech.onCallStateChanged(context, newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001582 }
1583 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001584
1585 void dispatchRemovedFromCallList() {
1586 for (VideoTech videoTech : videoTechs) {
1587 videoTech.onRemovedFromCallList();
1588 }
1589 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001590 }
1591
Eric Erfanianccca3152017-02-22 16:32:36 -08001592 /** Called when canned text responses have been loaded. */
1593 public interface CannedTextResponsesLoadedListener {
1594 void onCannedTextResponsesLoaded(DialerCall call);
1595 }
1596}