2022-08-17 18:07:00 +02:00
|
|
|
import 'dart:convert';
|
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-16 15:06:56 +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 {
|
2022-08-17 13:48:03 +02:00
|
|
|
double? progress = 0.0;
|
2022-08-13 11:56:30 +02:00
|
|
|
String logs = '';
|
|
|
|
bool isPatching = false;
|
2022-08-15 04:31:36 +02:00
|
|
|
bool isInstalled = false;
|
2022-08-13 11:56:30 +02:00
|
|
|
|
|
|
|
Future<void> initialize() async {
|
2022-08-17 14:48:56 +02:00
|
|
|
try {
|
|
|
|
await FlutterBackground.initialize(
|
|
|
|
androidConfig: const FlutterBackgroundAndroidConfig(
|
|
|
|
notificationTitle: 'Patching',
|
|
|
|
notificationText: 'ReVanced Manager is patching',
|
|
|
|
notificationImportance: AndroidNotificationImportance.Default,
|
|
|
|
notificationIcon: AndroidResource(
|
|
|
|
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 {
|
|
|
|
await locator<PatcherAPI>().handlePlatformChannelMethods();
|
|
|
|
await runPatcher();
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (progress == 0.0) {
|
|
|
|
logs = '';
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-17 13:48:03 +02:00
|
|
|
void updateLog(String message) {
|
|
|
|
if (message.isNotEmpty && !message.startsWith('Merging L')) {
|
|
|
|
if (logs.isNotEmpty) {
|
|
|
|
logs += '\n';
|
|
|
|
}
|
|
|
|
logs += message;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
Future<void> runPatcher() async {
|
|
|
|
updateProgress(0.0);
|
2022-08-14 20:40:34 +02:00
|
|
|
PatchedApplication? selectedApp =
|
|
|
|
locator<AppSelectorViewModel>().selectedApp;
|
2022-08-17 13:48:03 +02:00
|
|
|
List<Patch> selectedPatches =
|
|
|
|
locator<PatchesSelectorViewModel>().selectedPatches;
|
|
|
|
if (selectedApp != null && selectedPatches.isNotEmpty) {
|
2022-08-13 11:56:30 +02:00
|
|
|
String apkFilePath = selectedApp.apkFilePath;
|
2022-08-17 13:48:03 +02:00
|
|
|
try {
|
|
|
|
updateLog('Initializing installer');
|
2022-08-15 11:55:17 +02:00
|
|
|
if (selectedApp.isRooted && !selectedApp.isFromStorage) {
|
2022-08-17 13:48:03 +02:00
|
|
|
updateLog('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-17 13:48:03 +02:00
|
|
|
updateLog('Deleting old patched version');
|
2022-08-15 04:31:36 +02:00
|
|
|
await locator<PatcherAPI>().deleteOldPatch(selectedApp);
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 13:48:03 +02:00
|
|
|
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;
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-08-17 13:48:03 +02:00
|
|
|
await locator<PatcherAPI>().initPatcher(mergeIntegrations);
|
|
|
|
await locator<PatcherAPI>().runPatcher(
|
|
|
|
apkFilePath,
|
|
|
|
selectedPatches,
|
|
|
|
mergeIntegrations,
|
|
|
|
resourcePatching,
|
|
|
|
);
|
|
|
|
} on Exception {
|
|
|
|
updateLog('An error occurred! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 13:48:03 +02:00
|
|
|
updateLog('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-14 20:40:34 +02:00
|
|
|
PatchedApplication? selectedApp =
|
|
|
|
locator<AppSelectorViewModel>().selectedApp;
|
2022-08-17 19:44:27 +02:00
|
|
|
List<Patch> selectedPatches =
|
|
|
|
locator<PatchesSelectorViewModel>().selectedPatches;
|
2022-08-14 20:40:34 +02:00
|
|
|
if (selectedApp != null) {
|
2022-08-17 13:48:03 +02:00
|
|
|
updateLog(selectedApp.isRooted
|
2022-08-16 15:06:56 +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-17 13:48:03 +02:00
|
|
|
updateLog('Done');
|
2022-08-17 19:44:27 +02:00
|
|
|
selectedApp.appliedPatches
|
|
|
|
.addAll(selectedPatches.map((p) => p.name).toList());
|
2022-08-16 15:06:56 +02:00
|
|
|
await saveApp(selectedApp);
|
2022-08-14 22:32:03 +02:00
|
|
|
} else {
|
2022-08-17 13:48:03 +02:00
|
|
|
updateLog('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-16 15:06:56 +02:00
|
|
|
|
|
|
|
Future<void> saveApp(PatchedApplication selectedApp) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
List<String> patchedApps = prefs.getStringList('patchedApps') ?? [];
|
2022-08-17 18:07:00 +02:00
|
|
|
String app = json.encode(selectedApp.toJson());
|
2022-08-17 19:44:27 +02:00
|
|
|
patchedApps.remove(app);
|
|
|
|
patchedApps.add(app);
|
|
|
|
prefs.setStringList('patchedApps', patchedApps);
|
2022-08-16 15:06:56 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|