package com.ndob.yarrar;

import android.content.res.AssetManager;
import android.hardware.Camera;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

public class YarrarActivity extends ActionBarActivity implements Camera.PreviewCallback {
    private static final String TAG = "YarrarActivity";
    private Camera mCamera;
    private AssetManager mAssetManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAssetManager = getResources().getAssets();

        mCamera = getCameraInstance();
        mCamera.setPreviewCallback(this);
        mCamera.startPreview();

        System.loadLibrary("yarrar");
        Log.i(TAG, "Yarrar glue loaded.");
    }

    @Override
    protected void onDestroy() {
        deinitYarrar();
        super.onDestroy();
    }

    public static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e) {
            Log.i(TAG, "error:" + e.getMessage());
        }
        return c;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
        }
        mCamera = null;
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Size size = mCamera.getParameters().getPreviewSize();
        injectCameraFrame(size.width, size.height, data);
    }

    public void onOpenGLSurfaceCreated() {
        Camera.Size size = mCamera.getParameters().getPreviewSize();
        initYarrar(size.width, size.height, mAssetManager);
        onYarrarInitialized();
    }

    public void onOpenGLDrawFrame() {
        run();
    }

    protected void initialize(LinearLayout layout) {
        layout.addView(new OpenGLSurface(this));
    }

    public void onYarrarInitialized() {
    }

    private native void initYarrar(int width, int height, AssetManager assetManager);
    private native void deinitYarrar();
    private native void run();
    private native void injectCameraFrame(int width, int height, byte[] cameraData);
    protected native void addModel(float[] vertices);
}

@@ -19,16 +19,48 @@
 arrar;%0A%0A
+import android.app.AlertDialog;%0A
 import a
@@ -588,24 +588,106 @@
 nceState);%0A%0A
+        System.loadLibrary(%22yarrar%22);%0A        Log.i(TAG, %22Yarrar glue loaded.%22);%0A%0A
         mAss
@@ -722,25 +722,24 @@
 etAssets();%0A
-%0A
         mCam
@@ -777,153 +777,185 @@
     
+if(
 mCamera
-.setPreviewCallback(this);%0A        mCamera.startPreview();%0A%0A        System.loadLibrary(%22yarrar%22);%0A        Log.i(TAG, %22Yarrar glue loaded.%22
+ == null)%0A        %7B%0A            showError(%22Can't open camera.%22);%0A            return;%0A        %7D%0A%0A        mCamera.setPreviewCallback(this);%0A        mCamera.startPreview(
 );%0A 
@@ -2182,24 +2182,270 @@
 () %7B%0A    %7D%0A%0A
+    private void showError(String msg) %7B%0A        AlertDialog.Builder alert = new AlertDialog.Builder(this);%0A        alert.setTitle(%22Error%22);%0A        alert.setMessage(msg);%0A        alert.setPositiveButton(%22OK%22, null);%0A        alert.show();%0A    %7D%0A%0A
     private 

Add popping up error message, when can't open camera on Android.