Skip to content

Instantly share code, notes, and snippets.

@Grishu
Created March 17, 2016 11:37
Show Gist options
  • Save Grishu/15a8158fae63f91890f3 to your computer and use it in GitHub Desktop.
Save Grishu/15a8158fae63f91890f3 to your computer and use it in GitHub Desktop.
Android Library to help you with your runtime Permissions.
package com.reigndesign.falabella_seguros.library;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import com.orhanobut.logger.Logger;
/**
* Created by tuo on 12/11/15.
*/
public class PermissionHelper {
public interface PermissionAffirmativeCallback
{
public void onPermissionConfirmed();
}
private static final int PERMISSIONS_REQUEST_LOCATION = 200;
private static final int PERMISSIONS_REQUEST_CALL_PHONE = 201;
private PermissionAffirmativeCallback mAffirmativeCallback;
private Activity mActivity;
//parameters
private String mManifestPersmission;
private int mRequestCode;
private String mDeniedMsg;
private String mDeniedNeverAskTitle;
private String mDeniedNeverAskMsg;
public static PermissionHelper permissionHelper(PermissionType type,
Activity activity,
PermissionAffirmativeCallback callback){
return new PermissionHelper(type, activity, callback);
}
public PermissionHelper(PermissionType type, Activity activity, PermissionAffirmativeCallback callback) {
if(type == PermissionType.LOCATION){
mManifestPersmission = Manifest.permission.ACCESS_FINE_LOCATION;
mRequestCode = PERMISSIONS_REQUEST_LOCATION;
mDeniedMsg = "Without this permission the app is unable to find your location.Are you sure you want to deny this permission?";
mDeniedNeverAskTitle = "Unable to locate your position";
mDeniedNeverAskMsg = "You have denied the permission for location access. Please go to app settings and allow permission";
}else if(type == PermissionType.CALL){
mManifestPersmission = Manifest.permission.CALL_PHONE;
mRequestCode = PERMISSIONS_REQUEST_CALL_PHONE;
mDeniedMsg = "Without this permission the app is unable to make call.Are you sure you want to deny this permission?";
mDeniedNeverAskTitle = "Unable to make call";
mDeniedNeverAskMsg = "You have denied the permission for calling.. Please go to app settings and allow permission";
}
this.mActivity = activity;
this.mAffirmativeCallback = callback;
checkPermission();
}
private void checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permerssion = ActivityCompat.checkSelfPermission(mActivity, mManifestPersmission);
Logger.t(mManifestPersmission);
Logger.d("permission: %d", permerssion);
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission);
Logger.d("shouldShowRequestPermissionRationale: %s", String.valueOf(should));
if (permerssion != PackageManager.PERMISSION_GRANTED) {
if (should) {
// No explanation needed, we can request the permission.
Logger.d("should show raltionale, but here now, just promopt ask again");
//request window
requestPermission();
} else {
//TWO CASE:
//1. first time - system up - //request window
if(!PrefUtils.hasLocationPermissionBeenRequested(mActivity)){
PrefUtils.markLocationPermissionBeenRequested(mActivity, true);
Logger.d("rationale flase, but first time, so we request window");
requestPermission();
}else{
//2. second time - user denied with never ask - go to settings
Logger.d("user denied with never ask again, need show settings choose");
promptSettings();
}
}
return;
}
}
if(this.mAffirmativeCallback != null){
this.mAffirmativeCallback.onPermissionConfirmed();
}
}
private void requestPermission() {
ActivityCompat.requestPermissions(mActivity, new String[]{mManifestPersmission}, mRequestCode);
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if(requestCode == mRequestCode){
Logger.t(mManifestPersmission);
boolean hasSth = grantResults.length > 0;
if(hasSth){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//user accepted , make call
Logger.d("Permission granted");
if(this.mAffirmativeCallback != null){
this.mAffirmativeCallback.onPermissionConfirmed();
}
} else if(grantResults[0] == PackageManager.PERMISSION_DENIED) {
//http://stackoverflow.com/questions/30719047/android-m-check-runtime-permission-how-to-determine-if-the-user-checked-nev
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission);
Logger.d("onRequestPermissionsResult - shouldShowRequestPermissionRationale: %s", String.valueOf(should));
if(should){
//true,
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Permission Denied");
builder.setMessage(mDeniedMsg);
builder.setPositiveButton("I'M SURE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("RE-TRY", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
requestPermission();
}
});
builder.show();
}else{
promptSettings();
}
}
}
}
}
private void promptSettings() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, R.style.AppCompatAlertDialogStyle);
builder.setTitle(mDeniedNeverAskTitle);
builder.setMessage(mDeniedNeverAskMsg);
builder.setPositiveButton("go to Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
goToSettings();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
private void goToSettings() {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + mActivity.getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity.startActivity(myAppSettings);
}
}
@nyamwaya
Copy link

whats the PrefUtils class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment