Created
January 6, 2018 02:36
-
-
Save talenguyen/4271b80626680183ca5246fcd5766f40 to your computer and use it in GitHub Desktop.
Runtime permission Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.pm.PackageManager; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v7.app.AlertDialog; | |
public class PermissionRequest { | |
private static int RC = 1; | |
private final String permission; | |
@Nullable | |
private String rationalExplanation; | |
private Runnable onGranted; | |
private Runnable onRejected; | |
private int requestCode; | |
private PermissionRequest(String permission) { | |
this.permission = permission; | |
requestCode = ++RC; | |
} | |
public static PermissionRequest of(String permission) { | |
return new PermissionRequest(permission); | |
} | |
public PermissionRequest rationalExplanation(String rationalExplanation) { | |
this.rationalExplanation = rationalExplanation; | |
return this; | |
} | |
public PermissionRequest onGranted(Runnable onGranted) { | |
this.onGranted = onGranted; | |
return this; | |
} | |
public PermissionRequest onRejected(Runnable onRejected) { | |
this.onRejected = onRejected; | |
return this; | |
} | |
public void send(Activity activity) { | |
if (ActivityCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) { | |
postGranted(); | |
} else { | |
requestPermission(activity); | |
} | |
} | |
@SuppressWarnings("unused") | |
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { | |
if (requestCode == this.requestCode) { | |
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
postGranted(); | |
} else { | |
postRejected(); | |
} | |
} | |
} | |
private void requestPermission(Activity activity) { | |
if (ActivityCompat.shouldShowRequestPermissionRationale( | |
activity, | |
permission)) { | |
if (rationalExplanation == null) { | |
postRejected(); | |
} else { | |
new AlertDialog.Builder(activity) | |
.setMessage(rationalExplanation) | |
.setPositiveButton("OK", (dialog, which) -> { | |
// Contact permissions have not been granted yet. Request them directly. | |
ActivityCompat.requestPermissions( | |
activity, | |
new String[]{permission}, | |
requestCode); | |
}) | |
.show(); | |
} | |
} else { | |
ActivityCompat.requestPermissions( | |
activity, | |
new String[]{permission}, | |
requestCode); | |
} | |
} | |
private void postGranted() { | |
if (onGranted != null) { | |
onGranted.run(); | |
} | |
} | |
private void postRejected() { | |
if (onRejected != null) { | |
onRejected.run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment