/*
 * Copyright (c) 2014 Mario Guggenberger <mg@protyposis.net>
 *
 * This file is part of MediaPlayer-Extended.
 *
 * MediaPlayer-Extended is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MediaPlayer-Extended is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MediaPlayer-Extended.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.protyposis.android.spectaculum;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;

import java.io.IOException;
import java.util.List;

/**
 * @author Mario Guggenberger
 */
public class CameraView extends SpectaculumView {

    private static final String TAG = CameraView.class.getSimpleName();

    private Camera mCamera;
    private int mCameraId;

    public CameraView(Context context) {
        super(context);
        init(context);
    }

    public CameraView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mCameraId = 0;
        if(!checkCameraHardware(context)) {
            Log.w(TAG, "no camera present");
        }
    }

    /** Check if this device has a camera */
    private boolean checkCameraHardware(Context context) {
        if(isInEditMode()) {
            // there's no camera in the layout editor available
            return false;
        }
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    @Override
    public void onInputSurfaceCreated(InputSurfaceHolder inputSurfaceHolder) {
        startCamera();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        stopCamera();
        super.surfaceDestroyed(holder);
    }

    @Override
    public void onPause() {
        stopCamera();
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void startCamera() {
        try {
            if(mCamera == null) {
                mCamera = Camera.open(mCameraId);

                // enable autofocus if available
                List<String> supportedFocusModes = mCamera.getParameters().getSupportedFocusModes();
                if(supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
                    Camera.Parameters params = mCamera.getParameters();
                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                    mCamera.setParameters(params);
                }

                // set orientation
                Camera.CameraInfo info = new Camera.CameraInfo();
                Camera.getCameraInfo(mCameraId, info);
                WindowManager windowManager = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
                int rotation = windowManager.getDefaultDisplay().getRotation();
                int degrees = 0;
                switch (rotation) {
                    case Surface.ROTATION_0: degrees = 0; break;
                    case Surface.ROTATION_90: degrees = 90; break;
                    case Surface.ROTATION_180: degrees = 180; break;
                    case Surface.ROTATION_270: degrees = 270; break;
                }
                int result;
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    result = (info.orientation + degrees) % 360;
                    result = (360 - result) % 360;
                } else {
                    result = (info.orientation - degrees + 360) % 360;
                }
                mCamera.setDisplayOrientation(result);

                // setup preview
                mCamera.setPreviewTexture(getInputHolder().getSurfaceTexture());
                mCamera.startPreview();

                if(result == 0 || result == 180) {
                    mImageWidth = mCamera.getParameters().getPreviewSize().width;
                    mImageHeight = mCamera.getParameters().getPreviewSize().height;
                } else {
                    // swap width/height in portrait mode for a correct aspect ratio
                    mImageHeight = mCamera.getParameters().getPreviewSize().width;
                    mImageWidth = mCamera.getParameters().getPreviewSize().height;
                }

                updateResolution(mImageWidth, mImageHeight);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stopCamera() {
        if(mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

    public boolean supportsCameraSwitch() {
        return Camera.getNumberOfCameras() > 1;
    }

    public void switchCamera() {
        mCameraId = ((mCameraId + 1) % Camera.getNumberOfCameras());
        stopCamera();
        startCamera();
    }
}

@@ -4565,24 +4565,59 @@
 Preview();%0A%0A
+                int width, height;%0A
             
@@ -4667,39 +4667,33 @@
                 
-mImageW
+w
 idth = mCamera.g
@@ -4743,39 +4743,33 @@
                 
-mImageH
+h
 eight = mCamera.
@@ -4939,23 +4939,17 @@
         
-mImageH
+h
 eight = 
@@ -5016,23 +5016,17 @@
         
-mImageW
+w
 idth = m
@@ -5129,28 +5129,16 @@
 ion(
-mImageWidth, mImageH
+width, h
 eigh

Fix camera view layout resizing