1 /*
2  * Copyright (C) 2016 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.answer.impl;
18 
19 import android.content.res.Configuration;
20 import android.graphics.Point;
21 import android.support.annotation.NonNull;
22 import android.support.v4.app.Fragment;
23 import android.view.TextureView;
24 import android.view.View;
25 import com.android.dialer.common.Assert;
26 import com.android.dialer.common.FragmentUtils;
27 import com.android.dialer.common.LogUtil;
28 import com.android.incallui.video.protocol.VideoCallScreen;
29 import com.android.incallui.video.protocol.VideoCallScreenDelegate;
30 import com.android.incallui.video.protocol.VideoCallScreenDelegateFactory;
31 import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
32 
33 /** Shows a video preview for an incoming call. */
34 public class AnswerVideoCallScreen implements VideoCallScreen {
35   @NonNull private final String callId;
36   @NonNull private final Fragment fragment;
37   @NonNull private final TextureView textureView;
38   @NonNull private final VideoCallScreenDelegate delegate;
39 
AnswerVideoCallScreen( @onNull String callId, @NonNull Fragment fragment, @NonNull View view)40   public AnswerVideoCallScreen(
41       @NonNull String callId, @NonNull Fragment fragment, @NonNull View view) {
42     this.callId = Assert.isNotNull(callId);
43     this.fragment = Assert.isNotNull(fragment);
44 
45     textureView =
46         Assert.isNotNull((TextureView) view.findViewById(R.id.incoming_preview_texture_view));
47     View overlayView =
48         Assert.isNotNull(view.findViewById(R.id.incoming_preview_texture_view_overlay));
49     view.setBackgroundColor(0xff000000);
50     delegate =
51         FragmentUtils.getParentUnsafe(fragment, VideoCallScreenDelegateFactory.class)
52             .newVideoCallScreenDelegate(this);
53     delegate.initVideoCallScreenDelegate(fragment.getContext(), this);
54 
55     textureView.setVisibility(View.VISIBLE);
56     overlayView.setVisibility(View.VISIBLE);
57   }
58 
59   @Override
onVideoScreenStart()60   public void onVideoScreenStart() {
61     LogUtil.i("AnswerVideoCallScreen.onStart", null);
62     delegate.onVideoCallScreenUiReady();
63     delegate.getLocalVideoSurfaceTexture().attachToTextureView(textureView);
64   }
65 
66   @Override
onVideoScreenStop()67   public void onVideoScreenStop() {
68     LogUtil.i("AnswerVideoCallScreen.onStop", null);
69     delegate.onVideoCallScreenUiUnready();
70   }
71 
72   @Override
showVideoViews( boolean shouldShowPreview, boolean shouldShowRemote, boolean isRemotelyHeld)73   public void showVideoViews(
74       boolean shouldShowPreview, boolean shouldShowRemote, boolean isRemotelyHeld) {
75     LogUtil.i(
76         "AnswerVideoCallScreen.showVideoViews",
77         "showPreview: %b, shouldShowRemote: %b",
78         shouldShowPreview,
79         shouldShowRemote);
80   }
81 
82   @Override
onLocalVideoDimensionsChanged()83   public void onLocalVideoDimensionsChanged() {
84     LogUtil.i("AnswerVideoCallScreen.onLocalVideoDimensionsChanged", null);
85     updatePreviewVideoScaling();
86   }
87 
88   @Override
onRemoteVideoDimensionsChanged()89   public void onRemoteVideoDimensionsChanged() {}
90 
91   @Override
onLocalVideoOrientationChanged()92   public void onLocalVideoOrientationChanged() {
93     LogUtil.i("AnswerVideoCallScreen.onLocalVideoOrientationChanged", null);
94     updatePreviewVideoScaling();
95   }
96 
97   @Override
updateFullscreenAndGreenScreenMode( boolean shouldShowFullscreen, boolean shouldShowGreenScreen)98   public void updateFullscreenAndGreenScreenMode(
99       boolean shouldShowFullscreen, boolean shouldShowGreenScreen) {}
100 
101   @Override
getVideoCallScreenFragment()102   public Fragment getVideoCallScreenFragment() {
103     return fragment;
104   }
105 
106   @NonNull
107   @Override
getCallId()108   public String getCallId() {
109     return callId;
110   }
111 
112   @Override
onHandoverFromWiFiToLte()113   public void onHandoverFromWiFiToLte() {}
114 
updatePreviewVideoScaling()115   private void updatePreviewVideoScaling() {
116     if (textureView.getWidth() == 0 || textureView.getHeight() == 0) {
117       LogUtil.i(
118           "AnswerVideoCallScreen.updatePreviewVideoScaling", "view layout hasn't finished yet");
119       return;
120     }
121     Point cameraDimensions = delegate.getLocalVideoSurfaceTexture().getSurfaceDimensions();
122     if (cameraDimensions == null) {
123       LogUtil.i("AnswerVideoCallScreen.updatePreviewVideoScaling", "camera dimensions not set");
124       return;
125     }
126     if (isLandscape()) {
127       VideoSurfaceBindings.scaleVideoAndFillView(
128           textureView, cameraDimensions.x, cameraDimensions.y, delegate.getDeviceOrientation());
129     } else {
130       // Landscape, so dimensions are swapped
131       //noinspection SuspiciousNameCombination
132       VideoSurfaceBindings.scaleVideoAndFillView(
133           textureView, cameraDimensions.y, cameraDimensions.x, delegate.getDeviceOrientation());
134     }
135   }
136 
isLandscape()137   private boolean isLandscape() {
138     return fragment.getResources().getConfiguration().orientation
139         == Configuration.ORIENTATION_LANDSCAPE;
140   }
141 }
142