Skip to content

Commit

Permalink
Merge pull request #272 from apivideo/refactor/android_use_activity_c…
Browse files Browse the repository at this point in the history
…ompat

refactor(java): for android examples, use ActivityCompat
  • Loading branch information
ThibaultBee authored Sep 26, 2023
2 parents 3c977a9 + 0ed96c6 commit eb98523
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 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
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 eb98523

Please sign in to comment.