diff --git a/app/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt index cad2d2faa..98f4fe809 100644 --- a/app/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt @@ -82,16 +82,12 @@ abstract class BaseViewModel( ViewActionEvent(action).publish() } - fun withPermissions(vararg permissions: String, callback: (Boolean) -> Unit) { - PermissionEvent(permissions.toList(), callback).publish() + fun withPermission(permission: String, callback: (Boolean) -> Unit) { + PermissionEvent(permission, callback).publish() } fun withExternalRW(callback: () -> Unit) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - callback() - return - } - withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE) { + withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) { if (!it) { SnackbarEvent(R.string.external_rw_permission_denied).publish() } else { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt b/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt index 2f83a6b44..1e50a737f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.res.Configuration +import android.os.Build import androidx.appcompat.app.AppCompatActivity import androidx.collection.SparseArrayCompat import androidx.core.app.ActivityCompat @@ -30,13 +31,17 @@ abstract class BaseActivity : AppCompatActivity() { 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 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() } else { val requestCode = Random.nextInt(256, 512) @@ -46,12 +51,12 @@ abstract class BaseActivity : AppCompatActivity() { else request.onFailure() } - ActivityCompat.requestPermissions(this, ungranted.toTypedArray(), requestCode) + ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode) } } fun withExternalRW(builder: PermissionRequestBuilder.() -> Unit) { - withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, builder = builder) + withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, builder = builder) } override fun onRequestPermissionsResult( diff --git a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt index 69fbad00b..bc3e8e27e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt @@ -24,12 +24,12 @@ class OpenChangelogEvent(val item: Repo) : ViewEventWithScope(), ContextExecutor } class PermissionEvent( - private val permissions: List, + private val permission: String, private val callback: (Boolean) -> Unit ) : ViewEvent(), ActivityExecutor { override fun invoke(activity: BaseUIActivity<*, *>) = - activity.withPermissions(*permissions.toTypedArray()) { + activity.withPermission(permission) { onSuccess { callback(true) }