Skip to content

Commit

Permalink
Using cr_file_saver to simplify export
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitsaini committed Nov 1, 2022
1 parent 10efbbd commit ec7b41d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package app.revanced.manager.flutter

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
Expand All @@ -21,32 +18,19 @@ import dalvik.system.DexClassLoader
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.Result
import java.io.File
import java.io.OutputStream

private const val PATCHER_CHANNEL = "app.revanced.manager.flutter/patcher"
private const val INSTALLER_CHANNEL = "app.revanced.manager.flutter/installer"
private const val EXPORTER_CHANNEL = "app.revanced.manager.flutter/exporter"

private const val APK_MIME_TYPE = "application/vnd.android.package-archive"

class MainActivity : FlutterActivity() {
private val handler = Handler(Looper.getMainLooper())
private lateinit var installerChannel: MethodChannel
private lateinit var exporterChannel: MethodChannel


// Export APK
internal val export_request_code = 1
internal var export_result: Result? = null
internal var export_source_path: String? = null

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
val mainChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, PATCHER_CHANNEL)
installerChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, INSTALLER_CHANNEL)
exporterChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, EXPORTER_CHANNEL)
mainChannel.setMethodCallHandler { call, result ->
when (call.method) {
"runPatcher" -> {
Expand Down Expand Up @@ -91,71 +75,6 @@ class MainActivity : FlutterActivity() {
else -> result.notImplemented()
}
}

exporterChannel.setMethodCallHandler { call, result ->
// Referenced from https://gist.github.com/MSVCode/9ccedfa6692f8bc3b82fdc74fad65bc6
if (call.method == "exportApk") {
export_result = result;

export_source_path = call.argument<String>("source_path")!!;
var name = call.argument<String>("name")!!;
startFileExport(APK_MIME_TYPE, name);
} else {
result.notImplemented();
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

// Check which request we're responding to
if (requestCode == export_request_code) {
if (resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) {
exportToFile(data.getData() as Uri) // data.getData() is Uri
} else {
export_result?.error("NO DATA", "Did not get valid data (Uri) for export", null)
}
} else {
export_result?.error("CANCELED", "User cancelled", null)
}
}
}

private fun startFileExport(mimeType: String, fileName: String) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
// Filter to only show results that can be "opened", such as
// a file (as opposed to a list of contacts or timezones).
addCategory(Intent.CATEGORY_OPENABLE)

// Create a file with the requested MIME type.
type = mimeType
putExtra(Intent.EXTRA_TITLE, fileName)
}

startActivityForResult(intent, export_request_code)
}


private fun exportToFile(uri: Uri) {
val outputStream: OutputStream?
try {
outputStream = getContentResolver().openOutputStream(uri)
if (outputStream != null) {
File(export_source_path).inputStream().copyTo(outputStream)
export_result?.success("SUCCESS")
} else {
export_result?.error("ERROR", "Unable to open output Uri", null)
}
} catch (e: Exception) {

// log to console
print("Error exporting apk: ${e.message}")
e.printStackTrace()

export_result?.error("ERROR", "Unable to write", null)
}
}

private fun runPatcher(
Expand Down
10 changes: 5 additions & 5 deletions lib/services/patcher_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -232,12 +233,11 @@ class PatcherAPI {
void exportPatchedFile(String appName, String version) {
try {
if (_outFile != null) {
const platform = MethodChannel("app.revanced.manager.flutter/exporter");
String newName = _getFileName(appName, version);
platform.invokeMethod("exportApk", {
"source_path": _outFile!.path,
"name": newName
});
CRFileSaver.saveFileWithDialog(SaveFileDialogParams(
sourceFilePath: _outFile!.path,
destinationFileName: newName
));
}
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s);
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec7b41d

Please sign in to comment.