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

@@ -16,7 +16,12 @@ import 'package:share_extend/share_extend.dart';
@lazySingleton
class PatcherAPI {
static const platform = MethodChannel('app.revanced.manager/patcher');
static const patcherChannel = MethodChannel(
'app.revanced.manager/patcher',
);
static const installerChannel = MethodChannel(
'app.revanced.manager/installer',
);
final GithubAPI githubAPI = GithubAPI();
final RootAPI rootAPI = RootAPI();
final List<ApplicationWithIcon> _filteredPackages = [];
@@ -31,9 +36,18 @@ class PatcherAPI {
File? _outFile;
Future<dynamic> handlePlatformChannelMethods() async {
platform.setMethodCallHandler((call) async {
if (call.method == 'updateInstallerLog' && call.arguments != null) {
locator<InstallerViewModel>().addLog(call.arguments);
installerChannel.setMethodCallHandler((call) async {
switch (call.method) {
case 'updateProgress':
if (call.arguments != null) {
locator<InstallerViewModel>().updateProgress(call.arguments);
}
break;
case 'updateLog':
if (call.arguments != null) {
locator<InstallerViewModel>().updateLog(call.arguments);
}
break;
}
});
}
@@ -46,7 +60,7 @@ class PatcherAPI {
try {
_patchBundleFile =
await DefaultCacheManager().getSingleFile(dexFileUrl);
return await platform.invokeMethod<bool>(
return await patcherChannel.invokeMethod<bool>(
'loadPatches',
{
'pathBundlesPaths': <String>[_patchBundleFile!.absolute.path],
@@ -65,8 +79,8 @@ class PatcherAPI {
Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async {
if (_patchBundleFile != null && _filteredPackages.isEmpty) {
try {
List<String>? patchesPackages =
await platform.invokeListMethod<String>('getCompatiblePackages');
List<String>? patchesPackages = await patcherChannel
.invokeListMethod<String>('getCompatiblePackages');
if (patchesPackages != null) {
for (String package in patchesPackages) {
try {
@@ -96,7 +110,8 @@ class PatcherAPI {
_filteredPatches[selectedApp.packageName]!.isEmpty) {
_filteredPatches[selectedApp.packageName] = [];
try {
var patches = await platform.invokeListMethod<Map<dynamic, dynamic>>(
var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'getFilteredPatches',
{
'targetPackage': selectedApp.packageName,
@@ -143,118 +158,41 @@ class PatcherAPI {
return null;
}
Future<bool?> initPatcher() async {
try {
Future<void> initPatcher(bool mergeIntegrations) async {
if (mergeIntegrations) {
_integrations = await downloadIntegrations();
if (_integrations != null) {
_tmpDir = await getTemporaryDirectory();
_workDir = _tmpDir!.createTempSync('tmp-');
_inputFile = File('${_workDir!.path}/base.apk');
_patchedFile = File('${_workDir!.path}/patched.apk');
_outFile = File('${_workDir!.path}/out.apk');
_cacheDir = Directory('${_workDir!.path}/cache');
_cacheDir!.createSync();
return true;
}
} on Exception {
return false;
} else {
_integrations = File('');
}
return false;
_tmpDir = await getTemporaryDirectory();
_workDir = _tmpDir!.createTempSync('tmp-');
_inputFile = File('${_workDir!.path}/base.apk');
_patchedFile = File('${_workDir!.path}/patched.apk');
_outFile = File('${_workDir!.path}/out.apk');
_cacheDir = Directory('${_workDir!.path}/cache');
_cacheDir!.createSync();
}
Future<bool?> copyInputFile(String originalFilePath) async {
if (_inputFile != null) {
try {
return await platform.invokeMethod<bool>(
'copyInputFile',
{
'originalFilePath': originalFilePath,
'inputFilePath': _inputFile!.path,
},
);
} on Exception {
return false;
}
}
return false;
}
Future<bool?> createPatcher(bool resourcePatching) async {
if (_inputFile != null && _cacheDir != null) {
try {
return await platform.invokeMethod<bool>(
'createPatcher',
{
'inputFilePath': _inputFile!.path,
'cacheDirPath': _cacheDir!.path,
'resourcePatching': resourcePatching,
},
);
} on Exception {
return false;
}
}
return false;
}
Future<bool?> mergeIntegrations() async {
try {
return await platform.invokeMethod<bool>(
'mergeIntegrations',
{
'integrationsPath': _integrations!.path,
},
);
} on Exception {
return false;
}
}
Future<bool?> applyPatches(List<Patch> selectedPatches) async {
try {
return await platform.invokeMethod<bool>(
'applyPatches',
{
'selectedPatches': selectedPatches.map((e) => e.name).toList(),
},
);
} on Exception {
return false;
}
}
Future<bool?> repackPatchedFile() async {
if (_inputFile != null && _patchedFile != null) {
try {
return await platform.invokeMethod<bool>(
'repackPatchedFile',
{
'inputFilePath': _inputFile!.path,
'patchedFilePath': _patchedFile!.path,
},
);
} on Exception {
return false;
}
}
return false;
}
Future<bool?> signPatchedFile() async {
if (_patchedFile != null && _outFile != null) {
try {
return await platform.invokeMethod<bool>(
'signPatchedFile',
{
'patchedFilePath': _patchedFile!.path,
'outFilePath': _outFile!.path,
},
);
} on Exception {
return false;
}
}
return false;
Future<void> runPatcher(
String originalFilePath,
List<Patch> selectedPatches,
bool mergeIntegrations,
bool resourcePatching,
) async {
await patcherChannel.invokeMethod(
'runPatcher',
{
'originalFilePath': originalFilePath,
'inputFilePath': _inputFile!.path,
'patchedFilePath': _patchedFile!.path,
'outFilePath': _outFile!.path,
'integrationsPath': _integrations!.path,
'selectedPatches': selectedPatches.map((e) => e.name).toList(),
'cacheDirPath': _cacheDir!.path,
'mergeIntegrations': mergeIntegrations,
'resourcePatching': resourcePatching,
},
);
}
Future<bool> installPatchedFile(PatchedApplication patchedApp) async {

View File

@@ -45,7 +45,6 @@ class AppSelectorViewModel extends BaseViewModel {
);
locator<AppSelectorViewModel>().selectedApp = app;
locator<PatchesSelectorViewModel>().selectedPatches.clear();
locator<PatcherViewModel>().dimPatchCard = false;
locator<PatcherViewModel>().notifyListeners();
}
@@ -74,7 +73,6 @@ class AppSelectorViewModel extends BaseViewModel {
);
locator<AppSelectorViewModel>().selectedApp = app;
locator<PatchesSelectorViewModel>().selectedPatches.clear();
locator<PatcherViewModel>().dimPatchCard = false;
locator<PatcherViewModel>().notifyListeners();
}
}

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/ui/views/installer/installer_viewmodel.dart';
@@ -21,7 +22,7 @@ class InstallerView extends StatelessWidget {
builder: (context, model, child) => WillPopScope(
child: Scaffold(
floatingActionButton: Visibility(
visible: model.showButtons,
visible: !model.isPatching,
child: FloatingActionButton.extended(
onPressed: () =>
model.isInstalled ? model.openApp() : model.installResult(),
@@ -54,7 +55,7 @@ class InstallerView extends StatelessWidget {
),
),
Visibility(
visible: model.showButtons,
visible: !model.isPatching,
child: IconButton(
icon: const Icon(Icons.share),
onPressed: () => model.shareResult(),

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');
}
}
}

View File

@@ -18,7 +18,7 @@ class PatcherView extends StatelessWidget {
viewModelBuilder: () => locator<PatcherViewModel>(),
builder: (context, model, child) => Scaffold(
floatingActionButton: Visibility(
visible: locator<PatcherViewModel>().showFabButton,
visible: model.showFabButton(),
child: FloatingActionButton.extended(
onPressed: () => model.navigateToInstaller(),
label: I18nText('patcherView.fabButton'),
@@ -52,8 +52,8 @@ class PatcherView extends StatelessWidget {
const SizedBox(height: 16),
Opacity(
opacity: isDark
? (model.dimPatchCard ? 0.5 : 1)
: (model.dimPatchCard ? 0.75 : 1),
? (model.dimPatchesCard() ? 0.5 : 1)
: (model.dimPatchesCard() ? 0.75 : 1),
child: PatchSelectorCard(
onPressed: model.navigateToPatchesSelector,
color: Theme.of(context).colorScheme.primary,

View File

@@ -1,12 +1,12 @@
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/app/app.router.dart';
import 'package:revanced_manager/ui/views/app_selector/app_selector_viewmodel.dart';
import 'package:revanced_manager/ui/views/patches_selector/patches_selector_viewmodel.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
class PatcherViewModel extends BaseViewModel {
final _navigationService = locator<NavigationService>();
bool dimPatchCard = true;
bool showFabButton = false;
void navigateToAppSelector() {
_navigationService.navigateTo(Routes.appSelectorView);
@@ -19,4 +19,12 @@ class PatcherViewModel extends BaseViewModel {
void navigateToInstaller() {
_navigationService.navigateTo(Routes.installerView);
}
bool showFabButton() {
return locator<PatchesSelectorViewModel>().selectedPatches.isNotEmpty;
}
bool dimPatchesCard() {
return locator<AppSelectorViewModel>().selectedApp == null;
}
}

View File

@@ -35,8 +35,6 @@ class PatchesSelectorViewModel extends BaseViewModel {
}
}
}
locator<PatcherViewModel>().showFabButton =
selectedPatches.isNotEmpty ? true : false;
locator<PatcherViewModel>().notifyListeners();
}
}