blob: 1700d530dbe16b6e0d85f875b596058a175ea0aa [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;
wangqida410d32018-03-06 16:51:38 -080023import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080024import android.support.annotation.Nullable;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.telecom.InCallService.VideoCall;
26import android.telecom.VideoProfile;
27import android.telecom.VideoProfile.CameraCapabilities;
28import android.view.Surface;
Eric Erfanian90508232017-03-24 09:31:16 -070029import android.view.SurfaceView;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080031import com.android.dialer.common.LogUtil;
zachh190343a2018-05-31 17:30:46 -070032import com.android.dialer.configprovider.ConfigProviderComponent;
Eric Erfanian2ca43182017-08-31 06:57:16 -070033import 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.InCallVideoCallCallbackNotifier;
42import com.android.incallui.call.InCallVideoCallCallbackNotifier.SurfaceChangeListener;
wangqibb94ca62018-04-27 14:34:04 -070043import com.android.incallui.call.state.DialerCallState;
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,
wangqida410d32018-03-06 16:51:38 -080083 VideoCallScreenDelegate,
84 CallList.Listener {
Eric Erfanianccca3152017-02-22 16:32:36 -080085
linyuh183cb712017-12-27 17:02:37 -080086 private static boolean isVideoMode = false;
Eric Erfanianccca3152017-02-22 16:32:36 -080087
linyuh183cb712017-12-27 17:02:37 -080088 private final Handler handler = new Handler();
89 private VideoCallScreen videoCallScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -080090
91 /** The current context. */
linyuh183cb712017-12-27 17:02:37 -080092 private Context context;
Eric Erfanianccca3152017-02-22 16:32:36 -080093
Eric Erfanianccca3152017-02-22 16:32:36 -080094 /** The call the video surfaces are currently related to */
linyuh183cb712017-12-27 17:02:37 -080095 private DialerCall primaryCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080096 /**
97 * The {@link VideoCall} used to inform the video telephony layer of changes to the video
98 * surfaces.
99 */
linyuh183cb712017-12-27 17:02:37 -0800100 private VideoCall videoCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800101 /** Determines if the current UI state represents a video call. */
linyuh183cb712017-12-27 17:02:37 -0800102 private int currentVideoState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800103 /** DialerCall's current state */
wangqibb94ca62018-04-27 14:34:04 -0700104 private int currentCallState = DialerCallState.INVALID;
Eric Erfanianccca3152017-02-22 16:32:36 -0800105 /** Determines the device orientation (portrait/lanscape). */
linyuh183cb712017-12-27 17:02:37 -0800106 private int deviceOrientation = InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800107 /** Tracks the state of the preview surface negotiation with the telephony layer. */
linyuh183cb712017-12-27 17:02:37 -0800108 private int previewSurfaceState = PreviewSurfaceState.NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800109 /**
110 * Determines whether video calls should automatically enter full screen mode after {@link
linyuh183cb712017-12-27 17:02:37 -0800111 * #autoFullscreenTimeoutMillis} milliseconds.
Eric Erfanianccca3152017-02-22 16:32:36 -0800112 */
linyuh183cb712017-12-27 17:02:37 -0800113 private boolean isAutoFullscreenEnabled = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 /**
115 * Determines the number of milliseconds after which a video call will automatically enter
linyuh183cb712017-12-27 17:02:37 -0800116 * fullscreen mode. Requires {@link #isAutoFullscreenEnabled} to be {@code true}.
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 */
linyuh183cb712017-12-27 17:02:37 -0800118 private int autoFullscreenTimeoutMillis = 0;
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 /**
120 * Determines if the countdown is currently running to automatically enter full screen video mode.
121 */
linyuh183cb712017-12-27 17:02:37 -0800122 private boolean autoFullScreenPending = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 /** Whether if the call is remotely held. */
linyuh183cb712017-12-27 17:02:37 -0800124 private boolean isRemotelyHeld = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800125 /**
126 * Runnable which is posted to schedule automatically entering fullscreen mode. Will not auto
127 * enter fullscreen mode if the dialpad is visible (doing so would make it impossible to exit the
128 * dialpad).
129 */
linyuh183cb712017-12-27 17:02:37 -0800130 private Runnable autoFullscreenRunnable =
Eric Erfanianccca3152017-02-22 16:32:36 -0800131 new Runnable() {
132 @Override
133 public void run() {
linyuh183cb712017-12-27 17:02:37 -0800134 if (autoFullScreenPending
Eric Erfanianccca3152017-02-22 16:32:36 -0800135 && !InCallPresenter.getInstance().isDialpadVisible()
linyuh183cb712017-12-27 17:02:37 -0800136 && isVideoMode) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800137
138 LogUtil.v("VideoCallPresenter.mAutoFullScreenRunnable", "entering fullscreen mode");
139 InCallPresenter.getInstance().setFullScreen(true);
linyuh183cb712017-12-27 17:02:37 -0800140 autoFullScreenPending = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800141 } else {
142 LogUtil.v(
143 "VideoCallPresenter.mAutoFullScreenRunnable",
144 "skipping scheduled fullscreen mode.");
145 }
146 }
147 };
148
149 private boolean isVideoCallScreenUiReady;
150
151 private static boolean isCameraRequired(int videoState, int sessionModificationState) {
152 return VideoProfile.isBidirectional(videoState)
153 || VideoProfile.isTransmissionEnabled(videoState)
154 || isVideoUpgrade(sessionModificationState);
155 }
156
157 /**
158 * Determines if the incoming video surface should be shown based on the current videoState and
Eric Erfaniand8046e52017-04-06 09:41:50 -0700159 * callState. The video surface is shown when incoming video is not paused, the call is active or
160 * dialing and video reception is enabled.
Eric Erfanianccca3152017-02-22 16:32:36 -0800161 *
162 * @param videoState The current video state.
163 * @param callState The current call state.
164 * @return {@code true} if the incoming video surface should be shown, {@code false} otherwise.
165 */
linyuh122fb0b2018-03-26 13:35:32 -0700166 static boolean showIncomingVideo(int videoState, int callState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800167
168 boolean isPaused = VideoProfile.isPaused(videoState);
wangqibb94ca62018-04-27 14:34:04 -0700169 boolean isCallActive = callState == DialerCallState.ACTIVE;
wangqi385a5a12017-09-28 10:44:54 -0700170 // Show incoming Video for dialing calls to support early media
Eric Erfaniand8046e52017-04-06 09:41:50 -0700171 boolean isCallOutgoingPending =
wangqibb94ca62018-04-27 14:34:04 -0700172 DialerCallState.isDialing(callState) || callState == DialerCallState.CONNECTING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800173
Eric Erfaniand8046e52017-04-06 09:41:50 -0700174 return !isPaused
175 && (isCallActive || isCallOutgoingPending)
176 && VideoProfile.isReceptionEnabled(videoState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800177 }
178
179 /**
180 * Determines if the outgoing video surface should be shown based on the current videoState. The
181 * video surface is shown if video transmission is enabled.
182 *
183 * @return {@code true} if the the outgoing video surface should be shown, {@code false}
184 * otherwise.
185 */
linyuh122fb0b2018-03-26 13:35:32 -0700186 private static boolean showOutgoingVideo(
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 Context context, int videoState, int sessionModificationState) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700188 if (!VideoUtils.hasCameraPermissionAndShownPrivacyToast(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800189 LogUtil.i("VideoCallPresenter.showOutgoingVideo", "Camera permission is disabled by user.");
190 return false;
191 }
192
Eric Erfanianccca3152017-02-22 16:32:36 -0800193 return VideoProfile.isTransmissionEnabled(videoState)
194 || isVideoUpgrade(sessionModificationState);
195 }
196
197 private static void updateCameraSelection(DialerCall call) {
198 LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + call);
199 LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + toSimpleString(call));
200
201 final DialerCall activeCall = CallList.getInstance().getActiveCall();
202 int cameraDir;
203
204 // this function should never be called with null call object, however if it happens we
205 // should handle it gracefully.
206 if (call == null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700207 cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800208 LogUtil.e(
209 "VideoCallPresenter.updateCameraSelection",
210 "call is null. Setting camera direction to default value (CAMERA_DIRECTION_UNKNOWN)");
211 }
212
213 // Clear camera direction if this is not a video call.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700214 else if (isAudioCall(call) && !isVideoUpgrade(call)) {
215 cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN;
216 call.setCameraDir(cameraDir);
Eric Erfanianccca3152017-02-22 16:32:36 -0800217 }
218
219 // If this is a waiting video call, default to active call's camera,
220 // since we don't want to change the current camera for waiting call
221 // without user's permission.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700222 else if (isVideoCall(activeCall) && isIncomingVideoCall(call)) {
223 cameraDir = activeCall.getCameraDir();
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 }
225
226 // Infer the camera direction from the video state and store it,
227 // if this is an outgoing video call.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700228 else if (isOutgoingVideoCall(call) && !isCameraDirectionSet(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800229 cameraDir = toCameraDirection(call.getVideoState());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700230 call.setCameraDir(cameraDir);
Eric Erfanianccca3152017-02-22 16:32:36 -0800231 }
232
233 // Use the stored camera dir if this is an outgoing video call for which camera direction
234 // is set.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700235 else if (isOutgoingVideoCall(call)) {
236 cameraDir = call.getCameraDir();
Eric Erfanianccca3152017-02-22 16:32:36 -0800237 }
238
239 // Infer the camera direction from the video state and store it,
240 // if this is an active video call and camera direction is not set.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700241 else if (isActiveVideoCall(call) && !isCameraDirectionSet(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 cameraDir = toCameraDirection(call.getVideoState());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700243 call.setCameraDir(cameraDir);
Eric Erfanianccca3152017-02-22 16:32:36 -0800244 }
245
246 // Use the stored camera dir if this is an active video call for which camera direction
247 // is set.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700248 else if (isActiveVideoCall(call)) {
249 cameraDir = call.getCameraDir();
Eric Erfanianccca3152017-02-22 16:32:36 -0800250 }
251
252 // For all other cases infer the camera direction but don't store it in the call object.
253 else {
254 cameraDir = toCameraDirection(call.getVideoState());
255 }
256
257 LogUtil.i(
258 "VideoCallPresenter.updateCameraSelection",
259 "setting camera direction to %d, call: %s",
260 cameraDir,
261 call);
262 final InCallCameraManager cameraManager =
263 InCallPresenter.getInstance().getInCallCameraManager();
264 cameraManager.setUseFrontFacingCamera(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700265 cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800266 }
267
268 private static int toCameraDirection(int videoState) {
269 return VideoProfile.isTransmissionEnabled(videoState)
270 && !VideoProfile.isBidirectional(videoState)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700271 ? CameraDirection.CAMERA_DIRECTION_BACK_FACING
272 : CameraDirection.CAMERA_DIRECTION_FRONT_FACING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800273 }
274
275 private static boolean isCameraDirectionSet(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700276 return isVideoCall(call) && call.getCameraDir() != CameraDirection.CAMERA_DIRECTION_UNKNOWN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 }
278
279 private static String toSimpleString(DialerCall call) {
280 return call == null ? null : call.toSimpleString();
281 }
282
283 /**
284 * Initializes the presenter.
285 *
286 * @param context The current context.
287 */
288 @Override
289 public void initVideoCallScreenDelegate(Context context, VideoCallScreen videoCallScreen) {
linyuh183cb712017-12-27 17:02:37 -0800290 this.context = context;
291 this.videoCallScreen = videoCallScreen;
292 isAutoFullscreenEnabled =
293 this.context.getResources().getBoolean(R.bool.video_call_auto_fullscreen);
294 autoFullscreenTimeoutMillis =
295 this.context.getResources().getInteger(R.integer.video_call_auto_fullscreen_timeout);
Eric Erfanianccca3152017-02-22 16:32:36 -0800296 }
297
298 /** Called when the user interface is ready to be used. */
299 @Override
300 public void onVideoCallScreenUiReady() {
301 LogUtil.v("VideoCallPresenter.onVideoCallScreenUiReady", "");
302 Assert.checkState(!isVideoCallScreenUiReady);
303
linyuh183cb712017-12-27 17:02:37 -0800304 deviceOrientation = InCallOrientationEventListener.getCurrentOrientation();
Eric Erfanianccca3152017-02-22 16:32:36 -0800305
306 // Register for call state changes last
307 InCallPresenter.getInstance().addListener(this);
308 InCallPresenter.getInstance().addDetailsListener(this);
309 InCallPresenter.getInstance().addIncomingCallListener(this);
310 InCallPresenter.getInstance().addOrientationListener(this);
311 // To get updates of video call details changes
312 InCallPresenter.getInstance().addInCallEventListener(this);
313 InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(new LocalDelegate());
314 InCallPresenter.getInstance().getRemoteVideoSurfaceTexture().setDelegate(new RemoteDelegate());
315
wangqida410d32018-03-06 16:51:38 -0800316 CallList.getInstance().addListener(this);
317
Eric Erfanianccca3152017-02-22 16:32:36 -0800318 // Register for surface and video events from {@link InCallVideoCallListener}s.
319 InCallVideoCallCallbackNotifier.getInstance().addSurfaceChangeListener(this);
linyuh183cb712017-12-27 17:02:37 -0800320 currentVideoState = VideoProfile.STATE_AUDIO_ONLY;
wangqibb94ca62018-04-27 14:34:04 -0700321 currentCallState = DialerCallState.INVALID;
Eric Erfanianccca3152017-02-22 16:32:36 -0800322
323 InCallPresenter.InCallState inCallState = InCallPresenter.getInstance().getInCallState();
324 onStateChange(inCallState, inCallState, CallList.getInstance());
325 isVideoCallScreenUiReady = true;
Sekine Yasuakied336cb2017-07-04 16:50:09 +0900326
327 Point sourceVideoDimensions = getRemoteVideoSurfaceTexture().getSourceVideoDimensions();
328 if (sourceVideoDimensions != null && primaryCall != null) {
329 int width = primaryCall.getPeerDimensionWidth();
330 int height = primaryCall.getPeerDimensionHeight();
331 boolean updated = DialerCall.UNKNOWN_PEER_DIMENSIONS != width
332 && DialerCall.UNKNOWN_PEER_DIMENSIONS != height;
333 if (updated && (sourceVideoDimensions.x != width || sourceVideoDimensions.y != height)) {
334 onUpdatePeerDimensions(primaryCall, width, height);
335 }
336 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800337 }
338
339 /** Called when the user interface is no longer ready to be used. */
340 @Override
341 public void onVideoCallScreenUiUnready() {
342 LogUtil.v("VideoCallPresenter.onVideoCallScreenUiUnready", "");
343 Assert.checkState(isVideoCallScreenUiReady);
344
Eric Erfanianccca3152017-02-22 16:32:36 -0800345 cancelAutoFullScreen();
346
347 InCallPresenter.getInstance().removeListener(this);
348 InCallPresenter.getInstance().removeDetailsListener(this);
349 InCallPresenter.getInstance().removeIncomingCallListener(this);
350 InCallPresenter.getInstance().removeOrientationListener(this);
351 InCallPresenter.getInstance().removeInCallEventListener(this);
352 InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(null);
353
wangqida410d32018-03-06 16:51:38 -0800354 CallList.getInstance().removeListener(this);
355
Eric Erfanianccca3152017-02-22 16:32:36 -0800356 InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800357
358 // Ensure that the call's camera direction is updated (most likely to UNKNOWN). Normally this
359 // happens after any call state changes but we're unregistering from InCallPresenter above so
Eric Erfanian938468d2017-10-24 14:05:52 -0700360 // we won't get any more call state changes. See a bug.
linyuh183cb712017-12-27 17:02:37 -0800361 if (primaryCall != null) {
362 updateCameraSelection(primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800363 }
364
365 isVideoCallScreenUiReady = false;
366 }
367
368 /**
369 * Handles clicks on the video surfaces. If not currently in fullscreen mode, will set fullscreen.
370 */
371 private void onSurfaceClick() {
372 LogUtil.i("VideoCallPresenter.onSurfaceClick", "");
373 cancelAutoFullScreen();
374 if (!InCallPresenter.getInstance().isFullscreen()) {
375 InCallPresenter.getInstance().setFullScreen(true);
376 } else {
377 InCallPresenter.getInstance().setFullScreen(false);
linyuh183cb712017-12-27 17:02:37 -0800378 maybeAutoEnterFullscreen(primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800379 // If Activity is not multiwindow, fullscreen will be driven by SystemUI visibility changes
380 // instead. See #onSystemUiVisibilityChange(boolean)
381
382 // TODO (keyboardr): onSystemUiVisibilityChange isn't being called the first time
383 // visibility changes after orientation change, so this is currently always done as a backup.
384 }
385 }
386
387 @Override
388 public void onSystemUiVisibilityChange(boolean visible) {
389 // If the SystemUI has changed to be visible, take us out of fullscreen mode
390 LogUtil.i("VideoCallPresenter.onSystemUiVisibilityChange", "visible: " + visible);
391 if (visible) {
392 InCallPresenter.getInstance().setFullScreen(false);
linyuh183cb712017-12-27 17:02:37 -0800393 maybeAutoEnterFullscreen(primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800394 }
395 }
396
397 @Override
398 public VideoSurfaceTexture getLocalVideoSurfaceTexture() {
399 return InCallPresenter.getInstance().getLocalVideoSurfaceTexture();
400 }
401
402 @Override
403 public VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
404 return InCallPresenter.getInstance().getRemoteVideoSurfaceTexture();
405 }
406
407 @Override
Eric Erfanian90508232017-03-24 09:31:16 -0700408 public void setSurfaceViews(SurfaceView preview, SurfaceView remote) {
409 throw Assert.createUnsupportedOperationFailException();
410 }
411
412 @Override
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 public int getDeviceOrientation() {
linyuh183cb712017-12-27 17:02:37 -0800414 return deviceOrientation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800415 }
416
417 /**
418 * This should only be called when user approved the camera permission, which is local action and
419 * does NOT change any call states.
420 */
421 @Override
422 public void onCameraPermissionGranted() {
423 LogUtil.i("VideoCallPresenter.onCameraPermissionGranted", "");
linyuh183cb712017-12-27 17:02:37 -0800424 PermissionsUtil.setCameraPrivacyToastShown(context);
425 enableCamera(primaryCall, isCameraRequired());
Eric Erfanianccca3152017-02-22 16:32:36 -0800426 showVideoUi(
linyuh183cb712017-12-27 17:02:37 -0800427 primaryCall.getVideoState(),
428 primaryCall.getState(),
429 primaryCall.getVideoTech().getSessionModificationState(),
430 primaryCall.isRemotelyHeld());
Eric Erfanianccca3152017-02-22 16:32:36 -0800431 InCallPresenter.getInstance().getInCallCameraManager().onCameraPermissionGranted();
432 }
433
434 /**
435 * Called when the user interacts with the UI. If a fullscreen timer is pending then we start the
436 * timer from scratch to avoid having the UI disappear while the user is interacting with it.
437 */
438 @Override
439 public void resetAutoFullscreenTimer() {
linyuh183cb712017-12-27 17:02:37 -0800440 if (autoFullScreenPending) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800441 LogUtil.i("VideoCallPresenter.resetAutoFullscreenTimer", "resetting");
linyuh183cb712017-12-27 17:02:37 -0800442 handler.removeCallbacks(autoFullscreenRunnable);
443 handler.postDelayed(autoFullscreenRunnable, autoFullscreenTimeoutMillis);
Eric Erfanianccca3152017-02-22 16:32:36 -0800444 }
445 }
446
447 /**
448 * Handles incoming calls.
449 *
450 * @param oldState The old in call state.
451 * @param newState The new in call state.
452 * @param call The call.
453 */
454 @Override
455 public void onIncomingCall(
456 InCallPresenter.InCallState oldState, InCallPresenter.InCallState newState, DialerCall call) {
wangqi385a5a12017-09-28 10:44:54 -0700457 // If video call screen ui is already destroyed, this shouldn't be called. But the UI may be
458 // updated synchronized by {@link CallCardPresenter#onIncomingCall} before this is called, this
459 // could still be called. Thus just do nothing in this case.
460 if (!isVideoCallScreenUiReady) {
461 LogUtil.i("VideoCallPresenter.onIncomingCall", "UI is not ready");
462 return;
463 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800464 // same logic should happen as with onStateChange()
465 onStateChange(oldState, newState, CallList.getInstance());
466 }
467
468 /**
469 * Handles state changes (including incoming calls)
470 *
471 * @param newState The in call state.
472 * @param callList The call list.
473 */
474 @Override
475 public void onStateChange(
476 InCallPresenter.InCallState oldState,
477 InCallPresenter.InCallState newState,
478 CallList callList) {
479 LogUtil.v(
480 "VideoCallPresenter.onStateChange",
481 "oldState: %s, newState: %s, isVideoMode: %b",
482 oldState,
483 newState,
484 isVideoMode());
485
486 if (newState == InCallPresenter.InCallState.NO_CALLS) {
487 if (isVideoMode()) {
488 exitVideoMode();
489 }
490
491 InCallPresenter.getInstance().cleanupSurfaces();
492 }
493
494 // Determine the primary active call).
495 DialerCall primary = null;
496
497 // Determine the call which is the focus of the user's attention. In the case of an
498 // incoming call waiting call, the primary call is still the active video call, however
499 // the determination of whether we should be in fullscreen mode is based on the type of the
500 // incoming call, not the active video call.
501 DialerCall currentCall = null;
502
503 if (newState == InCallPresenter.InCallState.INCOMING) {
504 // We don't want to replace active video call (primary call)
505 // with a waiting call, since user may choose to ignore/decline the waiting call and
506 // this should have no impact on current active video call, that is, we should not
507 // change the camera or UI unless the waiting VT call becomes active.
508 primary = callList.getActiveCall();
509 currentCall = callList.getIncomingCall();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700510 if (!isActiveVideoCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800511 primary = callList.getIncomingCall();
512 }
513 } else if (newState == InCallPresenter.InCallState.OUTGOING) {
514 currentCall = primary = callList.getOutgoingCall();
515 } else if (newState == InCallPresenter.InCallState.PENDING_OUTGOING) {
516 currentCall = primary = callList.getPendingOutgoingCall();
517 } else if (newState == InCallPresenter.InCallState.INCALL) {
518 currentCall = primary = callList.getActiveCall();
519 }
520
linyuh183cb712017-12-27 17:02:37 -0800521 final boolean primaryChanged = !Objects.equals(primaryCall, primary);
Eric Erfanianccca3152017-02-22 16:32:36 -0800522 LogUtil.i(
523 "VideoCallPresenter.onStateChange",
524 "primaryChanged: %b, primary: %s, mPrimaryCall: %s",
525 primaryChanged,
526 primary,
linyuh183cb712017-12-27 17:02:37 -0800527 primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800528 if (primaryChanged) {
529 onPrimaryCallChanged(primary);
linyuh183cb712017-12-27 17:02:37 -0800530 } else if (primaryCall != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800531 updateVideoCall(primary);
532 }
533 updateCallCache(primary);
534
535 // If the call context changed, potentially exit fullscreen or schedule auto enter of
536 // fullscreen mode.
537 // If the current call context is no longer a video call, exit fullscreen mode.
538 maybeExitFullscreen(currentCall);
539 // Schedule auto-enter of fullscreen mode if the current call context is a video call
540 maybeAutoEnterFullscreen(currentCall);
541 }
542
543 /**
544 * Handles a change to the fullscreen mode of the app.
545 *
546 * @param isFullscreenMode {@code true} if the app is now fullscreen, {@code false} otherwise.
547 */
548 @Override
549 public void onFullscreenModeChanged(boolean isFullscreenMode) {
550 cancelAutoFullScreen();
linyuh183cb712017-12-27 17:02:37 -0800551 if (primaryCall != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800552 updateFullscreenAndGreenScreenMode(
linyuh183cb712017-12-27 17:02:37 -0800553 primaryCall.getState(), primaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800554 } else {
wangqibb94ca62018-04-27 14:34:04 -0700555 updateFullscreenAndGreenScreenMode(
556 DialerCallState.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);
linyuh183cb712017-12-27 17:02:37 -0800562 final boolean hasVideoStateChanged = currentVideoState != call.getVideoState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800563
564 LogUtil.v(
565 "VideoCallPresenter.checkForVideoStateChange",
566 "shouldShowVideoUi: %b, hasVideoStateChanged: %b, isVideoMode: %b, previousVideoState: %s,"
567 + " newVideoState: %s",
568 shouldShowVideoUi,
569 hasVideoStateChanged,
570 isVideoMode(),
linyuh183cb712017-12-27 17:02:37 -0800571 VideoProfile.videoStateToString(currentVideoState),
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 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 =
linyuh183cb712017-12-27 17:02:37 -0800589 currentCallState != call.getState() || isRemotelyHeld != call.isRemotelyHeld();
590 isRemotelyHeld = call.isRemotelyHeld();
Eric Erfanianccca3152017-02-22 16:32:36 -0800591
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)) {
roldenburgca475472017-10-25 13:00:42 -0700612 enableCamera(call, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800613 }
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() {
linyuh183cb712017-12-27 17:02:37 -0800649 return isVideoMode;
Eric Erfanianccca3152017-02-22 16:32:36 -0800650 }
651
652 private void updateCallCache(DialerCall call) {
653 if (call == null) {
linyuh183cb712017-12-27 17:02:37 -0800654 currentVideoState = VideoProfile.STATE_AUDIO_ONLY;
wangqibb94ca62018-04-27 14:34:04 -0700655 currentCallState = DialerCallState.INVALID;
linyuh183cb712017-12-27 17:02:37 -0800656 videoCall = null;
657 primaryCall = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800658 } else {
linyuh183cb712017-12-27 17:02:37 -0800659 currentVideoState = call.getVideoState();
660 videoCall = call.getVideoCall();
661 currentCallState = call.getState();
662 primaryCall = call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800663 }
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,
linyuh183cb712017-12-27 17:02:37 -0800680 primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800681 if (call == null) {
682 return;
683 }
684 // If the details change is not for the currently active call no update is required.
linyuh183cb712017-12-27 17:02:37 -0800685 if (!call.equals(primaryCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800686 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) {
wangqi4a22adb2018-06-18 13:18:06 -0700705 // Call could be null when video call ended. This check could prevent unwanted orientation
706 // change before incall UI gets destroyed.
707 if (call != null) {
708 InCallPresenter.getInstance()
709 .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call));
710 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800711 }
712
713 private void updateFullscreenAndGreenScreenMode(
714 int callState, @SessionModificationState int sessionModificationState) {
linyuh183cb712017-12-27 17:02:37 -0800715 if (videoCallScreen != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 boolean shouldShowFullscreen = InCallPresenter.getInstance().isFullscreen();
717 boolean shouldShowGreenScreen =
wangqibb94ca62018-04-27 14:34:04 -0700718 callState == DialerCallState.DIALING
719 || callState == DialerCallState.CONNECTING
720 || callState == DialerCallState.INCOMING
Eric Erfanianccca3152017-02-22 16:32:36 -0800721 || isVideoUpgrade(sessionModificationState);
linyuh183cb712017-12-27 17:02:37 -0800722 videoCallScreen.updateFullscreenAndGreenScreenMode(
Eric Erfanianccca3152017-02-22 16:32:36 -0800723 shouldShowFullscreen, shouldShowGreenScreen);
724 }
725 }
726
727 /** Checks for a change to the video call and changes it if required. */
728 private void checkForVideoCallChange(DialerCall call) {
729 final VideoCall videoCall = call.getVideoCall();
730 LogUtil.v(
731 "VideoCallPresenter.checkForVideoCallChange",
732 "videoCall: %s, mVideoCall: %s",
733 videoCall,
linyuh183cb712017-12-27 17:02:37 -0800734 this.videoCall);
735 if (!Objects.equals(videoCall, this.videoCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800736 changeVideoCall(call);
737 }
738 }
739
740 /**
741 * Handles a change to the video call. Sets the surfaces on the previous call to null and sets the
742 * surfaces on the new video call accordingly.
743 *
744 * @param call The new video call.
745 */
746 private void changeVideoCall(DialerCall call) {
747 final VideoCall videoCall = call == null ? null : call.getVideoCall();
748 LogUtil.i(
749 "VideoCallPresenter.changeVideoCall",
750 "videoCall: %s, mVideoCall: %s",
751 videoCall,
linyuh183cb712017-12-27 17:02:37 -0800752 this.videoCall);
753 final boolean hasChanged = this.videoCall == null && videoCall != null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800754
linyuh183cb712017-12-27 17:02:37 -0800755 this.videoCall = videoCall;
756 if (this.videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800757 LogUtil.v("VideoCallPresenter.changeVideoCall", "video call or primary call is null. Return");
758 return;
759 }
760
761 if (shouldShowVideoUiForCall(call) && hasChanged) {
762 adjustVideoMode(call);
763 }
764 }
765
766 private boolean isCameraRequired() {
linyuh183cb712017-12-27 17:02:37 -0800767 return primaryCall != null
Eric Erfanianccca3152017-02-22 16:32:36 -0800768 && isCameraRequired(
linyuh183cb712017-12-27 17:02:37 -0800769 primaryCall.getVideoState(), primaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800770 }
771
772 /**
773 * Adjusts the current video mode by setting up the preview and display surfaces as necessary.
774 * Expected to be called whenever the video state associated with a call changes (e.g. a user
Eric Erfanian2ca43182017-08-31 06:57:16 -0700775 * turns their camera on or off) to ensure the correct surfaces are shown/hidden. TODO(vt): Need
Eric Erfanianccca3152017-02-22 16:32:36 -0800776 * to adjust size and orientation of preview surface here.
777 */
778 private void adjustVideoMode(DialerCall call) {
779 VideoCall videoCall = call.getVideoCall();
780 int newVideoState = call.getVideoState();
781
782 LogUtil.i(
783 "VideoCallPresenter.adjustVideoMode",
784 "videoCall: %s, videoState: %d",
785 videoCall,
786 newVideoState);
linyuh183cb712017-12-27 17:02:37 -0800787 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800788 LogUtil.e("VideoCallPresenter.adjustVideoMode", "error VideoCallScreen is null so returning");
789 return;
790 }
791
792 showVideoUi(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700793 newVideoState,
794 call.getState(),
795 call.getVideoTech().getSessionModificationState(),
796 call.isRemotelyHeld());
Eric Erfanianccca3152017-02-22 16:32:36 -0800797
798 // Communicate the current camera to telephony and make a request for the camera
799 // capabilities.
800 if (videoCall != null) {
801 Surface surface = getRemoteVideoSurfaceTexture().getSavedSurface();
802 if (surface != null) {
803 LogUtil.v(
804 "VideoCallPresenter.adjustVideoMode", "calling setDisplaySurface with: " + surface);
805 videoCall.setDisplaySurface(surface);
806 }
807
808 Assert.checkState(
linyuh183cb712017-12-27 17:02:37 -0800809 deviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN);
810 videoCall.setDeviceOrientation(deviceOrientation);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700811 enableCamera(
roldenburgca475472017-10-25 13:00:42 -0700812 call, isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800813 }
linyuh183cb712017-12-27 17:02:37 -0800814 int previousVideoState = currentVideoState;
815 currentVideoState = newVideoState;
816 isVideoMode = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800817
818 // adjustVideoMode may be called if we are already in a 1-way video state. In this case
819 // we do not want to trigger auto-fullscreen mode.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700820 if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800821 maybeAutoEnterFullscreen(call);
822 }
823 }
824
825 private static boolean shouldShowVideoUiForCall(@Nullable DialerCall call) {
826 if (call == null) {
827 return false;
828 }
829
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700830 if (isVideoCall(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800831 return true;
832 }
833
834 if (isVideoUpgrade(call)) {
835 return true;
836 }
837
838 return false;
839 }
840
roldenburgca475472017-10-25 13:00:42 -0700841 private void enableCamera(DialerCall call, boolean isCameraRequired) {
842 LogUtil.v("VideoCallPresenter.enableCamera", "call: %s, enabling: %b", call, isCameraRequired);
843 if (call == null) {
844 LogUtil.i("VideoCallPresenter.enableCamera", "call is null");
Eric Erfanianccca3152017-02-22 16:32:36 -0800845 return;
846 }
847
linyuh183cb712017-12-27 17:02:37 -0800848 boolean hasCameraPermission = VideoUtils.hasCameraPermissionAndShownPrivacyToast(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800849 if (!hasCameraPermission) {
roldenburgca475472017-10-25 13:00:42 -0700850 call.getVideoTech().setCamera(null);
linyuh183cb712017-12-27 17:02:37 -0800851 previewSurfaceState = PreviewSurfaceState.NONE;
Eric Erfanian938468d2017-10-24 14:05:52 -0700852 // TODO(wangqi): Inform remote party that the video is off. This is similar to a bug.
Eric Erfanianccca3152017-02-22 16:32:36 -0800853 } else if (isCameraRequired) {
854 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
roldenburgca475472017-10-25 13:00:42 -0700855 call.getVideoTech().setCamera(cameraManager.getActiveCameraId());
linyuh183cb712017-12-27 17:02:37 -0800856 previewSurfaceState = PreviewSurfaceState.CAMERA_SET;
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 } else {
linyuh183cb712017-12-27 17:02:37 -0800858 previewSurfaceState = PreviewSurfaceState.NONE;
roldenburgca475472017-10-25 13:00:42 -0700859 call.getVideoTech().setCamera(null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 }
861 }
862
863 /** Exits video mode by hiding the video surfaces and making other adjustments (eg. audio). */
864 private void exitVideoMode() {
865 LogUtil.i("VideoCallPresenter.exitVideoMode", "");
866
867 showVideoUi(
868 VideoProfile.STATE_AUDIO_ONLY,
wangqibb94ca62018-04-27 14:34:04 -0700869 DialerCallState.ACTIVE,
Eric Erfanian90508232017-03-24 09:31:16 -0700870 SessionModificationState.NO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800871 false /* isRemotelyHeld */);
linyuh183cb712017-12-27 17:02:37 -0800872 enableCamera(primaryCall, false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 InCallPresenter.getInstance().setFullScreen(false);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700874 InCallPresenter.getInstance().enableScreenTimeout(false);
linyuh183cb712017-12-27 17:02:37 -0800875 isVideoMode = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800876 }
877
878 /**
879 * Based on the current video state and call state, show or hide the incoming and outgoing video
880 * surfaces. The outgoing video surface is shown any time video is transmitting. The incoming
881 * video surface is shown whenever the video is un-paused and active.
882 *
883 * @param videoState The video state.
884 * @param callState The call state.
885 */
886 private void showVideoUi(
887 int videoState,
888 int callState,
889 @SessionModificationState int sessionModificationState,
890 boolean isRemotelyHeld) {
linyuh183cb712017-12-27 17:02:37 -0800891 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800892 LogUtil.e("VideoCallPresenter.showVideoUi", "videoCallScreen is null returning");
893 return;
894 }
895 boolean showIncomingVideo = showIncomingVideo(videoState, callState);
linyuh183cb712017-12-27 17:02:37 -0800896 boolean showOutgoingVideo = showOutgoingVideo(context, videoState, sessionModificationState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800897 LogUtil.i(
898 "VideoCallPresenter.showVideoUi",
899 "showIncoming: %b, showOutgoing: %b, isRemotelyHeld: %b",
900 showIncomingVideo,
901 showOutgoingVideo,
902 isRemotelyHeld);
903 updateRemoteVideoSurfaceDimensions();
linyuh183cb712017-12-27 17:02:37 -0800904 videoCallScreen.showVideoViews(showOutgoingVideo, showIncomingVideo, isRemotelyHeld);
Eric Erfanianccca3152017-02-22 16:32:36 -0800905
906 InCallPresenter.getInstance().enableScreenTimeout(VideoProfile.isAudioOnly(videoState));
907 updateFullscreenAndGreenScreenMode(callState, sessionModificationState);
908 }
909
910 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800911 * Handles peer video dimension changes.
912 *
913 * @param call The call which experienced a peer video dimension change.
914 * @param width The new peer video width .
915 * @param height The new peer video height.
916 */
917 @Override
918 public void onUpdatePeerDimensions(DialerCall call, int width, int height) {
919 LogUtil.i("VideoCallPresenter.onUpdatePeerDimensions", "width: %d, height: %d", width, height);
linyuh183cb712017-12-27 17:02:37 -0800920 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800921 LogUtil.e("VideoCallPresenter.onUpdatePeerDimensions", "videoCallScreen is null");
922 return;
923 }
linyuh183cb712017-12-27 17:02:37 -0800924 if (!call.equals(primaryCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800925 LogUtil.e(
926 "VideoCallPresenter.onUpdatePeerDimensions", "current call is not equal to primary");
927 return;
928 }
929
930 // Change size of display surface to match the peer aspect ratio
linyuh183cb712017-12-27 17:02:37 -0800931 if (width > 0 && height > 0 && videoCallScreen != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800932 getRemoteVideoSurfaceTexture().setSourceVideoDimensions(new Point(width, height));
linyuh183cb712017-12-27 17:02:37 -0800933 videoCallScreen.onRemoteVideoDimensionsChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800934 }
935 }
936
937 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800938 * Handles a change to the dimensions of the local camera. Receiving the camera capabilities
939 * triggers the creation of the video
940 *
941 * @param call The call which experienced the camera dimension change.
942 * @param width The new camera video width.
943 * @param height The new camera video height.
944 */
945 @Override
946 public void onCameraDimensionsChange(DialerCall call, int width, int height) {
947 LogUtil.i(
948 "VideoCallPresenter.onCameraDimensionsChange",
949 "call: %s, width: %d, height: %d",
950 call,
951 width,
952 height);
linyuh183cb712017-12-27 17:02:37 -0800953 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800954 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "ui is null");
955 return;
956 }
957
linyuh183cb712017-12-27 17:02:37 -0800958 if (!call.equals(primaryCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "not the primary call");
960 return;
961 }
962
linyuh183cb712017-12-27 17:02:37 -0800963 previewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
Eric Erfanianccca3152017-02-22 16:32:36 -0800964 changePreviewDimensions(width, height);
965
966 // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
967 // If it not yet ready, it will be set when when creation completes.
968 Surface surface = getLocalVideoSurfaceTexture().getSavedSurface();
969 if (surface != null) {
linyuh183cb712017-12-27 17:02:37 -0800970 previewSurfaceState = PreviewSurfaceState.SURFACE_SET;
971 videoCall.setPreviewSurface(surface);
Eric Erfanianccca3152017-02-22 16:32:36 -0800972 }
973 }
974
975 /**
976 * Changes the dimensions of the preview surface.
977 *
978 * @param width The new width.
979 * @param height The new height.
980 */
981 private void changePreviewDimensions(int width, int height) {
linyuh183cb712017-12-27 17:02:37 -0800982 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800983 return;
984 }
985
986 // Resize the surface used to display the preview video
987 getLocalVideoSurfaceTexture().setSurfaceDimensions(new Point(width, height));
linyuh183cb712017-12-27 17:02:37 -0800988 videoCallScreen.onLocalVideoDimensionsChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800989 }
990
991 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800992 * Handles changes to the device orientation.
993 *
994 * @param orientation The screen orientation of the device (one of: {@link
995 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
996 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
997 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
998 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
999 */
1000 @Override
1001 public void onDeviceOrientationChanged(int orientation) {
1002 LogUtil.i(
1003 "VideoCallPresenter.onDeviceOrientationChanged",
1004 "orientation: %d -> %d",
linyuh183cb712017-12-27 17:02:37 -08001005 deviceOrientation,
Eric Erfanianccca3152017-02-22 16:32:36 -08001006 orientation);
linyuh183cb712017-12-27 17:02:37 -08001007 deviceOrientation = orientation;
Eric Erfanianccca3152017-02-22 16:32:36 -08001008
linyuh183cb712017-12-27 17:02:37 -08001009 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 LogUtil.e("VideoCallPresenter.onDeviceOrientationChanged", "videoCallScreen is null");
1011 return;
1012 }
1013
1014 Point previewDimensions = getLocalVideoSurfaceTexture().getSurfaceDimensions();
1015 if (previewDimensions == null) {
1016 return;
1017 }
1018 LogUtil.v(
1019 "VideoCallPresenter.onDeviceOrientationChanged",
1020 "orientation: %d, size: %s",
1021 orientation,
1022 previewDimensions);
1023 changePreviewDimensions(previewDimensions.x, previewDimensions.y);
1024
linyuh183cb712017-12-27 17:02:37 -08001025 videoCallScreen.onLocalVideoOrientationChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001026 }
1027
1028 /**
1029 * Exits fullscreen mode if the current call context has changed to a non-video call.
1030 *
1031 * @param call The call.
1032 */
1033 protected void maybeExitFullscreen(DialerCall call) {
1034 if (call == null) {
1035 return;
1036 }
1037
wangqibb94ca62018-04-27 14:34:04 -07001038 if (!isVideoCall(call) || call.getState() == DialerCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen");
1040 InCallPresenter.getInstance().setFullScreen(false);
1041 }
1042 }
1043
1044 /**
1045 * Schedules auto-entering of fullscreen mode. Will not enter full screen mode if any of the
1046 * following conditions are met: 1. No call 2. DialerCall is not active 3. The current video state
1047 * is not bi-directional. 4. Already in fullscreen mode 5. In accessibility mode
1048 *
1049 * @param call The current call.
1050 */
1051 protected void maybeAutoEnterFullscreen(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -08001052 if (!isAutoFullscreenEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001053 return;
1054 }
1055
1056 if (call == null
wangqibb94ca62018-04-27 14:34:04 -07001057 || call.getState() != DialerCallState.ACTIVE
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001058 || !isBidirectionalVideoCall(call)
Eric Erfanianccca3152017-02-22 16:32:36 -08001059 || InCallPresenter.getInstance().isFullscreen()
linyuh183cb712017-12-27 17:02:37 -08001060 || (context != null && AccessibilityUtil.isTouchExplorationEnabled(context))) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001061 // Ensure any previously scheduled attempt to enter fullscreen is cancelled.
1062 cancelAutoFullScreen();
1063 return;
1064 }
1065
linyuh183cb712017-12-27 17:02:37 -08001066 if (autoFullScreenPending) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001067 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "already pending.");
1068 return;
1069 }
1070 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "scheduled");
linyuh183cb712017-12-27 17:02:37 -08001071 autoFullScreenPending = true;
1072 handler.removeCallbacks(autoFullscreenRunnable);
1073 handler.postDelayed(autoFullscreenRunnable, autoFullscreenTimeoutMillis);
Eric Erfanianccca3152017-02-22 16:32:36 -08001074 }
1075
1076 /** Cancels pending auto fullscreen mode. */
1077 @Override
1078 public void cancelAutoFullScreen() {
linyuh183cb712017-12-27 17:02:37 -08001079 if (!autoFullScreenPending) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001080 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "none pending.");
1081 return;
1082 }
1083 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "cancelling pending");
linyuh183cb712017-12-27 17:02:37 -08001084 autoFullScreenPending = false;
1085 handler.removeCallbacks(autoFullscreenRunnable);
Eric Erfanianccca3152017-02-22 16:32:36 -08001086 }
1087
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001088 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001089 public boolean shouldShowCameraPermissionToast() {
linyuh183cb712017-12-27 17:02:37 -08001090 if (primaryCall == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001091 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001092 return false;
1093 }
linyuh183cb712017-12-27 17:02:37 -08001094 if (primaryCall.didShowCameraPermission()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001095 LogUtil.i(
Eric Erfanian2ca43182017-08-31 06:57:16 -07001096 "VideoCallPresenter.shouldShowCameraPermissionToast", "already shown for this call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001097 return false;
1098 }
zachh190343a2018-05-31 17:30:46 -07001099 if (!ConfigProviderComponent.get(context)
1100 .getConfigProvider()
1101 .getBoolean("camera_permission_dialog_allowed", true)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001102 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "disabled by config");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001103 return false;
1104 }
linyuh183cb712017-12-27 17:02:37 -08001105 return !VideoUtils.hasCameraPermission(context)
1106 || !PermissionsUtil.hasCameraPrivacyToastShown(context);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001107 }
1108
1109 @Override
1110 public void onCameraPermissionDialogShown() {
linyuh183cb712017-12-27 17:02:37 -08001111 if (primaryCall != null) {
1112 primaryCall.setDidShowCameraPermission(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001113 }
1114 }
1115
Eric Erfanianccca3152017-02-22 16:32:36 -08001116 private void updateRemoteVideoSurfaceDimensions() {
linyuh183cb712017-12-27 17:02:37 -08001117 Activity activity = videoCallScreen.getVideoCallScreenFragment().getActivity();
Eric Erfanianccca3152017-02-22 16:32:36 -08001118 if (activity != null) {
1119 Point screenSize = new Point();
1120 activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
1121 getRemoteVideoSurfaceTexture().setSurfaceDimensions(screenSize);
1122 }
1123 }
1124
1125 private static boolean isVideoUpgrade(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001126 return call != null
1127 && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -08001128 }
1129
1130 private static boolean isVideoUpgrade(@SessionModificationState int state) {
1131 return VideoUtils.hasSentVideoUpgradeRequest(state)
1132 || VideoUtils.hasReceivedVideoUpgradeRequest(state);
1133 }
1134
wangqida410d32018-03-06 16:51:38 -08001135 @Override
1136 public void onIncomingCall(DialerCall call) {}
1137
1138 @Override
1139 public void onUpgradeToVideo(DialerCall call) {}
1140
1141 @Override
1142 public void onSessionModificationStateChange(DialerCall call) {}
1143
1144 @Override
1145 public void onCallListChange(CallList callList) {}
1146
1147 @Override
1148 public void onDisconnect(DialerCall call) {}
1149
1150 @Override
1151 public void onWiFiToLteHandover(DialerCall call) {
1152 if (call.isVideoCall() || call.hasSentVideoUpgradeRequest()) {
1153 videoCallScreen.onHandoverFromWiFiToLte();
1154 }
1155 }
1156
1157 @Override
1158 public void onHandoverToWifiFailed(DialerCall call) {}
1159
1160 @Override
1161 public void onInternationalCallOnWifi(@NonNull DialerCall call) {}
1162
Eric Erfanianccca3152017-02-22 16:32:36 -08001163 private class LocalDelegate implements VideoSurfaceDelegate {
1164 @Override
1165 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001166 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001167 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no UI");
1168 return;
1169 }
linyuh183cb712017-12-27 17:02:37 -08001170 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001171 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no video call");
1172 return;
1173 }
1174
1175 // If the preview surface has just been created and we have already received camera
1176 // capabilities, but not yet set the surface, we will set the surface now.
linyuh183cb712017-12-27 17:02:37 -08001177 if (previewSurfaceState == PreviewSurfaceState.CAPABILITIES_RECEIVED) {
1178 previewSurfaceState = PreviewSurfaceState.SURFACE_SET;
1179 videoCall.setPreviewSurface(videoCallSurface.getSavedSurface());
1180 } else if (previewSurfaceState == PreviewSurfaceState.NONE && isCameraRequired()) {
1181 enableCamera(primaryCall, true);
Eric Erfanianccca3152017-02-22 16:32:36 -08001182 }
1183 }
1184
1185 @Override
1186 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001187 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001188 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceReleased", "no video call");
1189 return;
1190 }
1191
linyuh183cb712017-12-27 17:02:37 -08001192 videoCall.setPreviewSurface(null);
1193 enableCamera(primaryCall, false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001194 }
1195
1196 @Override
1197 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001198 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001199 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", "no video call");
1200 return;
1201 }
1202
1203 boolean isChangingConfigurations = InCallPresenter.getInstance().isChangingConfigurations();
1204 if (!isChangingConfigurations) {
linyuh183cb712017-12-27 17:02:37 -08001205 enableCamera(primaryCall, false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001206 } else {
1207 LogUtil.i(
1208 "VideoCallPresenter.LocalDelegate.onSurfaceDestroyed",
1209 "activity is being destroyed due to configuration changes. Not closing the camera.");
1210 }
1211 }
1212
1213 @Override
1214 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1215 VideoCallPresenter.this.onSurfaceClick();
1216 }
1217 }
1218
1219 private class RemoteDelegate implements VideoSurfaceDelegate {
1220 @Override
1221 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001222 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001223 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no UI");
1224 return;
1225 }
linyuh183cb712017-12-27 17:02:37 -08001226 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no video call");
1228 return;
1229 }
linyuh183cb712017-12-27 17:02:37 -08001230 videoCall.setDisplaySurface(videoCallSurface.getSavedSurface());
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 }
1232
1233 @Override
1234 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001235 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001236 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceReleased", "no video call");
1237 return;
1238 }
linyuh183cb712017-12-27 17:02:37 -08001239 videoCall.setDisplaySurface(null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001240 }
1241
1242 @Override
1243 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {}
1244
1245 @Override
1246 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1247 VideoCallPresenter.this.onSurfaceClick();
1248 }
1249 }
1250
1251 /** Defines the state of the preview surface negotiation with the telephony layer. */
1252 private static class PreviewSurfaceState {
1253
1254 /**
1255 * The camera has not yet been set on the {@link VideoCall}; negotiation has not yet started.
1256 */
1257 private static final int NONE = 0;
1258
1259 /**
1260 * The camera has been set on the {@link VideoCall}, but camera capabilities have not yet been
1261 * received.
1262 */
1263 private static final int CAMERA_SET = 1;
1264
1265 /**
1266 * The camera capabilties have been received from telephony, but the surface has not yet been
1267 * set on the {@link VideoCall}.
1268 */
1269 private static final int CAPABILITIES_RECEIVED = 2;
1270
1271 /** The surface has been set on the {@link VideoCall}. */
1272 private static final int SURFACE_SET = 3;
1273 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001274
1275 private static boolean isBidirectionalVideoCall(DialerCall call) {
linyuh122fb0b2018-03-26 13:35:32 -07001276 return VideoProfile.isBidirectional(call.getVideoState());
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001277 }
1278
1279 private static boolean isIncomingVideoCall(DialerCall call) {
1280 if (!isVideoCall(call)) {
1281 return false;
1282 }
1283 final int state = call.getState();
wangqibb94ca62018-04-27 14:34:04 -07001284 return (state == DialerCallState.INCOMING) || (state == DialerCallState.CALL_WAITING);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001285 }
1286
1287 private static boolean isActiveVideoCall(DialerCall call) {
wangqibb94ca62018-04-27 14:34:04 -07001288 return isVideoCall(call) && call.getState() == DialerCallState.ACTIVE;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001289 }
1290
1291 private static boolean isOutgoingVideoCall(DialerCall call) {
1292 if (!isVideoCall(call)) {
1293 return false;
1294 }
1295 final int state = call.getState();
wangqibb94ca62018-04-27 14:34:04 -07001296 return DialerCallState.isDialing(state)
1297 || state == DialerCallState.CONNECTING
1298 || state == DialerCallState.SELECT_PHONE_ACCOUNT;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001299 }
1300
1301 private static boolean isAudioCall(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001302 return call != null && VideoProfile.isAudioOnly(call.getVideoState());
1303 }
1304
1305 private static boolean isVideoCall(@Nullable DialerCall call) {
1306 return call != null && call.isVideoCall();
1307 }
1308
1309 private static boolean isVideoCall(int videoState) {
linyuh122fb0b2018-03-26 13:35:32 -07001310 return VideoProfile.isTransmissionEnabled(videoState)
1311 || VideoProfile.isReceptionEnabled(videoState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001312 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001313}