package com.anthonycr.grant;

import android.content.pm.PackageManager;
import android.support.annotation.NonNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * This abstract class should be used to create an if/else action that the PermissionsManager
 * can execute when the permissions you request are granted or denied. Simple use involves
 * creating an anonymous instance of it and passing that instance to the
 * requestPermissionsIfNecessaryForResult method. The result will be sent back to you as
 * either onGranted (all permissions have been granted), or onDenied (a required permission
 * has been denied). Ideally you put your functionality in the onGranted method and notify
 * the user what won't work in the onDenied method.
 */
public abstract class PermissionsResultAction {

    private final Set<String> mPermissions = new HashSet<>(1);

    public abstract void onGranted();

    public abstract void onDenied(String permission);

    /**
     * This method is called when a particular permission has changed.
     * This method will be called for all permissions, so this method determines
     * if the permission affects the state or not and whether it can proceed with
     * calling onGranted or if onDenied should be called.
     *
     * @param permission the permission that changed.
     * @param result     the result for that permission.
     * @return this method returns true if its primary action has been completed
     * and it should be removed from the data structure holding a reference to it.
     */
    @SuppressWarnings("unused")
    public synchronized final boolean onResult(@NonNull String permission, int result) {
        if (result == PackageManager.PERMISSION_GRANTED) {
            mPermissions.remove(permission);
            if (mPermissions.isEmpty()) {
                onGranted();
                return true;
            }
        } else {
            onDenied(permission);
            return true;
        }
        return false;
    }

    /**
     * This method registers the PermissionsResultAction object for the specified permissions
     * so that it will know which permissions to look for changes to. The PermissionsResultAction
     * will then know to look out for changes to these permissions.
     *
     * @param perms the permissions to listen for
     */
    @SuppressWarnings("unused")
    public synchronized final void registerPermissions(@NonNull String[] perms) {
        Collections.addAll(mPermissions, perms);
    }
}

@@ -65,16 +65,69 @@
 anager;%0A
+import android.os.Handler;%0Aimport android.os.Looper;%0A
 import a
@@ -942,16 +942,698 @@
 et%3C%3E(1);
+%0A    private Looper mLooper = Looper.getMainLooper();%0A%0A    /**%0A     * Default Constructor%0A     */%0A    public PermissionsResultAction() %7B%7D%0A%0A    /**%0A     * Alternate Constructor. Pass the looper you wish the PermissionsResultAction%0A     * callbacks to be executed on if it is not the current Looper. For instance,%0A     * if you are making a permissions request from a background thread but wish the%0A     * callback to be on the UI thread, use this constructor to specify the UI Looper.%0A     *%0A     * @param looper the looper that the callbacks will be called using.%0A     */%0A    @SuppressWarnings(%22unused%22)%0A    public PermissionsResultAction(@NonNull Looper looper) %7BmLooper = looper;%7D
 %0A%0A    pu
@@ -2314,40 +2314,8 @@
  */%0A
-    @SuppressWarnings(%22unused%22)%0A
     
@@ -2357,16 +2357,22 @@
 nResult(
+final 
 @NonNull
@@ -2571,112 +2571,450 @@
     
-onGranted();%0A                return true;%0A            %7D%0A        %7D else %7B%0A            onDenied(permission
+new Handler(mLooper).post(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        onGranted();%0A                    %7D%0A                %7D);%0A                return true;%0A            %7D%0A        %7D else %7B%0A            new Handler(mLooper).post(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    onDenied(permission);%0A                %7D%0A            %7D
 );%0A 
@@ -3413,40 +3413,8 @@
  */%0A
-    @SuppressWarnings(%22unused%22)%0A
     

Allow different callback to be made on a different thread than the request was originally made on.