package org.xwiki.android.authenticator.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * PermissionsCheck.
 */
public class PermissionsUtils {
    private Activity mContext;
    private String[] mRequiredPermissions;
    private List<String> mPermissionsToRequest = new ArrayList<>();

    public PermissionsUtils(Activity context, String... requiredPermissions) {
        mContext = context;
        mRequiredPermissions = requiredPermissions;
    }

    /**
     * Checks if all the required permissions are granted.
     * @return true if all the required permissions are granted, otherwise false
     */
    public boolean checkPermissions() {
        for (String permission : mRequiredPermissions) {
            if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
                mPermissionsToRequest.add(permission);
            }
        }

        if (mPermissionsToRequest.isEmpty()) {
            return true;
        }

        return false;
    }

    /**
     * Requests the missing permissions.
     * The activity from which this method is called has to implement
     * {@link Activity#onRequestPermissionsResult(int, String[], int[])}
     * and then, inside it, it has to call the method
     * {@link PermissionsUtils#areAllRequiredPermissionsGranted(int[])} to check that all the
     * requested permissions are granted by the user
     * @param requestCode request code used by the activity
     */
    public void requestPermissions(int requestCode) {
        String[] request = mPermissionsToRequest.toArray(new String[mPermissionsToRequest.size()]);

        StringBuilder log = new StringBuilder();
        log.append("Requesting permissions:\n");

        for (String permission : request) {
            log.append(permission).append("\n");
        }

        Log.i(getClass().getSimpleName(), log.toString());

        ActivityCompat.requestPermissions(mContext, request, requestCode);
    }

    /**
     * Method to call inside
     * {@link Activity#onRequestPermissionsResult(int, String[], int[])}, to check if the
     * required permissions are granted.
     * @param grantResults results
     * @return true if all the required permissions are granted, otherwise false
     */
    public boolean areAllRequiredPermissionsGranted(int[] grantResults) {
        if (grantResults == null || grantResults.length == 0) {
            return false;
        }

        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }

        return true;
    }

}

@@ -70,16 +70,55 @@
 tivity;%0A
+import android.content.pm.PackageInfo;%0A
 import a
@@ -686,32 +686,751 @@
 issions;%0A    %7D%0A%0A
+    public PermissionsUtils(Activity activity) throws IllegalArgumentException %7B%0A        mContext = activity;%0A        try %7B%0A            PackageInfo info = activity%0A                    .getPackageManager()%0A                    .getPackageInfo(%0A                            activity.getPackageName(),%0A                            PackageManager.GET_PERMISSIONS%0A                    );%0A            mRequiredPermissions = info.requestedPermissions;%0A        %7D catch (PackageManager.NameNotFoundException e) %7B%0A            Log.e(PermissionsUtils.class.getSimpleName(), %22Can't get package of input activity%22);%0A            throw new IllegalArgumentException(%22This activity can't be used as source of app context%22);%0A        %7D%0A    %7D%0A%0A
     /**%0A     * C

add constructor for PermissionsUtils for simplify access to PermissionsUtils