package com.appli.gui;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.appli.patoche.R;

// http://www.techotopia.com/index.php/Making_Runtime_Permission_Requests_in_Android_6.0
/*
Challenge:
        Permissions
        Rationale
*/

// https://guides.codepath.com/android/Understanding-App-Permissions

// https://guides.codepath.com/android/Understanding-App-Permissions

// https://code-troopers.com/2015/09/22/android_m_permissions/

//https://www.android-examples.com/android-marshmallow-request-runtime-permission/

public class test_permissions extends Activity
{
    private static final int RECORD_REQUEST_CODE = 101;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);

        if (permission != PackageManager.PERMISSION_GRANTED)
        {
            Log.i("TAG ++++ : ", "Permission to record denied");

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,  Manifest.permission.RECORD_AUDIO))
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Permission to access the microphone is required for this app to record audio.")
                        .setTitle("Permission required");

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        Log.i("TAG", "Clicked");
                        makeRequest();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }

            else
            {
                makeRequest();
            }

        }

    }

    protected void makeRequest()
    {
        ActivityCompat.requestPermissions(this,  new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {
        switch (requestCode)
        {
            case RECORD_REQUEST_CODE:
                {

                    if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED)
                    {
                        Log.i("TAG", "Permission has been denied by user");
                    }
                    else
                    {
                        Log.i("TAG", "Permission has been granted by user");
                    }
                    return;
            }
        }
    }
}