blob: 9c5e0623e93d951461660fe7cd88688303fdcaa2 [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 Erfanian2ca43182017-08-31 06:57:16 -070049import com.android.dialer.configprovider.ConfigProviderBindings;
Eric Erfanian8369df02017-05-03 10:27:13 -070050import com.android.dialer.logging.DialerImpression;
Eric Erfaniand8046e52017-04-06 09:41:50 -070051import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080052import com.android.dialer.multimedia.MultimediaData;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import com.android.dialer.oem.MotorolaUtils;
wangqi97539352017-09-25 11:15:16 -070054import com.android.dialer.phonenumberutil.PhoneNumberHelper;
Eric Erfanian2ca43182017-08-31 06:57:16 -070055import com.android.dialer.postcall.PostCall;
twyen4fde0ac2018-03-22 18:04:23 -070056import com.android.dialer.preferredsim.suggestion.SuggestionProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.incallui.ContactInfoCache.ContactCacheEntry;
58import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
59import com.android.incallui.InCallPresenter.InCallDetailsListener;
60import com.android.incallui.InCallPresenter.InCallEventListener;
61import com.android.incallui.InCallPresenter.InCallState;
62import com.android.incallui.InCallPresenter.InCallStateListener;
63import com.android.incallui.InCallPresenter.IncomingCallListener;
64import com.android.incallui.call.CallList;
65import com.android.incallui.call.DialerCall;
Eric Erfanian2ca43182017-08-31 06:57:16 -070066import com.android.incallui.call.DialerCall.State;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.incallui.call.DialerCallListener;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070068import com.android.incallui.calllocation.CallLocation;
69import com.android.incallui.calllocation.CallLocationComponent;
Eric Erfanianccca3152017-02-22 16:32:36 -080070import com.android.incallui.incall.protocol.ContactPhotoType;
71import com.android.incallui.incall.protocol.InCallScreen;
72import com.android.incallui.incall.protocol.InCallScreenDelegate;
73import com.android.incallui.incall.protocol.PrimaryCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -070074import com.android.incallui.incall.protocol.PrimaryCallState.ButtonState;
Eric Erfanianccca3152017-02-22 16:32:36 -080075import com.android.incallui.incall.protocol.PrimaryInfo;
76import com.android.incallui.incall.protocol.SecondaryInfo;
Eric Erfanian90508232017-03-24 09:31:16 -070077import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import java.lang.ref.WeakReference;
79
80/**
81 * Controller for the Call Card Fragment. This class listens for changes to InCallState and passes
82 * it along to the fragment.
83 */
84public class CallCardPresenter
85 implements InCallStateListener,
86 IncomingCallListener,
87 InCallDetailsListener,
88 InCallEventListener,
89 InCallScreenDelegate,
Eric Erfanian2ca43182017-08-31 06:57:16 -070090 DialerCallListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080091
92 /**
93 * Amount of time to wait before sending an announcement via the accessibility manager. When the
94 * call state changes to an outgoing or incoming state for the first time, the UI can often be
95 * changing due to call updates or contact lookup. This allows the UI to settle to a stable state
96 * to ensure that the correct information is announced.
97 */
98 private static final long ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS = 500;
99
100 /** Flag to allow the user's current location to be shown during emergency calls. */
101 private static final String CONFIG_ENABLE_EMERGENCY_LOCATION = "config_enable_emergency_location";
102
103 private static final boolean CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT = true;
104
105 /**
106 * Make it possible to not get location during an emergency call if the battery is too low, since
107 * doing so could trigger gps and thus potentially cause the phone to die in the middle of the
108 * call.
109 */
110 private static final String CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION =
111 "min_battery_percent_for_emergency_location";
112
113 private static final long CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT = 10;
114
linyuh183cb712017-12-27 17:02:37 -0800115 private final Context context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800116 private final Handler handler = new Handler();
117
linyuh183cb712017-12-27 17:02:37 -0800118 private DialerCall primary;
wangqidd5f1a52018-01-23 18:01:00 -0800119 private String primaryNumber;
linyuh183cb712017-12-27 17:02:37 -0800120 private DialerCall secondary;
wangqidd5f1a52018-01-23 18:01:00 -0800121 private String secondaryNumber;
linyuh183cb712017-12-27 17:02:37 -0800122 private ContactCacheEntry primaryContactInfo;
123 private ContactCacheEntry secondaryContactInfo;
124 @Nullable private ContactsPreferences contactsPreferences;
125 private boolean isFullscreen = false;
126 private InCallScreen inCallScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 private boolean isInCallScreenReady;
128 private boolean shouldSendAccessibilityEvent;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700129
130 @NonNull private final CallLocation callLocation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800131 private final Runnable sendAccessibilityEventRunnable =
132 new Runnable() {
133 @Override
134 public void run() {
linyuh183cb712017-12-27 17:02:37 -0800135 shouldSendAccessibilityEvent = !sendAccessibilityEvent(context, getUi());
Eric Erfanianccca3152017-02-22 16:32:36 -0800136 LogUtil.i(
137 "CallCardPresenter.sendAccessibilityEventRunnable",
138 "still should send: %b",
139 shouldSendAccessibilityEvent);
140 if (!shouldSendAccessibilityEvent) {
141 handler.removeCallbacks(this);
142 }
143 }
144 };
145
146 public CallCardPresenter(Context context) {
wangqi385a5a12017-09-28 10:44:54 -0700147 LogUtil.i("CallCardPresenter.constructor", null);
linyuh183cb712017-12-27 17:02:37 -0800148 this.context = Assert.isNotNull(context).getApplicationContext();
149 callLocation = CallLocationComponent.get(this.context).getCallLocation();
Eric Erfanianccca3152017-02-22 16:32:36 -0800150 }
151
152 private static boolean hasCallSubject(DialerCall call) {
153 return !TextUtils.isEmpty(call.getCallSubject());
154 }
155
156 @Override
157 public void onInCallScreenDelegateInit(InCallScreen inCallScreen) {
158 Assert.isNotNull(inCallScreen);
linyuh183cb712017-12-27 17:02:37 -0800159 this.inCallScreen = inCallScreen;
160 contactsPreferences = ContactsPreferencesFactory.newContactsPreferences(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800161
162 // Call may be null if disconnect happened already.
163 DialerCall call = CallList.getInstance().getFirstCall();
164 if (call != null) {
linyuh183cb712017-12-27 17:02:37 -0800165 primary = call;
166 if (shouldShowNoteSentToast(primary)) {
167 this.inCallScreen.showNoteSentToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 }
169 call.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 // start processing lookups right away.
171 if (!call.isConferenceCall()) {
172 startContactInfoSearch(call, true, call.getState() == DialerCall.State.INCOMING);
173 } else {
174 updateContactEntry(null, true);
175 }
176 }
177
178 onStateChange(null, InCallPresenter.getInstance().getInCallState(), CallList.getInstance());
179 }
180
181 @Override
182 public void onInCallScreenReady() {
wangqi385a5a12017-09-28 10:44:54 -0700183 LogUtil.i("CallCardPresenter.onInCallScreenReady", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 Assert.checkState(!isInCallScreenReady);
linyuh183cb712017-12-27 17:02:37 -0800185 if (contactsPreferences != null) {
186 contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 }
188
Eric Erfanianccca3152017-02-22 16:32:36 -0800189 // Contact search may have completed before ui is ready.
linyuh183cb712017-12-27 17:02:37 -0800190 if (primaryContactInfo != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800191 updatePrimaryDisplayInfo();
192 }
193
194 // Register for call state changes last
195 InCallPresenter.getInstance().addListener(this);
196 InCallPresenter.getInstance().addIncomingCallListener(this);
197 InCallPresenter.getInstance().addDetailsListener(this);
198 InCallPresenter.getInstance().addInCallEventListener(this);
199 isInCallScreenReady = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700200
Eric Erfaniand8046e52017-04-06 09:41:50 -0700201 // Log location impressions
linyuh183cb712017-12-27 17:02:37 -0800202 if (isOutgoingEmergencyCall(primary)) {
203 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_NEW_EMERGENCY_CALL);
204 } else if (isIncomingEmergencyCall(primary) || isIncomingEmergencyCall(secondary)) {
205 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_CALLBACK);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700206 }
207
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700208 // Showing the location may have been skipped if the UI wasn't ready during previous layout.
209 if (shouldShowLocation()) {
linyuh183cb712017-12-27 17:02:37 -0800210 inCallScreen.showLocationUi(getLocationFragment());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700211
212 // Log location impressions
213 if (!hasLocationPermission()) {
linyuh183cb712017-12-27 17:02:37 -0800214 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_NO_LOCATION_PERMISSION);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700215 } else if (isBatteryTooLowForEmergencyLocation()) {
linyuh183cb712017-12-27 17:02:37 -0800216 Logger.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700217 .logImpression(DialerImpression.Type.EMERGENCY_BATTERY_TOO_LOW_TO_GET_LOCATION);
linyuh183cb712017-12-27 17:02:37 -0800218 } else if (!callLocation.canGetLocation(context)) {
219 Logger.get(context).logImpression(DialerImpression.Type.EMERGENCY_CANT_GET_LOCATION);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700220 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700221 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800222 }
223
224 @Override
225 public void onInCallScreenUnready() {
wangqi385a5a12017-09-28 10:44:54 -0700226 LogUtil.i("CallCardPresenter.onInCallScreenUnready", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800227 Assert.checkState(isInCallScreenReady);
228
Eric Erfanianccca3152017-02-22 16:32:36 -0800229 // stop getting call state changes
230 InCallPresenter.getInstance().removeListener(this);
231 InCallPresenter.getInstance().removeIncomingCallListener(this);
232 InCallPresenter.getInstance().removeDetailsListener(this);
233 InCallPresenter.getInstance().removeInCallEventListener(this);
linyuh183cb712017-12-27 17:02:37 -0800234 if (primary != null) {
235 primary.removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800236 }
237
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700238 callLocation.close();
239
linyuh183cb712017-12-27 17:02:37 -0800240 primary = null;
241 primaryContactInfo = null;
242 secondaryContactInfo = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800243 isInCallScreenReady = false;
244 }
245
246 @Override
247 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
248 // same logic should happen as with onStateChange()
249 onStateChange(oldState, newState, CallList.getInstance());
250 }
251
252 @Override
253 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700254 Trace.beginSection("CallCardPresenter.onStateChange");
255 LogUtil.v("CallCardPresenter.onStateChange", "oldState: %s, newState: %s", oldState, newState);
linyuh183cb712017-12-27 17:02:37 -0800256 if (inCallScreen == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700257 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800258 return;
259 }
260
261 DialerCall primary = null;
262 DialerCall secondary = null;
263
264 if (newState == InCallState.INCOMING) {
265 primary = callList.getIncomingCall();
266 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
267 primary = callList.getOutgoingCall();
268 if (primary == null) {
269 primary = callList.getPendingOutgoingCall();
270 }
271
272 // getCallToDisplay doesn't go through outgoing or incoming calls. It will return the
273 // highest priority call to display as the secondary call.
274 secondary = getCallToDisplay(callList, null, true);
275 } else if (newState == InCallState.INCALL) {
276 primary = getCallToDisplay(callList, null, false);
277 secondary = getCallToDisplay(callList, primary, true);
278 }
279
280 LogUtil.v("CallCardPresenter.onStateChange", "primary call: " + primary);
281 LogUtil.v("CallCardPresenter.onStateChange", "secondary call: " + secondary);
wangqidd5f1a52018-01-23 18:01:00 -0800282 String primaryNumber = null;
283 String secondaryNumber = null;
284 if (primary != null) {
285 primaryNumber = primary.getNumber();
286 }
287 if (secondary != null) {
288 secondaryNumber = secondary.getNumber();
289 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800290
291 final boolean primaryChanged =
linyuh183cb712017-12-27 17:02:37 -0800292 !(DialerCall.areSame(this.primary, primary)
wangqidd5f1a52018-01-23 18:01:00 -0800293 && TextUtils.equals(this.primaryNumber, primaryNumber));
Eric Erfanianccca3152017-02-22 16:32:36 -0800294 final boolean secondaryChanged =
linyuh183cb712017-12-27 17:02:37 -0800295 !(DialerCall.areSame(this.secondary, secondary)
wangqidd5f1a52018-01-23 18:01:00 -0800296 && TextUtils.equals(this.secondaryNumber, secondaryNumber));
Eric Erfanianccca3152017-02-22 16:32:36 -0800297
linyuh183cb712017-12-27 17:02:37 -0800298 this.secondary = secondary;
wangqidd5f1a52018-01-23 18:01:00 -0800299 this.secondaryNumber = secondaryNumber;
linyuh183cb712017-12-27 17:02:37 -0800300 DialerCall previousPrimary = this.primary;
301 this.primary = primary;
wangqidd5f1a52018-01-23 18:01:00 -0800302 this.primaryNumber = primaryNumber;
Eric Erfanianccca3152017-02-22 16:32:36 -0800303
linyuh183cb712017-12-27 17:02:37 -0800304 if (this.primary != null) {
305 InCallPresenter.getInstance().onForegroundCallChanged(this.primary);
306 inCallScreen.updateInCallScreenColors();
Eric Erfanianccca3152017-02-22 16:32:36 -0800307 }
308
309 if (primaryChanged && shouldShowNoteSentToast(primary)) {
linyuh183cb712017-12-27 17:02:37 -0800310 inCallScreen.showNoteSentToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800311 }
312
313 // Refresh primary call information if either:
314 // 1. Primary call changed.
315 // 2. The call's ability to manage conference has changed.
316 if (shouldRefreshPrimaryInfo(primaryChanged)) {
317 // primary call has changed
318 if (previousPrimary != null) {
319 previousPrimary.removeListener(this);
320 }
linyuh183cb712017-12-27 17:02:37 -0800321 this.primary.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800322
linyuh183cb712017-12-27 17:02:37 -0800323 primaryContactInfo =
Eric Erfanianccca3152017-02-22 16:32:36 -0800324 ContactInfoCache.buildCacheEntryFromCall(
linyuh183cb712017-12-27 17:02:37 -0800325 context, this.primary, this.primary.getState() == DialerCall.State.INCOMING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 updatePrimaryDisplayInfo();
linyuh183cb712017-12-27 17:02:37 -0800327 maybeStartSearch(this.primary, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800328 }
329
linyuh183cb712017-12-27 17:02:37 -0800330 if (previousPrimary != null && this.primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800331 previousPrimary.removeListener(this);
332 }
333
wangqic8cf79e2017-10-17 09:21:00 -0700334 if (secondaryChanged) {
linyuh183cb712017-12-27 17:02:37 -0800335 if (this.secondary == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700336 // Secondary call may have ended. Update the ui.
linyuh183cb712017-12-27 17:02:37 -0800337 secondaryContactInfo = null;
wangqic8cf79e2017-10-17 09:21:00 -0700338 updateSecondaryDisplayInfo();
339 } else {
340 // secondary call has changed
linyuh183cb712017-12-27 17:02:37 -0800341 secondaryContactInfo =
wangqic8cf79e2017-10-17 09:21:00 -0700342 ContactInfoCache.buildCacheEntryFromCall(
linyuh183cb712017-12-27 17:02:37 -0800343 context, this.secondary, this.secondary.getState() == DialerCall.State.INCOMING);
wangqic8cf79e2017-10-17 09:21:00 -0700344 updateSecondaryDisplayInfo();
linyuh183cb712017-12-27 17:02:37 -0800345 maybeStartSearch(this.secondary, false);
wangqic8cf79e2017-10-17 09:21:00 -0700346 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800347 }
348
349 // Set the call state
350 int callState = DialerCall.State.IDLE;
linyuh183cb712017-12-27 17:02:37 -0800351 if (this.primary != null) {
352 callState = this.primary.getState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800353 updatePrimaryCallState();
354 } else {
Android Dialer31fe9982018-02-26 13:29:09 -0800355 getUi().setCallState(PrimaryCallState.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800356 }
357
358 maybeShowManageConferenceCallButton();
359
360 // Hide the end call button instantly if we're receiving an incoming call.
361 getUi()
362 .setEndCallButtonEnabled(
linyuh183cb712017-12-27 17:02:37 -0800363 shouldShowEndCallButton(this.primary, callState),
Eric Erfanianccca3152017-02-22 16:32:36 -0800364 callState != DialerCall.State.INCOMING /* animate */);
365
366 maybeSendAccessibilityEvent(oldState, newState, primaryChanged);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700367 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800368 }
369
370 @Override
371 public void onDetailsChanged(DialerCall call, Details details) {
372 updatePrimaryCallState();
373
374 if (call.can(Details.CAPABILITY_MANAGE_CONFERENCE)
375 != details.can(Details.CAPABILITY_MANAGE_CONFERENCE)) {
376 maybeShowManageConferenceCallButton();
377 }
378 }
379
380 @Override
381 public void onDialerCallDisconnect() {}
382
383 @Override
384 public void onDialerCallUpdate() {
385 // No-op; specific call updates handled elsewhere.
386 }
387
388 @Override
389 public void onWiFiToLteHandover() {}
390
391 @Override
392 public void onHandoverToWifiFailure() {}
393
Eric Erfanianc857f902017-05-15 14:05:33 -0700394 @Override
395 public void onInternationalCallOnWifi() {}
396
Eric Erfanian2ca43182017-08-31 06:57:16 -0700397 @Override
398 public void onEnrichedCallSessionUpdate() {
399 LogUtil.enterBlock("CallCardPresenter.onEnrichedCallSessionUpdate");
400 updatePrimaryDisplayInfo();
401 }
402
Eric Erfanianccca3152017-02-22 16:32:36 -0800403 /** Handles a change to the child number by refreshing the primary call info. */
404 @Override
405 public void onDialerCallChildNumberChange() {
406 LogUtil.v("CallCardPresenter.onDialerCallChildNumberChange", "");
407
linyuh183cb712017-12-27 17:02:37 -0800408 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 return;
410 }
411 updatePrimaryDisplayInfo();
412 }
413
414 /** Handles a change to the last forwarding number by refreshing the primary call info. */
415 @Override
416 public void onDialerCallLastForwardedNumberChange() {
417 LogUtil.v("CallCardPresenter.onDialerCallLastForwardedNumberChange", "");
418
linyuh183cb712017-12-27 17:02:37 -0800419 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 return;
421 }
422 updatePrimaryDisplayInfo();
423 updatePrimaryCallState();
424 }
425
426 @Override
427 public void onDialerCallUpgradeToVideo() {}
428
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700429 /** Handles a change to the session modification state for a call. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700431 public void onDialerCallSessionModificationStateChange() {
432 LogUtil.enterBlock("CallCardPresenter.onDialerCallSessionModificationStateChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800433
linyuh183cb712017-12-27 17:02:37 -0800434 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800435 return;
436 }
437 getUi()
438 .setEndCallButtonEnabled(
linyuh183cb712017-12-27 17:02:37 -0800439 primary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700440 != SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800441 true /* shouldAnimate */);
442 updatePrimaryCallState();
443 }
444
Eric Erfanianccca3152017-02-22 16:32:36 -0800445 private boolean shouldRefreshPrimaryInfo(boolean primaryChanged) {
linyuh183cb712017-12-27 17:02:37 -0800446 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800447 return false;
448 }
449 return primaryChanged
linyuh183cb712017-12-27 17:02:37 -0800450 || inCallScreen.isManageConferenceVisible() != shouldShowManageConference();
Eric Erfanianccca3152017-02-22 16:32:36 -0800451 }
452
453 private void updatePrimaryCallState() {
linyuh183cb712017-12-27 17:02:37 -0800454 if (getUi() != null && primary != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800455 boolean isWorkCall =
linyuh183cb712017-12-27 17:02:37 -0800456 primary.hasProperty(PROPERTY_ENTERPRISE_CALL)
457 || (primaryContactInfo != null
458 && primaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
Eric Erfanianccca3152017-02-22 16:32:36 -0800459 boolean isHdAudioCall =
linyuh183cb712017-12-27 17:02:37 -0800460 isPrimaryCallActive() && primary.hasProperty(Details.PROPERTY_HIGH_DEF_AUDIO);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700461 boolean isAttemptingHdAudioCall =
462 !isHdAudioCall
linyuh183cb712017-12-27 17:02:37 -0800463 && !primary.hasProperty(DialerCall.PROPERTY_CODEC_KNOWN)
464 && MotorolaUtils.shouldBlinkHdIconWhenConnectingCall(context);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700465
linyuh183cb712017-12-27 17:02:37 -0800466 boolean isBusiness = primaryContactInfo != null && primaryContactInfo.isBusiness;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700467
Eric Erfanianccca3152017-02-22 16:32:36 -0800468 // Check for video state change and update the visibility of the contact photo. The contact
469 // photo is hidden when the incoming video surface is shown.
470 // The contact photo visibility can also change in setPrimary().
471 boolean shouldShowContactPhoto =
linyuh183cb712017-12-27 17:02:37 -0800472 !VideoCallPresenter.showIncomingVideo(primary.getVideoState(), primary.getState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800473 getUi()
474 .setCallState(
Android Dialer31fe9982018-02-26 13:29:09 -0800475 PrimaryCallState.builder()
476 .setState(primary.getState())
477 .setIsVideoCall(primary.isVideoCall())
478 .setSessionModificationState(primary.getVideoTech().getSessionModificationState())
479 .setDisconnectCause(primary.getDisconnectCause())
480 .setConnectionLabel(getConnectionLabel())
twyen4fde0ac2018-03-22 18:04:23 -0700481 .setSimSuggestionReason(getSimSuggestionReason())
Android Dialer31fe9982018-02-26 13:29:09 -0800482 .setConnectionIcon(getCallStateIcon())
483 .setGatewayNumber(getGatewayNumber())
484 .setCallSubject(shouldShowCallSubject(primary) ? primary.getCallSubject() : null)
485 .setCallbackNumber(
486 PhoneNumberHelper.formatNumber(
linyuhb06d0092018-03-01 15:05:36 -0800487 context, primary.getCallbackNumber(), primary.getSimCountryIso()))
Android Dialer31fe9982018-02-26 13:29:09 -0800488 .setIsWifi(primary.hasProperty(Details.PROPERTY_WIFI))
489 .setIsConference(
490 primary.isConferenceCall()
491 && !primary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE))
492 .setIsWorkCall(isWorkCall)
493 .setIsHdAttempting(isAttemptingHdAudioCall)
494 .setIsHdAudioCall(isHdAudioCall)
495 .setIsForwardedNumber(
496 !TextUtils.isEmpty(primary.getLastForwardedNumber())
497 || primary.isCallForwarded())
498 .setShouldShowContactPhoto(shouldShowContactPhoto)
499 .setConnectTimeMillis(primary.getConnectTimeMillis())
500 .setIsVoiceMailNumber(primary.isVoiceMailNumber())
501 .setIsRemotelyHeld(primary.isRemotelyHeld())
502 .setIsBusinessNumber(isBusiness)
503 .setSupportsCallOnHold(supports2ndCallOnHold())
504 .setSwapToSecondaryButtonState(getSwapToSecondaryButtonState())
505 .setIsAssistedDialed(primary.isAssistedDialed())
506 .setCustomLabel(null)
507 .setAssistedDialingExtras(primary.getAssistedDialingExtras())
508 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800509
510 InCallActivity activity =
linyuh183cb712017-12-27 17:02:37 -0800511 (InCallActivity) (inCallScreen.getInCallScreenFragment().getActivity());
Eric Erfanianccca3152017-02-22 16:32:36 -0800512 if (activity != null) {
513 activity.onPrimaryCallStateChanged();
514 }
515 }
516 }
517
Eric Erfanian2ca43182017-08-31 06:57:16 -0700518 private @ButtonState int getSwapToSecondaryButtonState() {
linyuh183cb712017-12-27 17:02:37 -0800519 if (secondary == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700520 return ButtonState.NOT_SUPPORT;
521 }
linyuh183cb712017-12-27 17:02:37 -0800522 if (primary.getState() == State.ACTIVE) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700523 return ButtonState.ENABLED;
524 }
525 return ButtonState.DISABLED;
526 }
527
Eric Erfanianccca3152017-02-22 16:32:36 -0800528 /** Only show the conference call button if we can manage the conference. */
529 private void maybeShowManageConferenceCallButton() {
530 getUi().showManageConferenceCallButton(shouldShowManageConference());
531 }
532
533 /**
534 * Determines if the manage conference button should be visible, based on the current primary
535 * call.
536 *
537 * @return {@code True} if the manage conference button should be visible.
538 */
539 private boolean shouldShowManageConference() {
linyuh183cb712017-12-27 17:02:37 -0800540 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800541 return false;
542 }
543
linyuh183cb712017-12-27 17:02:37 -0800544 return primary.can(android.telecom.Call.Details.CAPABILITY_MANAGE_CONFERENCE) && !isFullscreen;
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 }
546
Eric Erfanian2ca43182017-08-31 06:57:16 -0700547 private boolean supports2ndCallOnHold() {
548 DialerCall firstCall = CallList.getInstance().getActiveOrBackgroundCall();
549 DialerCall incomingCall = CallList.getInstance().getIncomingCall();
550 if (firstCall != null && incomingCall != null && firstCall != incomingCall) {
551 return incomingCall.can(Details.CAPABILITY_HOLD);
552 }
553 return true;
554 }
555
Eric Erfanianccca3152017-02-22 16:32:36 -0800556 @Override
557 public void onCallStateButtonClicked() {
linyuh183cb712017-12-27 17:02:37 -0800558 Intent broadcastIntent = Bindings.get(context).getCallStateButtonBroadcastIntent(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800559 if (broadcastIntent != null) {
560 LogUtil.v(
561 "CallCardPresenter.onCallStateButtonClicked",
562 "sending call state button broadcast: " + broadcastIntent);
linyuh183cb712017-12-27 17:02:37 -0800563 context.sendBroadcast(broadcastIntent, Manifest.permission.READ_PHONE_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800564 }
565 }
566
567 @Override
568 public void onManageConferenceClicked() {
569 InCallActivity activity =
linyuh183cb712017-12-27 17:02:37 -0800570 (InCallActivity) (inCallScreen.getInCallScreenFragment().getActivity());
Eric Erfanianccca3152017-02-22 16:32:36 -0800571 activity.showConferenceFragment(true);
572 }
573
574 @Override
575 public void onShrinkAnimationComplete() {
576 InCallPresenter.getInstance().onShrinkAnimationComplete();
577 }
578
Eric Erfanianccca3152017-02-22 16:32:36 -0800579 private void maybeStartSearch(DialerCall call, boolean isPrimary) {
580 // no need to start search for conference calls which show generic info.
581 if (call != null && !call.isConferenceCall()) {
582 startContactInfoSearch(call, isPrimary, call.getState() == DialerCall.State.INCOMING);
583 }
584 }
585
Eric Erfanianccca3152017-02-22 16:32:36 -0800586 /** Starts a query for more contact data for the save primary and secondary calls. */
587 private void startContactInfoSearch(
588 final DialerCall call, final boolean isPrimary, boolean isIncoming) {
linyuh183cb712017-12-27 17:02:37 -0800589 final ContactInfoCache cache = ContactInfoCache.getInstance(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800590
591 cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary));
592 }
593
594 private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) {
595 final boolean entryMatchesExistingCall =
linyuh183cb712017-12-27 17:02:37 -0800596 (isPrimary && primary != null && TextUtils.equals(callId, primary.getId()))
597 || (!isPrimary && secondary != null && TextUtils.equals(callId, secondary.getId()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800598 if (entryMatchesExistingCall) {
599 updateContactEntry(entry, isPrimary);
600 } else {
601 LogUtil.e(
602 "CallCardPresenter.onContactInfoComplete",
603 "dropping stale contact lookup info for " + callId);
604 }
605
606 final DialerCall call = CallList.getInstance().getCallById(callId);
607 if (call != null) {
608 call.getLogState().contactLookupResult = entry.contactLookupResult;
609 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700610 if (entry.lookupUri != null) {
linyuh183cb712017-12-27 17:02:37 -0800611 CallerInfoUtils.sendViewNotification(context, entry.lookupUri);
Eric Erfanianccca3152017-02-22 16:32:36 -0800612 }
613 }
614
615 private void onImageLoadComplete(String callId, ContactCacheEntry entry) {
616 if (getUi() == null) {
617 return;
618 }
619
620 if (entry.photo != null) {
linyuh183cb712017-12-27 17:02:37 -0800621 if (primary != null && callId.equals(primary.getId())) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800622 updateContactEntry(entry, true /* isPrimary */);
linyuh183cb712017-12-27 17:02:37 -0800623 } else if (secondary != null && callId.equals(secondary.getId())) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800624 updateContactEntry(entry, false /* isPrimary */);
625 }
626 }
627 }
628
629 private void updateContactEntry(ContactCacheEntry entry, boolean isPrimary) {
630 if (isPrimary) {
linyuh183cb712017-12-27 17:02:37 -0800631 primaryContactInfo = entry;
Eric Erfanianccca3152017-02-22 16:32:36 -0800632 updatePrimaryDisplayInfo();
633 } else {
linyuh183cb712017-12-27 17:02:37 -0800634 secondaryContactInfo = entry;
Eric Erfanianccca3152017-02-22 16:32:36 -0800635 updateSecondaryDisplayInfo();
636 }
637 }
638
639 /**
640 * Get the highest priority call to display. Goes through the calls and chooses which to return
641 * based on priority of which type of call to display to the user. Callers can use the "ignore"
642 * feature to get the second best call by passing a previously found primary call as ignore.
643 *
644 * @param ignore A call to ignore if found.
645 */
646 private DialerCall getCallToDisplay(
647 CallList callList, DialerCall ignore, boolean skipDisconnected) {
648 // Active calls come second. An active call always gets precedent.
649 DialerCall retval = callList.getActiveCall();
650 if (retval != null && retval != ignore) {
651 return retval;
652 }
653
654 // Sometimes there is intemediate state that two calls are in active even one is about
655 // to be on hold.
656 retval = callList.getSecondActiveCall();
657 if (retval != null && retval != ignore) {
658 return retval;
659 }
660
661 // Disconnected calls get primary position if there are no active calls
662 // to let user know quickly what call has disconnected. Disconnected
663 // calls are very short lived.
664 if (!skipDisconnected) {
665 retval = callList.getDisconnectingCall();
666 if (retval != null && retval != ignore) {
667 return retval;
668 }
669 retval = callList.getDisconnectedCall();
670 if (retval != null && retval != ignore) {
671 return retval;
672 }
673 }
674
675 // Then we go to background call (calls on hold)
676 retval = callList.getBackgroundCall();
677 if (retval != null && retval != ignore) {
678 return retval;
679 }
680
681 // Lastly, we go to a second background call.
682 retval = callList.getSecondBackgroundCall();
683
684 return retval;
685 }
686
687 private void updatePrimaryDisplayInfo() {
linyuh183cb712017-12-27 17:02:37 -0800688 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800689 // TODO: May also occur if search result comes back after ui is destroyed. Look into
690 // removing that case completely.
691 LogUtil.v(
692 "CallCardPresenter.updatePrimaryDisplayInfo",
693 "updatePrimaryDisplayInfo called but ui is null!");
694 return;
695 }
696
linyuh183cb712017-12-27 17:02:37 -0800697 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800698 // Clear the primary display info.
Android Dialerf9439102018-02-21 08:05:14 -0800699 inCallScreen.setPrimary(PrimaryInfo.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 return;
701 }
702
703 // Hide the contact photo if we are in a video call and the incoming video surface is
704 // showing.
705 boolean showContactPhoto =
linyuh183cb712017-12-27 17:02:37 -0800706 !VideoCallPresenter.showIncomingVideo(primary.getVideoState(), primary.getState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800707
708 // DialerCall placed through a work phone account.
linyuh183cb712017-12-27 17:02:37 -0800709 boolean hasWorkCallProperty = primary.hasProperty(PROPERTY_ENTERPRISE_CALL);
Eric Erfanianccca3152017-02-22 16:32:36 -0800710
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700711 MultimediaData multimediaData = null;
linyuh183cb712017-12-27 17:02:37 -0800712 if (primary.getEnrichedCallSession() != null) {
713 multimediaData = primary.getEnrichedCallSession().getMultimediaData();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700714 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800715
linyuh183cb712017-12-27 17:02:37 -0800716 if (primary.isConferenceCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800717 LogUtil.v(
718 "CallCardPresenter.updatePrimaryDisplayInfo",
719 "update primary display info for conference call.");
720
linyuh183cb712017-12-27 17:02:37 -0800721 inCallScreen.setPrimary(
Android Dialerf9439102018-02-21 08:05:14 -0800722 PrimaryInfo.builder()
723 .setName(
724 CallerInfoUtils.getConferenceString(
725 context, primary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)))
726 .setNameIsNumber(false)
727 .setPhotoType(ContactPhotoType.DEFAULT_PLACEHOLDER)
728 .setIsSipCall(false)
729 .setIsContactPhotoShown(showContactPhoto)
730 .setIsWorkCall(hasWorkCallProperty)
731 .setIsSpam(false)
732 .setIsLocalContact(false)
733 .setAnsweringDisconnectsOngoingCall(false)
734 .setShouldShowLocation(shouldShowLocation())
735 .setShowInCallButtonGrid(true)
736 .setNumberPresentation(primary.getNumberPresentation())
737 .build());
linyuh183cb712017-12-27 17:02:37 -0800738 } else if (primaryContactInfo != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800739 LogUtil.v(
740 "CallCardPresenter.updatePrimaryDisplayInfo",
linyuh183cb712017-12-27 17:02:37 -0800741 "update primary display info for " + primaryContactInfo);
Eric Erfanianccca3152017-02-22 16:32:36 -0800742
linyuh183cb712017-12-27 17:02:37 -0800743 String name = getNameForCall(primaryContactInfo);
Eric Erfanianccca3152017-02-22 16:32:36 -0800744 String number;
745
linyuh183cb712017-12-27 17:02:37 -0800746 boolean isChildNumberShown = !TextUtils.isEmpty(primary.getChildNumber());
747 boolean isForwardedNumberShown = !TextUtils.isEmpty(primary.getLastForwardedNumber());
748 boolean isCallSubjectShown = shouldShowCallSubject(primary);
Eric Erfanianccca3152017-02-22 16:32:36 -0800749
750 if (isCallSubjectShown) {
751 number = null;
752 } else if (isChildNumberShown) {
linyuh183cb712017-12-27 17:02:37 -0800753 number = context.getString(R.string.child_number, primary.getChildNumber());
Eric Erfanianccca3152017-02-22 16:32:36 -0800754 } else if (isForwardedNumberShown) {
755 // Use last forwarded number instead of second line, if present.
linyuh183cb712017-12-27 17:02:37 -0800756 number = primary.getLastForwardedNumber();
Eric Erfanianccca3152017-02-22 16:32:36 -0800757 } else {
linyuh183cb712017-12-27 17:02:37 -0800758 number = primaryContactInfo.number;
Eric Erfanianccca3152017-02-22 16:32:36 -0800759 }
760
linyuh183cb712017-12-27 17:02:37 -0800761 boolean nameIsNumber = name != null && name.equals(primaryContactInfo.number);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700762
Eric Erfanianccca3152017-02-22 16:32:36 -0800763 // DialerCall with caller that is a work contact.
linyuh183cb712017-12-27 17:02:37 -0800764 boolean isWorkContact = (primaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
765 inCallScreen.setPrimary(
Android Dialerf9439102018-02-21 08:05:14 -0800766 PrimaryInfo.builder()
767 .setNumber(number)
768 .setName(primary.updateNameIfRestricted(name))
769 .setNameIsNumber(nameIsNumber)
twyencb2a9812018-03-23 18:21:13 -0700770 .setLocation(
Android Dialerf9439102018-02-21 08:05:14 -0800771 shouldShowLocationAsLabel(nameIsNumber, primaryContactInfo.shouldShowLocation)
772 ? primaryContactInfo.location
773 : null)
twyencb2a9812018-03-23 18:21:13 -0700774 .setLabel(isChildNumberShown || isCallSubjectShown ? null : primaryContactInfo.label)
Android Dialerf9439102018-02-21 08:05:14 -0800775 .setPhoto(primaryContactInfo.photo)
776 .setPhotoType(primaryContactInfo.photoType)
777 .setIsSipCall(primaryContactInfo.isSipCall)
778 .setIsContactPhotoShown(showContactPhoto)
779 .setIsWorkCall(hasWorkCallProperty || isWorkContact)
780 .setIsSpam(primary.isSpam())
781 .setIsLocalContact(primaryContactInfo.isLocalContact())
782 .setAnsweringDisconnectsOngoingCall(primary.answeringDisconnectsForegroundVideoCall())
783 .setShouldShowLocation(shouldShowLocation())
784 .setContactInfoLookupKey(primaryContactInfo.lookupKey)
785 .setMultimediaData(multimediaData)
786 .setShowInCallButtonGrid(true)
787 .setNumberPresentation(primary.getNumberPresentation())
788 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800789 } else {
790 // Clear the primary display info.
Android Dialerf9439102018-02-21 08:05:14 -0800791 inCallScreen.setPrimary(PrimaryInfo.empty());
Eric Erfanianccca3152017-02-22 16:32:36 -0800792 }
793
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700794 if (isInCallScreenReady) {
linyuh183cb712017-12-27 17:02:37 -0800795 inCallScreen.showLocationUi(getLocationFragment());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700796 } else {
797 LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location");
798 }
799 }
800
Eric Erfaniand8046e52017-04-06 09:41:50 -0700801 private static boolean shouldShowLocationAsLabel(
802 boolean nameIsNumber, boolean shouldShowLocation) {
803 if (nameIsNumber) {
804 return true;
805 }
806 if (shouldShowLocation) {
807 return true;
808 }
809 return false;
810 }
811
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700812 private Fragment getLocationFragment() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700813 if (!shouldShowLocation()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 return null;
815 }
816 LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment");
linyuh183cb712017-12-27 17:02:37 -0800817 return callLocation.getLocationFragment(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800818 }
819
820 private boolean shouldShowLocation() {
linyuh183cb712017-12-27 17:02:37 -0800821 if (!ConfigProviderBindings.get(context)
wangqi1d62ab22017-11-29 14:29:31 -0800822 .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) {
823 LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config.");
824 return false;
825 }
826 if (!isPotentialEmergencyCall()) {
827 LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location");
828 return false;
829 }
830 if (!hasLocationPermission()) {
831 LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission.");
832 return false;
833 }
834 if (isBatteryTooLowForEmergencyLocation()) {
835 LogUtil.i("CallCardPresenter.getLocationFragment", "low battery.");
836 return false;
837 }
linyuh437ae952018-03-26 12:46:18 -0700838 if (inCallScreen.getInCallScreenFragment().getActivity().isInMultiWindowMode()) {
wangqi1d62ab22017-11-29 14:29:31 -0800839 LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode");
840 return false;
841 }
linyuh183cb712017-12-27 17:02:37 -0800842 if (primary.isVideoCall()) {
wangqi1d62ab22017-11-29 14:29:31 -0800843 LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported");
844 return false;
845 }
linyuh183cb712017-12-27 17:02:37 -0800846 if (!callLocation.canGetLocation(context)) {
wangqi1d62ab22017-11-29 14:29:31 -0800847 LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location");
848 return false;
849 }
850 return true;
851 }
852
853 private boolean isPotentialEmergencyCall() {
linyuh183cb712017-12-27 17:02:37 -0800854 if (isOutgoingEmergencyCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800855 LogUtil.i("CallCardPresenter.shouldShowLocation", "new emergency call");
856 return true;
linyuh183cb712017-12-27 17:02:37 -0800857 } else if (isIncomingEmergencyCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 LogUtil.i("CallCardPresenter.shouldShowLocation", "potential emergency callback");
859 return true;
linyuh183cb712017-12-27 17:02:37 -0800860 } else if (isIncomingEmergencyCall(secondary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800861 LogUtil.i("CallCardPresenter.shouldShowLocation", "has potential emergency callback");
862 return true;
863 }
864 return false;
865 }
866
867 private static boolean isOutgoingEmergencyCall(@Nullable DialerCall call) {
868 return call != null && !call.isIncoming() && call.isEmergencyCall();
869 }
870
871 private static boolean isIncomingEmergencyCall(@Nullable DialerCall call) {
872 return call != null && call.isIncoming() && call.isPotentialEmergencyCallback();
873 }
874
875 private boolean hasLocationPermission() {
linyuh183cb712017-12-27 17:02:37 -0800876 return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
Eric Erfanianccca3152017-02-22 16:32:36 -0800877 == PackageManager.PERMISSION_GRANTED;
878 }
879
880 private boolean isBatteryTooLowForEmergencyLocation() {
881 Intent batteryStatus =
linyuh183cb712017-12-27 17:02:37 -0800882 context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
884 if (status == BatteryManager.BATTERY_STATUS_CHARGING
885 || status == BatteryManager.BATTERY_STATUS_FULL) {
886 // Plugged in or full battery
887 return false;
888 }
889 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
890 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
891 float batteryPercent = (100f * level) / scale;
892 long threshold =
linyuh183cb712017-12-27 17:02:37 -0800893 ConfigProviderBindings.get(context)
Eric Erfanianccca3152017-02-22 16:32:36 -0800894 .getLong(
895 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION,
896 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT);
897 LogUtil.i(
898 "CallCardPresenter.isBatteryTooLowForEmergencyLocation",
899 "percent charged: " + batteryPercent + ", min required charge: " + threshold);
900 return batteryPercent < threshold;
901 }
902
903 private void updateSecondaryDisplayInfo() {
linyuh183cb712017-12-27 17:02:37 -0800904 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800905 return;
906 }
907
linyuh183cb712017-12-27 17:02:37 -0800908 if (secondary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800909 // Clear the secondary display info.
Android Dialer9e335e22018-03-01 08:27:39 -0800910 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800911 return;
912 }
913
linyuh183cb712017-12-27 17:02:37 -0800914 if (secondary.isMergeInProcess()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700915 LogUtil.i(
916 "CallCardPresenter.updateSecondaryDisplayInfo",
917 "secondary call is merge in process, clearing info");
Android Dialer9e335e22018-03-01 08:27:39 -0800918 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700919 return;
920 }
921
linyuh183cb712017-12-27 17:02:37 -0800922 if (secondary.isConferenceCall()) {
923 inCallScreen.setSecondary(
Android Dialer9e335e22018-03-01 08:27:39 -0800924 SecondaryInfo.builder()
925 .setShouldShow(true)
926 .setName(
927 CallerInfoUtils.getConferenceString(
928 context, secondary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)))
929 .setProviderLabel(secondary.getCallProviderLabel())
930 .setIsConference(true)
931 .setIsVideoCall(secondary.isVideoCall())
932 .setIsFullscreen(isFullscreen)
933 .build());
linyuh183cb712017-12-27 17:02:37 -0800934 } else if (secondaryContactInfo != null) {
935 LogUtil.v("CallCardPresenter.updateSecondaryDisplayInfo", "" + secondaryContactInfo);
936 String name = getNameForCall(secondaryContactInfo);
937 boolean nameIsNumber = name != null && name.equals(secondaryContactInfo.number);
938 inCallScreen.setSecondary(
Android Dialer9e335e22018-03-01 08:27:39 -0800939 SecondaryInfo.builder()
940 .setShouldShow(true)
941 .setName(secondary.updateNameIfRestricted(name))
942 .setNameIsNumber(nameIsNumber)
943 .setLabel(secondaryContactInfo.label)
944 .setProviderLabel(secondary.getCallProviderLabel())
945 .setIsVideoCall(secondary.isVideoCall())
946 .setIsFullscreen(isFullscreen)
947 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800948 } else {
949 // Clear the secondary display info.
Android Dialer9e335e22018-03-01 08:27:39 -0800950 inCallScreen.setSecondary(SecondaryInfo.builder().setIsFullscreen(isFullscreen).build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800951 }
952 }
953
954 /** Returns the gateway number for any existing outgoing call. */
955 private String getGatewayNumber() {
956 if (hasOutgoingGatewayCall()) {
linyuh183cb712017-12-27 17:02:37 -0800957 return DialerCall.getNumberFromHandle(primary.getGatewayInfo().getGatewayAddress());
Eric Erfanianccca3152017-02-22 16:32:36 -0800958 }
959 return null;
960 }
961
962 /**
963 * Returns the label (line of text above the number/name) for any given call. For example,
964 * "calling via [Account/Google Voice]" for outgoing calls.
965 */
966 private String getConnectionLabel() {
linyuh183cb712017-12-27 17:02:37 -0800967 if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800968 != PackageManager.PERMISSION_GRANTED) {
969 return null;
970 }
linyuh183cb712017-12-27 17:02:37 -0800971 StatusHints statusHints = primary.getStatusHints();
Eric Erfanianccca3152017-02-22 16:32:36 -0800972 if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
973 return statusHints.getLabel().toString();
974 }
975
976 if (hasOutgoingGatewayCall() && getUi() != null) {
977 // Return the label for the gateway app on outgoing calls.
linyuh183cb712017-12-27 17:02:37 -0800978 final PackageManager pm = context.getPackageManager();
Eric Erfanianccca3152017-02-22 16:32:36 -0800979 try {
980 ApplicationInfo info =
linyuh183cb712017-12-27 17:02:37 -0800981 pm.getApplicationInfo(primary.getGatewayInfo().getGatewayProviderPackageName(), 0);
Eric Erfanianccca3152017-02-22 16:32:36 -0800982 return pm.getApplicationLabel(info).toString();
983 } catch (PackageManager.NameNotFoundException e) {
984 LogUtil.e("CallCardPresenter.getConnectionLabel", "gateway Application Not Found.", e);
985 return null;
986 }
987 }
linyuh183cb712017-12-27 17:02:37 -0800988 return primary.getCallProviderLabel();
Eric Erfanianccca3152017-02-22 16:32:36 -0800989 }
990
twyen4fde0ac2018-03-22 18:04:23 -0700991 @Nullable
992 private SuggestionProvider.Reason getSimSuggestionReason() {
993 String value =
994 primary.getIntentExtras().getString(SuggestionProvider.EXTRA_SIM_SUGGESTION_REASON);
995 if (value == null) {
996 return null;
997 }
998 try {
999 return SuggestionProvider.Reason.valueOf(value);
1000 } catch (IllegalArgumentException e) {
1001 LogUtil.e("CallCardPresenter.getConnectionLabel", "unknown reason " + value);
1002 return null;
1003 }
1004 }
1005
Eric Erfanianccca3152017-02-22 16:32:36 -08001006 private Drawable getCallStateIcon() {
1007 // Return connection icon if one exists.
linyuh183cb712017-12-27 17:02:37 -08001008 StatusHints statusHints = primary.getStatusHints();
Eric Erfanianccca3152017-02-22 16:32:36 -08001009 if (statusHints != null && statusHints.getIcon() != null) {
linyuh183cb712017-12-27 17:02:37 -08001010 Drawable icon = statusHints.getIcon().loadDrawable(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001011 if (icon != null) {
1012 return icon;
1013 }
1014 }
1015
1016 return null;
1017 }
1018
1019 private boolean hasOutgoingGatewayCall() {
1020 // We only display the gateway information while STATE_DIALING so return false for any other
1021 // call state.
1022 // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
1023 // is also called after a contact search completes (call is not present yet). Split the
1024 // UI update so it can receive independent updates.
linyuh183cb712017-12-27 17:02:37 -08001025 if (primary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001026 return false;
1027 }
linyuh183cb712017-12-27 17:02:37 -08001028 return DialerCall.State.isDialing(primary.getState())
1029 && primary.getGatewayInfo() != null
1030 && !primary.getGatewayInfo().isEmpty();
Eric Erfanianccca3152017-02-22 16:32:36 -08001031 }
1032
1033 /** Gets the name to display for the call. */
Eric Erfanian2ca43182017-08-31 06:57:16 -07001034 private String getNameForCall(ContactCacheEntry contactInfo) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 String preferredName =
1036 ContactDisplayUtils.getPreferredDisplayName(
linyuh183cb712017-12-27 17:02:37 -08001037 contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
Eric Erfanianccca3152017-02-22 16:32:36 -08001038 if (TextUtils.isEmpty(preferredName)) {
1039 return contactInfo.number;
1040 }
1041 return preferredName;
1042 }
1043
Eric Erfanianccca3152017-02-22 16:32:36 -08001044 @Override
1045 public void onSecondaryInfoClicked() {
linyuh183cb712017-12-27 17:02:37 -08001046 if (secondary == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001047 LogUtil.e(
1048 "CallCardPresenter.onSecondaryInfoClicked",
1049 "secondary info clicked but no secondary call.");
1050 return;
1051 }
1052
linyuh183cb712017-12-27 17:02:37 -08001053 Logger.get(context)
Eric Erfanian2ca43182017-08-31 06:57:16 -07001054 .logCallImpression(
1055 DialerImpression.Type.IN_CALL_SWAP_SECONDARY_BUTTON_PRESSED,
linyuh183cb712017-12-27 17:02:37 -08001056 primary.getUniqueCallId(),
1057 primary.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -08001058 LogUtil.i(
linyuh183cb712017-12-27 17:02:37 -08001059 "CallCardPresenter.onSecondaryInfoClicked", "swapping call to foreground: " + secondary);
1060 secondary.unhold();
Eric Erfanianccca3152017-02-22 16:32:36 -08001061 }
1062
1063 @Override
1064 public void onEndCallClicked() {
linyuh183cb712017-12-27 17:02:37 -08001065 LogUtil.i("CallCardPresenter.onEndCallClicked", "disconnecting call: " + primary);
1066 if (primary != null) {
1067 primary.disconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -08001068 }
linyuh183cb712017-12-27 17:02:37 -08001069 PostCall.onDisconnectPressed(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001070 }
1071
1072 /**
1073 * Handles a change to the fullscreen mode of the in-call UI.
1074 *
1075 * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1076 */
1077 @Override
1078 public void onFullscreenModeChanged(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001079 isFullscreen = isFullscreenMode;
1080 if (inCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001081 return;
1082 }
1083 maybeShowManageConferenceCallButton();
1084 }
1085
1086 private boolean isPrimaryCallActive() {
linyuh183cb712017-12-27 17:02:37 -08001087 return primary != null && primary.getState() == DialerCall.State.ACTIVE;
Eric Erfanianccca3152017-02-22 16:32:36 -08001088 }
1089
Eric Erfanianccca3152017-02-22 16:32:36 -08001090 private boolean shouldShowEndCallButton(DialerCall primary, int callState) {
1091 if (primary == null) {
1092 return false;
1093 }
1094 if ((!DialerCall.State.isConnectingOrConnected(callState)
1095 && callState != DialerCall.State.DISCONNECTING
1096 && callState != DialerCall.State.DISCONNECTED)
1097 || callState == DialerCall.State.INCOMING) {
1098 return false;
1099 }
linyuh183cb712017-12-27 17:02:37 -08001100 if (this.primary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001101 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001102 return false;
1103 }
1104 return true;
1105 }
1106
1107 @Override
1108 public void onInCallScreenResumed() {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001109 updatePrimaryDisplayInfo();
1110
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 if (shouldSendAccessibilityEvent) {
1112 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1113 }
1114 }
1115
Eric Erfaniand8046e52017-04-06 09:41:50 -07001116 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001117 public void onInCallScreenPaused() {}
Eric Erfaniand8046e52017-04-06 09:41:50 -07001118
Eric Erfanianccca3152017-02-22 16:32:36 -08001119 static boolean sendAccessibilityEvent(Context context, InCallScreen inCallScreen) {
1120 AccessibilityManager am =
1121 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
1122 if (!am.isEnabled()) {
1123 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "accessibility is off");
1124 return false;
1125 }
1126 if (inCallScreen == null) {
1127 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "incallscreen is null");
1128 return false;
1129 }
1130 Fragment fragment = inCallScreen.getInCallScreenFragment();
1131 if (fragment == null || fragment.getView() == null || fragment.getView().getParent() == null) {
1132 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "fragment/view/parent is null");
1133 return false;
1134 }
1135
1136 DisplayManager displayManager =
1137 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
1138 Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
1139 boolean screenIsOn = display.getState() == Display.STATE_ON;
1140 LogUtil.d("CallCardPresenter.sendAccessibilityEvent", "screen is on: %b", screenIsOn);
1141 if (!screenIsOn) {
1142 return false;
1143 }
1144
1145 AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
1146 inCallScreen.dispatchPopulateAccessibilityEvent(event);
1147 View view = inCallScreen.getInCallScreenFragment().getView();
1148 view.getParent().requestSendAccessibilityEvent(view, event);
1149 return true;
1150 }
1151
1152 private void maybeSendAccessibilityEvent(
1153 InCallState oldState, final InCallState newState, boolean primaryChanged) {
1154 shouldSendAccessibilityEvent = false;
linyuh183cb712017-12-27 17:02:37 -08001155 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001156 return;
1157 }
1158 final AccessibilityManager am =
linyuh183cb712017-12-27 17:02:37 -08001159 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
Eric Erfanianccca3152017-02-22 16:32:36 -08001160 if (!am.isEnabled()) {
1161 return;
1162 }
1163 // Announce the current call if it's new incoming/outgoing call or primary call is changed
1164 // due to switching calls between two ongoing calls (one is on hold).
1165 if ((oldState != InCallState.OUTGOING && newState == InCallState.OUTGOING)
1166 || (oldState != InCallState.INCOMING && newState == InCallState.INCOMING)
1167 || primaryChanged) {
1168 LogUtil.i(
1169 "CallCardPresenter.maybeSendAccessibilityEvent", "schedule accessibility announcement");
1170 shouldSendAccessibilityEvent = true;
1171 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1172 }
1173 }
1174
1175 /**
1176 * Determines whether the call subject should be visible on the UI. For the call subject to be
1177 * visible, the call has to be in an incoming or waiting state, and the subject must not be empty.
1178 *
1179 * @param call The call.
1180 * @return {@code true} if the subject should be shown, {@code false} otherwise.
1181 */
1182 private boolean shouldShowCallSubject(DialerCall call) {
1183 if (call == null) {
1184 return false;
1185 }
1186
1187 boolean isIncomingOrWaiting =
linyuh183cb712017-12-27 17:02:37 -08001188 primary.getState() == DialerCall.State.INCOMING
1189 || primary.getState() == DialerCall.State.CALL_WAITING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001190 return isIncomingOrWaiting
1191 && !TextUtils.isEmpty(call.getCallSubject())
1192 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED
1193 && call.isCallSubjectSupported();
1194 }
1195
1196 /**
1197 * Determines whether the "note sent" toast should be shown. It should be shown for a new outgoing
1198 * call with a subject.
1199 *
1200 * @param call The call
1201 * @return {@code true} if the toast should be shown, {@code false} otherwise.
1202 */
1203 private boolean shouldShowNoteSentToast(DialerCall call) {
1204 return call != null
1205 && hasCallSubject(call)
1206 && (call.getState() == DialerCall.State.DIALING
1207 || call.getState() == DialerCall.State.CONNECTING);
1208 }
1209
1210 private InCallScreen getUi() {
linyuh183cb712017-12-27 17:02:37 -08001211 return inCallScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001212 }
1213
Android Dialerf9439102018-02-21 08:05:14 -08001214 /** Callback for contact lookup. */
Eric Erfanianccca3152017-02-22 16:32:36 -08001215 public static class ContactLookupCallback implements ContactInfoCacheCallback {
1216
linyuh183cb712017-12-27 17:02:37 -08001217 private final WeakReference<CallCardPresenter> callCardPresenter;
1218 private final boolean isPrimary;
Eric Erfanianccca3152017-02-22 16:32:36 -08001219
1220 public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
linyuh183cb712017-12-27 17:02:37 -08001221 this.callCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
1222 this.isPrimary = isPrimary;
Eric Erfanianccca3152017-02-22 16:32:36 -08001223 }
1224
1225 @Override
1226 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
linyuh183cb712017-12-27 17:02:37 -08001227 CallCardPresenter presenter = callCardPresenter.get();
Eric Erfanianccca3152017-02-22 16:32:36 -08001228 if (presenter != null) {
linyuh183cb712017-12-27 17:02:37 -08001229 presenter.onContactInfoComplete(callId, entry, isPrimary);
Eric Erfanianccca3152017-02-22 16:32:36 -08001230 }
1231 }
1232
1233 @Override
1234 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
linyuh183cb712017-12-27 17:02:37 -08001235 CallCardPresenter presenter = callCardPresenter.get();
Eric Erfanianccca3152017-02-22 16:32:36 -08001236 if (presenter != null) {
1237 presenter.onImageLoadComplete(callId, entry);
1238 }
1239 }
1240 }
1241}