Last active
May 6, 2016 14:25
-
-
Save Tufan21/1c5f633ce62f04ed5f1c596924be1e41 to your computer and use it in GitHub Desktop.
Marshmellow runtime permission jeneric class
This file contains 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.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; | |
import com.webplat.tprecharge.R; | |
/** | |
* Created by tufan 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; | |
public static final int READ_CONTACTS_PERMISSIONS_REQUEST = 202; | |
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(int type, | |
Activity activity, | |
PermissionAffirmativeCallback callback){ | |
return new PermissionHelper(type, activity, callback); | |
} | |
public PermissionHelper(int type, Activity activity, PermissionAffirmativeCallback callback) { | |
if(type == PermissionType.PERMISSIONS_REQUEST_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.PERMISSIONS_REQUEST_CALL_PHONE){ | |
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"; | |
} | |
else if(type == PermissionType.READ_CONTACTS_PERMISSIONS_REQUEST){ | |
mManifestPersmission = Manifest.permission.READ_CONTACTS; | |
mRequestCode = READ_CONTACTS_PERMISSIONS_REQUEST; | |
mDeniedMsg = "Without this permission the app is unable to read Contact.Are you sure you want to deny this permission?"; | |
mDeniedNeverAskTitle = "Unable to read Contact"; | |
mDeniedNeverAskMsg = "You have denied the permission for reading Contact.. 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); | |
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission); | |
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); | |
requestPermission(); | |
}else{ | |
//2. second time - user denied with never ask - go to settings | |
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); | |
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); | |
} | |
} | |
public class PermissionType { | |
public static final int READ_CONTACTS_PERMISSIONS_REQUEST = 202; | |
public static final int PERMISSIONS_REQUEST_LOCATION = 200; | |
public static final int PERMISSIONS_REQUEST_CALL_PHONE = 201; | |
} | |
//calling from activity | |
PermissionHelper permissionHelper = PermissionHelper.permissionHelper(PermissionType.READ_CONTACTS_PERMISSIONS_REQUEST, getActivity(), | |
new PermissionHelper.PermissionAffirmativeCallback() { | |
@Override | |
public void onPermissionConfirmed() { | |
Intent it = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); | |
startActivityForResult(it, CONTACTPICK); | |
} | |
}); | |
mPermissionHelpers.add(permissionHelper); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment