Last active
December 7, 2020 11:28
Helper class for runtime permission of Android M
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.annotation.TargetApi; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.os.Build; | |
import android.support.annotation.NonNull; | |
import static android.content.pm.PackageManager.*; | |
import static android.content.pm.PackageManager.PERMISSION_GRANTED; | |
/** | |
* Helper class for runtime permission of Android M.<br> | |
* Created based on the information of <b>M Preview2</b>. | |
* <p> | |
* Reference: <a href="https://github.com/googlesamples/android-RuntimePermissions">Android RuntimePermissions Sample</a> | |
* </p> | |
* | |
* @author kumagai | |
* | |
*/ | |
public final class Permission { | |
private static final String MNC = "MNC"; | |
// Calendar group. | |
public static final String READ_CALENDAR = Manifest.permission.READ_CALENDAR; | |
public static final String WRITE_CALENDAR = Manifest.permission.WRITE_CALENDAR; | |
// Camera group. | |
public static final String CAMERA = Manifest.permission.CAMERA; | |
// Contacts group. | |
public static final String READ_CONTACTS = Manifest.permission.READ_CONTACTS; | |
public static final String WRITE_CONTACTS = Manifest.permission.WRITE_CONTACTS; | |
@Deprecated | |
public static final String WRITE_PROFILE = Manifest.permission.WRITE_PROFILE; | |
@Deprecated | |
public static final String READ_PROFILE = Manifest.permission.READ_PROFILE; | |
// Location group. | |
public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; | |
public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION; | |
// Microphone group. | |
public static final String RECORD_AUDIO = Manifest.permission.RECORD_AUDIO; | |
// Phone group. | |
public static final String READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE; | |
public static final String CALL_PHONE = Manifest.permission.CALL_PHONE; | |
public static final String READ_CALL_LOG = Manifest.permission.READ_CALL_LOG; | |
public static final String WRITE_CALL_LOG = Manifest.permission.WRITE_CALL_LOG; | |
public static final String ADD_VOICEMAIL = Manifest.permission.ADD_VOICEMAIL; | |
public static final String USE_SIP = Manifest.permission.USE_SIP; | |
public static final String PROCESS_OUTGOING_CALLS = Manifest.permission.PROCESS_OUTGOING_CALLS; | |
// Sensors group. | |
public static final String BODY_SENSORS = Manifest.permission.BODY_SENSORS; | |
public static final String USE_FINGERPRINT = Manifest.permission.USE_FINGERPRINT; | |
// SMS group. | |
public static final String SEND_SMS = Manifest.permission.SEND_SMS; | |
public static final String RECEIVE_SMS = Manifest.permission.RECEIVE_SMS; | |
public static final String READ_SMS = Manifest.permission.READ_SMS; | |
public static final String RECEIVE_WAP_PUSH = Manifest.permission.RECEIVE_WAP_PUSH; | |
public static final String RECEIVE_MMS = Manifest.permission.RECEIVE_MMS; | |
public static final String READ_CELL_BROADCASTS = "android.permission.READ_CELL_BROADCASTS"; | |
// Bookmarks group. | |
public static final String READ_HISTORY_BOOKMARKS = "com.android.browser.permission.READ_HISTORY_BOOKMARKS"; | |
public static final String WRITE_HISTORY_BOOKMARKS = "com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"; | |
// Social Info group. | |
@Deprecated | |
public static final String WRITE_SOCIAL_STREAM = Manifest.permission.WRITE_SOCIAL_STREAM; | |
// User Dictionary group. | |
public static final String READ_USER_DICTIONARY = Manifest.permission.READ_USER_DICTIONARY; | |
public static final String WRITE_USER_DICTIONARY = Manifest.permission.WRITE_USER_DICTIONARY; | |
/** | |
* Create an array from a given permissions. | |
* | |
* @throws IllegalArgumentException | |
*/ | |
public static String[] asArray(@NonNull String... permissions) { | |
if (permissions.length == 0) { | |
throw new IllegalArgumentException("There is no given permission"); | |
} | |
final String[] dest = new String[permissions.length]; | |
for (int i = 0, len = permissions.length; i < len; i++) { | |
dest[i] = permissions[i]; | |
} | |
return dest; | |
} | |
/** | |
* Check that given permission have been granted. | |
*/ | |
public static boolean hasGranted(int grantResult) { | |
return grantResult == PERMISSION_GRANTED; | |
} | |
/** | |
* Check that all given permissions have been granted by verifying that each entry in the | |
* given array is of the value {@link PackageManager#PERMISSION_GRANTED}. | |
*/ | |
public static boolean hasGranted(int[] grantResults) { | |
for (int result : grantResults) { | |
if (!hasGranted(result)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Returns true if the Context has access to a given permission. | |
* Always returns true on platforms below M. | |
*/ | |
public static boolean hasSelfPermission(Context context, String permission) { | |
if (isMNC()) { | |
return permissionHasGranted(context, permission); | |
} | |
return true; | |
} | |
/** | |
* Returns true if the Context has access to all given permissions. | |
* Always returns true on platforms below M. | |
*/ | |
public static boolean hasSelfPermissions(Context context, String[] permissions) { | |
if (!isMNC()) { | |
return true; | |
} | |
for (String permission : permissions) { | |
if (!permissionHasGranted(context, permission)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Requests permissions to be granted to this application. | |
*/ | |
public static void requestAllPermissions(@NonNull Activity activity, @NonNull String[] permissions, int requestCode) { | |
if (isMNC()) { | |
internalRequestPermissions(activity, permissions, requestCode); | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.MNC) | |
private static void internalRequestPermissions(Activity activity, String[] permissions, int requestCode) { | |
if (activity == null) { | |
throw new IllegalArgumentException("Given activity is null."); | |
} | |
activity.requestPermissions(permissions, requestCode); | |
} | |
@TargetApi(Build.VERSION_CODES.MNC) | |
private static boolean permissionHasGranted(Context context, String permission) { | |
return hasGranted(context.checkSelfPermission(permission)); | |
} | |
private static boolean isMNC() { | |
return MNC.equals(Build.VERSION.CODENAME); | |
} | |
} |
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
public fun Context.hasSelfPermission(permission: String): Boolean = Permission.hasSelfPermission(this, permission) | |
public fun Context.hasSelfPermissions(permissions: Array<String>): Boolean = Permission.hasSelfPermissions(this, permissions) | |
public class TestActivity: Activity() { | |
companion object { | |
val REQUEST_CODE = 0 | |
} | |
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>?, grantResults: IntArray?) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
if (requestCode != REQUEST_CODE) { | |
return; | |
} | |
if (hasSelfPermissions(arrayOf(Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION))) { | |
} | |
// if (hasSelfPermission(Permission.ACCESS_COARSE_LOCATION)) { | |
// } | |
} | |
private fun requestPermission() { | |
requestPermissions(arrayOf(Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION), REQUEST_CODE) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
private static boolean isMNC() {
return (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M);
}