blob: 15b04eaaf239f5c0dd2949b3263ddc0e1e4202e8 [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) {
145 LogUtil.i("CallCardController.constructor", null);
146 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() {
182 LogUtil.i("CallCardController.onInCallScreenReady", null);
183 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() {
225 LogUtil.i("CallCardController.onInCallScreenUnready", null);
226 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 */,
715 false /* answeringDisconnectsOngoingCall */,
716 shouldShowLocation(),
717 null /* contactInfoLookupKey */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700718 null /* enrichedCallMultimediaData */,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700719 true /* showInCallButtonGrid */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700720 mPrimary.getNumberPresentation()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800721 } else if (mPrimaryContactInfo != null) {
722 LogUtil.v(
723 "CallCardPresenter.updatePrimaryDisplayInfo",
724 "update primary display info for " + mPrimaryContactInfo);
725
726 String name = getNameForCall(mPrimaryContactInfo);
727 String number;
728
729 boolean isChildNumberShown = !TextUtils.isEmpty(mPrimary.getChildNumber());
730 boolean isForwardedNumberShown = !TextUtils.isEmpty(mPrimary.getLastForwardedNumber());
731 boolean isCallSubjectShown = shouldShowCallSubject(mPrimary);
732
733 if (isCallSubjectShown) {
734 number = null;
735 } else if (isChildNumberShown) {
736 number = mContext.getString(R.string.child_number, mPrimary.getChildNumber());
737 } else if (isForwardedNumberShown) {
738 // Use last forwarded number instead of second line, if present.
739 number = mPrimary.getLastForwardedNumber();
740 } else {
741 number = mPrimaryContactInfo.number;
742 }
743
744 boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700745
Eric Erfanianccca3152017-02-22 16:32:36 -0800746 // DialerCall with caller that is a work contact.
747 boolean isWorkContact = (mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
748 mInCallScreen.setPrimary(
749 new PrimaryInfo(
750 number,
Eric Erfanianc857f902017-05-15 14:05:33 -0700751 mPrimary.updateNameIfRestricted(name),
Eric Erfanianccca3152017-02-22 16:32:36 -0800752 nameIsNumber,
Eric Erfaniand8046e52017-04-06 09:41:50 -0700753 shouldShowLocationAsLabel(nameIsNumber, mPrimaryContactInfo.shouldShowLocation)
754 ? mPrimaryContactInfo.location
755 : null,
Eric Erfanianccca3152017-02-22 16:32:36 -0800756 isChildNumberShown || isCallSubjectShown ? null : mPrimaryContactInfo.label,
757 mPrimaryContactInfo.photo,
758 mPrimaryContactInfo.photoType,
759 mPrimaryContactInfo.isSipCall,
760 showContactPhoto,
761 hasWorkCallProperty || isWorkContact,
762 mPrimary.isSpam(),
763 mPrimary.answeringDisconnectsForegroundVideoCall(),
764 shouldShowLocation(),
765 mPrimaryContactInfo.lookupKey,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700766 multimediaData,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700767 true /* showInCallButtonGrid */,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700768 mPrimary.getNumberPresentation()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800769 } else {
770 // Clear the primary display info.
771 mInCallScreen.setPrimary(PrimaryInfo.createEmptyPrimaryInfo());
772 }
773
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700774 if (isInCallScreenReady) {
775 mInCallScreen.showLocationUi(getLocationFragment());
776 } else {
777 LogUtil.i("CallCardPresenter.updatePrimaryDisplayInfo", "UI not ready, not showing location");
778 }
779 }
780
Eric Erfaniand8046e52017-04-06 09:41:50 -0700781 private static boolean shouldShowLocationAsLabel(
782 boolean nameIsNumber, boolean shouldShowLocation) {
783 if (nameIsNumber) {
784 return true;
785 }
786 if (shouldShowLocation) {
787 return true;
788 }
789 return false;
790 }
791
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700792 private Fragment getLocationFragment() {
793 if (!ConfigProviderBindings.get(mContext)
794 .getBoolean(CONFIG_ENABLE_EMERGENCY_LOCATION, CONFIG_ENABLE_EMERGENCY_LOCATION_DEFAULT)) {
795 LogUtil.i("CallCardPresenter.getLocationFragment", "disabled by config.");
796 return null;
797 }
798 if (!shouldShowLocation()) {
799 LogUtil.i("CallCardPresenter.getLocationFragment", "shouldn't show location");
800 return null;
801 }
802 if (!hasLocationPermission()) {
803 LogUtil.i("CallCardPresenter.getLocationFragment", "no location permission.");
804 return null;
805 }
806 if (isBatteryTooLowForEmergencyLocation()) {
807 LogUtil.i("CallCardPresenter.getLocationFragment", "low battery.");
808 return null;
809 }
810 if (ActivityCompat.isInMultiWindowMode(mInCallScreen.getInCallScreenFragment().getActivity())) {
811 LogUtil.i("CallCardPresenter.getLocationFragment", "in multi-window mode");
812 return null;
813 }
814 if (mPrimary.isVideoCall()) {
815 LogUtil.i("CallCardPresenter.getLocationFragment", "emergency video calls not supported");
816 return null;
817 }
818 if (!callLocation.canGetLocation(mContext)) {
819 LogUtil.i("CallCardPresenter.getLocationFragment", "can't get current location");
820 return null;
821 }
822 LogUtil.i("CallCardPresenter.getLocationFragment", "returning location fragment");
823 return callLocation.getLocationFragment(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800824 }
825
826 private boolean shouldShowLocation() {
827 if (isOutgoingEmergencyCall(mPrimary)) {
828 LogUtil.i("CallCardPresenter.shouldShowLocation", "new emergency call");
829 return true;
830 } else if (isIncomingEmergencyCall(mPrimary)) {
831 LogUtil.i("CallCardPresenter.shouldShowLocation", "potential emergency callback");
832 return true;
833 } else if (isIncomingEmergencyCall(mSecondary)) {
834 LogUtil.i("CallCardPresenter.shouldShowLocation", "has potential emergency callback");
835 return true;
836 }
837 return false;
838 }
839
840 private static boolean isOutgoingEmergencyCall(@Nullable DialerCall call) {
841 return call != null && !call.isIncoming() && call.isEmergencyCall();
842 }
843
844 private static boolean isIncomingEmergencyCall(@Nullable DialerCall call) {
845 return call != null && call.isIncoming() && call.isPotentialEmergencyCallback();
846 }
847
848 private boolean hasLocationPermission() {
849 return ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
850 == PackageManager.PERMISSION_GRANTED;
851 }
852
853 private boolean isBatteryTooLowForEmergencyLocation() {
854 Intent batteryStatus =
855 mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
856 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
857 if (status == BatteryManager.BATTERY_STATUS_CHARGING
858 || status == BatteryManager.BATTERY_STATUS_FULL) {
859 // Plugged in or full battery
860 return false;
861 }
862 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
863 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
864 float batteryPercent = (100f * level) / scale;
865 long threshold =
866 ConfigProviderBindings.get(mContext)
867 .getLong(
868 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION,
869 CONFIG_MIN_BATTERY_PERCENT_FOR_EMERGENCY_LOCATION_DEFAULT);
870 LogUtil.i(
871 "CallCardPresenter.isBatteryTooLowForEmergencyLocation",
872 "percent charged: " + batteryPercent + ", min required charge: " + threshold);
873 return batteryPercent < threshold;
874 }
875
876 private void updateSecondaryDisplayInfo() {
877 if (mInCallScreen == null) {
878 return;
879 }
880
881 if (mSecondary == null) {
882 // Clear the secondary display info.
883 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
884 return;
885 }
886
Eric Erfanian2ca43182017-08-31 06:57:16 -0700887 if (mSecondary.isMergeInProcess()) {
888 LogUtil.i(
889 "CallCardPresenter.updateSecondaryDisplayInfo",
890 "secondary call is merge in process, clearing info");
891 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
892 return;
893 }
894
Eric Erfanianccca3152017-02-22 16:32:36 -0800895 if (mSecondary.isConferenceCall()) {
896 mInCallScreen.setSecondary(
897 new SecondaryInfo(
898 true /* show */,
Eric Erfanian2ca43182017-08-31 06:57:16 -0700899 CallerInfoUtils.getConferenceString(
900 mContext, mSecondary.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)),
Eric Erfanianccca3152017-02-22 16:32:36 -0800901 false /* nameIsNumber */,
902 null /* label */,
903 mSecondary.getCallProviderLabel(),
904 true /* isConference */,
905 mSecondary.isVideoCall(),
906 mIsFullscreen));
907 } else if (mSecondaryContactInfo != null) {
908 LogUtil.v("CallCardPresenter.updateSecondaryDisplayInfo", "" + mSecondaryContactInfo);
909 String name = getNameForCall(mSecondaryContactInfo);
910 boolean nameIsNumber = name != null && name.equals(mSecondaryContactInfo.number);
911 mInCallScreen.setSecondary(
912 new SecondaryInfo(
913 true /* show */,
Eric Erfanianc857f902017-05-15 14:05:33 -0700914 mSecondary.updateNameIfRestricted(name),
Eric Erfanianccca3152017-02-22 16:32:36 -0800915 nameIsNumber,
916 mSecondaryContactInfo.label,
917 mSecondary.getCallProviderLabel(),
918 false /* isConference */,
919 mSecondary.isVideoCall(),
920 mIsFullscreen));
921 } else {
922 // Clear the secondary display info.
923 mInCallScreen.setSecondary(SecondaryInfo.createEmptySecondaryInfo(mIsFullscreen));
924 }
925 }
926
927 /** Returns the gateway number for any existing outgoing call. */
928 private String getGatewayNumber() {
929 if (hasOutgoingGatewayCall()) {
930 return DialerCall.getNumberFromHandle(mPrimary.getGatewayInfo().getGatewayAddress());
931 }
932 return null;
933 }
934
935 /**
936 * Returns the label (line of text above the number/name) for any given call. For example,
937 * "calling via [Account/Google Voice]" for outgoing calls.
938 */
939 private String getConnectionLabel() {
940 if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
941 != PackageManager.PERMISSION_GRANTED) {
942 return null;
943 }
944 StatusHints statusHints = mPrimary.getStatusHints();
945 if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
946 return statusHints.getLabel().toString();
947 }
948
949 if (hasOutgoingGatewayCall() && getUi() != null) {
950 // Return the label for the gateway app on outgoing calls.
951 final PackageManager pm = mContext.getPackageManager();
952 try {
953 ApplicationInfo info =
954 pm.getApplicationInfo(mPrimary.getGatewayInfo().getGatewayProviderPackageName(), 0);
955 return pm.getApplicationLabel(info).toString();
956 } catch (PackageManager.NameNotFoundException e) {
957 LogUtil.e("CallCardPresenter.getConnectionLabel", "gateway Application Not Found.", e);
958 return null;
959 }
960 }
961 return mPrimary.getCallProviderLabel();
962 }
963
964 private Drawable getCallStateIcon() {
965 // Return connection icon if one exists.
966 StatusHints statusHints = mPrimary.getStatusHints();
967 if (statusHints != null && statusHints.getIcon() != null) {
968 Drawable icon = statusHints.getIcon().loadDrawable(mContext);
969 if (icon != null) {
970 return icon;
971 }
972 }
973
974 return null;
975 }
976
977 private boolean hasOutgoingGatewayCall() {
978 // We only display the gateway information while STATE_DIALING so return false for any other
979 // call state.
980 // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
981 // is also called after a contact search completes (call is not present yet). Split the
982 // UI update so it can receive independent updates.
983 if (mPrimary == null) {
984 return false;
985 }
986 return DialerCall.State.isDialing(mPrimary.getState())
987 && mPrimary.getGatewayInfo() != null
988 && !mPrimary.getGatewayInfo().isEmpty();
989 }
990
991 /** Gets the name to display for the call. */
Eric Erfanian2ca43182017-08-31 06:57:16 -0700992 private String getNameForCall(ContactCacheEntry contactInfo) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800993 String preferredName =
994 ContactDisplayUtils.getPreferredDisplayName(
995 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
996 if (TextUtils.isEmpty(preferredName)) {
997 return contactInfo.number;
998 }
999 return preferredName;
1000 }
1001
Eric Erfanianccca3152017-02-22 16:32:36 -08001002 @Override
1003 public void onSecondaryInfoClicked() {
1004 if (mSecondary == null) {
1005 LogUtil.e(
1006 "CallCardPresenter.onSecondaryInfoClicked",
1007 "secondary info clicked but no secondary call.");
1008 return;
1009 }
1010
Eric Erfanian2ca43182017-08-31 06:57:16 -07001011 Logger.get(mContext)
1012 .logCallImpression(
1013 DialerImpression.Type.IN_CALL_SWAP_SECONDARY_BUTTON_PRESSED,
1014 mPrimary.getUniqueCallId(),
1015 mPrimary.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -08001016 LogUtil.i(
1017 "CallCardPresenter.onSecondaryInfoClicked", "swapping call to foreground: " + mSecondary);
1018 mSecondary.unhold();
1019 }
1020
1021 @Override
1022 public void onEndCallClicked() {
1023 LogUtil.i("CallCardPresenter.onEndCallClicked", "disconnecting call: " + mPrimary);
1024 if (mPrimary != null) {
1025 mPrimary.disconnect();
1026 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001027 PostCall.onDisconnectPressed(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -08001028 }
1029
1030 /**
1031 * Handles a change to the fullscreen mode of the in-call UI.
1032 *
1033 * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1034 */
1035 @Override
1036 public void onFullscreenModeChanged(boolean isFullscreenMode) {
1037 mIsFullscreen = isFullscreenMode;
1038 if (mInCallScreen == null) {
1039 return;
1040 }
1041 maybeShowManageConferenceCallButton();
1042 }
1043
1044 private boolean isPrimaryCallActive() {
1045 return mPrimary != null && mPrimary.getState() == DialerCall.State.ACTIVE;
1046 }
1047
Eric Erfanianccca3152017-02-22 16:32:36 -08001048 private boolean shouldShowEndCallButton(DialerCall primary, int callState) {
1049 if (primary == null) {
1050 return false;
1051 }
1052 if ((!DialerCall.State.isConnectingOrConnected(callState)
1053 && callState != DialerCall.State.DISCONNECTING
1054 && callState != DialerCall.State.DISCONNECTED)
1055 || callState == DialerCall.State.INCOMING) {
1056 return false;
1057 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001058 if (mPrimary.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001059 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001060 return false;
1061 }
1062 return true;
1063 }
1064
1065 @Override
1066 public void onInCallScreenResumed() {
Eric Erfaniand8046e52017-04-06 09:41:50 -07001067 updatePrimaryDisplayInfo();
1068
Eric Erfanianccca3152017-02-22 16:32:36 -08001069 if (shouldSendAccessibilityEvent) {
1070 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1071 }
1072 }
1073
Eric Erfaniand8046e52017-04-06 09:41:50 -07001074 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001075 public void onInCallScreenPaused() {}
Eric Erfaniand8046e52017-04-06 09:41:50 -07001076
Eric Erfanianccca3152017-02-22 16:32:36 -08001077 static boolean sendAccessibilityEvent(Context context, InCallScreen inCallScreen) {
1078 AccessibilityManager am =
1079 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
1080 if (!am.isEnabled()) {
1081 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "accessibility is off");
1082 return false;
1083 }
1084 if (inCallScreen == null) {
1085 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "incallscreen is null");
1086 return false;
1087 }
1088 Fragment fragment = inCallScreen.getInCallScreenFragment();
1089 if (fragment == null || fragment.getView() == null || fragment.getView().getParent() == null) {
1090 LogUtil.w("CallCardPresenter.sendAccessibilityEvent", "fragment/view/parent is null");
1091 return false;
1092 }
1093
1094 DisplayManager displayManager =
1095 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
1096 Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
1097 boolean screenIsOn = display.getState() == Display.STATE_ON;
1098 LogUtil.d("CallCardPresenter.sendAccessibilityEvent", "screen is on: %b", screenIsOn);
1099 if (!screenIsOn) {
1100 return false;
1101 }
1102
1103 AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
1104 inCallScreen.dispatchPopulateAccessibilityEvent(event);
1105 View view = inCallScreen.getInCallScreenFragment().getView();
1106 view.getParent().requestSendAccessibilityEvent(view, event);
1107 return true;
1108 }
1109
1110 private void maybeSendAccessibilityEvent(
1111 InCallState oldState, final InCallState newState, boolean primaryChanged) {
1112 shouldSendAccessibilityEvent = false;
1113 if (mContext == null) {
1114 return;
1115 }
1116 final AccessibilityManager am =
1117 (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
1118 if (!am.isEnabled()) {
1119 return;
1120 }
1121 // Announce the current call if it's new incoming/outgoing call or primary call is changed
1122 // due to switching calls between two ongoing calls (one is on hold).
1123 if ((oldState != InCallState.OUTGOING && newState == InCallState.OUTGOING)
1124 || (oldState != InCallState.INCOMING && newState == InCallState.INCOMING)
1125 || primaryChanged) {
1126 LogUtil.i(
1127 "CallCardPresenter.maybeSendAccessibilityEvent", "schedule accessibility announcement");
1128 shouldSendAccessibilityEvent = true;
1129 handler.postDelayed(sendAccessibilityEventRunnable, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MILLIS);
1130 }
1131 }
1132
1133 /**
1134 * Determines whether the call subject should be visible on the UI. For the call subject to be
1135 * visible, the call has to be in an incoming or waiting state, and the subject must not be empty.
1136 *
1137 * @param call The call.
1138 * @return {@code true} if the subject should be shown, {@code false} otherwise.
1139 */
1140 private boolean shouldShowCallSubject(DialerCall call) {
1141 if (call == null) {
1142 return false;
1143 }
1144
1145 boolean isIncomingOrWaiting =
1146 mPrimary.getState() == DialerCall.State.INCOMING
1147 || mPrimary.getState() == DialerCall.State.CALL_WAITING;
1148 return isIncomingOrWaiting
1149 && !TextUtils.isEmpty(call.getCallSubject())
1150 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED
1151 && call.isCallSubjectSupported();
1152 }
1153
1154 /**
1155 * Determines whether the "note sent" toast should be shown. It should be shown for a new outgoing
1156 * call with a subject.
1157 *
1158 * @param call The call
1159 * @return {@code true} if the toast should be shown, {@code false} otherwise.
1160 */
1161 private boolean shouldShowNoteSentToast(DialerCall call) {
1162 return call != null
1163 && hasCallSubject(call)
1164 && (call.getState() == DialerCall.State.DIALING
1165 || call.getState() == DialerCall.State.CONNECTING);
1166 }
1167
1168 private InCallScreen getUi() {
1169 return mInCallScreen;
1170 }
1171
1172 public static class ContactLookupCallback implements ContactInfoCacheCallback {
1173
1174 private final WeakReference<CallCardPresenter> mCallCardPresenter;
1175 private final boolean mIsPrimary;
1176
1177 public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
1178 mCallCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
1179 mIsPrimary = isPrimary;
1180 }
1181
1182 @Override
1183 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1184 CallCardPresenter presenter = mCallCardPresenter.get();
1185 if (presenter != null) {
1186 presenter.onContactInfoComplete(callId, entry, mIsPrimary);
1187 }
1188 }
1189
1190 @Override
1191 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1192 CallCardPresenter presenter = mCallCardPresenter.get();
1193 if (presenter != null) {
1194 presenter.onImageLoadComplete(callId, entry);
1195 }
1196 }
1197 }
1198}