blob: 89cddb9c4bdd152ff54812fa7538820054fc4aae [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>
63 * <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.
71 * </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;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700172 //Show incoming Video for dialing calls to support early media
173 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
361 // we won't get any more call state changes. See b/32957114.
362 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) {
458 // same logic should happen as with onStateChange()
459 onStateChange(oldState, newState, CallList.getInstance());
460 }
461
462 /**
463 * Handles state changes (including incoming calls)
464 *
465 * @param newState The in call state.
466 * @param callList The call list.
467 */
468 @Override
469 public void onStateChange(
470 InCallPresenter.InCallState oldState,
471 InCallPresenter.InCallState newState,
472 CallList callList) {
473 LogUtil.v(
474 "VideoCallPresenter.onStateChange",
475 "oldState: %s, newState: %s, isVideoMode: %b",
476 oldState,
477 newState,
478 isVideoMode());
479
480 if (newState == InCallPresenter.InCallState.NO_CALLS) {
481 if (isVideoMode()) {
482 exitVideoMode();
483 }
484
485 InCallPresenter.getInstance().cleanupSurfaces();
486 }
487
488 // Determine the primary active call).
489 DialerCall primary = null;
490
491 // Determine the call which is the focus of the user's attention. In the case of an
492 // incoming call waiting call, the primary call is still the active video call, however
493 // the determination of whether we should be in fullscreen mode is based on the type of the
494 // incoming call, not the active video call.
495 DialerCall currentCall = null;
496
497 if (newState == InCallPresenter.InCallState.INCOMING) {
498 // We don't want to replace active video call (primary call)
499 // with a waiting call, since user may choose to ignore/decline the waiting call and
500 // this should have no impact on current active video call, that is, we should not
501 // change the camera or UI unless the waiting VT call becomes active.
502 primary = callList.getActiveCall();
503 currentCall = callList.getIncomingCall();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700504 if (!isActiveVideoCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800505 primary = callList.getIncomingCall();
506 }
507 } else if (newState == InCallPresenter.InCallState.OUTGOING) {
508 currentCall = primary = callList.getOutgoingCall();
509 } else if (newState == InCallPresenter.InCallState.PENDING_OUTGOING) {
510 currentCall = primary = callList.getPendingOutgoingCall();
511 } else if (newState == InCallPresenter.InCallState.INCALL) {
512 currentCall = primary = callList.getActiveCall();
513 }
514
515 final boolean primaryChanged = !Objects.equals(mPrimaryCall, primary);
516 LogUtil.i(
517 "VideoCallPresenter.onStateChange",
518 "primaryChanged: %b, primary: %s, mPrimaryCall: %s",
519 primaryChanged,
520 primary,
521 mPrimaryCall);
522 if (primaryChanged) {
523 onPrimaryCallChanged(primary);
524 } else if (mPrimaryCall != null) {
525 updateVideoCall(primary);
526 }
527 updateCallCache(primary);
528
529 // If the call context changed, potentially exit fullscreen or schedule auto enter of
530 // fullscreen mode.
531 // If the current call context is no longer a video call, exit fullscreen mode.
532 maybeExitFullscreen(currentCall);
533 // Schedule auto-enter of fullscreen mode if the current call context is a video call
534 maybeAutoEnterFullscreen(currentCall);
535 }
536
537 /**
538 * Handles a change to the fullscreen mode of the app.
539 *
540 * @param isFullscreenMode {@code true} if the app is now fullscreen, {@code false} otherwise.
541 */
542 @Override
543 public void onFullscreenModeChanged(boolean isFullscreenMode) {
544 cancelAutoFullScreen();
545 if (mPrimaryCall != null) {
546 updateFullscreenAndGreenScreenMode(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700547 mPrimaryCall.getState(), mPrimaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800548 } else {
Eric Erfanian90508232017-03-24 09:31:16 -0700549 updateFullscreenAndGreenScreenMode(State.INVALID, SessionModificationState.NO_REQUEST);
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 }
551 }
552
553 private void checkForVideoStateChange(DialerCall call) {
554 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
555 final boolean hasVideoStateChanged = mCurrentVideoState != call.getVideoState();
556
557 LogUtil.v(
558 "VideoCallPresenter.checkForVideoStateChange",
559 "shouldShowVideoUi: %b, hasVideoStateChanged: %b, isVideoMode: %b, previousVideoState: %s,"
560 + " newVideoState: %s",
561 shouldShowVideoUi,
562 hasVideoStateChanged,
563 isVideoMode(),
564 VideoProfile.videoStateToString(mCurrentVideoState),
565 VideoProfile.videoStateToString(call.getVideoState()));
566 if (!hasVideoStateChanged) {
567 return;
568 }
569
570 updateCameraSelection(call);
571
572 if (shouldShowVideoUi) {
573 adjustVideoMode(call);
574 } else if (isVideoMode()) {
575 exitVideoMode();
576 }
577 }
578
579 private void checkForCallStateChange(DialerCall call) {
580 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
581 final boolean hasCallStateChanged =
582 mCurrentCallState != call.getState() || mIsRemotelyHeld != call.isRemotelyHeld();
583 mIsRemotelyHeld = call.isRemotelyHeld();
584
585 LogUtil.v(
586 "VideoCallPresenter.checkForCallStateChange",
587 "shouldShowVideoUi: %b, hasCallStateChanged: %b, isVideoMode: %b",
588 shouldShowVideoUi,
589 hasCallStateChanged,
590 isVideoMode());
591
592 if (!hasCallStateChanged) {
593 return;
594 }
595
596 if (shouldShowVideoUi) {
597 final InCallCameraManager cameraManager =
598 InCallPresenter.getInstance().getInCallCameraManager();
599
600 String prevCameraId = cameraManager.getActiveCameraId();
601 updateCameraSelection(call);
602 String newCameraId = cameraManager.getActiveCameraId();
603
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700604 if (!Objects.equals(prevCameraId, newCameraId) && isActiveVideoCall(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 enableCamera(call.getVideoCall(), true);
606 }
607 }
608
609 // Make sure we hide or show the video UI if needed.
610 showVideoUi(
611 call.getVideoState(),
612 call.getState(),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700613 call.getVideoTech().getSessionModificationState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800614 call.isRemotelyHeld());
615 }
616
617 private void onPrimaryCallChanged(DialerCall newPrimaryCall) {
618 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(newPrimaryCall);
619 final boolean isVideoMode = isVideoMode();
620
621 LogUtil.v(
622 "VideoCallPresenter.onPrimaryCallChanged",
623 "shouldShowVideoUi: %b, isVideoMode: %b",
624 shouldShowVideoUi,
625 isVideoMode);
626
627 if (!shouldShowVideoUi && isVideoMode) {
628 // Terminate video mode if new primary call is not a video call
629 // and we are currently in video mode.
630 LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "exiting video mode...");
631 exitVideoMode();
632 } else if (shouldShowVideoUi) {
633 LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "entering video mode...");
634
635 updateCameraSelection(newPrimaryCall);
636 adjustVideoMode(newPrimaryCall);
637 }
638 checkForOrientationAllowedChange(newPrimaryCall);
639 }
640
641 private boolean isVideoMode() {
642 return mIsVideoMode;
643 }
644
645 private void updateCallCache(DialerCall call) {
646 if (call == null) {
647 mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY;
648 mCurrentCallState = DialerCall.State.INVALID;
649 mVideoCall = null;
650 mPrimaryCall = null;
651 } else {
652 mCurrentVideoState = call.getVideoState();
653 mVideoCall = call.getVideoCall();
654 mCurrentCallState = call.getState();
655 mPrimaryCall = call;
656 }
657 }
658
659 /**
660 * Handles changes to the details of the call. The {@link VideoCallPresenter} is interested in
661 * changes to the video state.
662 *
663 * @param call The call for which the details changed.
664 * @param details The new call details.
665 */
666 @Override
667 public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
668 LogUtil.v(
669 "VideoCallPresenter.onDetailsChanged",
670 "call: %s, details: %s, mPrimaryCall: %s",
671 call,
672 details,
673 mPrimaryCall);
674 if (call == null) {
675 return;
676 }
677 // If the details change is not for the currently active call no update is required.
678 if (!call.equals(mPrimaryCall)) {
679 LogUtil.v("VideoCallPresenter.onDetailsChanged", "details not for current active call");
680 return;
681 }
682
683 updateVideoCall(call);
684
685 updateCallCache(call);
686 }
687
688 private void updateVideoCall(DialerCall call) {
689 checkForVideoCallChange(call);
690 checkForVideoStateChange(call);
691 checkForCallStateChange(call);
692 checkForOrientationAllowedChange(call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700693 updateFullscreenAndGreenScreenMode(
694 call.getState(), call.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800695 }
696
697 private void checkForOrientationAllowedChange(@Nullable DialerCall call) {
698 InCallPresenter.getInstance()
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700699 .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call));
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 }
701
702 private void updateFullscreenAndGreenScreenMode(
703 int callState, @SessionModificationState int sessionModificationState) {
704 if (mVideoCallScreen != null) {
705 boolean shouldShowFullscreen = InCallPresenter.getInstance().isFullscreen();
706 boolean shouldShowGreenScreen =
707 callState == State.DIALING
708 || callState == State.CONNECTING
709 || callState == State.INCOMING
710 || isVideoUpgrade(sessionModificationState);
711 mVideoCallScreen.updateFullscreenAndGreenScreenMode(
712 shouldShowFullscreen, shouldShowGreenScreen);
713 }
714 }
715
716 /** Checks for a change to the video call and changes it if required. */
717 private void checkForVideoCallChange(DialerCall call) {
718 final VideoCall videoCall = call.getVideoCall();
719 LogUtil.v(
720 "VideoCallPresenter.checkForVideoCallChange",
721 "videoCall: %s, mVideoCall: %s",
722 videoCall,
723 mVideoCall);
724 if (!Objects.equals(videoCall, mVideoCall)) {
725 changeVideoCall(call);
726 }
727 }
728
729 /**
730 * Handles a change to the video call. Sets the surfaces on the previous call to null and sets the
731 * surfaces on the new video call accordingly.
732 *
733 * @param call The new video call.
734 */
735 private void changeVideoCall(DialerCall call) {
736 final VideoCall videoCall = call == null ? null : call.getVideoCall();
737 LogUtil.i(
738 "VideoCallPresenter.changeVideoCall",
739 "videoCall: %s, mVideoCall: %s",
740 videoCall,
741 mVideoCall);
742 final boolean hasChanged = mVideoCall == null && videoCall != null;
743
744 mVideoCall = videoCall;
745 if (mVideoCall == null) {
746 LogUtil.v("VideoCallPresenter.changeVideoCall", "video call or primary call is null. Return");
747 return;
748 }
749
750 if (shouldShowVideoUiForCall(call) && hasChanged) {
751 adjustVideoMode(call);
752 }
753 }
754
755 private boolean isCameraRequired() {
756 return mPrimaryCall != null
757 && isCameraRequired(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700758 mPrimaryCall.getVideoState(),
759 mPrimaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 }
761
762 /**
763 * Adjusts the current video mode by setting up the preview and display surfaces as necessary.
764 * Expected to be called whenever the video state associated with a call changes (e.g. a user
Eric Erfanian2ca43182017-08-31 06:57:16 -0700765 * turns their camera on or off) to ensure the correct surfaces are shown/hidden. TODO(vt): Need
Eric Erfanianccca3152017-02-22 16:32:36 -0800766 * to adjust size and orientation of preview surface here.
767 */
768 private void adjustVideoMode(DialerCall call) {
769 VideoCall videoCall = call.getVideoCall();
770 int newVideoState = call.getVideoState();
771
772 LogUtil.i(
773 "VideoCallPresenter.adjustVideoMode",
774 "videoCall: %s, videoState: %d",
775 videoCall,
776 newVideoState);
777 if (mVideoCallScreen == null) {
778 LogUtil.e("VideoCallPresenter.adjustVideoMode", "error VideoCallScreen is null so returning");
779 return;
780 }
781
782 showVideoUi(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700783 newVideoState,
784 call.getState(),
785 call.getVideoTech().getSessionModificationState(),
786 call.isRemotelyHeld());
Eric Erfanianccca3152017-02-22 16:32:36 -0800787
788 // Communicate the current camera to telephony and make a request for the camera
789 // capabilities.
790 if (videoCall != null) {
791 Surface surface = getRemoteVideoSurfaceTexture().getSavedSurface();
792 if (surface != null) {
793 LogUtil.v(
794 "VideoCallPresenter.adjustVideoMode", "calling setDisplaySurface with: " + surface);
795 videoCall.setDisplaySurface(surface);
796 }
797
798 Assert.checkState(
799 mDeviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN);
800 videoCall.setDeviceOrientation(mDeviceOrientation);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700801 enableCamera(
802 videoCall,
803 isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800804 }
805 int previousVideoState = mCurrentVideoState;
806 mCurrentVideoState = newVideoState;
807 mIsVideoMode = true;
808
809 // adjustVideoMode may be called if we are already in a 1-way video state. In this case
810 // we do not want to trigger auto-fullscreen mode.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700811 if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800812 maybeAutoEnterFullscreen(call);
813 }
814 }
815
816 private static boolean shouldShowVideoUiForCall(@Nullable DialerCall call) {
817 if (call == null) {
818 return false;
819 }
820
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700821 if (isVideoCall(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 return true;
823 }
824
825 if (isVideoUpgrade(call)) {
826 return true;
827 }
828
829 return false;
830 }
831
832 private void enableCamera(VideoCall videoCall, boolean isCameraRequired) {
833 LogUtil.v(
834 "VideoCallPresenter.enableCamera",
835 "videoCall: %s, enabling: %b",
836 videoCall,
837 isCameraRequired);
838 if (videoCall == null) {
839 LogUtil.i("VideoCallPresenter.enableCamera", "videoCall is null.");
840 return;
841 }
842
Eric Erfanian2ca43182017-08-31 06:57:16 -0700843 boolean hasCameraPermission = VideoUtils.hasCameraPermissionAndShownPrivacyToast(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800844 if (!hasCameraPermission) {
845 videoCall.setCamera(null);
846 mPreviewSurfaceState = PreviewSurfaceState.NONE;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700847 // TODO(wangqi): Inform remote party that the video is off. This is similar to b/30256571.
Eric Erfanianccca3152017-02-22 16:32:36 -0800848 } else if (isCameraRequired) {
849 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
850 videoCall.setCamera(cameraManager.getActiveCameraId());
851 mPreviewSurfaceState = PreviewSurfaceState.CAMERA_SET;
852 videoCall.requestCameraCapabilities();
853 } else {
854 mPreviewSurfaceState = PreviewSurfaceState.NONE;
855 videoCall.setCamera(null);
856 }
857 }
858
859 /** Exits video mode by hiding the video surfaces and making other adjustments (eg. audio). */
860 private void exitVideoMode() {
861 LogUtil.i("VideoCallPresenter.exitVideoMode", "");
862
863 showVideoUi(
864 VideoProfile.STATE_AUDIO_ONLY,
865 DialerCall.State.ACTIVE,
Eric Erfanian90508232017-03-24 09:31:16 -0700866 SessionModificationState.NO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800867 false /* isRemotelyHeld */);
868 enableCamera(mVideoCall, false);
869 InCallPresenter.getInstance().setFullScreen(false);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700870 InCallPresenter.getInstance().enableScreenTimeout(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800871 mIsVideoMode = false;
872 }
873
874 /**
875 * Based on the current video state and call state, show or hide the incoming and outgoing video
876 * surfaces. The outgoing video surface is shown any time video is transmitting. The incoming
877 * video surface is shown whenever the video is un-paused and active.
878 *
879 * @param videoState The video state.
880 * @param callState The call state.
881 */
882 private void showVideoUi(
883 int videoState,
884 int callState,
885 @SessionModificationState int sessionModificationState,
886 boolean isRemotelyHeld) {
887 if (mVideoCallScreen == null) {
888 LogUtil.e("VideoCallPresenter.showVideoUi", "videoCallScreen is null returning");
889 return;
890 }
891 boolean showIncomingVideo = showIncomingVideo(videoState, callState);
892 boolean showOutgoingVideo = showOutgoingVideo(mContext, videoState, sessionModificationState);
893 LogUtil.i(
894 "VideoCallPresenter.showVideoUi",
895 "showIncoming: %b, showOutgoing: %b, isRemotelyHeld: %b",
896 showIncomingVideo,
897 showOutgoingVideo,
898 isRemotelyHeld);
899 updateRemoteVideoSurfaceDimensions();
900 mVideoCallScreen.showVideoViews(showOutgoingVideo, showIncomingVideo, isRemotelyHeld);
901
902 InCallPresenter.getInstance().enableScreenTimeout(VideoProfile.isAudioOnly(videoState));
903 updateFullscreenAndGreenScreenMode(callState, sessionModificationState);
904 }
905
906 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800907 * Handles peer video dimension changes.
908 *
909 * @param call The call which experienced a peer video dimension change.
910 * @param width The new peer video width .
911 * @param height The new peer video height.
912 */
913 @Override
914 public void onUpdatePeerDimensions(DialerCall call, int width, int height) {
915 LogUtil.i("VideoCallPresenter.onUpdatePeerDimensions", "width: %d, height: %d", width, height);
916 if (mVideoCallScreen == null) {
917 LogUtil.e("VideoCallPresenter.onUpdatePeerDimensions", "videoCallScreen is null");
918 return;
919 }
920 if (!call.equals(mPrimaryCall)) {
921 LogUtil.e(
922 "VideoCallPresenter.onUpdatePeerDimensions", "current call is not equal to primary");
923 return;
924 }
925
926 // Change size of display surface to match the peer aspect ratio
927 if (width > 0 && height > 0 && mVideoCallScreen != null) {
928 getRemoteVideoSurfaceTexture().setSourceVideoDimensions(new Point(width, height));
929 mVideoCallScreen.onRemoteVideoDimensionsChanged();
930 }
931 }
932
933 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800934 * Handles a change to the dimensions of the local camera. Receiving the camera capabilities
935 * triggers the creation of the video
936 *
937 * @param call The call which experienced the camera dimension change.
938 * @param width The new camera video width.
939 * @param height The new camera video height.
940 */
941 @Override
942 public void onCameraDimensionsChange(DialerCall call, int width, int height) {
943 LogUtil.i(
944 "VideoCallPresenter.onCameraDimensionsChange",
945 "call: %s, width: %d, height: %d",
946 call,
947 width,
948 height);
949 if (mVideoCallScreen == null) {
950 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "ui is null");
951 return;
952 }
953
954 if (!call.equals(mPrimaryCall)) {
955 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "not the primary call");
956 return;
957 }
958
959 mPreviewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
960 changePreviewDimensions(width, height);
961
962 // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
963 // If it not yet ready, it will be set when when creation completes.
964 Surface surface = getLocalVideoSurfaceTexture().getSavedSurface();
965 if (surface != null) {
966 mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
967 mVideoCall.setPreviewSurface(surface);
968 }
969 }
970
971 /**
972 * Changes the dimensions of the preview surface.
973 *
974 * @param width The new width.
975 * @param height The new height.
976 */
977 private void changePreviewDimensions(int width, int height) {
978 if (mVideoCallScreen == null) {
979 return;
980 }
981
982 // Resize the surface used to display the preview video
983 getLocalVideoSurfaceTexture().setSurfaceDimensions(new Point(width, height));
984 mVideoCallScreen.onLocalVideoDimensionsChanged();
985 }
986
987 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800988 * Handles changes to the device orientation.
989 *
990 * @param orientation The screen orientation of the device (one of: {@link
991 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
992 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
993 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
994 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
995 */
996 @Override
997 public void onDeviceOrientationChanged(int orientation) {
998 LogUtil.i(
999 "VideoCallPresenter.onDeviceOrientationChanged",
1000 "orientation: %d -> %d",
1001 mDeviceOrientation,
1002 orientation);
1003 mDeviceOrientation = orientation;
1004
1005 if (mVideoCallScreen == null) {
1006 LogUtil.e("VideoCallPresenter.onDeviceOrientationChanged", "videoCallScreen is null");
1007 return;
1008 }
1009
1010 Point previewDimensions = getLocalVideoSurfaceTexture().getSurfaceDimensions();
1011 if (previewDimensions == null) {
1012 return;
1013 }
1014 LogUtil.v(
1015 "VideoCallPresenter.onDeviceOrientationChanged",
1016 "orientation: %d, size: %s",
1017 orientation,
1018 previewDimensions);
1019 changePreviewDimensions(previewDimensions.x, previewDimensions.y);
1020
1021 mVideoCallScreen.onLocalVideoOrientationChanged();
1022 }
1023
1024 /**
1025 * Exits fullscreen mode if the current call context has changed to a non-video call.
1026 *
1027 * @param call The call.
1028 */
1029 protected void maybeExitFullscreen(DialerCall call) {
1030 if (call == null) {
1031 return;
1032 }
1033
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001034 if (!isVideoCall(call) || call.getState() == DialerCall.State.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen");
1036 InCallPresenter.getInstance().setFullScreen(false);
1037 }
1038 }
1039
1040 /**
1041 * Schedules auto-entering of fullscreen mode. Will not enter full screen mode if any of the
1042 * following conditions are met: 1. No call 2. DialerCall is not active 3. The current video state
1043 * is not bi-directional. 4. Already in fullscreen mode 5. In accessibility mode
1044 *
1045 * @param call The current call.
1046 */
1047 protected void maybeAutoEnterFullscreen(DialerCall call) {
1048 if (!mIsAutoFullscreenEnabled) {
1049 return;
1050 }
1051
1052 if (call == null
1053 || call.getState() != DialerCall.State.ACTIVE
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001054 || !isBidirectionalVideoCall(call)
Eric Erfanianccca3152017-02-22 16:32:36 -08001055 || InCallPresenter.getInstance().isFullscreen()
1056 || (mContext != null && AccessibilityUtil.isTouchExplorationEnabled(mContext))) {
1057 // Ensure any previously scheduled attempt to enter fullscreen is cancelled.
1058 cancelAutoFullScreen();
1059 return;
1060 }
1061
1062 if (mAutoFullScreenPending) {
1063 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "already pending.");
1064 return;
1065 }
1066 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "scheduled");
1067 mAutoFullScreenPending = true;
1068 mHandler.removeCallbacks(mAutoFullscreenRunnable);
1069 mHandler.postDelayed(mAutoFullscreenRunnable, mAutoFullscreenTimeoutMillis);
1070 }
1071
1072 /** Cancels pending auto fullscreen mode. */
1073 @Override
1074 public void cancelAutoFullScreen() {
1075 if (!mAutoFullScreenPending) {
1076 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "none pending.");
1077 return;
1078 }
1079 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "cancelling pending");
1080 mAutoFullScreenPending = false;
1081 mHandler.removeCallbacks(mAutoFullscreenRunnable);
1082 }
1083
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001084 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001085 public boolean shouldShowCameraPermissionToast() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001086 if (mPrimaryCall == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001087 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001088 return false;
1089 }
1090 if (mPrimaryCall.didShowCameraPermission()) {
1091 LogUtil.i(
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 "VideoCallPresenter.shouldShowCameraPermissionToast", "already shown for this call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001093 return false;
1094 }
1095 if (!ConfigProviderBindings.get(mContext)
1096 .getBoolean("camera_permission_dialog_allowed", true)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001097 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "disabled by config");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001098 return false;
1099 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001100 return !VideoUtils.hasCameraPermission(mContext)
1101 || !PermissionsUtil.hasCameraPrivacyToastShown(mContext);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001102 }
1103
1104 @Override
1105 public void onCameraPermissionDialogShown() {
1106 if (mPrimaryCall != null) {
1107 mPrimaryCall.setDidShowCameraPermission(true);
1108 }
1109 }
1110
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 private void updateRemoteVideoSurfaceDimensions() {
1112 Activity activity = mVideoCallScreen.getVideoCallScreenFragment().getActivity();
1113 if (activity != null) {
1114 Point screenSize = new Point();
1115 activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
1116 getRemoteVideoSurfaceTexture().setSurfaceDimensions(screenSize);
1117 }
1118 }
1119
1120 private static boolean isVideoUpgrade(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001121 return call != null
1122 && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -08001123 }
1124
1125 private static boolean isVideoUpgrade(@SessionModificationState int state) {
1126 return VideoUtils.hasSentVideoUpgradeRequest(state)
1127 || VideoUtils.hasReceivedVideoUpgradeRequest(state);
1128 }
1129
1130 private class LocalDelegate implements VideoSurfaceDelegate {
1131 @Override
1132 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
1133 if (mVideoCallScreen == null) {
1134 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no UI");
1135 return;
1136 }
1137 if (mVideoCall == null) {
1138 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no video call");
1139 return;
1140 }
1141
1142 // If the preview surface has just been created and we have already received camera
1143 // capabilities, but not yet set the surface, we will set the surface now.
1144 if (mPreviewSurfaceState == PreviewSurfaceState.CAPABILITIES_RECEIVED) {
1145 mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
1146 mVideoCall.setPreviewSurface(videoCallSurface.getSavedSurface());
1147 } else if (mPreviewSurfaceState == PreviewSurfaceState.NONE && isCameraRequired()) {
1148 enableCamera(mVideoCall, true);
1149 }
1150 }
1151
1152 @Override
1153 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
1154 if (mVideoCall == null) {
1155 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceReleased", "no video call");
1156 return;
1157 }
1158
1159 mVideoCall.setPreviewSurface(null);
1160 enableCamera(mVideoCall, false);
1161 }
1162
1163 @Override
1164 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {
1165 if (mVideoCall == null) {
1166 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", "no video call");
1167 return;
1168 }
1169
1170 boolean isChangingConfigurations = InCallPresenter.getInstance().isChangingConfigurations();
1171 if (!isChangingConfigurations) {
1172 enableCamera(mVideoCall, false);
1173 } else {
1174 LogUtil.i(
1175 "VideoCallPresenter.LocalDelegate.onSurfaceDestroyed",
1176 "activity is being destroyed due to configuration changes. Not closing the camera.");
1177 }
1178 }
1179
1180 @Override
1181 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1182 VideoCallPresenter.this.onSurfaceClick();
1183 }
1184 }
1185
1186 private class RemoteDelegate implements VideoSurfaceDelegate {
1187 @Override
1188 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
1189 if (mVideoCallScreen == null) {
1190 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no UI");
1191 return;
1192 }
1193 if (mVideoCall == null) {
1194 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no video call");
1195 return;
1196 }
1197 mVideoCall.setDisplaySurface(videoCallSurface.getSavedSurface());
1198 }
1199
1200 @Override
1201 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
1202 if (mVideoCall == null) {
1203 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceReleased", "no video call");
1204 return;
1205 }
1206 mVideoCall.setDisplaySurface(null);
1207 }
1208
1209 @Override
1210 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {}
1211
1212 @Override
1213 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1214 VideoCallPresenter.this.onSurfaceClick();
1215 }
1216 }
1217
1218 /** Defines the state of the preview surface negotiation with the telephony layer. */
1219 private static class PreviewSurfaceState {
1220
1221 /**
1222 * The camera has not yet been set on the {@link VideoCall}; negotiation has not yet started.
1223 */
1224 private static final int NONE = 0;
1225
1226 /**
1227 * The camera has been set on the {@link VideoCall}, but camera capabilities have not yet been
1228 * received.
1229 */
1230 private static final int CAMERA_SET = 1;
1231
1232 /**
1233 * The camera capabilties have been received from telephony, but the surface has not yet been
1234 * set on the {@link VideoCall}.
1235 */
1236 private static final int CAPABILITIES_RECEIVED = 2;
1237
1238 /** The surface has been set on the {@link VideoCall}. */
1239 private static final int SURFACE_SET = 3;
1240 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001241
1242 private static boolean isBidirectionalVideoCall(DialerCall call) {
1243 return CompatUtils.isVideoCompatible() && VideoProfile.isBidirectional(call.getVideoState());
1244 }
1245
1246 private static boolean isIncomingVideoCall(DialerCall call) {
1247 if (!isVideoCall(call)) {
1248 return false;
1249 }
1250 final int state = call.getState();
1251 return (state == DialerCall.State.INCOMING) || (state == DialerCall.State.CALL_WAITING);
1252 }
1253
1254 private static boolean isActiveVideoCall(DialerCall call) {
1255 return isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE;
1256 }
1257
1258 private static boolean isOutgoingVideoCall(DialerCall call) {
1259 if (!isVideoCall(call)) {
1260 return false;
1261 }
1262 final int state = call.getState();
1263 return DialerCall.State.isDialing(state)
1264 || state == DialerCall.State.CONNECTING
1265 || state == DialerCall.State.SELECT_PHONE_ACCOUNT;
1266 }
1267
1268 private static boolean isAudioCall(DialerCall call) {
1269 if (!CompatUtils.isVideoCompatible()) {
1270 return true;
1271 }
1272
1273 return call != null && VideoProfile.isAudioOnly(call.getVideoState());
1274 }
1275
1276 private static boolean isVideoCall(@Nullable DialerCall call) {
1277 return call != null && call.isVideoCall();
1278 }
1279
1280 private static boolean isVideoCall(int videoState) {
1281 return CompatUtils.isVideoCompatible()
1282 && (VideoProfile.isTransmissionEnabled(videoState)
1283 || VideoProfile.isReceptionEnabled(videoState));
1284 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001285}