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.dialer.callcomposer.camera;
18 
19 import android.content.Context;
20 import android.graphics.SurfaceTexture;
21 import android.hardware.Camera;
22 import android.os.Parcelable;
23 import android.util.AttributeSet;
24 import android.view.TextureView;
25 import android.view.View;
26 import java.io.IOException;
27 
28 /**
29  * A hardware accelerated preview texture for the camera. This is the preferred CameraPreview
30  * because it animates smoother. When hardware acceleration isn't available, SoftwareCameraPreview
31  * is used.
32  *
33  * <p>There is a significant amount of duplication between HardwareCameraPreview and
34  * SoftwareCameraPreview which we can't easily share due to a lack of multiple inheritance, The
35  * implementations of the shared methods are delegated to CameraPreview
36  */
37 public class HardwareCameraPreview extends TextureView implements CameraPreview.CameraPreviewHost {
38   private CameraPreview preview;
39 
HardwareCameraPreview(final Context context, final AttributeSet attrs)40   public HardwareCameraPreview(final Context context, final AttributeSet attrs) {
41     super(context, attrs);
42     preview = new CameraPreview(this);
43     setSurfaceTextureListener(
44         new SurfaceTextureListener() {
45           @Override
46           public void onSurfaceTextureAvailable(
47               final SurfaceTexture surfaceTexture, final int i, final int i2) {
48             CameraManager.get().setSurface(preview);
49           }
50 
51           @Override
52           public void onSurfaceTextureSizeChanged(
53               final SurfaceTexture surfaceTexture, final int i, final int i2) {
54             CameraManager.get().setSurface(preview);
55           }
56 
57           @Override
58           public boolean onSurfaceTextureDestroyed(final SurfaceTexture surfaceTexture) {
59             CameraManager.get().setSurface(null);
60             return true;
61           }
62 
63           @Override
64           public void onSurfaceTextureUpdated(final SurfaceTexture surfaceTexture) {
65             CameraManager.get().setSurface(preview);
66           }
67         });
68   }
69 
70   @Override
setShown()71   public void setShown() {
72     preview.setShown();
73   }
74 
75   @Override
onVisibilityChanged(final View changedView, final int visibility)76   protected void onVisibilityChanged(final View changedView, final int visibility) {
77     super.onVisibilityChanged(changedView, visibility);
78     preview.onVisibilityChanged(visibility);
79   }
80 
81   @Override
onDetachedFromWindow()82   protected void onDetachedFromWindow() {
83     super.onDetachedFromWindow();
84     preview.onDetachedFromWindow();
85   }
86 
87   @Override
onAttachedToWindow()88   protected void onAttachedToWindow() {
89     super.onAttachedToWindow();
90     preview.onAttachedToWindow();
91   }
92 
93   @Override
onRestoreInstanceState(final Parcelable state)94   protected void onRestoreInstanceState(final Parcelable state) {
95     super.onRestoreInstanceState(state);
96     preview.onRestoreInstanceState();
97   }
98 
99   @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)100   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
101     widthMeasureSpec = preview.getWidthMeasureSpec(widthMeasureSpec, heightMeasureSpec);
102     heightMeasureSpec = preview.getHeightMeasureSpec(widthMeasureSpec, heightMeasureSpec);
103     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
104   }
105 
106   @Override
getView()107   public View getView() {
108     return this;
109   }
110 
111   @Override
isValid()112   public boolean isValid() {
113     return getSurfaceTexture() != null;
114   }
115 
116   @Override
startPreview(final Camera camera)117   public void startPreview(final Camera camera) throws IOException {
118     camera.setPreviewTexture(getSurfaceTexture());
119   }
120 
121   @Override
onCameraPermissionGranted()122   public void onCameraPermissionGranted() {
123     preview.onCameraPermissionGranted();
124   }
125 }
126