fix: Prevent Manager from crashing when a patcher exception occurs and print its stack trace on installer's log

This commit is contained in:
Alberto Ponces 2022-09-23 00:38:42 +01:00
parent f9242c1958
commit 1028ec8e2a
2 changed files with 149 additions and 127 deletions

View File

@ -74,7 +74,6 @@ class MainActivity : FlutterActivity() {
result.notImplemented() result.notImplemented()
} }
} }
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
@ -101,6 +100,8 @@ class MainActivity : FlutterActivity() {
val integrations = File(integrationsPath) val integrations = File(integrationsPath)
val keyStoreFile = File(keyStoreFilePath) val keyStoreFile = File(keyStoreFilePath)
Thread {
try {
val patches = DexPatchBundle( val patches = DexPatchBundle(
patchBundleFilePath, patchBundleFilePath,
DexClassLoader( DexClassLoader(
@ -111,7 +112,6 @@ class MainActivity : FlutterActivity() {
) )
).loadPatches().filter { patch -> selectedPatches.any { it == patch.patchName } } ).loadPatches().filter { patch -> selectedPatches.any { it == patch.patchName } }
Thread {
handler.post { handler.post {
installerChannel.invokeMethod( installerChannel.invokeMethod(
"update", "update",
@ -176,6 +176,7 @@ class MainActivity : FlutterActivity() {
) )
) )
} }
patcher.addPatches(patches) patcher.addPatches(patches)
patcher.applyPatches().forEach { (patch, res) -> patcher.applyPatches().forEach { (patch, res) ->
if (res.isSuccess) { if (res.isSuccess) {
@ -252,10 +253,21 @@ class MainActivity : FlutterActivity() {
) )
) )
} }
} catch (ex: Throwable) {
handler.post { result.success(null) } val stack = ex.stackTraceToString()
handler.post {
installerChannel.invokeMethod(
"update",
mapOf(
"progress" to -100.0,
"header" to "Aborting...",
"log" to "An error occurred! Aborting\nError:\n$stack"
)
)
} }
.start() }
handler.post { result.success(null) }
}.start()
} }
inner class ManagerLogger : Logger { inner class ManagerLogger : Logger {

View File

@ -78,14 +78,20 @@ class InstallerViewModel extends BaseViewModel {
} }
void update(double value, String header, String log) { void update(double value, String header, String log) {
if (value > 0) { if (value >= 0.0) {
progress = value; progress = value;
} }
isPatching = progress == 1.0 ? false : true; if (value == 0.0) {
if (progress == 0.0) {
logs = ''; logs = '';
isPatching = true;
isInstalled = false; isInstalled = false;
hasErrors = false; hasErrors = false;
} else if (value == 1.0) {
isPatching = false;
hasErrors = false;
} else if (value == -100.0) {
isPatching = false;
hasErrors = true;
} }
if (header.isNotEmpty) { if (header.isNotEmpty) {
headerLogs = header; headerLogs = header;
@ -95,6 +101,9 @@ class InstallerViewModel extends BaseViewModel {
logs += '\n'; logs += '\n';
} }
logs += log; logs += log;
if (logs[logs.length - 1] == '\n') {
logs = logs.substring(0, logs.length - 1);
}
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,
@ -117,12 +126,14 @@ class InstallerViewModel extends BaseViewModel {
_patches, _patches,
); );
} catch (e) { } catch (e) {
hasErrors = true; update(
update(-1.0, 'Aborting...', 'An error occurred! Aborting\nError: $e'); -100.0,
'Aborting...',
'An error occurred! Aborting\nError:\n$e',
);
} }
} else { } else {
hasErrors = true; update(-100.0, 'Aborting...', 'No app or patches selected! Aborting');
update(-1.0, 'Aborting...', 'No app or patches selected! Aborting');
} }
if (FlutterBackground.isBackgroundExecutionEnabled) { if (FlutterBackground.isBackgroundExecutionEnabled) {
try { try {
@ -132,7 +143,6 @@ class InstallerViewModel extends BaseViewModel {
} }
} }
await Wakelock.disable(); await Wakelock.disable();
isPatching = false;
} }
void installResult(BuildContext context, bool installAsRoot) async { void installResult(BuildContext context, bool installAsRoot) async {