summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/VideoCallPresenter.java
blob: a10602227309bbad5564676ee580692aa61fe325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.incallui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.telecom.InCallService.VideoCall;
import android.telecom.VideoProfile;
import android.telecom.VideoProfile.CameraCapabilities;
import android.view.Surface;
import android.view.SurfaceView;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.compat.CompatUtils;
import com.android.dialer.configprovider.ConfigProviderBindings;
import com.android.dialer.util.PermissionsUtil;
import com.android.incallui.InCallPresenter.InCallDetailsListener;
import com.android.incallui.InCallPresenter.InCallOrientationListener;
import com.android.incallui.InCallPresenter.InCallStateListener;
import com.android.incallui.InCallPresenter.IncomingCallListener;
import com.android.incallui.call.CallList;
import com.android.incallui.call.DialerCall;
import com.android.incallui.call.DialerCall.CameraDirection;
import com.android.incallui.call.DialerCall.State;
import com.android.incallui.call.InCallVideoCallCallbackNotifier;
import com.android.incallui.call.InCallVideoCallCallbackNotifier.SurfaceChangeListener;
import com.android.incallui.util.AccessibilityUtil;
import com.android.incallui.video.protocol.VideoCallScreen;
import com.android.incallui.video.protocol.VideoCallScreenDelegate;
import com.android.incallui.videosurface.protocol.VideoSurfaceDelegate;
import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
import com.android.incallui.videotech.utils.SessionModificationState;
import com.android.incallui.videotech.utils.VideoUtils;
import java.util.Objects;

/**
 * Logic related to the {@link VideoCallScreen} and for managing changes to the video calling
 * surfaces based on other user interface events and incoming events from the {@class
 * VideoCallListener}.
 *
 * <p>When a call's video state changes to bi-directional video, the {@link
 * com.android.incallui.VideoCallPresenter} performs the following negotiation with the telephony
 * layer:
 *
 * <ul>
 * <li>{@code VideoCallPresenter} creates and informs telephony of the display surface.
 * <li>{@code VideoCallPresenter} creates the preview surface.
 * <li>{@code VideoCallPresenter} informs telephony of the currently selected camera.
 * <li>Telephony layer sends {@link CameraCapabilities}, including the dimensions of the video for
 *     the current camera.
 * <li>{@code VideoCallPresenter} adjusts size of the preview surface to match the aspect ratio of
 *     the camera.
 * <li>{@code VideoCallPresenter} informs telephony of the new preview surface.
 * </ul>
 *
 * <p>When downgrading to an audio-only video state, the {@code VideoCallPresenter} nulls both
 * surfaces.
 */
public class VideoCallPresenter
    implements IncomingCallListener,
        InCallOrientationListener,
        InCallStateListener,
        InCallDetailsListener,
        SurfaceChangeListener,
        InCallPresenter.InCallEventListener,
        VideoCallScreenDelegate {

  private static boolean mIsVideoMode = false;

  private final Handler mHandler = new Handler();
  private VideoCallScreen mVideoCallScreen;

  /** The current context. */
  private Context mContext;

  /** The call the video surfaces are currently related to */
  private DialerCall mPrimaryCall;
  /**
   * The {@link VideoCall} used to inform the video telephony layer of changes to the video
   * surfaces.
   */
  private VideoCall mVideoCall;
  /** Determines if the current UI state represents a video call. */
  private int mCurrentVideoState;
  /** DialerCall's current state */
  private int mCurrentCallState = DialerCall.State.INVALID;
  /** Determines the device orientation (portrait/lanscape). */
  private int mDeviceOrientation = InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN;
  /** Tracks the state of the preview surface negotiation with the telephony layer. */
  private int mPreviewSurfaceState = PreviewSurfaceState.NONE;
  /**
   * Determines whether video calls should automatically enter full screen mode after {@link
   * #mAutoFullscreenTimeoutMillis} milliseconds.
   */
  private boolean mIsAutoFullscreenEnabled = false;
  /**
   * Determines the number of milliseconds after which a video call will automatically enter
   * fullscreen mode. Requires {@link #mIsAutoFullscreenEnabled} to be {@code true}.
   */
  private int mAutoFullscreenTimeoutMillis = 0;
  /**
   * Determines if the countdown is currently running to automatically enter full screen video mode.
   */
  private boolean mAutoFullScreenPending = false;
  /** Whether if the call is remotely held. */
  private boolean mIsRemotelyHeld = false;
  /**
   * Runnable which is posted to schedule automatically entering fullscreen mode. Will not auto
   * enter fullscreen mode if the dialpad is visible (doing so would make it impossible to exit the
   * dialpad).
   */
  private Runnable mAutoFullscreenRunnable =
      new Runnable() {
        @Override
        public void run() {
          if (mAutoFullScreenPending
              && !InCallPresenter.getInstance().isDialpadVisible()
              && mIsVideoMode) {

            LogUtil.v("VideoCallPresenter.mAutoFullScreenRunnable", "entering fullscreen mode");
            InCallPresenter.getInstance().setFullScreen(true);
            mAutoFullScreenPending = false;
          } else {
            LogUtil.v(
                "VideoCallPresenter.mAutoFullScreenRunnable",
                "skipping scheduled fullscreen mode.");
          }
        }
      };

  private boolean isVideoCallScreenUiReady;

  private static boolean isCameraRequired(int videoState, int sessionModificationState) {
    return VideoProfile.isBidirectional(videoState)
        || VideoProfile.isTransmissionEnabled(videoState)
        || isVideoUpgrade(sessionModificationState);
  }

  /**
   * Determines if the incoming video surface should be shown based on the current videoState and
   * callState. The video surface is shown when incoming video is not paused, the call is active or
   * dialing and video reception is enabled.
   *
   * @param videoState The current video state.
   * @param callState The current call state.
   * @return {@code true} if the incoming video surface should be shown, {@code false} otherwise.
   */
  public static boolean showIncomingVideo(int videoState, int callState) {
    if (!CompatUtils.isVideoCompatible()) {
      return false;
    }

    boolean isPaused = VideoProfile.isPaused(videoState);
    boolean isCallActive = callState == DialerCall.State.ACTIVE;
    //Show incoming Video for dialing calls to support early media
    boolean isCallOutgoingPending =
        DialerCall.State.isDialing(callState) || callState == DialerCall.State.CONNECTING;

    return !isPaused
        && (isCallActive || isCallOutgoingPending)
        && VideoProfile.isReceptionEnabled(videoState);
  }

  /**
   * Determines if the outgoing video surface should be shown based on the current videoState. The
   * video surface is shown if video transmission is enabled.
   *
   * @return {@code true} if the the outgoing video surface should be shown, {@code false}
   *     otherwise.
   */
  public static boolean showOutgoingVideo(
      Context context, int videoState, int sessionModificationState) {
    if (!VideoUtils.hasCameraPermissionAndShownPrivacyToast(context)) {
      LogUtil.i("VideoCallPresenter.showOutgoingVideo", "Camera permission is disabled by user.");
      return false;
    }

    if (!CompatUtils.isVideoCompatible()) {
      return false;
    }

    return VideoProfile.isTransmissionEnabled(videoState)
        || isVideoUpgrade(sessionModificationState);
  }

  private static void updateCameraSelection(DialerCall call) {
    LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + call);
    LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + toSimpleString(call));

    final DialerCall activeCall = CallList.getInstance().getActiveCall();
    int cameraDir;

    // this function should never be called with null call object, however if it happens we
    // should handle it gracefully.
    if (call == null) {
      cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
      LogUtil.e(
          "VideoCallPresenter.updateCameraSelection",
          "call is null. Setting camera direction to default value (CAMERA_DIRECTION_UNKNOWN)");
    }

    // Clear camera direction if this is not a video call.
    else if (isAudioCall(call) && !isVideoUpgrade(call)) {
      cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
      call.setCameraDir(cameraDir);
    }

    // If this is a waiting video call, default to active call's camera,
    // since we don't want to change the current camera for waiting call
    // without user's permission.
    else if (isVideoCall(activeCall) && isIncomingVideoCall(call)) {
      cameraDir = activeCall.getCameraDir();
    }

    // Infer the camera direction from the video state and store it,
    // if this is an outgoing video call.
    else if (isOutgoingVideoCall(call) && !isCameraDirectionSet(call)) {
      cameraDir = toCameraDirection(call.getVideoState());
      call.setCameraDir(cameraDir);
    }

    // Use the stored camera dir if this is an outgoing video call for which camera direction
    // is set.
    else if (isOutgoingVideoCall(call)) {
      cameraDir = call.getCameraDir();
    }

    // Infer the camera direction from the video state and store it,
    // if this is an active video call and camera direction is not set.
    else if (isActiveVideoCall(call) && !isCameraDirectionSet(call)) {
      cameraDir = toCameraDirection(call.getVideoState());
      call.setCameraDir(cameraDir);
    }

    // Use the stored camera dir if this is an active video call for which camera direction
    // is set.
    else if (isActiveVideoCall(call)) {
      cameraDir = call.getCameraDir();
    }

    // For all other cases infer the camera direction but don't store it in the call object.
    else {
      cameraDir = toCameraDirection(call.getVideoState());
    }

    LogUtil.i(
        "VideoCallPresenter.updateCameraSelection",
        "setting camera direction to %d, call: %s",
        cameraDir,
        call);
    final InCallCameraManager cameraManager =
        InCallPresenter.getInstance().getInCallCameraManager();
    cameraManager.setUseFrontFacingCamera(
        cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING);
  }

  private static int toCameraDirection(int videoState) {
    return VideoProfile.isTransmissionEnabled(videoState)
            && !VideoProfile.isBidirectional(videoState)
        ? CameraDirection.CAMERA_DIRECTION_BACK_FACING
        : CameraDirection.CAMERA_DIRECTION_FRONT_FACING;
  }

  private static boolean isCameraDirectionSet(DialerCall call) {
    return isVideoCall(call) && call.getCameraDir() != CameraDirection.CAMERA_DIRECTION_UNKNOWN;
  }

  private static String toSimpleString(DialerCall call) {
    return call == null ? null : call.toSimpleString();
  }

  /**
   * Initializes the presenter.
   *
   * @param context The current context.
   */
  @Override
  public void initVideoCallScreenDelegate(Context context, VideoCallScreen videoCallScreen) {
    mContext = context;
    mVideoCallScreen = videoCallScreen;
    mIsAutoFullscreenEnabled =
        mContext.getResources().getBoolean(R.bool.video_call_auto_fullscreen);
    mAutoFullscreenTimeoutMillis =
        mContext.getResources().getInteger(R.integer.video_call_auto_fullscreen_timeout);
  }

  /** Called when the user interface is ready to be used. */
  @Override
  public void onVideoCallScreenUiReady() {
    LogUtil.v("VideoCallPresenter.onVideoCallScreenUiReady", "");
    Assert.checkState(!isVideoCallScreenUiReady);

    // Do not register any listeners if video calling is not compatible to safeguard against
    // any accidental calls of video calling code.
    if (!CompatUtils.isVideoCompatible()) {
      return;
    }

    mDeviceOrientation = InCallOrientationEventListener.getCurrentOrientation();

    // Register for call state changes last
    InCallPresenter.getInstance().addListener(this);
    InCallPresenter.getInstance().addDetailsListener(this);
    InCallPresenter.getInstance().addIncomingCallListener(this);
    InCallPresenter.getInstance().addOrientationListener(this);
    // To get updates of video call details changes
    InCallPresenter.getInstance().addInCallEventListener(this);
    InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(new LocalDelegate());
    InCallPresenter.getInstance().getRemoteVideoSurfaceTexture().setDelegate(new RemoteDelegate());

    // Register for surface and video events from {@link InCallVideoCallListener}s.
    InCallVideoCallCallbackNotifier.getInstance().addSurfaceChangeListener(this);
    mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY;
    mCurrentCallState = DialerCall.State.INVALID;

    InCallPresenter.InCallState inCallState = InCallPresenter.getInstance().getInCallState();
    onStateChange(inCallState, inCallState, CallList.getInstance());
    isVideoCallScreenUiReady = true;
  }

  /** Called when the user interface is no longer ready to be used. */
  @Override
  public void onVideoCallScreenUiUnready() {
    LogUtil.v("VideoCallPresenter.onVideoCallScreenUiUnready", "");
    Assert.checkState(isVideoCallScreenUiReady);

    if (!CompatUtils.isVideoCompatible()) {
      return;
    }

    cancelAutoFullScreen();

    InCallPresenter.getInstance().removeListener(this);
    InCallPresenter.getInstance().removeDetailsListener(this);
    InCallPresenter.getInstance().removeIncomingCallListener(this);
    InCallPresenter.getInstance().removeOrientationListener(this);
    InCallPresenter.getInstance().removeInCallEventListener(this);
    InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(null);

    InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this);

    // Ensure that the call's camera direction is updated (most likely to UNKNOWN). Normally this
    // happens after any call state changes but we're unregistering from InCallPresenter above so
    // we won't get any more call state changes. See b/32957114.
    if (mPrimaryCall != null) {
      updateCameraSelection(mPrimaryCall);
    }

    isVideoCallScreenUiReady = false;
  }

  /**
   * Handles clicks on the video surfaces. If not currently in fullscreen mode, will set fullscreen.
   */
  private void onSurfaceClick() {
    LogUtil.i("VideoCallPresenter.onSurfaceClick", "");
    cancelAutoFullScreen();
    if (!InCallPresenter.getInstance().isFullscreen()) {
      InCallPresenter.getInstance().setFullScreen(true);
    } else {
      InCallPresenter.getInstance().setFullScreen(false);
      maybeAutoEnterFullscreen(mPrimaryCall);
      // If Activity is not multiwindow, fullscreen will be driven by SystemUI visibility changes
      // instead. See #onSystemUiVisibilityChange(boolean)

      // TODO (keyboardr): onSystemUiVisibilityChange isn't being called the first time
      // visibility changes after orientation change, so this is currently always done as a backup.
    }
  }

  @Override
  public void onSystemUiVisibilityChange(boolean visible) {
    // If the SystemUI has changed to be visible, take us out of fullscreen mode
    LogUtil.i("VideoCallPresenter.onSystemUiVisibilityChange", "visible: " + visible);
    if (visible) {
      InCallPresenter.getInstance().setFullScreen(false);
      maybeAutoEnterFullscreen(mPrimaryCall);
    }
  }

  @Override
  public VideoSurfaceTexture getLocalVideoSurfaceTexture() {
    return InCallPresenter.getInstance().getLocalVideoSurfaceTexture();
  }

  @Override
  public VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
    return InCallPresenter.getInstance().getRemoteVideoSurfaceTexture();
  }

  @Override
  public void setSurfaceViews(SurfaceView preview, SurfaceView remote) {
    throw Assert.createUnsupportedOperationFailException();
  }

  @Override
  public int getDeviceOrientation() {
    return mDeviceOrientation;
  }

  /**
   * This should only be called when user approved the camera permission, which is local action and
   * does NOT change any call states.
   */
  @Override
  public void onCameraPermissionGranted() {
    LogUtil.i("VideoCallPresenter.onCameraPermissionGranted", "");
    PermissionsUtil.setCameraPrivacyToastShown(mContext);
    enableCamera(mPrimaryCall.getVideoCall(), isCameraRequired());
    showVideoUi(
        mPrimaryCall.getVideoState(),
        mPrimaryCall.getState(),
        mPrimaryCall.getVideoTech().getSessionModificationState(),
        mPrimaryCall.isRemotelyHeld());
    InCallPresenter.getInstance().getInCallCameraManager().onCameraPermissionGranted();
  }

  /**
   * Called when the user interacts with the UI. If a fullscreen timer is pending then we start the
   * timer from scratch to avoid having the UI disappear while the user is interacting with it.
   */
  @Override
  public void resetAutoFullscreenTimer() {
    if (mAutoFullScreenPending) {
      LogUtil.i("VideoCallPresenter.resetAutoFullscreenTimer", "resetting");
      mHandler.removeCallbacks(mAutoFullscreenRunnable);
      mHandler.postDelayed(mAutoFullscreenRunnable, mAutoFullscreenTimeoutMillis);
    }
  }

  /**
   * Handles incoming calls.
   *
   * @param oldState The old in call state.
   * @param newState The new in call state.
   * @param call The call.
   */
  @Override
  public void onIncomingCall(
      InCallPresenter.InCallState oldState, InCallPresenter.InCallState newState, DialerCall call) {
    // same logic should happen as with onStateChange()
    onStateChange(oldState, newState, CallList.getInstance());
  }

  /**
   * Handles state changes (including incoming calls)
   *
   * @param newState The in call state.
   * @param callList The call list.
   */
  @Override
  public void onStateChange(
      InCallPresenter.InCallState oldState,
      InCallPresenter.InCallState newState,
      CallList callList) {
    LogUtil.v(
        "VideoCallPresenter.onStateChange",
        "oldState: %s, newState: %s, isVideoMode: %b",
        oldState,
        newState,
        isVideoMode());

    if (newState == InCallPresenter.InCallState.NO_CALLS) {
      if (isVideoMode()) {
        exitVideoMode();
      }

      InCallPresenter.getInstance().cleanupSurfaces();
    }

    // Determine the primary active call).
    DialerCall primary = null;

    // Determine the call which is the focus of the user's attention.  In the case of an
    // incoming call waiting call, the primary call is still the active video call, however
    // the determination of whether we should be in fullscreen mode is based on the type of the
    // incoming call, not the active video call.
    DialerCall currentCall = null;

    if (newState == InCallPresenter.InCallState.INCOMING) {
      // We don't want to replace active video call (primary call)
      // with a waiting call, since user may choose to ignore/decline the waiting call and
      // this should have no impact on current active video call, that is, we should not
      // change the camera or UI unless the waiting VT call becomes active.
      primary = callList.getActiveCall();
      currentCall = callList.getIncomingCall();
      if (!isActiveVideoCall(primary)) {
        primary = callList.getIncomingCall();
      }
    } else if (newState == InCallPresenter.InCallState.OUTGOING) {
      currentCall = primary = callList.getOutgoingCall();
    } else if (newState == InCallPresenter.InCallState.PENDING_OUTGOING) {
      currentCall = primary = callList.getPendingOutgoingCall();
    } else if (newState == InCallPresenter.InCallState.INCALL) {
      currentCall = primary = callList.getActiveCall();
    }

    final boolean primaryChanged = !Objects.equals(mPrimaryCall, primary);
    LogUtil.i(
        "VideoCallPresenter.onStateChange",
        "primaryChanged: %b, primary: %s, mPrimaryCall: %s",
        primaryChanged,
        primary,
        mPrimaryCall);
    if (primaryChanged) {
      onPrimaryCallChanged(primary);
    } else if (mPrimaryCall != null) {
      updateVideoCall(primary);
    }
    updateCallCache(primary);

    // If the call context changed, potentially exit fullscreen or schedule auto enter of
    // fullscreen mode.
    // If the current call context is no longer a video call, exit fullscreen mode.
    maybeExitFullscreen(currentCall);
    // Schedule auto-enter of fullscreen mode if the current call context is a video call
    maybeAutoEnterFullscreen(currentCall);
  }

  /**
   * Handles a change to the fullscreen mode of the app.
   *
   * @param isFullscreenMode {@code true} if the app is now fullscreen, {@code false} otherwise.
   */
  @Override
  public void onFullscreenModeChanged(boolean isFullscreenMode) {
    cancelAutoFullScreen();
    if (mPrimaryCall != null) {
      updateFullscreenAndGreenScreenMode(
          mPrimaryCall.getState(), mPrimaryCall.getVideoTech().getSessionModificationState());
    } else {
      updateFullscreenAndGreenScreenMode(State.INVALID, SessionModificationState.NO_REQUEST);
    }
  }

  private void checkForVideoStateChange(DialerCall call) {
    final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
    final boolean hasVideoStateChanged = mCurrentVideoState != call.getVideoState();

    LogUtil.v(
        "VideoCallPresenter.checkForVideoStateChange",
        "shouldShowVideoUi: %b, hasVideoStateChanged: %b, isVideoMode: %b, previousVideoState: %s,"
            + " newVideoState: %s",
        shouldShowVideoUi,
        hasVideoStateChanged,
        isVideoMode(),
        VideoProfile.videoStateToString(mCurrentVideoState),
        VideoProfile.videoStateToString(call.getVideoState()));
    if (!hasVideoStateChanged) {
      return;
    }

    updateCameraSelection(call);

    if (shouldShowVideoUi) {
      adjustVideoMode(call);
    } else if (isVideoMode()) {
      exitVideoMode();
    }
  }

  private void checkForCallStateChange(DialerCall call) {
    final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
    final boolean hasCallStateChanged =
        mCurrentCallState != call.getState() || mIsRemotelyHeld != call.isRemotelyHeld();
    mIsRemotelyHeld = call.isRemotelyHeld();

    LogUtil.v(
        "VideoCallPresenter.checkForCallStateChange",
        "shouldShowVideoUi: %b, hasCallStateChanged: %b, isVideoMode: %b",
        shouldShowVideoUi,
        hasCallStateChanged,
        isVideoMode());

    if (!hasCallStateChanged) {
      return;
    }

    if (shouldShowVideoUi) {
      final InCallCameraManager cameraManager =
          InCallPresenter.getInstance().getInCallCameraManager();

      String prevCameraId = cameraManager.getActiveCameraId();
      updateCameraSelection(call);
      String newCameraId = cameraManager.getActiveCameraId();

      if (!Objects.equals(prevCameraId, newCameraId) && isActiveVideoCall(call)) {
        enableCamera(call.getVideoCall(), true);
      }
    }

    // Make sure we hide or show the video UI if needed.
    showVideoUi(
        call.getVideoState(),
        call.getState(),
        call.getVideoTech().getSessionModificationState(),
        call.isRemotelyHeld());
  }

  private void onPrimaryCallChanged(DialerCall newPrimaryCall) {
    final boolean shouldShowVideoUi = shouldShowVideoUiForCall(newPrimaryCall);
    final boolean isVideoMode = isVideoMode();

    LogUtil.v(
        "VideoCallPresenter.onPrimaryCallChanged",
        "shouldShowVideoUi: %b, isVideoMode: %b",
        shouldShowVideoUi,
        isVideoMode);

    if (!shouldShowVideoUi && isVideoMode) {
      // Terminate video mode if new primary call is not a video call
      // and we are currently in video mode.
      LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "exiting video mode...");
      exitVideoMode();
    } else if (shouldShowVideoUi) {
      LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "entering video mode...");

      updateCameraSelection(newPrimaryCall);
      adjustVideoMode(newPrimaryCall);
    }
    checkForOrientationAllowedChange(newPrimaryCall);
  }

  private boolean isVideoMode() {
    return mIsVideoMode;
  }

  private void updateCallCache(DialerCall call) {
    if (call == null) {
      mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY;
      mCurrentCallState = DialerCall.State.INVALID;
      mVideoCall = null;
      mPrimaryCall = null;
    } else {
      mCurrentVideoState = call.getVideoState();
      mVideoCall = call.getVideoCall();
      mCurrentCallState = call.getState();
      mPrimaryCall = call;
    }
  }

  /**
   * Handles changes to the details of the call. The {@link VideoCallPresenter} is interested in
   * changes to the video state.
   *
   * @param call The call for which the details changed.
   * @param details The new call details.
   */
  @Override
  public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
    LogUtil.v(
        "VideoCallPresenter.onDetailsChanged",
        "call: %s, details: %s, mPrimaryCall: %s",
        call,
        details,
        mPrimaryCall);
    if (call == null) {
      return;
    }
    // If the details change is not for the currently active call no update is required.
    if (!call.equals(mPrimaryCall)) {
      LogUtil.v("VideoCallPresenter.onDetailsChanged", "details not for current active call");
      return;
    }

    updateVideoCall(call);

    updateCallCache(call);
  }

  private void updateVideoCall(DialerCall call) {
    checkForVideoCallChange(call);
    checkForVideoStateChange(call);
    checkForCallStateChange(call);
    checkForOrientationAllowedChange(call);
    updateFullscreenAndGreenScreenMode(
        call.getState(), call.getVideoTech().getSessionModificationState());
  }

  private void checkForOrientationAllowedChange(@Nullable DialerCall call) {
    InCallPresenter.getInstance()
        .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call));
  }

  private void updateFullscreenAndGreenScreenMode(
      int callState, @SessionModificationState int sessionModificationState) {
    if (mVideoCallScreen != null) {
      boolean shouldShowFullscreen = InCallPresenter.getInstance().isFullscreen();
      boolean shouldShowGreenScreen =
          callState == State.DIALING
              || callState == State.CONNECTING
              || callState == State.INCOMING
              || isVideoUpgrade(sessionModificationState);
      mVideoCallScreen.updateFullscreenAndGreenScreenMode(
          shouldShowFullscreen, shouldShowGreenScreen);
    }
  }

  /** Checks for a change to the video call and changes it if required. */
  private void checkForVideoCallChange(DialerCall call) {
    final VideoCall videoCall = call.getVideoCall();
    LogUtil.v(
        "VideoCallPresenter.checkForVideoCallChange",
        "videoCall: %s, mVideoCall: %s",
        videoCall,
        mVideoCall);
    if (!Objects.equals(videoCall, mVideoCall)) {
      changeVideoCall(call);
    }
  }

  /**
   * Handles a change to the video call. Sets the surfaces on the previous call to null and sets the
   * surfaces on the new video call accordingly.
   *
   * @param call The new video call.
   */
  private void changeVideoCall(DialerCall call) {
    final VideoCall videoCall = call == null ? null : call.getVideoCall();
    LogUtil.i(
        "VideoCallPresenter.changeVideoCall",
        "videoCall: %s, mVideoCall: %s",
        videoCall,
        mVideoCall);
    final boolean hasChanged = mVideoCall == null && videoCall != null;

    mVideoCall = videoCall;
    if (mVideoCall == null) {
      LogUtil.v("VideoCallPresenter.changeVideoCall", "video call or primary call is null. Return");
      return;
    }

    if (shouldShowVideoUiForCall(call) && hasChanged) {
      adjustVideoMode(call);
    }
  }

  private boolean isCameraRequired() {
    return mPrimaryCall != null
        && isCameraRequired(
            mPrimaryCall.getVideoState(),
            mPrimaryCall.getVideoTech().getSessionModificationState());
  }

  /**
   * Adjusts the current video mode by setting up the preview and display surfaces as necessary.
   * Expected to be called whenever the video state associated with a call changes (e.g. a user
   * turns their camera on or off) to ensure the correct surfaces are shown/hidden. TODO: Need
   * to adjust size and orientation of preview surface here.
   */
  private void adjustVideoMode(DialerCall call) {
    VideoCall videoCall = call.getVideoCall();
    int newVideoState = call.getVideoState();

    LogUtil.i(
        "VideoCallPresenter.adjustVideoMode",
        "videoCall: %s, videoState: %d",
        videoCall,
        newVideoState);
    if (mVideoCallScreen == null) {
      LogUtil.e("VideoCallPresenter.adjustVideoMode", "error VideoCallScreen is null so returning");
      return;
    }

    showVideoUi(
        newVideoState,
        call.getState(),
        call.getVideoTech().getSessionModificationState(),
        call.isRemotelyHeld());

    // Communicate the current camera to telephony and make a request for the camera
    // capabilities.
    if (videoCall != null) {
      Surface surface = getRemoteVideoSurfaceTexture().getSavedSurface();
      if (surface != null) {
        LogUtil.v(
            "VideoCallPresenter.adjustVideoMode", "calling setDisplaySurface with: " + surface);
        videoCall.setDisplaySurface(surface);
      }

      Assert.checkState(
          mDeviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN);
      videoCall.setDeviceOrientation(mDeviceOrientation);
      enableCamera(
          videoCall,
          isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState()));
    }
    int previousVideoState = mCurrentVideoState;
    mCurrentVideoState = newVideoState;
    mIsVideoMode = true;

    // adjustVideoMode may be called if we are already in a 1-way video state.  In this case
    // we do not want to trigger auto-fullscreen mode.
    if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) {
      maybeAutoEnterFullscreen(call);
    }
  }

  private static boolean shouldShowVideoUiForCall(@Nullable DialerCall call) {
    if (call == null) {
      return false;
    }

    if (isVideoCall(call)) {
      return true;
    }

    if (isVideoUpgrade(call)) {
      return true;
    }

    return false;
  }

  private void enableCamera(VideoCall videoCall, boolean isCameraRequired) {
    LogUtil.v(
        "VideoCallPresenter.enableCamera",
        "videoCall: %s, enabling: %b",
        videoCall,
        isCameraRequired);
    if (videoCall == null) {
      LogUtil.i("VideoCallPresenter.enableCamera", "videoCall is null.");
      return;
    }

    boolean hasCameraPermission = VideoUtils.hasCameraPermissionAndShownPrivacyToast(mContext);
    if (!hasCameraPermission) {
      videoCall.setCamera(null);
      mPreviewSurfaceState = PreviewSurfaceState.NONE;
      // TODO: Inform remote party that the video is off. This is similar to b/30256571.
    } else if (isCameraRequired) {
      InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
      videoCall.setCamera(cameraManager.getActiveCameraId());
      mPreviewSurfaceState = PreviewSurfaceState.CAMERA_SET;
      videoCall.requestCameraCapabilities();
    } else {
      mPreviewSurfaceState = PreviewSurfaceState.NONE;
      videoCall.setCamera(null);
    }
  }

  /** Exits video mode by hiding the video surfaces and making other adjustments (eg. audio). */
  private void exitVideoMode() {
    LogUtil.i("VideoCallPresenter.exitVideoMode", "");

    showVideoUi(
        VideoProfile.STATE_AUDIO_ONLY,
        DialerCall.State.ACTIVE,
        SessionModificationState.NO_REQUEST,
        false /* isRemotelyHeld */);
    enableCamera(mVideoCall, false);
    InCallPresenter.getInstance().setFullScreen(false);

    mIsVideoMode = false;
  }

  /**
   * Based on the current video state and call state, show or hide the incoming and outgoing video
   * surfaces. The outgoing video surface is shown any time video is transmitting. The incoming
   * video surface is shown whenever the video is un-paused and active.
   *
   * @param videoState The video state.
   * @param callState The call state.
   */
  private void showVideoUi(
      int videoState,
      int callState,
      @SessionModificationState int sessionModificationState,
      boolean isRemotelyHeld) {
    if (mVideoCallScreen == null) {
      LogUtil.e("VideoCallPresenter.showVideoUi", "videoCallScreen is null returning");
      return;
    }
    boolean showIncomingVideo = showIncomingVideo(videoState, callState);
    boolean showOutgoingVideo = showOutgoingVideo(mContext, videoState, sessionModificationState);
    LogUtil.i(
        "VideoCallPresenter.showVideoUi",
        "showIncoming: %b, showOutgoing: %b, isRemotelyHeld: %b",
        showIncomingVideo,
        showOutgoingVideo,
        isRemotelyHeld);
    updateRemoteVideoSurfaceDimensions();
    mVideoCallScreen.showVideoViews(showOutgoingVideo, showIncomingVideo, isRemotelyHeld);

    InCallPresenter.getInstance().enableScreenTimeout(VideoProfile.isAudioOnly(videoState));
    updateFullscreenAndGreenScreenMode(callState, sessionModificationState);
  }

  /**
   * Handles peer video dimension changes.
   *
   * @param call The call which experienced a peer video dimension change.
   * @param width The new peer video width .
   * @param height The new peer video height.
   */
  @Override
  public void onUpdatePeerDimensions(DialerCall call, int width, int height) {
    LogUtil.i("VideoCallPresenter.onUpdatePeerDimensions", "width: %d, height: %d", width, height);
    if (mVideoCallScreen == null) {
      LogUtil.e("VideoCallPresenter.onUpdatePeerDimensions", "videoCallScreen is null");
      return;
    }
    if (!call.equals(mPrimaryCall)) {
      LogUtil.e(
          "VideoCallPresenter.onUpdatePeerDimensions", "current call is not equal to primary");
      return;
    }

    // Change size of display surface to match the peer aspect ratio
    if (width > 0 && height > 0 && mVideoCallScreen != null) {
      getRemoteVideoSurfaceTexture().setSourceVideoDimensions(new Point(width, height));
      mVideoCallScreen.onRemoteVideoDimensionsChanged();
    }
  }

  /**
   * Handles a change to the dimensions of the local camera. Receiving the camera capabilities
   * triggers the creation of the video
   *
   * @param call The call which experienced the camera dimension change.
   * @param width The new camera video width.
   * @param height The new camera video height.
   */
  @Override
  public void onCameraDimensionsChange(DialerCall call, int width, int height) {
    LogUtil.i(
        "VideoCallPresenter.onCameraDimensionsChange",
        "call: %s, width: %d, height: %d",
        call,
        width,
        height);
    if (mVideoCallScreen == null) {
      LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "ui is null");
      return;
    }

    if (!call.equals(mPrimaryCall)) {
      LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "not the primary call");
      return;
    }

    mPreviewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
    changePreviewDimensions(width, height);

    // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
    // If it not yet ready, it will be set when when creation completes.
    Surface surface = getLocalVideoSurfaceTexture().getSavedSurface();
    if (surface != null) {
      mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
      mVideoCall.setPreviewSurface(surface);
    }
  }

  /**
   * Changes the dimensions of the preview surface.
   *
   * @param width The new width.
   * @param height The new height.
   */
  private void changePreviewDimensions(int width, int height) {
    if (mVideoCallScreen == null) {
      return;
    }

    // Resize the surface used to display the preview video
    getLocalVideoSurfaceTexture().setSurfaceDimensions(new Point(width, height));
    mVideoCallScreen.onLocalVideoDimensionsChanged();
  }

  /**
   * Handles changes to the device orientation.
   *
   * @param orientation The screen orientation of the device (one of: {@link
   *     InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
   *     InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
   *     InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
   *     InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
   */
  @Override
  public void onDeviceOrientationChanged(int orientation) {
    LogUtil.i(
        "VideoCallPresenter.onDeviceOrientationChanged",
        "orientation: %d -> %d",
        mDeviceOrientation,
        orientation);
    mDeviceOrientation = orientation;

    if (mVideoCallScreen == null) {
      LogUtil.e("VideoCallPresenter.onDeviceOrientationChanged", "videoCallScreen is null");
      return;
    }

    Point previewDimensions = getLocalVideoSurfaceTexture().getSurfaceDimensions();
    if (previewDimensions == null) {
      return;
    }
    LogUtil.v(
        "VideoCallPresenter.onDeviceOrientationChanged",
        "orientation: %d, size: %s",
        orientation,
        previewDimensions);
    changePreviewDimensions(previewDimensions.x, previewDimensions.y);

    mVideoCallScreen.onLocalVideoOrientationChanged();
  }

  /**
   * Exits fullscreen mode if the current call context has changed to a non-video call.
   *
   * @param call The call.
   */
  protected void maybeExitFullscreen(DialerCall call) {
    if (call == null) {
      return;
    }

    if (!isVideoCall(call) || call.getState() == DialerCall.State.INCOMING) {
      LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen");
      InCallPresenter.getInstance().setFullScreen(false);
    }
  }

  /**
   * Schedules auto-entering of fullscreen mode. Will not enter full screen mode if any of the
   * following conditions are met: 1. No call 2. DialerCall is not active 3. The current video state
   * is not bi-directional. 4. Already in fullscreen mode 5. In accessibility mode
   *
   * @param call The current call.
   */
  protected void maybeAutoEnterFullscreen(DialerCall call) {
    if (!mIsAutoFullscreenEnabled) {
      return;
    }

    if (call == null
        || call.getState() != DialerCall.State.ACTIVE
        || !isBidirectionalVideoCall(call)
        || InCallPresenter.getInstance().isFullscreen()
        || (mContext != null && AccessibilityUtil.isTouchExplorationEnabled(mContext))) {
      // Ensure any previously scheduled attempt to enter fullscreen is cancelled.
      cancelAutoFullScreen();
      return;
    }

    if (mAutoFullScreenPending) {
      LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "already pending.");
      return;
    }
    LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "scheduled");
    mAutoFullScreenPending = true;
    mHandler.removeCallbacks(mAutoFullscreenRunnable);
    mHandler.postDelayed(mAutoFullscreenRunnable, mAutoFullscreenTimeoutMillis);
  }

  /** Cancels pending auto fullscreen mode. */
  @Override
  public void cancelAutoFullScreen() {
    if (!mAutoFullScreenPending) {
      LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "none pending.");
      return;
    }
    LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "cancelling pending");
    mAutoFullScreenPending = false;
    mHandler.removeCallbacks(mAutoFullscreenRunnable);
  }

  @Override
  public boolean shouldShowCameraPermissionToast() {
    if (mPrimaryCall == null) {
      LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "null call");
      return false;
    }
    if (mPrimaryCall.didShowCameraPermission()) {
      LogUtil.i(
          "VideoCallPresenter.shouldShowCameraPermissionToast", "already shown for this call");
      return false;
    }
    if (!ConfigProviderBindings.get(mContext)
        .getBoolean("camera_permission_dialog_allowed", true)) {
      LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "disabled by config");
      return false;
    }
    return !VideoUtils.hasCameraPermission(mContext)
        || !PermissionsUtil.hasCameraPrivacyToastShown(mContext);
  }

  @Override
  public void onCameraPermissionDialogShown() {
    if (mPrimaryCall != null) {
      mPrimaryCall.setDidShowCameraPermission(true);
    }
  }

  private void updateRemoteVideoSurfaceDimensions() {
    Activity activity = mVideoCallScreen.getVideoCallScreenFragment().getActivity();
    if (activity != null) {
      Point screenSize = new Point();
      activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
      getRemoteVideoSurfaceTexture().setSurfaceDimensions(screenSize);
    }
  }

  private static boolean isVideoUpgrade(DialerCall call) {
    return call != null
        && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
  }

  private static boolean isVideoUpgrade(@SessionModificationState int state) {
    return VideoUtils.hasSentVideoUpgradeRequest(state)
        || VideoUtils.hasReceivedVideoUpgradeRequest(state);
  }

  private class LocalDelegate implements VideoSurfaceDelegate {
    @Override
    public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
      if (mVideoCallScreen == null) {
        LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no UI");
        return;
      }
      if (mVideoCall == null) {
        LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no video call");
        return;
      }

      // If the preview surface has just been created and we have already received camera
      // capabilities, but not yet set the surface, we will set the surface now.
      if (mPreviewSurfaceState == PreviewSurfaceState.CAPABILITIES_RECEIVED) {
        mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
        mVideoCall.setPreviewSurface(videoCallSurface.getSavedSurface());
      } else if (mPreviewSurfaceState == PreviewSurfaceState.NONE && isCameraRequired()) {
        enableCamera(mVideoCall, true);
      }
    }

    @Override
    public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
      if (mVideoCall == null) {
        LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceReleased", "no video call");
        return;
      }

      mVideoCall.setPreviewSurface(null);
      enableCamera(mVideoCall, false);
    }

    @Override
    public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {
      if (mVideoCall == null) {
        LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", "no video call");
        return;
      }

      boolean isChangingConfigurations = InCallPresenter.getInstance().isChangingConfigurations();
      if (!isChangingConfigurations) {
        enableCamera(mVideoCall, false);
      } else {
        LogUtil.i(
            "VideoCallPresenter.LocalDelegate.onSurfaceDestroyed",
            "activity is being destroyed due to configuration changes. Not closing the camera.");
      }
    }

    @Override
    public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
      VideoCallPresenter.this.onSurfaceClick();
    }
  }

  private class RemoteDelegate implements VideoSurfaceDelegate {
    @Override
    public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
      if (mVideoCallScreen == null) {
        LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no UI");
        return;
      }
      if (mVideoCall == null) {
        LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no video call");
        return;
      }
      mVideoCall.setDisplaySurface(videoCallSurface.getSavedSurface());
    }

    @Override
    public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
      if (mVideoCall == null) {
        LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceReleased", "no video call");
        return;
      }
      mVideoCall.setDisplaySurface(null);
    }

    @Override
    public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {}

    @Override
    public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
      VideoCallPresenter.this.onSurfaceClick();
    }
  }

  /** Defines the state of the preview surface negotiation with the telephony layer. */
  private static class PreviewSurfaceState {

    /**
     * The camera has not yet been set on the {@link VideoCall}; negotiation has not yet started.
     */
    private static final int NONE = 0;

    /**
     * The camera has been set on the {@link VideoCall}, but camera capabilities have not yet been
     * received.
     */
    private static final int CAMERA_SET = 1;

    /**
     * The camera capabilties have been received from telephony, but the surface has not yet been
     * set on the {@link VideoCall}.
     */
    private static final int CAPABILITIES_RECEIVED = 2;

    /** The surface has been set on the {@link VideoCall}. */
    private static final int SURFACE_SET = 3;
  }

  private static boolean isBidirectionalVideoCall(DialerCall call) {
    return CompatUtils.isVideoCompatible() && VideoProfile.isBidirectional(call.getVideoState());
  }

  private static boolean isIncomingVideoCall(DialerCall call) {
    if (!isVideoCall(call)) {
      return false;
    }
    final int state = call.getState();
    return (state == DialerCall.State.INCOMING) || (state == DialerCall.State.CALL_WAITING);
  }

  private static boolean isActiveVideoCall(DialerCall call) {
    return isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE;
  }

  private static boolean isOutgoingVideoCall(DialerCall call) {
    if (!isVideoCall(call)) {
      return false;
    }
    final int state = call.getState();
    return DialerCall.State.isDialing(state)
        || state == DialerCall.State.CONNECTING
        || state == DialerCall.State.SELECT_PHONE_ACCOUNT;
  }

  private static boolean isAudioCall(DialerCall call) {
    if (!CompatUtils.isVideoCompatible()) {
      return true;
    }

    return call != null && VideoProfile.isAudioOnly(call.getVideoState());
  }

  private static boolean isVideoCall(@Nullable DialerCall call) {
    return call != null && call.isVideoCall();
  }

  private static boolean isVideoCall(int videoState) {
    return CompatUtils.isVideoCompatible()
        && (VideoProfile.isTransmissionEnabled(videoState)
            || VideoProfile.isReceptionEnabled(videoState));
  }
}