-
-
Save yusuf-murodov/6288ee5323d417775b96345f956b1e90 to your computer and use it in GitHub Desktop.
Android Permission Utility class. A collection of static convenience methods for checking if Android Runtime permissions are required.
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.app.Activity; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.preference.PreferenceManager; | |
import android.provider.Settings; | |
import android.support.v4.app.Fragment; | |
import java.util.List; | |
public class PermissionUtils { | |
public static boolean useRunTimePermissions() { | |
return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; | |
} | |
public static boolean hasPermission(Activity activity, String permission) { | |
if (useRunTimePermissions()) { | |
return activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED; | |
} | |
return true; | |
} | |
public static void requestPermissions(Activity activity, String[] permission, int requestCode) { | |
if (useRunTimePermissions()) { | |
activity.requestPermissions(permission, requestCode); | |
} | |
} | |
public static void requestPermissions(Fragment fragment, String[] permission, int requestCode) { | |
if (useRunTimePermissions()) { | |
fragment.requestPermissions(permission, requestCode); | |
} | |
} | |
public static boolean shouldShowRational(Activity activity, String permission) { | |
if (useRunTimePermissions()) { | |
return activity.shouldShowRequestPermissionRationale(permission); | |
} | |
return false; | |
} | |
public static boolean shouldAskForPermission(Activity activity, String permission) { | |
if (useRunTimePermissions()) { | |
return !hasPermission(activity, permission) && | |
(!hasAskedForPermission(activity, permission) || | |
shouldShowRational(activity, permission)); | |
} | |
return false; | |
} | |
public static void goToAppSettings(Activity activity) { | |
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, | |
Uri.fromParts("package", activity.getPackageName(), null)); | |
intent.addCategory(Intent.CATEGORY_DEFAULT); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
activity.startActivity(intent); | |
} | |
public static boolean hasAskedForPermission(Activity activity, String permission) { | |
return PreferenceManager | |
.getDefaultSharedPreferences(activity) | |
.getBoolean(permission, false); | |
} | |
public static void markedPermissionAsAsked(Activity activity, String permission) { | |
PreferenceManager | |
.getDefaultSharedPreferences(activity) | |
.edit() | |
.putBoolean(permission, true) | |
.apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment