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.hardware.Camera; 21 import android.os.Parcelable; 22 import android.view.SurfaceHolder; 23 import android.view.SurfaceView; 24 import android.view.View; 25 26 import java.io.IOException; 27 28 /** 29 * A software rendered preview surface for the camera. This renders slower and causes more jank, so 30 * HardwareCameraPreview is preferred if possible. 31 * 32 * There is a significant amount of duplication between HardwareCameraPreview and 33 * SoftwareCameraPreview which we can't easily share due to a lack of multiple inheritance, The 34 * implementations of the shared methods are delegated to CameraPreview 35 */ 36 public class SoftwareCameraPreview extends SurfaceView implements CameraPreview.CameraPreviewHost { 37 private final CameraPreview mPreview; 38 SoftwareCameraPreview(final Context context)39 public SoftwareCameraPreview(final Context context) { 40 super(context); 41 mPreview = new CameraPreview(this); 42 getHolder().addCallback(new SurfaceHolder.Callback() { 43 @Override 44 public void surfaceCreated(final SurfaceHolder surfaceHolder) { 45 CameraManager.get().setSurface(mPreview); 46 } 47 48 @Override 49 public void surfaceChanged(final SurfaceHolder surfaceHolder, final int format, final int width, 50 final int height) { 51 CameraManager.get().setSurface(mPreview); 52 } 53 54 @Override 55 public void surfaceDestroyed(final SurfaceHolder surfaceHolder) { 56 CameraManager.get().setSurface(null); 57 } 58 }); 59 } 60 61 62 @Override onVisibilityChanged(final View changedView, final int visibility)63 protected void onVisibilityChanged(final View changedView, final int visibility) { 64 super.onVisibilityChanged(changedView, visibility); 65 mPreview.onVisibilityChanged(visibility); 66 } 67 68 @Override onDetachedFromWindow()69 protected void onDetachedFromWindow() { 70 super.onDetachedFromWindow(); 71 mPreview.onDetachedFromWindow(); 72 } 73 74 @Override onAttachedToWindow()75 protected void onAttachedToWindow() { 76 super.onAttachedToWindow(); 77 mPreview.onAttachedToWindow(); 78 } 79 80 @Override onRestoreInstanceState(final Parcelable state)81 protected void onRestoreInstanceState(final Parcelable state) { 82 super.onRestoreInstanceState(state); 83 mPreview.onRestoreInstanceState(); 84 } 85 86 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)87 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 88 widthMeasureSpec = mPreview.getWidthMeasureSpec(widthMeasureSpec, heightMeasureSpec); 89 heightMeasureSpec = mPreview.getHeightMeasureSpec(widthMeasureSpec, heightMeasureSpec); 90 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 91 } 92 93 @Override getView()94 public View getView() { 95 return this; 96 } 97 98 @Override isValid()99 public boolean isValid() { 100 return getHolder() != null; 101 } 102 103 @Override startPreview(final Camera camera)104 public void startPreview(final Camera camera) throws IOException { 105 camera.setPreviewDisplay(getHolder()); 106 } 107 108 @Override onCameraPermissionGranted()109 public void onCameraPermissionGranted() { 110 mPreview.onCameraPermissionGranted(); 111 } 112 } 113 114 115