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';
|
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-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-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-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-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
|
|
|
|
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) {
|
|
|
|
case 'updateProgress':
|
|
|
|
if (call.arguments != null) {
|
|
|
|
updateProgress(call.arguments);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'updateLog':
|
|
|
|
if (call.arguments != null) {
|
|
|
|
updateLog(call.arguments);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-18 16:33:33 +02:00
|
|
|
if (_app != null && _patches.isNotEmpty) {
|
|
|
|
String apkFilePath = _app!.apkFilePath;
|
2022-08-17 13:48:03 +02:00
|
|
|
try {
|
|
|
|
updateLog('Initializing installer');
|
2022-08-18 16:33:33 +02:00
|
|
|
if (_app!.isRooted && !_app!.isFromStorage) {
|
2022-08-17 13:48:03 +02:00
|
|
|
updateLog('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-17 13:48:03 +02:00
|
|
|
updateLog('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-17 13:48:03 +02:00
|
|
|
updateLog('Creating working directory');
|
|
|
|
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-18 16:33:33 +02:00
|
|
|
await _patcherAPI.initPatcher(mergeIntegrations);
|
|
|
|
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 {
|
|
|
|
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-18 16:33:33 +02:00
|
|
|
if (_app != null) {
|
|
|
|
updateLog(_app!.isRooted
|
2022-08-16 15:06:56 +02:00
|
|
|
? '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-17 13:48:03 +02:00
|
|
|
updateLog('Done');
|
2022-08-18 16:33:33 +02:00
|
|
|
_app!.patchDate = DateTime.now();
|
|
|
|
_app!.appliedPatches.addAll(_patches.map((p) => p.name).toList());
|
|
|
|
await saveApp();
|
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-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-15 04:31:36 +02:00
|
|
|
Future<void> cleanWorkplace() 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-16 15:06:56 +02:00
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
Future<void> saveApp() async {
|
|
|
|
if (_app != null) {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
List<String> patchedApps = prefs.getStringList('patchedApps') ?? [];
|
|
|
|
String appStr = json.encode(_app!.toJson());
|
|
|
|
patchedApps.removeWhere(
|
|
|
|
(a) => json.decode(a)['packageName'] == _app!.packageName);
|
|
|
|
patchedApps.add(appStr);
|
|
|
|
prefs.setStringList('patchedApps', patchedApps);
|
|
|
|
}
|
2022-08-16 15:06:56 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|