1 /*
2  * Copyright (C) 2015 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.messaging.ui.mediapicker;
18 
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.hardware.Camera;
22 import android.view.View;
23 import android.view.View.MeasureSpec;
24 import com.android.messaging.util.Assert;
25 
26 import java.io.IOException;
27 
28 /**
29  * Contains shared code for SoftwareCameraPreview and HardwareCameraPreview, cannot use inheritance
30  * because those classes must inherit from separate Views, so those classes delegate calls to this
31  * helper class.  Specifics for each implementation are in CameraPreviewHost
32  */
33 public class CameraPreview {
34     public interface CameraPreviewHost {
getView()35         View getView();
isValid()36         boolean isValid();
startPreview(final Camera camera)37         void startPreview(final Camera camera) throws IOException;
onCameraPermissionGranted()38         void onCameraPermissionGranted();
39 
40     }
41 
42     private int mCameraWidth = -1;
43     private int mCameraHeight = -1;
44 
45     private final CameraPreviewHost mHost;
46 
CameraPreview(final CameraPreviewHost host)47     public CameraPreview(final CameraPreviewHost host) {
48         Assert.notNull(host);
49         Assert.notNull(host.getView());
50         mHost = host;
51     }
52 
setSize(final Camera.Size size, final int orientation)53     public void setSize(final Camera.Size size, final int orientation) {
54         switch (orientation) {
55             case 0:
56             case 180:
57                 mCameraWidth = size.width;
58                 mCameraHeight = size.height;
59                 break;
60             case 90:
61             case 270:
62             default:
63                 mCameraWidth = size.height;
64                 mCameraHeight = size.width;
65         }
66         mHost.getView().requestLayout();
67     }
68 
getWidthMeasureSpec(final int widthMeasureSpec, final int heightMeasureSpec)69     public int getWidthMeasureSpec(final int widthMeasureSpec, final int heightMeasureSpec) {
70         if (mCameraHeight >= 0) {
71             final int width = View.MeasureSpec.getSize(widthMeasureSpec);
72             return MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
73         } else {
74             return widthMeasureSpec;
75         }
76     }
77 
getHeightMeasureSpec(final int widthMeasureSpec, final int heightMeasureSpec)78     public int getHeightMeasureSpec(final int widthMeasureSpec, final int heightMeasureSpec) {
79         if (mCameraHeight >= 0) {
80             final int orientation = getContext().getResources().getConfiguration().orientation;
81             final int width = View.MeasureSpec.getSize(widthMeasureSpec);
82             final float aspectRatio = (float) mCameraWidth / (float) mCameraHeight;
83             int height;
84             if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
85                 height = (int) (width * aspectRatio);
86             } else {
87                 height = (int) (width / aspectRatio);
88             }
89             return View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
90         } else {
91             return heightMeasureSpec;
92         }
93     }
94 
onVisibilityChanged(final int visibility)95     public void onVisibilityChanged(final int visibility) {
96         if (CameraManager.hasCameraPermission()) {
97             if (visibility == View.VISIBLE) {
98                 CameraManager.get().openCamera();
99             } else {
100                 CameraManager.get().closeCamera();
101             }
102         }
103     }
104 
getContext()105     public Context getContext() {
106         return mHost.getView().getContext();
107     }
108 
setOnTouchListener(final View.OnTouchListener listener)109     public void setOnTouchListener(final View.OnTouchListener listener) {
110         mHost.getView().setOnTouchListener(listener);
111     }
112 
getHeight()113     public int getHeight() {
114         return mHost.getView().getHeight();
115     }
116 
onAttachedToWindow()117     public void onAttachedToWindow() {
118         if (CameraManager.hasCameraPermission()) {
119             CameraManager.get().openCamera();
120         }
121     }
122 
onDetachedFromWindow()123     public void onDetachedFromWindow() {
124         CameraManager.get().closeCamera();
125     }
126 
onRestoreInstanceState()127     public void onRestoreInstanceState() {
128         if (CameraManager.hasCameraPermission()) {
129             CameraManager.get().openCamera();
130         }
131     }
132 
onCameraPermissionGranted()133     public void onCameraPermissionGranted() {
134         CameraManager.get().openCamera();
135     }
136 
137     /**
138      * @return True if the view is valid and prepared for the camera to start showing the preview
139      */
isValid()140     public boolean isValid() {
141         return mHost.isValid();
142     }
143 
144     /**
145      * Starts the camera preview on the current surface.  Abstracts out the differences in API
146      * from the CameraManager
147      * @throws IOException Which is caught by the CameraManager to display an error
148      */
startPreview(final Camera camera)149     public void startPreview(final Camera camera) throws IOException {
150         mHost.startPreview(camera);
151     }
152 }
153