Skip to content

Instantly share code, notes, and snippets.

@passiondroid
Last active February 26, 2021 05:51
Show Gist options
  • Save passiondroid/cd2ad32e7ba26f4e47ae to your computer and use it in GitHub Desktop.
Save passiondroid/cd2ad32e7ba26f4e47ae to your computer and use it in GitHub Desktop.
Android Runtime Permission Helper class
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import net.settings.manager.ErrorActivity;
import net.settings.manager.R;
import java.util.ArrayList;
public final class RuntimePermissionHelper {
private static RuntimePermissionHelper runtimePermissionHelper;
public static final int PERMISSION_REQUEST_CODE = 1;
private Activity activity;
public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private ArrayList<String> requiredPermissions;
private ArrayList<String> ungrantedPermissions = new ArrayList<String>();
public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
private RuntimePermissionHelper(Activity activity) {
this.activity = activity;
}
public static synchronized RuntimePermissionHelper getInstance(Activity activity){
if(runtimePermissionHelper == null) {
runtimePermissionHelper = new RuntimePermissionHelper(activity);
}
return runtimePermissionHelper;
}
private void initPermissions() {
requiredPermissions = new ArrayList<String>();
requiredPermissions.add(PERMISSION_ACCESS_FINE_LOCATION);
//Add all the required permission in the list
}
public void requestPermissionsIfDenied(){
ungrantedPermissions = getUnGrantedPermissionsList();
if(canShowPermissionRationaleDialog()){
showMessageOKCancel(activity.getResources().getString(R.string.permission_message),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
askPermissions();
}
});
return;
}
askPermissions();
}
public void requestPermissionsIfDenied(final String permission){
if(canShowPermissionRationaleDialog(permission)){
showMessageOKCancel(activity.getResources().getString(R.string.permission_message),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
askPermission(permission);
}
});
return;
}
askPermission(permission);
}
public void setActivity(Activity activity) {
this.activity = activity;
}
public boolean canShowPermissionRationaleDialog() {
boolean shouldShowRationale = false;
for(String permission: ungrantedPermissions) {
boolean shouldShow = ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
if(shouldShow) {
shouldShowRationale = true;
}
}
return shouldShowRationale;
}
public boolean canShowPermissionRationaleDialog(String permission) {
boolean shouldShowRationale = false;
boolean shouldShow = ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
if(shouldShow) {
shouldShowRationale = true;
}
return shouldShowRationale;
}
private void askPermissions() {
if(ungrantedPermissions.size()>0) {
ActivityCompat.requestPermissions(activity, ungrantedPermissions.toArray(new String[ungrantedPermissions.size()]), PERMISSION_REQUEST_CODE);
}
}
private void askPermission(String permission) {
ActivityCompat.requestPermissions(activity, new String[] {permission}, PERMISSION_REQUEST_CODE);
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(activity)
.setMessage(message)
.setPositiveButton(R.string.ok, okListener)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(activity,ErrorActivity.class);
intent.putExtra("permissions_denied",true);
activity.startActivity(intent);
activity.finish();
}
})
.create()
.show();
}
public boolean isAllPermissionAvailable() {
boolean isAllPermissionAvailable = true;
initPermissions();
for(String permission : requiredPermissions){
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED){
isAllPermissionAvailable = false;
break;
}
}
return isAllPermissionAvailable;
}
public ArrayList<String> getUnGrantedPermissionsList() {
ArrayList<String> list = new ArrayList<String>();
for(String permission: requiredPermissions) {
int result = ActivityCompat.checkSelfPermission(activity, permission);
if(result != PackageManager.PERMISSION_GRANTED) {
list.add(permission);
}
}
return list;
}
public boolean isPermissionAvailable(String permission) {
boolean isPermissionAvailable = true;
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED){
isPermissionAvailable = false;
}
return isPermissionAvailable;
}
}
@mage1k99
Copy link

Do not place Android context classes in static fields (static reference to RuntimePermissionHelper which has field activity pointing to Activity); this is a memory leak

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