Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An approach for dealing with Marshmallow permissions
To use this:
1) Your activity must:
a) implement ActivityCompat.OnRequestPermissionsResultCallback
b) Have a `protected PermissionActivityHelper permissionActivityHelper = null;`
c) The implementation should be:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (permissionActivityHelper!=null){
permissionActivityHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
2) In the onCreate of your main activity where you would normally set up your UI you should do the following:
permissionActivityHelper = new PermissionActivityHelper(
this,
new String[]{
// List of permissions your app needs
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
},
// The string that describes why you need the permission
R.string.error_permission_rationale,
new Runnable() {
@Override
public void run() {
// Code that continues your app goes here
}
},
new Runnable() {
@Override
public void run() {
new Alerts(Alerts.Type.ERROR)
.withMessage(R.string.error_permission_denied)
.onDismiss(new Runnable() {
@Override
public void run() {
// If your app can't run w/o the permissions, just finish()
finish();
}
})
.show(AppActivity.this);
}
}
);
permissionActivityHelper.checkPermissions();
package base.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import app.R;
import retrofit.RetrofitError;
import retrofit.mime.TypedByteArray;
public class Alerts {
private final static int NO_ID = -1;
private final Type type;
private Runnable onDismissRunnable = null;
private RetrofitError retrofitError;
private int messageId = NO_ID;
private String message = null;
public enum Type {
ALERT(R.string.word_alert,NO_ID,R.string.word_ok),
ERROR(R.string.word_error,NO_ID,R.string.word_ok),
NETWORK_ERROR(R.string.word_alert,R.string.error_network,R.string.word_ok),
;
int titleId;
int messageId;
int buttonId;
Type(int titleId, int messageId, int buttonId){
this.titleId = titleId;
this.messageId = messageId;
this.buttonId = buttonId;
}
}
public Alerts(Type type){
this.type = type;
}
public Alerts onDismiss(Runnable runnable){
onDismissRunnable = runnable;
return this;
}
public Alerts withMessage(String message){
this.message = message;
return this;
}
public Alerts withRetrofitError(RetrofitError retrofitError){
this.retrofitError = retrofitError;
return this;
}
public Alerts withMessage(int messageId){
this.messageId = messageId;
return this;
}
public void show(Activity activity){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
// All types have titles and okd messages
builder.setTitle(type.titleId);
builder.setPositiveButton(type.buttonId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onDismissRunnable!=null)
onDismissRunnable.run();
}
});
String m = "";
if (message!=null)
m+=message;
else if (messageId!=NO_ID)
m+=activity.getString(messageId);
else if (type.messageId!=NO_ID)
m+=activity.getString(type.messageId);
else
throw new IllegalArgumentException("A message or message id must be supplied");
if (retrofitError!=null){
m+="\n"+new String(((TypedByteArray) retrofitError.getResponse().getBody()).getBytes());
}
builder.setMessage(m);
builder.show();
}
}
package base.module;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import base.util.Alerts;
public class PermissionActivityHelper {
private static final int PERMISSION_REQUEST = 0;
private final Activity activity;
private final String[] permissions;
private final int rationale;
private final Runnable onSuccess;
private final Runnable onFail;
public <T extends Activity & ActivityCompat.OnRequestPermissionsResultCallback> PermissionActivityHelper(
T activity,
String[] permissions,
int rationale,
Runnable onSuccess,
Runnable onFail
){
this.activity = activity;
this.permissions = permissions;
this.rationale = rationale;
this.onSuccess = onSuccess;
this.onFail = onFail;
}
public PermissionActivityHelper checkPermissions(){
if (ActivityCompat.checkSelfPermission(activity, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
onSuccess.run();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[0])) {
new Alerts(Alerts.Type.ALERT)
.withMessage(rationale)
.onDismiss(new Runnable() {
@Override
public void run() {
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST);
}
})
.show(activity);
} else {
ActivityCompat.requestPermissions(activity,permissions,PERMISSION_REQUEST);
}
}
return this;
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST) {
boolean valid = true;
for(int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
valid = false;
break;
}
}
if (valid){
onSuccess.run();
} else {
onFail.run();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment