blob: d393b421189fbc7f6198c7221624352ba1854926 [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
yueg265089a2017-10-06 14:35:15 -0700709 public int getNonConferenceState() {
710 return mState;
711 }
712
Eric Erfanianccca3152017-02-22 16:32:36 -0800713 public void setState(int state) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700714 if (state == State.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800715 mLogState.isIncoming = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700716 } else if (state == State.DISCONNECTED) {
717 long newDuration =
Eric Erfanianccca3152017-02-22 16:32:36 -0800718 getConnectTimeMillis() == 0 ? 0 : System.currentTimeMillis() - getConnectTimeMillis();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700719 if (mState != state) {
720 mLogState.duration = newDuration;
721 } else {
722 LogUtil.i(
723 "DialerCall.setState",
724 "ignoring state transition from DISCONNECTED to DISCONNECTED."
725 + " Duration would have changed from %s to %s",
726 mLogState.duration,
727 newDuration);
728 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800729 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700730 mState = state;
Eric Erfanianccca3152017-02-22 16:32:36 -0800731 }
732
733 public int getNumberPresentation() {
734 return mTelecomCall == null ? -1 : mTelecomCall.getDetails().getHandlePresentation();
735 }
736
737 public int getCnapNamePresentation() {
738 return mTelecomCall == null ? -1 : mTelecomCall.getDetails().getCallerDisplayNamePresentation();
739 }
740
741 @Nullable
742 public String getCnapName() {
743 return mTelecomCall == null ? null : getTelecomCall().getDetails().getCallerDisplayName();
744 }
745
746 public Bundle getIntentExtras() {
747 return mTelecomCall.getDetails().getIntentExtras();
748 }
749
750 @Nullable
751 public Bundle getExtras() {
752 return mTelecomCall == null ? null : mTelecomCall.getDetails().getExtras();
753 }
754
755 /** @return The child number for the call, or {@code null} if none specified. */
756 public String getChildNumber() {
757 return mChildNumber;
758 }
759
760 /** @return The last forwarded number for the call, or {@code null} if none specified. */
761 public String getLastForwardedNumber() {
762 return mLastForwardedNumber;
763 }
764
765 /** @return The call subject, or {@code null} if none specified. */
766 public String getCallSubject() {
767 return mCallSubject;
768 }
769
770 /**
771 * @return {@code true} if the call's phone account supports call subjects, {@code false}
772 * otherwise.
773 */
774 public boolean isCallSubjectSupported() {
775 return mIsCallSubjectSupported;
776 }
777
778 /** Returns call disconnect cause, defined by {@link DisconnectCause}. */
779 public DisconnectCause getDisconnectCause() {
780 if (mState == State.DISCONNECTED || mState == State.IDLE) {
781 return mDisconnectCause;
782 }
783
784 return new DisconnectCause(DisconnectCause.UNKNOWN);
785 }
786
787 public void setDisconnectCause(DisconnectCause disconnectCause) {
788 mDisconnectCause = disconnectCause;
789 mLogState.disconnectCause = mDisconnectCause;
790 }
791
792 /** Returns the possible text message responses. */
793 public List<String> getCannedSmsResponses() {
794 return mTelecomCall.getCannedTextResponses();
795 }
796
797 /** Checks if the call supports the given set of capabilities supplied as a bit mask. */
798 public boolean can(int capabilities) {
799 int supportedCapabilities = mTelecomCall.getDetails().getCallCapabilities();
800
801 if ((capabilities & Call.Details.CAPABILITY_MERGE_CONFERENCE) != 0) {
802 // We allow you to merge if the capabilities allow it or if it is a call with
803 // conferenceable calls.
804 if (mTelecomCall.getConferenceableCalls().isEmpty()
805 && ((Call.Details.CAPABILITY_MERGE_CONFERENCE & supportedCapabilities) == 0)) {
806 // Cannot merge calls if there are no calls to merge with.
807 return false;
808 }
809 capabilities &= ~Call.Details.CAPABILITY_MERGE_CONFERENCE;
810 }
811 return (capabilities == (capabilities & supportedCapabilities));
812 }
813
814 public boolean hasProperty(int property) {
815 return mTelecomCall.getDetails().hasProperty(property);
816 }
817
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700818 @NonNull
Eric Erfanianccca3152017-02-22 16:32:36 -0800819 public String getUniqueCallId() {
820 return uniqueCallId;
821 }
822
823 /** Gets the time when the call first became active. */
824 public long getConnectTimeMillis() {
825 return mTelecomCall.getDetails().getConnectTimeMillis();
826 }
827
828 public boolean isConferenceCall() {
829 return hasProperty(Call.Details.PROPERTY_CONFERENCE);
830 }
831
832 @Nullable
833 public GatewayInfo getGatewayInfo() {
834 return mTelecomCall == null ? null : mTelecomCall.getDetails().getGatewayInfo();
835 }
836
837 @Nullable
838 public PhoneAccountHandle getAccountHandle() {
839 return mTelecomCall == null ? null : mTelecomCall.getDetails().getAccountHandle();
840 }
841
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700842 /** @return The {@link VideoCall} instance associated with the {@link Call}. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800843 public VideoCall getVideoCall() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700844 return mTelecomCall == null ? null : mTelecomCall.getVideoCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800845 }
846
847 public List<String> getChildCallIds() {
848 return mChildCallIds;
849 }
850
851 public String getParentId() {
852 Call parentCall = mTelecomCall.getParent();
853 if (parentCall != null) {
854 return mDialerCallDelegate.getDialerCallFromTelecomCall(parentCall).getId();
855 }
856 return null;
857 }
858
859 public int getVideoState() {
860 return mTelecomCall.getDetails().getVideoState();
861 }
862
863 public boolean isVideoCall() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700864 return getVideoTech().isTransmittingOrReceiving();
865 }
866
867 public boolean hasReceivedVideoUpgradeRequest() {
868 return VideoUtils.hasReceivedVideoUpgradeRequest(getVideoTech().getSessionModificationState());
869 }
870
871 public boolean hasSentVideoUpgradeRequest() {
872 return VideoUtils.hasSentVideoUpgradeRequest(getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 }
874
875 /**
876 * Determines if the call handle is an emergency number or not and caches the result to avoid
877 * repeated calls to isEmergencyNumber.
878 */
879 private void updateEmergencyCallState() {
880 mIsEmergencyCall = TelecomCallUtil.isEmergencyCall(mTelecomCall);
881 }
882
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 public LogState getLogState() {
884 return mLogState;
885 }
886
887 /**
888 * Determines if the call is an external call.
889 *
890 * <p>An external call is one which does not exist locally for the {@link
891 * android.telecom.ConnectionService} it is associated with.
892 *
893 * <p>External calls are only supported in N and higher.
894 *
895 * @return {@code true} if the call is an external call, {@code false} otherwise.
896 */
897 public boolean isExternalCall() {
898 return VERSION.SDK_INT >= VERSION_CODES.N
899 && hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL);
900 }
901
902 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800903 * Determines if answering this call will cause an ongoing video call to be dropped.
904 *
905 * @return {@code true} if answering this call will drop an ongoing video call, {@code false}
906 * otherwise.
907 */
908 public boolean answeringDisconnectsForegroundVideoCall() {
909 Bundle extras = getExtras();
910 if (extras == null
911 || !extras.containsKey(CallCompat.Details.EXTRA_ANSWERING_DROPS_FOREGROUND_CALL)) {
912 return false;
913 }
914 return extras.getBoolean(CallCompat.Details.EXTRA_ANSWERING_DROPS_FOREGROUND_CALL);
915 }
916
917 private void parseCallSpecificAppData() {
918 if (isExternalCall()) {
919 return;
920 }
921
922 mLogState.callSpecificAppData = CallIntentParser.getCallSpecificAppData(getIntentExtras());
923 if (mLogState.callSpecificAppData == null) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700924
925 mLogState.callSpecificAppData =
926 CallSpecificAppData.newBuilder()
927 .setCallInitiationType(CallInitiationType.Type.EXTERNAL_INITIATION)
928 .build();
Eric Erfanianccca3152017-02-22 16:32:36 -0800929 }
930 if (getState() == State.INCOMING) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700931 mLogState.callSpecificAppData =
932 mLogState
933 .callSpecificAppData
934 .toBuilder()
935 .setCallInitiationType(CallInitiationType.Type.INCOMING_INITIATION)
936 .build();
Eric Erfanianccca3152017-02-22 16:32:36 -0800937 }
938 }
939
940 @Override
941 public String toString() {
942 if (mTelecomCall == null) {
943 // This should happen only in testing since otherwise we would never have a null
944 // Telecom call.
945 return String.valueOf(mId);
946 }
947
948 return String.format(
949 Locale.US,
950 "[%s, %s, %s, %s, children:%s, parent:%s, "
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700951 + "conferenceable:%s, videoState:%s, mSessionModificationState:%d, CameraDir:%s]",
Eric Erfanianccca3152017-02-22 16:32:36 -0800952 mId,
953 State.toString(getState()),
954 Details.capabilitiesToString(mTelecomCall.getDetails().getCallCapabilities()),
955 Details.propertiesToString(mTelecomCall.getDetails().getCallProperties()),
956 mChildCallIds,
957 getParentId(),
958 this.mTelecomCall.getConferenceableCalls(),
959 VideoProfile.videoStateToString(mTelecomCall.getDetails().getVideoState()),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700960 getVideoTech().getSessionModificationState(),
961 getCameraDir());
Eric Erfanianccca3152017-02-22 16:32:36 -0800962 }
963
964 public String toSimpleString() {
965 return super.toString();
966 }
967
968 @CallHistoryStatus
969 public int getCallHistoryStatus() {
970 return mCallHistoryStatus;
971 }
972
973 public void setCallHistoryStatus(@CallHistoryStatus int callHistoryStatus) {
974 mCallHistoryStatus = callHistoryStatus;
975 }
976
977 public boolean didShowCameraPermission() {
978 return didShowCameraPermission;
979 }
980
981 public void setDidShowCameraPermission(boolean didShow) {
982 didShowCameraPermission = didShow;
983 }
984
985 public boolean isInGlobalSpamList() {
986 return isInGlobalSpamList;
987 }
988
989 public void setIsInGlobalSpamList(boolean inSpamList) {
990 isInGlobalSpamList = inSpamList;
991 }
992
993 public boolean isInUserSpamList() {
994 return isInUserSpamList;
995 }
996
997 public void setIsInUserSpamList(boolean inSpamList) {
998 isInUserSpamList = inSpamList;
999 }
1000
1001 public boolean isInUserWhiteList() {
1002 return isInUserWhiteList;
1003 }
1004
1005 public void setIsInUserWhiteList(boolean inWhiteList) {
1006 isInUserWhiteList = inWhiteList;
1007 }
1008
1009 public boolean isSpam() {
1010 return mIsSpam;
1011 }
1012
1013 public void setSpam(boolean isSpam) {
1014 mIsSpam = isSpam;
1015 }
1016
1017 public boolean isBlocked() {
1018 return mIsBlocked;
1019 }
1020
1021 public void setBlockedStatus(boolean isBlocked) {
1022 mIsBlocked = isBlocked;
1023 }
1024
1025 public boolean isRemotelyHeld() {
1026 return isRemotelyHeld;
1027 }
1028
Eric Erfanian2ca43182017-08-31 06:57:16 -07001029 public boolean isMergeInProcess() {
1030 return isMergeInProcess;
1031 }
1032
Eric Erfanianccca3152017-02-22 16:32:36 -08001033 public boolean isIncoming() {
1034 return mLogState.isIncoming;
1035 }
1036
Eric Erfanian2ca43182017-08-31 06:57:16 -07001037 public boolean isAssistedDialed() {
1038 if (getIntentExtras() != null) {
1039 return getIntentExtras().getBoolean(TelephonyManagerCompat.IS_ASSISTED_DIALED, false);
1040 }
1041 return false;
1042 }
1043
Eric Erfanianccca3152017-02-22 16:32:36 -08001044 public LatencyReport getLatencyReport() {
1045 return mLatencyReport;
1046 }
1047
Eric Erfanian2ca43182017-08-31 06:57:16 -07001048 public int getAnswerAndReleaseButtonDisplayedTimes() {
1049 return answerAndReleaseButtonDisplayedTimes;
1050 }
1051
1052 public void increaseAnswerAndReleaseButtonDisplayedTimes() {
1053 answerAndReleaseButtonDisplayedTimes++;
1054 }
1055
1056 public boolean getReleasedByAnsweringSecondCall() {
1057 return releasedByAnsweringSecondCall;
1058 }
1059
1060 public void setReleasedByAnsweringSecondCall(boolean releasedByAnsweringSecondCall) {
1061 this.releasedByAnsweringSecondCall = releasedByAnsweringSecondCall;
1062 }
1063
1064 public int getSecondCallWithoutAnswerAndReleasedButtonTimes() {
1065 return secondCallWithoutAnswerAndReleasedButtonTimes;
1066 }
1067
1068 public void increaseSecondCallWithoutAnswerAndReleasedButtonTimes() {
1069 secondCallWithoutAnswerAndReleasedButtonTimes++;
1070 }
1071
Eric Erfanian8369df02017-05-03 10:27:13 -07001072 @Nullable
1073 public EnrichedCallCapabilities getEnrichedCallCapabilities() {
1074 return mEnrichedCallCapabilities;
1075 }
1076
1077 public void setEnrichedCallCapabilities(
1078 @Nullable EnrichedCallCapabilities mEnrichedCallCapabilities) {
1079 this.mEnrichedCallCapabilities = mEnrichedCallCapabilities;
1080 }
1081
1082 @Nullable
1083 public Session getEnrichedCallSession() {
1084 return mEnrichedCallSession;
1085 }
1086
1087 public void setEnrichedCallSession(@Nullable Session mEnrichedCallSession) {
1088 this.mEnrichedCallSession = mEnrichedCallSession;
1089 }
1090
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 public void unregisterCallback() {
1092 mTelecomCall.unregisterCallback(mTelecomCallCallback);
1093 }
1094
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 public void phoneAccountSelected(PhoneAccountHandle accountHandle, boolean setDefault) {
1096 LogUtil.i(
1097 "DialerCall.phoneAccountSelected",
1098 "accountHandle: %s, setDefault: %b",
1099 accountHandle,
1100 setDefault);
1101 mTelecomCall.phoneAccountSelected(accountHandle, setDefault);
1102 }
1103
1104 public void disconnect() {
1105 LogUtil.i("DialerCall.disconnect", "");
1106 setState(DialerCall.State.DISCONNECTING);
1107 for (DialerCallListener listener : mListeners) {
1108 listener.onDialerCallUpdate();
1109 }
1110 mTelecomCall.disconnect();
1111 }
1112
1113 public void hold() {
1114 LogUtil.i("DialerCall.hold", "");
1115 mTelecomCall.hold();
1116 }
1117
1118 public void unhold() {
1119 LogUtil.i("DialerCall.unhold", "");
1120 mTelecomCall.unhold();
1121 }
1122
1123 public void splitFromConference() {
1124 LogUtil.i("DialerCall.splitFromConference", "");
1125 mTelecomCall.splitFromConference();
1126 }
1127
1128 public void answer(int videoState) {
1129 LogUtil.i("DialerCall.answer", "videoState: " + videoState);
1130 mTelecomCall.answer(videoState);
1131 }
1132
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001133 public void answer() {
1134 answer(mTelecomCall.getDetails().getVideoState());
1135 }
1136
Eric Erfanianccca3152017-02-22 16:32:36 -08001137 public void reject(boolean rejectWithMessage, String message) {
1138 LogUtil.i("DialerCall.reject", "");
1139 mTelecomCall.reject(rejectWithMessage, message);
1140 }
1141
1142 /** Return the string label to represent the call provider */
1143 public String getCallProviderLabel() {
1144 if (callProviderLabel == null) {
1145 PhoneAccount account = getPhoneAccount();
1146 if (account != null && !TextUtils.isEmpty(account.getLabel())) {
1147 List<PhoneAccountHandle> accounts =
1148 mContext.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts();
1149 if (accounts != null && accounts.size() > 1) {
1150 callProviderLabel = account.getLabel().toString();
1151 }
1152 }
1153 if (callProviderLabel == null) {
1154 callProviderLabel = "";
1155 }
1156 }
1157 return callProviderLabel;
1158 }
1159
1160 private PhoneAccount getPhoneAccount() {
1161 PhoneAccountHandle accountHandle = getAccountHandle();
1162 if (accountHandle == null) {
1163 return null;
1164 }
1165 return mContext.getSystemService(TelecomManager.class).getPhoneAccount(accountHandle);
1166 }
1167
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001168 public VideoTech getVideoTech() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001169 return videoTech;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001170 }
1171
Eric Erfanianccca3152017-02-22 16:32:36 -08001172 public String getCallbackNumber() {
1173 if (callbackNumber == null) {
1174 // Show the emergency callback number if either:
1175 // 1. This is an emergency call.
1176 // 2. The phone is in Emergency Callback Mode, which means we should show the callback
1177 // number.
1178 boolean showCallbackNumber = hasProperty(Details.PROPERTY_EMERGENCY_CALLBACK_MODE);
1179
1180 if (isEmergencyCall() || showCallbackNumber) {
wangqi339b4f32017-09-18 14:32:09 -07001181 callbackNumber =
1182 mContext.getSystemService(TelecomManager.class).getLine1Number(getAccountHandle());
Eric Erfanianccca3152017-02-22 16:32:36 -08001183 }
1184
Eric Erfanianccca3152017-02-22 16:32:36 -08001185 if (callbackNumber == null) {
1186 callbackNumber = "";
1187 }
1188 }
1189 return callbackNumber;
1190 }
1191
wangqi97539352017-09-25 11:15:16 -07001192 public String getSimCountryIso() {
1193 String simCountryIso =
1194 TelephonyManagerCompat.getTelephonyManagerForPhoneAccountHandle(
1195 mContext, getAccountHandle())
1196 .getSimCountryIso();
1197 if (!TextUtils.isEmpty(simCountryIso)) {
1198 simCountryIso = simCountryIso.toUpperCase(Locale.US);
1199 }
1200 return simCountryIso;
1201 }
1202
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001203 @Override
1204 public void onVideoTechStateChanged() {
1205 update();
1206 }
1207
1208 @Override
1209 public void onSessionModificationStateChanged() {
1210 for (DialerCallListener listener : mListeners) {
1211 listener.onDialerCallSessionModificationStateChange();
1212 }
1213 }
1214
1215 @Override
1216 public void onCameraDimensionsChanged(int width, int height) {
1217 InCallVideoCallCallbackNotifier.getInstance().cameraDimensionsChanged(this, width, height);
1218 }
1219
1220 @Override
1221 public void onPeerDimensionsChanged(int width, int height) {
1222 InCallVideoCallCallbackNotifier.getInstance().peerDimensionsChanged(this, width, height);
1223 }
1224
1225 @Override
1226 public void onVideoUpgradeRequestReceived() {
1227 LogUtil.enterBlock("DialerCall.onVideoUpgradeRequestReceived");
1228
1229 for (DialerCallListener listener : mListeners) {
1230 listener.onDialerCallUpgradeToVideo();
1231 }
1232
1233 update();
Eric Erfanian8369df02017-05-03 10:27:13 -07001234
1235 Logger.get(mContext)
1236 .logCallImpression(
1237 DialerImpression.Type.VIDEO_CALL_REQUEST_RECEIVED, getUniqueCallId(), getTimeAddedMs());
1238 }
1239
1240 @Override
1241 public void onUpgradedToVideo(boolean switchToSpeaker) {
1242 LogUtil.enterBlock("DialerCall.onUpgradedToVideo");
1243
1244 if (!switchToSpeaker) {
1245 return;
1246 }
1247
1248 CallAudioState audioState = AudioModeProvider.getInstance().getAudioState();
1249
1250 if (0 != (CallAudioState.ROUTE_BLUETOOTH & audioState.getSupportedRouteMask())) {
1251 LogUtil.e(
1252 "DialerCall.onUpgradedToVideo",
1253 "toggling speakerphone not allowed when bluetooth supported.");
1254 return;
1255 }
1256
1257 if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
1258 return;
1259 }
1260
1261 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001262 }
1263
Eric Erfanian2ca43182017-08-31 06:57:16 -07001264 @Override
1265 public void onCapabilitiesUpdated() {
1266 if (getNumber() == null) {
1267 return;
1268 }
1269 EnrichedCallCapabilities capabilities =
1270 EnrichedCallComponent.get(mContext).getEnrichedCallManager().getCapabilities(getNumber());
1271 if (capabilities != null) {
1272 setEnrichedCallCapabilities(capabilities);
1273 update();
1274 }
1275 }
1276
1277 @Override
1278 public void onEnrichedCallStateChanged() {
1279 updateEnrichedCallSession();
1280 }
1281
1282 @Override
1283 public void onImpressionLoggingNeeded(DialerImpression.Type impressionType) {
1284 Logger.get(mContext).logCallImpression(impressionType, getUniqueCallId(), getTimeAddedMs());
twyendde01c52017-09-22 10:07:31 -07001285 if (impressionType == DialerImpression.Type.LIGHTBRINGER_UPGRADE_REQUESTED) {
1286 if (getLogState().contactLookupResult == Type.NOT_FOUND) {
1287 Logger.get(mContext)
1288 .logCallImpression(
1289 DialerImpression.Type.LIGHTBRINGER_NON_CONTACT_UPGRADE_REQUESTED,
1290 getUniqueCallId(),
1291 getTimeAddedMs());
1292 }
1293 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001294 }
1295
1296 private void updateEnrichedCallSession() {
1297 if (getNumber() == null) {
1298 return;
1299 }
1300 if (getEnrichedCallSession() != null) {
1301 // State changes to existing sessions are currently handled by the UI components (which have
1302 // their own listeners). Someday instead we could remove those and just call update() here and
1303 // have the usual onDialerCallUpdate update the UI.
1304 dispatchOnEnrichedCallSessionUpdate();
1305 return;
1306 }
1307
1308 EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager();
1309
1310 Filter filter =
1311 isIncoming()
1312 ? manager.createIncomingCallComposerFilter()
1313 : manager.createOutgoingCallComposerFilter();
1314
1315 Session session = manager.getSession(getUniqueCallId(), getNumber(), filter);
1316 if (session == null) {
1317 return;
1318 }
1319
1320 session.setUniqueDialerCallId(getUniqueCallId());
1321 setEnrichedCallSession(session);
1322
1323 LogUtil.i(
1324 "DialerCall.updateEnrichedCallSession",
1325 "setting session %d's dialer id to %s",
1326 session.getSessionId(),
1327 getUniqueCallId());
1328
1329 dispatchOnEnrichedCallSessionUpdate();
1330 }
1331
1332 private void dispatchOnEnrichedCallSessionUpdate() {
1333 for (DialerCallListener listener : mListeners) {
1334 listener.onEnrichedCallSessionUpdate();
1335 }
1336 }
1337
1338 void onRemovedFromCallList() {
1339 // Ensure we clean up when this call is removed.
1340 mVideoTechManager.dispatchRemovedFromCallList();
1341 }
1342
wangqi4d705e52017-09-28 12:23:35 -07001343 public com.android.dialer.logging.VideoTech.Type getSelectedAvailableVideoTechType() {
1344 return selectedAvailableVideoTechType;
yueg457b3972017-09-18 15:11:47 -07001345 }
1346
Eric Erfanianccca3152017-02-22 16:32:36 -08001347 /**
1348 * Specifies whether a number is in the call history or not. {@link #CALL_HISTORY_STATUS_UNKNOWN}
1349 * means there is no result.
1350 */
1351 @IntDef({
1352 CALL_HISTORY_STATUS_UNKNOWN,
1353 CALL_HISTORY_STATUS_PRESENT,
1354 CALL_HISTORY_STATUS_NOT_PRESENT
1355 })
1356 @Retention(RetentionPolicy.SOURCE)
1357 public @interface CallHistoryStatus {}
1358
1359 /* Defines different states of this call */
1360 public static class State {
1361
1362 public static final int INVALID = 0;
1363 public static final int NEW = 1; /* The call is new. */
1364 public static final int IDLE = 2; /* The call is idle. Nothing active */
1365 public static final int ACTIVE = 3; /* There is an active call */
1366 public static final int INCOMING = 4; /* A normal incoming phone call */
1367 public static final int CALL_WAITING = 5; /* Incoming call while another is active */
1368 public static final int DIALING = 6; /* An outgoing call during dial phase */
1369 public static final int REDIALING = 7; /* Subsequent dialing attempt after a failure */
1370 public static final int ONHOLD = 8; /* An active phone call placed on hold */
1371 public static final int DISCONNECTING = 9; /* A call is being ended. */
1372 public static final int DISCONNECTED = 10; /* State after a call disconnects */
1373 public static final int CONFERENCED = 11; /* DialerCall part of a conference call */
1374 public static final int SELECT_PHONE_ACCOUNT = 12; /* Waiting for account selection */
1375 public static final int CONNECTING = 13; /* Waiting for Telecom broadcast to finish */
1376 public static final int BLOCKED = 14; /* The number was found on the block list */
1377 public static final int PULLING = 15; /* An external call being pulled to the device */
Eric Erfanian2ca43182017-08-31 06:57:16 -07001378 public static final int CALL_PENDING = 16; /* A call is pending on a long process to finish */
Eric Erfanianccca3152017-02-22 16:32:36 -08001379
1380 public static boolean isConnectingOrConnected(int state) {
1381 switch (state) {
1382 case ACTIVE:
1383 case INCOMING:
1384 case CALL_WAITING:
1385 case CONNECTING:
1386 case DIALING:
1387 case PULLING:
1388 case REDIALING:
1389 case ONHOLD:
1390 case CONFERENCED:
1391 return true;
1392 default:
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001393 return false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001394 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001395 }
1396
1397 public static boolean isDialing(int state) {
1398 return state == DIALING || state == PULLING || state == REDIALING;
1399 }
1400
1401 public static String toString(int state) {
1402 switch (state) {
1403 case INVALID:
1404 return "INVALID";
1405 case NEW:
1406 return "NEW";
1407 case IDLE:
1408 return "IDLE";
1409 case ACTIVE:
1410 return "ACTIVE";
1411 case INCOMING:
1412 return "INCOMING";
1413 case CALL_WAITING:
1414 return "CALL_WAITING";
1415 case DIALING:
1416 return "DIALING";
1417 case PULLING:
1418 return "PULLING";
1419 case REDIALING:
1420 return "REDIALING";
1421 case ONHOLD:
1422 return "ONHOLD";
1423 case DISCONNECTING:
1424 return "DISCONNECTING";
1425 case DISCONNECTED:
1426 return "DISCONNECTED";
1427 case CONFERENCED:
1428 return "CONFERENCED";
1429 case SELECT_PHONE_ACCOUNT:
1430 return "SELECT_PHONE_ACCOUNT";
1431 case CONNECTING:
1432 return "CONNECTING";
1433 case BLOCKED:
1434 return "BLOCKED";
1435 default:
1436 return "UNKNOWN";
1437 }
1438 }
1439 }
1440
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001441 /** Camera direction constants */
1442 public static class CameraDirection {
Eric Erfanianccca3152017-02-22 16:32:36 -08001443 public static final int CAMERA_DIRECTION_UNKNOWN = -1;
1444 public static final int CAMERA_DIRECTION_FRONT_FACING = CameraCharacteristics.LENS_FACING_FRONT;
1445 public static final int CAMERA_DIRECTION_BACK_FACING = CameraCharacteristics.LENS_FACING_BACK;
Eric Erfanianccca3152017-02-22 16:32:36 -08001446 }
1447
1448 /**
1449 * Tracks any state variables that is useful for logging. There is some amount of overlap with
1450 * existing call member variables, but this duplication helps to ensure that none of these logging
1451 * variables will interface with/and affect call logic.
1452 */
1453 public static class LogState {
1454
1455 public DisconnectCause disconnectCause;
1456 public boolean isIncoming = false;
Eric Erfanian8369df02017-05-03 10:27:13 -07001457 public ContactLookupResult.Type contactLookupResult =
1458 ContactLookupResult.Type.UNKNOWN_LOOKUP_RESULT_TYPE;
Eric Erfanianccca3152017-02-22 16:32:36 -08001459 public CallSpecificAppData callSpecificAppData;
1460 // If this was a conference call, the total number of calls involved in the conference.
1461 public int conferencedCalls = 0;
1462 public long duration = 0;
1463 public boolean isLogged = false;
1464
Eric Erfanian8369df02017-05-03 10:27:13 -07001465 private static String lookupToString(ContactLookupResult.Type lookupType) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001466 switch (lookupType) {
Eric Erfanian8369df02017-05-03 10:27:13 -07001467 case LOCAL_CONTACT:
Eric Erfanianccca3152017-02-22 16:32:36 -08001468 return "Local";
Eric Erfanian8369df02017-05-03 10:27:13 -07001469 case LOCAL_CACHE:
Eric Erfanianccca3152017-02-22 16:32:36 -08001470 return "Cache";
Eric Erfanian8369df02017-05-03 10:27:13 -07001471 case REMOTE:
Eric Erfanianccca3152017-02-22 16:32:36 -08001472 return "Remote";
Eric Erfanian8369df02017-05-03 10:27:13 -07001473 case EMERGENCY:
Eric Erfanianccca3152017-02-22 16:32:36 -08001474 return "Emergency";
Eric Erfanian8369df02017-05-03 10:27:13 -07001475 case VOICEMAIL:
Eric Erfanianccca3152017-02-22 16:32:36 -08001476 return "Voicemail";
1477 default:
1478 return "Not found";
1479 }
1480 }
1481
1482 private static String initiationToString(CallSpecificAppData callSpecificAppData) {
1483 if (callSpecificAppData == null) {
1484 return "null";
1485 }
Eric Erfanian8369df02017-05-03 10:27:13 -07001486 switch (callSpecificAppData.getCallInitiationType()) {
1487 case INCOMING_INITIATION:
Eric Erfanianccca3152017-02-22 16:32:36 -08001488 return "Incoming";
Eric Erfanian8369df02017-05-03 10:27:13 -07001489 case DIALPAD:
Eric Erfanianccca3152017-02-22 16:32:36 -08001490 return "Dialpad";
Eric Erfanian8369df02017-05-03 10:27:13 -07001491 case SPEED_DIAL:
Eric Erfanianccca3152017-02-22 16:32:36 -08001492 return "Speed Dial";
Eric Erfanian8369df02017-05-03 10:27:13 -07001493 case REMOTE_DIRECTORY:
Eric Erfanianccca3152017-02-22 16:32:36 -08001494 return "Remote Directory";
Eric Erfanian8369df02017-05-03 10:27:13 -07001495 case SMART_DIAL:
Eric Erfanianccca3152017-02-22 16:32:36 -08001496 return "Smart Dial";
Eric Erfanian8369df02017-05-03 10:27:13 -07001497 case REGULAR_SEARCH:
Eric Erfanianccca3152017-02-22 16:32:36 -08001498 return "Regular Search";
Eric Erfanian8369df02017-05-03 10:27:13 -07001499 case CALL_LOG:
Eric Erfanianccca3152017-02-22 16:32:36 -08001500 return "DialerCall Log";
Eric Erfanian8369df02017-05-03 10:27:13 -07001501 case CALL_LOG_FILTER:
Eric Erfanianccca3152017-02-22 16:32:36 -08001502 return "DialerCall Log Filter";
Eric Erfanian8369df02017-05-03 10:27:13 -07001503 case VOICEMAIL_LOG:
Eric Erfanianccca3152017-02-22 16:32:36 -08001504 return "Voicemail Log";
Eric Erfanian8369df02017-05-03 10:27:13 -07001505 case CALL_DETAILS:
Eric Erfanianccca3152017-02-22 16:32:36 -08001506 return "DialerCall Details";
Eric Erfanian8369df02017-05-03 10:27:13 -07001507 case QUICK_CONTACTS:
Eric Erfanianccca3152017-02-22 16:32:36 -08001508 return "Quick Contacts";
Eric Erfanian8369df02017-05-03 10:27:13 -07001509 case EXTERNAL_INITIATION:
Eric Erfanianccca3152017-02-22 16:32:36 -08001510 return "External";
Eric Erfanian8369df02017-05-03 10:27:13 -07001511 case LAUNCHER_SHORTCUT:
Eric Erfanianccca3152017-02-22 16:32:36 -08001512 return "Launcher Shortcut";
1513 default:
Eric Erfanian8369df02017-05-03 10:27:13 -07001514 return "Unknown: " + callSpecificAppData.getCallInitiationType();
Eric Erfanianccca3152017-02-22 16:32:36 -08001515 }
1516 }
1517
1518 @Override
1519 public String toString() {
1520 return String.format(
1521 Locale.US,
1522 "["
1523 + "%s, " // DisconnectCause toString already describes the object type
1524 + "isIncoming: %s, "
1525 + "contactLookup: %s, "
1526 + "callInitiation: %s, "
1527 + "duration: %s"
1528 + "]",
1529 disconnectCause,
1530 isIncoming,
1531 lookupToString(contactLookupResult),
1532 initiationToString(callSpecificAppData),
1533 duration);
1534 }
1535 }
1536
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001537 private static class VideoTechManager {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001538 private final Context context;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001539 private final EmptyVideoTech emptyVideoTech = new EmptyVideoTech();
Eric Erfanian90508232017-03-24 09:31:16 -07001540 private final List<VideoTech> videoTechs;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001541
1542 VideoTechManager(DialerCall call) {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001543 this.context = call.mContext;
1544
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001545 String phoneNumber = call.getNumber();
Eric Erfaniand8046e52017-04-06 09:41:50 -07001546 phoneNumber = phoneNumber != null ? phoneNumber : "";
Eric Erfanian2ca43182017-08-31 06:57:16 -07001547 phoneNumber = phoneNumber.replaceAll("[^+0-9]", "");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001548
1549 // Insert order here determines the priority of that video tech option
Eric Erfanian8369df02017-05-03 10:27:13 -07001550 videoTechs = new ArrayList<>();
yueg457b3972017-09-18 15:11:47 -07001551
wangqi4d705e52017-09-28 12:23:35 -07001552 videoTechs.add(new ImsVideoTech(Logger.get(call.mContext), call, call.mTelecomCall));
Eric Erfanian90508232017-03-24 09:31:16 -07001553
1554 VideoTech rcsVideoTech =
1555 EnrichedCallComponent.get(call.mContext)
1556 .getRcsVideoShareFactory()
1557 .newRcsVideoShare(
1558 EnrichedCallComponent.get(call.mContext).getEnrichedCallManager(),
1559 call,
Eric Erfaniand8046e52017-04-06 09:41:50 -07001560 phoneNumber);
Eric Erfanian90508232017-03-24 09:31:16 -07001561 if (rcsVideoTech != null) {
1562 videoTechs.add(rcsVideoTech);
1563 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001564
1565 videoTechs.add(
1566 new LightbringerTech(
Eric Erfanian2ca43182017-08-31 06:57:16 -07001567 LightbringerComponent.get(call.mContext).getLightbringer(),
1568 call,
1569 call.mTelecomCall,
1570 phoneNumber));
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001571 }
1572
wangqi4d705e52017-09-28 12:23:35 -07001573 VideoTech findBestAvailableVideoTech() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001574 for (VideoTech tech : videoTechs) {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001575 if (tech.isAvailable(context)) {
wangqi4d705e52017-09-28 12:23:35 -07001576 return tech;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001577 }
1578 }
1579
1580 return emptyVideoTech;
1581 }
1582
1583 void dispatchCallStateChanged(int newState) {
1584 for (VideoTech videoTech : videoTechs) {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001585 videoTech.onCallStateChanged(context, newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001586 }
1587 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001588
1589 void dispatchRemovedFromCallList() {
1590 for (VideoTech videoTech : videoTechs) {
1591 videoTech.onRemovedFromCallList();
1592 }
1593 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001594 }
1595
Eric Erfanianccca3152017-02-22 16:32:36 -08001596 /** Called when canned text responses have been loaded. */
1597 public interface CannedTextResponsesLoadedListener {
1598 void onCannedTextResponsesLoaded(DialerCall call);
1599 }
1600}