Created
February 4, 2016 18:33
-
-
Save dGorod/c1cecb4fdaa3280873a6 to your computer and use it in GitHub Desktop.
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
| package com.exsoft.musarium.util; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.content.DialogInterface; | |
| import android.content.pm.PackageManager; | |
| import android.support.annotation.NonNull; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.support.v7.app.AlertDialog; | |
| import com.exsoft.musarium.R; | |
| /** | |
| * Helper class to clear Marshmallow permissions handling code a little bit. | |
| * Still requests results should be handled in appropriate methods: | |
| * {@link android.support.v4.app.FragmentActivity#onRequestPermissionsResult(int, String[], int[])} | |
| * {@link android.support.v4.app.Fragment#onRequestPermissionsResult(int, String[], int[])} | |
| * | |
| * Created by Dmitriy Gorodnytskiy on 15-Nov-15. | |
| */ | |
| public abstract class PermissionsUtil { | |
| public static boolean check(@NonNull Context context, @NonNull String permission) { | |
| return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; | |
| } | |
| public static void request(@NonNull Activity activity, @NonNull String permission, int requestCode) { | |
| ActivityCompat.requestPermissions(activity, new String[]{ permission }, requestCode); | |
| } | |
| public static boolean beg(@NonNull Activity activity, @NonNull String permission) { | |
| return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission); | |
| } | |
| public static void explain(@NonNull Context context, @NonNull String explanation) { | |
| explain(context, explanation, defaultListener); | |
| } | |
| /** | |
| * Shows default explaining dialog to ask user to grant us permission. | |
| * External util method, relates on app resources. | |
| * @param context | |
| * @param explanation string with explaining text | |
| * @param okListener handler for positive result | |
| */ | |
| public static void explain(@NonNull Context context, @NonNull String explanation, | |
| @NonNull DialogInterface.OnClickListener okListener) { | |
| AlertDialog.Builder b = new AlertDialog.Builder(context); | |
| b.setIcon(R.drawable.ic_alert_circle_white_36dp); | |
| b.setTitle(R.string.permission_request); | |
| b.setMessage(explanation); | |
| b.setPositiveButton(R.string.ok, okListener); | |
| b.setNegativeButton(R.string.cancel, defaultListener); | |
| b.create().show(); | |
| } | |
| private static DialogInterface.OnClickListener defaultListener = new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| dialog.dismiss(); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment