package org.pma.nutrifami.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.zxing.Result;

import org.pma.nutrifami.R;

import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.core.ViewFinderView;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class StartActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        final Button getStartedButton = (Button) findViewById(R.id.get_started_button);
        assert getStartedButton != null;
        final StartActivity activity = this;

        getStartedButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, LoginActivity.class);
                startActivity(intent);
            }
        });

        final ViewGroup contentFrame = (ViewGroup) findViewById(R.id.start_qr_layout);
        this.mScannerView = new ZXingScannerView(this) {
            @Override
            protected IViewFinder createViewFinderView(Context context) {
                return new CustomViewFinderView(context);
            }
        };
        contentFrame.addView(this.mScannerView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.mScannerView.setResultHandler(this);
        this.mScannerView.startCamera();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.mScannerView.stopCamera();
    }

    @Override
    public void handleResult(Result result) {
    }

    private static class CustomViewFinderView extends ViewFinderView {
        public static final String TRADE_MARK_TEXT = "ZXing";
        public static final int TRADE_MARK_TEXT_SIZE_SP = 40;
        public final Paint PAINT = new Paint();

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

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

        private void init() {
            PAINT.setColor(Color.WHITE);
            PAINT.setAntiAlias(true);
            float textPixelSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                    TRADE_MARK_TEXT_SIZE_SP, getResources().getDisplayMetrics());
            PAINT.setTextSize(textPixelSize);
        }

        @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            drawTradeMark(canvas);
        }

        private void drawTradeMark(Canvas canvas) {
            Rect framingRect = getFramingRect();
            float tradeMarkTop;
            float tradeMarkLeft;
            if (framingRect != null) {
                tradeMarkTop = framingRect.bottom + PAINT.getTextSize() + 10;
                tradeMarkLeft = framingRect.left;
            } else {
                tradeMarkTop = 10;
                tradeMarkLeft = canvas.getHeight() - PAINT.getTextSize() - 10;
            }
            canvas.drawText(TRADE_MARK_TEXT, tradeMarkLeft, tradeMarkTop, PAINT);
        }
    }
}

@@ -35,16 +35,41 @@
 ivity;%0A%0A
+import android.Manifest;%0A
 import a
@@ -119,24 +119,66 @@
 ent.Intent;%0A
+import android.content.pm.PackageManager;%0A
 import andro
@@ -315,16 +315,207 @@
 Bundle;%0A
+import android.support.annotation.NonNull;%0Aimport android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.content.ContextCompat;%0Aimport android.support.v4.content.PermissionChecker;%0A
 import a
@@ -589,16 +589,41 @@
 uteSet;%0A
+import android.util.Log;%0A
 import a
@@ -1562,24 +1562,26 @@
 k(View v) %7B%0A
+//
             
@@ -1639,24 +1639,26 @@
 ity.class);%0A
+//
             
@@ -1680,24 +1680,63 @@
 ty(intent);%0A
+                getCameraPermission();%0A
             
@@ -2121,32 +2121,880 @@
 erView);%0A    %7D%0A%0A
+    private void getCameraPermission() %7B%0A        // Here, thisActivity is the current activity%0A        if (ContextCompat.checkSelfPermission(this,%0A                Manifest.permission.CAMERA)%0A                != PackageManager.PERMISSION_GRANTED) %7B%0A%0A            // Should we show an explanation?%0A            if (ActivityCompat.shouldShowRequestPermissionRationale(this,%0A                    Manifest.permission.CAMERA)) %7B%0A            %7D else %7B%0A                ActivityCompat.requestPermissions(this,%0A                        new String%5B%5D%7BManifest.permission.CAMERA%7D,%0A                        1);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onRequestPermissionsResult(int requestCode, @NonNull String%5B%5D permissions, @NonNull int%5B%5D grantResults) %7B%0A        super.onRequestPermissionsResult(requestCode, permissions, grantResults);%0A%0A%0A    %7D%0A%0A
     @Override%0A  

Add permissions request