From 88d917b66264aa2f0f49f4ae5c4e0d08ca83a1d6 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sun, 1 Mar 2020 13:41:27 +0100 Subject: [PATCH] Added permission check for installing/downloading modules --- .../events/InstallExternalModuleEvent.kt | 13 +++++------ .../magisk/ui/module/ModuleViewModel.kt | 22 +++++++++++++++++-- app/src/main/res/values/strings.xml | 1 + 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt index 308ac927d..e4a955ecf 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt @@ -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 { diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt index ce359abb4..64224380f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt @@ -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() + } + } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e1b68de7..715a80d79 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -136,6 +136,7 @@ Update Available @string/home_installed_version Sorting Order + Grant storage permission to enable this functionality Theme Mode