import 'package:device_apps/device_apps.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_background/flutter_background.dart'; import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:revanced_manager/app/app.locator.dart'; import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patched_application.dart'; import 'package:revanced_manager/services/manager_api.dart'; import 'package:revanced_manager/services/patcher_api.dart'; import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart'; import 'package:stacked/stacked.dart'; import 'package:wakelock/wakelock.dart'; class InstallerViewModel extends BaseViewModel { final ManagerAPI _managerAPI = locator(); final PatcherAPI _patcherAPI = locator(); final PatchedApplication _app = locator().selectedApp!; final List _patches = locator().selectedPatches; static const _installerChannel = MethodChannel( 'app.revanced.manager.flutter/installer', ); final ScrollController scrollController = ScrollController(); double? progress = 0.0; String logs = ''; String headerLogs = ''; bool isPatching = true; bool isInstalled = false; bool hasErrors = false; Future initialize(BuildContext context) async { try { await FlutterBackground.initialize( androidConfig: FlutterBackgroundAndroidConfig( notificationTitle: FlutterI18n.translate( context, 'installerView.notificationTitle', ), notificationText: FlutterI18n.translate( context, 'installerView.notificationText', ), notificationImportance: AndroidNotificationImportance.Default, notificationIcon: const AndroidResource( name: 'ic_notification', defType: 'drawable', ), ), ); await FlutterBackground.enableBackgroundExecution(); await Wakelock.enable(); } on Exception { // ignore } await handlePlatformChannelMethods(); await runPatcher(); } Future handlePlatformChannelMethods() async { _installerChannel.setMethodCallHandler((call) async { switch (call.method) { case 'update': if (call.arguments != null) { Map arguments = call.arguments; double progress = arguments['progress']; String header = arguments['header']; String log = arguments['log']; update(progress, header, log); } break; } }); } void update(double value, String header, String log) { if (value > 0) { progress = value; } isPatching = progress == 1.0 ? false : true; if (progress == 0.0) { logs = ''; isInstalled = false; hasErrors = false; } if (header.isNotEmpty) { headerLogs = header; } if (log.isNotEmpty && !log.startsWith('Merging L')) { if (logs.isNotEmpty) { logs += '\n'; } logs += log; Future.delayed(const Duration(milliseconds: 500)).then((value) { scrollController.animateTo( scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 200), curve: Curves.fastOutSlowIn, ); }); } notifyListeners(); } Future runPatcher() async { update(0.0, 'Initializing...', 'Initializing installer'); if (_patches.isNotEmpty) { try { update(0.1, '', 'Creating working directory'); await _patcherAPI.runPatcher( _app.packageName, _app.apkFilePath, _patches, ); } catch (e) { hasErrors = true; update(-1.0, 'Aborting...', 'An error occurred! Aborting\nError: $e'); } } else { hasErrors = true; update(-1.0, 'Aborting...', 'No app or patches selected! Aborting'); } try { await FlutterBackground.disableBackgroundExecution(); await Wakelock.disable(); } on Exception { // ignore } isPatching = false; } void installResult(bool installAsRoot) async { _app.isRooted = installAsRoot; 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) { update(1.0, 'Installed!', 'Installed!'); _app.patchDate = DateTime.now(); _app.appliedPatches = _patches.map((p) => p.name).toList(); bool hasMicroG = _patches.any((p) => p.name.endsWith('microg-support')); if (hasMicroG) { _app.packageName = _app.packageName.replaceFirst( 'com.google.', 'app.revanced.', ); } await _managerAPI.savePatchedApp(_app); } } void shareResult() { _patcherAPI.sharePatchedFile(_app.name, _app.version); } void shareLog() { _patcherAPI.sharePatcherLog(logs); } Future cleanPatcher() async { _patcherAPI.cleanPatcher(); locator().selectedApp = null; locator().selectedPatches.clear(); locator().notifyListeners(); } void openApp() { DeviceApps.openApp(_app.packageName); } void onMenuSelection(int value) { switch (value) { case 0: shareResult(); break; case 1: shareLog(); break; } } }