Added permission check for installing/downloading modules

This commit is contained in:
Viktor De Pasquale 2020-03-01 13:41:27 +01:00 committed by John Wu
parent faf077b494
commit 88d917b662
3 changed files with 27 additions and 9 deletions

View File

@ -1,8 +1,10 @@
package com.topjohnwu.magisk.model.events
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.annotation.RequiresPermission
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.core.intent
@ -10,14 +12,11 @@ import com.topjohnwu.magisk.legacy.flash.FlashActivity
class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor {
@RequiresPermission(allOf = [Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE])
override fun invoke(activity: BaseActivity) {
activity.withExternalRW {
onSuccess {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "application/zip"
activity.startActivityForResult(intent, Const.ID.FETCH_ZIP)
}
}
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "application/zip"
activity.startActivityForResult(intent, Const.ID.FETCH_ZIP)
}
companion object {

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.ui.module
import android.Manifest
import androidx.annotation.WorkerThread
import androidx.databinding.Bindable
import androidx.databinding.ObservableArrayList
@ -20,6 +21,7 @@ import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
import com.topjohnwu.magisk.model.entity.recycler.*
import com.topjohnwu.magisk.model.events.InstallExternalModuleEvent
import com.topjohnwu.magisk.model.events.OpenChangelogEvent
import com.topjohnwu.magisk.model.events.SnackbarEvent
import com.topjohnwu.magisk.model.events.dialog.ModuleInstallDialog
import com.topjohnwu.magisk.ui.base.*
import com.topjohnwu.magisk.utils.EndlessRecyclerScrollListener
@ -330,11 +332,27 @@ class ModuleViewModel(
else -> Unit
}
fun downloadPressed(item: RepoItem) = ModuleInstallDialog(item.item).publish()
fun installPressed() = InstallExternalModuleEvent().publish()
fun downloadPressed(item: RepoItem) = withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
).any { it }.subscribeK(onError = { permissionDenied() }) {
ModuleInstallDialog(item.item).publish()
}.add()
fun installPressed() = withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
).any { it }.subscribeK(onError = { permissionDenied() }) {
InstallExternalModuleEvent().publish()
}.add()
fun infoPressed(item: RepoItem) = OpenChangelogEvent(item.item).publish()
fun infoPressed(item: ModuleItem) {
OpenChangelogEvent(item.repo ?: return).publish()
}
private fun permissionDenied() {
SnackbarEvent(R.string.module_permission_declined).publish()
}
}

View File

@ -136,6 +136,7 @@
<string name="update_available">Update Available</string>
<string name="module_installed">@string/home_installed_version</string>
<string name="sorting_order">Sorting Order</string>
<string name="module_permission_declined">Grant storage permission to enable this functionality</string>
<!--Settings -->
<string name="settings_dark_mode_title">Theme Mode</string>