2022-08-15 04:31:36 +02:00
|
|
|
import 'package:device_apps/device_apps.dart';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2022-08-15 04:31:36 +02:00
|
|
|
import 'package:flutter_background/flutter_background.dart';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'package:flutter_i18n/flutter_i18n.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
2022-08-14 20:40:34 +02:00
|
|
|
import 'package:revanced_manager/models/patched_application.dart';
|
2022-08-25 01:51:47 +02:00
|
|
|
import 'package:revanced_manager/services/manager_api.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:revanced_manager/services/patcher_api.dart';
|
2022-08-14 04:07:28 +02:00
|
|
|
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
|
|
|
|
|
|
|
class InstallerViewModel extends BaseViewModel {
|
2022-08-25 01:51:47 +02:00
|
|
|
final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
2022-08-18 16:33:33 +02:00
|
|
|
final PatcherAPI _patcherAPI = locator<PatcherAPI>();
|
|
|
|
final PatchedApplication? _app = locator<PatcherViewModel>().selectedApp;
|
|
|
|
final List<Patch> _patches = locator<PatcherViewModel>().selectedPatches;
|
|
|
|
static const _installerChannel = MethodChannel(
|
|
|
|
'app.revanced.manager/installer',
|
|
|
|
);
|
2022-08-25 01:51:47 +02:00
|
|
|
final ScrollController scrollController = ScrollController();
|
2022-08-17 13:48:03 +02:00
|
|
|
double? progress = 0.0;
|
2022-08-13 11:56:30 +02:00
|
|
|
String logs = '';
|
2022-08-23 20:13:06 +02:00
|
|
|
String headerLogs = '';
|
2022-08-24 15:17:16 +02:00
|
|
|
bool isPatching = true;
|
2022-08-15 04:31:36 +02:00
|
|
|
bool isInstalled = false;
|
2022-08-13 11:56:30 +02:00
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
Future<void> initialize(BuildContext context) async {
|
2022-08-17 14:48:56 +02:00
|
|
|
try {
|
|
|
|
await FlutterBackground.initialize(
|
2022-08-18 16:33:33 +02:00
|
|
|
androidConfig: FlutterBackgroundAndroidConfig(
|
|
|
|
notificationTitle: FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'installerView.notificationTitle',
|
|
|
|
),
|
|
|
|
notificationText: FlutterI18n.translate(
|
|
|
|
context,
|
|
|
|
'installerView.notificationText',
|
|
|
|
),
|
2022-08-17 14:48:56 +02:00
|
|
|
notificationImportance: AndroidNotificationImportance.Default,
|
2022-08-18 16:33:33 +02:00
|
|
|
notificationIcon: const AndroidResource(
|
2022-08-17 14:48:56 +02:00
|
|
|
name: 'ic_notification',
|
|
|
|
defType: 'drawable',
|
|
|
|
),
|
2022-08-15 04:31:36 +02:00
|
|
|
),
|
2022-08-17 14:48:56 +02:00
|
|
|
);
|
|
|
|
await FlutterBackground.enableBackgroundExecution();
|
|
|
|
} finally {
|
2022-08-18 16:33:33 +02:00
|
|
|
await handlePlatformChannelMethods();
|
2022-08-17 14:48:56 +02:00
|
|
|
await runPatcher();
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
Future<dynamic> handlePlatformChannelMethods() async {
|
|
|
|
_installerChannel.setMethodCallHandler((call) async {
|
|
|
|
switch (call.method) {
|
2022-08-24 15:17:16 +02:00
|
|
|
case 'update':
|
2022-08-18 16:33:33 +02:00
|
|
|
if (call.arguments != null) {
|
2022-08-24 15:17:16 +02:00
|
|
|
Map<dynamic, dynamic> arguments = call.arguments;
|
|
|
|
double progress = arguments['progress'];
|
|
|
|
String header = arguments['header'];
|
|
|
|
String log = arguments['log'];
|
|
|
|
update(progress, header, log);
|
2022-08-18 16:33:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:17:16 +02:00
|
|
|
void update(double value, String header, String log) {
|
2022-08-13 11:56:30 +02:00
|
|
|
progress = value;
|
2022-08-15 04:31:36 +02:00
|
|
|
isInstalled = false;
|
2022-08-13 11:56:30 +02:00
|
|
|
isPatching = progress == 1.0 ? false : true;
|
|
|
|
if (progress == 0.0) {
|
|
|
|
logs = '';
|
|
|
|
}
|
2022-08-24 15:17:16 +02:00
|
|
|
if (header.isNotEmpty) {
|
|
|
|
headerLogs = header;
|
|
|
|
}
|
|
|
|
if (log.isNotEmpty && !log.startsWith('Merging L')) {
|
2022-08-17 13:48:03 +02:00
|
|
|
if (logs.isNotEmpty) {
|
|
|
|
logs += '\n';
|
|
|
|
}
|
2022-08-24 15:17:16 +02:00
|
|
|
logs += log;
|
2022-08-22 01:49:09 +02:00
|
|
|
Future.delayed(const Duration(milliseconds: 500)).then((value) {
|
|
|
|
scrollController.animateTo(
|
|
|
|
scrollController.position.maxScrollExtent,
|
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
curve: Curves.fastOutSlowIn,
|
|
|
|
);
|
|
|
|
});
|
2022-08-17 13:48:03 +02:00
|
|
|
}
|
2022-08-24 15:17:16 +02:00
|
|
|
notifyListeners();
|
2022-08-17 13:48:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
Future<void> runPatcher() async {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(0.0, 'Initializing...', 'Initializing installer');
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app != null && _patches.isNotEmpty) {
|
|
|
|
String apkFilePath = _app!.apkFilePath;
|
2022-08-17 13:48:03 +02:00
|
|
|
try {
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app!.isRooted && !_app!.isFromStorage) {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(0.0, '', 'Checking if an old patched version exists');
|
2022-08-18 16:33:33 +02:00
|
|
|
bool oldExists = await _patcherAPI.checkOldPatch(_app!);
|
2022-08-15 04:31:36 +02:00
|
|
|
if (oldExists) {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(0.0, '', 'Deleting old patched version');
|
2022-08-18 16:33:33 +02:00
|
|
|
await _patcherAPI.deleteOldPatch(_app!);
|
2022-08-15 04:31:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 15:17:16 +02:00
|
|
|
update(0.0, '', 'Creating working directory');
|
2022-08-17 13:48:03 +02:00
|
|
|
bool mergeIntegrations = false;
|
|
|
|
bool resourcePatching = false;
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app!.packageName == 'com.google.android.youtube') {
|
2022-08-17 13:48:03 +02:00
|
|
|
mergeIntegrations = true;
|
|
|
|
resourcePatching = true;
|
2022-08-18 16:33:33 +02:00
|
|
|
} else if (_app!.packageName ==
|
2022-08-17 13:48:03 +02:00
|
|
|
'com.google.android.apps.youtube.music') {
|
|
|
|
resourcePatching = true;
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-08-19 20:13:43 +02:00
|
|
|
await _patcherAPI.mergeIntegrations(mergeIntegrations);
|
2022-08-18 16:33:33 +02:00
|
|
|
await _patcherAPI.runPatcher(
|
2022-08-17 13:48:03 +02:00
|
|
|
apkFilePath,
|
2022-08-18 16:33:33 +02:00
|
|
|
_patches,
|
2022-08-17 13:48:03 +02:00
|
|
|
mergeIntegrations,
|
|
|
|
resourcePatching,
|
|
|
|
);
|
|
|
|
} on Exception {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(1.0, 'Aborting...', 'An error occurred! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(1.0, 'Aborting...', 'No app or patches selected! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-08-17 14:48:56 +02:00
|
|
|
try {
|
|
|
|
await FlutterBackground.disableBackgroundExecution();
|
|
|
|
} finally {
|
|
|
|
isPatching = false;
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void installResult() async {
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app != null) {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(
|
|
|
|
1.0,
|
|
|
|
'Installing...',
|
|
|
|
_app!.isRooted
|
|
|
|
? 'Installing patched file using root method'
|
|
|
|
: 'Installing patched file using nonroot method',
|
|
|
|
);
|
2022-08-18 16:33:33 +02:00
|
|
|
isInstalled = await _patcherAPI.installPatchedFile(_app!);
|
2022-08-15 04:31:36 +02:00
|
|
|
if (isInstalled) {
|
2022-08-25 01:51:47 +02:00
|
|
|
update(1.0, 'Installed!', 'Installed');
|
2022-08-18 16:33:33 +02:00
|
|
|
_app!.patchDate = DateTime.now();
|
|
|
|
_app!.appliedPatches.addAll(_patches.map((p) => p.name).toList());
|
2022-08-25 01:51:47 +02:00
|
|
|
_managerAPI.savePatchedApp(_app!);
|
2022-08-14 22:32:03 +02:00
|
|
|
} else {
|
2022-08-24 15:17:16 +02:00
|
|
|
update(1.0, 'Aborting...', 'An error occurred! Aborting');
|
2022-08-14 22:32:03 +02:00
|
|
|
}
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void shareResult() {
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app != null) {
|
|
|
|
_patcherAPI.sharePatchedFile(_app!.name, _app!.version);
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 04:07:28 +02:00
|
|
|
|
2022-08-22 02:31:27 +02:00
|
|
|
Future<void> cleanPatcher() async {
|
2022-08-18 16:33:33 +02:00
|
|
|
_patcherAPI.cleanPatcher();
|
|
|
|
locator<PatcherViewModel>().selectedApp = null;
|
|
|
|
locator<PatcherViewModel>().selectedPatches.clear();
|
2022-08-14 04:07:28 +02:00
|
|
|
locator<PatcherViewModel>().notifyListeners();
|
|
|
|
}
|
2022-08-15 04:31:36 +02:00
|
|
|
|
|
|
|
void openApp() {
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app != null) {
|
|
|
|
DeviceApps.openApp(_app!.packageName);
|
2022-08-15 04:31:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|