Before 2022/Android

퍼미션 Enum으로 관리하기

Eljoe 2019. 3. 13. 14:04
Enum 선언(Kotlin)
enum class PermissionType(val permissionValue: String) {
	READ_PHONE_STATE(Manifest.permission.READ_PHONE_STATE), // 전화
	WRITE_EXTERNAL_STORAGE(Manifest.permission.WRITE_EXTERNAL_STORAGE), // 저장소
	RECORD_AUDIO(Manifest.permission.RECORD_AUDIO); // 녹음

	// 퍼미션 체크
	fun permissionCheck(context: Context) = 
		ContextCompat.checkSelfPermission(context, permissionValue) == PackageManager.PERMISSION_GRANTED
}

Enum 사용 예시(Java)
private boolean permissionCheck(Context context) {
	for (PermissionType permissionType : PermissionType.values()) {
		if (!permissionType.permissionCheck(context)) {
			return false;
		}
	}
	return true;
}