blob: a51f54edb1e775e0415b2175ed3024df45afb12f [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 Erfaniand5e47f62017-03-15 14:41:07 -070031import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.support.annotation.Nullable;
33import android.support.v4.app.Fragment;
34import android.support.v4.content.ContextCompat;
35import android.telecom.Call.Details;
36import android.telecom.StatusHints;
37import android.telecom.TelecomManager;
38import android.text.TextUtils;
39import android.view.Display;
40import android.view.View;
41import android.view.accessibility.AccessibilityEvent;
42import android.view.accessibility.AccessibilityManager;
43import com.android.contacts.common.ContactsUtils;
44import com.android.contacts.common.preference.ContactsPreferences;
45import com.android.contacts.common.util.ContactDisplayUtils;
46import com.android.dialer.common.Assert;
47import com.android.dialer.common.ConfigProviderBindings;
48import com.android.dialer.common.LogUtil;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070049import com.android.dialer.compat.ActivityCompat;
50import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import com.android.dialer.enrichedcall.EnrichedCallManager;
52import com.android.dialer.enrichedcall.Session;
Eric Erfanian8369df02017-05-03 10:27:13 -070053import com.android.dialer.logging.DialerImpression;
Eric Erfaniand8046e52017-04-06 09:41:50 -070054import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080055import com.android.dialer.multimedia.MultimediaData;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070056import com.android.dialer.oem.MotorolaUtils;
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 Erfanianccca3152017-02-22 16:32:36 -080066import com.android.incallui.call.DialerCallListener;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070067import com.android.incallui.calllocation.CallLocation;
68import com.android.incallui.calllocation.CallLocationComponent;
Eric Erfanianccca3152017-02-22 16:32:36 -080069import com.android.incallui.incall.protocol.ContactPhotoType;
70import com.android.incallui.incall.protocol.InCallScreen;
71import com.android.incallui.incall.protocol.InCallScreenDelegate;
72import com.android.incallui.incall.protocol.PrimaryCallState;
73import com.android.incallui.incall.protocol.PrimaryInfo;
74import com.android.incallui.incall.protocol.SecondaryInfo;
Eric Erfanian90508232017-03-24 09:31:16 -070075import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfanianccca3152017-02-22 16:32:36 -080076import java.lang.ref.WeakReference;
77
78/**
79 * Controller for the Call Card Fragment. This class listens for changes to InCallState and passes
80 * it along to the fragment.
81 */
82public class CallCardPresenter
83 implements InCallStateListener,
84 IncomingCallListener,
85 InCallDetailsListener,
86 InCallEventListener,
87 InCallScreenDelegate,
88 DialerCallListener,
89 EnrichedCallManager.StateChangedListener {
90
91 /**
92 * Amount of time to wait before sending an announcement via the accessibility manager. When the
93 * call state changes to an outgoing or incoming state for the first time, the UI can often be
94 * changing due to call updates or contact lookup. This allows the UI to settle to a stable state
95 * to ensure that the correct information is announced.
96 */
97 private static final long ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS = 500;
98
99 /** Flag to allow the user's current location to be shown during emergency calls. */
100 private static final String CONFIG_ENABLE_EMERGENCY_LOCATION = "config_enable_emergency_location";
101
102 private static final boolean CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT = true;
103
104 /**
105 * Make it possible to not get location during an emergency call if the battery is too low, since
106 * doing so could trigger gps and thus potentially cause the phone to die in the middle of the
107 * call.
108 */
109 private static final String CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION =
110 "min_battery_percent_for_emergency_location";
111
112 private static final long CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT = 10;
113
114 private final Context mContext;
115 private final Handler handler = new Handler();
116
117 private DialerCall mPrimary;
118 private DialerCall mSecondary;
119 private ContactCacheEntry mPrimaryContactInfo;
120 private ContactCacheEntry mSecondaryContactInfo;
121 @Nullable private ContactsPreferences mContactsPreferences;
122 private boolean mIsFullscreen = false;
123 private InCallScreen mInCallScreen;
124 private boolean isInCallScreenReady;
125 private boolean shouldSendAccessibilityEvent;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700126
127 @NonNull private final CallLocation callLocation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800128 private final Runnable sendAccessibilityEventRunnable =
129 new Runnable() {
130 @Override
131 public void run() {
132 shouldSendAccessibilityEvent = !sendAccessibilityEvent(mContext, getUi());
133 LogUtil.i(
134 "CallCardPresenter.sendAccessibilityEventRunnable",
135 "still should send: %b",
136 shouldSendAccessibilityEvent);
137 if (!shouldSendAccessibilityEvent) {
138 handler.removeCallbacks(this);
139 }
140 }
141 };
142
143 public CallCardPresenter(Context context) {
144 LogUtil.i("CallCardController.constructor", null);
145 mContext = Assert.isNotNull(context).getApplicationContext();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700146 callLocation = CallLocationComponent.get(mContext).getCallLocation();
Eric Erfanianccca3152017-02-22 16:32:36 -0800147 }
148
149 private static boolean hasCallSubject(DialerCall call) {
150 return !TextUtils.isEmpty(call.getCallSubject());
151 }
152
153 @Override
154 public void onInCallScreenDelegateInit(InCallScreen inCallScreen) {
155 Assert.isNotNull(inCallScreen);
156 mInCallScreen = inCallScreen;
157 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
158
159 // Call may be null if disconnect happened already.
160 DialerCall call = CallList.getInstance().getFirstCall();
161 if (call != null) {
162 mPrimary = call;
163 if (shouldShowNoteSentToast(mPrimary)) {
164 mInCallScreen.showNoteSentToast();
165 }
166 call.addListener(this);
167
168 // start processing lookups right away.
169 if (!call.isConferenceCall()) {
170 startContactInfoSearch(call, true, call.getState() == DialerCall.State.INCOMING);
171 } else {
172 updateContactEntry(null, true);
173 }
174 }
175
176 onStateChange(null, InCallPresenter.getInstance().getInCallState(), CallList.getInstance());
177 }
178
179 @Override
180 public void onInCallScreenReady() {
181 LogUtil.i("CallCardController.onInCallScreenReady", null);
182 Assert.checkState(!isInCallScreenReady);
183 if (mContactsPreferences != null) {
184 mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
185 }
186
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 // Contact search may have completed before ui is ready.
188 if (mPrimaryContactInfo != null) {
189 updatePrimaryDisplayInfo();
190 }
191
192 // Register for call state changes last
193 InCallPresenter.getInstance().addListener(this);
194 InCallPresenter.getInstance().addIncomingCallListener(this);
195 InCallPresenter.getInstance().addDetailsListener(this);
196 InCallPresenter.getInstance().addInCallEventListener(this);
197 isInCallScreenReady = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700198
Eric Erfaniand8046e52017-04-06 09:41:50 -0700199 // Log location impressions
200 if (isOutgoingEmergencyCall(mPrimary)) {
201 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_NEW_EMERGENCY_CALL);
202 } else if (isIncomingEmergencyCall(mPrimary) || isIncomingEmergencyCall(mSecondary)) {
203 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_CALLBACK);
204 }
205
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700206 // Showing the location may have been skipped if the UI wasn't ready during previous layout.
207 if (shouldShowLocation()) {
208 updatePrimaryDisplayInfo();
Eric Erfaniand8046e52017-04-06 09:41:50 -0700209
210 // Log location impressions
211 if (!hasLocationPermission()) {
212 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_NO_LOCATION_PERMISSION);
213 } else if (isBatteryTooLowForEmergencyLocation()) {
214 Logger.get(mContext)
215 .logImpression(DialerImpression.Type.EMERGENCY_BATTERY_TOO_LOW_TO_GET_LOCATION);
216 } else if (!callLocation.canGetLocation(mContext)) {
217 Logger.get(mContext).logImpression(DialerImpression.Type.EMERGENCY_CANT_GET_LOCATION);
218 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700219 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800220 }
221
222 @Override
223 public void onInCallScreenUnready() {
224 LogUtil.i("CallCardController.onInCallScreenUnready", null);
225 Assert.checkState(isInCallScreenReady);
226
Eric Erfanianccca3152017-02-22 16:32:36 -0800227 // stop getting call state changes
228 InCallPresenter.getInstance().removeListener(this);
229 InCallPresenter.getInstance().removeIncomingCallListener(this);
230 InCallPresenter.getInstance().removeDetailsListener(this);
231 InCallPresenter.getInstance().removeInCallEventListener(this);
232 if (mPrimary != null) {
233 mPrimary.removeListener(this);
234 }
235
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700236 callLocation.close();
237
Eric Erfanianccca3152017-02-22 16:32:36 -0800238 mPrimary = null;
239 mPrimaryContactInfo = null;
240 mSecondaryContactInfo = null;
241 isInCallScreenReady = false;
242 }
243
244 @Override
245 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
246 // same logic should happen as with onStateChange()
247 onStateChange(oldState, newState, CallList.getInstance());
248 }
249
250 @Override
251 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
252 LogUtil.v("CallCardPresenter.onStateChange", "" + newState);
253 if (mInCallScreen == null) {
254 return;
255 }
256
257 DialerCall primary = null;
258 DialerCall secondary = null;
259
260 if (newState == InCallState.INCOMING) {
261 primary = callList.getIncomingCall();
262 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
263 primary = callList.getOutgoingCall();
264 if (primary == null) {
265 primary = callList.getPendingOutgoingCall();
266 }
267
268 // getCallToDisplay doesn't go through outgoing or incoming calls. It will return the
269 // highest priority call to display as the secondary call.
270 secondary = getCallToDisplay(callList, null, true);
271 } else if (newState == InCallState.INCALL) {
272 primary = getCallToDisplay(callList, null, false);
273 secondary = getCallToDisplay(callList, primary, true);
274 }
275
276 LogUtil.v("CallCardPresenter.onStateChange", "primary call: " + primary);
277 LogUtil.v("CallCardPresenter.onStateChange", "secondary call: " + secondary);
278
279 final boolean primaryChanged =
280 !(DialerCall.areSame(mPrimary, primary) && DialerCall.areSameNumber(mPrimary, primary));
281 final boolean secondaryChanged =
282 !(DialerCall.areSame(mSecondary, secondary)
283 && DialerCall.areSameNumber(mSecondary, secondary));
284
285 mSecondary = secondary;
286 DialerCall previousPrimary = mPrimary;
287 mPrimary = primary;
288
289 if (mPrimary != null) {
290 InCallPresenter.getInstance().onForegroundCallChanged(mPrimary);
291 mInCallScreen.updateInCallScreenColors();
292 }
293
294 if (primaryChanged && shouldShowNoteSentToast(primary)) {
295 mInCallScreen.showNoteSentToast();
296 }
297
298 // Refresh primary call information if either:
299 // 1. Primary call changed.
300 // 2. The call's ability to manage conference has changed.
301 if (shouldRefreshPrimaryInfo(primaryChanged)) {
302 // primary call has changed
303 if (previousPrimary != null) {
304 previousPrimary.removeListener(this);
305 }
306 mPrimary.addListener(this);
307
308 mPrimaryContactInfo =
309 ContactInfoCache.buildCacheEntryFromCall(
310 mContext, mPrimary, mPrimary.getState() == DialerCall.State.INCOMING);
311 updatePrimaryDisplayInfo();
312 maybeStartSearch(mPrimary, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800313 }
314
315 if (previousPrimary != null && mPrimary == null) {
316 previousPrimary.removeListener(this);
317 }
318
319 if (mSecondary == null) {
320 // Secondary call may have ended. Update the ui.
321 mSecondaryContactInfo = null;
322 updateSecondaryDisplayInfo();
323 } else if (secondaryChanged) {
324 // secondary call has changed
325 mSecondaryContactInfo =
326 ContactInfoCache.buildCacheEntryFromCall(
327 mContext, mSecondary, mSecondary.getState() == DialerCall.State.INCOMING);
328 updateSecondaryDisplayInfo();
329 maybeStartSearch(mSecondary, false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 }
331
332 // Set the call state
333 int callState = DialerCall.State.IDLE;
334 if (mPrimary != null) {
335 callState = mPrimary.getState();
336 updatePrimaryCallState();
337 } else {
338 getUi().setCallState(PrimaryCallState.createEmptyPrimaryCallState());
339 }
340
341 maybeShowManageConferenceCallButton();
342
343 // Hide the end call button instantly if we're receiving an incoming call.
344 getUi()
345 .setEndCallButtonEnabled(
346 shouldShowEndCallButton(mPrimary, callState),
347 callState != DialerCall.State.INCOMING /* animate */);
348
349 maybeSendAccessibilityEvent(oldState, newState, primaryChanged);
350 }
351
352 @Override
353 public void onDetailsChanged(DialerCall call, Details details) {
354 updatePrimaryCallState();
355
356 if (call.can(Details.CAPABILITY_MANAGE_CONFERENCE)
357 != details.can(Details.CAPABILITY_MANAGE_CONFERENCE)) {
358 maybeShowManageConferenceCallButton();
359 }
360 }
361
362 @Override
363 public void onDialerCallDisconnect() {}
364
365 @Override
366 public void onDialerCallUpdate() {
367 // No-op; specific call updates handled elsewhere.
368 }
369
370 @Override
371 public void onWiFiToLteHandover() {}
372
373 @Override
374 public void onHandoverToWifiFailure() {}
375
376 /** Handles a change to the child number by refreshing the primary call info. */
377 @Override
378 public void onDialerCallChildNumberChange() {
379 LogUtil.v("CallCardPresenter.onDialerCallChildNumberChange", "");
380
381 if (mPrimary == null) {
382 return;
383 }
384 updatePrimaryDisplayInfo();
385 }
386
387 /** Handles a change to the last forwarding number by refreshing the primary call info. */
388 @Override
389 public void onDialerCallLastForwardedNumberChange() {
390 LogUtil.v("CallCardPresenter.onDialerCallLastForwardedNumberChange", "");
391
392 if (mPrimary == null) {
393 return;
394 }
395 updatePrimaryDisplayInfo();
396 updatePrimaryCallState();
397 }
398
399 @Override
400 public void onDialerCallUpgradeToVideo() {}
401
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700402 /** Handles a change to the session modification state for a call. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800403 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700404 public void onDialerCallSessionModificationStateChange() {
405 LogUtil.enterBlock("CallCardPresenter.onDialerCallSessionModificationStateChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800406
407 if (mPrimary == null) {
408 return;
409 }
410 getUi()
411 .setEndCallButtonEnabled(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700412 mPrimary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700413 != SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800414 true /* shouldAnimate */);
415 updatePrimaryCallState();
416 }
417
418 @Override
419 public void onEnrichedCallStateChanged() {
420 LogUtil.enterBlock("CallCardPresenter.onEnrichedCallStateChanged");
421 updatePrimaryDisplayInfo();
422 }
423
424 private boolean shouldRefreshPrimaryInfo(boolean primaryChanged) {
425 if (mPrimary == null) {
426 return false;
427 }
428 return primaryChanged
429 || mInCallScreen.isManageConferenceVisible() != shouldShowManageConference();
430 }
431
432 private void updatePrimaryCallState() {
433 if (getUi() != null && mPrimary != null) {
434 boolean isWorkCall =
435 mPrimary.hasProperty(PROPERTY_ENTERPRISE_CALL)
436 || (mPrimaryContactInfo != null
437 && mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
438 boolean isHdAudioCall =
439 isPrimaryCallActive() && mPrimary.hasProperty(Details.PROPERTY_HIGH_DEF_AUDIO);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700440 boolean isAttemptingHdAudioCall =
441 !isHdAudioCall
442 && !mPrimary.hasProperty(DialerCall.PROPERTY_CODEC_KNOWN)
443 && MotorolaUtils.shouldBlinkHdIconWhenConnectingCall(mContext);
444
445 boolean isBusiness = mPrimaryContactInfo != null && mPrimaryContactInfo.isBusiness;
446
Eric Erfanianccca3152017-02-22 16:32:36 -0800447 // Check for video state change and update the visibility of the contact photo. The contact
448 // photo is hidden when the incoming video surface is shown.
449 // The contact photo visibility can also change in setPrimary().
450 boolean shouldShowContactPhoto =
451 !VideoCallPresenter.showIncomingVideo(mPrimary.getVideoState(), mPrimary.getState());
452 getUi()
453 .setCallState(
454 new PrimaryCallState(
455 mPrimary.getState(),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700456 mPrimary.isVideoCall(),
457 mPrimary.getVideoTech().getSessionModificationState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800458 mPrimary.getDisconnectCause(),
459 getConnectionLabel(),
460 getCallStateIcon(),
461 getGatewayNumber(),
462 shouldShowCallSubject(mPrimary) ? mPrimary.getCallSubject() : null,
463 mPrimary.getCallbackNumber(),
464 mPrimary.hasProperty(Details.PROPERTY_WIFI),
465 mPrimary.isConferenceCall(),
466 isWorkCall,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700467 isAttemptingHdAudioCall,
Eric Erfanianccca3152017-02-22 16:32:36 -0800468 isHdAudioCall,
469 !TextUtils.isEmpty(mPrimary.getLastForwardedNumber()),
470 shouldShowContactPhoto,
471 mPrimary.getConnectTimeMillis(),
472 CallerInfoUtils.isVoiceMailNumber(mContext, mPrimary),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700473 mPrimary.isRemotelyHeld(),
474 isBusiness));
Eric Erfanianccca3152017-02-22 16:32:36 -0800475
476 InCallActivity activity =
477 (InCallActivity) (mInCallScreen.getInCallScreenFragment().getActivity());
478 if (activity != null) {
479 activity.onPrimaryCallStateChanged();
480 }
481 }
482 }
483
484 /** Only show the conference call button if we can manage the conference. */
485 private void maybeShowManageConferenceCallButton() {
486 getUi().showManageConferenceCallButton(shouldShowManageConference());
487 }
488
489 /**
490 * Determines if the manage conference button should be visible, based on the current primary
491 * call.
492 *
493 * @return {@code True} if the manage conference button should be visible.
494 */
495 private boolean shouldShowManageConference() {
496 if (mPrimary == null) {
497 return false;
498 }
499
500 return mPrimary.can(android.telecom.Call.Details.CAPABILITY_MANAGE_CONFERENCE)
501 && !mIsFullscreen;
502 }
503
504 @Override
505 public void onCallStateButtonClicked() {
506 Intent broadcastIntent = Bindings.get(mContext).getCallStateButtonBroadcastIntent(mContext);
507 if (broadcastIntent != null) {
508 LogUtil.v(
509 "CallCardPresenter.onCallStateButtonClicked",
510 "sending call state button broadcast: " + broadcastIntent);
511 mContext.sendBroadcast(broadcastIntent, Manifest.permission.READ_PHONE_STATE);
512 }
513 }
514
515 @Override
516 public void onManageConferenceClicked() {
517 InCallActivity activity =
518 (InCallActivity) (mInCallScreen.getInCallScreenFragment().getActivity());
519 activity.showConferenceFragment(true);
520 }
521
522 @Override
523 public void onShrinkAnimationComplete() {
524 InCallPresenter.getInstance().onShrinkAnimationComplete();
525 }
526
527 @Override
528 public Drawable getDefaultContactPhotoDrawable() {
529 return ContactInfoCache.getInstance(mContext).getDefaultContactPhotoDrawable();
530 }
531
532 private void maybeStartSearch(DialerCall call, boolean isPrimary) {
533 // no need to start search for conference calls which show generic info.
534 if (call != null && !call.isConferenceCall()) {
535 startContactInfoSearch(call, isPrimary, call.getState() == DialerCall.State.INCOMING);
536 }
537 }
538
Eric Erfanianccca3152017-02-22 16:32:36 -0800539 /** Starts a query for more contact data for the save primary and secondary calls. */
540 private void startContactInfoSearch(
541 final DialerCall call, final boolean isPrimary, boolean isIncoming) {
542 final ContactInfoCache cache = ContactInfoCache.getInstance(mContext);
543
544 cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary));
545 }
546
547 private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) {
548 final boolean entryMatchesExistingCall =
549 (isPrimary && mPrimary != null && TextUtils.equals(callId, mPrimary.getId()))
550 || (!isPrimary && mSecondary != null && TextUtils.equals(callId, mSecondary.getId()));
551 if (entryMatchesExistingCall) {
552 updateContactEntry(entry, isPrimary);
553 } else {
554 LogUtil.e(
555 "CallCardPresenter.onContactInfoComplete",
556 "dropping stale contact lookup info for " + callId);
557 }
558
559 final DialerCall call = CallList.getInstance().getCallById(callId);
560 if (call != null) {
561 call.getLogState().contactLookupResult = entry.contactLookupResult;
562 }
563 if (entry.contactUri != null) {
564 CallerInfoUtils.sendViewNotification(mContext, entry.contactUri);
565 }
566 }
567
568 private void onImageLoadComplete(String callId, ContactCacheEntry entry) {
569 if (getUi() == null) {
570 return;
571 }
572
573 if (entry.photo != null) {
574 if (mPrimary != null && callId.equals(mPrimary.getId())) {
575 updateContactEntry(entry, true /* isPrimary */);
576 } else if (mSecondary != null && callId.equals(mSecondary.getId())) {
577 updateContactEntry(entry, false /* isPrimary */);
578 }
579 }
580 }
581
582 private void updateContactEntry(ContactCacheEntry entry, boolean isPrimary) {
583 if (isPrimary) {
584 mPrimaryContactInfo = entry;
585 updatePrimaryDisplayInfo();
586 } else {
587 mSecondaryContactInfo = entry;
588 updateSecondaryDisplayInfo();
589 }
590 }
591
592 /**
593 * Get the highest priority call to display. Goes through the calls and chooses which to return
594 * based on priority of which type of call to display to the user. Callers can use the "ignore"
595 * feature to get the second best call by passing a previously found primary call as ignore.
596 *
597 * @param ignore A call to ignore if found.
598 */
599 private DialerCall getCallToDisplay(
600 CallList callList, DialerCall ignore, boolean skipDisconnected) {
601 // Active calls come second. An active call always gets precedent.
602 DialerCall retval = callList.getActiveCall();
603 if (retval != null && retval != ignore) {
604 return retval;
605 }
606
607 // Sometimes there is intemediate state that two calls are in active even one is about
608 // to be on hold.
609 retval = callList.getSecondActiveCall();
610 if (retval != null && retval != ignore) {
611 return retval;
612 }
613
614 // Disconnected calls get primary position if there are no active calls
615 // to let user know quickly what call has disconnected. Disconnected
616 // calls are very short lived.
617 if (!skipDisconnected) {
618 retval = callList.getDisconnectingCall();
619 if (retval != null && retval != ignore) {
620 return retval;
621 }
622 retval = callList.getDisconnectedCall();
623 if (retval != null && retval != ignore) {
624 return retval;
625 }
626 }
627
628 // Then we go to background call (calls on hold)
629 retval = callList.getBackgroundCall();
630 if (retval != null && retval != ignore) {
631 return retval;
632 }
633
634 // Lastly, we go to a second background call.
635 retval = callList.getSecondBackgroundCall();
636
637 return retval;
638 }
639
640 private void updatePrimaryDisplayInfo() {
641 if (mInCallScreen == null) {
642 // TODO: May also occur if search result comes back after ui is destroyed. Look into
643 // removing that case completely.
644 LogUtil.v(
645 "CallCardPresenter.updatePrimaryDisplayInfo",
646 "updatePrimaryDisplayInfo called but ui is null!");
647 return;
648 }
649
650 if (mPrimary == null) {
651 // Clear the primary display info.
652 mInCallScreen.setPrimary(PrimaryInfo.createEmptyPrimaryInfo());
653 return;
654 }
655
656 // Hide the contact photo if we are in a video call and the incoming video surface is
657 // showing.
658 boolean showContactPhoto =
659 !VideoCallPresenter.showIncomingVideo(mPrimary.getVideoState(), mPrimary.getState());
660
661 // DialerCall placed through a work phone account.
662 boolean hasWorkCallProperty = mPrimary.hasProperty(PROPERTY_ENTERPRISE_CALL);
663
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700664 MultimediaData multimediaData = null;
665 if (mPrimary.getNumber() != null) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700666 EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700667 Session enrichedCallSession =
Eric Erfanian8369df02017-05-03 10:27:13 -0700668 manager.getSession(mPrimary.getUniqueCallId(), mPrimary.getNumber());
669
670 mPrimary.setEnrichedCallSession(enrichedCallSession);
671 mPrimary.setEnrichedCallCapabilities(manager.getCapabilities(mPrimary.getNumber()));
672
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700673 if (enrichedCallSession != null) {
674 enrichedCallSession.setUniqueDialerCallId(mPrimary.getUniqueCallId());
675 multimediaData = enrichedCallSession.getMultimediaData();
676 }
677 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800678
679 if (mPrimary.isConferenceCall()) {
680 LogUtil.v(
681 "CallCardPresenter.updatePrimaryDisplayInfo",
682 "update primary display info for conference call.");
683
684 mInCallScreen.setPrimary(
685 new PrimaryInfo(
686 null /* number */,
687 getConferenceString(mPrimary),
688 false /* nameIsNumber */,
689 null /* location */,
690 null /* label */,
691 getConferencePhoto(mPrimary),
692 ContactPhotoType.DEFAULT_PLACEHOLDER,
693 false /* isSipCall */,
694 showContactPhoto,
695 hasWorkCallProperty,
696 false /* isSpam */,
697 false /* answeringDisconnectsOngoingCall */,
698 shouldShowLocation(),
699 null /* contactInfoLookupKey */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700700 null /* enrichedCallMultimediaData */,
701 mPrimary.getNumberPresentation()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 } else if (mPrimaryContactInfo != null) {
703 LogUtil.v(
704 "CallCardPresenter.updatePrimaryDisplayInfo",
705 "update primary display info for " + mPrimaryContactInfo);
706
707 String name = getNameForCall(mPrimaryContactInfo);
708 String number;
709
710 boolean isChildNumberShown = !TextUtils.isEmpty(mPrimary.getChildNumber());
711 boolean isForwardedNumberShown = !TextUtils.isEmpty(mPrimary.getLastForwardedNumber());
712 boolean isCallSubjectShown = shouldShowCallSubject(mPrimary);
713
714 if (isCallSubjectShown) {
715 number = null;
716 } else if (isChildNumberShown) {
717 number = mContext.getString(R.string.child_number, mPrimary.getChildNumber());
718 } else if (isForwardedNumberShown) {
719 // Use last forwarded number instead of second line, if present.
720 number = mPrimary.getLastForwardedNumber();
721 } else {
722 number = mPrimaryContactInfo.number;
723 }
724
725 boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700726
Eric Erfanianccca3152017-02-22 16:32:36 -0800727 // DialerCall with caller that is a work contact.
728 boolean isWorkContact = (mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
729 mInCallScreen.setPrimary(
730 new PrimaryInfo(
731 number,
732 name,
733 nameIsNumber,
Eric Erfaniand8046e52017-04-06 09:41:50 -0700734 shouldShowLocationAsLabel(nameIsNumber, mPrimaryContactInfo.shouldShowLocation)
735 ? mPrimaryContactInfo.location
736 : null,
Eric Erfanianccca3152017-02-22 16:32:36 -0800737 isChildNumberShown || isCallSubjectShown ? null : mPrimaryContactInfo.label,
738 mPrimaryContactInfo.photo,
739 mPrimaryContactInfo.photoType,
740 mPrimaryContactInfo.isSipCall,
741 showContactPhoto,
742 hasWorkCallProperty || isWorkContact,
743 mPrimary.isSpam(),
744 mPrimary.answeringDisconnectsForegroundVideoCall(),
745 shouldShowLocation(),
746 mPrimaryContactInfo.lookupKey,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700747 multimediaData,
748 mPrimary.getNumberPresentation()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800749 } else {
750 // Clear the primary display info.
751 mInCallScreen.setPrimary(PrimaryInfo.createEmptyPrimaryInfo());
752 }
753
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700754 if (isInCallScreenReady) {
755 mInCallScreen.showLocationUi(getLocationFragment());
756 } else {
757 LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location");
758 }
759 }
760
Eric Erfaniand8046e52017-04-06 09:41:50 -0700761 private static boolean shouldShowLocationAsLabel(
762 boolean nameIsNumber, boolean shouldShowLocation) {
763 if (nameIsNumber) {
764 return true;
765 }
766 if (shouldShowLocation) {
767 return true;
768 }
769 return false;
770 }
771
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700772 private Fragment getLocationFragment() {
773 if (!ConfigProviderBindings.get(mContext)
774 .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) {
775 LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config.");
776 return null;
777 }
778 if (!shouldShowLocation()) {
779 LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location");
780 return null;
781 }
782 if (!hasLocationPermission()) {
783 LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission.");
784 return null;
785 }
786 if (isBatteryTooLowForEmergencyLocation()) {
787 LogUtil.i("CallCardPresenter.getLocationFragment", "low battery.");
788 return null;
789 }
790 if (ActivityCompat.isInMultiWindowMode(mInCallScreen.getInCallScreenFragment().getActivity())) {
791 LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode");
792 return null;
793 }
794 if (mPrimary.isVideoCall()) {
795 LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported");
796 return null;
797 }
798 if (!callLocation.canGetLocation(mContext)) {
799 LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location");
800 return null;
801 }
802 LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment");
803 return callLocation.getLocationFragment(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800804 }
805
806 private boolean shouldShowLocation() {
807 if (isOutgoingEmergencyCall(mPrimary)) {
808 LogUtil.i("CallCardPresenter.shouldShowLocation", "new emergency call");
809 return true;
810 } else if (isIncomingEmergencyCall(mPrimary)) {
811 LogUtil.i("CallCardPresenter.shouldShowLocation", "potential emergency callback");
812 return true;
813 } else if (isIncomingEmergencyCall(mSecondary)) {
814 LogUtil.i("CallCardPresenter.shouldShowLocation", "has potential emergency callback");
815 return true;
816 }
817 return false;
818 }
819
820 private static boolean isOutgoingEmergencyCall(@Nullable DialerCall call) {
821 return call != null && !call.isIncoming() && call.isEmergencyCall();
822 }
823
824 private static boolean isIncomingEmergencyCall(@Nullable DialerCall call) {
825 return call != null && call.isIncoming() && call.isPotentialEmergencyCallback();
826 }
827
828 private boolean hasLocationPermission() {
829 return ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
830 == PackageManager.PERMISSION_GRANTED;
831 }
832
833 private boolean isBatteryTooLowForEmergencyLocation() {
834 Intent batteryStatus =
835 mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
836 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
837 if (status == BatteryManager.BATTERY_STATUS_CHARGING
838 || status == BatteryManager.BATTERY_STATUS_FULL) {
839 // Plugged in or full battery
840 return false;
841 }
842 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
843 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
844 float batteryPercent = (100f * level) / scale;
845 long threshold =
846 ConfigProviderBindings.get(mContext)
847 .getLong(
848 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION,
849 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT);
850 LogUtil.i(
851 "CallCardPresenter.isBatteryTooLowForEmergencyLocation",
852 "percent charged: " + batteryPercent + ", min required charge: " + threshold);
853 return batteryPercent < threshold;
854 }
855
856 private void updateSecondaryDisplayInfo() {
857 if (mInCallScreen == null) {
858 return;
859 }
860
861 if (mSecondary == null) {
862 // Clear the secondary display info.
863 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
864 return;
865 }
866
867 if (mSecondary.isConferenceCall()) {
868 mInCallScreen.setSecondary(
869 new SecondaryInfo(
870 true /* show */,
871 getConferenceString(mSecondary),
872 false /* nameIsNumber */,
873 null /* label */,
874 mSecondary.getCallProviderLabel(),
875 true /* isConference */,
876 mSecondary.isVideoCall(),
877 mIsFullscreen));
878 } else if (mSecondaryContactInfo != null) {
879 LogUtil.v("CallCardPresenter.updateSecondaryDisplayInfo", "" + mSecondaryContactInfo);
880 String name = getNameForCall(mSecondaryContactInfo);
881 boolean nameIsNumber = name != null && name.equals(mSecondaryContactInfo.number);
882 mInCallScreen.setSecondary(
883 new SecondaryInfo(
884 true /* show */,
885 name,
886 nameIsNumber,
887 mSecondaryContactInfo.label,
888 mSecondary.getCallProviderLabel(),
889 false /* isConference */,
890 mSecondary.isVideoCall(),
891 mIsFullscreen));
892 } else {
893 // Clear the secondary display info.
894 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
895 }
896 }
897
898 /** Returns the gateway number for any existing outgoing call. */
899 private String getGatewayNumber() {
900 if (hasOutgoingGatewayCall()) {
901 return DialerCall.getNumberFromHandle(mPrimary.getGatewayInfo().getGatewayAddress());
902 }
903 return null;
904 }
905
906 /**
907 * Returns the label (line of text above the number/name) for any given call. For example,
908 * "calling via [Account/Google Voice]" for outgoing calls.
909 */
910 private String getConnectionLabel() {
911 if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
912 != PackageManager.PERMISSION_GRANTED) {
913 return null;
914 }
915 StatusHints statusHints = mPrimary.getStatusHints();
916 if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
917 return statusHints.getLabel().toString();
918 }
919
920 if (hasOutgoingGatewayCall() && getUi() != null) {
921 // Return the label for the gateway app on outgoing calls.
922 final PackageManager pm = mContext.getPackageManager();
923 try {
924 ApplicationInfo info =
925 pm.getApplicationInfo(mPrimary.getGatewayInfo().getGatewayProviderPackageName(), 0);
926 return pm.getApplicationLabel(info).toString();
927 } catch (PackageManager.NameNotFoundException e) {
928 LogUtil.e("CallCardPresenter.getConnectionLabel", "gateway Application Not Found.", e);
929 return null;
930 }
931 }
932 return mPrimary.getCallProviderLabel();
933 }
934
935 private Drawable getCallStateIcon() {
936 // Return connection icon if one exists.
937 StatusHints statusHints = mPrimary.getStatusHints();
938 if (statusHints != null && statusHints.getIcon() != null) {
939 Drawable icon = statusHints.getIcon().loadDrawable(mContext);
940 if (icon != null) {
941 return icon;
942 }
943 }
944
945 return null;
946 }
947
948 private boolean hasOutgoingGatewayCall() {
949 // We only display the gateway information while STATE_DIALING so return false for any other
950 // call state.
951 // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
952 // is also called after a contact search completes (call is not present yet). Split the
953 // UI update so it can receive independent updates.
954 if (mPrimary == null) {
955 return false;
956 }
957 return DialerCall.State.isDialing(mPrimary.getState())
958 && mPrimary.getGatewayInfo() != null
959 && !mPrimary.getGatewayInfo().isEmpty();
960 }
961
962 /** Gets the name to display for the call. */
963 String getNameForCall(ContactCacheEntry contactInfo) {
964 String preferredName =
965 ContactDisplayUtils.getPreferredDisplayName(
966 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
967 if (TextUtils.isEmpty(preferredName)) {
968 return contactInfo.number;
969 }
970 return preferredName;
971 }
972
973 /** Gets the number to display for a call. */
974 String getNumberForCall(ContactCacheEntry contactInfo) {
975 // If the name is empty, we use the number for the name...so don't show a second
976 // number in the number field
977 String preferredName =
978 ContactDisplayUtils.getPreferredDisplayName(
979 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
980 if (TextUtils.isEmpty(preferredName)) {
981 return contactInfo.location;
982 }
983 return contactInfo.number;
984 }
985
986 @Override
987 public void onSecondaryInfoClicked() {
988 if (mSecondary == null) {
989 LogUtil.e(
990 "CallCardPresenter.onSecondaryInfoClicked",
991 "secondary info clicked but no secondary call.");
992 return;
993 }
994
995 LogUtil.i(
996 "CallCardPresenter.onSecondaryInfoClicked", "swapping call to foreground: " + mSecondary);
997 mSecondary.unhold();
998 }
999
1000 @Override
1001 public void onEndCallClicked() {
1002 LogUtil.i("CallCardPresenter.onEndCallClicked", "disconnecting call: " + mPrimary);
1003 if (mPrimary != null) {
1004 mPrimary.disconnect();
1005 }
1006 }
1007
1008 /**
1009 * Handles a change to the fullscreen mode of the in-call UI.
1010 *
1011 * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1012 */
1013 @Override
1014 public void onFullscreenModeChanged(boolean isFullscreenMode) {
1015 mIsFullscreen = isFullscreenMode;
1016 if (mInCallScreen == null) {
1017 return;
1018 }
1019 maybeShowManageConferenceCallButton();
1020 }
1021
1022 private boolean isPrimaryCallActive() {
1023 return mPrimary != null && mPrimary.getState() == DialerCall.State.ACTIVE;
1024 }
1025
1026 private String getConferenceString(DialerCall call) {
1027 boolean isGenericConference = call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE);
1028 LogUtil.v("CallCardPresenter.getConferenceString", "" + isGenericConference);
1029
1030 final int resId =
1031 isGenericConference ? R.string.generic_conference_call_name : R.string.conference_call_name;
1032 return mContext.getResources().getString(resId);
1033 }
1034
1035 private Drawable getConferencePhoto(DialerCall call) {
1036 boolean isGenericConference = call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE);
1037 LogUtil.v("CallCardPresenter.getConferencePhoto", "" + isGenericConference);
1038
1039 final int resId = isGenericConference ? R.drawable.img_phone : R.drawable.img_conference;
1040 Drawable photo = mContext.getResources().getDrawable(resId);
1041 photo.setAutoMirrored(true);
1042 return photo;
1043 }
1044
1045 private boolean shouldShowEndCallButton(DialerCall primary, int callState) {
1046 if (primary == null) {
1047 return false;
1048 }
1049 if ((!DialerCall.State.isConnectingOrConnected(callState)
1050 && callState != DialerCall.State.DISCONNECTING
1051 && callState != DialerCall.State.DISCONNECTED)
1052 || callState == DialerCall.State.INCOMING) {
1053 return false;
1054 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001055 if (mPrimary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001056 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001057 return false;
1058 }
1059 return true;
1060 }
1061
1062 @Override
1063 public void onInCallScreenResumed() {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001064 EnrichedCallComponent.get(mContext).getEnrichedCallManager().registerStateChangedListener(this);
1065 updatePrimaryDisplayInfo();
1066
Eric Erfanianccca3152017-02-22 16:32:36 -08001067 if (shouldSendAccessibilityEvent) {
1068 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1069 }
1070 }
1071
Eric Erfaniand8046e52017-04-06 09:41:50 -07001072 @Override
1073 public void onInCallScreenPaused() {
1074 EnrichedCallComponent.get(mContext)
1075 .getEnrichedCallManager()
1076 .unregisterStateChangedListener(this);
1077 }
1078
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}