Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.incallui; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
| 21 | import android.graphics.Point; |
| 22 | import android.os.Handler; |
wangqi | da410d3 | 2018-03-06 16:51:38 -0800 | [diff] [blame] | 23 | import android.support.annotation.NonNull; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 24 | import android.support.annotation.Nullable; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 25 | import android.telecom.InCallService.VideoCall; |
| 26 | import android.telecom.VideoProfile; |
| 27 | import android.telecom.VideoProfile.CameraCapabilities; |
| 28 | import android.view.Surface; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 29 | import android.view.SurfaceView; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 30 | import com.android.dialer.common.Assert; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 31 | import com.android.dialer.common.LogUtil; |
zachh | 190343a | 2018-05-31 17:30:46 -0700 | [diff] [blame] | 32 | import com.android.dialer.configprovider.ConfigProviderComponent; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 33 | import com.android.dialer.util.PermissionsUtil; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 34 | import com.android.incallui.InCallPresenter.InCallDetailsListener; |
| 35 | import com.android.incallui.InCallPresenter.InCallOrientationListener; |
| 36 | import com.android.incallui.InCallPresenter.InCallStateListener; |
| 37 | import com.android.incallui.InCallPresenter.IncomingCallListener; |
| 38 | import com.android.incallui.call.CallList; |
| 39 | import com.android.incallui.call.DialerCall; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 40 | import com.android.incallui.call.DialerCall.CameraDirection; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 41 | import com.android.incallui.call.InCallVideoCallCallbackNotifier; |
| 42 | import com.android.incallui.call.InCallVideoCallCallbackNotifier.SurfaceChangeListener; |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 43 | import com.android.incallui.call.state.DialerCallState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 44 | import com.android.incallui.util.AccessibilityUtil; |
| 45 | import com.android.incallui.video.protocol.VideoCallScreen; |
| 46 | import com.android.incallui.video.protocol.VideoCallScreenDelegate; |
| 47 | import com.android.incallui.videosurface.protocol.VideoSurfaceDelegate; |
| 48 | import com.android.incallui.videosurface.protocol.VideoSurfaceTexture; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 49 | import com.android.incallui.videotech.utils.SessionModificationState; |
| 50 | import com.android.incallui.videotech.utils.VideoUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 51 | import 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> |
wangqi | 385a5a1 | 2017-09-28 10:44:54 -0700 | [diff] [blame] | 63 | * <li>{@code VideoCallPresenter} creates and informs telephony of the display surface. |
| 64 | * <li>{@code VideoCallPresenter} creates the preview surface. |
| 65 | * <li>{@code VideoCallPresenter} informs telephony of the currently selected camera. |
| 66 | * <li>Telephony layer sends {@link CameraCapabilities}, including the dimensions of the video for |
| 67 | * the current camera. |
| 68 | * <li>{@code VideoCallPresenter} adjusts size of the preview surface to match the aspect ratio of |
| 69 | * the camera. |
| 70 | * <li>{@code VideoCallPresenter} informs telephony of the new preview surface. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 71 | * </ul> |
| 72 | * |
| 73 | * <p>When downgrading to an audio-only video state, the {@code VideoCallPresenter} nulls both |
| 74 | * surfaces. |
| 75 | */ |
| 76 | public class VideoCallPresenter |
| 77 | implements IncomingCallListener, |
| 78 | InCallOrientationListener, |
| 79 | InCallStateListener, |
| 80 | InCallDetailsListener, |
| 81 | SurfaceChangeListener, |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 82 | InCallPresenter.InCallEventListener, |
wangqi | da410d3 | 2018-03-06 16:51:38 -0800 | [diff] [blame] | 83 | VideoCallScreenDelegate, |
| 84 | CallList.Listener { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 85 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 86 | private static boolean isVideoMode = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 87 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 88 | private final Handler handler = new Handler(); |
| 89 | private VideoCallScreen videoCallScreen; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 90 | |
| 91 | /** The current context. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 92 | private Context context; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 93 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 94 | /** The call the video surfaces are currently related to */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 95 | private DialerCall primaryCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 96 | /** |
| 97 | * The {@link VideoCall} used to inform the video telephony layer of changes to the video |
| 98 | * surfaces. |
| 99 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 100 | private VideoCall videoCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 101 | /** Determines if the current UI state represents a video call. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 102 | private int currentVideoState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 103 | /** DialerCall's current state */ |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 104 | private int currentCallState = DialerCallState.INVALID; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 105 | /** Determines the device orientation (portrait/lanscape). */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 106 | private int deviceOrientation = InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 107 | /** Tracks the state of the preview surface negotiation with the telephony layer. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 108 | private int previewSurfaceState = PreviewSurfaceState.NONE; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Determines whether video calls should automatically enter full screen mode after {@link |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 111 | * #autoFullscreenTimeoutMillis} milliseconds. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 112 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 113 | private boolean isAutoFullscreenEnabled = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Determines the number of milliseconds after which a video call will automatically enter |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 116 | * fullscreen mode. Requires {@link #isAutoFullscreenEnabled} to be {@code true}. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 117 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 118 | private int autoFullscreenTimeoutMillis = 0; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Determines if the countdown is currently running to automatically enter full screen video mode. |
| 121 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 122 | private boolean autoFullScreenPending = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 123 | /** Whether if the call is remotely held. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 124 | private boolean isRemotelyHeld = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 125 | /** |
| 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 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 130 | private Runnable autoFullscreenRunnable = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 131 | new Runnable() { |
| 132 | @Override |
| 133 | public void run() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 134 | if (autoFullScreenPending |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 135 | && !InCallPresenter.getInstance().isDialpadVisible() |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 136 | && isVideoMode) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 137 | |
| 138 | LogUtil.v("VideoCallPresenter.mAutoFullScreenRunnable", "entering fullscreen mode"); |
| 139 | InCallPresenter.getInstance().setFullScreen(true); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 140 | autoFullScreenPending = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 141 | } 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 Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 159 | * 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 161 | * |
| 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 | */ |
linyuh | 122fb0b | 2018-03-26 13:35:32 -0700 | [diff] [blame] | 166 | static boolean showIncomingVideo(int videoState, int callState) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 167 | |
| 168 | boolean isPaused = VideoProfile.isPaused(videoState); |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 169 | boolean isCallActive = callState == DialerCallState.ACTIVE; |
wangqi | 385a5a1 | 2017-09-28 10:44:54 -0700 | [diff] [blame] | 170 | // Show incoming Video for dialing calls to support early media |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 171 | boolean isCallOutgoingPending = |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 172 | DialerCallState.isDialing(callState) || callState == DialerCallState.CONNECTING; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 173 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 174 | return !isPaused |
| 175 | && (isCallActive || isCallOutgoingPending) |
| 176 | && VideoProfile.isReceptionEnabled(videoState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 177 | } |
| 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 | */ |
linyuh | 122fb0b | 2018-03-26 13:35:32 -0700 | [diff] [blame] | 186 | private static boolean showOutgoingVideo( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 187 | Context context, int videoState, int sessionModificationState) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 188 | if (!VideoUtils.hasCameraPermissionAndShownPrivacyToast(context)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 189 | LogUtil.i("VideoCallPresenter.showOutgoingVideo", "Camera permission is disabled by user."); |
| 190 | return false; |
| 191 | } |
| 192 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 193 | return VideoProfile.isTransmissionEnabled(videoState) |
| 194 | || isVideoUpgrade(sessionModificationState); |
| 195 | } |
| 196 | |
| 197 | private static void updateCameraSelection(DialerCall call) { |
| 198 | LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + call); |
| 199 | LogUtil.v("VideoCallPresenter.updateCameraSelection", "call=" + toSimpleString(call)); |
| 200 | |
| 201 | final DialerCall activeCall = CallList.getInstance().getActiveCall(); |
| 202 | int cameraDir; |
| 203 | |
| 204 | // this function should never be called with null call object, however if it happens we |
| 205 | // should handle it gracefully. |
| 206 | if (call == null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 207 | cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 208 | 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 Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 214 | else if (isAudioCall(call) && !isVideoUpgrade(call)) { |
| 215 | cameraDir = CameraDirection.CAMERA_DIRECTION_UNKNOWN; |
| 216 | call.setCameraDir(cameraDir); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 217 | } |
| 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 Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 222 | else if (isVideoCall(activeCall) && isIncomingVideoCall(call)) { |
| 223 | cameraDir = activeCall.getCameraDir(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Infer the camera direction from the video state and store it, |
| 227 | // if this is an outgoing video call. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 228 | else if (isOutgoingVideoCall(call) && !isCameraDirectionSet(call)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 229 | cameraDir = toCameraDirection(call.getVideoState()); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 230 | call.setCameraDir(cameraDir); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Use the stored camera dir if this is an outgoing video call for which camera direction |
| 234 | // is set. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 235 | else if (isOutgoingVideoCall(call)) { |
| 236 | cameraDir = call.getCameraDir(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 237 | } |
| 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 Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 241 | else if (isActiveVideoCall(call) && !isCameraDirectionSet(call)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 242 | cameraDir = toCameraDirection(call.getVideoState()); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 243 | call.setCameraDir(cameraDir); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Use the stored camera dir if this is an active video call for which camera direction |
| 247 | // is set. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 248 | else if (isActiveVideoCall(call)) { |
| 249 | cameraDir = call.getCameraDir(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 250 | } |
| 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 Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 265 | cameraDir == CameraDirection.CAMERA_DIRECTION_FRONT_FACING); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | private static int toCameraDirection(int videoState) { |
| 269 | return VideoProfile.isTransmissionEnabled(videoState) |
| 270 | && !VideoProfile.isBidirectional(videoState) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 271 | ? CameraDirection.CAMERA_DIRECTION_BACK_FACING |
| 272 | : CameraDirection.CAMERA_DIRECTION_FRONT_FACING; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private static boolean isCameraDirectionSet(DialerCall call) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 276 | return isVideoCall(call) && call.getCameraDir() != CameraDirection.CAMERA_DIRECTION_UNKNOWN; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 277 | } |
| 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) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 290 | 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** Called when the user interface is ready to be used. */ |
| 299 | @Override |
| 300 | public void onVideoCallScreenUiReady() { |
| 301 | LogUtil.v("VideoCallPresenter.onVideoCallScreenUiReady", ""); |
| 302 | Assert.checkState(!isVideoCallScreenUiReady); |
| 303 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 304 | deviceOrientation = InCallOrientationEventListener.getCurrentOrientation(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 305 | |
| 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 | |
wangqi | da410d3 | 2018-03-06 16:51:38 -0800 | [diff] [blame] | 316 | CallList.getInstance().addListener(this); |
| 317 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 318 | // Register for surface and video events from {@link InCallVideoCallListener}s. |
| 319 | InCallVideoCallCallbackNotifier.getInstance().addSurfaceChangeListener(this); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 320 | currentVideoState = VideoProfile.STATE_AUDIO_ONLY; |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 321 | currentCallState = DialerCallState.INVALID; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 322 | |
| 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 334 | 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 | |
wangqi | da410d3 | 2018-03-06 16:51:38 -0800 | [diff] [blame] | 343 | CallList.getInstance().removeListener(this); |
| 344 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 345 | InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 346 | |
| 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 Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 349 | // we won't get any more call state changes. See a bug. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 350 | if (primaryCall != null) { |
| 351 | updateCameraSelection(primaryCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 352 | } |
| 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); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 367 | maybeAutoEnterFullscreen(primaryCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 368 | // 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); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 382 | maybeAutoEnterFullscreen(primaryCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 383 | } |
| 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 Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 397 | public void setSurfaceViews(SurfaceView preview, SurfaceView remote) { |
| 398 | throw Assert.createUnsupportedOperationFailException(); |
| 399 | } |
| 400 | |
| 401 | @Override |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 402 | public int getDeviceOrientation() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 403 | return deviceOrientation; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 404 | } |
| 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", ""); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 413 | PermissionsUtil.setCameraPrivacyToastShown(context); |
| 414 | enableCamera(primaryCall, isCameraRequired()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 415 | showVideoUi( |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 416 | primaryCall.getVideoState(), |
| 417 | primaryCall.getState(), |
| 418 | primaryCall.getVideoTech().getSessionModificationState(), |
| 419 | primaryCall.isRemotelyHeld()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 420 | 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() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 429 | if (autoFullScreenPending) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 430 | LogUtil.i("VideoCallPresenter.resetAutoFullscreenTimer", "resetting"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 431 | handler.removeCallbacks(autoFullscreenRunnable); |
| 432 | handler.postDelayed(autoFullscreenRunnable, autoFullscreenTimeoutMillis); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 433 | } |
| 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) { |
wangqi | 385a5a1 | 2017-09-28 10:44:54 -0700 | [diff] [blame] | 446 | // 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 453 | // 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 Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 499 | if (!isActiveVideoCall(primary)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 500 | 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 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 510 | final boolean primaryChanged = !Objects.equals(primaryCall, primary); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 511 | LogUtil.i( |
| 512 | "VideoCallPresenter.onStateChange", |
| 513 | "primaryChanged: %b, primary: %s, mPrimaryCall: %s", |
| 514 | primaryChanged, |
| 515 | primary, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 516 | primaryCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 517 | if (primaryChanged) { |
| 518 | onPrimaryCallChanged(primary); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 519 | } else if (primaryCall != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 520 | 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(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 540 | if (primaryCall != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 541 | updateFullscreenAndGreenScreenMode( |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 542 | primaryCall.getState(), primaryCall.getVideoTech().getSessionModificationState()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 543 | } else { |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 544 | updateFullscreenAndGreenScreenMode( |
| 545 | DialerCallState.INVALID, SessionModificationState.NO_REQUEST); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | private void checkForVideoStateChange(DialerCall call) { |
| 550 | final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 551 | final boolean hasVideoStateChanged = currentVideoState != call.getVideoState(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 552 | |
| 553 | LogUtil.v( |
| 554 | "VideoCallPresenter.checkForVideoStateChange", |
| 555 | "shouldShowVideoUi: %b, hasVideoStateChanged: %b, isVideoMode: %b, previousVideoState: %s," |
| 556 | + " newVideoState: %s", |
| 557 | shouldShowVideoUi, |
| 558 | hasVideoStateChanged, |
| 559 | isVideoMode(), |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 560 | VideoProfile.videoStateToString(currentVideoState), |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 561 | VideoProfile.videoStateToString(call.getVideoState())); |
| 562 | if (!hasVideoStateChanged) { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | updateCameraSelection(call); |
| 567 | |
| 568 | if (shouldShowVideoUi) { |
| 569 | adjustVideoMode(call); |
| 570 | } else if (isVideoMode()) { |
| 571 | exitVideoMode(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | private void checkForCallStateChange(DialerCall call) { |
| 576 | final boolean shouldShowVideoUi = shouldShowVideoUiForCall(call); |
| 577 | final boolean hasCallStateChanged = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 578 | currentCallState != call.getState() || isRemotelyHeld != call.isRemotelyHeld(); |
| 579 | isRemotelyHeld = call.isRemotelyHeld(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 580 | |
| 581 | LogUtil.v( |
| 582 | "VideoCallPresenter.checkForCallStateChange", |
| 583 | "shouldShowVideoUi: %b, hasCallStateChanged: %b, isVideoMode: %b", |
| 584 | shouldShowVideoUi, |
| 585 | hasCallStateChanged, |
| 586 | isVideoMode()); |
| 587 | |
| 588 | if (!hasCallStateChanged) { |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | if (shouldShowVideoUi) { |
| 593 | final InCallCameraManager cameraManager = |
| 594 | InCallPresenter.getInstance().getInCallCameraManager(); |
| 595 | |
| 596 | String prevCameraId = cameraManager.getActiveCameraId(); |
| 597 | updateCameraSelection(call); |
| 598 | String newCameraId = cameraManager.getActiveCameraId(); |
| 599 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 600 | if (!Objects.equals(prevCameraId, newCameraId) && isActiveVideoCall(call)) { |
roldenburg | ca47547 | 2017-10-25 13:00:42 -0700 | [diff] [blame] | 601 | enableCamera(call, true); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
| 605 | // Make sure we hide or show the video UI if needed. |
| 606 | showVideoUi( |
| 607 | call.getVideoState(), |
| 608 | call.getState(), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 609 | call.getVideoTech().getSessionModificationState(), |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 610 | call.isRemotelyHeld()); |
| 611 | } |
| 612 | |
| 613 | private void onPrimaryCallChanged(DialerCall newPrimaryCall) { |
| 614 | final boolean shouldShowVideoUi = shouldShowVideoUiForCall(newPrimaryCall); |
| 615 | final boolean isVideoMode = isVideoMode(); |
| 616 | |
| 617 | LogUtil.v( |
| 618 | "VideoCallPresenter.onPrimaryCallChanged", |
| 619 | "shouldShowVideoUi: %b, isVideoMode: %b", |
| 620 | shouldShowVideoUi, |
| 621 | isVideoMode); |
| 622 | |
| 623 | if (!shouldShowVideoUi && isVideoMode) { |
| 624 | // Terminate video mode if new primary call is not a video call |
| 625 | // and we are currently in video mode. |
| 626 | LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "exiting video mode..."); |
| 627 | exitVideoMode(); |
| 628 | } else if (shouldShowVideoUi) { |
| 629 | LogUtil.i("VideoCallPresenter.onPrimaryCallChanged", "entering video mode..."); |
| 630 | |
| 631 | updateCameraSelection(newPrimaryCall); |
| 632 | adjustVideoMode(newPrimaryCall); |
| 633 | } |
| 634 | checkForOrientationAllowedChange(newPrimaryCall); |
| 635 | } |
| 636 | |
| 637 | private boolean isVideoMode() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 638 | return isVideoMode; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | private void updateCallCache(DialerCall call) { |
| 642 | if (call == null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 643 | currentVideoState = VideoProfile.STATE_AUDIO_ONLY; |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 644 | currentCallState = DialerCallState.INVALID; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 645 | videoCall = null; |
| 646 | primaryCall = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 647 | } else { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 648 | currentVideoState = call.getVideoState(); |
| 649 | videoCall = call.getVideoCall(); |
| 650 | currentCallState = call.getState(); |
| 651 | primaryCall = call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Handles changes to the details of the call. The {@link VideoCallPresenter} is interested in |
| 657 | * changes to the video state. |
| 658 | * |
| 659 | * @param call The call for which the details changed. |
| 660 | * @param details The new call details. |
| 661 | */ |
| 662 | @Override |
| 663 | public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) { |
| 664 | LogUtil.v( |
| 665 | "VideoCallPresenter.onDetailsChanged", |
| 666 | "call: %s, details: %s, mPrimaryCall: %s", |
| 667 | call, |
| 668 | details, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 669 | primaryCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 670 | if (call == null) { |
| 671 | return; |
| 672 | } |
| 673 | // If the details change is not for the currently active call no update is required. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 674 | if (!call.equals(primaryCall)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 675 | LogUtil.v("VideoCallPresenter.onDetailsChanged", "details not for current active call"); |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | updateVideoCall(call); |
| 680 | |
| 681 | updateCallCache(call); |
| 682 | } |
| 683 | |
| 684 | private void updateVideoCall(DialerCall call) { |
| 685 | checkForVideoCallChange(call); |
| 686 | checkForVideoStateChange(call); |
| 687 | checkForCallStateChange(call); |
| 688 | checkForOrientationAllowedChange(call); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 689 | updateFullscreenAndGreenScreenMode( |
| 690 | call.getState(), call.getVideoTech().getSessionModificationState()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | private void checkForOrientationAllowedChange(@Nullable DialerCall call) { |
wangqi | 4a22adb | 2018-06-18 13:18:06 -0700 | [diff] [blame^] | 694 | // Call could be null when video call ended. This check could prevent unwanted orientation |
| 695 | // change before incall UI gets destroyed. |
| 696 | if (call != null) { |
| 697 | InCallPresenter.getInstance() |
| 698 | .setInCallAllowsOrientationChange(isVideoCall(call) || isVideoUpgrade(call)); |
| 699 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | private void updateFullscreenAndGreenScreenMode( |
| 703 | int callState, @SessionModificationState int sessionModificationState) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 704 | if (videoCallScreen != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 705 | boolean shouldShowFullscreen = InCallPresenter.getInstance().isFullscreen(); |
| 706 | boolean shouldShowGreenScreen = |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 707 | callState == DialerCallState.DIALING |
| 708 | || callState == DialerCallState.CONNECTING |
| 709 | || callState == DialerCallState.INCOMING |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 710 | || isVideoUpgrade(sessionModificationState); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 711 | videoCallScreen.updateFullscreenAndGreenScreenMode( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 712 | shouldShowFullscreen, shouldShowGreenScreen); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /** Checks for a change to the video call and changes it if required. */ |
| 717 | private void checkForVideoCallChange(DialerCall call) { |
| 718 | final VideoCall videoCall = call.getVideoCall(); |
| 719 | LogUtil.v( |
| 720 | "VideoCallPresenter.checkForVideoCallChange", |
| 721 | "videoCall: %s, mVideoCall: %s", |
| 722 | videoCall, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 723 | this.videoCall); |
| 724 | if (!Objects.equals(videoCall, this.videoCall)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 725 | changeVideoCall(call); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Handles a change to the video call. Sets the surfaces on the previous call to null and sets the |
| 731 | * surfaces on the new video call accordingly. |
| 732 | * |
| 733 | * @param call The new video call. |
| 734 | */ |
| 735 | private void changeVideoCall(DialerCall call) { |
| 736 | final VideoCall videoCall = call == null ? null : call.getVideoCall(); |
| 737 | LogUtil.i( |
| 738 | "VideoCallPresenter.changeVideoCall", |
| 739 | "videoCall: %s, mVideoCall: %s", |
| 740 | videoCall, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 741 | this.videoCall); |
| 742 | final boolean hasChanged = this.videoCall == null && videoCall != null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 743 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 744 | this.videoCall = videoCall; |
| 745 | if (this.videoCall == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 746 | LogUtil.v("VideoCallPresenter.changeVideoCall", "video call or primary call is null. Return"); |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | if (shouldShowVideoUiForCall(call) && hasChanged) { |
| 751 | adjustVideoMode(call); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | private boolean isCameraRequired() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 756 | return primaryCall != null |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 757 | && isCameraRequired( |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 758 | primaryCall.getVideoState(), primaryCall.getVideoTech().getSessionModificationState()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Adjusts the current video mode by setting up the preview and display surfaces as necessary. |
| 763 | * Expected to be called whenever the video state associated with a call changes (e.g. a user |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 764 | * turns their camera on or off) to ensure the correct surfaces are shown/hidden. TODO(vt): Need |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 765 | * to adjust size and orientation of preview surface here. |
| 766 | */ |
| 767 | private void adjustVideoMode(DialerCall call) { |
| 768 | VideoCall videoCall = call.getVideoCall(); |
| 769 | int newVideoState = call.getVideoState(); |
| 770 | |
| 771 | LogUtil.i( |
| 772 | "VideoCallPresenter.adjustVideoMode", |
| 773 | "videoCall: %s, videoState: %d", |
| 774 | videoCall, |
| 775 | newVideoState); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 776 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 777 | LogUtil.e("VideoCallPresenter.adjustVideoMode", "error VideoCallScreen is null so returning"); |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | showVideoUi( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 782 | newVideoState, |
| 783 | call.getState(), |
| 784 | call.getVideoTech().getSessionModificationState(), |
| 785 | call.isRemotelyHeld()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 786 | |
| 787 | // Communicate the current camera to telephony and make a request for the camera |
| 788 | // capabilities. |
| 789 | if (videoCall != null) { |
| 790 | Surface surface = getRemoteVideoSurfaceTexture().getSavedSurface(); |
| 791 | if (surface != null) { |
| 792 | LogUtil.v( |
| 793 | "VideoCallPresenter.adjustVideoMode", "calling setDisplaySurface with: " + surface); |
| 794 | videoCall.setDisplaySurface(surface); |
| 795 | } |
| 796 | |
| 797 | Assert.checkState( |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 798 | deviceOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN); |
| 799 | videoCall.setDeviceOrientation(deviceOrientation); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 800 | enableCamera( |
roldenburg | ca47547 | 2017-10-25 13:00:42 -0700 | [diff] [blame] | 801 | call, isCameraRequired(newVideoState, call.getVideoTech().getSessionModificationState())); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 802 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 803 | int previousVideoState = currentVideoState; |
| 804 | currentVideoState = newVideoState; |
| 805 | isVideoMode = true; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 806 | |
| 807 | // adjustVideoMode may be called if we are already in a 1-way video state. In this case |
| 808 | // we do not want to trigger auto-fullscreen mode. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 809 | if (!isVideoCall(previousVideoState) && isVideoCall(newVideoState)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 810 | maybeAutoEnterFullscreen(call); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | private static boolean shouldShowVideoUiForCall(@Nullable DialerCall call) { |
| 815 | if (call == null) { |
| 816 | return false; |
| 817 | } |
| 818 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 819 | if (isVideoCall(call)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 820 | return true; |
| 821 | } |
| 822 | |
| 823 | if (isVideoUpgrade(call)) { |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | return false; |
| 828 | } |
| 829 | |
roldenburg | ca47547 | 2017-10-25 13:00:42 -0700 | [diff] [blame] | 830 | private void enableCamera(DialerCall call, boolean isCameraRequired) { |
| 831 | LogUtil.v("VideoCallPresenter.enableCamera", "call: %s, enabling: %b", call, isCameraRequired); |
| 832 | if (call == null) { |
| 833 | LogUtil.i("VideoCallPresenter.enableCamera", "call is null"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 834 | return; |
| 835 | } |
| 836 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 837 | boolean hasCameraPermission = VideoUtils.hasCameraPermissionAndShownPrivacyToast(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 838 | if (!hasCameraPermission) { |
roldenburg | ca47547 | 2017-10-25 13:00:42 -0700 | [diff] [blame] | 839 | call.getVideoTech().setCamera(null); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 840 | previewSurfaceState = PreviewSurfaceState.NONE; |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 841 | // TODO(wangqi): Inform remote party that the video is off. This is similar to a bug. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 842 | } else if (isCameraRequired) { |
| 843 | InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager(); |
roldenburg | ca47547 | 2017-10-25 13:00:42 -0700 | [diff] [blame] | 844 | call.getVideoTech().setCamera(cameraManager.getActiveCameraId()); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 845 | previewSurfaceState = PreviewSurfaceState.CAMERA_SET; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 846 | } else { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 847 | previewSurfaceState = PreviewSurfaceState.NONE; |
roldenburg | ca47547 | 2017-10-25 13:00:42 -0700 | [diff] [blame] | 848 | call.getVideoTech().setCamera(null); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | /** Exits video mode by hiding the video surfaces and making other adjustments (eg. audio). */ |
| 853 | private void exitVideoMode() { |
| 854 | LogUtil.i("VideoCallPresenter.exitVideoMode", ""); |
| 855 | |
| 856 | showVideoUi( |
| 857 | VideoProfile.STATE_AUDIO_ONLY, |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 858 | DialerCallState.ACTIVE, |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 859 | SessionModificationState.NO_REQUEST, |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 860 | false /* isRemotelyHeld */); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 861 | enableCamera(primaryCall, false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 862 | InCallPresenter.getInstance().setFullScreen(false); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 863 | InCallPresenter.getInstance().enableScreenTimeout(false); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 864 | isVideoMode = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Based on the current video state and call state, show or hide the incoming and outgoing video |
| 869 | * surfaces. The outgoing video surface is shown any time video is transmitting. The incoming |
| 870 | * video surface is shown whenever the video is un-paused and active. |
| 871 | * |
| 872 | * @param videoState The video state. |
| 873 | * @param callState The call state. |
| 874 | */ |
| 875 | private void showVideoUi( |
| 876 | int videoState, |
| 877 | int callState, |
| 878 | @SessionModificationState int sessionModificationState, |
| 879 | boolean isRemotelyHeld) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 880 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 881 | LogUtil.e("VideoCallPresenter.showVideoUi", "videoCallScreen is null returning"); |
| 882 | return; |
| 883 | } |
| 884 | boolean showIncomingVideo = showIncomingVideo(videoState, callState); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 885 | boolean showOutgoingVideo = showOutgoingVideo(context, videoState, sessionModificationState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 886 | LogUtil.i( |
| 887 | "VideoCallPresenter.showVideoUi", |
| 888 | "showIncoming: %b, showOutgoing: %b, isRemotelyHeld: %b", |
| 889 | showIncomingVideo, |
| 890 | showOutgoingVideo, |
| 891 | isRemotelyHeld); |
| 892 | updateRemoteVideoSurfaceDimensions(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 893 | videoCallScreen.showVideoViews(showOutgoingVideo, showIncomingVideo, isRemotelyHeld); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 894 | |
| 895 | InCallPresenter.getInstance().enableScreenTimeout(VideoProfile.isAudioOnly(videoState)); |
| 896 | updateFullscreenAndGreenScreenMode(callState, sessionModificationState); |
| 897 | } |
| 898 | |
| 899 | /** |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 900 | * Handles peer video dimension changes. |
| 901 | * |
| 902 | * @param call The call which experienced a peer video dimension change. |
| 903 | * @param width The new peer video width . |
| 904 | * @param height The new peer video height. |
| 905 | */ |
| 906 | @Override |
| 907 | public void onUpdatePeerDimensions(DialerCall call, int width, int height) { |
| 908 | LogUtil.i("VideoCallPresenter.onUpdatePeerDimensions", "width: %d, height: %d", width, height); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 909 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 910 | LogUtil.e("VideoCallPresenter.onUpdatePeerDimensions", "videoCallScreen is null"); |
| 911 | return; |
| 912 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 913 | if (!call.equals(primaryCall)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 914 | LogUtil.e( |
| 915 | "VideoCallPresenter.onUpdatePeerDimensions", "current call is not equal to primary"); |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | // Change size of display surface to match the peer aspect ratio |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 920 | if (width > 0 && height > 0 && videoCallScreen != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 921 | getRemoteVideoSurfaceTexture().setSourceVideoDimensions(new Point(width, height)); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 922 | videoCallScreen.onRemoteVideoDimensionsChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 927 | * Handles a change to the dimensions of the local camera. Receiving the camera capabilities |
| 928 | * triggers the creation of the video |
| 929 | * |
| 930 | * @param call The call which experienced the camera dimension change. |
| 931 | * @param width The new camera video width. |
| 932 | * @param height The new camera video height. |
| 933 | */ |
| 934 | @Override |
| 935 | public void onCameraDimensionsChange(DialerCall call, int width, int height) { |
| 936 | LogUtil.i( |
| 937 | "VideoCallPresenter.onCameraDimensionsChange", |
| 938 | "call: %s, width: %d, height: %d", |
| 939 | call, |
| 940 | width, |
| 941 | height); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 942 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 943 | LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "ui is null"); |
| 944 | return; |
| 945 | } |
| 946 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 947 | if (!call.equals(primaryCall)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 948 | LogUtil.e("VideoCallPresenter.onCameraDimensionsChange", "not the primary call"); |
| 949 | return; |
| 950 | } |
| 951 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 952 | previewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 953 | changePreviewDimensions(width, height); |
| 954 | |
| 955 | // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}. |
| 956 | // If it not yet ready, it will be set when when creation completes. |
| 957 | Surface surface = getLocalVideoSurfaceTexture().getSavedSurface(); |
| 958 | if (surface != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 959 | previewSurfaceState = PreviewSurfaceState.SURFACE_SET; |
| 960 | videoCall.setPreviewSurface(surface); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Changes the dimensions of the preview surface. |
| 966 | * |
| 967 | * @param width The new width. |
| 968 | * @param height The new height. |
| 969 | */ |
| 970 | private void changePreviewDimensions(int width, int height) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 971 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | |
| 975 | // Resize the surface used to display the preview video |
| 976 | getLocalVideoSurfaceTexture().setSurfaceDimensions(new Point(width, height)); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 977 | videoCallScreen.onLocalVideoDimensionsChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /** |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 981 | * Handles changes to the device orientation. |
| 982 | * |
| 983 | * @param orientation The screen orientation of the device (one of: {@link |
| 984 | * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link |
| 985 | * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link |
| 986 | * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link |
| 987 | * InCallOrientationEventListener#SCREEN_ORIENTATION_270}). |
| 988 | */ |
| 989 | @Override |
| 990 | public void onDeviceOrientationChanged(int orientation) { |
| 991 | LogUtil.i( |
| 992 | "VideoCallPresenter.onDeviceOrientationChanged", |
| 993 | "orientation: %d -> %d", |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 994 | deviceOrientation, |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 995 | orientation); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 996 | deviceOrientation = orientation; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 997 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 998 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 999 | LogUtil.e("VideoCallPresenter.onDeviceOrientationChanged", "videoCallScreen is null"); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | Point previewDimensions = getLocalVideoSurfaceTexture().getSurfaceDimensions(); |
| 1004 | if (previewDimensions == null) { |
| 1005 | return; |
| 1006 | } |
| 1007 | LogUtil.v( |
| 1008 | "VideoCallPresenter.onDeviceOrientationChanged", |
| 1009 | "orientation: %d, size: %s", |
| 1010 | orientation, |
| 1011 | previewDimensions); |
| 1012 | changePreviewDimensions(previewDimensions.x, previewDimensions.y); |
| 1013 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1014 | videoCallScreen.onLocalVideoOrientationChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * Exits fullscreen mode if the current call context has changed to a non-video call. |
| 1019 | * |
| 1020 | * @param call The call. |
| 1021 | */ |
| 1022 | protected void maybeExitFullscreen(DialerCall call) { |
| 1023 | if (call == null) { |
| 1024 | return; |
| 1025 | } |
| 1026 | |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 1027 | if (!isVideoCall(call) || call.getState() == DialerCallState.INCOMING) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1028 | LogUtil.i("VideoCallPresenter.maybeExitFullscreen", "exiting fullscreen"); |
| 1029 | InCallPresenter.getInstance().setFullScreen(false); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * Schedules auto-entering of fullscreen mode. Will not enter full screen mode if any of the |
| 1035 | * following conditions are met: 1. No call 2. DialerCall is not active 3. The current video state |
| 1036 | * is not bi-directional. 4. Already in fullscreen mode 5. In accessibility mode |
| 1037 | * |
| 1038 | * @param call The current call. |
| 1039 | */ |
| 1040 | protected void maybeAutoEnterFullscreen(DialerCall call) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1041 | if (!isAutoFullscreenEnabled) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | if (call == null |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 1046 | || call.getState() != DialerCallState.ACTIVE |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1047 | || !isBidirectionalVideoCall(call) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1048 | || InCallPresenter.getInstance().isFullscreen() |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1049 | || (context != null && AccessibilityUtil.isTouchExplorationEnabled(context))) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1050 | // Ensure any previously scheduled attempt to enter fullscreen is cancelled. |
| 1051 | cancelAutoFullScreen(); |
| 1052 | return; |
| 1053 | } |
| 1054 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1055 | if (autoFullScreenPending) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1056 | LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "already pending."); |
| 1057 | return; |
| 1058 | } |
| 1059 | LogUtil.v("VideoCallPresenter.maybeAutoEnterFullscreen", "scheduled"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1060 | autoFullScreenPending = true; |
| 1061 | handler.removeCallbacks(autoFullscreenRunnable); |
| 1062 | handler.postDelayed(autoFullscreenRunnable, autoFullscreenTimeoutMillis); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** Cancels pending auto fullscreen mode. */ |
| 1066 | @Override |
| 1067 | public void cancelAutoFullScreen() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1068 | if (!autoFullScreenPending) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1069 | LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "none pending."); |
| 1070 | return; |
| 1071 | } |
| 1072 | LogUtil.v("VideoCallPresenter.cancelAutoFullScreen", "cancelling pending"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1073 | autoFullScreenPending = false; |
| 1074 | handler.removeCallbacks(autoFullscreenRunnable); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1077 | @Override |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1078 | public boolean shouldShowCameraPermissionToast() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1079 | if (primaryCall == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1080 | LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "null call"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1081 | return false; |
| 1082 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1083 | if (primaryCall.didShowCameraPermission()) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1084 | LogUtil.i( |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1085 | "VideoCallPresenter.shouldShowCameraPermissionToast", "already shown for this call"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1086 | return false; |
| 1087 | } |
zachh | 190343a | 2018-05-31 17:30:46 -0700 | [diff] [blame] | 1088 | if (!ConfigProviderComponent.get(context) |
| 1089 | .getConfigProvider() |
| 1090 | .getBoolean("camera_permission_dialog_allowed", true)) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1091 | LogUtil.i("VideoCallPresenter.shouldShowCameraPermissionToast", "disabled by config"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1094 | return !VideoUtils.hasCameraPermission(context) |
| 1095 | || !PermissionsUtil.hasCameraPrivacyToastShown(context); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | @Override |
| 1099 | public void onCameraPermissionDialogShown() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1100 | if (primaryCall != null) { |
| 1101 | primaryCall.setDidShowCameraPermission(true); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1105 | private void updateRemoteVideoSurfaceDimensions() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1106 | Activity activity = videoCallScreen.getVideoCallScreenFragment().getActivity(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1107 | if (activity != null) { |
| 1108 | Point screenSize = new Point(); |
| 1109 | activity.getWindowManager().getDefaultDisplay().getSize(screenSize); |
| 1110 | getRemoteVideoSurfaceTexture().setSurfaceDimensions(screenSize); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | private static boolean isVideoUpgrade(DialerCall call) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1115 | return call != null |
| 1116 | && (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | private static boolean isVideoUpgrade(@SessionModificationState int state) { |
| 1120 | return VideoUtils.hasSentVideoUpgradeRequest(state) |
| 1121 | || VideoUtils.hasReceivedVideoUpgradeRequest(state); |
| 1122 | } |
| 1123 | |
wangqi | da410d3 | 2018-03-06 16:51:38 -0800 | [diff] [blame] | 1124 | @Override |
| 1125 | public void onIncomingCall(DialerCall call) {} |
| 1126 | |
| 1127 | @Override |
| 1128 | public void onUpgradeToVideo(DialerCall call) {} |
| 1129 | |
| 1130 | @Override |
| 1131 | public void onSessionModificationStateChange(DialerCall call) {} |
| 1132 | |
| 1133 | @Override |
| 1134 | public void onCallListChange(CallList callList) {} |
| 1135 | |
| 1136 | @Override |
| 1137 | public void onDisconnect(DialerCall call) {} |
| 1138 | |
| 1139 | @Override |
| 1140 | public void onWiFiToLteHandover(DialerCall call) { |
| 1141 | if (call.isVideoCall() || call.hasSentVideoUpgradeRequest()) { |
| 1142 | videoCallScreen.onHandoverFromWiFiToLte(); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | @Override |
| 1147 | public void onHandoverToWifiFailed(DialerCall call) {} |
| 1148 | |
| 1149 | @Override |
| 1150 | public void onInternationalCallOnWifi(@NonNull DialerCall call) {} |
| 1151 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1152 | private class LocalDelegate implements VideoSurfaceDelegate { |
| 1153 | @Override |
| 1154 | public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1155 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1156 | LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no UI"); |
| 1157 | return; |
| 1158 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1159 | if (videoCall == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1160 | LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceCreated", "no video call"); |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | // If the preview surface has just been created and we have already received camera |
| 1165 | // capabilities, but not yet set the surface, we will set the surface now. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1166 | if (previewSurfaceState == PreviewSurfaceState.CAPABILITIES_RECEIVED) { |
| 1167 | previewSurfaceState = PreviewSurfaceState.SURFACE_SET; |
| 1168 | videoCall.setPreviewSurface(videoCallSurface.getSavedSurface()); |
| 1169 | } else if (previewSurfaceState == PreviewSurfaceState.NONE && isCameraRequired()) { |
| 1170 | enableCamera(primaryCall, true); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | @Override |
| 1175 | public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1176 | if (videoCall == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1177 | LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceReleased", "no video call"); |
| 1178 | return; |
| 1179 | } |
| 1180 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1181 | videoCall.setPreviewSurface(null); |
| 1182 | enableCamera(primaryCall, false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | @Override |
| 1186 | public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1187 | if (videoCall == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1188 | LogUtil.e("VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", "no video call"); |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | boolean isChangingConfigurations = InCallPresenter.getInstance().isChangingConfigurations(); |
| 1193 | if (!isChangingConfigurations) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1194 | enableCamera(primaryCall, false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1195 | } else { |
| 1196 | LogUtil.i( |
| 1197 | "VideoCallPresenter.LocalDelegate.onSurfaceDestroyed", |
| 1198 | "activity is being destroyed due to configuration changes. Not closing the camera."); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | @Override |
| 1203 | public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) { |
| 1204 | VideoCallPresenter.this.onSurfaceClick(); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | private class RemoteDelegate implements VideoSurfaceDelegate { |
| 1209 | @Override |
| 1210 | public void onSurfaceCreated(VideoSurfaceTexture videoCallSurface) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1211 | if (videoCallScreen == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1212 | LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no UI"); |
| 1213 | return; |
| 1214 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1215 | if (videoCall == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1216 | LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceCreated", "no video call"); |
| 1217 | return; |
| 1218 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1219 | videoCall.setDisplaySurface(videoCallSurface.getSavedSurface()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | @Override |
| 1223 | public void onSurfaceReleased(VideoSurfaceTexture videoCallSurface) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1224 | if (videoCall == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1225 | LogUtil.e("VideoCallPresenter.RemoteDelegate.onSurfaceReleased", "no video call"); |
| 1226 | return; |
| 1227 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1228 | videoCall.setDisplaySurface(null); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | @Override |
| 1232 | public void onSurfaceDestroyed(VideoSurfaceTexture videoCallSurface) {} |
| 1233 | |
| 1234 | @Override |
| 1235 | public void onSurfaceClick(VideoSurfaceTexture videoCallSurface) { |
| 1236 | VideoCallPresenter.this.onSurfaceClick(); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | /** Defines the state of the preview surface negotiation with the telephony layer. */ |
| 1241 | private static class PreviewSurfaceState { |
| 1242 | |
| 1243 | /** |
| 1244 | * The camera has not yet been set on the {@link VideoCall}; negotiation has not yet started. |
| 1245 | */ |
| 1246 | private static final int NONE = 0; |
| 1247 | |
| 1248 | /** |
| 1249 | * The camera has been set on the {@link VideoCall}, but camera capabilities have not yet been |
| 1250 | * received. |
| 1251 | */ |
| 1252 | private static final int CAMERA_SET = 1; |
| 1253 | |
| 1254 | /** |
| 1255 | * The camera capabilties have been received from telephony, but the surface has not yet been |
| 1256 | * set on the {@link VideoCall}. |
| 1257 | */ |
| 1258 | private static final int CAPABILITIES_RECEIVED = 2; |
| 1259 | |
| 1260 | /** The surface has been set on the {@link VideoCall}. */ |
| 1261 | private static final int SURFACE_SET = 3; |
| 1262 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1263 | |
| 1264 | private static boolean isBidirectionalVideoCall(DialerCall call) { |
linyuh | 122fb0b | 2018-03-26 13:35:32 -0700 | [diff] [blame] | 1265 | return VideoProfile.isBidirectional(call.getVideoState()); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | private static boolean isIncomingVideoCall(DialerCall call) { |
| 1269 | if (!isVideoCall(call)) { |
| 1270 | return false; |
| 1271 | } |
| 1272 | final int state = call.getState(); |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 1273 | return (state == DialerCallState.INCOMING) || (state == DialerCallState.CALL_WAITING); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | private static boolean isActiveVideoCall(DialerCall call) { |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 1277 | return isVideoCall(call) && call.getState() == DialerCallState.ACTIVE; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | private static boolean isOutgoingVideoCall(DialerCall call) { |
| 1281 | if (!isVideoCall(call)) { |
| 1282 | return false; |
| 1283 | } |
| 1284 | final int state = call.getState(); |
wangqi | bb94ca6 | 2018-04-27 14:34:04 -0700 | [diff] [blame] | 1285 | return DialerCallState.isDialing(state) |
| 1286 | || state == DialerCallState.CONNECTING |
| 1287 | || state == DialerCallState.SELECT_PHONE_ACCOUNT; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | private static boolean isAudioCall(DialerCall call) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1291 | return call != null && VideoProfile.isAudioOnly(call.getVideoState()); |
| 1292 | } |
| 1293 | |
| 1294 | private static boolean isVideoCall(@Nullable DialerCall call) { |
| 1295 | return call != null && call.isVideoCall(); |
| 1296 | } |
| 1297 | |
| 1298 | private static boolean isVideoCall(int videoState) { |
linyuh | 122fb0b | 2018-03-26 13:35:32 -0700 | [diff] [blame] | 1299 | return VideoProfile.isTransmissionEnabled(videoState) |
| 1300 | || VideoProfile.isReceptionEnabled(videoState); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1301 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1302 | } |