blob: 49f819babe7e457eb6ec1957bb34acddfd6b5aff [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;
18
19import static com.android.contacts.common.compat.CallCompat.Details.PROPERTY_ENTERPRISE_CALL;
20
21import android.Manifest;
Eric Erfanianccca3152017-02-22 16:32:36 -080022import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
27import android.graphics.drawable.Drawable;
28import android.hardware.display.DisplayManager;
29import android.os.BatteryManager;
30import android.os.Handler;
Eric Erfanian2ca43182017-08-31 06:57:16 -070031import android.os.Trace;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070032import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080033import android.support.annotation.Nullable;
34import android.support.v4.app.Fragment;
35import android.support.v4.content.ContextCompat;
36import android.telecom.Call.Details;
37import android.telecom.StatusHints;
38import android.telecom.TelecomManager;
39import android.text.TextUtils;
40import android.view.Display;
41import android.view.View;
42import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityManager;
44import com.android.contacts.common.ContactsUtils;
45import com.android.contacts.common.preference.ContactsPreferences;
46import com.android.contacts.common.util.ContactDisplayUtils;
47import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080048import com.android.dialer.common.LogUtil;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070049import com.android.dialer.compat.ActivityCompat;
Eric Erfanian2ca43182017-08-31 06:57:16 -070050import com.android.dialer.configprovider.ConfigProviderBindings;
Eric Erfanian8369df02017-05-03 10:27:13 -070051import com.android.dialer.logging.DialerImpression;
Eric Erfaniand8046e52017-04-06 09:41:50 -070052import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080053import com.android.dialer.multimedia.MultimediaData;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070054import com.android.dialer.oem.MotorolaUtils;
wangqi97539352017-09-25 11:15:16 -070055import com.android.dialer.phonenumberutil.PhoneNumberHelper;
Eric Erfanian2ca43182017-08-31 06:57:16 -070056import com.android.dialer.postcall.PostCall;
twyen4fde0ac2018-03-22 18:04:23 -070057import com.android.dialer.preferredsim.suggestion.SuggestionProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080058import com.android.incallui.ContactInfoCache.ContactCacheEntry;
59import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
60import com.android.incallui.InCallPresenter.InCallDetailsListener;
61import com.android.incallui.InCallPresenter.InCallEventListener;
62import com.android.incallui.InCallPresenter.InCallState;
63import com.android.incallui.InCallPresenter.InCallStateListener;
64import com.android.incallui.InCallPresenter.IncomingCallListener;
65import com.android.incallui.call.CallList;
66import com.android.incallui.call.DialerCall;
Eric Erfanian2ca43182017-08-31 06:57:16 -070067import com.android.incallui.call.DialerCall.State;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.incallui.call.DialerCallListener;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070069import com.android.incallui.calllocation.CallLocation;
70import com.android.incallui.calllocation.CallLocationComponent;
Eric Erfanianccca3152017-02-22 16:32:36 -080071import com.android.incallui.incall.protocol.ContactPhotoType;
72import com.android.incallui.incall.protocol.InCallScreen;
73import com.android.incallui.incall.protocol.InCallScreenDelegate;
74import com.android.incallui.incall.protocol.PrimaryCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -070075import com.android.incallui.incall.protocol.PrimaryCallState.ButtonState;
Eric Erfanianccca3152017-02-22 16:32:36 -080076import com.android.incallui.incall.protocol.PrimaryInfo;
77import com.android.incallui.incall.protocol.SecondaryInfo;
Eric Erfanian90508232017-03-24 09:31:16 -070078import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfanianccca3152017-02-22 16:32:36 -080079import java.lang.ref.WeakReference;
80
81/**
82 * Controller for the Call Card Fragment. This class listens for changes to InCallState and passes
83 * it along to the fragment.
84 */
85public class CallCardPresenter
86 implements InCallStateListener,
87 IncomingCallListener,
88 InCallDetailsListener,
89 InCallEventListener,
90 InCallScreenDelegate,
Eric Erfanian2ca43182017-08-31 06:57:16 -070091 DialerCallListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080092
93 /**
94 * Amount of time to wait before sending an announcement via the accessibility manager. When the
95 * call state changes to an outgoing or incoming state for the first time, the UI can often be
96 * changing due to call updates or contact lookup. This allows the UI to settle to a stable state
97 * to ensure that the correct information is announced.
98 */
99 private static final long ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS = 500;
100
101 /** Flag to allow the user's current location to be shown during emergency calls. */
102 private static final String CONFIG_ENABLE_EMERGENCY_LOCATION = "config_enable_emergency_location";
103
104 private static final boolean CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT = true;
105
106 /**
107 * Make it possible to not get location during an emergency call if the battery is too low, since
108 * doing so could trigger gps and thus potentially cause the phone to die in the middle of the
109 * call.
110 */
111 private static final String CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION =
112 "min_battery_percent_for_emergency_location";
113
114 private static final long CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT = 10;
115
linyuh183cb712017-12-27 17:02:37 -0800116 private final Context context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 private final Handler handler = new Handler();
118
linyuh183cb712017-12-27 17:02:37 -0800119 private DialerCall primary;
wangqidd5f1a52018-01-23 18:01:00 -0800120 private String primaryNumber;
linyuh183cb712017-12-27 17:02:37 -0800121 private DialerCall secondary;
wangqidd5f1a52018-01-23 18:01:00 -0800122 private String secondaryNumber;
linyuh183cb712017-12-27 17:02:37 -0800123 private ContactCacheEntry primaryContactInfo;
124 private ContactCacheEntry secondaryContactInfo;
125 @Nullable private ContactsPreferences contactsPreferences;
126 private boolean isFullscreen = false;
127 private InCallScreen inCallScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -0800128 private boolean isInCallScreenReady;
129 private boolean shouldSendAccessibilityEvent;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700130
131 @NonNull private final CallLocation callLocation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800132 private final Runnable sendAccessibilityEventRunnable =
133 new Runnable() {
134 @Override
135 public void run() {
linyuh183cb712017-12-27 17:02:37 -0800136 shouldSendAccessibilityEvent = !sendAccessibilityEvent(context, getUi());
Eric Erfanianccca3152017-02-22 16:32:36 -0800137 LogUtil.i(
138 "CallCardPresenter.sendAccessibilityEventRunnable",
139 "still should send: %b",
140 shouldSendAccessibilityEvent);
141 if (!shouldSendAccessibilityEvent) {
142 handler.removeCallbacks(this);
143 }
144 }
145 };
146
147 public CallCardPresenter(Context context) {
wangqi385a5a12017-09-28 10:44:54 -0700148 LogUtil.i("CallCardPresenter.constructor", null);
linyuh183cb712017-12-27 17:02:37 -0800149 this.context = Assert.isNotNull(context).getApplicationContext();
150 callLocation = CallLocationComponent.get(this.context).getCallLocation();
Eric Erfanianccca3152017-02-22 16:32:36 -0800151 }
152
153 private static boolean hasCallSubject(DialerCall call) {
154 return !TextUtils.isEmpty(call.getCallSubject());
155 }
156
157 @Override
158 public void onInCallScreenDelegateInit(InCallScreen inCallScreen) {
159 Assert.isNotNull(inCallScreen);
linyuh183cb712017-12-27 17:02:37 -0800160 this.inCallScreen = inCallScreen;
161 contactsPreferences = ContactsPreferencesFactory.newContactsPreferences(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800162
163 // Call may be null if disconnect happened already.
164 DialerCall call = CallList.getInstance().getFirstCall();
165 if (call != null) {
linyuh183cb712017-12-27 17:02:37 -0800166 primary = call;
167 if (shouldShowNoteSentToast(primary)) {
168 this.inCallScreen.showNoteSentToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800169 }
170 call.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800171 // start processing lookups right away.
172 if (!call.isConferenceCall()) {
173 startContactInfoSearch(call, true, call.getState() == DialerCall.State.INCOMING);
174 } else {
175 updateContactEntry(null, true);
176 }
177 }
178
179 onStateChange(null, InCallPresenter.getInstance().getInCallState(), CallList.getInstance());
180 }
181
182 @Override
183 public void onInCallScreenReady() {
wangqi385a5a12017-09-28 10:44:54 -0700184 LogUtil.i("CallCardPresenter.onInCallScreenReady", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800185 Assert.checkState(!isInCallScreenReady);
linyuh183cb712017-12-27 17:02:37 -0800186 if (contactsPreferences != null) {
187 contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
Eric Erfanianccca3152017-02-22 16:32:36 -0800188 }
189
Eric Erfanianccca3152017-02-22 16:32:36 -0800190 // Contact search may have completed before ui is ready.
linyuh183cb712017-12-27 17:02:37 -0800191 if (primaryContactInfo != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800192 updatePrimaryDisplayInfo();
193 }
194
195 // Register for call state changes last
196 InCallPresenter.getInstance().addListener(this);
197 InCallPresenter.getInstance().addIncomingCallListener(this);
198 InCallPresenter.getInstance().addDetailsListener(this);
199 InCallPresenter.getInstance().addInCallEventListener(this);
200 isInCallScreenReady = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700201
Eric Erfaniand8046e52017-04-06 09:41:50 -0700202 // Log location impressions
linyuh183cb712017-12-27 17:02:37 -0800203 if (isOutgoingEmergencyCall(primary)) {
204 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_NEW_EMERGENCY_CALL);
205 } else if (isIncomingEmergencyCall(primary) || isIncomingEmergencyCall(secondary)) {
206 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_CALLBACK);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700207 }
208
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700209 // Showing the location may have been skipped if the UI wasn't ready during previous layout.
210 if (shouldShowLocation()) {
linyuh183cb712017-12-27 17:02:37 -0800211 inCallScreen.showLocationUi(getLocationFragment());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700212
213 // Log location impressions
214 if (!hasLocationPermission()) {
linyuh183cb712017-12-27 17:02:37 -0800215 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_NO_LOCATION_PERMISSION);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700216 } else if (isBatteryTooLowForEmergencyLocation()) {
linyuh183cb712017-12-27 17:02:37 -0800217 Logger.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700218 .logImpression(DialerImpression.Type.EMERGENCY_BATTERY_TOO_LOW_TO_GET_LOCATION);
linyuh183cb712017-12-27 17:02:37 -0800219 } else if (!callLocation.canGetLocation(context)) {
220 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_CANT_GET_LOCATION);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700221 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700222 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800223 }
224
225 @Override
226 public void onInCallScreenUnready() {
wangqi385a5a12017-09-28 10:44:54 -0700227 LogUtil.i("CallCardPresenter.onInCallScreenUnready", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800228 Assert.checkState(isInCallScreenReady);
229
Eric Erfanianccca3152017-02-22 16:32:36 -0800230 // stop getting call state changes
231 InCallPresenter.getInstance().removeListener(this);
232 InCallPresenter.getInstance().removeIncomingCallListener(this);
233 InCallPresenter.getInstance().removeDetailsListener(this);
234 InCallPresenter.getInstance().removeInCallEventListener(this);
linyuh183cb712017-12-27 17:02:37 -0800235 if (primary != null) {
236 primary.removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800237 }
238
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700239 callLocation.close();
240
linyuh183cb712017-12-27 17:02:37 -0800241 primary = null;
242 primaryContactInfo = null;
243 secondaryContactInfo = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800244 isInCallScreenReady = false;
245 }
246
247 @Override
248 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
249 // same logic should happen as with onStateChange()
250 onStateChange(oldState, newState, CallList.getInstance());
251 }
252
253 @Override
254 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700255 Trace.beginSection("CallCardPresenter.onStateChange");
256 LogUtil.v("CallCardPresenter.onStateChange", "oldState: %s, newState: %s", oldState, newState);
linyuh183cb712017-12-27 17:02:37 -0800257 if (inCallScreen == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700258 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800259 return;
260 }
261
262 DialerCall primary = null;
263 DialerCall secondary = null;
264
265 if (newState == InCallState.INCOMING) {
266 primary = callList.getIncomingCall();
267 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
268 primary = callList.getOutgoingCall();
269 if (primary == null) {
270 primary = callList.getPendingOutgoingCall();
271 }
272
273 // getCallToDisplay doesn't go through outgoing or incoming calls. It will return the
274 // highest priority call to display as the secondary call.
275 secondary = getCallToDisplay(callList, null, true);
276 } else if (newState == InCallState.INCALL) {
277 primary = getCallToDisplay(callList, null, false);
278 secondary = getCallToDisplay(callList, primary, true);
279 }
280
281 LogUtil.v("CallCardPresenter.onStateChange", "primary call: " + primary);
282 LogUtil.v("CallCardPresenter.onStateChange", "secondary call: " + secondary);
wangqidd5f1a52018-01-23 18:01:00 -0800283 String primaryNumber = null;
284 String secondaryNumber = null;
285 if (primary != null) {
286 primaryNumber = primary.getNumber();
287 }
288 if (secondary != null) {
289 secondaryNumber = secondary.getNumber();
290 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800291
292 final boolean primaryChanged =
linyuh183cb712017-12-27 17:02:37 -0800293 !(DialerCall.areSame(this.primary, primary)
wangqidd5f1a52018-01-23 18:01:00 -0800294 && TextUtils.equals(this.primaryNumber, primaryNumber));
Eric Erfanianccca3152017-02-22 16:32:36 -0800295 final boolean secondaryChanged =
linyuh183cb712017-12-27 17:02:37 -0800296 !(DialerCall.areSame(this.secondary, secondary)
wangqidd5f1a52018-01-23 18:01:00 -0800297 && TextUtils.equals(this.secondaryNumber, secondaryNumber));
Eric Erfanianccca3152017-02-22 16:32:36 -0800298
linyuh183cb712017-12-27 17:02:37 -0800299 this.secondary = secondary;
wangqidd5f1a52018-01-23 18:01:00 -0800300 this.secondaryNumber = secondaryNumber;
linyuh183cb712017-12-27 17:02:37 -0800301 DialerCall previousPrimary = this.primary;
302 this.primary = primary;
wangqidd5f1a52018-01-23 18:01:00 -0800303 this.primaryNumber = primaryNumber;
Eric Erfanianccca3152017-02-22 16:32:36 -0800304
linyuh183cb712017-12-27 17:02:37 -0800305 if (this.primary != null) {
306 InCallPresenter.getInstance().onForegroundCallChanged(this.primary);
307 inCallScreen.updateInCallScreenColors();
Eric Erfanianccca3152017-02-22 16:32:36 -0800308 }
309
310 if (primaryChanged && shouldShowNoteSentToast(primary)) {
linyuh183cb712017-12-27 17:02:37 -0800311 inCallScreen.showNoteSentToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800312 }
313
314 // Refresh primary call information if either:
315 // 1. Primary call changed.
316 // 2. The call's ability to manage conference has changed.
317 if (shouldRefreshPrimaryInfo(primaryChanged)) {
318 // primary call has changed
319 if (previousPrimary != null) {
320 previousPrimary.removeListener(this);
321 }
linyuh183cb712017-12-27 17:02:37 -0800322 this.primary.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800323
linyuh183cb712017-12-27 17:02:37 -0800324 primaryContactInfo =
Eric Erfanianccca3152017-02-22 16:32:36 -0800325 ContactInfoCache.buildCacheEntryFromCall(
linyuh183cb712017-12-27 17:02:37 -0800326 context, this.primary, this.primary.getState() == DialerCall.State.INCOMING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800327 updatePrimaryDisplayInfo();
linyuh183cb712017-12-27 17:02:37 -0800328 maybeStartSearch(this.primary, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800329 }
330
linyuh183cb712017-12-27 17:02:37 -0800331 if (previousPrimary != null && this.primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800332 previousPrimary.removeListener(this);
333 }
334
wangqic8cf79e2017-10-17 09:21:00 -0700335 if (secondaryChanged) {
linyuh183cb712017-12-27 17:02:37 -0800336 if (this.secondary == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700337 // Secondary call may have ended. Update the ui.
linyuh183cb712017-12-27 17:02:37 -0800338 secondaryContactInfo = null;
wangqic8cf79e2017-10-17 09:21:00 -0700339 updateSecondaryDisplayInfo();
340 } else {
341 // secondary call has changed
linyuh183cb712017-12-27 17:02:37 -0800342 secondaryContactInfo =
wangqic8cf79e2017-10-17 09:21:00 -0700343 ContactInfoCache.buildCacheEntryFromCall(
linyuh183cb712017-12-27 17:02:37 -0800344 context, this.secondary, this.secondary.getState() == DialerCall.State.INCOMING);
wangqic8cf79e2017-10-17 09:21:00 -0700345 updateSecondaryDisplayInfo();
linyuh183cb712017-12-27 17:02:37 -0800346 maybeStartSearch(this.secondary, false);
wangqic8cf79e2017-10-17 09:21:00 -0700347 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800348 }
349
350 // Set the call state
351 int callState = DialerCall.State.IDLE;
linyuh183cb712017-12-27 17:02:37 -0800352 if (this.primary != null) {
353 callState = this.primary.getState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800354 updatePrimaryCallState();
355 } else {
Android Dialer31fe9982018-02-26 13:29:09 -0800356 getUi().setCallState(PrimaryCallState.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800357 }
358
359 maybeShowManageConferenceCallButton();
360
361 // Hide the end call button instantly if we're receiving an incoming call.
362 getUi()
363 .setEndCallButtonEnabled(
linyuh183cb712017-12-27 17:02:37 -0800364 shouldShowEndCallButton(this.primary, callState),
Eric Erfanianccca3152017-02-22 16:32:36 -0800365 callState != DialerCall.State.INCOMING /* animate */);
366
367 maybeSendAccessibilityEvent(oldState, newState, primaryChanged);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700368 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800369 }
370
371 @Override
372 public void onDetailsChanged(DialerCall call, Details details) {
373 updatePrimaryCallState();
374
375 if (call.can(Details.CAPABILITY_MANAGE_CONFERENCE)
376 != details.can(Details.CAPABILITY_MANAGE_CONFERENCE)) {
377 maybeShowManageConferenceCallButton();
378 }
379 }
380
381 @Override
382 public void onDialerCallDisconnect() {}
383
384 @Override
385 public void onDialerCallUpdate() {
386 // No-op; specific call updates handled elsewhere.
387 }
388
389 @Override
390 public void onWiFiToLteHandover() {}
391
392 @Override
393 public void onHandoverToWifiFailure() {}
394
Eric Erfanianc857f902017-05-15 14:05:33 -0700395 @Override
396 public void onInternationalCallOnWifi() {}
397
Eric Erfanian2ca43182017-08-31 06:57:16 -0700398 @Override
399 public void onEnrichedCallSessionUpdate() {
400 LogUtil.enterBlock("CallCardPresenter.onEnrichedCallSessionUpdate");
401 updatePrimaryDisplayInfo();
402 }
403
Eric Erfanianccca3152017-02-22 16:32:36 -0800404 /** Handles a change to the child number by refreshing the primary call info. */
405 @Override
406 public void onDialerCallChildNumberChange() {
407 LogUtil.v("CallCardPresenter.onDialerCallChildNumberChange", "");
408
linyuh183cb712017-12-27 17:02:37 -0800409 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800410 return;
411 }
412 updatePrimaryDisplayInfo();
413 }
414
415 /** Handles a change to the last forwarding number by refreshing the primary call info. */
416 @Override
417 public void onDialerCallLastForwardedNumberChange() {
418 LogUtil.v("CallCardPresenter.onDialerCallLastForwardedNumberChange", "");
419
linyuh183cb712017-12-27 17:02:37 -0800420 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800421 return;
422 }
423 updatePrimaryDisplayInfo();
424 updatePrimaryCallState();
425 }
426
427 @Override
428 public void onDialerCallUpgradeToVideo() {}
429
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700430 /** Handles a change to the session modification state for a call. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800431 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700432 public void onDialerCallSessionModificationStateChange() {
433 LogUtil.enterBlock("CallCardPresenter.onDialerCallSessionModificationStateChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800434
linyuh183cb712017-12-27 17:02:37 -0800435 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800436 return;
437 }
438 getUi()
439 .setEndCallButtonEnabled(
linyuh183cb712017-12-27 17:02:37 -0800440 primary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700441 != SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800442 true /* shouldAnimate */);
443 updatePrimaryCallState();
444 }
445
Eric Erfanianccca3152017-02-22 16:32:36 -0800446 private boolean shouldRefreshPrimaryInfo(boolean primaryChanged) {
linyuh183cb712017-12-27 17:02:37 -0800447 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800448 return false;
449 }
450 return primaryChanged
linyuh183cb712017-12-27 17:02:37 -0800451 || inCallScreen.isManageConferenceVisible() != shouldShowManageConference();
Eric Erfanianccca3152017-02-22 16:32:36 -0800452 }
453
454 private void updatePrimaryCallState() {
linyuh183cb712017-12-27 17:02:37 -0800455 if (getUi() != null && primary != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800456 boolean isWorkCall =
linyuh183cb712017-12-27 17:02:37 -0800457 primary.hasProperty(PROPERTY_ENTERPRISE_CALL)
458 || (primaryContactInfo != null
459 && primaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
Eric Erfanianccca3152017-02-22 16:32:36 -0800460 boolean isHdAudioCall =
linyuh183cb712017-12-27 17:02:37 -0800461 isPrimaryCallActive() && primary.hasProperty(Details.PROPERTY_HIGH_DEF_AUDIO);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700462 boolean isAttemptingHdAudioCall =
463 !isHdAudioCall
linyuh183cb712017-12-27 17:02:37 -0800464 && !primary.hasProperty(DialerCall.PROPERTY_CODEC_KNOWN)
465 && MotorolaUtils.shouldBlinkHdIconWhenConnectingCall(context);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700466
linyuh183cb712017-12-27 17:02:37 -0800467 boolean isBusiness = primaryContactInfo != null && primaryContactInfo.isBusiness;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700468
Eric Erfanianccca3152017-02-22 16:32:36 -0800469 // Check for video state change and update the visibility of the contact photo. The contact
470 // photo is hidden when the incoming video surface is shown.
471 // The contact photo visibility can also change in setPrimary().
472 boolean shouldShowContactPhoto =
linyuh183cb712017-12-27 17:02:37 -0800473 !VideoCallPresenter.showIncomingVideo(primary.getVideoState(), primary.getState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800474 getUi()
475 .setCallState(
Android Dialer31fe9982018-02-26 13:29:09 -0800476 PrimaryCallState.builder()
477 .setState(primary.getState())
478 .setIsVideoCall(primary.isVideoCall())
479 .setSessionModificationState(primary.getVideoTech().getSessionModificationState())
480 .setDisconnectCause(primary.getDisconnectCause())
481 .setConnectionLabel(getConnectionLabel())
twyen4fde0ac2018-03-22 18:04:23 -0700482 .setSimSuggestionReason(getSimSuggestionReason())
Android Dialer31fe9982018-02-26 13:29:09 -0800483 .setConnectionIcon(getCallStateIcon())
484 .setGatewayNumber(getGatewayNumber())
485 .setCallSubject(shouldShowCallSubject(primary) ? primary.getCallSubject() : null)
486 .setCallbackNumber(
487 PhoneNumberHelper.formatNumber(
linyuhb06d0092018-03-01 15:05:36 -0800488 context, primary.getCallbackNumber(), primary.getSimCountryIso()))
Android Dialer31fe9982018-02-26 13:29:09 -0800489 .setIsWifi(primary.hasProperty(Details.PROPERTY_WIFI))
490 .setIsConference(
491 primary.isConferenceCall()
492 && !primary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE))
493 .setIsWorkCall(isWorkCall)
494 .setIsHdAttempting(isAttemptingHdAudioCall)
495 .setIsHdAudioCall(isHdAudioCall)
496 .setIsForwardedNumber(
497 !TextUtils.isEmpty(primary.getLastForwardedNumber())
498 || primary.isCallForwarded())
499 .setShouldShowContactPhoto(shouldShowContactPhoto)
500 .setConnectTimeMillis(primary.getConnectTimeMillis())
501 .setIsVoiceMailNumber(primary.isVoiceMailNumber())
502 .setIsRemotelyHeld(primary.isRemotelyHeld())
503 .setIsBusinessNumber(isBusiness)
504 .setSupportsCallOnHold(supports2ndCallOnHold())
505 .setSwapToSecondaryButtonState(getSwapToSecondaryButtonState())
506 .setIsAssistedDialed(primary.isAssistedDialed())
507 .setCustomLabel(null)
508 .setAssistedDialingExtras(primary.getAssistedDialingExtras())
509 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800510
511 InCallActivity activity =
linyuh183cb712017-12-27 17:02:37 -0800512 (InCallActivity) (inCallScreen.getInCallScreenFragment().getActivity());
Eric Erfanianccca3152017-02-22 16:32:36 -0800513 if (activity != null) {
514 activity.onPrimaryCallStateChanged();
515 }
516 }
517 }
518
Eric Erfanian2ca43182017-08-31 06:57:16 -0700519 private @ButtonState int getSwapToSecondaryButtonState() {
linyuh183cb712017-12-27 17:02:37 -0800520 if (secondary == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700521 return ButtonState.NOT_SUPPORT;
522 }
linyuh183cb712017-12-27 17:02:37 -0800523 if (primary.getState() == State.ACTIVE) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700524 return ButtonState.ENABLED;
525 }
526 return ButtonState.DISABLED;
527 }
528
Eric Erfanianccca3152017-02-22 16:32:36 -0800529 /** Only show the conference call button if we can manage the conference. */
530 private void maybeShowManageConferenceCallButton() {
531 getUi().showManageConferenceCallButton(shouldShowManageConference());
532 }
533
534 /**
535 * Determines if the manage conference button should be visible, based on the current primary
536 * call.
537 *
538 * @return {@code True} if the manage conference button should be visible.
539 */
540 private boolean shouldShowManageConference() {
linyuh183cb712017-12-27 17:02:37 -0800541 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800542 return false;
543 }
544
linyuh183cb712017-12-27 17:02:37 -0800545 return primary.can(android.telecom.Call.Details.CAPABILITY_MANAGE_CONFERENCE) && !isFullscreen;
Eric Erfanianccca3152017-02-22 16:32:36 -0800546 }
547
Eric Erfanian2ca43182017-08-31 06:57:16 -0700548 private boolean supports2ndCallOnHold() {
549 DialerCall firstCall = CallList.getInstance().getActiveOrBackgroundCall();
550 DialerCall incomingCall = CallList.getInstance().getIncomingCall();
551 if (firstCall != null && incomingCall != null && firstCall != incomingCall) {
552 return incomingCall.can(Details.CAPABILITY_HOLD);
553 }
554 return true;
555 }
556
Eric Erfanianccca3152017-02-22 16:32:36 -0800557 @Override
558 public void onCallStateButtonClicked() {
linyuh183cb712017-12-27 17:02:37 -0800559 Intent broadcastIntent = Bindings.get(context).getCallStateButtonBroadcastIntent(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800560 if (broadcastIntent != null) {
561 LogUtil.v(
562 "CallCardPresenter.onCallStateButtonClicked",
563 "sending call state button broadcast: " + broadcastIntent);
linyuh183cb712017-12-27 17:02:37 -0800564 context.sendBroadcast(broadcastIntent, Manifest.permission.READ_PHONE_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800565 }
566 }
567
568 @Override
569 public void onManageConferenceClicked() {
570 InCallActivity activity =
linyuh183cb712017-12-27 17:02:37 -0800571 (InCallActivity) (inCallScreen.getInCallScreenFragment().getActivity());
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 activity.showConferenceFragment(true);
573 }
574
575 @Override
576 public void onShrinkAnimationComplete() {
577 InCallPresenter.getInstance().onShrinkAnimationComplete();
578 }
579
Eric Erfanianccca3152017-02-22 16:32:36 -0800580 private void maybeStartSearch(DialerCall call, boolean isPrimary) {
581 // no need to start search for conference calls which show generic info.
582 if (call != null && !call.isConferenceCall()) {
583 startContactInfoSearch(call, isPrimary, call.getState() == DialerCall.State.INCOMING);
584 }
585 }
586
Eric Erfanianccca3152017-02-22 16:32:36 -0800587 /** Starts a query for more contact data for the save primary and secondary calls. */
588 private void startContactInfoSearch(
589 final DialerCall call, final boolean isPrimary, boolean isIncoming) {
linyuh183cb712017-12-27 17:02:37 -0800590 final ContactInfoCache cache = ContactInfoCache.getInstance(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800591
592 cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary));
593 }
594
595 private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) {
596 final boolean entryMatchesExistingCall =
linyuh183cb712017-12-27 17:02:37 -0800597 (isPrimary && primary != null && TextUtils.equals(callId, primary.getId()))
598 || (!isPrimary && secondary != null && TextUtils.equals(callId, secondary.getId()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 if (entryMatchesExistingCall) {
600 updateContactEntry(entry, isPrimary);
601 } else {
602 LogUtil.e(
603 "CallCardPresenter.onContactInfoComplete",
604 "dropping stale contact lookup info for " + callId);
605 }
606
607 final DialerCall call = CallList.getInstance().getCallById(callId);
608 if (call != null) {
609 call.getLogState().contactLookupResult = entry.contactLookupResult;
610 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700611 if (entry.lookupUri != null) {
linyuh183cb712017-12-27 17:02:37 -0800612 CallerInfoUtils.sendViewNotification(context, entry.lookupUri);
Eric Erfanianccca3152017-02-22 16:32:36 -0800613 }
614 }
615
616 private void onImageLoadComplete(String callId, ContactCacheEntry entry) {
617 if (getUi() == null) {
618 return;
619 }
620
621 if (entry.photo != null) {
linyuh183cb712017-12-27 17:02:37 -0800622 if (primary != null && callId.equals(primary.getId())) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 updateContactEntry(entry, true /* isPrimary */);
linyuh183cb712017-12-27 17:02:37 -0800624 } else if (secondary != null && callId.equals(secondary.getId())) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800625 updateContactEntry(entry, false /* isPrimary */);
626 }
627 }
628 }
629
630 private void updateContactEntry(ContactCacheEntry entry, boolean isPrimary) {
631 if (isPrimary) {
linyuh183cb712017-12-27 17:02:37 -0800632 primaryContactInfo = entry;
Eric Erfanianccca3152017-02-22 16:32:36 -0800633 updatePrimaryDisplayInfo();
634 } else {
linyuh183cb712017-12-27 17:02:37 -0800635 secondaryContactInfo = entry;
Eric Erfanianccca3152017-02-22 16:32:36 -0800636 updateSecondaryDisplayInfo();
637 }
638 }
639
640 /**
641 * Get the highest priority call to display. Goes through the calls and chooses which to return
642 * based on priority of which type of call to display to the user. Callers can use the "ignore"
643 * feature to get the second best call by passing a previously found primary call as ignore.
644 *
645 * @param ignore A call to ignore if found.
646 */
647 private DialerCall getCallToDisplay(
648 CallList callList, DialerCall ignore, boolean skipDisconnected) {
649 // Active calls come second. An active call always gets precedent.
650 DialerCall retval = callList.getActiveCall();
651 if (retval != null && retval != ignore) {
652 return retval;
653 }
654
655 // Sometimes there is intemediate state that two calls are in active even one is about
656 // to be on hold.
657 retval = callList.getSecondActiveCall();
658 if (retval != null && retval != ignore) {
659 return retval;
660 }
661
662 // Disconnected calls get primary position if there are no active calls
663 // to let user know quickly what call has disconnected. Disconnected
664 // calls are very short lived.
665 if (!skipDisconnected) {
666 retval = callList.getDisconnectingCall();
667 if (retval != null && retval != ignore) {
668 return retval;
669 }
670 retval = callList.getDisconnectedCall();
671 if (retval != null && retval != ignore) {
672 return retval;
673 }
674 }
675
676 // Then we go to background call (calls on hold)
677 retval = callList.getBackgroundCall();
678 if (retval != null && retval != ignore) {
679 return retval;
680 }
681
682 // Lastly, we go to a second background call.
683 retval = callList.getSecondBackgroundCall();
684
685 return retval;
686 }
687
688 private void updatePrimaryDisplayInfo() {
linyuh183cb712017-12-27 17:02:37 -0800689 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800690 // TODO: May also occur if search result comes back after ui is destroyed. Look into
691 // removing that case completely.
692 LogUtil.v(
693 "CallCardPresenter.updatePrimaryDisplayInfo",
694 "updatePrimaryDisplayInfo called but ui is null!");
695 return;
696 }
697
linyuh183cb712017-12-27 17:02:37 -0800698 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800699 // Clear the primary display info.
Android Dialerf9439102018-02-21 08:05:14 -0800700 inCallScreen.setPrimary(PrimaryInfo.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800701 return;
702 }
703
704 // Hide the contact photo if we are in a video call and the incoming video surface is
705 // showing.
706 boolean showContactPhoto =
linyuh183cb712017-12-27 17:02:37 -0800707 !VideoCallPresenter.showIncomingVideo(primary.getVideoState(), primary.getState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800708
709 // DialerCall placed through a work phone account.
linyuh183cb712017-12-27 17:02:37 -0800710 boolean hasWorkCallProperty = primary.hasProperty(PROPERTY_ENTERPRISE_CALL);
Eric Erfanianccca3152017-02-22 16:32:36 -0800711
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700712 MultimediaData multimediaData = null;
linyuh183cb712017-12-27 17:02:37 -0800713 if (primary.getEnrichedCallSession() != null) {
714 multimediaData = primary.getEnrichedCallSession().getMultimediaData();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700715 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800716
linyuh183cb712017-12-27 17:02:37 -0800717 if (primary.isConferenceCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800718 LogUtil.v(
719 "CallCardPresenter.updatePrimaryDisplayInfo",
720 "update primary display info for conference call.");
721
linyuh183cb712017-12-27 17:02:37 -0800722 inCallScreen.setPrimary(
Android Dialerf9439102018-02-21 08:05:14 -0800723 PrimaryInfo.builder()
724 .setName(
725 CallerInfoUtils.getConferenceString(
726 context, primary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)))
727 .setNameIsNumber(false)
728 .setPhotoType(ContactPhotoType.DEFAULT_PLACEHOLDER)
729 .setIsSipCall(false)
730 .setIsContactPhotoShown(showContactPhoto)
731 .setIsWorkCall(hasWorkCallProperty)
732 .setIsSpam(false)
733 .setIsLocalContact(false)
734 .setAnsweringDisconnectsOngoingCall(false)
735 .setShouldShowLocation(shouldShowLocation())
736 .setShowInCallButtonGrid(true)
737 .setNumberPresentation(primary.getNumberPresentation())
738 .build());
linyuh183cb712017-12-27 17:02:37 -0800739 } else if (primaryContactInfo != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800740 LogUtil.v(
741 "CallCardPresenter.updatePrimaryDisplayInfo",
linyuh183cb712017-12-27 17:02:37 -0800742 "update primary display info for " + primaryContactInfo);
Eric Erfanianccca3152017-02-22 16:32:36 -0800743
linyuh183cb712017-12-27 17:02:37 -0800744 String name = getNameForCall(primaryContactInfo);
Eric Erfanianccca3152017-02-22 16:32:36 -0800745 String number;
746
linyuh183cb712017-12-27 17:02:37 -0800747 boolean isChildNumberShown = !TextUtils.isEmpty(primary.getChildNumber());
748 boolean isForwardedNumberShown = !TextUtils.isEmpty(primary.getLastForwardedNumber());
749 boolean isCallSubjectShown = shouldShowCallSubject(primary);
Eric Erfanianccca3152017-02-22 16:32:36 -0800750
751 if (isCallSubjectShown) {
752 number = null;
753 } else if (isChildNumberShown) {
linyuh183cb712017-12-27 17:02:37 -0800754 number = context.getString(R.string.child_number, primary.getChildNumber());
Eric Erfanianccca3152017-02-22 16:32:36 -0800755 } else if (isForwardedNumberShown) {
756 // Use last forwarded number instead of second line, if present.
linyuh183cb712017-12-27 17:02:37 -0800757 number = primary.getLastForwardedNumber();
Eric Erfanianccca3152017-02-22 16:32:36 -0800758 } else {
linyuh183cb712017-12-27 17:02:37 -0800759 number = primaryContactInfo.number;
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 }
761
linyuh183cb712017-12-27 17:02:37 -0800762 boolean nameIsNumber = name != null && name.equals(primaryContactInfo.number);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700763
Eric Erfanianccca3152017-02-22 16:32:36 -0800764 // DialerCall with caller that is a work contact.
linyuh183cb712017-12-27 17:02:37 -0800765 boolean isWorkContact = (primaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
766 inCallScreen.setPrimary(
Android Dialerf9439102018-02-21 08:05:14 -0800767 PrimaryInfo.builder()
768 .setNumber(number)
769 .setName(primary.updateNameIfRestricted(name))
770 .setNameIsNumber(nameIsNumber)
771 .setLabel(
772 shouldShowLocationAsLabel(nameIsNumber, primaryContactInfo.shouldShowLocation)
773 ? primaryContactInfo.location
774 : null)
775 .setLocation(
776 isChildNumberShown || isCallSubjectShown ? null : primaryContactInfo.label)
777 .setPhoto(primaryContactInfo.photo)
778 .setPhotoType(primaryContactInfo.photoType)
779 .setIsSipCall(primaryContactInfo.isSipCall)
780 .setIsContactPhotoShown(showContactPhoto)
781 .setIsWorkCall(hasWorkCallProperty || isWorkContact)
782 .setIsSpam(primary.isSpam())
783 .setIsLocalContact(primaryContactInfo.isLocalContact())
784 .setAnsweringDisconnectsOngoingCall(primary.answeringDisconnectsForegroundVideoCall())
785 .setShouldShowLocation(shouldShowLocation())
786 .setContactInfoLookupKey(primaryContactInfo.lookupKey)
787 .setMultimediaData(multimediaData)
788 .setShowInCallButtonGrid(true)
789 .setNumberPresentation(primary.getNumberPresentation())
790 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800791 } else {
792 // Clear the primary display info.
Android Dialerf9439102018-02-21 08:05:14 -0800793 inCallScreen.setPrimary(PrimaryInfo.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800794 }
795
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700796 if (isInCallScreenReady) {
linyuh183cb712017-12-27 17:02:37 -0800797 inCallScreen.showLocationUi(getLocationFragment());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700798 } else {
799 LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location");
800 }
801 }
802
Eric Erfaniand8046e52017-04-06 09:41:50 -0700803 private static boolean shouldShowLocationAsLabel(
804 boolean nameIsNumber, boolean shouldShowLocation) {
805 if (nameIsNumber) {
806 return true;
807 }
808 if (shouldShowLocation) {
809 return true;
810 }
811 return false;
812 }
813
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 private Fragment getLocationFragment() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700815 if (!shouldShowLocation()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700816 return null;
817 }
818 LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment");
linyuh183cb712017-12-27 17:02:37 -0800819 return callLocation.getLocationFragment(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800820 }
821
822 private boolean shouldShowLocation() {
linyuh183cb712017-12-27 17:02:37 -0800823 if (!ConfigProviderBindings.get(context)
wangqi1d62ab22017-11-29 14:29:31 -0800824 .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) {
825 LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config.");
826 return false;
827 }
828 if (!isPotentialEmergencyCall()) {
829 LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location");
830 return false;
831 }
832 if (!hasLocationPermission()) {
833 LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission.");
834 return false;
835 }
836 if (isBatteryTooLowForEmergencyLocation()) {
837 LogUtil.i("CallCardPresenter.getLocationFragment", "low battery.");
838 return false;
839 }
linyuh183cb712017-12-27 17:02:37 -0800840 if (ActivityCompat.isInMultiWindowMode(inCallScreen.getInCallScreenFragment().getActivity())) {
wangqi1d62ab22017-11-29 14:29:31 -0800841 LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode");
842 return false;
843 }
linyuh183cb712017-12-27 17:02:37 -0800844 if (primary.isVideoCall()) {
wangqi1d62ab22017-11-29 14:29:31 -0800845 LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported");
846 return false;
847 }
linyuh183cb712017-12-27 17:02:37 -0800848 if (!callLocation.canGetLocation(context)) {
wangqi1d62ab22017-11-29 14:29:31 -0800849 LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location");
850 return false;
851 }
852 return true;
853 }
854
855 private boolean isPotentialEmergencyCall() {
linyuh183cb712017-12-27 17:02:37 -0800856 if (isOutgoingEmergencyCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 LogUtil.i("CallCardPresenter.shouldShowLocation", "new emergency call");
858 return true;
linyuh183cb712017-12-27 17:02:37 -0800859 } else if (isIncomingEmergencyCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 LogUtil.i("CallCardPresenter.shouldShowLocation", "potential emergency callback");
861 return true;
linyuh183cb712017-12-27 17:02:37 -0800862 } else if (isIncomingEmergencyCall(secondary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800863 LogUtil.i("CallCardPresenter.shouldShowLocation", "has potential emergency callback");
864 return true;
865 }
866 return false;
867 }
868
869 private static boolean isOutgoingEmergencyCall(@Nullable DialerCall call) {
870 return call != null && !call.isIncoming() && call.isEmergencyCall();
871 }
872
873 private static boolean isIncomingEmergencyCall(@Nullable DialerCall call) {
874 return call != null && call.isIncoming() && call.isPotentialEmergencyCallback();
875 }
876
877 private boolean hasLocationPermission() {
linyuh183cb712017-12-27 17:02:37 -0800878 return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
Eric Erfanianccca3152017-02-22 16:32:36 -0800879 == PackageManager.PERMISSION_GRANTED;
880 }
881
882 private boolean isBatteryTooLowForEmergencyLocation() {
883 Intent batteryStatus =
linyuh183cb712017-12-27 17:02:37 -0800884 context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Eric Erfanianccca3152017-02-22 16:32:36 -0800885 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
886 if (status == BatteryManager.BATTERY_STATUS_CHARGING
887 || status == BatteryManager.BATTERY_STATUS_FULL) {
888 // Plugged in or full battery
889 return false;
890 }
891 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
892 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
893 float batteryPercent = (100f * level) / scale;
894 long threshold =
linyuh183cb712017-12-27 17:02:37 -0800895 ConfigProviderBindings.get(context)
Eric Erfanianccca3152017-02-22 16:32:36 -0800896 .getLong(
897 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION,
898 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT);
899 LogUtil.i(
900 "CallCardPresenter.isBatteryTooLowForEmergencyLocation",
901 "percent charged: " + batteryPercent + ", min required charge: " + threshold);
902 return batteryPercent < threshold;
903 }
904
905 private void updateSecondaryDisplayInfo() {
linyuh183cb712017-12-27 17:02:37 -0800906 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800907 return;
908 }
909
linyuh183cb712017-12-27 17:02:37 -0800910 if (secondary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800911 // Clear the secondary display info.
Android Dialer9e335e22018-03-01 08:27:39 -0800912 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800913 return;
914 }
915
linyuh183cb712017-12-27 17:02:37 -0800916 if (secondary.isMergeInProcess()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700917 LogUtil.i(
918 "CallCardPresenter.updateSecondaryDisplayInfo",
919 "secondary call is merge in process, clearing info");
Android Dialer9e335e22018-03-01 08:27:39 -0800920 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700921 return;
922 }
923
linyuh183cb712017-12-27 17:02:37 -0800924 if (secondary.isConferenceCall()) {
925 inCallScreen.setSecondary(
Android Dialer9e335e22018-03-01 08:27:39 -0800926 SecondaryInfo.builder()
927 .setShouldShow(true)
928 .setName(
929 CallerInfoUtils.getConferenceString(
930 context, secondary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)))
931 .setProviderLabel(secondary.getCallProviderLabel())
932 .setIsConference(true)
933 .setIsVideoCall(secondary.isVideoCall())
934 .setIsFullscreen(isFullscreen)
935 .build());
linyuh183cb712017-12-27 17:02:37 -0800936 } else if (secondaryContactInfo != null) {
937 LogUtil.v("CallCardPresenter.updateSecondaryDisplayInfo", "" + secondaryContactInfo);
938 String name = getNameForCall(secondaryContactInfo);
939 boolean nameIsNumber = name != null && name.equals(secondaryContactInfo.number);
940 inCallScreen.setSecondary(
Android Dialer9e335e22018-03-01 08:27:39 -0800941 SecondaryInfo.builder()
942 .setShouldShow(true)
943 .setName(secondary.updateNameIfRestricted(name))
944 .setNameIsNumber(nameIsNumber)
945 .setLabel(secondaryContactInfo.label)
946 .setProviderLabel(secondary.getCallProviderLabel())
947 .setIsVideoCall(secondary.isVideoCall())
948 .setIsFullscreen(isFullscreen)
949 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800950 } else {
951 // Clear the secondary display info.
Android Dialer9e335e22018-03-01 08:27:39 -0800952 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800953 }
954 }
955
956 /** Returns the gateway number for any existing outgoing call. */
957 private String getGatewayNumber() {
958 if (hasOutgoingGatewayCall()) {
linyuh183cb712017-12-27 17:02:37 -0800959 return DialerCall.getNumberFromHandle(primary.getGatewayInfo().getGatewayAddress());
Eric Erfanianccca3152017-02-22 16:32:36 -0800960 }
961 return null;
962 }
963
964 /**
965 * Returns the label (line of text above the number/name) for any given call. For example,
966 * "calling via [Account/Google Voice]" for outgoing calls.
967 */
968 private String getConnectionLabel() {
linyuh183cb712017-12-27 17:02:37 -0800969 if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800970 != PackageManager.PERMISSION_GRANTED) {
971 return null;
972 }
linyuh183cb712017-12-27 17:02:37 -0800973 StatusHints statusHints = primary.getStatusHints();
Eric Erfanianccca3152017-02-22 16:32:36 -0800974 if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
975 return statusHints.getLabel().toString();
976 }
977
978 if (hasOutgoingGatewayCall() && getUi() != null) {
979 // Return the label for the gateway app on outgoing calls.
linyuh183cb712017-12-27 17:02:37 -0800980 final PackageManager pm = context.getPackageManager();
Eric Erfanianccca3152017-02-22 16:32:36 -0800981 try {
982 ApplicationInfo info =
linyuh183cb712017-12-27 17:02:37 -0800983 pm.getApplicationInfo(primary.getGatewayInfo().getGatewayProviderPackageName(), 0);
Eric Erfanianccca3152017-02-22 16:32:36 -0800984 return pm.getApplicationLabel(info).toString();
985 } catch (PackageManager.NameNotFoundException e) {
986 LogUtil.e("CallCardPresenter.getConnectionLabel", "gateway Application Not Found.", e);
987 return null;
988 }
989 }
linyuh183cb712017-12-27 17:02:37 -0800990 return primary.getCallProviderLabel();
Eric Erfanianccca3152017-02-22 16:32:36 -0800991 }
992
twyen4fde0ac2018-03-22 18:04:23 -0700993 @Nullable
994 private SuggestionProvider.Reason getSimSuggestionReason() {
995 String value =
996 primary.getIntentExtras().getString(SuggestionProvider.EXTRA_SIM_SUGGESTION_REASON);
997 if (value == null) {
998 return null;
999 }
1000 try {
1001 return SuggestionProvider.Reason.valueOf(value);
1002 } catch (IllegalArgumentException e) {
1003 LogUtil.e("CallCardPresenter.getConnectionLabel", "unknown reason " + value);
1004 return null;
1005 }
1006 }
1007
Eric Erfanianccca3152017-02-22 16:32:36 -08001008 private Drawable getCallStateIcon() {
1009 // Return connection icon if one exists.
linyuh183cb712017-12-27 17:02:37 -08001010 StatusHints statusHints = primary.getStatusHints();
Eric Erfanianccca3152017-02-22 16:32:36 -08001011 if (statusHints != null && statusHints.getIcon() != null) {
linyuh183cb712017-12-27 17:02:37 -08001012 Drawable icon = statusHints.getIcon().loadDrawable(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001013 if (icon != null) {
1014 return icon;
1015 }
1016 }
1017
1018 return null;
1019 }
1020
1021 private boolean hasOutgoingGatewayCall() {
1022 // We only display the gateway information while STATE_DIALING so return false for any other
1023 // call state.
1024 // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
1025 // is also called after a contact search completes (call is not present yet). Split the
1026 // UI update so it can receive independent updates.
linyuh183cb712017-12-27 17:02:37 -08001027 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001028 return false;
1029 }
linyuh183cb712017-12-27 17:02:37 -08001030 return DialerCall.State.isDialing(primary.getState())
1031 && primary.getGatewayInfo() != null
1032 && !primary.getGatewayInfo().isEmpty();
Eric Erfanianccca3152017-02-22 16:32:36 -08001033 }
1034
1035 /** Gets the name to display for the call. */
Eric Erfanian2ca43182017-08-31 06:57:16 -07001036 private String getNameForCall(ContactCacheEntry contactInfo) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001037 String preferredName =
1038 ContactDisplayUtils.getPreferredDisplayName(
linyuh183cb712017-12-27 17:02:37 -08001039 contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
Eric Erfanianccca3152017-02-22 16:32:36 -08001040 if (TextUtils.isEmpty(preferredName)) {
1041 return contactInfo.number;
1042 }
1043 return preferredName;
1044 }
1045
Eric Erfanianccca3152017-02-22 16:32:36 -08001046 @Override
1047 public void onSecondaryInfoClicked() {
linyuh183cb712017-12-27 17:02:37 -08001048 if (secondary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001049 LogUtil.e(
1050 "CallCardPresenter.onSecondaryInfoClicked",
1051 "secondary info clicked but no secondary call.");
1052 return;
1053 }
1054
linyuh183cb712017-12-27 17:02:37 -08001055 Logger.get(context)
Eric Erfanian2ca43182017-08-31 06:57:16 -07001056 .logCallImpression(
1057 DialerImpression.Type.IN_CALL_SWAP_SECONDARY_BUTTON_PRESSED,
linyuh183cb712017-12-27 17:02:37 -08001058 primary.getUniqueCallId(),
1059 primary.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -08001060 LogUtil.i(
linyuh183cb712017-12-27 17:02:37 -08001061 "CallCardPresenter.onSecondaryInfoClicked", "swapping call to foreground: " + secondary);
1062 secondary.unhold();
Eric Erfanianccca3152017-02-22 16:32:36 -08001063 }
1064
1065 @Override
1066 public void onEndCallClicked() {
linyuh183cb712017-12-27 17:02:37 -08001067 LogUtil.i("CallCardPresenter.onEndCallClicked", "disconnecting call: " + primary);
1068 if (primary != null) {
1069 primary.disconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -08001070 }
linyuh183cb712017-12-27 17:02:37 -08001071 PostCall.onDisconnectPressed(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001072 }
1073
1074 /**
1075 * Handles a change to the fullscreen mode of the in-call UI.
1076 *
1077 * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1078 */
1079 @Override
1080 public void onFullscreenModeChanged(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001081 isFullscreen = isFullscreenMode;
1082 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001083 return;
1084 }
1085 maybeShowManageConferenceCallButton();
1086 }
1087
1088 private boolean isPrimaryCallActive() {
linyuh183cb712017-12-27 17:02:37 -08001089 return primary != null && primary.getState() == DialerCall.State.ACTIVE;
Eric Erfanianccca3152017-02-22 16:32:36 -08001090 }
1091
Eric Erfanianccca3152017-02-22 16:32:36 -08001092 private boolean shouldShowEndCallButton(DialerCall primary, int callState) {
1093 if (primary == null) {
1094 return false;
1095 }
1096 if ((!DialerCall.State.isConnectingOrConnected(callState)
1097 && callState != DialerCall.State.DISCONNECTING
1098 && callState != DialerCall.State.DISCONNECTED)
1099 || callState == DialerCall.State.INCOMING) {
1100 return false;
1101 }
linyuh183cb712017-12-27 17:02:37 -08001102 if (this.primary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001103 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001104 return false;
1105 }
1106 return true;
1107 }
1108
1109 @Override
1110 public void onInCallScreenResumed() {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001111 updatePrimaryDisplayInfo();
1112
Eric Erfanianccca3152017-02-22 16:32:36 -08001113 if (shouldSendAccessibilityEvent) {
1114 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1115 }
1116 }
1117
Eric Erfaniand8046e52017-04-06 09:41:50 -07001118 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001119 public void onInCallScreenPaused() {}
Eric Erfaniand8046e52017-04-06 09:41:50 -07001120
Eric Erfanianccca3152017-02-22 16:32:36 -08001121 static boolean sendAccessibilityEvent(Context context, InCallScreen inCallScreen) {
1122 AccessibilityManager am =
1123 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
1124 if (!am.isEnabled()) {
1125 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "accessibility is off");
1126 return false;
1127 }
1128 if (inCallScreen == null) {
1129 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "incallscreen is null");
1130 return false;
1131 }
1132 Fragment fragment = inCallScreen.getInCallScreenFragment();
1133 if (fragment == null || fragment.getView() == null || fragment.getView().getParent() == null) {
1134 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "fragment/view/parent is null");
1135 return false;
1136 }
1137
1138 DisplayManager displayManager =
1139 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
1140 Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
1141 boolean screenIsOn = display.getState() == Display.STATE_ON;
1142 LogUtil.d("CallCardPresenter.sendAccessibilityEvent", "screen is on: %b", screenIsOn);
1143 if (!screenIsOn) {
1144 return false;
1145 }
1146
1147 AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
1148 inCallScreen.dispatchPopulateAccessibilityEvent(event);
1149 View view = inCallScreen.getInCallScreenFragment().getView();
1150 view.getParent().requestSendAccessibilityEvent(view, event);
1151 return true;
1152 }
1153
1154 private void maybeSendAccessibilityEvent(
1155 InCallState oldState, final InCallState newState, boolean primaryChanged) {
1156 shouldSendAccessibilityEvent = false;
linyuh183cb712017-12-27 17:02:37 -08001157 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001158 return;
1159 }
1160 final AccessibilityManager am =
linyuh183cb712017-12-27 17:02:37 -08001161 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
Eric Erfanianccca3152017-02-22 16:32:36 -08001162 if (!am.isEnabled()) {
1163 return;
1164 }
1165 // Announce the current call if it's new incoming/outgoing call or primary call is changed
1166 // due to switching calls between two ongoing calls (one is on hold).
1167 if ((oldState != InCallState.OUTGOING && newState == InCallState.OUTGOING)
1168 || (oldState != InCallState.INCOMING && newState == InCallState.INCOMING)
1169 || primaryChanged) {
1170 LogUtil.i(
1171 "CallCardPresenter.maybeSendAccessibilityEvent", "schedule accessibility announcement");
1172 shouldSendAccessibilityEvent = true;
1173 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1174 }
1175 }
1176
1177 /**
1178 * Determines whether the call subject should be visible on the UI. For the call subject to be
1179 * visible, the call has to be in an incoming or waiting state, and the subject must not be empty.
1180 *
1181 * @param call The call.
1182 * @return {@code true} if the subject should be shown, {@code false} otherwise.
1183 */
1184 private boolean shouldShowCallSubject(DialerCall call) {
1185 if (call == null) {
1186 return false;
1187 }
1188
1189 boolean isIncomingOrWaiting =
linyuh183cb712017-12-27 17:02:37 -08001190 primary.getState() == DialerCall.State.INCOMING
1191 || primary.getState() == DialerCall.State.CALL_WAITING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001192 return isIncomingOrWaiting
1193 && !TextUtils.isEmpty(call.getCallSubject())
1194 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED
1195 && call.isCallSubjectSupported();
1196 }
1197
1198 /**
1199 * Determines whether the "note sent" toast should be shown. It should be shown for a new outgoing
1200 * call with a subject.
1201 *
1202 * @param call The call
1203 * @return {@code true} if the toast should be shown, {@code false} otherwise.
1204 */
1205 private boolean shouldShowNoteSentToast(DialerCall call) {
1206 return call != null
1207 && hasCallSubject(call)
1208 && (call.getState() == DialerCall.State.DIALING
1209 || call.getState() == DialerCall.State.CONNECTING);
1210 }
1211
1212 private InCallScreen getUi() {
linyuh183cb712017-12-27 17:02:37 -08001213 return inCallScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001214 }
1215
Android Dialerf9439102018-02-21 08:05:14 -08001216 /** Callback for contact lookup. */
Eric Erfanianccca3152017-02-22 16:32:36 -08001217 public static class ContactLookupCallback implements ContactInfoCacheCallback {
1218
linyuh183cb712017-12-27 17:02:37 -08001219 private final WeakReference<CallCardPresenter> callCardPresenter;
1220 private final boolean isPrimary;
Eric Erfanianccca3152017-02-22 16:32:36 -08001221
1222 public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
linyuh183cb712017-12-27 17:02:37 -08001223 this.callCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
1224 this.isPrimary = isPrimary;
Eric Erfanianccca3152017-02-22 16:32:36 -08001225 }
1226
1227 @Override
1228 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
linyuh183cb712017-12-27 17:02:37 -08001229 CallCardPresenter presenter = callCardPresenter.get();
Eric Erfanianccca3152017-02-22 16:32:36 -08001230 if (presenter != null) {
linyuh183cb712017-12-27 17:02:37 -08001231 presenter.onContactInfoComplete(callId, entry, isPrimary);
Eric Erfanianccca3152017-02-22 16:32:36 -08001232 }
1233 }
1234
1235 @Override
1236 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
linyuh183cb712017-12-27 17:02:37 -08001237 CallCardPresenter presenter = callCardPresenter.get();
Eric Erfanianccca3152017-02-22 16:32:36 -08001238 if (presenter != null) {
1239 presenter.onImageLoadComplete(callId, entry);
1240 }
1241 }
1242 }
1243}