Example usage for android.app Activity onRequestPermissionsResult

List of usage examples for android.app Activity onRequestPermissionsResult

Introduction

In this page you can find the example usage for android.app Activity onRequestPermissionsResult.

Prototype

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) 

Source Link

Document

Callback for the result from requesting permissions.

Usage

From source file:org.citra_emu.citra.utils.PermissionUtil.java

/**
 * Checks a permission, if needed shows a dialog to request it
 *
 * @param activity    the activity requiring the permission
 * @param permission  the permission needed
 * @param requestCode supplied to the callback to determine the next action
 *///  w  w w . j a v a2  s .  co m
public static void verifyPermission(Activity activity, String permission, int requestCode) {
    if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) {
        // call the callback called by requestPermissions
        activity.onRequestPermissionsResult(requestCode, new String[] { permission },
                new int[] { PackageManager.PERMISSION_GRANTED });
        return;
    }

    ActivityCompat.requestPermissions(activity, new String[] { permission }, requestCode);
}

From source file:dev.nick.accessories.injection.processors.PermissionProcessor.java

@Override
public boolean process(@NonNull Object obj, @NonNull Type type) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return false;

    in();//w  w w .j av a  2  s .c o m

    Activity activity = null;

    if (obj instanceof Activity) {
        activity = (Activity) obj;
    }

    if (obj instanceof Fragment) {
        Fragment fragment = (Fragment) obj;
        activity = fragment.getActivity();
    }

    if (obj instanceof android.support.v4.app.Fragment) {
        android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj;
        activity = fragment.getActivity();
    }

    Preconditions.checkNotNull(activity, "Only Activity or Fragment is supported for this annotation.");

    RequestPermissions autoRequirePermission = activity.getClass().getAnnotation(RequestPermissions.class);
    int code = autoRequirePermission.requestCode();

    String[] scope = autoRequirePermission.permissions();
    String[] required;
    if (scope.length == 0) {
        scope = getPkgInfo(activity).requestedPermissions;
        required = extractUnGranted(activity, scope);
    } else {
        required = extractUnGranted(activity, scope);
    }
    if (required == null || required.length == 0) {
        int[] codes = new int[0];
        if (required != null) {
            codes = new int[scope.length];
            for (int i = 0; i < codes.length; i++) {
                codes[i] = PackageManager.PERMISSION_GRANTED;
            }
        }
        activity.onRequestPermissionsResult(code, scope, codes);
    } else {
        activity.requestPermissions(required, code);
    }
    return true;
}

From source file:com.nick.scalpel.core.request.RequestPermissionWirer.java

@Override
public void wire(Object obj) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return;/*from  w ww .j  a  v  a 2 s . c o m*/

    Activity activity = null;

    if (obj instanceof Activity) {
        activity = (Activity) obj;
    }

    if (obj instanceof Fragment) {
        Fragment fragment = (Fragment) obj;
        activity = fragment.getActivity();
    }

    if (obj instanceof android.support.v4.app.Fragment) {
        android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj;
        activity = fragment.getActivity();
    }

    Preconditions.checkNotNull(activity, "Only Activity or Fragment(app/v4) is supported for this annotation.");

    RequirePermission autoRequirePermission = activity.getClass().getAnnotation(RequirePermission.class);
    int code = autoRequirePermission.requestCode();

    String[] scope = autoRequirePermission.permissions();
    String[] required;
    if (scope.length == 0) {
        scope = getPkgInfo(activity).requestedPermissions;
        required = extractUnGranted(activity, scope);
    } else {
        required = extractUnGranted(activity, scope);
    }
    if (required == null || required.length == 0) {
        int[] codes = new int[0];
        if (required != null) {
            codes = new int[scope.length];
            for (int i = 0; i < codes.length; i++) {
                codes[i] = PackageManager.PERMISSION_GRANTED;
            }
        }
        activity.onRequestPermissionsResult(code, scope, codes);
    } else {
        activity.requestPermissions(required, code);
    }
}