package learn2crack.marshmallowpermissions;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Context context;
    private Activity activity;
    private static final int PERMISSION_REQUEST_CODE = 1;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        context = getApplicationContext();
        activity = this;
        Button check_permission = (Button)findViewById(R.id.check_permission);
        Button request_permission = (Button)findViewById(R.id.request_permission);
        check_permission.setOnClickListener(this);
        request_permission.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        view = v;

        int id = v.getId();
        switch (id){
            case R.id.check_permission:
                if (checkPermission()) {

                    Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
                }
                break;
            case R.id.request_permission:
                if (!checkPermission()) {

                    requestPermission();

                } else {

                    Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

                }
                break;
        }
    }

    private boolean checkPermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED){

            return true;

        } else {

            return false;

        }
    }

    private void requestPermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)){

            Toast.makeText(context,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Snackbar.make(view,"Permission Granted, Now you can access location data.",Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view,"Permission Denied, You cannot access location data.",Snackbar.LENGTH_LONG).show();

                }
                break;
        }
    }
}

@@ -601,68 +601,8 @@
 r%7B%0A%0A
-    private Context context;%0A    private Activity activity;%0A
     
@@ -949,76 +949,8 @@
 );%0A%0A
-        context = getApplicationContext();%0A        activity = this;%0A
     
@@ -2088,23 +2088,39 @@
 mission(
-c
+getApplicationC
 ontext
+()
 , Manife
@@ -2406,24 +2406,20 @@
 tionale(
-activity
+this
 ,Manifes
@@ -2487,15 +2487,31 @@
 ext(
-c
+getApplicationC
 ontext
+()
 ,%22GP
@@ -2710,24 +2710,20 @@
 issions(
-activity
+this
 ,new Str
@@ -3442,9 +3442,8 @@
 %0A    %7D%0A%7D
-%0A

Fix potential memory leak