From 1df152f41b7b96bba82021a1979e2c0f50f46eba Mon Sep 17 00:00:00 2001 From: wingio Date: Sat, 25 Mar 2023 14:26:31 -0400 Subject: [PATCH] Refactor: Move keystore to external dir --- app/build.gradle.kts | 1 + .../manager/installer/util/Patcher.kt | 1 - .../utils => installer/util}/Signer.kt | 11 ++- .../manager/ui/screen/main/MainScreen.kt | 3 + .../viewmodel/installer/InstallerViewModel.kt | 2 +- .../dialog/StoragePermissionsDialog.kt | 86 +++++++++++++++++++ .../vendetta/manager/utils/Constants.kt | 4 + app/src/main/res/values/strings.xml | 3 + 8 files changed, 107 insertions(+), 4 deletions(-) rename app/src/main/java/dev/beefers/vendetta/manager/{network/utils => installer/util}/Signer.kt (90%) create mode 100644 app/src/main/java/dev/beefers/vendetta/manager/ui/widgets/dialog/StoragePermissionsDialog.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7c3f2e9..0c45f1f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -101,6 +101,7 @@ dependencies { implementation("com.google.accompanist:accompanist-systemuicontroller:$accompanistVersion") implementation("com.google.accompanist:accompanist-pager:$accompanistVersion") + implementation("com.google.accompanist:accompanist-permissions:$accompanistVersion") val voyagerVersion = "1.0.0-rc03" diff --git a/app/src/main/java/dev/beefers/vendetta/manager/installer/util/Patcher.kt b/app/src/main/java/dev/beefers/vendetta/manager/installer/util/Patcher.kt index 9cc83a5..078244e 100644 --- a/app/src/main/java/dev/beefers/vendetta/manager/installer/util/Patcher.kt +++ b/app/src/main/java/dev/beefers/vendetta/manager/installer/util/Patcher.kt @@ -1,6 +1,5 @@ package dev.beefers.vendetta.manager.installer.util -import dev.beefers.vendetta.manager.network.utils.Signer import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.lsposed.lspatch.share.PatchConfig diff --git a/app/src/main/java/dev/beefers/vendetta/manager/network/utils/Signer.kt b/app/src/main/java/dev/beefers/vendetta/manager/installer/util/Signer.kt similarity index 90% rename from app/src/main/java/dev/beefers/vendetta/manager/network/utils/Signer.kt rename to app/src/main/java/dev/beefers/vendetta/manager/installer/util/Signer.kt index a5b1381..94f3f1f 100644 --- a/app/src/main/java/dev/beefers/vendetta/manager/network/utils/Signer.kt +++ b/app/src/main/java/dev/beefers/vendetta/manager/installer/util/Signer.kt @@ -1,7 +1,8 @@ -package dev.beefers.vendetta.manager.network.utils +package dev.beefers.vendetta.manager.installer.util import android.content.Context import com.android.apksig.ApkSigner +import dev.beefers.vendetta.manager.utils.Constants import org.bouncycastle.asn1.x500.X500Name import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo import org.bouncycastle.cert.X509v3CertificateBuilder @@ -28,8 +29,14 @@ object Signer : KoinComponent { get() { lateinit var ks: File cacheDir.resolve("ks.keystore").also { + if (it.exists()) { + it.copyTo(Constants.VENDETTA_DIR.resolve("ks.keystore"), true) + it.delete() + } + } + Constants.VENDETTA_DIR.resolve("ks.keystore").also { if (!it.exists()) { - cacheDir.mkdir() + Constants.VENDETTA_DIR.mkdir() newKeystore(it) } ks = it diff --git a/app/src/main/java/dev/beefers/vendetta/manager/ui/screen/main/MainScreen.kt b/app/src/main/java/dev/beefers/vendetta/manager/ui/screen/main/MainScreen.kt index e5653b8..8f517ac 100644 --- a/app/src/main/java/dev/beefers/vendetta/manager/ui/screen/main/MainScreen.kt +++ b/app/src/main/java/dev/beefers/vendetta/manager/ui/screen/main/MainScreen.kt @@ -25,6 +25,7 @@ import com.google.accompanist.pager.PagerState import com.google.accompanist.pager.rememberPagerState import dev.beefers.vendetta.manager.ui.screen.home.HomeScreen import dev.beefers.vendetta.manager.ui.viewmodel.main.MainViewModel +import dev.beefers.vendetta.manager.ui.widgets.dialog.StoragePermissionsDialog import dev.beefers.vendetta.manager.ui.widgets.updater.UpdateDialog import dev.beefers.vendetta.manager.utils.MainTab import kotlinx.coroutines.launch @@ -38,6 +39,8 @@ class MainScreen : Screen { val viewModel: MainViewModel = getScreenModel() val pagerState = rememberPagerState() + StoragePermissionsDialog() + CompositionLocalProvider( LocalPagerState provides pagerState ) { diff --git a/app/src/main/java/dev/beefers/vendetta/manager/ui/viewmodel/installer/InstallerViewModel.kt b/app/src/main/java/dev/beefers/vendetta/manager/ui/viewmodel/installer/InstallerViewModel.kt index fdb7c7a..da541ae 100644 --- a/app/src/main/java/dev/beefers/vendetta/manager/ui/viewmodel/installer/InstallerViewModel.kt +++ b/app/src/main/java/dev/beefers/vendetta/manager/ui/viewmodel/installer/InstallerViewModel.kt @@ -23,7 +23,7 @@ import dev.beefers.vendetta.manager.domain.manager.PreferenceManager import dev.beefers.vendetta.manager.installer.util.ManifestPatcher import dev.beefers.vendetta.manager.installer.util.Patcher import dev.beefers.vendetta.manager.installer.util.installApks -import dev.beefers.vendetta.manager.network.utils.Signer +import dev.beefers.vendetta.manager.installer.util.Signer import dev.beefers.vendetta.manager.utils.DiscordVersion import dev.beefers.vendetta.manager.utils.copyText import dev.beefers.vendetta.manager.utils.showToast diff --git a/app/src/main/java/dev/beefers/vendetta/manager/ui/widgets/dialog/StoragePermissionsDialog.kt b/app/src/main/java/dev/beefers/vendetta/manager/ui/widgets/dialog/StoragePermissionsDialog.kt new file mode 100644 index 0000000..97392ef --- /dev/null +++ b/app/src/main/java/dev/beefers/vendetta/manager/ui/widgets/dialog/StoragePermissionsDialog.kt @@ -0,0 +1,86 @@ +package dev.beefers.vendetta.manager.ui.widgets.dialog + +import android.Manifest +import android.annotation.SuppressLint +import android.content.Intent +import android.os.Build +import android.os.Environment +import android.provider.Settings +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.window.DialogProperties +import androidx.core.net.toUri +import com.google.accompanist.permissions.* +import dev.beefers.vendetta.manager.BuildConfig +import dev.beefers.vendetta.manager.R + +@Composable +fun StoragePermissionsDialog() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + ManageStorageDialog() + } else { + ExternalStorageDialog() + } +} + +@Composable +@SuppressLint("NewApi") +private fun ManageStorageDialog() { + var manageStorageGranted by remember { mutableStateOf(Environment.isExternalStorageManager()) } + + if (!manageStorageGranted) { + AlertDialog( + onDismissRequest = {}, + confirmButton = { + val launcher = + rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { + if (Environment.isExternalStorageManager()) { + manageStorageGranted = true + } + } + + Button( + onClick = { + Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) + .setData("package:${BuildConfig.APPLICATION_ID}".toUri()) + .let { launcher.launch(it) } + } + ) { + Text(stringResource(R.string.action_open_settings)) + } + }, + title = { Text(stringResource(R.string.title_permission_grant)) }, + text = { Text(stringResource(R.string.msg_permission_grant)) }, + properties = DialogProperties( + dismissOnBackPress = false, + dismissOnClickOutside = false + ) + ) + } +} + +@OptIn(ExperimentalPermissionsApi::class) +@Composable +private fun ExternalStorageDialog() { + val writeStorageState = rememberPermissionState(Manifest.permission.WRITE_EXTERNAL_STORAGE) + + if (!writeStorageState.status.isGranted) { + AlertDialog( + onDismissRequest = {}, + confirmButton = { + Button(onClick = writeStorageState::launchPermissionRequest) { + Text(stringResource(R.string.action_confirm)) + } + }, + title = { Text(stringResource(R.string.title_permission_grant)) }, + text = { Text(stringResource(R.string.msg_permission_grant)) }, + properties = DialogProperties( + dismissOnBackPress = false, + dismissOnClickOutside = false + ) + ) + } +} diff --git a/app/src/main/java/dev/beefers/vendetta/manager/utils/Constants.kt b/app/src/main/java/dev/beefers/vendetta/manager/utils/Constants.kt index a733ccc..001353f 100644 --- a/app/src/main/java/dev/beefers/vendetta/manager/utils/Constants.kt +++ b/app/src/main/java/dev/beefers/vendetta/manager/utils/Constants.kt @@ -1,5 +1,7 @@ package dev.beefers.vendetta.manager.utils +import android.os.Environment + object Constants { val TEAM_MEMBERS = listOf( @@ -7,6 +9,8 @@ object Constants { TeamMember("Kasi", "Developer - Xposed Module", "redstonekasi") ) + val VENDETTA_DIR = Environment.getExternalStorageDirectory().resolve("Vendetta") + } data class TeamMember( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 66e59f8..0b04773 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ 5 more taps 2 more taps You are now a developer + In order for Vendetta Manager to function, file permissions are required. Since shared data is stored in ~/Vendetta, permissions are required in order to access it. Download APKs Patching @@ -52,6 +53,7 @@ Retry Reload Open about + Open settings Cached @@ -60,6 +62,7 @@ Settings Update available! About + Grant Permissions System Light