From dc47da75f2d4483a540f1ee5713c5594a14250ad Mon Sep 17 00:00:00 2001 From: nkitsaini <74284503+nkitsaini@users.noreply.github.com> Date: Wed, 2 Nov 2022 17:22:40 +0530 Subject: [PATCH] feat: Adding support for Exporting APK (#439) * feat: Adding Export APK support * Using cr_file_saver to simplify export --- assets/i18n/en_US.json | 1 + lib/services/patcher_api.dart | 33 +++++++++++++++++-- lib/ui/views/installer/installer_view.dart | 11 ++++++- .../views/installer/installer_viewmodel.dart | 11 +++++++ pubspec.yaml | 1 + 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index ea0c717a..89718886 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -96,6 +96,7 @@ "notificationTitle": "ReVanced Manager is patching", "notificationText": "Tap to return to the installer", "shareApkMenuOption": "Share APK", + "exportApkMenuOption": "Export APK", "shareLogMenuOption": "Share log", "installErrorDialogTitle": "Error", "installErrorDialogText1": "Root install is not possible with the current patches selection.\nRepatch your app or choose non-root install.", diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index 31ad0cd4..bca0e902 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -12,6 +12,7 @@ import 'package:revanced_manager/services/manager_api.dart'; import 'package:revanced_manager/services/root_api.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:share_extend/share_extend.dart'; +import 'package:cr_file_saver/file_saver.dart'; @lazySingleton class PatcherAPI { @@ -228,11 +229,32 @@ class PatcherAPI { return false; } + + void exportPatchedFile(String appName, String version) { + try { + if (_outFile != null) { + String newName = _getFileName(appName, version); + + // This is temporary workaround to populate initial file name + // ref: https://github.com/Cleveroad/cr_file_saver/issues/7 + int lastSeparator = _outFile!.path.lastIndexOf('/'); + String newSourcePath = _outFile!.path.substring(0, lastSeparator + 1) + newName; + _outFile!.copySync(newSourcePath); + + CRFileSaver.saveFileWithDialog(SaveFileDialogParams( + sourceFilePath: newSourcePath, + destinationFileName: newName + )); + } + } on Exception catch (e, s) { + Sentry.captureException(e, stackTrace: s); + } + } + void sharePatchedFile(String appName, String version) { try { if (_outFile != null) { - String prefix = appName.toLowerCase().replaceAll(' ', '-'); - String newName = '$prefix-revanced_v$version.apk'; + String newName = _getFileName(appName, version); int lastSeparator = _outFile!.path.lastIndexOf('/'); String newPath = _outFile!.path.substring(0, lastSeparator + 1) + newName; @@ -244,6 +266,13 @@ class PatcherAPI { } } + String _getFileName(String appName, String version) { + String prefix = appName.toLowerCase().replaceAll(' ', '-'); + String newName = '$prefix-revanced_v$version.apk'; + return newName; + + } + Future sharePatcherLog(String logs) async { Directory appCache = await getTemporaryDirectory(); Directory logDir = Directory('${appCache.path}/logs'); diff --git a/lib/ui/views/installer/installer_view.dart b/lib/ui/views/installer/installer_view.dart index 7cfcbeb9..e6b5672f 100644 --- a/lib/ui/views/installer/installer_view.dart +++ b/lib/ui/views/installer/installer_view.dart @@ -48,7 +48,16 @@ class InstallerView extends StatelessWidget { ), ), ), - 1: I18nText( + 1: I18nText( + 'installerView.exportApkMenuOption', + child: const Text( + '', + style: TextStyle( + fontWeight: FontWeight.bold, + ), + ), + ), + 2: I18nText( 'installerView.shareLogMenuOption', child: const Text( '', diff --git a/lib/ui/views/installer/installer_viewmodel.dart b/lib/ui/views/installer/installer_viewmodel.dart index ae9fdc4d..50fa006a 100644 --- a/lib/ui/views/installer/installer_viewmodel.dart +++ b/lib/ui/views/installer/installer_viewmodel.dart @@ -217,6 +217,14 @@ class InstallerViewModel extends BaseViewModel { } } + void exportResult() { + try { + _patcherAPI.exportPatchedFile(_app.name, _app.version); + } on Exception catch (e, s) { + Sentry.captureException(e, stackTrace: s); + } + } + void shareResult() { try { _patcherAPI.sharePatchedFile(_app.name, _app.version); @@ -250,6 +258,9 @@ class InstallerViewModel extends BaseViewModel { shareResult(); break; case 1: + exportResult(); + break; + case 2: shareLog(); break; } diff --git a/pubspec.yaml b/pubspec.yaml index 40d84385..a2e37ff0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ dependencies: app_installer: ^1.1.0 collection: ^1.16.0 cross_connectivity: ^3.0.5 + cr_file_saver: ^0.0.1+2 device_apps: git: url: https://github.com/ponces/flutter_plugin_device_apps