From 1ef1f8d47a519e0b063839bcaa507bccc486e4b5 Mon Sep 17 00:00:00 2001 From: kitadai31 <90122968+kitadai31@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:42:03 +0900 Subject: [PATCH] fix: Move temporary files outside of the cache directory (#2193) Co-authored-by: Pun Butrach --- .../app/revanced/manager/flutter/MainActivity.kt | 3 +++ lib/services/patcher_api.dart | 10 +++++++++- lib/ui/views/installer/installer_viewmodel.dart | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt b/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt index fae87d03..4f4ed0d1 100644 --- a/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt +++ b/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt @@ -364,6 +364,9 @@ class MainActivity : FlutterActivity() { "An error occurred:\n$stack" ) } + } finally { + inFile.delete() + tmpDir.deleteRecursively() } handler.post { result.success(null) } diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index df9d2154..018d227e 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -34,7 +34,7 @@ class PatcherAPI { Future initialize() async { await loadPatches(); await _managerAPI.downloadIntegrations(); - final Directory appCache = await getTemporaryDirectory(); + final Directory appCache = await getApplicationSupportDirectory(); _dataDir = await getExternalStorageDirectory() ?? appCache; _tmpDir = Directory('${appCache.path}/patcher'); _keyStoreFile = File('${_dataDir.path}/revanced-manager.keystore'); @@ -151,6 +151,7 @@ class PatcherAPI { String packageName, String apkFilePath, List selectedPatches, + bool isFromStorage, ) async { final File? integrationsFile = await _managerAPI.downloadIntegrations(); final Map> options = {}; @@ -176,6 +177,13 @@ class PatcherAPI { final File inApkFile = File('${workDir.path}/in.apk'); await File(apkFilePath).copy(inApkFile.path); + if (isFromStorage) { + // The selected apk was copied to cacheDir by the file picker, so it's not needed anymore. + // rename() can't be used here, as Android system also counts the size of files moved out from cacheDir + // as part of the app's cache size. + File(apkFilePath).delete(); + } + outFile = File('${workDir.path}/out.apk'); final Directory tmpDir = diff --git a/lib/ui/views/installer/installer_viewmodel.dart b/lib/ui/views/installer/installer_viewmodel.dart index 81dea726..84276e54 100644 --- a/lib/ui/views/installer/installer_viewmodel.dart +++ b/lib/ui/views/installer/installer_viewmodel.dart @@ -188,6 +188,7 @@ class InstallerViewModel extends BaseViewModel { _app.packageName, _app.apkFilePath, _patches, + _app.isFromStorage, ); _app.appliedPatches = _patches.map((p) => p.name).toList(); if (_managerAPI.isLastPatchedAppEnabled()) {