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