blob: d49d556a65202a86a3e68b358fb0d17406ecbb99 [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;
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
115 private final Context mContext;
116 private final Handler handler = new Handler();
117
118 private DialerCall mPrimary;
119 private DialerCall mSecondary;
120 private ContactCacheEntry mPrimaryContactInfo;
121 private ContactCacheEntry mSecondaryContactInfo;
122 @Nullable private ContactsPreferences mContactsPreferences;
123 private boolean mIsFullscreen = false;
124 private InCallScreen mInCallScreen;
125 private boolean isInCallScreenReady;
126 private boolean shouldSendAccessibilityEvent;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700127
128 @NonNull private final CallLocation callLocation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800129 private final Runnable sendAccessibilityEventRunnable =
130 new Runnable() {
131 @Override
132 public void run() {
133 shouldSendAccessibilityEvent = !sendAccessibilityEvent(mContext, getUi());
134 LogUtil.i(
135 "CallCardPresenter.sendAccessibilityEventRunnable",
136 "still should send: %b",
137 shouldSendAccessibilityEvent);
138 if (!shouldSendAccessibilityEvent) {
139 handler.removeCallbacks(this);
140 }
141 }
142 };
143
144 public CallCardPresenter(Context context) {
wangqi385a5a12017-09-28 10:44:54 -0700145 LogUtil.i("CallCardPresenter.constructor", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800146 mContext = Assert.isNotNull(context).getApplicationContext();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700147 callLocation = CallLocationComponent.get(mContext).getCallLocation();
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 }
149
150 private static boolean hasCallSubject(DialerCall call) {
151 return !TextUtils.isEmpty(call.getCallSubject());
152 }
153
154 @Override
155 public void onInCallScreenDelegateInit(InCallScreen inCallScreen) {
156 Assert.isNotNull(inCallScreen);
157 mInCallScreen = inCallScreen;
158 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
159
160 // Call may be null if disconnect happened already.
161 DialerCall call = CallList.getInstance().getFirstCall();
162 if (call != null) {
163 mPrimary = call;
164 if (shouldShowNoteSentToast(mPrimary)) {
165 mInCallScreen.showNoteSentToast();
166 }
167 call.addListener(this);
168
169 // start processing lookups right away.
170 if (!call.isConferenceCall()) {
171 startContactInfoSearch(call, true, call.getState() == DialerCall.State.INCOMING);
172 } else {
173 updateContactEntry(null, true);
174 }
175 }
176
177 onStateChange(null, InCallPresenter.getInstance().getInCallState(), CallList.getInstance());
178 }
179
180 @Override
181 public void onInCallScreenReady() {
wangqi385a5a12017-09-28 10:44:54 -0700182 LogUtil.i("CallCardPresenter.onInCallScreenReady", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800183 Assert.checkState(!isInCallScreenReady);
184 if (mContactsPreferences != null) {
185 mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
186 }
187
Eric Erfanianccca3152017-02-22 16:32:36 -0800188 // Contact search may have completed before ui is ready.
189 if (mPrimaryContactInfo != null) {
190 updatePrimaryDisplayInfo();
191 }
192
193 // Register for call state changes last
194 InCallPresenter.getInstance().addListener(this);
195 InCallPresenter.getInstance().addIncomingCallListener(this);
196 InCallPresenter.getInstance().addDetailsListener(this);
197 InCallPresenter.getInstance().addInCallEventListener(this);
198 isInCallScreenReady = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700199
Eric Erfaniand8046e52017-04-06 09:41:50 -0700200 // Log location impressions
201 if (isOutgoingEmergencyCall(mPrimary)) {
202 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_NEW_EMERGENCY_CALL);
203 } else if (isIncomingEmergencyCall(mPrimary) || isIncomingEmergencyCall(mSecondary)) {
204 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_CALLBACK);
205 }
206
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700207 // Showing the location may have been skipped if the UI wasn't ready during previous layout.
208 if (shouldShowLocation()) {
209 updatePrimaryDisplayInfo();
Eric Erfaniand8046e52017-04-06 09:41:50 -0700210
211 // Log location impressions
212 if (!hasLocationPermission()) {
213 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_NO_LOCATION_PERMISSION);
214 } else if (isBatteryTooLowForEmergencyLocation()) {
215 Logger.get(mContext)
216 .logImpression(DialerImpression.Type.EMERGENCY_BATTERY_TOO_LOW_TO_GET_LOCATION);
217 } else if (!callLocation.canGetLocation(mContext)) {
218 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_CANT_GET_LOCATION);
219 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700220 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800221 }
222
223 @Override
224 public void onInCallScreenUnready() {
wangqi385a5a12017-09-28 10:44:54 -0700225 LogUtil.i("CallCardPresenter.onInCallScreenUnready", null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800226 Assert.checkState(isInCallScreenReady);
227
Eric Erfanianccca3152017-02-22 16:32:36 -0800228 // stop getting call state changes
229 InCallPresenter.getInstance().removeListener(this);
230 InCallPresenter.getInstance().removeIncomingCallListener(this);
231 InCallPresenter.getInstance().removeDetailsListener(this);
232 InCallPresenter.getInstance().removeInCallEventListener(this);
233 if (mPrimary != null) {
234 mPrimary.removeListener(this);
235 }
236
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700237 callLocation.close();
238
Eric Erfanianccca3152017-02-22 16:32:36 -0800239 mPrimary = null;
240 mPrimaryContactInfo = null;
241 mSecondaryContactInfo = null;
242 isInCallScreenReady = false;
243 }
244
245 @Override
246 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
247 // same logic should happen as with onStateChange()
248 onStateChange(oldState, newState, CallList.getInstance());
249 }
250
251 @Override
252 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700253 Trace.beginSection("CallCardPresenter.onStateChange");
254 LogUtil.v("CallCardPresenter.onStateChange", "oldState: %s, newState: %s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800255 if (mInCallScreen == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700256 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800257 return;
258 }
259
260 DialerCall primary = null;
261 DialerCall secondary = null;
262
263 if (newState == InCallState.INCOMING) {
264 primary = callList.getIncomingCall();
265 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
266 primary = callList.getOutgoingCall();
267 if (primary == null) {
268 primary = callList.getPendingOutgoingCall();
269 }
270
271 // getCallToDisplay doesn't go through outgoing or incoming calls. It will return the
272 // highest priority call to display as the secondary call.
273 secondary = getCallToDisplay(callList, null, true);
274 } else if (newState == InCallState.INCALL) {
275 primary = getCallToDisplay(callList, null, false);
276 secondary = getCallToDisplay(callList, primary, true);
277 }
278
279 LogUtil.v("CallCardPresenter.onStateChange", "primary call: " + primary);
280 LogUtil.v("CallCardPresenter.onStateChange", "secondary call: " + secondary);
281
282 final boolean primaryChanged =
283 !(DialerCall.areSame(mPrimary, primary) && DialerCall.areSameNumber(mPrimary, primary));
284 final boolean secondaryChanged =
285 !(DialerCall.areSame(mSecondary, secondary)
286 && DialerCall.areSameNumber(mSecondary, secondary));
287
288 mSecondary = secondary;
289 DialerCall previousPrimary = mPrimary;
290 mPrimary = primary;
291
292 if (mPrimary != null) {
293 InCallPresenter.getInstance().onForegroundCallChanged(mPrimary);
294 mInCallScreen.updateInCallScreenColors();
295 }
296
297 if (primaryChanged && shouldShowNoteSentToast(primary)) {
298 mInCallScreen.showNoteSentToast();
299 }
300
301 // Refresh primary call information if either:
302 // 1. Primary call changed.
303 // 2. The call's ability to manage conference has changed.
304 if (shouldRefreshPrimaryInfo(primaryChanged)) {
305 // primary call has changed
306 if (previousPrimary != null) {
307 previousPrimary.removeListener(this);
308 }
309 mPrimary.addListener(this);
310
311 mPrimaryContactInfo =
312 ContactInfoCache.buildCacheEntryFromCall(
313 mContext, mPrimary, mPrimary.getState() == DialerCall.State.INCOMING);
314 updatePrimaryDisplayInfo();
315 maybeStartSearch(mPrimary, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800316 }
317
318 if (previousPrimary != null && mPrimary == null) {
319 previousPrimary.removeListener(this);
320 }
321
322 if (mSecondary == null) {
323 // Secondary call may have ended. Update the ui.
324 mSecondaryContactInfo = null;
325 updateSecondaryDisplayInfo();
326 } else if (secondaryChanged) {
327 // secondary call has changed
328 mSecondaryContactInfo =
329 ContactInfoCache.buildCacheEntryFromCall(
330 mContext, mSecondary, mSecondary.getState() == DialerCall.State.INCOMING);
331 updateSecondaryDisplayInfo();
332 maybeStartSearch(mSecondary, false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800333 }
334
335 // Set the call state
336 int callState = DialerCall.State.IDLE;
337 if (mPrimary != null) {
338 callState = mPrimary.getState();
339 updatePrimaryCallState();
340 } else {
341 getUi().setCallState(PrimaryCallState.createEmptyPrimaryCallState());
342 }
343
344 maybeShowManageConferenceCallButton();
345
346 // Hide the end call button instantly if we're receiving an incoming call.
347 getUi()
348 .setEndCallButtonEnabled(
349 shouldShowEndCallButton(mPrimary, callState),
350 callState != DialerCall.State.INCOMING /* animate */);
351
352 maybeSendAccessibilityEvent(oldState, newState, primaryChanged);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700353 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800354 }
355
356 @Override
357 public void onDetailsChanged(DialerCall call, Details details) {
358 updatePrimaryCallState();
359
360 if (call.can(Details.CAPABILITY_MANAGE_CONFERENCE)
361 != details.can(Details.CAPABILITY_MANAGE_CONFERENCE)) {
362 maybeShowManageConferenceCallButton();
363 }
364 }
365
366 @Override
367 public void onDialerCallDisconnect() {}
368
369 @Override
370 public void onDialerCallUpdate() {
371 // No-op; specific call updates handled elsewhere.
372 }
373
374 @Override
375 public void onWiFiToLteHandover() {}
376
377 @Override
378 public void onHandoverToWifiFailure() {}
379
Eric Erfanianc857f902017-05-15 14:05:33 -0700380 @Override
381 public void onInternationalCallOnWifi() {}
382
Eric Erfanian2ca43182017-08-31 06:57:16 -0700383 @Override
384 public void onEnrichedCallSessionUpdate() {
385 LogUtil.enterBlock("CallCardPresenter.onEnrichedCallSessionUpdate");
386 updatePrimaryDisplayInfo();
387 }
388
Eric Erfanianccca3152017-02-22 16:32:36 -0800389 /** Handles a change to the child number by refreshing the primary call info. */
390 @Override
391 public void onDialerCallChildNumberChange() {
392 LogUtil.v("CallCardPresenter.onDialerCallChildNumberChange", "");
393
394 if (mPrimary == null) {
395 return;
396 }
397 updatePrimaryDisplayInfo();
398 }
399
400 /** Handles a change to the last forwarding number by refreshing the primary call info. */
401 @Override
402 public void onDialerCallLastForwardedNumberChange() {
403 LogUtil.v("CallCardPresenter.onDialerCallLastForwardedNumberChange", "");
404
405 if (mPrimary == null) {
406 return;
407 }
408 updatePrimaryDisplayInfo();
409 updatePrimaryCallState();
410 }
411
412 @Override
413 public void onDialerCallUpgradeToVideo() {}
414
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700415 /** Handles a change to the session modification state for a call. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800416 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700417 public void onDialerCallSessionModificationStateChange() {
418 LogUtil.enterBlock("CallCardPresenter.onDialerCallSessionModificationStateChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800419
420 if (mPrimary == null) {
421 return;
422 }
423 getUi()
424 .setEndCallButtonEnabled(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700425 mPrimary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700426 != SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800427 true /* shouldAnimate */);
428 updatePrimaryCallState();
429 }
430
Eric Erfanianccca3152017-02-22 16:32:36 -0800431 private boolean shouldRefreshPrimaryInfo(boolean primaryChanged) {
432 if (mPrimary == null) {
433 return false;
434 }
435 return primaryChanged
436 || mInCallScreen.isManageConferenceVisible() != shouldShowManageConference();
437 }
438
439 private void updatePrimaryCallState() {
440 if (getUi() != null && mPrimary != null) {
441 boolean isWorkCall =
442 mPrimary.hasProperty(PROPERTY_ENTERPRISE_CALL)
443 || (mPrimaryContactInfo != null
444 && mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
445 boolean isHdAudioCall =
446 isPrimaryCallActive() && mPrimary.hasProperty(Details.PROPERTY_HIGH_DEF_AUDIO);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700447 boolean isAttemptingHdAudioCall =
448 !isHdAudioCall
449 && !mPrimary.hasProperty(DialerCall.PROPERTY_CODEC_KNOWN)
450 && MotorolaUtils.shouldBlinkHdIconWhenConnectingCall(mContext);
451
452 boolean isBusiness = mPrimaryContactInfo != null && mPrimaryContactInfo.isBusiness;
453
Eric Erfanianccca3152017-02-22 16:32:36 -0800454 // Check for video state change and update the visibility of the contact photo. The contact
455 // photo is hidden when the incoming video surface is shown.
456 // The contact photo visibility can also change in setPrimary().
457 boolean shouldShowContactPhoto =
458 !VideoCallPresenter.showIncomingVideo(mPrimary.getVideoState(), mPrimary.getState());
459 getUi()
460 .setCallState(
461 new PrimaryCallState(
462 mPrimary.getState(),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700463 mPrimary.isVideoCall(),
464 mPrimary.getVideoTech().getSessionModificationState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800465 mPrimary.getDisconnectCause(),
466 getConnectionLabel(),
467 getCallStateIcon(),
468 getGatewayNumber(),
469 shouldShowCallSubject(mPrimary) ? mPrimary.getCallSubject() : null,
wangqi97539352017-09-25 11:15:16 -0700470 PhoneNumberHelper.formatNumber(
471 mPrimary.getCallbackNumber(), mPrimary.getSimCountryIso()),
Eric Erfanianccca3152017-02-22 16:32:36 -0800472 mPrimary.hasProperty(Details.PROPERTY_WIFI),
Eric Erfanian83b20212017-05-31 08:53:10 -0700473 mPrimary.isConferenceCall()
474 && !mPrimary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE),
Eric Erfanianccca3152017-02-22 16:32:36 -0800475 isWorkCall,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700476 isAttemptingHdAudioCall,
Eric Erfanianccca3152017-02-22 16:32:36 -0800477 isHdAudioCall,
478 !TextUtils.isEmpty(mPrimary.getLastForwardedNumber()),
479 shouldShowContactPhoto,
480 mPrimary.getConnectTimeMillis(),
481 CallerInfoUtils.isVoiceMailNumber(mContext, mPrimary),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700482 mPrimary.isRemotelyHeld(),
Eric Erfanian2ca43182017-08-31 06:57:16 -0700483 isBusiness,
484 supports2ndCallOnHold(),
485 getSwapToSecondaryButtonState(),
486 mPrimary.isAssistedDialed(),
487 null));
Eric Erfanianccca3152017-02-22 16:32:36 -0800488
489 InCallActivity activity =
490 (InCallActivity) (mInCallScreen.getInCallScreenFragment().getActivity());
491 if (activity != null) {
492 activity.onPrimaryCallStateChanged();
493 }
494 }
495 }
496
Eric Erfanian2ca43182017-08-31 06:57:16 -0700497 private @ButtonState int getSwapToSecondaryButtonState() {
498 if (mSecondary == null) {
499 return ButtonState.NOT_SUPPORT;
500 }
501 if (mPrimary.getState() == State.ACTIVE) {
502 return ButtonState.ENABLED;
503 }
504 return ButtonState.DISABLED;
505 }
506
Eric Erfanianccca3152017-02-22 16:32:36 -0800507 /** Only show the conference call button if we can manage the conference. */
508 private void maybeShowManageConferenceCallButton() {
509 getUi().showManageConferenceCallButton(shouldShowManageConference());
510 }
511
512 /**
513 * Determines if the manage conference button should be visible, based on the current primary
514 * call.
515 *
516 * @return {@code True} if the manage conference button should be visible.
517 */
518 private boolean shouldShowManageConference() {
519 if (mPrimary == null) {
520 return false;
521 }
522
523 return mPrimary.can(android.telecom.Call.Details.CAPABILITY_MANAGE_CONFERENCE)
524 && !mIsFullscreen;
525 }
526
Eric Erfanian2ca43182017-08-31 06:57:16 -0700527 private boolean supports2ndCallOnHold() {
528 DialerCall firstCall = CallList.getInstance().getActiveOrBackgroundCall();
529 DialerCall incomingCall = CallList.getInstance().getIncomingCall();
530 if (firstCall != null && incomingCall != null && firstCall != incomingCall) {
531 return incomingCall.can(Details.CAPABILITY_HOLD);
532 }
533 return true;
534 }
535
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 @Override
537 public void onCallStateButtonClicked() {
538 Intent broadcastIntent = Bindings.get(mContext).getCallStateButtonBroadcastIntent(mContext);
539 if (broadcastIntent != null) {
540 LogUtil.v(
541 "CallCardPresenter.onCallStateButtonClicked",
542 "sending call state button broadcast: " + broadcastIntent);
543 mContext.sendBroadcast(broadcastIntent, Manifest.permission.READ_PHONE_STATE);
544 }
545 }
546
547 @Override
548 public void onManageConferenceClicked() {
549 InCallActivity activity =
550 (InCallActivity) (mInCallScreen.getInCallScreenFragment().getActivity());
551 activity.showConferenceFragment(true);
552 }
553
554 @Override
555 public void onShrinkAnimationComplete() {
556 InCallPresenter.getInstance().onShrinkAnimationComplete();
557 }
558
Eric Erfanianccca3152017-02-22 16:32:36 -0800559 private void maybeStartSearch(DialerCall call, boolean isPrimary) {
560 // no need to start search for conference calls which show generic info.
561 if (call != null && !call.isConferenceCall()) {
562 startContactInfoSearch(call, isPrimary, call.getState() == DialerCall.State.INCOMING);
563 }
564 }
565
Eric Erfanianccca3152017-02-22 16:32:36 -0800566 /** Starts a query for more contact data for the save primary and secondary calls. */
567 private void startContactInfoSearch(
568 final DialerCall call, final boolean isPrimary, boolean isIncoming) {
569 final ContactInfoCache cache = ContactInfoCache.getInstance(mContext);
570
571 cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary));
572 }
573
574 private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) {
575 final boolean entryMatchesExistingCall =
576 (isPrimary && mPrimary != null && TextUtils.equals(callId, mPrimary.getId()))
577 || (!isPrimary && mSecondary != null && TextUtils.equals(callId, mSecondary.getId()));
578 if (entryMatchesExistingCall) {
579 updateContactEntry(entry, isPrimary);
580 } else {
581 LogUtil.e(
582 "CallCardPresenter.onContactInfoComplete",
583 "dropping stale contact lookup info for " + callId);
584 }
585
586 final DialerCall call = CallList.getInstance().getCallById(callId);
587 if (call != null) {
588 call.getLogState().contactLookupResult = entry.contactLookupResult;
589 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700590 if (entry.lookupUri != null) {
591 CallerInfoUtils.sendViewNotification(mContext, entry.lookupUri);
Eric Erfanianccca3152017-02-22 16:32:36 -0800592 }
593 }
594
595 private void onImageLoadComplete(String callId, ContactCacheEntry entry) {
596 if (getUi() == null) {
597 return;
598 }
599
600 if (entry.photo != null) {
601 if (mPrimary != null && callId.equals(mPrimary.getId())) {
602 updateContactEntry(entry, true /* isPrimary */);
603 } else if (mSecondary != null && callId.equals(mSecondary.getId())) {
604 updateContactEntry(entry, false /* isPrimary */);
605 }
606 }
607 }
608
609 private void updateContactEntry(ContactCacheEntry entry, boolean isPrimary) {
610 if (isPrimary) {
611 mPrimaryContactInfo = entry;
612 updatePrimaryDisplayInfo();
613 } else {
614 mSecondaryContactInfo = entry;
615 updateSecondaryDisplayInfo();
616 }
617 }
618
619 /**
620 * Get the highest priority call to display. Goes through the calls and chooses which to return
621 * based on priority of which type of call to display to the user. Callers can use the "ignore"
622 * feature to get the second best call by passing a previously found primary call as ignore.
623 *
624 * @param ignore A call to ignore if found.
625 */
626 private DialerCall getCallToDisplay(
627 CallList callList, DialerCall ignore, boolean skipDisconnected) {
628 // Active calls come second. An active call always gets precedent.
629 DialerCall retval = callList.getActiveCall();
630 if (retval != null && retval != ignore) {
631 return retval;
632 }
633
634 // Sometimes there is intemediate state that two calls are in active even one is about
635 // to be on hold.
636 retval = callList.getSecondActiveCall();
637 if (retval != null && retval != ignore) {
638 return retval;
639 }
640
641 // Disconnected calls get primary position if there are no active calls
642 // to let user know quickly what call has disconnected. Disconnected
643 // calls are very short lived.
644 if (!skipDisconnected) {
645 retval = callList.getDisconnectingCall();
646 if (retval != null && retval != ignore) {
647 return retval;
648 }
649 retval = callList.getDisconnectedCall();
650 if (retval != null && retval != ignore) {
651 return retval;
652 }
653 }
654
655 // Then we go to background call (calls on hold)
656 retval = callList.getBackgroundCall();
657 if (retval != null && retval != ignore) {
658 return retval;
659 }
660
661 // Lastly, we go to a second background call.
662 retval = callList.getSecondBackgroundCall();
663
664 return retval;
665 }
666
667 private void updatePrimaryDisplayInfo() {
668 if (mInCallScreen == null) {
669 // TODO: May also occur if search result comes back after ui is destroyed. Look into
670 // removing that case completely.
671 LogUtil.v(
672 "CallCardPresenter.updatePrimaryDisplayInfo",
673 "updatePrimaryDisplayInfo called but ui is null!");
674 return;
675 }
676
677 if (mPrimary == null) {
678 // Clear the primary display info.
679 mInCallScreen.setPrimary(PrimaryInfo.createEmptyPrimaryInfo());
680 return;
681 }
682
683 // Hide the contact photo if we are in a video call and the incoming video surface is
684 // showing.
685 boolean showContactPhoto =
686 !VideoCallPresenter.showIncomingVideo(mPrimary.getVideoState(), mPrimary.getState());
687
688 // DialerCall placed through a work phone account.
689 boolean hasWorkCallProperty = mPrimary.hasProperty(PROPERTY_ENTERPRISE_CALL);
690
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700691 MultimediaData multimediaData = null;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700692 if (mPrimary.getEnrichedCallSession() != null) {
693 multimediaData = mPrimary.getEnrichedCallSession().getMultimediaData();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700694 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800695
696 if (mPrimary.isConferenceCall()) {
697 LogUtil.v(
698 "CallCardPresenter.updatePrimaryDisplayInfo",
699 "update primary display info for conference call.");
700
701 mInCallScreen.setPrimary(
702 new PrimaryInfo(
703 null /* number */,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700704 CallerInfoUtils.getConferenceString(
705 mContext, mPrimary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)),
Eric Erfanianccca3152017-02-22 16:32:36 -0800706 false /* nameIsNumber */,
707 null /* location */,
708 null /* label */,
Eric Erfanian83b20212017-05-31 08:53:10 -0700709 null /* photo */,
Eric Erfanianccca3152017-02-22 16:32:36 -0800710 ContactPhotoType.DEFAULT_PLACEHOLDER,
711 false /* isSipCall */,
712 showContactPhoto,
713 hasWorkCallProperty,
714 false /* isSpam */,
wangqiae6c8ec2017-09-28 17:39:40 -0700715 false /* isLocalContact */,
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 false /* answeringDisconnectsOngoingCall */,
717 shouldShowLocation(),
718 null /* contactInfoLookupKey */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700719 null /* enrichedCallMultimediaData */,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700720 true /* showInCallButtonGrid */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700721 mPrimary.getNumberPresentation()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800722 } else if (mPrimaryContactInfo != null) {
723 LogUtil.v(
724 "CallCardPresenter.updatePrimaryDisplayInfo",
725 "update primary display info for " + mPrimaryContactInfo);
726
727 String name = getNameForCall(mPrimaryContactInfo);
728 String number;
729
730 boolean isChildNumberShown = !TextUtils.isEmpty(mPrimary.getChildNumber());
731 boolean isForwardedNumberShown = !TextUtils.isEmpty(mPrimary.getLastForwardedNumber());
732 boolean isCallSubjectShown = shouldShowCallSubject(mPrimary);
733
734 if (isCallSubjectShown) {
735 number = null;
736 } else if (isChildNumberShown) {
737 number = mContext.getString(R.string.child_number, mPrimary.getChildNumber());
738 } else if (isForwardedNumberShown) {
739 // Use last forwarded number instead of second line, if present.
740 number = mPrimary.getLastForwardedNumber();
741 } else {
742 number = mPrimaryContactInfo.number;
743 }
744
745 boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700746
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 // DialerCall with caller that is a work contact.
748 boolean isWorkContact = (mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
749 mInCallScreen.setPrimary(
750 new PrimaryInfo(
751 number,
Eric Erfanianc857f902017-05-15 14:05:33 -0700752 mPrimary.updateNameIfRestricted(name),
Eric Erfanianccca3152017-02-22 16:32:36 -0800753 nameIsNumber,
Eric Erfaniand8046e52017-04-06 09:41:50 -0700754 shouldShowLocationAsLabel(nameIsNumber, mPrimaryContactInfo.shouldShowLocation)
755 ? mPrimaryContactInfo.location
756 : null,
Eric Erfanianccca3152017-02-22 16:32:36 -0800757 isChildNumberShown || isCallSubjectShown ? null : mPrimaryContactInfo.label,
758 mPrimaryContactInfo.photo,
759 mPrimaryContactInfo.photoType,
760 mPrimaryContactInfo.isSipCall,
761 showContactPhoto,
762 hasWorkCallProperty || isWorkContact,
763 mPrimary.isSpam(),
wangqiae6c8ec2017-09-28 17:39:40 -0700764 mPrimaryContactInfo.isLocalContact(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800765 mPrimary.answeringDisconnectsForegroundVideoCall(),
766 shouldShowLocation(),
767 mPrimaryContactInfo.lookupKey,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700768 multimediaData,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700769 true /* showInCallButtonGrid */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700770 mPrimary.getNumberPresentation()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800771 } else {
772 // Clear the primary display info.
773 mInCallScreen.setPrimary(PrimaryInfo.createEmptyPrimaryInfo());
774 }
775
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700776 if (isInCallScreenReady) {
777 mInCallScreen.showLocationUi(getLocationFragment());
778 } else {
779 LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location");
780 }
781 }
782
Eric Erfaniand8046e52017-04-06 09:41:50 -0700783 private static boolean shouldShowLocationAsLabel(
784 boolean nameIsNumber, boolean shouldShowLocation) {
785 if (nameIsNumber) {
786 return true;
787 }
788 if (shouldShowLocation) {
789 return true;
790 }
791 return false;
792 }
793
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700794 private Fragment getLocationFragment() {
795 if (!ConfigProviderBindings.get(mContext)
796 .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) {
797 LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config.");
798 return null;
799 }
800 if (!shouldShowLocation()) {
801 LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location");
802 return null;
803 }
804 if (!hasLocationPermission()) {
805 LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission.");
806 return null;
807 }
808 if (isBatteryTooLowForEmergencyLocation()) {
809 LogUtil.i("CallCardPresenter.getLocationFragment", "low battery.");
810 return null;
811 }
812 if (ActivityCompat.isInMultiWindowMode(mInCallScreen.getInCallScreenFragment().getActivity())) {
813 LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode");
814 return null;
815 }
816 if (mPrimary.isVideoCall()) {
817 LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported");
818 return null;
819 }
820 if (!callLocation.canGetLocation(mContext)) {
821 LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location");
822 return null;
823 }
824 LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment");
825 return callLocation.getLocationFragment(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800826 }
827
828 private boolean shouldShowLocation() {
829 if (isOutgoingEmergencyCall(mPrimary)) {
830 LogUtil.i("CallCardPresenter.shouldShowLocation", "new emergency call");
831 return true;
832 } else if (isIncomingEmergencyCall(mPrimary)) {
833 LogUtil.i("CallCardPresenter.shouldShowLocation", "potential emergency callback");
834 return true;
835 } else if (isIncomingEmergencyCall(mSecondary)) {
836 LogUtil.i("CallCardPresenter.shouldShowLocation", "has potential emergency callback");
837 return true;
838 }
839 return false;
840 }
841
842 private static boolean isOutgoingEmergencyCall(@Nullable DialerCall call) {
843 return call != null && !call.isIncoming() && call.isEmergencyCall();
844 }
845
846 private static boolean isIncomingEmergencyCall(@Nullable DialerCall call) {
847 return call != null && call.isIncoming() && call.isPotentialEmergencyCallback();
848 }
849
850 private boolean hasLocationPermission() {
851 return ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
852 == PackageManager.PERMISSION_GRANTED;
853 }
854
855 private boolean isBatteryTooLowForEmergencyLocation() {
856 Intent batteryStatus =
857 mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
858 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
859 if (status == BatteryManager.BATTERY_STATUS_CHARGING
860 || status == BatteryManager.BATTERY_STATUS_FULL) {
861 // Plugged in or full battery
862 return false;
863 }
864 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
865 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
866 float batteryPercent = (100f * level) / scale;
867 long threshold =
868 ConfigProviderBindings.get(mContext)
869 .getLong(
870 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION,
871 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT);
872 LogUtil.i(
873 "CallCardPresenter.isBatteryTooLowForEmergencyLocation",
874 "percent charged: " + batteryPercent + ", min required charge: " + threshold);
875 return batteryPercent < threshold;
876 }
877
878 private void updateSecondaryDisplayInfo() {
879 if (mInCallScreen == null) {
880 return;
881 }
882
883 if (mSecondary == null) {
884 // Clear the secondary display info.
885 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
886 return;
887 }
888
Eric Erfanian2ca43182017-08-31 06:57:16 -0700889 if (mSecondary.isMergeInProcess()) {
890 LogUtil.i(
891 "CallCardPresenter.updateSecondaryDisplayInfo",
892 "secondary call is merge in process, clearing info");
893 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
894 return;
895 }
896
Eric Erfanianccca3152017-02-22 16:32:36 -0800897 if (mSecondary.isConferenceCall()) {
898 mInCallScreen.setSecondary(
899 new SecondaryInfo(
900 true /* show */,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700901 CallerInfoUtils.getConferenceString(
902 mContext, mSecondary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)),
Eric Erfanianccca3152017-02-22 16:32:36 -0800903 false /* nameIsNumber */,
904 null /* label */,
905 mSecondary.getCallProviderLabel(),
906 true /* isConference */,
907 mSecondary.isVideoCall(),
908 mIsFullscreen));
909 } else if (mSecondaryContactInfo != null) {
910 LogUtil.v("CallCardPresenter.updateSecondaryDisplayInfo", "" + mSecondaryContactInfo);
911 String name = getNameForCall(mSecondaryContactInfo);
912 boolean nameIsNumber = name != null && name.equals(mSecondaryContactInfo.number);
913 mInCallScreen.setSecondary(
914 new SecondaryInfo(
915 true /* show */,
Eric Erfanianc857f902017-05-15 14:05:33 -0700916 mSecondary.updateNameIfRestricted(name),
Eric Erfanianccca3152017-02-22 16:32:36 -0800917 nameIsNumber,
918 mSecondaryContactInfo.label,
919 mSecondary.getCallProviderLabel(),
920 false /* isConference */,
921 mSecondary.isVideoCall(),
922 mIsFullscreen));
923 } else {
924 // Clear the secondary display info.
925 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
926 }
927 }
928
929 /** Returns the gateway number for any existing outgoing call. */
930 private String getGatewayNumber() {
931 if (hasOutgoingGatewayCall()) {
932 return DialerCall.getNumberFromHandle(mPrimary.getGatewayInfo().getGatewayAddress());
933 }
934 return null;
935 }
936
937 /**
938 * Returns the label (line of text above the number/name) for any given call. For example,
939 * "calling via [Account/Google Voice]" for outgoing calls.
940 */
941 private String getConnectionLabel() {
942 if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
943 != PackageManager.PERMISSION_GRANTED) {
944 return null;
945 }
946 StatusHints statusHints = mPrimary.getStatusHints();
947 if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
948 return statusHints.getLabel().toString();
949 }
950
951 if (hasOutgoingGatewayCall() && getUi() != null) {
952 // Return the label for the gateway app on outgoing calls.
953 final PackageManager pm = mContext.getPackageManager();
954 try {
955 ApplicationInfo info =
956 pm.getApplicationInfo(mPrimary.getGatewayInfo().getGatewayProviderPackageName(), 0);
957 return pm.getApplicationLabel(info).toString();
958 } catch (PackageManager.NameNotFoundException e) {
959 LogUtil.e("CallCardPresenter.getConnectionLabel", "gateway Application Not Found.", e);
960 return null;
961 }
962 }
963 return mPrimary.getCallProviderLabel();
964 }
965
966 private Drawable getCallStateIcon() {
967 // Return connection icon if one exists.
968 StatusHints statusHints = mPrimary.getStatusHints();
969 if (statusHints != null && statusHints.getIcon() != null) {
970 Drawable icon = statusHints.getIcon().loadDrawable(mContext);
971 if (icon != null) {
972 return icon;
973 }
974 }
975
976 return null;
977 }
978
979 private boolean hasOutgoingGatewayCall() {
980 // We only display the gateway information while STATE_DIALING so return false for any other
981 // call state.
982 // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
983 // is also called after a contact search completes (call is not present yet). Split the
984 // UI update so it can receive independent updates.
985 if (mPrimary == null) {
986 return false;
987 }
988 return DialerCall.State.isDialing(mPrimary.getState())
989 && mPrimary.getGatewayInfo() != null
990 && !mPrimary.getGatewayInfo().isEmpty();
991 }
992
993 /** Gets the name to display for the call. */
Eric Erfanian2ca43182017-08-31 06:57:16 -0700994 private String getNameForCall(ContactCacheEntry contactInfo) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800995 String preferredName =
996 ContactDisplayUtils.getPreferredDisplayName(
997 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
998 if (TextUtils.isEmpty(preferredName)) {
999 return contactInfo.number;
1000 }
1001 return preferredName;
1002 }
1003
Eric Erfanianccca3152017-02-22 16:32:36 -08001004 @Override
1005 public void onSecondaryInfoClicked() {
1006 if (mSecondary == null) {
1007 LogUtil.e(
1008 "CallCardPresenter.onSecondaryInfoClicked",
1009 "secondary info clicked but no secondary call.");
1010 return;
1011 }
1012
Eric Erfanian2ca43182017-08-31 06:57:16 -07001013 Logger.get(mContext)
1014 .logCallImpression(
1015 DialerImpression.Type.IN_CALL_SWAP_SECONDARY_BUTTON_PRESSED,
1016 mPrimary.getUniqueCallId(),
1017 mPrimary.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -08001018 LogUtil.i(
1019 "CallCardPresenter.onSecondaryInfoClicked", "swapping call to foreground: " + mSecondary);
1020 mSecondary.unhold();
1021 }
1022
1023 @Override
1024 public void onEndCallClicked() {
1025 LogUtil.i("CallCardPresenter.onEndCallClicked", "disconnecting call: " + mPrimary);
1026 if (mPrimary != null) {
1027 mPrimary.disconnect();
1028 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001029 PostCall.onDisconnectPressed(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -08001030 }
1031
1032 /**
1033 * Handles a change to the fullscreen mode of the in-call UI.
1034 *
1035 * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1036 */
1037 @Override
1038 public void onFullscreenModeChanged(boolean isFullscreenMode) {
1039 mIsFullscreen = isFullscreenMode;
1040 if (mInCallScreen == null) {
1041 return;
1042 }
1043 maybeShowManageConferenceCallButton();
1044 }
1045
1046 private boolean isPrimaryCallActive() {
1047 return mPrimary != null && mPrimary.getState() == DialerCall.State.ACTIVE;
1048 }
1049
Eric Erfanianccca3152017-02-22 16:32:36 -08001050 private boolean shouldShowEndCallButton(DialerCall primary, int callState) {
1051 if (primary == null) {
1052 return false;
1053 }
1054 if ((!DialerCall.State.isConnectingOrConnected(callState)
1055 && callState != DialerCall.State.DISCONNECTING
1056 && callState != DialerCall.State.DISCONNECTED)
1057 || callState == DialerCall.State.INCOMING) {
1058 return false;
1059 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001060 if (mPrimary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001061 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001062 return false;
1063 }
1064 return true;
1065 }
1066
1067 @Override
1068 public void onInCallScreenResumed() {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001069 updatePrimaryDisplayInfo();
1070
Eric Erfanianccca3152017-02-22 16:32:36 -08001071 if (shouldSendAccessibilityEvent) {
1072 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1073 }
1074 }
1075
Eric Erfaniand8046e52017-04-06 09:41:50 -07001076 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001077 public void onInCallScreenPaused() {}
Eric Erfaniand8046e52017-04-06 09:41:50 -07001078
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 static boolean sendAccessibilityEvent(Context context, InCallScreen inCallScreen) {
1080 AccessibilityManager am =
1081 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
1082 if (!am.isEnabled()) {
1083 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "accessibility is off");
1084 return false;
1085 }
1086 if (inCallScreen == null) {
1087 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "incallscreen is null");
1088 return false;
1089 }
1090 Fragment fragment = inCallScreen.getInCallScreenFragment();
1091 if (fragment == null || fragment.getView() == null || fragment.getView().getParent() == null) {
1092 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "fragment/view/parent is null");
1093 return false;
1094 }
1095
1096 DisplayManager displayManager =
1097 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
1098 Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
1099 boolean screenIsOn = display.getState() == Display.STATE_ON;
1100 LogUtil.d("CallCardPresenter.sendAccessibilityEvent", "screen is on: %b", screenIsOn);
1101 if (!screenIsOn) {
1102 return false;
1103 }
1104
1105 AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
1106 inCallScreen.dispatchPopulateAccessibilityEvent(event);
1107 View view = inCallScreen.getInCallScreenFragment().getView();
1108 view.getParent().requestSendAccessibilityEvent(view, event);
1109 return true;
1110 }
1111
1112 private void maybeSendAccessibilityEvent(
1113 InCallState oldState, final InCallState newState, boolean primaryChanged) {
1114 shouldSendAccessibilityEvent = false;
1115 if (mContext == null) {
1116 return;
1117 }
1118 final AccessibilityManager am =
1119 (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
1120 if (!am.isEnabled()) {
1121 return;
1122 }
1123 // Announce the current call if it's new incoming/outgoing call or primary call is changed
1124 // due to switching calls between two ongoing calls (one is on hold).
1125 if ((oldState != InCallState.OUTGOING && newState == InCallState.OUTGOING)
1126 || (oldState != InCallState.INCOMING && newState == InCallState.INCOMING)
1127 || primaryChanged) {
1128 LogUtil.i(
1129 "CallCardPresenter.maybeSendAccessibilityEvent", "schedule accessibility announcement");
1130 shouldSendAccessibilityEvent = true;
1131 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1132 }
1133 }
1134
1135 /**
1136 * Determines whether the call subject should be visible on the UI. For the call subject to be
1137 * visible, the call has to be in an incoming or waiting state, and the subject must not be empty.
1138 *
1139 * @param call The call.
1140 * @return {@code true} if the subject should be shown, {@code false} otherwise.
1141 */
1142 private boolean shouldShowCallSubject(DialerCall call) {
1143 if (call == null) {
1144 return false;
1145 }
1146
1147 boolean isIncomingOrWaiting =
1148 mPrimary.getState() == DialerCall.State.INCOMING
1149 || mPrimary.getState() == DialerCall.State.CALL_WAITING;
1150 return isIncomingOrWaiting
1151 && !TextUtils.isEmpty(call.getCallSubject())
1152 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED
1153 && call.isCallSubjectSupported();
1154 }
1155
1156 /**
1157 * Determines whether the "note sent" toast should be shown. It should be shown for a new outgoing
1158 * call with a subject.
1159 *
1160 * @param call The call
1161 * @return {@code true} if the toast should be shown, {@code false} otherwise.
1162 */
1163 private boolean shouldShowNoteSentToast(DialerCall call) {
1164 return call != null
1165 && hasCallSubject(call)
1166 && (call.getState() == DialerCall.State.DIALING
1167 || call.getState() == DialerCall.State.CONNECTING);
1168 }
1169
1170 private InCallScreen getUi() {
1171 return mInCallScreen;
1172 }
1173
1174 public static class ContactLookupCallback implements ContactInfoCacheCallback {
1175
1176 private final WeakReference<CallCardPresenter> mCallCardPresenter;
1177 private final boolean mIsPrimary;
1178
1179 public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
1180 mCallCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
1181 mIsPrimary = isPrimary;
1182 }
1183
1184 @Override
1185 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1186 CallCardPresenter presenter = mCallCardPresenter.get();
1187 if (presenter != null) {
1188 presenter.onContactInfoComplete(callId, entry, mIsPrimary);
1189 }
1190 }
1191
1192 @Override
1193 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1194 CallCardPresenter presenter = mCallCardPresenter.get();
1195 if (presenter != null) {
1196 presenter.onImageLoadComplete(callId, entry);
1197 }
1198 }
1199 }
1200}