package com.tf.reusable.demoapplication.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;

/**
 * Created by kamran on 8/28/16.
 */

public class PermissionHelper {

    private static final int PERMISSION_REQUEST_CODE = 0;

    public static void askForAllPermissions(Activity activity) {
        try {
            String[] permissions = getRequiredPermissions(activity);
            ArrayList<String> permissionsToBeAsked = new ArrayList<>();

            if (permissions != null) {
                for (String permission : permissions) {
                    if (!isPermissionGranted(activity, permission) && isDangerous(activity, permission)) {
                        permissionsToBeAsked.add(permission);
                    }
                }
            }

            if (permissionsToBeAsked.size() > 0) {
                askForPermission(activity, permissionsToBeAsked.toArray(new String[]{}));
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static String[] getRequiredPermissions(Context context) throws PackageManager.NameNotFoundException {
        return context.getPackageManager()
                        .getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS)
                        .requestedPermissions;
    }

    public static boolean isPermissionGranted(Context context, String permission) {
        return Build.VERSION.SDK_INT <= 22
                || ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean areAllPermissionsGranted(Context context) {
        boolean areGranted = true;

        try {
            String[] permissions = getRequiredPermissions(context);

            for (String permission : permissions) {
                areGranted = areGranted
                        && (!isDangerous(context, permission) || isPermissionGranted(context, permission));
            }
        } catch (PackageManager.NameNotFoundException e) {
            Logger.printStackTrace(e);

            return false;
        }

        return areGranted;
    }

    public static void askForPermission(Activity activity, String permissions) {
        askForPermission(activity, new String[]{permissions});
    }

    public static void askForPermission(Activity activity, String... permissions) {
        if (Build.VERSION.SDK_INT >= 22) {
            ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST_CODE);
        }
    }

    private static boolean isDangerous(Context context, String permission) {
        boolean isDangerous = false;

        try {
            isDangerous = context.getPackageManager().getPermissionInfo(permission, 0).protectionLevel
                    == PermissionInfo.PROTECTION_DANGEROUS;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return isDangerous;
    }

    public static void showPermissionSettings(Activity activity, int code) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));

        activity.startActivityForResult(intent, code);
    }
}

@@ -3302,32 +3302,728 @@
 ngerous;%0A    %7D%0A%0A
+    public static boolean isAnyPermissionDeniedByUser(Activity activity) %7B%0A        boolean isChecked = false;%0A%0A        if (Build.VERSION.SDK_INT %3E= 22) %7B%0A            return false;%0A        %7D%0A%0A        try %7B%0A            String%5B%5D requiredPermissions = getRequiredPermissions(activity);%0A%0A            for (String requiredPermission : requiredPermissions) %7B%0A                if (isDangerous(activity, requiredPermission)) %7B%0A                    isChecked = isChecked %7C%7C activity.shouldShowRequestPermissionRationale(requiredPermission);%0A                %7D%0A            %7D%0A        %7D catch (PackageManager.NameNotFoundException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return isChecked;%0A    %7D%0A%0A
     public stati

Add a helper method to find out if user has denied at least one permission when prompted for