From e869db0555d23ade6b2a9f83619d717febb4c8c2 Mon Sep 17 00:00:00 2001 From: kitadai31 <90122968+kitadai31@users.noreply.github.com> Date: Tue, 13 Aug 2024 01:23:31 +0900 Subject: [PATCH] fix: Move temporary files outside of the cache directory (#2122) --- .../manager/data/platform/Filesystem.kt | 7 +++--- .../manager/patcher/worker/PatcherWorker.kt | 3 +++ .../ui/viewmodel/AppSelectorViewModel.kt | 2 +- .../manager/ui/viewmodel/PatcherViewModel.kt | 23 ++++--------------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/app/revanced/manager/data/platform/Filesystem.kt b/app/src/main/java/app/revanced/manager/data/platform/Filesystem.kt index ec01f09b..3afbe6e8 100644 --- a/app/src/main/java/app/revanced/manager/data/platform/Filesystem.kt +++ b/app/src/main/java/app/revanced/manager/data/platform/Filesystem.kt @@ -1,10 +1,11 @@ package app.revanced.manager.data.platform +import android.Manifest import android.app.Application +import android.content.Context +import android.content.pm.PackageManager import android.os.Build import android.os.Environment -import android.Manifest -import android.content.pm.PackageManager import androidx.activity.result.contract.ActivityResultContract import androidx.activity.result.contract.ActivityResultContracts import app.revanced.manager.util.RequestManageStorageContract @@ -16,7 +17,7 @@ class Filesystem(private val app: Application) { * A directory that gets cleared when the app restarts. * Do not store paths to this directory in a parcel. */ - val tempDir = app.cacheDir.resolve("ephemeral").apply { + val tempDir = app.getDir("ephemeral", Context.MODE_PRIVATE).apply { deleteRecursively() mkdirs() } diff --git a/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt b/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt index bb91cf11..0e779df7 100644 --- a/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt +++ b/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt @@ -193,6 +193,9 @@ class PatcherWorker( Result.failure() } finally { patchedApk.delete() + if (args.input is SelectedApp.Local && args.input.temporary) { + args.input.file.delete() + } } } diff --git a/app/src/main/java/app/revanced/manager/ui/viewmodel/AppSelectorViewModel.kt b/app/src/main/java/app/revanced/manager/ui/viewmodel/AppSelectorViewModel.kt index a2174852..85cee8d1 100644 --- a/app/src/main/java/app/revanced/manager/ui/viewmodel/AppSelectorViewModel.kt +++ b/app/src/main/java/app/revanced/manager/ui/viewmodel/AppSelectorViewModel.kt @@ -25,7 +25,7 @@ class AppSelectorViewModel( private val pm: PM, private val patchBundleRepository: PatchBundleRepository ) : ViewModel() { - private val inputFile = File(app.cacheDir, "input.apk").also { + private val inputFile = File(app.filesDir, "input.apk").also { it.delete() } val appList = pm.appList diff --git a/app/src/main/java/app/revanced/manager/ui/viewmodel/PatcherViewModel.kt b/app/src/main/java/app/revanced/manager/ui/viewmodel/PatcherViewModel.kt index 27049127..e0995911 100644 --- a/app/src/main/java/app/revanced/manager/ui/viewmodel/PatcherViewModel.kt +++ b/app/src/main/java/app/revanced/manager/ui/viewmodel/PatcherViewModel.kt @@ -48,7 +48,6 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import kotlinx.coroutines.time.withTimeout import kotlinx.coroutines.withContext -import kotlinx.coroutines.withTimeout import org.koin.core.component.KoinComponent import org.koin.core.component.inject import java.io.File @@ -189,26 +188,14 @@ class PatcherViewModel( app.unregisterReceiver(installBroadcastReceiver) workManager.cancelWorkById(patcherWorkerId) - when (val selectedApp = input.selectedApp) { - is SelectedApp.Local -> { - if (selectedApp.temporary) selectedApp.file.delete() - } - - is SelectedApp.Installed -> { - GlobalScope.launch(Dispatchers.Main) { - uiSafe(app, R.string.failed_to_mount, "Failed to mount") { - installedApp?.let { - if (it.installType == InstallType.ROOT) { - withTimeout(Duration.ofMinutes(1L)) { - rootInstaller.mount(packageName) - } - } - } + if (input.selectedApp is SelectedApp.Installed && installedApp?.installType == InstallType.ROOT) { + GlobalScope.launch(Dispatchers.Main) { + uiSafe(app, R.string.failed_to_mount, "Failed to mount") { + withTimeout(Duration.ofMinutes(1L)) { + rootInstaller.mount(packageName) } } } - - else -> Unit } tempDir.deleteRecursively()