Skip to content

Instantly share code, notes, and snippets.

@BenDLH
Last active December 31, 2018 19:49
Show Gist options
  • Save BenDLH/6e76a6cf898b13e54e61c27a96b88f2b to your computer and use it in GitHub Desktop.
Save BenDLH/6e76a6cf898b13e54e61c27a96b88f2b to your computer and use it in GitHub Desktop.
Android Marshmallow Permissions Helper
package dk.shape.goboat.managers;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import java.util.ArrayList;
import java.util.List;
//
// Marshmallow Permissions Helper
//
// Created by Ben De La Haye on 02/03/2017.
//
// Usage:
// _permissionsHelper.requestPermission(this,
// new PermissionsHelper.RequestData(Manifest.permission.WRITE_EXTERNAL_STORAGE,
// getString(R.string.permission_explanation_external_storage),
// () -> Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show(),
// () -> Toast.makeText(this, "Request permission failed!", Toast.LENGTH_SHORT).show()));
//
// Override the following in your Activity:
// @Override
// public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
// _permissionsHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
// }
//
public class PermissionsHelper {
private static final int REQUEST_CODE = 1289;
private List<RequestData> _ongoingPermissionData = new ArrayList<>();
public void requestPermission(Activity activity, RequestData requestData) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// No need to verify permissions
requestData.onPermissionGranted();
return;
}
_ongoingPermissionData.add(requestData);
String permission = requestData.getPermission();
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
if (requestData.hasPermissionExplanation()) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(activity)
.setMessage(requestData.getPermissionExplanation())
.setPositiveButton("Ok", (dialog, which) -> ActivityCompat.requestPermissions(activity, new String[]{permission}, REQUEST_CODE))
.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss())
.create().show();
}
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[]{permission}, REQUEST_CODE);
}
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode != REQUEST_CODE) return;
for (RequestData requestData : _ongoingPermissionData) {
for (int index = 0; index < permissions.length; index++) {
if (permissions[0].contentEquals(requestData.getPermission())) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
requestData.onPermissionGranted();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
requestData.onPermissionFailed();
}
_ongoingPermissionData.remove(requestData);
}
}
}
}
public static class RequestData {
private String permission;
private String permissionExplanation;
private Runnable onPermissionGranted;
private Runnable onPermissionFailed;
public RequestData(String permission, String permissionExplanation, Runnable onPermissionGranted) {
this.permission = permission;
this.permissionExplanation = permissionExplanation;
this.onPermissionGranted = onPermissionGranted;
}
public RequestData(String permission, String permissionExplanation, Runnable onPermissionGranted, Runnable onPermissionFailed) {
this.permission = permission;
this.permissionExplanation = permissionExplanation;
this.onPermissionGranted = onPermissionGranted;
this.onPermissionFailed = onPermissionFailed;
}
public String getPermission() {
return permission;
}
public boolean hasPermissionExplanation() {
return permissionExplanation != null;
}
public String getPermissionExplanation() {
return permissionExplanation;
}
public void onPermissionGranted() {
if (onPermissionGranted != null) onPermissionGranted.run();
}
public void onPermissionFailed() {
if (onPermissionFailed != null) onPermissionFailed.run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment