blob: 316b6cca05b7ec89b8862eaaf471cd416c5850d1 [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)
twyencb2a9812018-03-23 18:21:13 -0700771 .setLocation(
Android Dialerf9439102018-02-21 08:05:14 -0800772 shouldShowLocationAsLabel(nameIsNumber, primaryContactInfo.shouldShowLocation)
773 ? primaryContactInfo.location
774 : null)
twyencb2a9812018-03-23 18:21:13 -0700775 .setLabel(isChildNumberShown || isCallSubjectShown ? null : primaryContactInfo.label)
Android Dialerf9439102018-02-21 08:05:14 -0800776 .setPhoto(primaryContactInfo.photo)
777 .setPhotoType(primaryContactInfo.photoType)
778 .setIsSipCall(primaryContactInfo.isSipCall)
779 .setIsContactPhotoShown(showContactPhoto)
780 .setIsWorkCall(hasWorkCallProperty || isWorkContact)
781 .setIsSpam(primary.isSpam())
782 .setIsLocalContact(primaryContactInfo.isLocalContact())
783 .setAnsweringDisconnectsOngoingCall(primary.answeringDisconnectsForegroundVideoCall())
784 .setShouldShowLocation(shouldShowLocation())
785 .setContactInfoLookupKey(primaryContactInfo.lookupKey)
786 .setMultimediaData(multimediaData)
787 .setShowInCallButtonGrid(true)
788 .setNumberPresentation(primary.getNumberPresentation())
789 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800790 } else {
791 // Clear the primary display info.
Android Dialerf9439102018-02-21 08:05:14 -0800792 inCallScreen.setPrimary(PrimaryInfo.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800793 }
794
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700795 if (isInCallScreenReady) {
linyuh183cb712017-12-27 17:02:37 -0800796 inCallScreen.showLocationUi(getLocationFragment());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700797 } else {
798 LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location");
799 }
800 }
801
Eric Erfaniand8046e52017-04-06 09:41:50 -0700802 private static boolean shouldShowLocationAsLabel(
803 boolean nameIsNumber, boolean shouldShowLocation) {
804 if (nameIsNumber) {
805 return true;
806 }
807 if (shouldShowLocation) {
808 return true;
809 }
810 return false;
811 }
812
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700813 private Fragment getLocationFragment() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 if (!shouldShowLocation()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700815 return null;
816 }
817 LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment");
linyuh183cb712017-12-27 17:02:37 -0800818 return callLocation.getLocationFragment(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800819 }
820
821 private boolean shouldShowLocation() {
linyuh183cb712017-12-27 17:02:37 -0800822 if (!ConfigProviderBindings.get(context)
wangqi1d62ab22017-11-29 14:29:31 -0800823 .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) {
824 LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config.");
825 return false;
826 }
827 if (!isPotentialEmergencyCall()) {
828 LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location");
829 return false;
830 }
831 if (!hasLocationPermission()) {
832 LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission.");
833 return false;
834 }
835 if (isBatteryTooLowForEmergencyLocation()) {
836 LogUtil.i("CallCardPresenter.getLocationFragment", "low battery.");
837 return false;
838 }
linyuh183cb712017-12-27 17:02:37 -0800839 if (ActivityCompat.isInMultiWindowMode(inCallScreen.getInCallScreenFragment().getActivity())) {
wangqi1d62ab22017-11-29 14:29:31 -0800840 LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode");
841 return false;
842 }
linyuh183cb712017-12-27 17:02:37 -0800843 if (primary.isVideoCall()) {
wangqi1d62ab22017-11-29 14:29:31 -0800844 LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported");
845 return false;
846 }
linyuh183cb712017-12-27 17:02:37 -0800847 if (!callLocation.canGetLocation(context)) {
wangqi1d62ab22017-11-29 14:29:31 -0800848 LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location");
849 return false;
850 }
851 return true;
852 }
853
854 private boolean isPotentialEmergencyCall() {
linyuh183cb712017-12-27 17:02:37 -0800855 if (isOutgoingEmergencyCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800856 LogUtil.i("CallCardPresenter.shouldShowLocation", "new emergency call");
857 return true;
linyuh183cb712017-12-27 17:02:37 -0800858 } else if (isIncomingEmergencyCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800859 LogUtil.i("CallCardPresenter.shouldShowLocation", "potential emergency callback");
860 return true;
linyuh183cb712017-12-27 17:02:37 -0800861 } else if (isIncomingEmergencyCall(secondary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800862 LogUtil.i("CallCardPresenter.shouldShowLocation", "has potential emergency callback");
863 return true;
864 }
865 return false;
866 }
867
868 private static boolean isOutgoingEmergencyCall(@Nullable DialerCall call) {
869 return call != null && !call.isIncoming() && call.isEmergencyCall();
870 }
871
872 private static boolean isIncomingEmergencyCall(@Nullable DialerCall call) {
873 return call != null && call.isIncoming() && call.isPotentialEmergencyCallback();
874 }
875
876 private boolean hasLocationPermission() {
linyuh183cb712017-12-27 17:02:37 -0800877 return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
Eric Erfanianccca3152017-02-22 16:32:36 -0800878 == PackageManager.PERMISSION_GRANTED;
879 }
880
881 private boolean isBatteryTooLowForEmergencyLocation() {
882 Intent batteryStatus =
linyuh183cb712017-12-27 17:02:37 -0800883 context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Eric Erfanianccca3152017-02-22 16:32:36 -0800884 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
885 if (status == BatteryManager.BATTERY_STATUS_CHARGING
886 || status == BatteryManager.BATTERY_STATUS_FULL) {
887 // Plugged in or full battery
888 return false;
889 }
890 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
891 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
892 float batteryPercent = (100f * level) / scale;
893 long threshold =
linyuh183cb712017-12-27 17:02:37 -0800894 ConfigProviderBindings.get(context)
Eric Erfanianccca3152017-02-22 16:32:36 -0800895 .getLong(
896 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION,
897 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT);
898 LogUtil.i(
899 "CallCardPresenter.isBatteryTooLowForEmergencyLocation",
900 "percent charged: " + batteryPercent + ", min required charge: " + threshold);
901 return batteryPercent < threshold;
902 }
903
904 private void updateSecondaryDisplayInfo() {
linyuh183cb712017-12-27 17:02:37 -0800905 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800906 return;
907 }
908
linyuh183cb712017-12-27 17:02:37 -0800909 if (secondary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800910 // Clear the secondary display info.
Android Dialer9e335e22018-03-01 08:27:39 -0800911 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800912 return;
913 }
914
linyuh183cb712017-12-27 17:02:37 -0800915 if (secondary.isMergeInProcess()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700916 LogUtil.i(
917 "CallCardPresenter.updateSecondaryDisplayInfo",
918 "secondary call is merge in process, clearing info");
Android Dialer9e335e22018-03-01 08:27:39 -0800919 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700920 return;
921 }
922
linyuh183cb712017-12-27 17:02:37 -0800923 if (secondary.isConferenceCall()) {
924 inCallScreen.setSecondary(
Android Dialer9e335e22018-03-01 08:27:39 -0800925 SecondaryInfo.builder()
926 .setShouldShow(true)
927 .setName(
928 CallerInfoUtils.getConferenceString(
929 context, secondary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)))
930 .setProviderLabel(secondary.getCallProviderLabel())
931 .setIsConference(true)
932 .setIsVideoCall(secondary.isVideoCall())
933 .setIsFullscreen(isFullscreen)
934 .build());
linyuh183cb712017-12-27 17:02:37 -0800935 } else if (secondaryContactInfo != null) {
936 LogUtil.v("CallCardPresenter.updateSecondaryDisplayInfo", "" + secondaryContactInfo);
937 String name = getNameForCall(secondaryContactInfo);
938 boolean nameIsNumber = name != null && name.equals(secondaryContactInfo.number);
939 inCallScreen.setSecondary(
Android Dialer9e335e22018-03-01 08:27:39 -0800940 SecondaryInfo.builder()
941 .setShouldShow(true)
942 .setName(secondary.updateNameIfRestricted(name))
943 .setNameIsNumber(nameIsNumber)
944 .setLabel(secondaryContactInfo.label)
945 .setProviderLabel(secondary.getCallProviderLabel())
946 .setIsVideoCall(secondary.isVideoCall())
947 .setIsFullscreen(isFullscreen)
948 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800949 } else {
950 // Clear the secondary display info.
Android Dialer9e335e22018-03-01 08:27:39 -0800951 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800952 }
953 }
954
955 /** Returns the gateway number for any existing outgoing call. */
956 private String getGatewayNumber() {
957 if (hasOutgoingGatewayCall()) {
linyuh183cb712017-12-27 17:02:37 -0800958 return DialerCall.getNumberFromHandle(primary.getGatewayInfo().getGatewayAddress());
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 }
960 return null;
961 }
962
963 /**
964 * Returns the label (line of text above the number/name) for any given call. For example,
965 * "calling via [Account/Google Voice]" for outgoing calls.
966 */
967 private String getConnectionLabel() {
linyuh183cb712017-12-27 17:02:37 -0800968 if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800969 != PackageManager.PERMISSION_GRANTED) {
970 return null;
971 }
linyuh183cb712017-12-27 17:02:37 -0800972 StatusHints statusHints = primary.getStatusHints();
Eric Erfanianccca3152017-02-22 16:32:36 -0800973 if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
974 return statusHints.getLabel().toString();
975 }
976
977 if (hasOutgoingGatewayCall() && getUi() != null) {
978 // Return the label for the gateway app on outgoing calls.
linyuh183cb712017-12-27 17:02:37 -0800979 final PackageManager pm = context.getPackageManager();
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 try {
981 ApplicationInfo info =
linyuh183cb712017-12-27 17:02:37 -0800982 pm.getApplicationInfo(primary.getGatewayInfo().getGatewayProviderPackageName(), 0);
Eric Erfanianccca3152017-02-22 16:32:36 -0800983 return pm.getApplicationLabel(info).toString();
984 } catch (PackageManager.NameNotFoundException e) {
985 LogUtil.e("CallCardPresenter.getConnectionLabel", "gateway Application Not Found.", e);
986 return null;
987 }
988 }
linyuh183cb712017-12-27 17:02:37 -0800989 return primary.getCallProviderLabel();
Eric Erfanianccca3152017-02-22 16:32:36 -0800990 }
991
twyen4fde0ac2018-03-22 18:04:23 -0700992 @Nullable
993 private SuggestionProvider.Reason getSimSuggestionReason() {
994 String value =
995 primary.getIntentExtras().getString(SuggestionProvider.EXTRA_SIM_SUGGESTION_REASON);
996 if (value == null) {
997 return null;
998 }
999 try {
1000 return SuggestionProvider.Reason.valueOf(value);
1001 } catch (IllegalArgumentException e) {
1002 LogUtil.e("CallCardPresenter.getConnectionLabel", "unknown reason " + value);
1003 return null;
1004 }
1005 }
1006
Eric Erfanianccca3152017-02-22 16:32:36 -08001007 private Drawable getCallStateIcon() {
1008 // Return connection icon if one exists.
linyuh183cb712017-12-27 17:02:37 -08001009 StatusHints statusHints = primary.getStatusHints();
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 if (statusHints != null && statusHints.getIcon() != null) {
linyuh183cb712017-12-27 17:02:37 -08001011 Drawable icon = statusHints.getIcon().loadDrawable(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001012 if (icon != null) {
1013 return icon;
1014 }
1015 }
1016
1017 return null;
1018 }
1019
1020 private boolean hasOutgoingGatewayCall() {
1021 // We only display the gateway information while STATE_DIALING so return false for any other
1022 // call state.
1023 // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
1024 // is also called after a contact search completes (call is not present yet). Split the
1025 // UI update so it can receive independent updates.
linyuh183cb712017-12-27 17:02:37 -08001026 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001027 return false;
1028 }
linyuh183cb712017-12-27 17:02:37 -08001029 return DialerCall.State.isDialing(primary.getState())
1030 && primary.getGatewayInfo() != null
1031 && !primary.getGatewayInfo().isEmpty();
Eric Erfanianccca3152017-02-22 16:32:36 -08001032 }
1033
1034 /** Gets the name to display for the call. */
Eric Erfanian2ca43182017-08-31 06:57:16 -07001035 private String getNameForCall(ContactCacheEntry contactInfo) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001036 String preferredName =
1037 ContactDisplayUtils.getPreferredDisplayName(
linyuh183cb712017-12-27 17:02:37 -08001038 contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 if (TextUtils.isEmpty(preferredName)) {
1040 return contactInfo.number;
1041 }
1042 return preferredName;
1043 }
1044
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 @Override
1046 public void onSecondaryInfoClicked() {
linyuh183cb712017-12-27 17:02:37 -08001047 if (secondary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001048 LogUtil.e(
1049 "CallCardPresenter.onSecondaryInfoClicked",
1050 "secondary info clicked but no secondary call.");
1051 return;
1052 }
1053
linyuh183cb712017-12-27 17:02:37 -08001054 Logger.get(context)
Eric Erfanian2ca43182017-08-31 06:57:16 -07001055 .logCallImpression(
1056 DialerImpression.Type.IN_CALL_SWAP_SECONDARY_BUTTON_PRESSED,
linyuh183cb712017-12-27 17:02:37 -08001057 primary.getUniqueCallId(),
1058 primary.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -08001059 LogUtil.i(
linyuh183cb712017-12-27 17:02:37 -08001060 "CallCardPresenter.onSecondaryInfoClicked", "swapping call to foreground: " + secondary);
1061 secondary.unhold();
Eric Erfanianccca3152017-02-22 16:32:36 -08001062 }
1063
1064 @Override
1065 public void onEndCallClicked() {
linyuh183cb712017-12-27 17:02:37 -08001066 LogUtil.i("CallCardPresenter.onEndCallClicked", "disconnecting call: " + primary);
1067 if (primary != null) {
1068 primary.disconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -08001069 }
linyuh183cb712017-12-27 17:02:37 -08001070 PostCall.onDisconnectPressed(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001071 }
1072
1073 /**
1074 * Handles a change to the fullscreen mode of the in-call UI.
1075 *
1076 * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1077 */
1078 @Override
1079 public void onFullscreenModeChanged(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001080 isFullscreen = isFullscreenMode;
1081 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001082 return;
1083 }
1084 maybeShowManageConferenceCallButton();
1085 }
1086
1087 private boolean isPrimaryCallActive() {
linyuh183cb712017-12-27 17:02:37 -08001088 return primary != null && primary.getState() == DialerCall.State.ACTIVE;
Eric Erfanianccca3152017-02-22 16:32:36 -08001089 }
1090
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 private boolean shouldShowEndCallButton(DialerCall primary, int callState) {
1092 if (primary == null) {
1093 return false;
1094 }
1095 if ((!DialerCall.State.isConnectingOrConnected(callState)
1096 && callState != DialerCall.State.DISCONNECTING
1097 && callState != DialerCall.State.DISCONNECTED)
1098 || callState == DialerCall.State.INCOMING) {
1099 return false;
1100 }
linyuh183cb712017-12-27 17:02:37 -08001101 if (this.primary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001102 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001103 return false;
1104 }
1105 return true;
1106 }
1107
1108 @Override
1109 public void onInCallScreenResumed() {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001110 updatePrimaryDisplayInfo();
1111
Eric Erfanianccca3152017-02-22 16:32:36 -08001112 if (shouldSendAccessibilityEvent) {
1113 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1114 }
1115 }
1116
Eric Erfaniand8046e52017-04-06 09:41:50 -07001117 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001118 public void onInCallScreenPaused() {}
Eric Erfaniand8046e52017-04-06 09:41:50 -07001119
Eric Erfanianccca3152017-02-22 16:32:36 -08001120 static boolean sendAccessibilityEvent(Context context, InCallScreen inCallScreen) {
1121 AccessibilityManager am =
1122 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
1123 if (!am.isEnabled()) {
1124 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "accessibility is off");
1125 return false;
1126 }
1127 if (inCallScreen == null) {
1128 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "incallscreen is null");
1129 return false;
1130 }
1131 Fragment fragment = inCallScreen.getInCallScreenFragment();
1132 if (fragment == null || fragment.getView() == null || fragment.getView().getParent() == null) {
1133 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "fragment/view/parent is null");
1134 return false;
1135 }
1136
1137 DisplayManager displayManager =
1138 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
1139 Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
1140 boolean screenIsOn = display.getState() == Display.STATE_ON;
1141 LogUtil.d("CallCardPresenter.sendAccessibilityEvent", "screen is on: %b", screenIsOn);
1142 if (!screenIsOn) {
1143 return false;
1144 }
1145
1146 AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
1147 inCallScreen.dispatchPopulateAccessibilityEvent(event);
1148 View view = inCallScreen.getInCallScreenFragment().getView();
1149 view.getParent().requestSendAccessibilityEvent(view, event);
1150 return true;
1151 }
1152
1153 private void maybeSendAccessibilityEvent(
1154 InCallState oldState, final InCallState newState, boolean primaryChanged) {
1155 shouldSendAccessibilityEvent = false;
linyuh183cb712017-12-27 17:02:37 -08001156 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001157 return;
1158 }
1159 final AccessibilityManager am =
linyuh183cb712017-12-27 17:02:37 -08001160 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
Eric Erfanianccca3152017-02-22 16:32:36 -08001161 if (!am.isEnabled()) {
1162 return;
1163 }
1164 // Announce the current call if it's new incoming/outgoing call or primary call is changed
1165 // due to switching calls between two ongoing calls (one is on hold).
1166 if ((oldState != InCallState.OUTGOING && newState == InCallState.OUTGOING)
1167 || (oldState != InCallState.INCOMING && newState == InCallState.INCOMING)
1168 || primaryChanged) {
1169 LogUtil.i(
1170 "CallCardPresenter.maybeSendAccessibilityEvent", "schedule accessibility announcement");
1171 shouldSendAccessibilityEvent = true;
1172 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1173 }
1174 }
1175
1176 /**
1177 * Determines whether the call subject should be visible on the UI. For the call subject to be
1178 * visible, the call has to be in an incoming or waiting state, and the subject must not be empty.
1179 *
1180 * @param call The call.
1181 * @return {@code true} if the subject should be shown, {@code false} otherwise.
1182 */
1183 private boolean shouldShowCallSubject(DialerCall call) {
1184 if (call == null) {
1185 return false;
1186 }
1187
1188 boolean isIncomingOrWaiting =
linyuh183cb712017-12-27 17:02:37 -08001189 primary.getState() == DialerCall.State.INCOMING
1190 || primary.getState() == DialerCall.State.CALL_WAITING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001191 return isIncomingOrWaiting
1192 && !TextUtils.isEmpty(call.getCallSubject())
1193 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED
1194 && call.isCallSubjectSupported();
1195 }
1196
1197 /**
1198 * Determines whether the "note sent" toast should be shown. It should be shown for a new outgoing
1199 * call with a subject.
1200 *
1201 * @param call The call
1202 * @return {@code true} if the toast should be shown, {@code false} otherwise.
1203 */
1204 private boolean shouldShowNoteSentToast(DialerCall call) {
1205 return call != null
1206 && hasCallSubject(call)
1207 && (call.getState() == DialerCall.State.DIALING
1208 || call.getState() == DialerCall.State.CONNECTING);
1209 }
1210
1211 private InCallScreen getUi() {
linyuh183cb712017-12-27 17:02:37 -08001212 return inCallScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001213 }
1214
Android Dialerf9439102018-02-21 08:05:14 -08001215 /** Callback for contact lookup. */
Eric Erfanianccca3152017-02-22 16:32:36 -08001216 public static class ContactLookupCallback implements ContactInfoCacheCallback {
1217
linyuh183cb712017-12-27 17:02:37 -08001218 private final WeakReference<CallCardPresenter> callCardPresenter;
1219 private final boolean isPrimary;
Eric Erfanianccca3152017-02-22 16:32:36 -08001220
1221 public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
linyuh183cb712017-12-27 17:02:37 -08001222 this.callCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
1223 this.isPrimary = isPrimary;
Eric Erfanianccca3152017-02-22 16:32:36 -08001224 }
1225
1226 @Override
1227 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
linyuh183cb712017-12-27 17:02:37 -08001228 CallCardPresenter presenter = callCardPresenter.get();
Eric Erfanianccca3152017-02-22 16:32:36 -08001229 if (presenter != null) {
linyuh183cb712017-12-27 17:02:37 -08001230 presenter.onContactInfoComplete(callId, entry, isPrimary);
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 }
1232 }
1233
1234 @Override
1235 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
linyuh183cb712017-12-27 17:02:37 -08001236 CallCardPresenter presenter = callCardPresenter.get();
Eric Erfanianccca3152017-02-22 16:32:36 -08001237 if (presenter != null) {
1238 presenter.onImageLoadComplete(callId, entry);
1239 }
1240 }
1241 }
1242}