blob: 90eb0aa6a47c0113e2423815d0689612c4289047 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2016 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 android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.GradientDrawable;
22import android.graphics.drawable.GradientDrawable.Orientation;
23import android.os.Bundle;
24import android.support.annotation.ColorInt;
25import android.support.annotation.FloatRange;
26import android.support.annotation.Nullable;
27import android.support.v4.app.FragmentManager;
28import android.support.v4.app.FragmentTransaction;
29import android.support.v4.graphics.ColorUtils;
30import android.telecom.DisconnectCause;
Eric Erfanian90508232017-03-24 09:31:16 -070031import android.telephony.TelephonyManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.view.KeyEvent;
33import android.view.MenuItem;
34import android.view.MotionEvent;
35import android.view.View;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070036import com.android.dialer.common.Assert;
Eric Erfanian90508232017-03-24 09:31:16 -070037import com.android.dialer.common.ConfigProviderBindings;
Eric Erfanianccca3152017-02-22 16:32:36 -080038import com.android.dialer.common.LogUtil;
39import com.android.dialer.compat.ActivityCompat;
40import com.android.dialer.logging.Logger;
41import com.android.dialer.logging.nano.ScreenEvent;
42import com.android.incallui.answer.bindings.AnswerBindings;
43import com.android.incallui.answer.protocol.AnswerScreen;
44import com.android.incallui.answer.protocol.AnswerScreenDelegate;
45import com.android.incallui.answer.protocol.AnswerScreenDelegateFactory;
46import com.android.incallui.answerproximitysensor.PseudoScreenState;
47import com.android.incallui.call.CallList;
48import com.android.incallui.call.DialerCall;
49import com.android.incallui.call.DialerCall.State;
Eric Erfanianccca3152017-02-22 16:32:36 -080050import com.android.incallui.incall.bindings.InCallBindings;
51import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
52import com.android.incallui.incall.protocol.InCallButtonUiDelegateFactory;
53import com.android.incallui.incall.protocol.InCallScreen;
54import com.android.incallui.incall.protocol.InCallScreenDelegate;
55import com.android.incallui.incall.protocol.InCallScreenDelegateFactory;
56import com.android.incallui.video.bindings.VideoBindings;
57import com.android.incallui.video.protocol.VideoCallScreen;
58import com.android.incallui.video.protocol.VideoCallScreenDelegate;
59import com.android.incallui.video.protocol.VideoCallScreenDelegateFactory;
60
61/** Version of {@link InCallActivity} that shows the new UI */
62public class InCallActivity extends TransactionSafeFragmentActivity
63 implements AnswerScreenDelegateFactory,
64 InCallScreenDelegateFactory,
65 InCallButtonUiDelegateFactory,
66 VideoCallScreenDelegateFactory,
67 PseudoScreenState.StateChangedListener {
68
69 private static final String TAG_IN_CALL_SCREEN = "tag_in_call_screen";
70 private static final String TAG_ANSWER_SCREEN = "tag_answer_screen";
71 private static final String TAG_VIDEO_CALL_SCREEN = "tag_video_call_screen";
72
73 private static final String DID_SHOW_ANSWER_SCREEN_KEY = "did_show_answer_screen";
74 private static final String DID_SHOW_IN_CALL_SCREEN_KEY = "did_show_in_call_screen";
75 private static final String DID_SHOW_VIDEO_CALL_SCREEN_KEY = "did_show_video_call_screen";
76
Eric Erfanian90508232017-03-24 09:31:16 -070077 private static final String CONFIG_ANSWER_AND_RELEASE_ENABLED = "answer_and_release_enabled";
78
Eric Erfanianccca3152017-02-22 16:32:36 -080079 private final InCallActivityCommon common;
80 private boolean didShowAnswerScreen;
81 private boolean didShowInCallScreen;
82 private boolean didShowVideoCallScreen;
83 private int[] backgroundDrawableColors;
84 private GradientDrawable backgroundDrawable;
85 private boolean isVisible;
86 private View pseudoBlackScreenOverlay;
87 private boolean touchDownWhenPseudoScreenOff;
88 private boolean isInShowMainInCallFragment;
89 private boolean needDismissPendingDialogs;
90
91 public InCallActivity() {
92 common = new InCallActivityCommon(this);
93 }
94
95 public static Intent getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -070096 Context context, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -080097 Intent intent = new Intent(Intent.ACTION_MAIN, null);
98 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
99 intent.setClass(context, InCallActivity.class);
100 InCallActivityCommon.setIntentExtras(intent, showDialpad, newOutgoingCall, isForFullScreen);
101 return intent;
102 }
103
104 @Override
105 protected void onResumeFragments() {
106 super.onResumeFragments();
107 if (needDismissPendingDialogs) {
108 dismissPendingDialogs();
109 }
110 }
111
112 @Override
113 protected void onCreate(Bundle icicle) {
114 LogUtil.i("InCallActivity.onCreate", "");
115 super.onCreate(icicle);
116
117 if (icicle != null) {
118 didShowAnswerScreen = icicle.getBoolean(DID_SHOW_ANSWER_SCREEN_KEY);
119 didShowInCallScreen = icicle.getBoolean(DID_SHOW_IN_CALL_SCREEN_KEY);
120 didShowVideoCallScreen = icicle.getBoolean(DID_SHOW_VIDEO_CALL_SCREEN_KEY);
121 }
122
123 common.onCreate(icicle);
124
125 getWindow()
126 .getDecorView()
127 .setSystemUiVisibility(
128 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
129
130 pseudoBlackScreenOverlay = findViewById(R.id.psuedo_black_screen_overlay);
131 }
132
133 @Override
134 protected void onSaveInstanceState(Bundle out) {
135 LogUtil.i("InCallActivity.onSaveInstanceState", "");
136 common.onSaveInstanceState(out);
137 out.putBoolean(DID_SHOW_ANSWER_SCREEN_KEY, didShowAnswerScreen);
138 out.putBoolean(DID_SHOW_IN_CALL_SCREEN_KEY, didShowInCallScreen);
139 out.putBoolean(DID_SHOW_VIDEO_CALL_SCREEN_KEY, didShowVideoCallScreen);
140 super.onSaveInstanceState(out);
141 isVisible = false;
142 }
143
144 @Override
145 protected void onStart() {
146 LogUtil.i("InCallActivity.onStart", "");
147 super.onStart();
148 isVisible = true;
149 showMainInCallFragment();
150 common.onStart();
151 if (ActivityCompat.isInMultiWindowMode(this)
152 && !getResources().getBoolean(R.bool.incall_dialpad_allowed)) {
153 // Hide the dialpad because there may not be enough room
154 showDialpadFragment(false, false);
155 }
156 }
157
158 @Override
159 protected void onResume() {
160 LogUtil.i("InCallActivity.onResume", "");
161 super.onResume();
162 common.onResume();
163 PseudoScreenState pseudoScreenState = InCallPresenter.getInstance().getPseudoScreenState();
164 pseudoScreenState.addListener(this);
165 onPseudoScreenStateChanged(pseudoScreenState.isOn());
166 }
167
168 /** onPause is guaranteed to be called when the InCallActivity goes in the background. */
169 @Override
170 protected void onPause() {
171 LogUtil.i("InCallActivity.onPause", "");
172 super.onPause();
173 common.onPause();
174 InCallPresenter.getInstance().getPseudoScreenState().removeListener(this);
175 }
176
177 @Override
178 protected void onStop() {
179 LogUtil.i("InCallActivity.onStop", "");
180 super.onStop();
181 common.onStop();
182 isVisible = false;
183 }
184
185 @Override
186 protected void onDestroy() {
187 LogUtil.i("InCallActivity.onDestroy", "");
188 super.onDestroy();
189 common.onDestroy();
190 }
191
192 @Override
193 public void finish() {
194 if (shouldCloseActivityOnFinish()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700195 // When user select incall ui from recents after the call is disconnected, it tries to launch
196 // a new InCallActivity but InCallPresenter is already teared down at this point, which causes
197 // crash.
198 // By calling finishAndRemoveTask() instead of finish() the task associated with
199 // InCallActivity is cleared completely. So system won't try to create a new InCallActivity in
200 // this case.
201 //
202 // Calling finish won't clear the task and normally when an activity finishes it shouldn't
203 // clear the task since there could be parent activity in the same task that's still alive.
204 // But InCallActivity is special since it's singleInstance which means it's root activity and
205 // only instance of activity in the task. So it should be safe to also remove task when
206 // finishing.
207 // It's also necessary in the sense of it's excluded from recents. So whenever the activity
208 // finishes, the task should also be removed since it doesn't make sense to go back to it in
209 // anyway anymore.
210 super.finishAndRemoveTask();
Eric Erfanianccca3152017-02-22 16:32:36 -0800211 }
212 }
213
214 private boolean shouldCloseActivityOnFinish() {
215 if (!isVisible()) {
216 LogUtil.i(
217 "InCallActivity.shouldCloseActivityOnFinish",
218 "allowing activity to be closed because it's not visible");
219 return true;
220 }
221
222 if (common.hasPendingDialogs()) {
223 LogUtil.i(
224 "InCallActivity.shouldCloseActivityOnFinish", "dialog is visible, not closing activity");
225 return false;
226 }
227
228 AnswerScreen answerScreen = getAnswerScreen();
229 if (answerScreen != null && answerScreen.hasPendingDialogs()) {
230 LogUtil.i(
231 "InCallActivity.shouldCloseActivityOnFinish",
232 "answer screen dialog is visible, not closing activity");
233 return false;
234 }
235
236 LogUtil.i(
237 "InCallActivity.shouldCloseActivityOnFinish",
238 "activity is visible and has no dialogs, allowing activity to close");
239 return true;
240 }
241
242 @Override
243 protected void onNewIntent(Intent intent) {
244 LogUtil.i("InCallActivity.onNewIntent", "");
245 common.onNewIntent(intent);
246
247 // If the screen is off, we need to make sure it gets turned on for incoming calls.
248 // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
249 // when the activity is first created. Therefore, to ensure the screen is turned on
250 // for the call waiting case, we recreate() the current activity. There should be no jank from
251 // this since the screen is already off and will remain so until our new activity is up.
252 if (!isVisible()) {
253 LogUtil.i("InCallActivity.onNewIntent", "Restarting InCallActivity to force screen on.");
254 recreate();
255 }
256 }
257
258 @Override
259 public void onBackPressed() {
260 LogUtil.i("InCallActivity.onBackPressed", "");
261 if (!common.onBackPressed(didShowInCallScreen || didShowVideoCallScreen)) {
262 super.onBackPressed();
263 }
264 }
265
266 @Override
267 public boolean onOptionsItemSelected(MenuItem item) {
268 LogUtil.i("InCallActivity.onOptionsItemSelected", "item: " + item);
269 if (item.getItemId() == android.R.id.home) {
270 onBackPressed();
271 return true;
272 }
273 return super.onOptionsItemSelected(item);
274 }
275
276 @Override
277 public boolean onKeyUp(int keyCode, KeyEvent event) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700278 return common.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 }
280
281 @Override
282 public boolean onKeyDown(int keyCode, KeyEvent event) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700283 return common.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800284 }
285
286 public boolean isInCallScreenAnimating() {
287 return false;
288 }
289
290 public void showConferenceFragment(boolean show) {
291 if (show) {
292 startActivity(new Intent(this, ManageConferenceActivity.class));
293 }
294 }
295
296 public boolean showDialpadFragment(boolean show, boolean animate) {
297 boolean didChange = common.showDialpadFragment(show, animate);
298 if (didChange) {
299 // Note: onInCallScreenDialpadVisibilityChange is called here to ensure that the dialpad FAB
300 // repositions itself.
301 getInCallScreen().onInCallScreenDialpadVisibilityChange(show);
302 }
303 return didChange;
304 }
305
306 public boolean isDialpadVisible() {
307 return common.isDialpadVisible();
308 }
309
310 public void onForegroundCallChanged(DialerCall newForegroundCall) {
311 common.updateTaskDescription();
312 if (didShowAnswerScreen && newForegroundCall != null) {
313 if (newForegroundCall.getState() == State.DISCONNECTED
314 || newForegroundCall.getState() == State.IDLE) {
315 LogUtil.i(
316 "InCallActivity.onForegroundCallChanged",
317 "rejecting incoming call, not updating " + "window background color");
318 }
319 } else {
320 LogUtil.v("InCallActivity.onForegroundCallChanged", "resetting background color");
321 updateWindowBackgroundColor(0);
322 }
323 }
324
325 public void updateWindowBackgroundColor(@FloatRange(from = -1f, to = 1.0f) float progress) {
326 ThemeColorManager themeColorManager = InCallPresenter.getInstance().getThemeColorManager();
327 @ColorInt int top;
328 @ColorInt int middle;
329 @ColorInt int bottom;
330 @ColorInt int gray = 0x66000000;
331
332 if (ActivityCompat.isInMultiWindowMode(this)) {
333 top = themeColorManager.getBackgroundColorSolid();
334 middle = themeColorManager.getBackgroundColorSolid();
335 bottom = themeColorManager.getBackgroundColorSolid();
336 } else {
337 top = themeColorManager.getBackgroundColorTop();
338 middle = themeColorManager.getBackgroundColorMiddle();
339 bottom = themeColorManager.getBackgroundColorBottom();
340 }
341
342 if (progress < 0) {
343 float correctedProgress = Math.abs(progress);
344 top = ColorUtils.blendARGB(top, gray, correctedProgress);
345 middle = ColorUtils.blendARGB(middle, gray, correctedProgress);
346 bottom = ColorUtils.blendARGB(bottom, gray, correctedProgress);
347 }
348
349 boolean backgroundDirty = false;
350 if (backgroundDrawable == null) {
351 backgroundDrawableColors = new int[] {top, middle, bottom};
352 backgroundDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, backgroundDrawableColors);
353 backgroundDirty = true;
354 } else {
355 if (backgroundDrawableColors[0] != top) {
356 backgroundDrawableColors[0] = top;
357 backgroundDirty = true;
358 }
359 if (backgroundDrawableColors[1] != middle) {
360 backgroundDrawableColors[1] = middle;
361 backgroundDirty = true;
362 }
363 if (backgroundDrawableColors[2] != bottom) {
364 backgroundDrawableColors[2] = bottom;
365 backgroundDirty = true;
366 }
367 if (backgroundDirty) {
368 backgroundDrawable.setColors(backgroundDrawableColors);
369 }
370 }
371
372 if (backgroundDirty) {
373 getWindow().setBackgroundDrawable(backgroundDrawable);
374 }
375 }
376
377 public boolean isVisible() {
378 return isVisible;
379 }
380
381 public boolean getCallCardFragmentVisible() {
382 return didShowInCallScreen || didShowVideoCallScreen;
383 }
384
385 public void dismissKeyguard(boolean dismiss) {
386 common.dismissKeyguard(dismiss);
387 }
388
389 public void showPostCharWaitDialog(String callId, String chars) {
390 common.showPostCharWaitDialog(callId, chars);
391 }
392
393 public void maybeShowErrorDialogOnDisconnect(DisconnectCause disconnectCause) {
394 common.maybeShowErrorDialogOnDisconnect(disconnectCause);
395 }
396
397 public void dismissPendingDialogs() {
398 if (isVisible) {
399 LogUtil.i("InCallActivity.dismissPendingDialogs", "");
400 common.dismissPendingDialogs();
401 AnswerScreen answerScreen = getAnswerScreen();
402 if (answerScreen != null) {
403 answerScreen.dismissPendingDialogs();
404 }
405 needDismissPendingDialogs = false;
406 } else {
407 // The activity is not visible and onSaveInstanceState may have been called so defer the
408 // dismissing action.
409 LogUtil.i(
410 "InCallActivity.dismissPendingDialogs", "defer actions since activity is not visible");
411 needDismissPendingDialogs = true;
412 }
413 }
414
415 private void enableInCallOrientationEventListener(boolean enable) {
416 common.enableInCallOrientationEventListener(enable);
417 }
418
419 public void setExcludeFromRecents(boolean exclude) {
420 common.setExcludeFromRecents(exclude);
421 }
422
Eric Erfanianccca3152017-02-22 16:32:36 -0800423 @Nullable
424 public FragmentManager getDialpadFragmentManager() {
425 InCallScreen inCallScreen = getInCallScreen();
426 if (inCallScreen != null) {
427 return inCallScreen.getInCallScreenFragment().getChildFragmentManager();
428 }
429 return null;
430 }
431
432 public int getDialpadContainerId() {
433 return getInCallScreen().getAnswerAndDialpadContainerResourceId();
434 }
435
436 @Override
437 public AnswerScreenDelegate newAnswerScreenDelegate(AnswerScreen answerScreen) {
438 DialerCall call = CallList.getInstance().getCallById(answerScreen.getCallId());
439 if (call == null) {
440 // This is a work around for a bug where we attempt to create a new delegate after the call
441 // has already been removed. An example of when this can happen is:
442 // 1. incoming video call in landscape mode
443 // 2. remote party hangs up
444 // 3. activity switches from landscape to portrait
445 // At step #3 the answer fragment will try to create a new answer delegate but the call won't
446 // exist. In this case we'll simply return a stub delegate that does nothing. This is ok
447 // because this new state is transient and the activity will be destroyed soon.
448 LogUtil.i("InCallActivity.onPrimaryCallStateChanged", "call doesn't exist, using stub");
449 return new AnswerScreenPresenterStub();
450 } else {
451 return new AnswerScreenPresenter(
452 this, answerScreen, CallList.getInstance().getCallById(answerScreen.getCallId()));
453 }
454 }
455
456 @Override
457 public InCallScreenDelegate newInCallScreenDelegate() {
458 return new CallCardPresenter(this);
459 }
460
461 @Override
462 public InCallButtonUiDelegate newInCallButtonUiDelegate() {
463 return new CallButtonPresenter(this);
464 }
465
466 @Override
Eric Erfanian90508232017-03-24 09:31:16 -0700467 public VideoCallScreenDelegate newVideoCallScreenDelegate(VideoCallScreen videoCallScreen) {
468 DialerCall dialerCall = CallList.getInstance().getCallById(videoCallScreen.getCallId());
469 if (dialerCall != null && dialerCall.getVideoTech().shouldUseSurfaceView()) {
470 return dialerCall.getVideoTech().createVideoCallScreenDelegate(this, videoCallScreen);
471 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800472 return new VideoCallPresenter();
473 }
474
475 public void onPrimaryCallStateChanged() {
476 LogUtil.i("InCallActivity.onPrimaryCallStateChanged", "");
477 showMainInCallFragment();
478 }
479
480 public void onWiFiToLteHandover(DialerCall call) {
481 common.showWifiToLteHandoverToast(call);
482 }
483
484 public void onHandoverToWifiFailed(DialerCall call) {
485 common.showWifiFailedDialog(call);
486 }
487
488 public void setAllowOrientationChange(boolean allowOrientationChange) {
489 if (!allowOrientationChange) {
490 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_DISALLOW_ROTATION);
491 } else {
492 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
493 }
494 enableInCallOrientationEventListener(allowOrientationChange);
495 }
496
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700497 public void hideMainInCallFragment() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800498 LogUtil.i("InCallActivity.hideMainInCallFragment", "");
499 if (didShowInCallScreen || didShowVideoCallScreen) {
500 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
501 hideInCallScreenFragment(transaction);
502 hideVideoCallScreenFragment(transaction);
503 transaction.commitAllowingStateLoss();
504 getSupportFragmentManager().executePendingTransactions();
505 }
506 }
507
508 private void showMainInCallFragment() {
509 // If the activity's onStart method hasn't been called yet then defer doing any work.
510 if (!isVisible) {
511 LogUtil.i("InCallActivity.showMainInCallFragment", "not visible yet/anymore");
512 return;
513 }
514
515 // Don't let this be reentrant.
516 if (isInShowMainInCallFragment) {
517 LogUtil.i("InCallActivity.showMainInCallFragment", "already in method, bailing");
518 return;
519 }
520
521 isInShowMainInCallFragment = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700522 ShouldShowUiResult shouldShowAnswerUi = getShouldShowAnswerUi();
523 ShouldShowUiResult shouldShowVideoUi = getShouldShowVideoUi();
Eric Erfanianccca3152017-02-22 16:32:36 -0800524 LogUtil.i(
525 "InCallActivity.showMainInCallFragment",
526 "shouldShowAnswerUi: %b, shouldShowVideoUi: %b, "
527 + "didShowAnswerScreen: %b, didShowInCallScreen: %b, didShowVideoCallScreen: %b",
528 shouldShowAnswerUi.shouldShow,
529 shouldShowVideoUi,
530 didShowAnswerScreen,
531 didShowInCallScreen,
532 didShowVideoCallScreen);
533 // Only video call ui allows orientation change.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700534 setAllowOrientationChange(shouldShowVideoUi.shouldShow);
Eric Erfanianccca3152017-02-22 16:32:36 -0800535
536 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
537 boolean didChangeInCall;
538 boolean didChangeVideo;
539 boolean didChangeAnswer;
540 if (shouldShowAnswerUi.shouldShow) {
541 didChangeInCall = hideInCallScreenFragment(transaction);
542 didChangeVideo = hideVideoCallScreenFragment(transaction);
543 didChangeAnswer = showAnswerScreenFragment(transaction, shouldShowAnswerUi.call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700544 } else if (shouldShowVideoUi.shouldShow) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 didChangeInCall = hideInCallScreenFragment(transaction);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700546 didChangeVideo = showVideoCallScreenFragment(transaction, shouldShowVideoUi.call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800547 didChangeAnswer = hideAnswerScreenFragment(transaction);
548 } else {
549 didChangeInCall = showInCallScreenFragment(transaction);
550 didChangeVideo = hideVideoCallScreenFragment(transaction);
551 didChangeAnswer = hideAnswerScreenFragment(transaction);
552 }
553
554 if (didChangeInCall || didChangeVideo || didChangeAnswer) {
555 transaction.commitNow();
556 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
557 }
558 isInShowMainInCallFragment = false;
559 }
560
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700561 private ShouldShowUiResult getShouldShowAnswerUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800562 DialerCall call = CallList.getInstance().getIncomingCall();
563 if (call != null) {
564 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700565 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800566 }
567
568 call = CallList.getInstance().getVideoUpgradeRequestCall();
569 if (call != null) {
570 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found video upgrade request");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700571 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 }
573
574 // Check if we're showing the answer screen and the call is disconnected. If this condition is
575 // true then we won't switch from the answer UI to the in call UI. This prevents flicker when
576 // the user rejects an incoming call.
577 call = CallList.getInstance().getFirstCall();
578 if (call == null) {
579 call = CallList.getInstance().getBackgroundCall();
580 }
581 if (didShowAnswerScreen && (call == null || call.getState() == State.DISCONNECTED)) {
582 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found disconnecting incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700583 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800584 }
585
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700586 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800587 }
588
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700589 private static ShouldShowUiResult getShouldShowVideoUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800590 DialerCall call = CallList.getInstance().getFirstCall();
591 if (call == null) {
592 LogUtil.i("InCallActivity.getShouldShowVideoUi", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700593 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800594 }
595
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700596 if (call.isVideoCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800597 LogUtil.i("InCallActivity.getShouldShowVideoUi", "found video call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700598 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 }
600
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700601 if (call.hasSentVideoUpgradeRequest()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800602 LogUtil.i("InCallActivity.getShouldShowVideoUi", "upgrading to video");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700603 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 }
605
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700606 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800607 }
608
609 private boolean showAnswerScreenFragment(FragmentTransaction transaction, DialerCall call) {
610 // When rejecting a call the active call can become null in which case we should continue
611 // showing the answer screen.
612 if (didShowAnswerScreen && call == null) {
613 return false;
614 }
615
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700616 Assert.checkArgument(call != null, "didShowAnswerScreen was false but call was still null");
617
618 boolean isVideoUpgradeRequest = call.hasReceivedVideoUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800619
620 // Check if we're already showing an answer screen for this call.
621 if (didShowAnswerScreen) {
622 AnswerScreen answerScreen = getAnswerScreen();
623 if (answerScreen.getCallId().equals(call.getId())
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700624 && answerScreen.isVideoCall() == call.isVideoCall()
Eric Erfanianccca3152017-02-22 16:32:36 -0800625 && answerScreen.isVideoUpgradeRequest() == isVideoUpgradeRequest) {
626 return false;
627 }
628 LogUtil.i(
629 "InCallActivity.showAnswerScreenFragment",
630 "answer fragment exists but arguments do not match");
631 hideAnswerScreenFragment(transaction);
632 }
633
634 // Show a new answer screen.
635 AnswerScreen answerScreen =
Eric Erfanianfc37b022017-03-21 10:11:17 -0700636 AnswerBindings.createAnswerScreen(
637 call.getId(),
638 call.isVideoCall(),
639 isVideoUpgradeRequest,
Eric Erfanian90508232017-03-24 09:31:16 -0700640 call.getVideoTech().isSelfManagedCamera(),
641 shouldAllowAnswerAndRelease(call),
642 CallList.getInstance().getBackgroundCall() != null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800643 transaction.add(R.id.main, answerScreen.getAnswerScreenFragment(), TAG_ANSWER_SCREEN);
644
645 Logger.get(this).logScreenView(ScreenEvent.Type.INCOMING_CALL, this);
646 didShowAnswerScreen = true;
647 return true;
648 }
649
Eric Erfanian90508232017-03-24 09:31:16 -0700650 private boolean shouldAllowAnswerAndRelease(DialerCall call) {
651 if (CallList.getInstance().getActiveCall() == null) {
652 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "no active call");
653 return false;
654 }
655 if (getSystemService(TelephonyManager.class).getPhoneType()
656 == TelephonyManager.PHONE_TYPE_CDMA) {
657 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "PHONE_TYPE_CDMA not supported");
658 return false;
659 }
660 if (call.isVideoCall() || call.hasReceivedVideoUpgradeRequest()) {
661 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "video call");
662 return false;
663 }
664 if (!ConfigProviderBindings.get(this).getBoolean(CONFIG_ANSWER_AND_RELEASE_ENABLED, true)) {
665 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "disabled by config");
666 return false;
667 }
668
669 return true;
670 }
671
Eric Erfanianccca3152017-02-22 16:32:36 -0800672 private boolean hideAnswerScreenFragment(FragmentTransaction transaction) {
673 if (!didShowAnswerScreen) {
674 return false;
675 }
676 AnswerScreen answerScreen = getAnswerScreen();
677 if (answerScreen != null) {
678 transaction.remove(answerScreen.getAnswerScreenFragment());
679 }
680
681 didShowAnswerScreen = false;
682 return true;
683 }
684
685 private boolean showInCallScreenFragment(FragmentTransaction transaction) {
686 if (didShowInCallScreen) {
687 return false;
688 }
689 InCallScreen inCallScreen = getInCallScreen();
690 if (inCallScreen == null) {
691 inCallScreen = InCallBindings.createInCallScreen();
692 transaction.add(R.id.main, inCallScreen.getInCallScreenFragment(), TAG_IN_CALL_SCREEN);
693 } else {
694 transaction.show(inCallScreen.getInCallScreenFragment());
695 }
696 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
697 didShowInCallScreen = true;
698 return true;
699 }
700
701 private boolean hideInCallScreenFragment(FragmentTransaction transaction) {
702 if (!didShowInCallScreen) {
703 return false;
704 }
705 InCallScreen inCallScreen = getInCallScreen();
706 if (inCallScreen != null) {
707 transaction.hide(inCallScreen.getInCallScreenFragment());
708 }
709 didShowInCallScreen = false;
710 return true;
711 }
712
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700713 private boolean showVideoCallScreenFragment(FragmentTransaction transaction, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 if (didShowVideoCallScreen) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700715 VideoCallScreen videoCallScreen = getVideoCallScreen();
716 if (videoCallScreen.getCallId().equals(call.getId())) {
717 return false;
718 }
719 LogUtil.i(
720 "InCallActivity.showVideoCallScreenFragment",
721 "video call fragment exists but arguments do not match");
722 hideVideoCallScreenFragment(transaction);
Eric Erfanianccca3152017-02-22 16:32:36 -0800723 }
724
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700725 LogUtil.i("InCallActivity.showVideoCallScreenFragment", "call: %s", call);
726
Eric Erfanian90508232017-03-24 09:31:16 -0700727 VideoCallScreen videoCallScreen =
728 VideoBindings.createVideoCallScreen(
729 call.getId(), call.getVideoTech().shouldUseSurfaceView());
Eric Erfanianccca3152017-02-22 16:32:36 -0800730 transaction.add(R.id.main, videoCallScreen.getVideoCallScreenFragment(), TAG_VIDEO_CALL_SCREEN);
731
732 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
733 didShowVideoCallScreen = true;
734 return true;
735 }
736
737 private boolean hideVideoCallScreenFragment(FragmentTransaction transaction) {
738 if (!didShowVideoCallScreen) {
739 return false;
740 }
741 VideoCallScreen videoCallScreen = getVideoCallScreen();
742 if (videoCallScreen != null) {
743 transaction.remove(videoCallScreen.getVideoCallScreenFragment());
744 }
745 didShowVideoCallScreen = false;
746 return true;
747 }
748
749 AnswerScreen getAnswerScreen() {
750 return (AnswerScreen) getSupportFragmentManager().findFragmentByTag(TAG_ANSWER_SCREEN);
751 }
752
753 InCallScreen getInCallScreen() {
754 return (InCallScreen) getSupportFragmentManager().findFragmentByTag(TAG_IN_CALL_SCREEN);
755 }
756
757 VideoCallScreen getVideoCallScreen() {
758 return (VideoCallScreen) getSupportFragmentManager().findFragmentByTag(TAG_VIDEO_CALL_SCREEN);
759 }
760
761 @Override
762 public void onPseudoScreenStateChanged(boolean isOn) {
763 LogUtil.i("InCallActivity.onPseudoScreenStateChanged", "isOn: " + isOn);
764 pseudoBlackScreenOverlay.setVisibility(isOn ? View.GONE : View.VISIBLE);
765 }
766
767 /**
768 * For some touch related issue, turning off the screen can be faked by drawing a black view over
769 * the activity. All touch events started when the screen is "off" is rejected.
770 *
771 * @see PseudoScreenState
772 */
773 @Override
774 public boolean dispatchTouchEvent(MotionEvent event) {
775 // Reject any gesture that started when the screen is in the fake off state.
776 if (touchDownWhenPseudoScreenOff) {
777 if (event.getAction() == MotionEvent.ACTION_UP) {
778 touchDownWhenPseudoScreenOff = false;
779 }
780 return true;
781 }
782 // Reject all touch event when the screen is in the fake off state.
783 if (!InCallPresenter.getInstance().getPseudoScreenState().isOn()) {
784 if (event.getAction() == MotionEvent.ACTION_DOWN) {
785 touchDownWhenPseudoScreenOff = true;
786 LogUtil.i("InCallActivity.dispatchTouchEvent", "touchDownWhenPseudoScreenOff");
787 }
788 return true;
789 }
790 return super.dispatchTouchEvent(event);
791 }
792
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700793 private static class ShouldShowUiResult {
Eric Erfanianccca3152017-02-22 16:32:36 -0800794 public final boolean shouldShow;
795 public final DialerCall call;
796
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700797 ShouldShowUiResult(boolean shouldShow, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800798 this.shouldShow = shouldShow;
799 this.call = call;
800 }
801 }
802}