Bypass external rw check in the proper location

This commit is contained in:
topjohnwu 2020-08-22 05:27:30 -07:00
parent 14a2f63b8b
commit fb04e32480
3 changed files with 16 additions and 15 deletions

View File

@ -82,16 +82,12 @@ abstract class BaseViewModel(
ViewActionEvent(action).publish() ViewActionEvent(action).publish()
} }
fun withPermissions(vararg permissions: String, callback: (Boolean) -> Unit) { fun withPermission(permission: String, callback: (Boolean) -> Unit) {
PermissionEvent(permissions.toList(), callback).publish() PermissionEvent(permission, callback).publish()
} }
fun withExternalRW(callback: () -> Unit) { fun withExternalRW(callback: () -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) {
callback()
return
}
withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE) {
if (!it) { if (!it) {
SnackbarEvent(R.string.external_rw_permission_denied).publish() SnackbarEvent(R.string.external_rw_permission_denied).publish()
} else { } else {

View File

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Build
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.collection.SparseArrayCompat import androidx.collection.SparseArrayCompat
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
@ -30,13 +31,17 @@ abstract class BaseActivity : AppCompatActivity() {
super.attachBaseContext(base.wrap(false)) super.attachBaseContext(base.wrap(false))
} }
fun withPermissions(vararg permissions: String, builder: PermissionRequestBuilder.() -> Unit) { fun withPermission(permission: String, builder: PermissionRequestBuilder.() -> Unit) {
val request = PermissionRequestBuilder().apply(builder).build() val request = PermissionRequestBuilder().apply(builder).build()
val ungranted = permissions.filter {
ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED if (permission == Manifest.permission.WRITE_EXTERNAL_STORAGE &&
Build.VERSION.SDK_INT >= 29) {
// We do not need external rw on 29+
request.onSuccess()
return
} }
if (ungranted.isEmpty()) { if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
request.onSuccess() request.onSuccess()
} else { } else {
val requestCode = Random.nextInt(256, 512) val requestCode = Random.nextInt(256, 512)
@ -46,12 +51,12 @@ abstract class BaseActivity : AppCompatActivity() {
else else
request.onFailure() request.onFailure()
} }
ActivityCompat.requestPermissions(this, ungranted.toTypedArray(), requestCode) ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
} }
} }
fun withExternalRW(builder: PermissionRequestBuilder.() -> Unit) { fun withExternalRW(builder: PermissionRequestBuilder.() -> Unit) {
withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, builder = builder) withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, builder = builder)
} }
override fun onRequestPermissionsResult( override fun onRequestPermissionsResult(

View File

@ -24,12 +24,12 @@ class OpenChangelogEvent(val item: Repo) : ViewEventWithScope(), ContextExecutor
} }
class PermissionEvent( class PermissionEvent(
private val permissions: List<String>, private val permission: String,
private val callback: (Boolean) -> Unit private val callback: (Boolean) -> Unit
) : ViewEvent(), ActivityExecutor { ) : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseUIActivity<*, *>) = override fun invoke(activity: BaseUIActivity<*, *>) =
activity.withPermissions(*permissions.toTypedArray()) { activity.withPermission(permission) {
onSuccess { onSuccess {
callback(true) callback(true)
} }