package io.github.shark_app.shark;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;

import com.google.android.gms.samples.vision.barcodereader.BarcodeCapture;
import com.google.android.gms.samples.vision.barcodereader.BarcodeGraphic;
import com.google.android.gms.vision.barcode.Barcode;

import java.util.List;

import xyz.belvi.mobilevisionbarcodescanner.BarcodeRetriever;

import static io.github.shark_app.shark.R.id.barcode;

public class ScanActivity extends AppCompatActivity implements BarcodeRetriever {
    private static final String TAG = "CAMERA";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        final BarcodeCapture barcodeCapture = (BarcodeCapture) getSupportFragmentManager().findFragmentById(barcode);
        barcodeCapture.setRetrieval(this);
    }

    @Override
    public void onRetrieved(final Barcode barcode) {
        Log.d(TAG, "Barcode read: " + barcode.displayValue);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(ScanActivity.this)
                        .setTitle("code retrieved")
                        .setMessage(barcode.displayValue);
                builder.show();
            }
        });
    }

    @Override
    public void onRetrievedFailed(final String reason) {
        Log.d(TAG, "Barcode reading failed : " + reason);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(ScanActivity.this)
                        .setTitle("Barcode failed")
                        .setMessage(reason);
                builder.show();
            }
        });
    }

    @Override
    public void onRetrievedMultiple(final Barcode closetToClick, final List<BarcodeGraphic> barcodeGraphics) {}

    @Override
    public void onBitmapScanned(SparseArray<Barcode> sparseArray) {}
}

@@ -48,17 +48,269 @@
 oid.
-os.Bundle
+Manifest;%0Aimport android.content.pm.PackageManager;%0Aimport android.os.Bundle;%0Aimport android.support.annotation.NonNull;%0Aimport android.support.design.widget.Snackbar;%0Aimport android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.content.ContextCompat
 ;%0Aim
@@ -456,16 +456,42 @@
 seArray;
+%0Aimport android.view.View;
 %0A%0Aimport
@@ -961,16 +961,70 @@
 AMERA%22;%0A
+    private static final int PERMISSIONS_CAMERA = 1;%0A%0A
     @Ove
@@ -1190,158 +1190,25 @@
     
-final BarcodeCapture barcodeCapture = (BarcodeCapture) getSupportFragmentManager().findFragmentById(barcode);%0A        barcodeCapture.setRetrieval(this
+checkPermissions(
 );%0A 
@@ -2305,32 +2305,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -2381,11 +2381,1630 @@
 rray) %7B%7D
+%0A%0A    private void makeSnackbar(String snackbarText) %7B%0A        Snackbar.make(getWindow().getDecorView().getRootView(), snackbarText, Snackbar.LENGTH_LONG)%0A                .setAction(%22Dismiss%22, new View.OnClickListener() %7B%0A                    @Override%0A                    public void onClick(View v) %7B%7D%0A                %7D)%0A                .show();%0A    %7D%0A%0A    private void checkPermissions() %7B%0A        String permission = Manifest.permission.CAMERA;%0A        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) %7B%0A            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) %7B%0A                makeSnackbar(%22Allow camera access to scan QR code%22);%0A            %7D%0A            ActivityCompat.requestPermissions(this, new String%5B%5D%7Bpermission%7D, PERMISSIONS_CAMERA);%0A        %7D else %7B%0A            instantiateBarcodeScanner();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions%5B%5D, @NonNull int%5B%5D grantResults) %7B%0A        switch (requestCode) %7B%0A            case PERMISSIONS_CAMERA: %7B%0A                if (grantResults.length %3E 0 && grantResults%5B0%5D == PackageManager.PERMISSION_GRANTED) %7B%0A                    instantiateBarcodeScanner();%0A                %7D else %7B%0A                    makeSnackbar(%22Cannot continue without access to camera%22);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public void instantiateBarcodeScanner()%7B%0A        final BarcodeCapture barcodeCapture = (BarcodeCapture) getSupportFragmentManager().findFragmentById(barcode);%0A        barcodeCapture.setRetrieval(this);%0A    %7D
 %0A%7D%0A

Check and ask for camera permission