package me.ranmocy.monkeytree;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSIONS_REQUEST = 0;
    private static final List<String> REQUIRED_PERMISSIONS = Arrays.asList(
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (checkPermission()) {
            initUi();
        } else {
            if (shouldShowPermissionRationale()) {
                showDialogToExplainPermission();
            } else {
                requestPermissions();
            }
        }
    }

    private boolean checkPermission() {
        for (String permission : REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    private boolean shouldShowPermissionRationale() {
        for (String permission : REQUIRED_PERMISSIONS) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                return true;
            }
        }
        return false;
    }

    private void requestPermissions() {
        ActivityCompat.requestPermissions(
                this,
                REQUIRED_PERMISSIONS.toArray(new String[REQUIRED_PERMISSIONS.size()]),
                PERMISSIONS_REQUEST);
    }

    @Override
    public void onRequestPermissionsResult(
            int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (PERMISSIONS_REQUEST == requestCode) {
            if (grantResults.length < permissions.length) {
                showDialogToExplainPermission();
            } else {
                initUi();
            }
        }
    }

    private void showDialogToExplainPermission() {
        new AlertDialog.Builder(this)
                .setMessage(R.string.dialog_permission_message)
                .setCancelable(false)
                .setPositiveButton(R.string.btn_retry, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        requestPermissions();
                    }
                })
                .setNegativeButton(R.string.btn_quit, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                })
                .show();
    }

    private void initUi() {
        getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content, MainFragment.create())
                .commit();
    }
}

@@ -387,16 +387,41 @@
 ctivity;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -518,16 +518,70 @@
 vity %7B%0A%0A
+    private static final String TAG = %22MainActivity%22;%0A
     priv
@@ -957,33 +957,33 @@
            initU
-i
+I
 ();%0A        %7D el
@@ -1802,24 +1802,66 @@
 issions() %7B%0A
+        Log.i(TAG, %22requestPermissions%22);%0A
         Acti
@@ -2284,126 +2284,368 @@
 gth 
-%3C permissions.length) %7B%0A                showDialogToExplainPermission();%0A            %7D else %7B%0A                initUi()
+%3E 0 && allResultGranted(grantResults)) %7B%0A                initUI();%0A            %7D else %7B%0A                showDialogToExplainPermission();%0A            %7D%0A        %7D%0A    %7D%0A%0A    private boolean allResultGranted(int%5B%5D grantResults) %7B%0A        for (int result : grantResults) %7B%0A            if (PackageManager.PERMISSION_GRANTED != result) %7B%0A                return false
 ;%0A  
@@ -2658,32 +2658,53 @@
     %7D%0A        %7D%0A
+        return true;%0A
     %7D%0A%0A    priva
@@ -2737,32 +2737,80 @@
 nPermission() %7B%0A
+        Log.i(TAG, %22Showing rationale dialog%22);%0A
         new Aler
@@ -3669,14 +3669,75 @@
 nitU
-i
+I
 () %7B%0A
+        Log.i(TAG, %22ALl permissions are granted. Init UI.%22);%0A
     

fix permission request result handling