package com.wonderkiln.camerakit;

import android.annotation.TargetApi;
import android.content.Context;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

public class SurfaceViewPreview extends PreviewImpl {

    private Context mContext;
    private ViewGroup mParent;
    private SurfaceViewContainer mContainer;
    private SurfaceView mSurfaceView;

    private int mDisplayOrientation;

    SurfaceViewPreview(final Context context, ViewGroup parent) {
        this.mContext = context;
        this.mParent = parent;

        final View view = View.inflate(context, R.layout.surface_view, parent);
        mContainer = (SurfaceViewContainer) view.findViewById(R.id.surface_view_container);
        mSurfaceView = (SurfaceView)  mContainer.findViewById(R.id.surface_view);

        final SurfaceHolder holder = mSurfaceView.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                setSize(width, height);
                if (isReady()) dispatchSurfaceChanged();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                setSize(0, 0);
            }
        });
    }

    @Override
    SurfaceHolder getSurfaceHolder() {
        return mSurfaceView.getHolder();
    }

    @Override
    Surface getSurface() {
        return getSurfaceHolder().getSurface();
    }

    @Override
    View getView() {
        return mContainer;
    }

    @Override
    Class getOutputClass() {
        return SurfaceHolder.class;
    }

    @Override
    void setDisplayOrientation(int displayOrientation) {
        mDisplayOrientation = displayOrientation;
    }

    @Override
    boolean isReady() {
        return getWidth() != 0 && getHeight() != 0 && getWidth() == getPreviewWidth() && getHeight() == getPreviewHeight();
    }

    @TargetApi(15)
    @Override
    void setPreviewParameters(final int width, final int height, final int format) {
        super.setPreviewParameters(width, height, format);
        mContainer.setPreviewSize(new Size(width, height));
        getSurfaceHolder().setFixedSize(getPreviewWidth(), getPreviewHeight());
        getSurfaceHolder().setFormat(getPreviewFormat());
    }

}

@@ -2428,24 +2428,127 @@
 , height));%0A
+        mContainer.post(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         getS
@@ -2611,24 +2611,32 @@
 wHeight());%0A
+        
         getS
@@ -2677,21 +2677,47 @@
 wFormat());%0A
+            %7D%0A        %7D);%0A
     %7D%0A%0A%7D%0A

Fix surfaceview wrong thread crash