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