fix: improve installer progress and logs update

This commit is contained in:
Alberto Ponces 2022-08-24 14:17:16 +01:00
parent 5041a30fb5
commit 6f96c668de
2 changed files with 145 additions and 65 deletions

View file

@ -196,8 +196,14 @@ class MainActivity : FlutterActivity() {
Thread( Thread(
Runnable { Runnable {
handler.post { handler.post {
installerChannel.invokeMethod("updateProgress", 0.1) installerChannel.invokeMethod(
installerChannel.invokeMethod("updateLog", "Copying original apk") "update",
mapOf(
"progress" to 0.1,
"header" to "",
"log" to "Copying original apk"
)
)
} }
Files.copy( Files.copy(
originalFile.toPath(), originalFile.toPath(),
@ -206,8 +212,14 @@ class MainActivity : FlutterActivity() {
) )
handler.post { handler.post {
installerChannel.invokeMethod("updateProgress", 0.2) installerChannel.invokeMethod(
installerChannel.invokeMethod("updateLog", "Creating patcher") "update",
mapOf(
"progress" to 0.2,
"header" to "Unpacking apk...",
"log" to "Unpacking copied apk"
)
)
} }
val patcher = val patcher =
Patcher( Patcher(
@ -222,73 +234,131 @@ class MainActivity : FlutterActivity() {
app.revanced.patcher.logging.Logger { app.revanced.patcher.logging.Logger {
override fun error(msg: String) { override fun error(msg: String) {
handler.post { handler.post {
installerChannel installerChannel.invokeMethod(
.invokeMethod( "update",
"updateLog", mapOf(
msg "progress" to 0.2,
"header" to "",
"log" to msg
) )
)
} }
} }
override fun warn(msg: String) { override fun warn(msg: String) {
handler.post { handler.post {
installerChannel installerChannel.invokeMethod(
.invokeMethod( "update",
"updateLog", mapOf(
msg "progress" to 0.2,
"header" to "",
"log" to msg
) )
)
} }
} }
override fun info(msg: String) { override fun info(msg: String) {
handler.post { handler.post {
installerChannel installerChannel.invokeMethod(
.invokeMethod( "update",
"updateLog", mapOf(
msg "progress" to 0.2,
"header" to "",
"log" to msg
) )
)
} }
} }
override fun trace(msg: String) { override fun trace(msg: String) {
handler.post { handler.post {
installerChannel installerChannel.invokeMethod(
.invokeMethod( "update",
"updateLog", mapOf(
msg "progress" to 0.2,
"header" to "",
"log" to msg
) )
)
} }
} }
} }
) )
) )
handler.post { installerChannel.invokeMethod("updateProgress", 0.3) } handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.3,
"header" to "",
"log" to ""
)
)
}
if (mergeIntegrations) { if (mergeIntegrations) {
handler.post { handler.post {
installerChannel.invokeMethod( installerChannel.invokeMethod(
"updateLog", "update",
"Merging integrations" mapOf(
"progress" to 0.4,
"header" to "Merging integrations...",
"log" to "Merging integrations"
)
) )
} }
patcher.addFiles(listOf(integrations)) {} patcher.addFiles(listOf(integrations)) {}
} }
handler.post { installerChannel.invokeMethod("updateProgress", 0.5) } handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "Applying patches...",
"log" to ""
)
)
}
patcher.addPatches(filteredPatches) patcher.addPatches(filteredPatches)
patcher.applyPatches().forEach { (patch, res) -> patcher.applyPatches().forEach { (patch, res) ->
if (res.isSuccess) { if (res.isSuccess) {
val msg = "[success] $patch" val msg = "[success] $patch"
handler.post { installerChannel.invokeMethod("updateLog", msg) } handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "",
"log" to msg
)
)
}
return@forEach return@forEach
} }
val msg = "[error] $patch:" + res.exceptionOrNull()!! val msg = "[error] $patch:" + res.exceptionOrNull()!!
handler.post { installerChannel.invokeMethod("updateLog", msg) } handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.5,
"header" to "",
"log" to msg
)
)
}
} }
handler.post { handler.post {
installerChannel.invokeMethod("updateProgress", 0.7) installerChannel.invokeMethod(
installerChannel.invokeMethod("updateLog", "Repacking patched apk") "update",
mapOf(
"progress" to 0.7,
"header" to "Repacking apk...",
"log" to "Repacking patched apk"
)
)
} }
val res = patcher.save() val res = patcher.save()
ZipFile(patchedFile).use { file -> ZipFile(patchedFile).use { file ->
@ -309,13 +379,27 @@ class MainActivity : FlutterActivity() {
ZipAligner::getEntryAlignment ZipAligner::getEntryAlignment
) )
} }
handler.post {
handler.post { installerChannel.invokeMethod("updateProgress", 0.9) } installerChannel.invokeMethod(
"update",
mapOf(
"progress" to 0.9,
"header" to "Signing apk...",
"log" to ""
)
)
}
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile) Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile)
handler.post { handler.post {
installerChannel.invokeMethod("updateProgress", 1.0) installerChannel.invokeMethod(
installerChannel.invokeMethod("updateLog", "Finished") "update",
mapOf(
"progress" to 1.0,
"header" to "Finished",
"log" to "Finished"
)
)
} }
handler.post { result.success(null) } handler.post { result.success(null) }

View file

@ -23,7 +23,7 @@ class InstallerViewModel extends BaseViewModel {
double? progress = 0.0; double? progress = 0.0;
String logs = ''; String logs = '';
String headerLogs = ''; String headerLogs = '';
bool isPatching = false; bool isPatching = true;
bool isInstalled = false; bool isInstalled = false;
Future<void> initialize(BuildContext context) async { Future<void> initialize(BuildContext context) async {
@ -55,36 +55,34 @@ class InstallerViewModel extends BaseViewModel {
Future<dynamic> handlePlatformChannelMethods() async { Future<dynamic> handlePlatformChannelMethods() async {
_installerChannel.setMethodCallHandler((call) async { _installerChannel.setMethodCallHandler((call) async {
switch (call.method) { switch (call.method) {
case 'updateProgress': case 'update':
if (call.arguments != null) { if (call.arguments != null) {
updateProgress(call.arguments); Map<dynamic, dynamic> arguments = call.arguments;
} double progress = arguments['progress'];
break; String header = arguments['header'];
case 'updateLog': String log = arguments['log'];
if (call.arguments != null) { update(progress, header, log);
updateLog(call.arguments);
} }
break; break;
} }
}); });
} }
void updateProgress(double value) { void update(double value, String header, String log) {
progress = value; progress = value;
isInstalled = false; isInstalled = false;
isPatching = progress == 1.0 ? false : true; isPatching = progress == 1.0 ? false : true;
if (progress == 0.0) { if (progress == 0.0) {
logs = ''; logs = '';
} }
notifyListeners(); if (header.isNotEmpty) {
} headerLogs = header;
}
void updateLog(String message) { if (log.isNotEmpty && !log.startsWith('Merging L')) {
if (message.isNotEmpty && !message.startsWith('Merging L')) {
if (logs.isNotEmpty) { if (logs.isNotEmpty) {
logs += '\n'; logs += '\n';
} }
logs += message; logs += log;
Future.delayed(const Duration(milliseconds: 500)).then((value) { Future.delayed(const Duration(milliseconds: 500)).then((value) {
scrollController.animateTo( scrollController.animateTo(
scrollController.position.maxScrollExtent, scrollController.position.maxScrollExtent,
@ -92,26 +90,24 @@ class InstallerViewModel extends BaseViewModel {
curve: Curves.fastOutSlowIn, curve: Curves.fastOutSlowIn,
); );
}); });
notifyListeners();
} }
notifyListeners();
} }
Future<void> runPatcher() async { Future<void> runPatcher() async {
updateProgress(0.0); update(0.0, 'Initializing...', 'Initializing installer');
if (_app != null && _patches.isNotEmpty) { if (_app != null && _patches.isNotEmpty) {
String apkFilePath = _app!.apkFilePath; String apkFilePath = _app!.apkFilePath;
try { try {
updateLog('Initializing installer');
headerLogs = 'Initializing';
if (_app!.isRooted && !_app!.isFromStorage) { if (_app!.isRooted && !_app!.isFromStorage) {
updateLog('Checking if an old patched version exists'); update(0.0, '', 'Checking if an old patched version exists');
bool oldExists = await _patcherAPI.checkOldPatch(_app!); bool oldExists = await _patcherAPI.checkOldPatch(_app!);
if (oldExists) { if (oldExists) {
updateLog('Deleting old patched version'); update(0.0, '', 'Deleting old patched version');
await _patcherAPI.deleteOldPatch(_app!); await _patcherAPI.deleteOldPatch(_app!);
} }
} }
updateLog('Creating working directory'); update(0.0, '', 'Creating working directory');
bool mergeIntegrations = false; bool mergeIntegrations = false;
bool resourcePatching = false; bool resourcePatching = false;
if (_app!.packageName == 'com.google.android.youtube') { if (_app!.packageName == 'com.google.android.youtube') {
@ -122,7 +118,6 @@ class InstallerViewModel extends BaseViewModel {
resourcePatching = true; resourcePatching = true;
} }
await _patcherAPI.mergeIntegrations(mergeIntegrations); await _patcherAPI.mergeIntegrations(mergeIntegrations);
headerLogs = 'Merging integrations';
await _patcherAPI.runPatcher( await _patcherAPI.runPatcher(
apkFilePath, apkFilePath,
_patches, _patches,
@ -130,11 +125,10 @@ class InstallerViewModel extends BaseViewModel {
resourcePatching, resourcePatching,
); );
} on Exception { } on Exception {
updateLog('An error occurred! Aborting'); update(1.0, 'Aborting...', 'An error occurred! Aborting');
headerLogs = 'Aborting...';
} }
} else { } else {
updateLog('No app or patches selected! Aborting'); update(1.0, 'Aborting...', 'No app or patches selected! Aborting');
} }
try { try {
await FlutterBackground.disableBackgroundExecution(); await FlutterBackground.disableBackgroundExecution();
@ -145,19 +139,21 @@ class InstallerViewModel extends BaseViewModel {
void installResult() async { void installResult() async {
if (_app != null) { if (_app != null) {
updateLog(_app!.isRooted update(
? 'Installing patched file using root method' 1.0,
: 'Installing patched file using nonroot method'); 'Installing...',
headerLogs = 'Installing...'; _app!.isRooted
? 'Installing patched file using root method'
: 'Installing patched file using nonroot method',
);
isInstalled = await _patcherAPI.installPatchedFile(_app!); isInstalled = await _patcherAPI.installPatchedFile(_app!);
if (isInstalled) { if (isInstalled) {
updateLog('Done'); update(1.0, 'Installed...', 'Installed');
_app!.patchDate = DateTime.now(); _app!.patchDate = DateTime.now();
_app!.appliedPatches.addAll(_patches.map((p) => p.name).toList()); _app!.appliedPatches.addAll(_patches.map((p) => p.name).toList());
await saveApp(); await saveApp();
} else { } else {
updateLog('An error occurred! Aborting'); update(1.0, 'Aborting...', 'An error occurred! Aborting');
headerLogs = 'Aborting...';
} }
} }
} }