blob: 20dc987da6d489acc577e4f483f516dc557b8342 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2014 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.app.Activity;
20import android.content.Context;
21import android.graphics.Point;
22import android.os.Handler;
23import android.support.annotation.Nullable;
Eric Erfanianccca3152017-02-22 16:32:36 -080024import android.telecom.InCallService.VideoCall;
25import android.telecom.VideoProfile;
26import android.telecom.VideoProfile.CameraCapabilities;
27import android.view.Surface;
28import com.android.dialer.common.Assert;
29import com.android.dialer.common.ConfigProviderBindings;
30import com.android.dialer.common.LogUtil;
31import com.android.dialer.compat.CompatUtils;
32import com.android.incallui.InCallPresenter.InCallDetailsListener;
33import com.android.incallui.InCallPresenter.InCallOrientationListener;
34import com.android.incallui.InCallPresenter.InCallStateListener;
35import com.android.incallui.InCallPresenter.IncomingCallListener;
36import com.android.incallui.call.CallList;
37import com.android.incallui.call.DialerCall;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070038import com.android.incallui.call.DialerCall.CameraDirection;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import com.android.incallui.call.DialerCall.State;
40import com.android.incallui.call.InCallVideoCallCallbackNotifier;
41import com.android.incallui.call.InCallVideoCallCallbackNotifier.SurfaceChangeListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import com.android.incallui.call.VideoUtils;
43import com.android.incallui.util.AccessibilityUtil;
44import com.android.incallui.video.protocol.VideoCallScreen;
45import com.android.incallui.video.protocol.VideoCallScreenDelegate;
46import com.android.incallui.videosurface.protocol.VideoSurfaceDelegate;
47import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070048import com.android.incallui.videotech.VideoTech;
49import com.android.incallui.videotech.VideoTech.SessionModificationState;
Eric Erfanianccca3152017-02-22 16:32:36 -080050import java.util.Objects;
51
52/**
53 * Logic related to the {@link VideoCallScreen} and for managing changes to the video calling
54 * surfaces based on other user interface events and incoming events from the {@class
55 * VideoCallListener}.
56 *
57 * <p>When a call's video state changes to bi-directional video, the {@link
58 * com.android.incallui.VideoCallPresenter} performs the following negotiation with the telephony
59 * layer:
60 *
61 * <ul>
62 * <li>{@code VideoCallPresenter} creates and informs telephony of the display surface.
63 * <li>{@code VideoCallPresenter} creates the preview surface.
64 * <li>{@code VideoCallPresenter} informs telephony of the currently selected camera.
65 * <li>Telephony layer sends {@link CameraCapabilities}, including the dimensions of the video for
66 * the current camera.
67 * <li>{@code VideoCallPresenter} adjusts size of the preview surface to match the aspect ratio of
68 * the camera.
69 * <li>{@code VideoCallPresenter} informs telephony of the new preview surface.
70 * </ul>
71 *
72 * <p>When downgrading to an audio-only video state, the {@code VideoCallPresenter} nulls both
73 * surfaces.
74 */
75public class VideoCallPresenter
76 implements IncomingCallListener,
77 InCallOrientationListener,
78 InCallStateListener,
79 InCallDetailsListener,
80 SurfaceChangeListener,
Eric Erfanianccca3152017-02-22 16:32:36 -080081 InCallPresenter.InCallEventListener,
82 VideoCallScreenDelegate {
83
84 private static boolean mIsVideoMode = false;
85
86 private final Handler mHandler = new Handler();
87 private VideoCallScreen mVideoCallScreen;
88
89 /** The current context. */
90 private Context mContext;
91
Eric Erfanianccca3152017-02-22 16:32:36 -080092 /** The call the video surfaces are currently related to */
93 private DialerCall mPrimaryCall;
94 /**
95 * The {@link VideoCall} used to inform the video telephony layer of changes to the video
96 * surfaces.
97 */
98 private VideoCall mVideoCall;
99 /** Determines if the current UI state represents a video call. */
100 private int mCurrentVideoState;
101 /** DialerCall's current state */
102 private int mCurrentCallState = DialerCall.State.INVALID;
103 /** Determines the device orientation (portrait/lanscape). */
104 private int mDeviceOrientation = InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN;
105 /** Tracks the state of the preview surface negotiation with the telephony layer. */
106 private int mPreviewSurfaceState = PreviewSurfaceState.NONE;
107 /**
108 * Determines whether video calls should automatically enter full screen mode after {@link
109 * #mAutoFullscreenTimeoutMillis} milliseconds.
110 */
111 private boolean mIsAutoFullscreenEnabled = false;
112 /**
113 * Determines the number of milliseconds after which a video call will automatically enter
114 * fullscreen mode. Requires {@link #mIsAutoFullscreenEnabled} to be {@code true}.
115 */
116 private int mAutoFullscreenTimeoutMillis = 0;
117 /**
118 * Determines if the countdown is currently running to automatically enter full screen video mode.
119 */
120 private boolean mAutoFullScreenPending = false;
121 /** Whether if the call is remotely held. */
122 private boolean mIsRemotelyHeld = false;
123 /**
124 * Runnable which is posted to schedule automatically entering fullscreen mode. Will not auto
125 * enter fullscreen mode if the dialpad is visible (doing so would make it impossible to exit the
126 * dialpad).
127 */
128 private Runnable mAutoFullscreenRunnable =
129 new Runnable() {
130 @Override
131 public void run() {
132 if (mAutoFullScreenPending
133 && !InCallPresenter.getInstance().isDialpadVisible()
134 && mIsVideoMode) {
135
136 LogUtil.v("VideoCallPresenter.mAutoFullScreenRunnable", "entering fullscreen mode");
137 InCallPresenter.getInstance().setFullScreen(true);
138 mAutoFullScreenPending = false;
139 } else {
140 LogUtil.v(
141 "VideoCallPresenter.mAutoFullScreenRunnable",
142 "skipping scheduled fullscreen mode.");
143 }
144 }
145 };
146
147 private boolean isVideoCallScreenUiReady;
148
149 private static boolean isCameraRequired(int videoState, int sessionModificationState) {
150 return VideoProfile.isBidirectional(videoState)
151 || VideoProfile.isTransmissionEnabled(videoState)
152 || isVideoUpgrade(sessionModificationState);
153 }
154
155 /**
156 * Determines if the incoming video surface should be shown based on the current videoState and
157 * callState. The video surface is shown when incoming video is not paused, the call is active,
158 * and video reception is enabled.
159 *
160 * @param videoState The current video state.
161 * @param callState The current call state.
162 * @return {@code true} if the incoming video surface should be shown, {@code false} otherwise.
163 */
164 public static boolean showIncomingVideo(int videoState, int callState) {
165 if (!CompatUtils.isVideoCompatible()) {
166 return false;
167 }
168
169 boolean isPaused = VideoProfile.isPaused(videoState);
170 boolean isCallActive = callState == DialerCall.State.ACTIVE;
171
172 return !isPaused && isCallActive && VideoProfile.isReceptionEnabled(videoState);
173 }
174
175 /**
176 * Determines if the outgoing video surface should be shown based on the current videoState. The
177 * video surface is shown if video transmission is enabled.
178 *
179 * @return {@code true} if the the outgoing video surface should be shown, {@code false}
180 * otherwise.
181 */
182 public static boolean showOutgoingVideo(
183 Context context, int videoState, int sessionModificationState) {
184 if (!VideoUtils.hasCameraPermissionAndAllowedByUser(context)) {
185 LogUtil.i("VideoCallPresenter.showOutgoingVideo", "Camera permission is disabled by user.");
186 return false;
187 }
188
189 if (!CompatUtils.isVideoCompatible()) {
190 return false;
191 }
192
193 return VideoProfile.isTransmissionEnabled(videoState)
194 || isVideoUpgrade(sessionModificationState);
195 }
196
197 private static void updateCameraSelection(DialerCall call) {
198 LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + call);
199 LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + toSimpleString(call));
200
201 final DialerCall activeCall = CallList.getInstance().getActiveCall();
202 int cameraDir;
203
204 // this function should never be called with null call object, however if it happens we
205 // should handle it gracefully.
206 if (call == null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700207 cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800208 LogUtil.e(
209 "VideoCallPresenter.updateCameraSelection",
210 "call is null. Setting camera direction to default value (CAMERA_DIRECTION_UNKNOWN)");
211 }
212
213 // Clear camera direction if this is not a video call.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700214 else if (isAudioCall(call) && !isVideoUpgrade(call)) {
215 cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
216 call.setCameraDir(cameraDir);
Eric Erfanianccca3152017-02-22 16:32:36 -0800217 }
218
219 // If this is a waiting video call, default to active call's camera,
220 // since we don't want to change the current camera for waiting call
221 // without user's permission.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700222 else if (isVideoCall(activeCall) && isIncomingVideoCall(call)) {
223 cameraDir = activeCall.getCameraDir();
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 }
225
226 // Infer the camera direction from the video state and store it,
227 // if this is an outgoing video call.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700228 else if (isOutgoingVideoCall(call) && !isCameraDirectionSet(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800229 cameraDir = toCameraDirection(call.getVideoState());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700230 call.setCameraDir(cameraDir);
Eric Erfanianccca3152017-02-22 16:32:36 -0800231 }
232
233 // Use the stored camera dir if this is an outgoing video call for which camera direction
234 // is set.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700235 else if (isOutgoingVideoCall(call)) {
236 cameraDir = call.getCameraDir();
Eric Erfanianccca3152017-02-22 16:32:36 -0800237 }
238
239 // Infer the camera direction from the video state and store it,
240 // if this is an active video call and camera direction is not set.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700241 else if (isActiveVideoCall(call) && !isCameraDirectionSet(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 cameraDir = toCameraDirection(call.getVideoState());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700243 call.setCameraDir(cameraDir);
Eric Erfanianccca3152017-02-22 16:32:36 -0800244 }
245
246 // Use the stored camera dir if this is an active video call for which camera direction
247 // is set.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700248 else if (isActiveVideoCall(call)) {
249 cameraDir = call.getCameraDir();
Eric Erfanianccca3152017-02-22 16:32:36 -0800250 }
251
252 // For all other cases infer the camera direction but don't store it in the call object.
253 else {
254 cameraDir = toCameraDirection(call.getVideoState());
255 }
256
257 LogUtil.i(
258 "VideoCallPresenter.updateCameraSelection",
259 "setting camera direction to %d, call: %s",
260 cameraDir,
261 call);
262 final InCallCameraManager cameraManager =
263 InCallPresenter.getInstance().getInCallCameraManager();
264 cameraManager.setUseFrontFacingCamera(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700265 cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800266 }
267
268 private static int toCameraDirection(int videoState) {
269 return VideoProfile.isTransmissionEnabled(videoState)
270 && !VideoProfile.isBidirectional(videoState)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700271 ? CameraDirection.CAMERA_DIRECTION_BACK_FACING
272 : CameraDirection.CAMERA_DIRECTION_FRONT_FACING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800273 }
274
275 private static boolean isCameraDirectionSet(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700276 return isVideoCall(call) && call.getCameraDir() != CameraDirection.CAMERA_DIRECTION_UNKNOWN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 }
278
279 private static String toSimpleString(DialerCall call) {
280 return call == null ? null : call.toSimpleString();
281 }
282
283 /**
284 * Initializes the presenter.
285 *
286 * @param context The current context.
287 */
288 @Override
289 public void initVideoCallScreenDelegate(Context context, VideoCallScreen videoCallScreen) {
290 mContext = context;
291 mVideoCallScreen = videoCallScreen;
292 mIsAutoFullscreenEnabled =
293 mContext.getResources().getBoolean(R.bool.video_call_auto_fullscreen);
294 mAutoFullscreenTimeoutMillis =
295 mContext.getResources().getInteger(R.integer.video_call_auto_fullscreen_timeout);
296 }
297
298 /** Called when the user interface is ready to be used. */
299 @Override
300 public void onVideoCallScreenUiReady() {
301 LogUtil.v("VideoCallPresenter.onVideoCallScreenUiReady", "");
302 Assert.checkState(!isVideoCallScreenUiReady);
303
304 // Do not register any listeners if video calling is not compatible to safeguard against
305 // any accidental calls of video calling code.
306 if (!CompatUtils.isVideoCompatible()) {
307 return;
308 }
309
310 mDeviceOrientation = InCallOrientationEventListener.getCurrentOrientation();
311
312 // Register for call state changes last
313 InCallPresenter.getInstance().addListener(this);
314 InCallPresenter.getInstance().addDetailsListener(this);
315 InCallPresenter.getInstance().addIncomingCallListener(this);
316 InCallPresenter.getInstance().addOrientationListener(this);
317 // To get updates of video call details changes
318 InCallPresenter.getInstance().addInCallEventListener(this);
319 InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(new LocalDelegate());
320 InCallPresenter.getInstance().getRemoteVideoSurfaceTexture().setDelegate(new RemoteDelegate());
321
322 // Register for surface and video events from {@link InCallVideoCallListener}s.
323 InCallVideoCallCallbackNotifier.getInstance().addSurfaceChangeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800324 mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY;
325 mCurrentCallState = DialerCall.State.INVALID;
326
327 InCallPresenter.InCallState inCallState = InCallPresenter.getInstance().getInCallState();
328 onStateChange(inCallState, inCallState, CallList.getInstance());
329 isVideoCallScreenUiReady = true;
330 }
331
332 /** Called when the user interface is no longer ready to be used. */
333 @Override
334 public void onVideoCallScreenUiUnready() {
335 LogUtil.v("VideoCallPresenter.onVideoCallScreenUiUnready", "");
336 Assert.checkState(isVideoCallScreenUiReady);
337
338 if (!CompatUtils.isVideoCompatible()) {
339 return;
340 }
341
342 cancelAutoFullScreen();
343
344 InCallPresenter.getInstance().removeListener(this);
345 InCallPresenter.getInstance().removeDetailsListener(this);
346 InCallPresenter.getInstance().removeIncomingCallListener(this);
347 InCallPresenter.getInstance().removeOrientationListener(this);
348 InCallPresenter.getInstance().removeInCallEventListener(this);
349 InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(null);
350
351 InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800352
353 // Ensure that the call's camera direction is updated (most likely to UNKNOWN). Normally this
354 // happens after any call state changes but we're unregistering from InCallPresenter above so
355 // we won't get any more call state changes. See b/32957114.
356 if (mPrimaryCall != null) {
357 updateCameraSelection(mPrimaryCall);
358 }
359
360 isVideoCallScreenUiReady = false;
361 }
362
363 /**
364 * Handles clicks on the video surfaces. If not currently in fullscreen mode, will set fullscreen.
365 */
366 private void onSurfaceClick() {
367 LogUtil.i("VideoCallPresenter.onSurfaceClick", "");
368 cancelAutoFullScreen();
369 if (!InCallPresenter.getInstance().isFullscreen()) {
370 InCallPresenter.getInstance().setFullScreen(true);
371 } else {
372 InCallPresenter.getInstance().setFullScreen(false);
373 maybeAutoEnterFullscreen(mPrimaryCall);
374 // If Activity is not multiwindow, fullscreen will be driven by SystemUI visibility changes
375 // instead. See #onSystemUiVisibilityChange(boolean)
376
377 // TODO (keyboardr): onSystemUiVisibilityChange isn't being called the first time
378 // visibility changes after orientation change, so this is currently always done as a backup.
379 }
380 }
381
382 @Override
383 public void onSystemUiVisibilityChange(boolean visible) {
384 // If the SystemUI has changed to be visible, take us out of fullscreen mode
385 LogUtil.i("VideoCallPresenter.onSystemUiVisibilityChange", "visible: " + visible);
386 if (visible) {
387 InCallPresenter.getInstance().setFullScreen(false);
388 maybeAutoEnterFullscreen(mPrimaryCall);
389 }
390 }
391
392 @Override
393 public VideoSurfaceTexture getLocalVideoSurfaceTexture() {
394 return InCallPresenter.getInstance().getLocalVideoSurfaceTexture();
395 }
396
397 @Override
398 public VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
399 return InCallPresenter.getInstance().getRemoteVideoSurfaceTexture();
400 }
401
402 @Override
403 public int getDeviceOrientation() {
404 return mDeviceOrientation;
405 }
406
407 /**
408 * This should only be called when user approved the camera permission, which is local action and
409 * does NOT change any call states.
410 */
411 @Override
412 public void onCameraPermissionGranted() {
413 LogUtil.i("VideoCallPresenter.onCameraPermissionGranted", "");
414 VideoUtils.setCameraAllowedByUser(mContext);
415 enableCamera(mPrimaryCall.getVideoCall(), isCameraRequired());
416 showVideoUi(
417 mPrimaryCall.getVideoState(),
418 mPrimaryCall.getState(),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700419 mPrimaryCall.getVideoTech().getSessionModificationState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 mPrimaryCall.isRemotelyHeld());
421 InCallPresenter.getInstance().getInCallCameraManager().onCameraPermissionGranted();
422 }
423
424 /**
425 * Called when the user interacts with the UI. If a fullscreen timer is pending then we start the
426 * timer from scratch to avoid having the UI disappear while the user is interacting with it.
427 */
428 @Override
429 public void resetAutoFullscreenTimer() {
430 if (mAutoFullScreenPending) {
431 LogUtil.i("VideoCallPresenter.resetAutoFullscreenTimer", "resetting");
432 mHandler.removeCallbacks(mAutoFullscreenRunnable);
433 mHandler.postDelayed(mAutoFullscreenRunnable, mAutoFullscreenTimeoutMillis);
434 }
435 }
436
437 /**
438 * Handles incoming calls.
439 *
440 * @param oldState The old in call state.
441 * @param newState The new in call state.
442 * @param call The call.
443 */
444 @Override
445 public void onIncomingCall(
446 InCallPresenter.InCallState oldState, InCallPresenter.InCallState newState, DialerCall call) {
447 // same logic should happen as with onStateChange()
448 onStateChange(oldState, newState, CallList.getInstance());
449 }
450
451 /**
452 * Handles state changes (including incoming calls)
453 *
454 * @param newState The in call state.
455 * @param callList The call list.
456 */
457 @Override
458 public void onStateChange(
459 InCallPresenter.InCallState oldState,
460 InCallPresenter.InCallState newState,
461 CallList callList) {
462 LogUtil.v(
463 "VideoCallPresenter.onStateChange",
464 "oldState: %s, newState: %s, isVideoMode: %b",
465 oldState,
466 newState,
467 isVideoMode());
468
469 if (newState == InCallPresenter.InCallState.NO_CALLS) {
470 if (isVideoMode()) {
471 exitVideoMode();
472 }
473
474 InCallPresenter.getInstance().cleanupSurfaces();
475 }
476
477 // Determine the primary active call).
478 DialerCall primary = null;
479
480 // Determine the call which is the focus of the user's attention. In the case of an
481 // incoming call waiting call, the primary call is still the active video call, however
482 // the determination of whether we should be in fullscreen mode is based on the type of the
483 // incoming call, not the active video call.
484 DialerCall currentCall = null;
485
486 if (newState == InCallPresenter.InCallState.INCOMING) {
487 // We don't want to replace active video call (primary call)
488 // with a waiting call, since user may choose to ignore/decline the waiting call and
489 // this should have no impact on current active video call, that is, we should not
490 // change the camera or UI unless the waiting VT call becomes active.
491 primary = callList.getActiveCall();
492 currentCall = callList.getIncomingCall();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700493 if (!isActiveVideoCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800494 primary = callList.getIncomingCall();
495 }
496 } else if (newState == InCallPresenter.InCallState.OUTGOING) {
497 currentCall = primary = callList.getOutgoingCall();
498 } else if (newState == InCallPresenter.InCallState.PENDING_OUTGOING) {
499 currentCall = primary = callList.getPendingOutgoingCall();
500 } else if (newState == InCallPresenter.InCallState.INCALL) {
501 currentCall = primary = callList.getActiveCall();
502 }
503
504 final boolean primaryChanged = !Objects.equals(mPrimaryCall, primary);
505 LogUtil.i(
506 "VideoCallPresenter.onStateChange",
507 "primaryChanged: %b, primary: %s, mPrimaryCall: %s",
508 primaryChanged,
509 primary,
510 mPrimaryCall);
511 if (primaryChanged) {
512 onPrimaryCallChanged(primary);
513 } else if (mPrimaryCall != null) {
514 updateVideoCall(primary);
515 }
516 updateCallCache(primary);
517
518 // If the call context changed, potentially exit fullscreen or schedule auto enter of
519 // fullscreen mode.
520 // If the current call context is no longer a video call, exit fullscreen mode.
521 maybeExitFullscreen(currentCall);
522 // Schedule auto-enter of fullscreen mode if the current call context is a video call
523 maybeAutoEnterFullscreen(currentCall);
524 }
525
526 /**
527 * Handles a change to the fullscreen mode of the app.
528 *
529 * @param isFullscreenMode {@code true} if the app is now fullscreen, {@code false} otherwise.
530 */
531 @Override
532 public void onFullscreenModeChanged(boolean isFullscreenMode) {
533 cancelAutoFullScreen();
534 if (mPrimaryCall != null) {
535 updateFullscreenAndGreenScreenMode(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700536 mPrimaryCall.getState(), mPrimaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800537 } else {
538 updateFullscreenAndGreenScreenMode(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700539 State.INVALID, VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST);
Eric Erfanianccca3152017-02-22 16:32:36 -0800540 }
541 }
542
543 private void checkForVideoStateChange(DialerCall call) {
544 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
545 final boolean hasVideoStateChanged = mCurrentVideoState != call.getVideoState();
546
547 LogUtil.v(
548 "VideoCallPresenter.checkForVideoStateChange",
549 "shouldShowVideoUi: %b, hasVideoStateChanged: %b, isVideoMode: %b, previousVideoState: %s,"
550 + " newVideoState: %s",
551 shouldShowVideoUi,
552 hasVideoStateChanged,
553 isVideoMode(),
554 VideoProfile.videoStateToString(mCurrentVideoState),
555 VideoProfile.videoStateToString(call.getVideoState()));
556 if (!hasVideoStateChanged) {
557 return;
558 }
559
560 updateCameraSelection(call);
561
562 if (shouldShowVideoUi) {
563 adjustVideoMode(call);
564 } else if (isVideoMode()) {
565 exitVideoMode();
566 }
567 }
568
569 private void checkForCallStateChange(DialerCall call) {
570 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
571 final boolean hasCallStateChanged =
572 mCurrentCallState != call.getState() || mIsRemotelyHeld != call.isRemotelyHeld();
573 mIsRemotelyHeld = call.isRemotelyHeld();
574
575 LogUtil.v(
576 "VideoCallPresenter.checkForCallStateChange",
577 "shouldShowVideoUi: %b, hasCallStateChanged: %b, isVideoMode: %b",
578 shouldShowVideoUi,
579 hasCallStateChanged,
580 isVideoMode());
581
582 if (!hasCallStateChanged) {
583 return;
584 }
585
586 if (shouldShowVideoUi) {
587 final InCallCameraManager cameraManager =
588 InCallPresenter.getInstance().getInCallCameraManager();
589
590 String prevCameraId = cameraManager.getActiveCameraId();
591 updateCameraSelection(call);
592 String newCameraId = cameraManager.getActiveCameraId();
593
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700594 if (!Objects.equals(prevCameraId, newCameraId) && isActiveVideoCall(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800595 enableCamera(call.getVideoCall(), true);
596 }
597 }
598
599 // Make sure we hide or show the video UI if needed.
600 showVideoUi(
601 call.getVideoState(),
602 call.getState(),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700603 call.getVideoTech().getSessionModificationState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 call.isRemotelyHeld());
605 }
606
607 private void onPrimaryCallChanged(DialerCall newPrimaryCall) {
608 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(newPrimaryCall);
609 final boolean isVideoMode = isVideoMode();
610
611 LogUtil.v(
612 "VideoCallPresenter.onPrimaryCallChanged",
613 "shouldShowVideoUi: %b, isVideoMode: %b",
614 shouldShowVideoUi,
615 isVideoMode);
616
617 if (!shouldShowVideoUi && isVideoMode) {
618 // Terminate video mode if new primary call is not a video call
619 // and we are currently in video mode.
620 LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "exiting video mode...");
621 exitVideoMode();
622 } else if (shouldShowVideoUi) {
623 LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "entering video mode...");
624
625 updateCameraSelection(newPrimaryCall);
626 adjustVideoMode(newPrimaryCall);
627 }
628 checkForOrientationAllowedChange(newPrimaryCall);
629 }
630
631 private boolean isVideoMode() {
632 return mIsVideoMode;
633 }
634
635 private void updateCallCache(DialerCall call) {
636 if (call == null) {
637 mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY;
638 mCurrentCallState = DialerCall.State.INVALID;
639 mVideoCall = null;
640 mPrimaryCall = null;
641 } else {
642 mCurrentVideoState = call.getVideoState();
643 mVideoCall = call.getVideoCall();
644 mCurrentCallState = call.getState();
645 mPrimaryCall = call;
646 }
647 }
648
649 /**
650 * Handles changes to the details of the call. The {@link VideoCallPresenter} is interested in
651 * changes to the video state.
652 *
653 * @param call The call for which the details changed.
654 * @param details The new call details.
655 */
656 @Override
657 public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
658 LogUtil.v(
659 "VideoCallPresenter.onDetailsChanged",
660 "call: %s, details: %s, mPrimaryCall: %s",
661 call,
662 details,
663 mPrimaryCall);
664 if (call == null) {
665 return;
666 }
667 // If the details change is not for the currently active call no update is required.
668 if (!call.equals(mPrimaryCall)) {
669 LogUtil.v("VideoCallPresenter.onDetailsChanged", "details not for current active call");
670 return;
671 }
672
673 updateVideoCall(call);
674
675 updateCallCache(call);
676 }
677
678 private void updateVideoCall(DialerCall call) {
679 checkForVideoCallChange(call);
680 checkForVideoStateChange(call);
681 checkForCallStateChange(call);
682 checkForOrientationAllowedChange(call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700683 updateFullscreenAndGreenScreenMode(
684 call.getState(), call.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800685 }
686
687 private void checkForOrientationAllowedChange(@Nullable DialerCall call) {
688 InCallPresenter.getInstance()
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700689 .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call));
Eric Erfanianccca3152017-02-22 16:32:36 -0800690 }
691
692 private void updateFullscreenAndGreenScreenMode(
693 int callState, @SessionModificationState int sessionModificationState) {
694 if (mVideoCallScreen != null) {
695 boolean shouldShowFullscreen = InCallPresenter.getInstance().isFullscreen();
696 boolean shouldShowGreenScreen =
697 callState == State.DIALING
698 || callState == State.CONNECTING
699 || callState == State.INCOMING
700 || isVideoUpgrade(sessionModificationState);
701 mVideoCallScreen.updateFullscreenAndGreenScreenMode(
702 shouldShowFullscreen, shouldShowGreenScreen);
703 }
704 }
705
706 /** Checks for a change to the video call and changes it if required. */
707 private void checkForVideoCallChange(DialerCall call) {
708 final VideoCall videoCall = call.getVideoCall();
709 LogUtil.v(
710 "VideoCallPresenter.checkForVideoCallChange",
711 "videoCall: %s, mVideoCall: %s",
712 videoCall,
713 mVideoCall);
714 if (!Objects.equals(videoCall, mVideoCall)) {
715 changeVideoCall(call);
716 }
717 }
718
719 /**
720 * Handles a change to the video call. Sets the surfaces on the previous call to null and sets the
721 * surfaces on the new video call accordingly.
722 *
723 * @param call The new video call.
724 */
725 private void changeVideoCall(DialerCall call) {
726 final VideoCall videoCall = call == null ? null : call.getVideoCall();
727 LogUtil.i(
728 "VideoCallPresenter.changeVideoCall",
729 "videoCall: %s, mVideoCall: %s",
730 videoCall,
731 mVideoCall);
732 final boolean hasChanged = mVideoCall == null && videoCall != null;
733
734 mVideoCall = videoCall;
735 if (mVideoCall == null) {
736 LogUtil.v("VideoCallPresenter.changeVideoCall", "video call or primary call is null. Return");
737 return;
738 }
739
740 if (shouldShowVideoUiForCall(call) && hasChanged) {
741 adjustVideoMode(call);
742 }
743 }
744
745 private boolean isCameraRequired() {
746 return mPrimaryCall != null
747 && isCameraRequired(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700748 mPrimaryCall.getVideoState(),
749 mPrimaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 }
751
752 /**
753 * Adjusts the current video mode by setting up the preview and display surfaces as necessary.
754 * Expected to be called whenever the video state associated with a call changes (e.g. a user
755 * turns their camera on or off) to ensure the correct surfaces are shown/hidden. TODO: Need
756 * to adjust size and orientation of preview surface here.
757 */
758 private void adjustVideoMode(DialerCall call) {
759 VideoCall videoCall = call.getVideoCall();
760 int newVideoState = call.getVideoState();
761
762 LogUtil.i(
763 "VideoCallPresenter.adjustVideoMode",
764 "videoCall: %s, videoState: %d",
765 videoCall,
766 newVideoState);
767 if (mVideoCallScreen == null) {
768 LogUtil.e("VideoCallPresenter.adjustVideoMode", "error VideoCallScreen is null so returning");
769 return;
770 }
771
772 showVideoUi(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700773 newVideoState,
774 call.getState(),
775 call.getVideoTech().getSessionModificationState(),
776 call.isRemotelyHeld());
Eric Erfanianccca3152017-02-22 16:32:36 -0800777
778 // Communicate the current camera to telephony and make a request for the camera
779 // capabilities.
780 if (videoCall != null) {
781 Surface surface = getRemoteVideoSurfaceTexture().getSavedSurface();
782 if (surface != null) {
783 LogUtil.v(
784 "VideoCallPresenter.adjustVideoMode", "calling setDisplaySurface with: " + surface);
785 videoCall.setDisplaySurface(surface);
786 }
787
788 Assert.checkState(
789 mDeviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN);
790 videoCall.setDeviceOrientation(mDeviceOrientation);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700791 enableCamera(
792 videoCall,
793 isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800794 }
795 int previousVideoState = mCurrentVideoState;
796 mCurrentVideoState = newVideoState;
797 mIsVideoMode = true;
798
799 // adjustVideoMode may be called if we are already in a 1-way video state. In this case
800 // we do not want to trigger auto-fullscreen mode.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700801 if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800802 maybeAutoEnterFullscreen(call);
803 }
804 }
805
806 private static boolean shouldShowVideoUiForCall(@Nullable DialerCall call) {
807 if (call == null) {
808 return false;
809 }
810
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700811 if (isVideoCall(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800812 return true;
813 }
814
815 if (isVideoUpgrade(call)) {
816 return true;
817 }
818
819 return false;
820 }
821
822 private void enableCamera(VideoCall videoCall, boolean isCameraRequired) {
823 LogUtil.v(
824 "VideoCallPresenter.enableCamera",
825 "videoCall: %s, enabling: %b",
826 videoCall,
827 isCameraRequired);
828 if (videoCall == null) {
829 LogUtil.i("VideoCallPresenter.enableCamera", "videoCall is null.");
830 return;
831 }
832
833 boolean hasCameraPermission = VideoUtils.hasCameraPermissionAndAllowedByUser(mContext);
834 if (!hasCameraPermission) {
835 videoCall.setCamera(null);
836 mPreviewSurfaceState = PreviewSurfaceState.NONE;
837 // TODO: Inform remote party that the video is off. This is similar to b/30256571.
838 } else if (isCameraRequired) {
839 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
840 videoCall.setCamera(cameraManager.getActiveCameraId());
841 mPreviewSurfaceState = PreviewSurfaceState.CAMERA_SET;
842 videoCall.requestCameraCapabilities();
843 } else {
844 mPreviewSurfaceState = PreviewSurfaceState.NONE;
845 videoCall.setCamera(null);
846 }
847 }
848
849 /** Exits video mode by hiding the video surfaces and making other adjustments (eg. audio). */
850 private void exitVideoMode() {
851 LogUtil.i("VideoCallPresenter.exitVideoMode", "");
852
853 showVideoUi(
854 VideoProfile.STATE_AUDIO_ONLY,
855 DialerCall.State.ACTIVE,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700856 VideoTech.SESSION_MODIFICATION_STATE_NO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 false /* isRemotelyHeld */);
858 enableCamera(mVideoCall, false);
859 InCallPresenter.getInstance().setFullScreen(false);
860
861 mIsVideoMode = false;
862 }
863
864 /**
865 * Based on the current video state and call state, show or hide the incoming and outgoing video
866 * surfaces. The outgoing video surface is shown any time video is transmitting. The incoming
867 * video surface is shown whenever the video is un-paused and active.
868 *
869 * @param videoState The video state.
870 * @param callState The call state.
871 */
872 private void showVideoUi(
873 int videoState,
874 int callState,
875 @SessionModificationState int sessionModificationState,
876 boolean isRemotelyHeld) {
877 if (mVideoCallScreen == null) {
878 LogUtil.e("VideoCallPresenter.showVideoUi", "videoCallScreen is null returning");
879 return;
880 }
881 boolean showIncomingVideo = showIncomingVideo(videoState, callState);
882 boolean showOutgoingVideo = showOutgoingVideo(mContext, videoState, sessionModificationState);
883 LogUtil.i(
884 "VideoCallPresenter.showVideoUi",
885 "showIncoming: %b, showOutgoing: %b, isRemotelyHeld: %b",
886 showIncomingVideo,
887 showOutgoingVideo,
888 isRemotelyHeld);
889 updateRemoteVideoSurfaceDimensions();
890 mVideoCallScreen.showVideoViews(showOutgoingVideo, showIncomingVideo, isRemotelyHeld);
891
892 InCallPresenter.getInstance().enableScreenTimeout(VideoProfile.isAudioOnly(videoState));
893 updateFullscreenAndGreenScreenMode(callState, sessionModificationState);
894 }
895
896 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800897 * Handles peer video dimension changes.
898 *
899 * @param call The call which experienced a peer video dimension change.
900 * @param width The new peer video width .
901 * @param height The new peer video height.
902 */
903 @Override
904 public void onUpdatePeerDimensions(DialerCall call, int width, int height) {
905 LogUtil.i("VideoCallPresenter.onUpdatePeerDimensions", "width: %d, height: %d", width, height);
906 if (mVideoCallScreen == null) {
907 LogUtil.e("VideoCallPresenter.onUpdatePeerDimensions", "videoCallScreen is null");
908 return;
909 }
910 if (!call.equals(mPrimaryCall)) {
911 LogUtil.e(
912 "VideoCallPresenter.onUpdatePeerDimensions", "current call is not equal to primary");
913 return;
914 }
915
916 // Change size of display surface to match the peer aspect ratio
917 if (width > 0 && height > 0 && mVideoCallScreen != null) {
918 getRemoteVideoSurfaceTexture().setSourceVideoDimensions(new Point(width, height));
919 mVideoCallScreen.onRemoteVideoDimensionsChanged();
920 }
921 }
922
923 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800924 * Handles a change to the dimensions of the local camera. Receiving the camera capabilities
925 * triggers the creation of the video
926 *
927 * @param call The call which experienced the camera dimension change.
928 * @param width The new camera video width.
929 * @param height The new camera video height.
930 */
931 @Override
932 public void onCameraDimensionsChange(DialerCall call, int width, int height) {
933 LogUtil.i(
934 "VideoCallPresenter.onCameraDimensionsChange",
935 "call: %s, width: %d, height: %d",
936 call,
937 width,
938 height);
939 if (mVideoCallScreen == null) {
940 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "ui is null");
941 return;
942 }
943
944 if (!call.equals(mPrimaryCall)) {
945 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "not the primary call");
946 return;
947 }
948
949 mPreviewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
950 changePreviewDimensions(width, height);
951
952 // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
953 // If it not yet ready, it will be set when when creation completes.
954 Surface surface = getLocalVideoSurfaceTexture().getSavedSurface();
955 if (surface != null) {
956 mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
957 mVideoCall.setPreviewSurface(surface);
958 }
959 }
960
961 /**
962 * Changes the dimensions of the preview surface.
963 *
964 * @param width The new width.
965 * @param height The new height.
966 */
967 private void changePreviewDimensions(int width, int height) {
968 if (mVideoCallScreen == null) {
969 return;
970 }
971
972 // Resize the surface used to display the preview video
973 getLocalVideoSurfaceTexture().setSurfaceDimensions(new Point(width, height));
974 mVideoCallScreen.onLocalVideoDimensionsChanged();
975 }
976
977 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800978 * Handles changes to the device orientation.
979 *
980 * @param orientation The screen orientation of the device (one of: {@link
981 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
982 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
983 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
984 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
985 */
986 @Override
987 public void onDeviceOrientationChanged(int orientation) {
988 LogUtil.i(
989 "VideoCallPresenter.onDeviceOrientationChanged",
990 "orientation: %d -> %d",
991 mDeviceOrientation,
992 orientation);
993 mDeviceOrientation = orientation;
994
995 if (mVideoCallScreen == null) {
996 LogUtil.e("VideoCallPresenter.onDeviceOrientationChanged", "videoCallScreen is null");
997 return;
998 }
999
1000 Point previewDimensions = getLocalVideoSurfaceTexture().getSurfaceDimensions();
1001 if (previewDimensions == null) {
1002 return;
1003 }
1004 LogUtil.v(
1005 "VideoCallPresenter.onDeviceOrientationChanged",
1006 "orientation: %d, size: %s",
1007 orientation,
1008 previewDimensions);
1009 changePreviewDimensions(previewDimensions.x, previewDimensions.y);
1010
1011 mVideoCallScreen.onLocalVideoOrientationChanged();
1012 }
1013
1014 /**
1015 * Exits fullscreen mode if the current call context has changed to a non-video call.
1016 *
1017 * @param call The call.
1018 */
1019 protected void maybeExitFullscreen(DialerCall call) {
1020 if (call == null) {
1021 return;
1022 }
1023
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001024 if (!isVideoCall(call) || call.getState() == DialerCall.State.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001025 LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen");
1026 InCallPresenter.getInstance().setFullScreen(false);
1027 }
1028 }
1029
1030 /**
1031 * Schedules auto-entering of fullscreen mode. Will not enter full screen mode if any of the
1032 * following conditions are met: 1. No call 2. DialerCall is not active 3. The current video state
1033 * is not bi-directional. 4. Already in fullscreen mode 5. In accessibility mode
1034 *
1035 * @param call The current call.
1036 */
1037 protected void maybeAutoEnterFullscreen(DialerCall call) {
1038 if (!mIsAutoFullscreenEnabled) {
1039 return;
1040 }
1041
1042 if (call == null
1043 || call.getState() != DialerCall.State.ACTIVE
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001044 || !isBidirectionalVideoCall(call)
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 || InCallPresenter.getInstance().isFullscreen()
1046 || (mContext != null && AccessibilityUtil.isTouchExplorationEnabled(mContext))) {
1047 // Ensure any previously scheduled attempt to enter fullscreen is cancelled.
1048 cancelAutoFullScreen();
1049 return;
1050 }
1051
1052 if (mAutoFullScreenPending) {
1053 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "already pending.");
1054 return;
1055 }
1056 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "scheduled");
1057 mAutoFullScreenPending = true;
1058 mHandler.removeCallbacks(mAutoFullscreenRunnable);
1059 mHandler.postDelayed(mAutoFullscreenRunnable, mAutoFullscreenTimeoutMillis);
1060 }
1061
1062 /** Cancels pending auto fullscreen mode. */
1063 @Override
1064 public void cancelAutoFullScreen() {
1065 if (!mAutoFullScreenPending) {
1066 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "none pending.");
1067 return;
1068 }
1069 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "cancelling pending");
1070 mAutoFullScreenPending = false;
1071 mHandler.removeCallbacks(mAutoFullscreenRunnable);
1072 }
1073
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001074 @Override
1075 public boolean shouldShowCameraPermissionDialog() {
1076 if (mPrimaryCall == null) {
1077 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionDialog", "null call");
1078 return false;
1079 }
1080 if (mPrimaryCall.didShowCameraPermission()) {
1081 LogUtil.i(
1082 "VideoCallPresenter.shouldShowCameraPermissionDialog", "already shown for this call");
1083 return false;
1084 }
1085 if (!ConfigProviderBindings.get(mContext)
1086 .getBoolean("camera_permission_dialog_allowed", true)) {
1087 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionDialog", "disabled by config");
1088 return false;
1089 }
1090 return !VideoUtils.hasCameraPermission(mContext) || !VideoUtils.isCameraAllowedByUser(mContext);
1091 }
1092
1093 @Override
1094 public void onCameraPermissionDialogShown() {
1095 if (mPrimaryCall != null) {
1096 mPrimaryCall.setDidShowCameraPermission(true);
1097 }
1098 }
1099
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 private void updateRemoteVideoSurfaceDimensions() {
1101 Activity activity = mVideoCallScreen.getVideoCallScreenFragment().getActivity();
1102 if (activity != null) {
1103 Point screenSize = new Point();
1104 activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
1105 getRemoteVideoSurfaceTexture().setSurfaceDimensions(screenSize);
1106 }
1107 }
1108
1109 private static boolean isVideoUpgrade(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001110 return call != null
1111 && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -08001112 }
1113
1114 private static boolean isVideoUpgrade(@SessionModificationState int state) {
1115 return VideoUtils.hasSentVideoUpgradeRequest(state)
1116 || VideoUtils.hasReceivedVideoUpgradeRequest(state);
1117 }
1118
1119 private class LocalDelegate implements VideoSurfaceDelegate {
1120 @Override
1121 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
1122 if (mVideoCallScreen == null) {
1123 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no UI");
1124 return;
1125 }
1126 if (mVideoCall == null) {
1127 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no video call");
1128 return;
1129 }
1130
1131 // If the preview surface has just been created and we have already received camera
1132 // capabilities, but not yet set the surface, we will set the surface now.
1133 if (mPreviewSurfaceState == PreviewSurfaceState.CAPABILITIES_RECEIVED) {
1134 mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
1135 mVideoCall.setPreviewSurface(videoCallSurface.getSavedSurface());
1136 } else if (mPreviewSurfaceState == PreviewSurfaceState.NONE && isCameraRequired()) {
1137 enableCamera(mVideoCall, true);
1138 }
1139 }
1140
1141 @Override
1142 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
1143 if (mVideoCall == null) {
1144 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceReleased", "no video call");
1145 return;
1146 }
1147
1148 mVideoCall.setPreviewSurface(null);
1149 enableCamera(mVideoCall, false);
1150 }
1151
1152 @Override
1153 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {
1154 if (mVideoCall == null) {
1155 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", "no video call");
1156 return;
1157 }
1158
1159 boolean isChangingConfigurations = InCallPresenter.getInstance().isChangingConfigurations();
1160 if (!isChangingConfigurations) {
1161 enableCamera(mVideoCall, false);
1162 } else {
1163 LogUtil.i(
1164 "VideoCallPresenter.LocalDelegate.onSurfaceDestroyed",
1165 "activity is being destroyed due to configuration changes. Not closing the camera.");
1166 }
1167 }
1168
1169 @Override
1170 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1171 VideoCallPresenter.this.onSurfaceClick();
1172 }
1173 }
1174
1175 private class RemoteDelegate implements VideoSurfaceDelegate {
1176 @Override
1177 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
1178 if (mVideoCallScreen == null) {
1179 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no UI");
1180 return;
1181 }
1182 if (mVideoCall == null) {
1183 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no video call");
1184 return;
1185 }
1186 mVideoCall.setDisplaySurface(videoCallSurface.getSavedSurface());
1187 }
1188
1189 @Override
1190 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
1191 if (mVideoCall == null) {
1192 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceReleased", "no video call");
1193 return;
1194 }
1195 mVideoCall.setDisplaySurface(null);
1196 }
1197
1198 @Override
1199 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {}
1200
1201 @Override
1202 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1203 VideoCallPresenter.this.onSurfaceClick();
1204 }
1205 }
1206
1207 /** Defines the state of the preview surface negotiation with the telephony layer. */
1208 private static class PreviewSurfaceState {
1209
1210 /**
1211 * The camera has not yet been set on the {@link VideoCall}; negotiation has not yet started.
1212 */
1213 private static final int NONE = 0;
1214
1215 /**
1216 * The camera has been set on the {@link VideoCall}, but camera capabilities have not yet been
1217 * received.
1218 */
1219 private static final int CAMERA_SET = 1;
1220
1221 /**
1222 * The camera capabilties have been received from telephony, but the surface has not yet been
1223 * set on the {@link VideoCall}.
1224 */
1225 private static final int CAPABILITIES_RECEIVED = 2;
1226
1227 /** The surface has been set on the {@link VideoCall}. */
1228 private static final int SURFACE_SET = 3;
1229 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001230
1231 private static boolean isBidirectionalVideoCall(DialerCall call) {
1232 return CompatUtils.isVideoCompatible() && VideoProfile.isBidirectional(call.getVideoState());
1233 }
1234
1235 private static boolean isIncomingVideoCall(DialerCall call) {
1236 if (!isVideoCall(call)) {
1237 return false;
1238 }
1239 final int state = call.getState();
1240 return (state == DialerCall.State.INCOMING) || (state == DialerCall.State.CALL_WAITING);
1241 }
1242
1243 private static boolean isActiveVideoCall(DialerCall call) {
1244 return isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE;
1245 }
1246
1247 private static boolean isOutgoingVideoCall(DialerCall call) {
1248 if (!isVideoCall(call)) {
1249 return false;
1250 }
1251 final int state = call.getState();
1252 return DialerCall.State.isDialing(state)
1253 || state == DialerCall.State.CONNECTING
1254 || state == DialerCall.State.SELECT_PHONE_ACCOUNT;
1255 }
1256
1257 private static boolean isAudioCall(DialerCall call) {
1258 if (!CompatUtils.isVideoCompatible()) {
1259 return true;
1260 }
1261
1262 return call != null && VideoProfile.isAudioOnly(call.getVideoState());
1263 }
1264
1265 private static boolean isVideoCall(@Nullable DialerCall call) {
1266 return call != null && call.isVideoCall();
1267 }
1268
1269 private static boolean isVideoCall(int videoState) {
1270 return CompatUtils.isVideoCompatible()
1271 && (VideoProfile.isTransmissionEnabled(videoState)
1272 || VideoProfile.isReceptionEnabled(videoState));
1273 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001274}