2022-08-15 04:31:36 +02:00
|
|
|
import 'package:device_apps/device_apps.dart';
|
|
|
|
import 'package:flutter_background/flutter_background.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-13 11:56:30 +02:00
|
|
|
import 'package:revanced_manager/services/patcher_api.dart';
|
|
|
|
import 'package:revanced_manager/ui/views/app_selector/app_selector_viewmodel.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:revanced_manager/ui/views/patches_selector/patches_selector_viewmodel.dart';
|
2022-08-15 19:42:09 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
|
|
|
|
|
|
|
class InstallerViewModel extends BaseViewModel {
|
|
|
|
double? progress = 0.2;
|
|
|
|
String logs = '';
|
|
|
|
bool isPatching = false;
|
2022-08-15 04:31:36 +02:00
|
|
|
bool isInstalled = false;
|
2022-08-13 11:56:30 +02:00
|
|
|
bool showButtons = false;
|
|
|
|
|
|
|
|
Future<void> initialize() async {
|
2022-08-15 04:31:36 +02:00
|
|
|
await FlutterBackground.initialize(
|
|
|
|
androidConfig: const FlutterBackgroundAndroidConfig(
|
|
|
|
notificationTitle: 'Patching',
|
|
|
|
notificationText: 'ReVanced Manager is patching',
|
|
|
|
notificationImportance: AndroidNotificationImportance.Default,
|
|
|
|
notificationIcon: AndroidResource(
|
|
|
|
name: 'ic_launcher_foreground',
|
|
|
|
defType: 'drawable',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await FlutterBackground.enableBackgroundExecution();
|
2022-08-13 11:56:30 +02:00
|
|
|
await locator<PatcherAPI>().handlePlatformChannelMethods();
|
|
|
|
runPatcher();
|
|
|
|
}
|
|
|
|
|
|
|
|
void addLog(String message) {
|
|
|
|
if (logs.isNotEmpty) {
|
|
|
|
logs += '\n';
|
|
|
|
}
|
|
|
|
logs += message;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateProgress(double value) {
|
|
|
|
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;
|
|
|
|
showButtons = progress == 1.0 ? true : false;
|
|
|
|
if (progress == 0.0) {
|
|
|
|
logs = '';
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> runPatcher() async {
|
|
|
|
updateProgress(0.0);
|
2022-08-14 20:40:34 +02:00
|
|
|
PatchedApplication? selectedApp =
|
|
|
|
locator<AppSelectorViewModel>().selectedApp;
|
2022-08-13 11:56:30 +02:00
|
|
|
if (selectedApp != null) {
|
|
|
|
String apkFilePath = selectedApp.apkFilePath;
|
|
|
|
List<Patch> selectedPatches =
|
|
|
|
locator<PatchesSelectorViewModel>().selectedPatches;
|
|
|
|
if (selectedPatches.isNotEmpty) {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Initializing installer');
|
2022-08-15 11:55:17 +02:00
|
|
|
if (selectedApp.isRooted && !selectedApp.isFromStorage) {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Checking if an old patched version exists');
|
2022-08-15 04:31:36 +02:00
|
|
|
bool oldExists =
|
|
|
|
await locator<PatcherAPI>().checkOldPatch(selectedApp);
|
|
|
|
if (oldExists) {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Deleting old patched version');
|
2022-08-15 04:31:36 +02:00
|
|
|
await locator<PatcherAPI>().deleteOldPatch(selectedApp);
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Creating working directory');
|
2022-08-13 11:56:30 +02:00
|
|
|
bool? isSuccess = await locator<PatcherAPI>().initPatcher();
|
|
|
|
if (isSuccess != null && isSuccess) {
|
|
|
|
updateProgress(0.1);
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Copying original apk');
|
2022-08-13 11:56:30 +02:00
|
|
|
isSuccess = await locator<PatcherAPI>().copyInputFile(apkFilePath);
|
|
|
|
if (isSuccess != null && isSuccess) {
|
|
|
|
updateProgress(0.2);
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Creating patcher');
|
2022-08-14 04:21:32 +02:00
|
|
|
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,
|
|
|
|
);
|
2022-08-13 11:56:30 +02:00
|
|
|
if (isSuccess != null && isSuccess) {
|
|
|
|
if (selectedApp.packageName == 'com.google.android.youtube') {
|
|
|
|
updateProgress(0.3);
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Merging integrations');
|
2022-08-13 11:56:30 +02:00
|
|
|
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);
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Repacking patched apk');
|
2022-08-13 11:56:30 +02:00
|
|
|
isSuccess = await locator<PatcherAPI>().repackPatchedFile();
|
|
|
|
if (isSuccess != null && isSuccess) {
|
|
|
|
updateProgress(0.9);
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('Signing patched apk');
|
2022-08-13 11:56:30 +02:00
|
|
|
isSuccess = await locator<PatcherAPI>().signPatchedFile();
|
|
|
|
if (isSuccess != null && isSuccess) {
|
|
|
|
showButtons = true;
|
|
|
|
updateProgress(1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isSuccess == null || !isSuccess) {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('An error occurred! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('No patches selected! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('No app selected! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-08-15 04:31:36 +02:00
|
|
|
await FlutterBackground.disableBackgroundExecution();
|
2022-08-13 11:56:30 +02:00
|
|
|
isPatching = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void installResult() async {
|
2022-08-14 20:40:34 +02:00
|
|
|
PatchedApplication? selectedApp =
|
|
|
|
locator<AppSelectorViewModel>().selectedApp;
|
|
|
|
if (selectedApp != null) {
|
2022-08-14 22:32:03 +02:00
|
|
|
addLog(selectedApp.isRooted
|
2022-08-15 19:42:09 +02:00
|
|
|
? 'Installing patched file using root method'
|
|
|
|
: 'Installing patched file using nonroot method');
|
2022-08-15 04:31:36 +02:00
|
|
|
isInstalled = await locator<PatcherAPI>().installPatchedFile(selectedApp);
|
|
|
|
if (isInstalled) {
|
2022-08-14 22:32:03 +02:00
|
|
|
addLog('Done');
|
2022-08-15 19:42:09 +02:00
|
|
|
await saveApp(selectedApp);
|
2022-08-14 22:32:03 +02:00
|
|
|
} else {
|
2022-08-15 19:42:09 +02:00
|
|
|
addLog('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-14 20:40:34 +02:00
|
|
|
PatchedApplication? selectedApp =
|
|
|
|
locator<AppSelectorViewModel>().selectedApp;
|
2022-08-13 11:56:30 +02:00
|
|
|
if (selectedApp != null) {
|
2022-08-14 04:07:28 +02:00
|
|
|
locator<PatcherAPI>().sharePatchedFile(
|
|
|
|
selectedApp.name,
|
|
|
|
selectedApp.version,
|
|
|
|
);
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 04:07:28 +02:00
|
|
|
|
2022-08-15 04:31:36 +02:00
|
|
|
Future<void> cleanWorkplace() async {
|
2022-08-14 04:07:28 +02:00
|
|
|
locator<PatcherAPI>().cleanPatcher();
|
|
|
|
locator<AppSelectorViewModel>().selectedApp = null;
|
|
|
|
locator<PatchesSelectorViewModel>().selectedPatches.clear();
|
|
|
|
locator<PatcherViewModel>().notifyListeners();
|
|
|
|
}
|
2022-08-15 04:31:36 +02:00
|
|
|
|
|
|
|
void openApp() {
|
|
|
|
PatchedApplication? selectedApp =
|
|
|
|
locator<AppSelectorViewModel>().selectedApp;
|
|
|
|
if (selectedApp != null) {
|
|
|
|
DeviceApps.openApp(selectedApp.packageName);
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 19:42:09 +02:00
|
|
|
|
|
|
|
Future<void> saveApp(PatchedApplication selectedApp) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
List<String> patchedApps = prefs.getStringList('patchedApps') ?? [];
|
|
|
|
String app = selectedApp.toJson().toString();
|
|
|
|
if (!patchedApps.contains(app)) {
|
|
|
|
patchedApps.add(app);
|
|
|
|
prefs.setStringList('patchedApps', patchedApps);
|
|
|
|
}
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|