fix: unblock UI while running Patcher (#4)

This commit is contained in:
Alberto Ponces
2022-08-17 12:48:03 +01:00
committed by GitHub
parent 014450642d
commit 165bd4aec2
8 changed files with 354 additions and 396 deletions

View File

@ -11,11 +11,10 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:stacked/stacked.dart';
class InstallerViewModel extends BaseViewModel {
double? progress = 0.2;
double? progress = 0.0;
String logs = '';
bool isPatching = false;
bool isInstalled = false;
bool showButtons = false;
Future<void> initialize() async {
await FlutterBackground.initialize(
@ -31,10 +30,20 @@ class InstallerViewModel extends BaseViewModel {
);
await FlutterBackground.enableBackgroundExecution();
await locator<PatcherAPI>().handlePlatformChannelMethods();
runPatcher();
await runPatcher();
}
void addLog(String message) {
void updateProgress(double value) {
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 (logs.isNotEmpty) {
logs += '\n';
@ -44,91 +53,47 @@ class InstallerViewModel extends BaseViewModel {
}
}
void updateProgress(double value) {
progress = value;
isInstalled = false;
isPatching = progress == 1.0 ? false : true;
showButtons = progress == 1.0 ? true : false;
if (progress == 0.0) {
logs = '';
}
notifyListeners();
}
Future<void> runPatcher() async {
updateProgress(0.0);
PatchedApplication? selectedApp =
locator<AppSelectorViewModel>().selectedApp;
if (selectedApp != null) {
List<Patch> selectedPatches =
locator<PatchesSelectorViewModel>().selectedPatches;
if (selectedApp != null && selectedPatches.isNotEmpty) {
String apkFilePath = selectedApp.apkFilePath;
List<Patch> selectedPatches =
locator<PatchesSelectorViewModel>().selectedPatches;
if (selectedPatches.isNotEmpty) {
addLog('Initializing installer');
try {
updateLog('Initializing installer');
if (selectedApp.isRooted && !selectedApp.isFromStorage) {
addLog('Checking if an old patched version exists');
updateLog('Checking if an old patched version exists');
bool oldExists =
await locator<PatcherAPI>().checkOldPatch(selectedApp);
if (oldExists) {
addLog('Deleting old patched version');
updateLog('Deleting old patched version');
await locator<PatcherAPI>().deleteOldPatch(selectedApp);
}
}
addLog('Creating working directory');
bool? isSuccess = await locator<PatcherAPI>().initPatcher();
if (isSuccess != null && isSuccess) {
updateProgress(0.1);
addLog('Copying original apk');
isSuccess = await locator<PatcherAPI>().copyInputFile(apkFilePath);
if (isSuccess != null && isSuccess) {
updateProgress(0.2);
addLog('Creating patcher');
bool resourcePatching = false;
if (selectedApp.packageName == 'com.google.android.youtube' ||
selectedApp.packageName ==
'com.google.android.apps.youtube.music') {
resourcePatching = true;
}
isSuccess = await locator<PatcherAPI>().createPatcher(
resourcePatching,
);
if (isSuccess != null && isSuccess) {
if (selectedApp.packageName == 'com.google.android.youtube') {
updateProgress(0.3);
addLog('Merging integrations');
isSuccess = await locator<PatcherAPI>().mergeIntegrations();
}
if (isSuccess != null && isSuccess) {
updateProgress(0.5);
isSuccess =
await locator<PatcherAPI>().applyPatches(selectedPatches);
if (isSuccess != null && isSuccess) {
updateProgress(0.7);
addLog('Repacking patched apk');
isSuccess = await locator<PatcherAPI>().repackPatchedFile();
if (isSuccess != null && isSuccess) {
updateProgress(0.9);
addLog('Signing patched apk');
isSuccess = await locator<PatcherAPI>().signPatchedFile();
if (isSuccess != null && isSuccess) {
showButtons = true;
updateProgress(1.0);
addLog('Finished');
}
}
}
}
}
}
updateLog('Creating working directory');
bool mergeIntegrations = false;
bool resourcePatching = false;
if (selectedApp.packageName == 'com.google.android.youtube') {
mergeIntegrations = true;
resourcePatching = true;
} else if (selectedApp.packageName ==
'com.google.android.apps.youtube.music') {
resourcePatching = true;
}
if (isSuccess == null || !isSuccess) {
addLog('An error occurred! Aborting');
}
} else {
addLog('No patches selected! Aborting');
await locator<PatcherAPI>().initPatcher(mergeIntegrations);
await locator<PatcherAPI>().runPatcher(
apkFilePath,
selectedPatches,
mergeIntegrations,
resourcePatching,
);
} on Exception {
updateLog('An error occurred! Aborting');
}
} else {
addLog('No app selected! Aborting');
updateLog('No app or patches selected! Aborting');
}
await FlutterBackground.disableBackgroundExecution();
isPatching = false;
@ -138,15 +103,15 @@ class InstallerViewModel extends BaseViewModel {
PatchedApplication? selectedApp =
locator<AppSelectorViewModel>().selectedApp;
if (selectedApp != null) {
addLog(selectedApp.isRooted
updateLog(selectedApp.isRooted
? 'Installing patched file using root method'
: 'Installing patched file using nonroot method');
isInstalled = await locator<PatcherAPI>().installPatchedFile(selectedApp);
if (isInstalled) {
addLog('Done');
updateLog('Done');
await saveApp(selectedApp);
} else {
addLog('An error occurred! Aborting');
updateLog('An error occurred! Aborting');
}
}
}