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

View file

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