Skip to content

Commit

Permalink
refactor(java): for android examples, use ActivityCompat
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee authored Sep 26, 2023
1 parent b9c82bd commit cc56753
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class ReadStorePermissionManager(
private val activity: ComponentActivity,
Expand All @@ -18,20 +20,16 @@ class ReadStorePermissionManager(
}

private val hasPermission: Boolean
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.checkSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED
} else {
true
}
get() = ContextCompat.checkSelfPermission(
activity,
requiredPermission
) == PackageManager.PERMISSION_GRANTED

fun requestPermission() {
if (hasPermission) {
onGranted()
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
throw IllegalStateException("Permission should be granted")
}
if (activity.shouldShowRequestPermissionRationale(requiredPermission)) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requiredPermission)) {
onShowPermissionRationale(requiredPermission) {
requestPermission.launch(requiredPermission)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class ReadStorePermissionManager(
private val activity: ComponentActivity,
Expand All @@ -18,20 +20,16 @@ class ReadStorePermissionManager(
}

private val hasPermission: Boolean
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.checkSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED
} else {
true
}
get() = ContextCompat.checkSelfPermission(
activity,
requiredPermission
) == PackageManager.PERMISSION_GRANTED

fun requestPermission() {
if (hasPermission) {
onGranted()
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
throw IllegalStateException("Permission should be granted")
}
if (activity.shouldShowRequestPermissionRationale(requiredPermission)) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requiredPermission)) {
onShowPermissionRationale(requiredPermission) {
requestPermission.launch(requiredPermission)
}
Expand Down

0 comments on commit cc56753

Please sign in to comment.