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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 354 additions and 396 deletions

View File

@ -1,5 +1,7 @@
package app.revanced.manager package app.revanced.manager
import android.os.Handler
import android.os.Looper
import androidx.annotation.NonNull import androidx.annotation.NonNull
import app.revanced.manager.utils.Aapt import app.revanced.manager.utils.Aapt
import app.revanced.manager.utils.aligning.ZipAligner import app.revanced.manager.utils.aligning.ZipAligner
@ -19,92 +21,76 @@ import dalvik.system.DexClassLoader
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.Result
import java.io.File import java.io.File
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
class MainActivity : FlutterActivity() { class MainActivity : FlutterActivity() {
private val CHANNEL = "app.revanced.manager/patcher" private val PATCHER_CHANNEL = "app.revanced.manager/patcher"
private val INSTALLER_CHANNEL = "app.revanced.manager/installer"
private var patches = mutableListOf<Class<out Patch<Data>>>() private var patches = mutableListOf<Class<out Patch<Data>>>()
private val tag = "Patcher" private val handler = Handler(Looper.getMainLooper())
private lateinit var methodChannel: MethodChannel private lateinit var installerChannel: MethodChannel
private lateinit var patcher: Patcher
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)
methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL) val mainChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, PATCHER_CHANNEL)
methodChannel.setMethodCallHandler { call, result -> installerChannel =
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, INSTALLER_CHANNEL)
mainChannel.setMethodCallHandler { call, result ->
when (call.method) { when (call.method) {
"loadPatches" -> { "loadPatches" -> {
val pathBundlesPaths = call.argument<List<String>>("pathBundlesPaths") val pathBundlesPaths = call.argument<List<String>>("pathBundlesPaths")
if (pathBundlesPaths != null) { if (pathBundlesPaths != null) {
result.success(loadPatches(pathBundlesPaths)) loadPatches(result, pathBundlesPaths)
} else { } else {
result.notImplemented() result.notImplemented()
} }
} }
"getCompatiblePackages" -> result.success(getCompatiblePackages()) "getCompatiblePackages" -> getCompatiblePackages(result)
"getFilteredPatches" -> { "getFilteredPatches" -> {
val targetPackage = call.argument<String>("targetPackage") val targetPackage = call.argument<String>("targetPackage")
val targetVersion = call.argument<String>("targetVersion") val targetVersion = call.argument<String>("targetVersion")
val ignoreVersion = call.argument<Boolean>("ignoreVersion") val ignoreVersion = call.argument<Boolean>("ignoreVersion")
if (targetPackage != null && targetVersion != null && ignoreVersion != null) { if (targetPackage != null && targetVersion != null && ignoreVersion != null) {
result.success( getFilteredPatches(result, targetPackage, targetVersion, ignoreVersion)
getFilteredPatches(targetPackage, targetVersion, ignoreVersion)
)
} else { } else {
result.notImplemented() result.notImplemented()
} }
} }
"copyInputFile" -> { "runPatcher" -> {
val originalFilePath = call.argument<String>("originalFilePath") val originalFilePath = call.argument<String>("originalFilePath")
val inputFilePath = call.argument<String>("inputFilePath") val inputFilePath = call.argument<String>("inputFilePath")
if (originalFilePath != null && inputFilePath != null) {
result.success(copyInputFile(originalFilePath, inputFilePath))
} else {
result.notImplemented()
}
}
"createPatcher" -> {
val inputFilePath = call.argument<String>("inputFilePath")
val cacheDirPath = call.argument<String>("cacheDirPath")
val resourcePatching = call.argument<Boolean>("resourcePatching")
if (inputFilePath != null && cacheDirPath != null && resourcePatching != null) {
result.success(createPatcher(inputFilePath, cacheDirPath, resourcePatching))
} else {
result.notImplemented()
}
}
"mergeIntegrations" -> {
val integrationsPath = call.argument<String>("integrationsPath")
if (integrationsPath != null) {
result.success(mergeIntegrations(integrationsPath))
} else {
result.notImplemented()
}
}
"applyPatches" -> {
val selectedPatches = call.argument<List<String>>("selectedPatches")
if (selectedPatches != null) {
result.success(applyPatches(selectedPatches))
} else {
result.notImplemented()
}
}
"repackPatchedFile" -> {
val inputFilePath = call.argument<String>("inputFilePath")
val patchedFilePath = call.argument<String>("patchedFilePath")
if (inputFilePath != null && patchedFilePath != null) {
result.success(repackPatchedFile(inputFilePath, patchedFilePath))
} else {
result.notImplemented()
}
}
"signPatchedFile" -> {
val patchedFilePath = call.argument<String>("patchedFilePath") val patchedFilePath = call.argument<String>("patchedFilePath")
val outFilePath = call.argument<String>("outFilePath") val outFilePath = call.argument<String>("outFilePath")
if (patchedFilePath != null && outFilePath != null) { val integrationsPath = call.argument<String>("integrationsPath")
result.success(signPatchedFile(patchedFilePath, outFilePath)) val selectedPatches = call.argument<List<String>>("selectedPatches")
val cacheDirPath = call.argument<String>("cacheDirPath")
val mergeIntegrations = call.argument<Boolean>("mergeIntegrations")
val resourcePatching = call.argument<Boolean>("resourcePatching")
if (originalFilePath != null &&
inputFilePath != null &&
patchedFilePath != null &&
outFilePath != null &&
integrationsPath != null &&
selectedPatches != null &&
cacheDirPath != null &&
mergeIntegrations != null &&
resourcePatching != null
) {
runPatcher(
result,
originalFilePath,
inputFilePath,
patchedFilePath,
outFilePath,
integrationsPath,
selectedPatches,
cacheDirPath,
mergeIntegrations,
resourcePatching
)
} else { } else {
result.notImplemented() result.notImplemented()
} }
@ -114,157 +100,221 @@ class MainActivity : FlutterActivity() {
} }
} }
fun loadPatches(pathBundlesPaths: List<String>): Boolean { fun loadPatches(result: MethodChannel.Result, pathBundlesPaths: List<String>) {
try { Thread(
pathBundlesPaths.forEach { path -> Runnable {
patches.addAll( pathBundlesPaths.forEach { path ->
DexPatchBundle( patches.addAll(
path, DexPatchBundle(
DexClassLoader( path,
path, DexClassLoader(
applicationContext.cacheDir.path, path,
null, applicationContext.cacheDir.path,
javaClass.classLoader null,
) javaClass.classLoader
)
)
.loadPatches()
) )
.loadPatches() }
handler.post { result.success(null) }
}
) )
} .start()
} catch (e: Exception) {
return false
}
return true
} }
fun getCompatiblePackages(): List<String> { fun getCompatiblePackages(result: MethodChannel.Result) {
val filteredPackages = mutableListOf<String>() Thread(
patches.forEach patch@{ patch -> Runnable {
patch.compatiblePackages?.forEach { pkg -> filteredPackages.add(pkg.name) } val filteredPackages = mutableListOf<String>()
} patches.forEach patch@{ patch ->
return filteredPackages.distinct() patch.compatiblePackages?.forEach { pkg ->
filteredPackages.add(pkg.name)
}
}
handler.post { result.success(filteredPackages.distinct()) }
}
)
.start()
} }
fun getFilteredPatches( fun getFilteredPatches(
result: MethodChannel.Result,
targetPackage: String, targetPackage: String,
targetVersion: String, targetVersion: String,
ignoreVersion: Boolean ignoreVersion: Boolean
): List<Map<String, String?>> { ) {
val filteredPatches = mutableListOf<Map<String, String?>>() Thread(
patches.forEach patch@{ patch -> Runnable {
patch.compatiblePackages?.forEach { pkg -> val filteredPatches = mutableListOf<Map<String, String?>>()
if (pkg.name == targetPackage && patches.forEach patch@{ patch ->
(ignoreVersion || patch.compatiblePackages?.forEach { pkg ->
pkg.versions.isNotEmpty() || if (pkg.name == targetPackage &&
pkg.versions.contains(targetVersion)) (ignoreVersion ||
) { pkg.versions.isNotEmpty() ||
var p = mutableMapOf<String, String?>() pkg.versions.contains(targetVersion))
p.put("name", patch.patchName) ) {
p.put("version", patch.version) var p = mutableMapOf<String, String?>()
p.put("description", patch.description) p.put("name", patch.patchName)
filteredPatches.add(p) p.put("version", patch.version)
} p.put("description", patch.description)
} filteredPatches.add(p)
} }
return filteredPatches }
}
handler.post { result.success(filteredPatches) }
}
)
.start()
} }
private fun findPatchesByIds(ids: Iterable<String>): List<Class<out Patch<Data>>> { fun runPatcher(
return patches.filter { patch -> ids.any { it == patch.patchName } } result: MethodChannel.Result,
} originalFilePath: String,
inputFilePath: String,
fun copyInputFile(originalFilePath: String, inputFilePath: String): Boolean { patchedFilePath: String,
outFilePath: String,
integrationsPath: String,
selectedPatches: List<String>,
cacheDirPath: String,
mergeIntegrations: Boolean,
resourcePatching: Boolean
) {
val originalFile = File(originalFilePath) val originalFile = File(originalFilePath)
val inputFile = File(inputFilePath) val inputFile = File(inputFilePath)
Files.copy(originalFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
return true
}
fun createPatcher(
inputFilePath: String,
cacheDirPath: String,
resourcePatching: Boolean
): Boolean {
val inputFile = File(inputFilePath)
val aaptPath = Aapt.binary(applicationContext).absolutePath
patcher =
Patcher(
PatcherOptions(
inputFile,
cacheDirPath,
resourcePatching,
aaptPath,
cacheDirPath,
logger =
object : app.revanced.patcher.logging.Logger {
override fun error(msg: String) {
methodChannel.invokeMethod("updateInstallerLog", msg)
}
override fun warn(msg: String) {
methodChannel.invokeMethod("updateInstallerLog", msg)
}
override fun info(msg: String) {
methodChannel.invokeMethod("updateInstallerLog", msg)
}
override fun trace(msg: String) {
methodChannel.invokeMethod("updateInstallerLog", msg)
}
}
)
)
return true
}
fun mergeIntegrations(integrationsPath: String): Boolean {
val integrations = File(integrationsPath)
if (patcher == null) return false
patcher.addFiles(listOf(integrations)) {}
return true
}
fun applyPatches(selectedPatches: List<String>): Boolean {
val patches = findPatchesByIds(selectedPatches)
if (patches.isEmpty()) return false
if (patcher == null) return false
patcher.addPatches(patches)
patcher.applyPatches().forEach { (patch, result) ->
if (result.isSuccess) {
val msg = "[success] $patch"
methodChannel.invokeMethod("updateInstallerLog", msg)
return@forEach
}
val msg = "[error] $patch:" + result.exceptionOrNull()!!
methodChannel.invokeMethod("updateInstallerLog", msg)
}
return true
}
fun repackPatchedFile(inputFilePath: String, patchedFilePath: String): Boolean {
val inputFile = File(inputFilePath)
val patchedFile = File(patchedFilePath)
if (patcher == null) return false
val result = patcher.save()
ZipFile(patchedFile).use { file ->
result.dexFiles.forEach {
file.addEntryCompressData(
ZipEntry.createWithName(it.name),
it.dexFileInputStream.readBytes()
)
}
result.resourceFile?.let {
file.copyEntriesFromFileAligned(ZipFile(it), ZipAligner::getEntryAlignment)
}
file.copyEntriesFromFileAligned(ZipFile(inputFile), ZipAligner::getEntryAlignment)
}
return true
}
fun signPatchedFile(patchedFilePath: String, outFilePath: String): Boolean {
val patchedFile = File(patchedFilePath) val patchedFile = File(patchedFilePath)
val outFile = File(outFilePath) val outFile = File(outFilePath)
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile) val integrations = File(integrationsPath)
return true val filteredPatches = patches.filter { patch -> selectedPatches.any { it == patch.patchName } }
Thread(
Runnable {
handler.post {
installerChannel.invokeMethod("updateProgress", 0.1)
installerChannel.invokeMethod("updateLog", "Copying original apk")
}
Files.copy(
originalFile.toPath(),
inputFile.toPath(),
StandardCopyOption.REPLACE_EXISTING
)
handler.post {
installerChannel.invokeMethod("updateProgress", 0.2)
installerChannel.invokeMethod("updateLog", "Creating patcher")
}
val patcher =
Patcher(
PatcherOptions(
inputFile,
cacheDirPath,
resourcePatching,
Aapt.binary(applicationContext).absolutePath,
cacheDirPath,
logger =
object :
app.revanced.patcher.logging.Logger {
override fun error(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"updateLog",
msg
)
}
}
override fun warn(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"updateLog",
msg
)
}
}
override fun info(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"updateLog",
msg
)
}
}
override fun trace(msg: String) {
handler.post {
installerChannel
.invokeMethod(
"updateLog",
msg
)
}
}
}
)
)
handler.post { installerChannel.invokeMethod("updateProgress", 0.3) }
if (mergeIntegrations) {
handler.post {
installerChannel.invokeMethod(
"updateLog",
"Merging integrations"
)
}
patcher.addFiles(listOf(integrations)) {}
}
handler.post { installerChannel.invokeMethod("updateProgress", 0.5) }
patcher.addPatches(filteredPatches)
patcher.applyPatches().forEach { (patch, res) ->
if (res.isSuccess) {
val msg = "[success] $patch"
handler.post { installerChannel.invokeMethod("updateLog", msg) }
return@forEach
}
val msg = "[error] $patch:" + res.exceptionOrNull()!!
handler.post { installerChannel.invokeMethod("updateLog", msg) }
}
handler.post {
installerChannel.invokeMethod("updateProgress", 0.7)
installerChannel.invokeMethod("updateLog", "Repacking patched apk")
}
val res = patcher.save()
ZipFile(patchedFile).use { file ->
res.dexFiles.forEach {
file.addEntryCompressData(
ZipEntry.createWithName(it.name),
it.dexFileInputStream.readBytes()
)
}
res.resourceFile?.let {
file.copyEntriesFromFileAligned(
ZipFile(it),
ZipAligner::getEntryAlignment
)
}
file.copyEntriesFromFileAligned(
ZipFile(inputFile),
ZipAligner::getEntryAlignment
)
}
handler.post { installerChannel.invokeMethod("updateProgress", 0.9) }
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile)
handler.post {
installerChannel.invokeMethod("updateProgress", 1.0)
installerChannel.invokeMethod("updateLog", "Finished")
}
handler.post { result.success(null) }
}
)
.start()
} }
} }

View File

@ -16,7 +16,12 @@ import 'package:share_extend/share_extend.dart';
@lazySingleton @lazySingleton
class PatcherAPI { 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 GithubAPI githubAPI = GithubAPI();
final RootAPI rootAPI = RootAPI(); final RootAPI rootAPI = RootAPI();
final List<ApplicationWithIcon> _filteredPackages = []; final List<ApplicationWithIcon> _filteredPackages = [];
@ -31,9 +36,18 @@ class PatcherAPI {
File? _outFile; File? _outFile;
Future<dynamic> handlePlatformChannelMethods() async { Future<dynamic> handlePlatformChannelMethods() async {
platform.setMethodCallHandler((call) async { installerChannel.setMethodCallHandler((call) async {
if (call.method == 'updateInstallerLog' && call.arguments != null) { switch (call.method) {
locator<InstallerViewModel>().addLog(call.arguments); 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 { try {
_patchBundleFile = _patchBundleFile =
await DefaultCacheManager().getSingleFile(dexFileUrl); await DefaultCacheManager().getSingleFile(dexFileUrl);
return await platform.invokeMethod<bool>( return await patcherChannel.invokeMethod<bool>(
'loadPatches', 'loadPatches',
{ {
'pathBundlesPaths': <String>[_patchBundleFile!.absolute.path], 'pathBundlesPaths': <String>[_patchBundleFile!.absolute.path],
@ -65,8 +79,8 @@ class PatcherAPI {
Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async { Future<List<ApplicationWithIcon>> getFilteredInstalledApps() async {
if (_patchBundleFile != null && _filteredPackages.isEmpty) { if (_patchBundleFile != null && _filteredPackages.isEmpty) {
try { try {
List<String>? patchesPackages = List<String>? patchesPackages = await patcherChannel
await platform.invokeListMethod<String>('getCompatiblePackages'); .invokeListMethod<String>('getCompatiblePackages');
if (patchesPackages != null) { if (patchesPackages != null) {
for (String package in patchesPackages) { for (String package in patchesPackages) {
try { try {
@ -96,7 +110,8 @@ class PatcherAPI {
_filteredPatches[selectedApp.packageName]!.isEmpty) { _filteredPatches[selectedApp.packageName]!.isEmpty) {
_filteredPatches[selectedApp.packageName] = []; _filteredPatches[selectedApp.packageName] = [];
try { try {
var patches = await platform.invokeListMethod<Map<dynamic, dynamic>>( var patches =
await patcherChannel.invokeListMethod<Map<dynamic, dynamic>>(
'getFilteredPatches', 'getFilteredPatches',
{ {
'targetPackage': selectedApp.packageName, 'targetPackage': selectedApp.packageName,
@ -143,118 +158,41 @@ class PatcherAPI {
return null; return null;
} }
Future<bool?> initPatcher() async { Future<void> initPatcher(bool mergeIntegrations) async {
try { if (mergeIntegrations) {
_integrations = await downloadIntegrations(); _integrations = await downloadIntegrations();
if (_integrations != null) { } else {
_tmpDir = await getTemporaryDirectory(); _integrations = File('');
_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;
} }
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 { Future<void> runPatcher(
if (_inputFile != null) { String originalFilePath,
try { List<Patch> selectedPatches,
return await platform.invokeMethod<bool>( bool mergeIntegrations,
'copyInputFile', bool resourcePatching,
{ ) async {
'originalFilePath': originalFilePath, await patcherChannel.invokeMethod(
'inputFilePath': _inputFile!.path, 'runPatcher',
}, {
); 'originalFilePath': originalFilePath,
} on Exception { 'inputFilePath': _inputFile!.path,
return false; 'patchedFilePath': _patchedFile!.path,
} 'outFilePath': _outFile!.path,
} 'integrationsPath': _integrations!.path,
return false; 'selectedPatches': selectedPatches.map((e) => e.name).toList(),
} 'cacheDirPath': _cacheDir!.path,
'mergeIntegrations': mergeIntegrations,
Future<bool?> createPatcher(bool resourcePatching) async { 'resourcePatching': resourcePatching,
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<bool> installPatchedFile(PatchedApplication patchedApp) async { Future<bool> installPatchedFile(PatchedApplication patchedApp) async {

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ class PatcherView extends StatelessWidget {
viewModelBuilder: () => locator<PatcherViewModel>(), viewModelBuilder: () => locator<PatcherViewModel>(),
builder: (context, model, child) => Scaffold( builder: (context, model, child) => Scaffold(
floatingActionButton: Visibility( floatingActionButton: Visibility(
visible: locator<PatcherViewModel>().showFabButton, visible: model.showFabButton(),
child: FloatingActionButton.extended( child: FloatingActionButton.extended(
onPressed: () => model.navigateToInstaller(), onPressed: () => model.navigateToInstaller(),
label: I18nText('patcherView.fabButton'), label: I18nText('patcherView.fabButton'),
@ -52,8 +52,8 @@ class PatcherView extends StatelessWidget {
const SizedBox(height: 16), const SizedBox(height: 16),
Opacity( Opacity(
opacity: isDark opacity: isDark
? (model.dimPatchCard ? 0.5 : 1) ? (model.dimPatchesCard() ? 0.5 : 1)
: (model.dimPatchCard ? 0.75 : 1), : (model.dimPatchesCard() ? 0.75 : 1),
child: PatchSelectorCard( child: PatchSelectorCard(
onPressed: model.navigateToPatchesSelector, onPressed: model.navigateToPatchesSelector,
color: Theme.of(context).colorScheme.primary, 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.locator.dart';
import 'package:revanced_manager/app/app.router.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/stacked.dart';
import 'package:stacked_services/stacked_services.dart'; import 'package:stacked_services/stacked_services.dart';
class PatcherViewModel extends BaseViewModel { class PatcherViewModel extends BaseViewModel {
final _navigationService = locator<NavigationService>(); final _navigationService = locator<NavigationService>();
bool dimPatchCard = true;
bool showFabButton = false;
void navigateToAppSelector() { void navigateToAppSelector() {
_navigationService.navigateTo(Routes.appSelectorView); _navigationService.navigateTo(Routes.appSelectorView);
@ -19,4 +19,12 @@ class PatcherViewModel extends BaseViewModel {
void navigateToInstaller() { void navigateToInstaller() {
_navigationService.navigateTo(Routes.installerView); _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(); locator<PatcherViewModel>().notifyListeners();
} }
} }