Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
android 6.0 request permission callback pattern
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import java.util.Map;
public class PermissionUtils {
public static void request(Activity activity, final String permission, final SinglePermissionCallback listener,
@StringRes int requestRationaleId, @StringRes int okTextId, @StringRes int cancelTextId) {
request(activity, permission, listener, activity.getString(requestRationaleId), activity.getString(okTextId),
activity.getString(cancelTextId));
}
public static void request(Activity activity, final String permission, final SinglePermissionCallback listener,
@NonNull String requestRationale, @Nullable String okText, @Nullable String cancelText) {
request(activity, new String[]{permission}, new PermissionCallback() {
@Override
public void onCancel() {
if (listener != null) listener.onCancel();
}
@Override
public void onResult(Map<String, Integer> result) {
Integer resultCode = result.get(permission);
if (resultCode == null || resultCode == PackageManager.PERMISSION_DENIED) {
if (listener != null) listener.onDenied();
} else {
if (listener != null) listener.onGranted();
}
}
}, requestRationale, okText, cancelText);
}
public static void request(Activity activity, String[] permissions, final PermissionCallback listener,
@StringRes int requestRationaleId, @StringRes int okTextId, @StringRes int cancelTextId) {
request(activity, permissions, listener, activity.getString(requestRationaleId), activity.getString(okTextId),
activity.getString(cancelTextId));
}
public static void request(final Activity activity, final String[] permissions, final PermissionCallback listener,
@NonNull String requestRationale, @Nullable String okText, @Nullable String cancelText) {
boolean haveShowRationaleAndRequest = false;
for (String permission : permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
new Alerter(activity).alert(requestRationale, okText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RequestPermissionActivity.launch(activity, permissions, listener);
}
}, cancelText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onCancel();
}
}, false);
haveShowRationaleAndRequest = true;
break;
}
}
if (!haveShowRationaleAndRequest) {
RequestPermissionActivity.launch(activity, permissions, listener);
}
}
public interface PermissionCallback extends PermissionsResultListener {
void onCancel();
}
public interface SinglePermissionCallback {
void onGranted();
void onDenied();
void onCancel();
}
public static abstract class SimplePermissionCallback implements SinglePermissionCallback {
public abstract void onSuccess();
public abstract void onFailure();
@Override
public void onGranted() {
onSuccess();
}
@Override
public void onDenied() {
onFailure();
}
@Override
public void onCancel() {
onFailure();
}
}
}
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RequestPermissionActivity extends Activity {
private static final int REQUEST_GRANT_PERMISSION = 236;
public static void launch(Activity activity, String[] permissions, PermissionsResultListener listener) {
Intent intent = new Intent(activity, RequestPermissionActivity.class);
intent.putExtra("permissions", permissions);
activity.startActivity(intent);
activity.overridePendingTransition(0, 0);
sListener = listener;
}
public interface PermissionsResultListener {
void onResult(Map<String, Integer> result);
}
@Nullable private static PermissionsResultListener sListener;
private List<String> mPermissions;
private List<String> mGrantedPermissions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPermissions = Arrays.asList(getIntent().getStringArrayExtra("permissions"));
//all granted if api level < 23
if (!VersionUtils.marshmallowOrLater()) {
returnAllGranted();
return;
}
//filter granted permission
List<String> permissionsNotGranted = new ArrayList<>();
mGrantedPermissions = new ArrayList<>();
for (String permission : mPermissions) {
if (ActivityCompat.checkSelfPermission(getActivity(), permission)
== PackageManager.PERMISSION_DENIED) {
permissionsNotGranted.add(permission);
} else {
mGrantedPermissions.add(permission);
}
}
if (permissionsNotGranted.size() == 0) {
returnAllGranted();
return;
}
ActivityCompat.requestPermissions(getActivity(),
permissionsNotGranted.toArray(new String[permissionsNotGranted.size()]),
REQUEST_GRANT_PERMISSION);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != REQUEST_GRANT_PERMISSION) return;
Map<String, Integer> result = new HashMap<>();
for (String permission : mPermissions) {
boolean done = false;
for (int i = 0; i < permissions.length; i++) {
if (permissions[i].equals(permission)) {
result.put(permission, grantResults[i]);
done = true;
break;
}
}
if (done) continue;
if (mGrantedPermissions.contains(permission)) {
result.put(permission, PackageManager.PERMISSION_GRANTED);
} else {
result.put(permission, PackageManager.PERMISSION_DENIED);
}
}
if (sListener != null) {
sListener.onResult(result);
sListener = null;
}
finish();
overridePendingTransition(0, 0);
}
private void returnAllGranted() {
Map<String, Integer> result = new HashMap<>();
for (String permission : mPermissions) {
result.put(permission, PackageManager.PERMISSION_GRANTED);
}
if (sListener != null) {
sListener.onResult(result);
sListener = null;
}
finish();
overridePendingTransition(0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.