blob: 050ce98590ac4f1df52877f600f475425206489a [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;
Eric Erfanian2ca43182017-08-31 06:57:16 -070032import com.android.dialer.configprovider.ConfigProviderBindings;
33import com.android.dialer.util.PermissionsUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080034import com.android.incallui.InCallPresenter.InCallDetailsListener;
35import com.android.incallui.InCallPresenter.InCallOrientationListener;
36import com.android.incallui.InCallPresenter.InCallStateListener;
37import com.android.incallui.InCallPresenter.IncomingCallListener;
38import com.android.incallui.call.CallList;
39import com.android.incallui.call.DialerCall;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070040import com.android.incallui.call.DialerCall.CameraDirection;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import com.android.incallui.call.DialerCall.State;
42import com.android.incallui.call.InCallVideoCallCallbackNotifier;
43import com.android.incallui.call.InCallVideoCallCallbackNotifier.SurfaceChangeListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080044import com.android.incallui.util.AccessibilityUtil;
45import com.android.incallui.video.protocol.VideoCallScreen;
46import com.android.incallui.video.protocol.VideoCallScreenDelegate;
47import com.android.incallui.videosurface.protocol.VideoSurfaceDelegate;
48import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070049import com.android.incallui.videotech.utils.SessionModificationState;
50import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import java.util.Objects;
52
53/**
54 * Logic related to the {@link VideoCallScreen} and for managing changes to the video calling
55 * surfaces based on other user interface events and incoming events from the {@class
56 * VideoCallListener}.
57 *
58 * <p>When a call's video state changes to bi-directional video, the {@link
59 * com.android.incallui.VideoCallPresenter} performs the following negotiation with the telephony
60 * layer:
61 *
62 * <ul>
wangqi385a5a12017-09-28 10:44:54 -070063 * <li>{@code VideoCallPresenter} creates and informs telephony of the display surface.
64 * <li>{@code VideoCallPresenter} creates the preview surface.
65 * <li>{@code VideoCallPresenter} informs telephony of the currently selected camera.
66 * <li>Telephony layer sends {@link CameraCapabilities}, including the dimensions of the video for
67 * the current camera.
68 * <li>{@code VideoCallPresenter} adjusts size of the preview surface to match the aspect ratio of
69 * the camera.
70 * <li>{@code VideoCallPresenter} informs telephony of the new preview surface.
Eric Erfanianccca3152017-02-22 16:32:36 -080071 * </ul>
72 *
73 * <p>When downgrading to an audio-only video state, the {@code VideoCallPresenter} nulls both
74 * surfaces.
75 */
76public class VideoCallPresenter
77 implements IncomingCallListener,
78 InCallOrientationListener,
79 InCallStateListener,
80 InCallDetailsListener,
81 SurfaceChangeListener,
Eric Erfanianccca3152017-02-22 16:32:36 -080082 InCallPresenter.InCallEventListener,
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 */
linyuh183cb712017-12-27 17:02:37 -0800104 private int currentCallState = DialerCall.State.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);
169 boolean isCallActive = callState == DialerCall.State.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 =
172 DialerCall.State.isDialing(callState) || callState == DialerCall.State.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;
321 currentCallState = DialerCall.State.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;
326 }
327
328 /** Called when the user interface is no longer ready to be used. */
329 @Override
330 public void onVideoCallScreenUiUnready() {
331 LogUtil.v("VideoCallPresenter.onVideoCallScreenUiUnready", "");
332 Assert.checkState(isVideoCallScreenUiReady);
333
Eric Erfanianccca3152017-02-22 16:32:36 -0800334 cancelAutoFullScreen();
335
336 InCallPresenter.getInstance().removeListener(this);
337 InCallPresenter.getInstance().removeDetailsListener(this);
338 InCallPresenter.getInstance().removeIncomingCallListener(this);
339 InCallPresenter.getInstance().removeOrientationListener(this);
340 InCallPresenter.getInstance().removeInCallEventListener(this);
341 InCallPresenter.getInstance().getLocalVideoSurfaceTexture().setDelegate(null);
342
wangqida410d32018-03-06 16:51:38 -0800343 CallList.getInstance().removeListener(this);
344
Eric Erfanianccca3152017-02-22 16:32:36 -0800345 InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800346
347 // Ensure that the call's camera direction is updated (most likely to UNKNOWN). Normally this
348 // happens after any call state changes but we're unregistering from InCallPresenter above so
Eric Erfanian938468d2017-10-24 14:05:52 -0700349 // we won't get any more call state changes. See a bug.
linyuh183cb712017-12-27 17:02:37 -0800350 if (primaryCall != null) {
351 updateCameraSelection(primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800352 }
353
354 isVideoCallScreenUiReady = false;
355 }
356
357 /**
358 * Handles clicks on the video surfaces. If not currently in fullscreen mode, will set fullscreen.
359 */
360 private void onSurfaceClick() {
361 LogUtil.i("VideoCallPresenter.onSurfaceClick", "");
362 cancelAutoFullScreen();
363 if (!InCallPresenter.getInstance().isFullscreen()) {
364 InCallPresenter.getInstance().setFullScreen(true);
365 } else {
366 InCallPresenter.getInstance().setFullScreen(false);
linyuh183cb712017-12-27 17:02:37 -0800367 maybeAutoEnterFullscreen(primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800368 // If Activity is not multiwindow, fullscreen will be driven by SystemUI visibility changes
369 // instead. See #onSystemUiVisibilityChange(boolean)
370
371 // TODO (keyboardr): onSystemUiVisibilityChange isn't being called the first time
372 // visibility changes after orientation change, so this is currently always done as a backup.
373 }
374 }
375
376 @Override
377 public void onSystemUiVisibilityChange(boolean visible) {
378 // If the SystemUI has changed to be visible, take us out of fullscreen mode
379 LogUtil.i("VideoCallPresenter.onSystemUiVisibilityChange", "visible: " + visible);
380 if (visible) {
381 InCallPresenter.getInstance().setFullScreen(false);
linyuh183cb712017-12-27 17:02:37 -0800382 maybeAutoEnterFullscreen(primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800383 }
384 }
385
386 @Override
387 public VideoSurfaceTexture getLocalVideoSurfaceTexture() {
388 return InCallPresenter.getInstance().getLocalVideoSurfaceTexture();
389 }
390
391 @Override
392 public VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
393 return InCallPresenter.getInstance().getRemoteVideoSurfaceTexture();
394 }
395
396 @Override
Eric Erfanian90508232017-03-24 09:31:16 -0700397 public void setSurfaceViews(SurfaceView preview, SurfaceView remote) {
398 throw Assert.createUnsupportedOperationFailException();
399 }
400
401 @Override
Eric Erfanianccca3152017-02-22 16:32:36 -0800402 public int getDeviceOrientation() {
linyuh183cb712017-12-27 17:02:37 -0800403 return deviceOrientation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800404 }
405
406 /**
407 * This should only be called when user approved the camera permission, which is local action and
408 * does NOT change any call states.
409 */
410 @Override
411 public void onCameraPermissionGranted() {
412 LogUtil.i("VideoCallPresenter.onCameraPermissionGranted", "");
linyuh183cb712017-12-27 17:02:37 -0800413 PermissionsUtil.setCameraPrivacyToastShown(context);
414 enableCamera(primaryCall, isCameraRequired());
Eric Erfanianccca3152017-02-22 16:32:36 -0800415 showVideoUi(
linyuh183cb712017-12-27 17:02:37 -0800416 primaryCall.getVideoState(),
417 primaryCall.getState(),
418 primaryCall.getVideoTech().getSessionModificationState(),
419 primaryCall.isRemotelyHeld());
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 InCallPresenter.getInstance().getInCallCameraManager().onCameraPermissionGranted();
421 }
422
423 /**
424 * Called when the user interacts with the UI. If a fullscreen timer is pending then we start the
425 * timer from scratch to avoid having the UI disappear while the user is interacting with it.
426 */
427 @Override
428 public void resetAutoFullscreenTimer() {
linyuh183cb712017-12-27 17:02:37 -0800429 if (autoFullScreenPending) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 LogUtil.i("VideoCallPresenter.resetAutoFullscreenTimer", "resetting");
linyuh183cb712017-12-27 17:02:37 -0800431 handler.removeCallbacks(autoFullscreenRunnable);
432 handler.postDelayed(autoFullscreenRunnable, autoFullscreenTimeoutMillis);
Eric Erfanianccca3152017-02-22 16:32:36 -0800433 }
434 }
435
436 /**
437 * Handles incoming calls.
438 *
439 * @param oldState The old in call state.
440 * @param newState The new in call state.
441 * @param call The call.
442 */
443 @Override
444 public void onIncomingCall(
445 InCallPresenter.InCallState oldState, InCallPresenter.InCallState newState, DialerCall call) {
wangqi385a5a12017-09-28 10:44:54 -0700446 // If video call screen ui is already destroyed, this shouldn't be called. But the UI may be
447 // updated synchronized by {@link CallCardPresenter#onIncomingCall} before this is called, this
448 // could still be called. Thus just do nothing in this case.
449 if (!isVideoCallScreenUiReady) {
450 LogUtil.i("VideoCallPresenter.onIncomingCall", "UI is not ready");
451 return;
452 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800453 // same logic should happen as with onStateChange()
454 onStateChange(oldState, newState, CallList.getInstance());
455 }
456
457 /**
458 * Handles state changes (including incoming calls)
459 *
460 * @param newState The in call state.
461 * @param callList The call list.
462 */
463 @Override
464 public void onStateChange(
465 InCallPresenter.InCallState oldState,
466 InCallPresenter.InCallState newState,
467 CallList callList) {
468 LogUtil.v(
469 "VideoCallPresenter.onStateChange",
470 "oldState: %s, newState: %s, isVideoMode: %b",
471 oldState,
472 newState,
473 isVideoMode());
474
475 if (newState == InCallPresenter.InCallState.NO_CALLS) {
476 if (isVideoMode()) {
477 exitVideoMode();
478 }
479
480 InCallPresenter.getInstance().cleanupSurfaces();
481 }
482
483 // Determine the primary active call).
484 DialerCall primary = null;
485
486 // Determine the call which is the focus of the user's attention. In the case of an
487 // incoming call waiting call, the primary call is still the active video call, however
488 // the determination of whether we should be in fullscreen mode is based on the type of the
489 // incoming call, not the active video call.
490 DialerCall currentCall = null;
491
492 if (newState == InCallPresenter.InCallState.INCOMING) {
493 // We don't want to replace active video call (primary call)
494 // with a waiting call, since user may choose to ignore/decline the waiting call and
495 // this should have no impact on current active video call, that is, we should not
496 // change the camera or UI unless the waiting VT call becomes active.
497 primary = callList.getActiveCall();
498 currentCall = callList.getIncomingCall();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700499 if (!isActiveVideoCall(primary)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800500 primary = callList.getIncomingCall();
501 }
502 } else if (newState == InCallPresenter.InCallState.OUTGOING) {
503 currentCall = primary = callList.getOutgoingCall();
504 } else if (newState == InCallPresenter.InCallState.PENDING_OUTGOING) {
505 currentCall = primary = callList.getPendingOutgoingCall();
506 } else if (newState == InCallPresenter.InCallState.INCALL) {
507 currentCall = primary = callList.getActiveCall();
508 }
509
linyuh183cb712017-12-27 17:02:37 -0800510 final boolean primaryChanged = !Objects.equals(primaryCall, primary);
Eric Erfanianccca3152017-02-22 16:32:36 -0800511 LogUtil.i(
512 "VideoCallPresenter.onStateChange",
513 "primaryChanged: %b, primary: %s, mPrimaryCall: %s",
514 primaryChanged,
515 primary,
linyuh183cb712017-12-27 17:02:37 -0800516 primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800517 if (primaryChanged) {
518 onPrimaryCallChanged(primary);
linyuh183cb712017-12-27 17:02:37 -0800519 } else if (primaryCall != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800520 updateVideoCall(primary);
521 }
522 updateCallCache(primary);
523
524 // If the call context changed, potentially exit fullscreen or schedule auto enter of
525 // fullscreen mode.
526 // If the current call context is no longer a video call, exit fullscreen mode.
527 maybeExitFullscreen(currentCall);
528 // Schedule auto-enter of fullscreen mode if the current call context is a video call
529 maybeAutoEnterFullscreen(currentCall);
530 }
531
532 /**
533 * Handles a change to the fullscreen mode of the app.
534 *
535 * @param isFullscreenMode {@code true} if the app is now fullscreen, {@code false} otherwise.
536 */
537 @Override
538 public void onFullscreenModeChanged(boolean isFullscreenMode) {
539 cancelAutoFullScreen();
linyuh183cb712017-12-27 17:02:37 -0800540 if (primaryCall != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800541 updateFullscreenAndGreenScreenMode(
linyuh183cb712017-12-27 17:02:37 -0800542 primaryCall.getState(), primaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800543 } else {
Eric Erfanian90508232017-03-24 09:31:16 -0700544 updateFullscreenAndGreenScreenMode(State.INVALID, SessionModificationState.NO_REQUEST);
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 }
546 }
547
548 private void checkForVideoStateChange(DialerCall call) {
549 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
linyuh183cb712017-12-27 17:02:37 -0800550 final boolean hasVideoStateChanged = currentVideoState != call.getVideoState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800551
552 LogUtil.v(
553 "VideoCallPresenter.checkForVideoStateChange",
554 "shouldShowVideoUi: %b, hasVideoStateChanged: %b, isVideoMode: %b, previousVideoState: %s,"
555 + " newVideoState: %s",
556 shouldShowVideoUi,
557 hasVideoStateChanged,
558 isVideoMode(),
linyuh183cb712017-12-27 17:02:37 -0800559 VideoProfile.videoStateToString(currentVideoState),
Eric Erfanianccca3152017-02-22 16:32:36 -0800560 VideoProfile.videoStateToString(call.getVideoState()));
561 if (!hasVideoStateChanged) {
562 return;
563 }
564
565 updateCameraSelection(call);
566
567 if (shouldShowVideoUi) {
568 adjustVideoMode(call);
569 } else if (isVideoMode()) {
570 exitVideoMode();
571 }
572 }
573
574 private void checkForCallStateChange(DialerCall call) {
575 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call);
576 final boolean hasCallStateChanged =
linyuh183cb712017-12-27 17:02:37 -0800577 currentCallState != call.getState() || isRemotelyHeld != call.isRemotelyHeld();
578 isRemotelyHeld = call.isRemotelyHeld();
Eric Erfanianccca3152017-02-22 16:32:36 -0800579
580 LogUtil.v(
581 "VideoCallPresenter.checkForCallStateChange",
582 "shouldShowVideoUi: %b, hasCallStateChanged: %b, isVideoMode: %b",
583 shouldShowVideoUi,
584 hasCallStateChanged,
585 isVideoMode());
586
587 if (!hasCallStateChanged) {
588 return;
589 }
590
591 if (shouldShowVideoUi) {
592 final InCallCameraManager cameraManager =
593 InCallPresenter.getInstance().getInCallCameraManager();
594
595 String prevCameraId = cameraManager.getActiveCameraId();
596 updateCameraSelection(call);
597 String newCameraId = cameraManager.getActiveCameraId();
598
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700599 if (!Objects.equals(prevCameraId, newCameraId) && isActiveVideoCall(call)) {
roldenburgca475472017-10-25 13:00:42 -0700600 enableCamera(call, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800601 }
602 }
603
604 // Make sure we hide or show the video UI if needed.
605 showVideoUi(
606 call.getVideoState(),
607 call.getState(),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700608 call.getVideoTech().getSessionModificationState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800609 call.isRemotelyHeld());
610 }
611
612 private void onPrimaryCallChanged(DialerCall newPrimaryCall) {
613 final boolean shouldShowVideoUi = shouldShowVideoUiForCall(newPrimaryCall);
614 final boolean isVideoMode = isVideoMode();
615
616 LogUtil.v(
617 "VideoCallPresenter.onPrimaryCallChanged",
618 "shouldShowVideoUi: %b, isVideoMode: %b",
619 shouldShowVideoUi,
620 isVideoMode);
621
622 if (!shouldShowVideoUi && isVideoMode) {
623 // Terminate video mode if new primary call is not a video call
624 // and we are currently in video mode.
625 LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "exiting video mode...");
626 exitVideoMode();
627 } else if (shouldShowVideoUi) {
628 LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "entering video mode...");
629
630 updateCameraSelection(newPrimaryCall);
631 adjustVideoMode(newPrimaryCall);
632 }
633 checkForOrientationAllowedChange(newPrimaryCall);
634 }
635
636 private boolean isVideoMode() {
linyuh183cb712017-12-27 17:02:37 -0800637 return isVideoMode;
Eric Erfanianccca3152017-02-22 16:32:36 -0800638 }
639
640 private void updateCallCache(DialerCall call) {
641 if (call == null) {
linyuh183cb712017-12-27 17:02:37 -0800642 currentVideoState = VideoProfile.STATE_AUDIO_ONLY;
643 currentCallState = DialerCall.State.INVALID;
644 videoCall = null;
645 primaryCall = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800646 } else {
linyuh183cb712017-12-27 17:02:37 -0800647 currentVideoState = call.getVideoState();
648 videoCall = call.getVideoCall();
649 currentCallState = call.getState();
650 primaryCall = call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800651 }
652 }
653
654 /**
655 * Handles changes to the details of the call. The {@link VideoCallPresenter} is interested in
656 * changes to the video state.
657 *
658 * @param call The call for which the details changed.
659 * @param details The new call details.
660 */
661 @Override
662 public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
663 LogUtil.v(
664 "VideoCallPresenter.onDetailsChanged",
665 "call: %s, details: %s, mPrimaryCall: %s",
666 call,
667 details,
linyuh183cb712017-12-27 17:02:37 -0800668 primaryCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800669 if (call == null) {
670 return;
671 }
672 // If the details change is not for the currently active call no update is required.
linyuh183cb712017-12-27 17:02:37 -0800673 if (!call.equals(primaryCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800674 LogUtil.v("VideoCallPresenter.onDetailsChanged", "details not for current active call");
675 return;
676 }
677
678 updateVideoCall(call);
679
680 updateCallCache(call);
681 }
682
683 private void updateVideoCall(DialerCall call) {
684 checkForVideoCallChange(call);
685 checkForVideoStateChange(call);
686 checkForCallStateChange(call);
687 checkForOrientationAllowedChange(call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700688 updateFullscreenAndGreenScreenMode(
689 call.getState(), call.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800690 }
691
692 private void checkForOrientationAllowedChange(@Nullable DialerCall call) {
693 InCallPresenter.getInstance()
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700694 .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call));
Eric Erfanianccca3152017-02-22 16:32:36 -0800695 }
696
697 private void updateFullscreenAndGreenScreenMode(
698 int callState, @SessionModificationState int sessionModificationState) {
linyuh183cb712017-12-27 17:02:37 -0800699 if (videoCallScreen != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 boolean shouldShowFullscreen = InCallPresenter.getInstance().isFullscreen();
701 boolean shouldShowGreenScreen =
702 callState == State.DIALING
703 || callState == State.CONNECTING
704 || callState == State.INCOMING
705 || isVideoUpgrade(sessionModificationState);
linyuh183cb712017-12-27 17:02:37 -0800706 videoCallScreen.updateFullscreenAndGreenScreenMode(
Eric Erfanianccca3152017-02-22 16:32:36 -0800707 shouldShowFullscreen, shouldShowGreenScreen);
708 }
709 }
710
711 /** Checks for a change to the video call and changes it if required. */
712 private void checkForVideoCallChange(DialerCall call) {
713 final VideoCall videoCall = call.getVideoCall();
714 LogUtil.v(
715 "VideoCallPresenter.checkForVideoCallChange",
716 "videoCall: %s, mVideoCall: %s",
717 videoCall,
linyuh183cb712017-12-27 17:02:37 -0800718 this.videoCall);
719 if (!Objects.equals(videoCall, this.videoCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800720 changeVideoCall(call);
721 }
722 }
723
724 /**
725 * Handles a change to the video call. Sets the surfaces on the previous call to null and sets the
726 * surfaces on the new video call accordingly.
727 *
728 * @param call The new video call.
729 */
730 private void changeVideoCall(DialerCall call) {
731 final VideoCall videoCall = call == null ? null : call.getVideoCall();
732 LogUtil.i(
733 "VideoCallPresenter.changeVideoCall",
734 "videoCall: %s, mVideoCall: %s",
735 videoCall,
linyuh183cb712017-12-27 17:02:37 -0800736 this.videoCall);
737 final boolean hasChanged = this.videoCall == null && videoCall != null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800738
linyuh183cb712017-12-27 17:02:37 -0800739 this.videoCall = videoCall;
740 if (this.videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800741 LogUtil.v("VideoCallPresenter.changeVideoCall", "video call or primary call is null. Return");
742 return;
743 }
744
745 if (shouldShowVideoUiForCall(call) && hasChanged) {
746 adjustVideoMode(call);
747 }
748 }
749
750 private boolean isCameraRequired() {
linyuh183cb712017-12-27 17:02:37 -0800751 return primaryCall != null
Eric Erfanianccca3152017-02-22 16:32:36 -0800752 && isCameraRequired(
linyuh183cb712017-12-27 17:02:37 -0800753 primaryCall.getVideoState(), primaryCall.getVideoTech().getSessionModificationState());
Eric Erfanianccca3152017-02-22 16:32:36 -0800754 }
755
756 /**
757 * Adjusts the current video mode by setting up the preview and display surfaces as necessary.
758 * Expected to be called whenever the video state associated with a call changes (e.g. a user
Eric Erfanian2ca43182017-08-31 06:57:16 -0700759 * turns their camera on or off) to ensure the correct surfaces are shown/hidden. TODO(vt): Need
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 * to adjust size and orientation of preview surface here.
761 */
762 private void adjustVideoMode(DialerCall call) {
763 VideoCall videoCall = call.getVideoCall();
764 int newVideoState = call.getVideoState();
765
766 LogUtil.i(
767 "VideoCallPresenter.adjustVideoMode",
768 "videoCall: %s, videoState: %d",
769 videoCall,
770 newVideoState);
linyuh183cb712017-12-27 17:02:37 -0800771 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800772 LogUtil.e("VideoCallPresenter.adjustVideoMode", "error VideoCallScreen is null so returning");
773 return;
774 }
775
776 showVideoUi(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700777 newVideoState,
778 call.getState(),
779 call.getVideoTech().getSessionModificationState(),
780 call.isRemotelyHeld());
Eric Erfanianccca3152017-02-22 16:32:36 -0800781
782 // Communicate the current camera to telephony and make a request for the camera
783 // capabilities.
784 if (videoCall != null) {
785 Surface surface = getRemoteVideoSurfaceTexture().getSavedSurface();
786 if (surface != null) {
787 LogUtil.v(
788 "VideoCallPresenter.adjustVideoMode", "calling setDisplaySurface with: " + surface);
789 videoCall.setDisplaySurface(surface);
790 }
791
792 Assert.checkState(
linyuh183cb712017-12-27 17:02:37 -0800793 deviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN);
794 videoCall.setDeviceOrientation(deviceOrientation);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700795 enableCamera(
roldenburgca475472017-10-25 13:00:42 -0700796 call, isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800797 }
linyuh183cb712017-12-27 17:02:37 -0800798 int previousVideoState = currentVideoState;
799 currentVideoState = newVideoState;
800 isVideoMode = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800801
802 // adjustVideoMode may be called if we are already in a 1-way video state. In this case
803 // we do not want to trigger auto-fullscreen mode.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700804 if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800805 maybeAutoEnterFullscreen(call);
806 }
807 }
808
809 private static boolean shouldShowVideoUiForCall(@Nullable DialerCall call) {
810 if (call == null) {
811 return false;
812 }
813
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 if (isVideoCall(call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800815 return true;
816 }
817
818 if (isVideoUpgrade(call)) {
819 return true;
820 }
821
822 return false;
823 }
824
roldenburgca475472017-10-25 13:00:42 -0700825 private void enableCamera(DialerCall call, boolean isCameraRequired) {
826 LogUtil.v("VideoCallPresenter.enableCamera", "call: %s, enabling: %b", call, isCameraRequired);
827 if (call == null) {
828 LogUtil.i("VideoCallPresenter.enableCamera", "call is null");
Eric Erfanianccca3152017-02-22 16:32:36 -0800829 return;
830 }
831
linyuh183cb712017-12-27 17:02:37 -0800832 boolean hasCameraPermission = VideoUtils.hasCameraPermissionAndShownPrivacyToast(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800833 if (!hasCameraPermission) {
roldenburgca475472017-10-25 13:00:42 -0700834 call.getVideoTech().setCamera(null);
linyuh183cb712017-12-27 17:02:37 -0800835 previewSurfaceState = PreviewSurfaceState.NONE;
Eric Erfanian938468d2017-10-24 14:05:52 -0700836 // TODO(wangqi): Inform remote party that the video is off. This is similar to a bug.
Eric Erfanianccca3152017-02-22 16:32:36 -0800837 } else if (isCameraRequired) {
838 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
roldenburgca475472017-10-25 13:00:42 -0700839 call.getVideoTech().setCamera(cameraManager.getActiveCameraId());
linyuh183cb712017-12-27 17:02:37 -0800840 previewSurfaceState = PreviewSurfaceState.CAMERA_SET;
Eric Erfanianccca3152017-02-22 16:32:36 -0800841 } else {
linyuh183cb712017-12-27 17:02:37 -0800842 previewSurfaceState = PreviewSurfaceState.NONE;
roldenburgca475472017-10-25 13:00:42 -0700843 call.getVideoTech().setCamera(null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800844 }
845 }
846
847 /** Exits video mode by hiding the video surfaces and making other adjustments (eg. audio). */
848 private void exitVideoMode() {
849 LogUtil.i("VideoCallPresenter.exitVideoMode", "");
850
851 showVideoUi(
852 VideoProfile.STATE_AUDIO_ONLY,
853 DialerCall.State.ACTIVE,
Eric Erfanian90508232017-03-24 09:31:16 -0700854 SessionModificationState.NO_REQUEST,
Eric Erfanianccca3152017-02-22 16:32:36 -0800855 false /* isRemotelyHeld */);
linyuh183cb712017-12-27 17:02:37 -0800856 enableCamera(primaryCall, false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 InCallPresenter.getInstance().setFullScreen(false);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700858 InCallPresenter.getInstance().enableScreenTimeout(false);
linyuh183cb712017-12-27 17:02:37 -0800859 isVideoMode = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 }
861
862 /**
863 * Based on the current video state and call state, show or hide the incoming and outgoing video
864 * surfaces. The outgoing video surface is shown any time video is transmitting. The incoming
865 * video surface is shown whenever the video is un-paused and active.
866 *
867 * @param videoState The video state.
868 * @param callState The call state.
869 */
870 private void showVideoUi(
871 int videoState,
872 int callState,
873 @SessionModificationState int sessionModificationState,
874 boolean isRemotelyHeld) {
linyuh183cb712017-12-27 17:02:37 -0800875 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800876 LogUtil.e("VideoCallPresenter.showVideoUi", "videoCallScreen is null returning");
877 return;
878 }
879 boolean showIncomingVideo = showIncomingVideo(videoState, callState);
linyuh183cb712017-12-27 17:02:37 -0800880 boolean showOutgoingVideo = showOutgoingVideo(context, videoState, sessionModificationState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800881 LogUtil.i(
882 "VideoCallPresenter.showVideoUi",
883 "showIncoming: %b, showOutgoing: %b, isRemotelyHeld: %b",
884 showIncomingVideo,
885 showOutgoingVideo,
886 isRemotelyHeld);
887 updateRemoteVideoSurfaceDimensions();
linyuh183cb712017-12-27 17:02:37 -0800888 videoCallScreen.showVideoViews(showOutgoingVideo, showIncomingVideo, isRemotelyHeld);
Eric Erfanianccca3152017-02-22 16:32:36 -0800889
890 InCallPresenter.getInstance().enableScreenTimeout(VideoProfile.isAudioOnly(videoState));
891 updateFullscreenAndGreenScreenMode(callState, sessionModificationState);
892 }
893
894 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800895 * Handles peer video dimension changes.
896 *
897 * @param call The call which experienced a peer video dimension change.
898 * @param width The new peer video width .
899 * @param height The new peer video height.
900 */
901 @Override
902 public void onUpdatePeerDimensions(DialerCall call, int width, int height) {
903 LogUtil.i("VideoCallPresenter.onUpdatePeerDimensions", "width: %d, height: %d", width, height);
linyuh183cb712017-12-27 17:02:37 -0800904 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800905 LogUtil.e("VideoCallPresenter.onUpdatePeerDimensions", "videoCallScreen is null");
906 return;
907 }
linyuh183cb712017-12-27 17:02:37 -0800908 if (!call.equals(primaryCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800909 LogUtil.e(
910 "VideoCallPresenter.onUpdatePeerDimensions", "current call is not equal to primary");
911 return;
912 }
913
914 // Change size of display surface to match the peer aspect ratio
linyuh183cb712017-12-27 17:02:37 -0800915 if (width > 0 && height > 0 && videoCallScreen != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800916 getRemoteVideoSurfaceTexture().setSourceVideoDimensions(new Point(width, height));
linyuh183cb712017-12-27 17:02:37 -0800917 videoCallScreen.onRemoteVideoDimensionsChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800918 }
919 }
920
921 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800922 * Handles a change to the dimensions of the local camera. Receiving the camera capabilities
923 * triggers the creation of the video
924 *
925 * @param call The call which experienced the camera dimension change.
926 * @param width The new camera video width.
927 * @param height The new camera video height.
928 */
929 @Override
930 public void onCameraDimensionsChange(DialerCall call, int width, int height) {
931 LogUtil.i(
932 "VideoCallPresenter.onCameraDimensionsChange",
933 "call: %s, width: %d, height: %d",
934 call,
935 width,
936 height);
linyuh183cb712017-12-27 17:02:37 -0800937 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800938 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "ui is null");
939 return;
940 }
941
linyuh183cb712017-12-27 17:02:37 -0800942 if (!call.equals(primaryCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800943 LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "not the primary call");
944 return;
945 }
946
linyuh183cb712017-12-27 17:02:37 -0800947 previewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
Eric Erfanianccca3152017-02-22 16:32:36 -0800948 changePreviewDimensions(width, height);
949
950 // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
951 // If it not yet ready, it will be set when when creation completes.
952 Surface surface = getLocalVideoSurfaceTexture().getSavedSurface();
953 if (surface != null) {
linyuh183cb712017-12-27 17:02:37 -0800954 previewSurfaceState = PreviewSurfaceState.SURFACE_SET;
955 videoCall.setPreviewSurface(surface);
Eric Erfanianccca3152017-02-22 16:32:36 -0800956 }
957 }
958
959 /**
960 * Changes the dimensions of the preview surface.
961 *
962 * @param width The new width.
963 * @param height The new height.
964 */
965 private void changePreviewDimensions(int width, int height) {
linyuh183cb712017-12-27 17:02:37 -0800966 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800967 return;
968 }
969
970 // Resize the surface used to display the preview video
971 getLocalVideoSurfaceTexture().setSurfaceDimensions(new Point(width, height));
linyuh183cb712017-12-27 17:02:37 -0800972 videoCallScreen.onLocalVideoDimensionsChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800973 }
974
975 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800976 * Handles changes to the device orientation.
977 *
978 * @param orientation The screen orientation of the device (one of: {@link
979 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
980 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
981 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
982 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
983 */
984 @Override
985 public void onDeviceOrientationChanged(int orientation) {
986 LogUtil.i(
987 "VideoCallPresenter.onDeviceOrientationChanged",
988 "orientation: %d -> %d",
linyuh183cb712017-12-27 17:02:37 -0800989 deviceOrientation,
Eric Erfanianccca3152017-02-22 16:32:36 -0800990 orientation);
linyuh183cb712017-12-27 17:02:37 -0800991 deviceOrientation = orientation;
Eric Erfanianccca3152017-02-22 16:32:36 -0800992
linyuh183cb712017-12-27 17:02:37 -0800993 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 LogUtil.e("VideoCallPresenter.onDeviceOrientationChanged", "videoCallScreen is null");
995 return;
996 }
997
998 Point previewDimensions = getLocalVideoSurfaceTexture().getSurfaceDimensions();
999 if (previewDimensions == null) {
1000 return;
1001 }
1002 LogUtil.v(
1003 "VideoCallPresenter.onDeviceOrientationChanged",
1004 "orientation: %d, size: %s",
1005 orientation,
1006 previewDimensions);
1007 changePreviewDimensions(previewDimensions.x, previewDimensions.y);
1008
linyuh183cb712017-12-27 17:02:37 -08001009 videoCallScreen.onLocalVideoOrientationChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 }
1011
1012 /**
1013 * Exits fullscreen mode if the current call context has changed to a non-video call.
1014 *
1015 * @param call The call.
1016 */
1017 protected void maybeExitFullscreen(DialerCall call) {
1018 if (call == null) {
1019 return;
1020 }
1021
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001022 if (!isVideoCall(call) || call.getState() == DialerCall.State.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001023 LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen");
1024 InCallPresenter.getInstance().setFullScreen(false);
1025 }
1026 }
1027
1028 /**
1029 * Schedules auto-entering of fullscreen mode. Will not enter full screen mode if any of the
1030 * following conditions are met: 1. No call 2. DialerCall is not active 3. The current video state
1031 * is not bi-directional. 4. Already in fullscreen mode 5. In accessibility mode
1032 *
1033 * @param call The current call.
1034 */
1035 protected void maybeAutoEnterFullscreen(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -08001036 if (!isAutoFullscreenEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001037 return;
1038 }
1039
1040 if (call == null
1041 || call.getState() != DialerCall.State.ACTIVE
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001042 || !isBidirectionalVideoCall(call)
Eric Erfanianccca3152017-02-22 16:32:36 -08001043 || InCallPresenter.getInstance().isFullscreen()
linyuh183cb712017-12-27 17:02:37 -08001044 || (context != null && AccessibilityUtil.isTouchExplorationEnabled(context))) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 // Ensure any previously scheduled attempt to enter fullscreen is cancelled.
1046 cancelAutoFullScreen();
1047 return;
1048 }
1049
linyuh183cb712017-12-27 17:02:37 -08001050 if (autoFullScreenPending) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001051 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "already pending.");
1052 return;
1053 }
1054 LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "scheduled");
linyuh183cb712017-12-27 17:02:37 -08001055 autoFullScreenPending = true;
1056 handler.removeCallbacks(autoFullscreenRunnable);
1057 handler.postDelayed(autoFullscreenRunnable, autoFullscreenTimeoutMillis);
Eric Erfanianccca3152017-02-22 16:32:36 -08001058 }
1059
1060 /** Cancels pending auto fullscreen mode. */
1061 @Override
1062 public void cancelAutoFullScreen() {
linyuh183cb712017-12-27 17:02:37 -08001063 if (!autoFullScreenPending) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001064 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "none pending.");
1065 return;
1066 }
1067 LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "cancelling pending");
linyuh183cb712017-12-27 17:02:37 -08001068 autoFullScreenPending = false;
1069 handler.removeCallbacks(autoFullscreenRunnable);
Eric Erfanianccca3152017-02-22 16:32:36 -08001070 }
1071
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001072 @Override
Eric Erfanian2ca43182017-08-31 06:57:16 -07001073 public boolean shouldShowCameraPermissionToast() {
linyuh183cb712017-12-27 17:02:37 -08001074 if (primaryCall == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001075 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001076 return false;
1077 }
linyuh183cb712017-12-27 17:02:37 -08001078 if (primaryCall.didShowCameraPermission()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001079 LogUtil.i(
Eric Erfanian2ca43182017-08-31 06:57:16 -07001080 "VideoCallPresenter.shouldShowCameraPermissionToast", "already shown for this call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001081 return false;
1082 }
linyuh183cb712017-12-27 17:02:37 -08001083 if (!ConfigProviderBindings.get(context).getBoolean("camera_permission_dialog_allowed", true)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001084 LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "disabled by config");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001085 return false;
1086 }
linyuh183cb712017-12-27 17:02:37 -08001087 return !VideoUtils.hasCameraPermission(context)
1088 || !PermissionsUtil.hasCameraPrivacyToastShown(context);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001089 }
1090
1091 @Override
1092 public void onCameraPermissionDialogShown() {
linyuh183cb712017-12-27 17:02:37 -08001093 if (primaryCall != null) {
1094 primaryCall.setDidShowCameraPermission(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001095 }
1096 }
1097
Eric Erfanianccca3152017-02-22 16:32:36 -08001098 private void updateRemoteVideoSurfaceDimensions() {
linyuh183cb712017-12-27 17:02:37 -08001099 Activity activity = videoCallScreen.getVideoCallScreenFragment().getActivity();
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 if (activity != null) {
1101 Point screenSize = new Point();
1102 activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
1103 getRemoteVideoSurfaceTexture().setSurfaceDimensions(screenSize);
1104 }
1105 }
1106
1107 private static boolean isVideoUpgrade(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001108 return call != null
1109 && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -08001110 }
1111
1112 private static boolean isVideoUpgrade(@SessionModificationState int state) {
1113 return VideoUtils.hasSentVideoUpgradeRequest(state)
1114 || VideoUtils.hasReceivedVideoUpgradeRequest(state);
1115 }
1116
wangqida410d32018-03-06 16:51:38 -08001117 @Override
1118 public void onIncomingCall(DialerCall call) {}
1119
1120 @Override
1121 public void onUpgradeToVideo(DialerCall call) {}
1122
1123 @Override
1124 public void onSessionModificationStateChange(DialerCall call) {}
1125
1126 @Override
1127 public void onCallListChange(CallList callList) {}
1128
1129 @Override
1130 public void onDisconnect(DialerCall call) {}
1131
1132 @Override
1133 public void onWiFiToLteHandover(DialerCall call) {
1134 if (call.isVideoCall() || call.hasSentVideoUpgradeRequest()) {
1135 videoCallScreen.onHandoverFromWiFiToLte();
1136 }
1137 }
1138
1139 @Override
1140 public void onHandoverToWifiFailed(DialerCall call) {}
1141
1142 @Override
1143 public void onInternationalCallOnWifi(@NonNull DialerCall call) {}
1144
Eric Erfanianccca3152017-02-22 16:32:36 -08001145 private class LocalDelegate implements VideoSurfaceDelegate {
1146 @Override
1147 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001148 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001149 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no UI");
1150 return;
1151 }
linyuh183cb712017-12-27 17:02:37 -08001152 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001153 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no video call");
1154 return;
1155 }
1156
1157 // If the preview surface has just been created and we have already received camera
1158 // capabilities, but not yet set the surface, we will set the surface now.
linyuh183cb712017-12-27 17:02:37 -08001159 if (previewSurfaceState == PreviewSurfaceState.CAPABILITIES_RECEIVED) {
1160 previewSurfaceState = PreviewSurfaceState.SURFACE_SET;
1161 videoCall.setPreviewSurface(videoCallSurface.getSavedSurface());
1162 } else if (previewSurfaceState == PreviewSurfaceState.NONE && isCameraRequired()) {
1163 enableCamera(primaryCall, true);
Eric Erfanianccca3152017-02-22 16:32:36 -08001164 }
1165 }
1166
1167 @Override
1168 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001169 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001170 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceReleased", "no video call");
1171 return;
1172 }
1173
linyuh183cb712017-12-27 17:02:37 -08001174 videoCall.setPreviewSurface(null);
1175 enableCamera(primaryCall, false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001176 }
1177
1178 @Override
1179 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001180 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001181 LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", "no video call");
1182 return;
1183 }
1184
1185 boolean isChangingConfigurations = InCallPresenter.getInstance().isChangingConfigurations();
1186 if (!isChangingConfigurations) {
linyuh183cb712017-12-27 17:02:37 -08001187 enableCamera(primaryCall, false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001188 } else {
1189 LogUtil.i(
1190 "VideoCallPresenter.LocalDelegate.onSurfaceDestroyed",
1191 "activity is being destroyed due to configuration changes. Not closing the camera.");
1192 }
1193 }
1194
1195 @Override
1196 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1197 VideoCallPresenter.this.onSurfaceClick();
1198 }
1199 }
1200
1201 private class RemoteDelegate implements VideoSurfaceDelegate {
1202 @Override
1203 public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001204 if (videoCallScreen == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001205 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no UI");
1206 return;
1207 }
linyuh183cb712017-12-27 17:02:37 -08001208 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001209 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no video call");
1210 return;
1211 }
linyuh183cb712017-12-27 17:02:37 -08001212 videoCall.setDisplaySurface(videoCallSurface.getSavedSurface());
Eric Erfanianccca3152017-02-22 16:32:36 -08001213 }
1214
1215 @Override
1216 public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) {
linyuh183cb712017-12-27 17:02:37 -08001217 if (videoCall == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001218 LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceReleased", "no video call");
1219 return;
1220 }
linyuh183cb712017-12-27 17:02:37 -08001221 videoCall.setDisplaySurface(null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001222 }
1223
1224 @Override
1225 public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {}
1226
1227 @Override
1228 public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) {
1229 VideoCallPresenter.this.onSurfaceClick();
1230 }
1231 }
1232
1233 /** Defines the state of the preview surface negotiation with the telephony layer. */
1234 private static class PreviewSurfaceState {
1235
1236 /**
1237 * The camera has not yet been set on the {@link VideoCall}; negotiation has not yet started.
1238 */
1239 private static final int NONE = 0;
1240
1241 /**
1242 * The camera has been set on the {@link VideoCall}, but camera capabilities have not yet been
1243 * received.
1244 */
1245 private static final int CAMERA_SET = 1;
1246
1247 /**
1248 * The camera capabilties have been received from telephony, but the surface has not yet been
1249 * set on the {@link VideoCall}.
1250 */
1251 private static final int CAPABILITIES_RECEIVED = 2;
1252
1253 /** The surface has been set on the {@link VideoCall}. */
1254 private static final int SURFACE_SET = 3;
1255 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001256
1257 private static boolean isBidirectionalVideoCall(DialerCall call) {
linyuh122fb0b2018-03-26 13:35:32 -07001258 return VideoProfile.isBidirectional(call.getVideoState());
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001259 }
1260
1261 private static boolean isIncomingVideoCall(DialerCall call) {
1262 if (!isVideoCall(call)) {
1263 return false;
1264 }
1265 final int state = call.getState();
1266 return (state == DialerCall.State.INCOMING) || (state == DialerCall.State.CALL_WAITING);
1267 }
1268
1269 private static boolean isActiveVideoCall(DialerCall call) {
1270 return isVideoCall(call) && call.getState() == DialerCall.State.ACTIVE;
1271 }
1272
1273 private static boolean isOutgoingVideoCall(DialerCall call) {
1274 if (!isVideoCall(call)) {
1275 return false;
1276 }
1277 final int state = call.getState();
1278 return DialerCall.State.isDialing(state)
1279 || state == DialerCall.State.CONNECTING
1280 || state == DialerCall.State.SELECT_PHONE_ACCOUNT;
1281 }
1282
1283 private static boolean isAudioCall(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001284 return call != null && VideoProfile.isAudioOnly(call.getVideoState());
1285 }
1286
1287 private static boolean isVideoCall(@Nullable DialerCall call) {
1288 return call != null && call.isVideoCall();
1289 }
1290
1291 private static boolean isVideoCall(int videoState) {
linyuh122fb0b2018-03-26 13:35:32 -07001292 return VideoProfile.isTransmissionEnabled(videoState)
1293 || VideoProfile.isReceptionEnabled(videoState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001294 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001295}