/*
 * Copyright (C) 2016-2017, Collabora Ltd.
 *   Author: Justin Kim <justin.kim@collabora.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.freedesktop.nativecamera2;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class NativeCamera2 extends Activity {

    static final String TAG = "NativeCamera2";

    public static native void startPreview(Surface surface);

    public static native void stopPreview();

    static {
        System.loadLibrary("native-camera2-jni");
    }

    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(this, "DENIED to access Camera", Toast.LENGTH_LONG).show();
            return;
        }

        surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();

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

                Log.v(TAG, "surface created.");
                startPreview(holder.getSurface());
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                stopPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Log.v(TAG, "format=" + format + " w/h : (" + width + ", " + height + ")");
            }
        });
    }

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

@@ -815,16 +815,62 @@
 Bundle;%0A
+import android.support.v4.app.ActivityCompat;%0A
 import a
@@ -902,32 +902,32 @@
 .ContextCompat;%0A
-
 import android.u
@@ -1157,16 +1157,77 @@
 era2%22;%0A%0A
+    private static final int PERMISSION_REQUEST_CAMERA = 1;%0A%0A
     publ
@@ -1724,80 +1724,179 @@
     
-Toast.makeText(this, %22DENIED to access Camera%22, Toast.LENGTH_LONG).show(
+ActivityCompat.requestPermissions(%0A                    this,%0A                    new String%5B%5D %7B Manifest.permission.CAMERA %7D,%0A                    PERMISSION_REQUEST_CAMERA
 );%0A 

Allow users to grant permissions