2022-09-23 16:47:30 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
2022-08-15 04:31:36 +02:00
|
|
|
import 'package:device_apps/device_apps.dart';
|
2022-09-07 14:04:16 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-18 16:33:33 +02:00
|
|
|
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-09-18 23:13:29 +02:00
|
|
|
import 'package:permission_handler/permission_handler.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-09-23 16:47:30 +02:00
|
|
|
import 'package:revanced_manager/services/root_api.dart';
|
2022-09-20 01:49:31 +02:00
|
|
|
import 'package:revanced_manager/services/toast.dart';
|
2022-08-14 04:07:28 +02:00
|
|
|
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
|
2022-09-23 09:30:30 +02:00
|
|
|
import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart';
|
2022-10-14 20:05:33 +02:00
|
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
2022-09-12 01:34:36 +02:00
|
|
|
import 'package:wakelock/wakelock.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
|
|
|
|
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>();
|
2022-09-23 16:47:30 +02:00
|
|
|
final RootAPI _rootAPI = RootAPI();
|
2022-09-20 01:49:31 +02:00
|
|
|
final Toast _toast = locator<Toast>();
|
2022-09-06 15:40:49 +02:00
|
|
|
final PatchedApplication _app = locator<PatcherViewModel>().selectedApp!;
|
2022-08-18 16:33:33 +02:00
|
|
|
final List<Patch> _patches = locator<PatcherViewModel>().selectedPatches;
|
|
|
|
static const _installerChannel = MethodChannel(
|
2022-09-12 14:53:34 +02:00
|
|
|
'app.revanced.manager.flutter/installer',
|
2022-08-18 16:33:33 +02:00
|
|
|
);
|
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-09-23 16:47:30 +02:00
|
|
|
bool isRooted = false;
|
2022-08-24 15:17:16 +02:00
|
|
|
bool isPatching = true;
|
2022-08-15 04:31:36 +02:00
|
|
|
bool isInstalled = false;
|
2022-09-12 15:20:25 +02:00
|
|
|
bool hasErrors = false;
|
2022-08-13 11:56:30 +02:00
|
|
|
|
2022-08-18 16:33:33 +02:00
|
|
|
Future<void> initialize(BuildContext context) async {
|
2022-09-23 16:47:30 +02:00
|
|
|
isRooted = await _rootAPI.isRooted();
|
2022-09-18 23:13:29 +02:00
|
|
|
if (await Permission.ignoreBatteryOptimizations.isGranted) {
|
2022-09-17 15:55:00 +02:00
|
|
|
try {
|
2022-09-19 02:46:12 +02:00
|
|
|
FlutterBackground.initialize(
|
2022-09-17 15:55:00 +02:00
|
|
|
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',
|
|
|
|
),
|
2022-08-17 14:48:56 +02:00
|
|
|
),
|
2022-09-19 02:46:12 +02:00
|
|
|
).then((value) => FlutterBackground.enableBackgroundExecution());
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-09-17 15:55:00 +02:00
|
|
|
// ignore
|
|
|
|
}
|
2022-08-17 14:48:56 +02:00
|
|
|
}
|
2022-09-17 15:55:00 +02:00
|
|
|
await Wakelock.enable();
|
2022-09-11 03:01:06 +02:00
|
|
|
await handlePlatformChannelMethods();
|
|
|
|
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-09-23 01:38:42 +02:00
|
|
|
if (value >= 0.0) {
|
2022-09-05 14:48:56 +02:00
|
|
|
progress = value;
|
|
|
|
}
|
2022-09-23 01:38:42 +02:00
|
|
|
if (value == 0.0) {
|
2022-08-13 11:56:30 +02:00
|
|
|
logs = '';
|
2022-09-23 01:38:42 +02:00
|
|
|
isPatching = true;
|
2022-08-29 16:01:51 +02:00
|
|
|
isInstalled = false;
|
2022-09-12 15:20:25 +02:00
|
|
|
hasErrors = false;
|
2022-09-23 01:38:42 +02:00
|
|
|
} else if (value == 1.0) {
|
|
|
|
isPatching = false;
|
|
|
|
hasErrors = false;
|
|
|
|
} else if (value == -100.0) {
|
|
|
|
isPatching = false;
|
|
|
|
hasErrors = true;
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
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-09-23 01:38:42 +02:00
|
|
|
if (logs[logs.length - 1] == '\n') {
|
|
|
|
logs = logs.substring(0, logs.length - 1);
|
|
|
|
}
|
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-10-14 20:05:33 +02:00
|
|
|
try {
|
|
|
|
update(0.0, 'Initializing...', 'Initializing installer');
|
|
|
|
if (_patches.isNotEmpty) {
|
|
|
|
try {
|
|
|
|
update(0.1, '', 'Creating working directory');
|
|
|
|
await _patcherAPI.runPatcher(
|
|
|
|
_app.packageName,
|
|
|
|
_app.apkFilePath,
|
|
|
|
_patches,
|
|
|
|
);
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
update(
|
|
|
|
-100.0,
|
|
|
|
'Aborting...',
|
|
|
|
'An error occurred! Aborting\nError:\n$e',
|
|
|
|
);
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
|
|
|
throw await Sentry.captureException(e, stackTrace: s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
update(-100.0, 'Aborting...', 'No app or patches selected! Aborting');
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
if (FlutterBackground.isBackgroundExecutionEnabled) {
|
|
|
|
try {
|
|
|
|
FlutterBackground.disableBackgroundExecution();
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
|
|
|
// ignore
|
|
|
|
}
|
2022-09-17 15:55:00 +02:00
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
await Wakelock.disable();
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-08-17 14:48:56 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 02:44:27 +02:00
|
|
|
void installResult(BuildContext context, bool installAsRoot) async {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
|
|
|
_app.isRooted = installAsRoot;
|
|
|
|
bool hasMicroG = _patches.any((p) => p.name.endsWith('microg-support'));
|
|
|
|
bool rootMicroG = installAsRoot && hasMicroG;
|
|
|
|
bool rootFromStorage = installAsRoot && _app.isFromStorage;
|
|
|
|
bool ytWithoutRootMicroG =
|
|
|
|
!installAsRoot && !hasMicroG && _app.packageName.contains('youtube');
|
|
|
|
if (rootMicroG || rootFromStorage || ytWithoutRootMicroG) {
|
|
|
|
return showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
title: I18nText('installerView.installErrorDialogTitle'),
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
|
|
|
|
content: I18nText(
|
|
|
|
rootMicroG
|
|
|
|
? 'installerView.installErrorDialogText1'
|
|
|
|
: rootFromStorage
|
|
|
|
? 'installerView.installErrorDialogText3'
|
|
|
|
: 'installerView.installErrorDialogText2',
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
CustomMaterialButton(
|
|
|
|
label: I18nText('okButton'),
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
)
|
|
|
|
],
|
2022-09-19 02:44:27 +02:00
|
|
|
),
|
2022-10-14 20:05:33 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
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.isFromStorage = false;
|
|
|
|
_app.patchDate = DateTime.now();
|
|
|
|
_app.appliedPatches = _patches.map((p) => p.name).toList();
|
|
|
|
if (hasMicroG) {
|
|
|
|
_app.name += ' ReVanced';
|
|
|
|
_app.packageName = _app.packageName.replaceFirst(
|
|
|
|
'com.google.',
|
|
|
|
'app.revanced.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await _managerAPI.savePatchedApp(_app);
|
2022-09-19 02:44:27 +02:00
|
|
|
}
|
2022-09-15 00:40:08 +02:00
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2022-11-02 12:52:40 +01:00
|
|
|
void exportResult() {
|
|
|
|
try {
|
|
|
|
_patcherAPI.exportPatchedFile(_app.name, _app.version);
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
Sentry.captureException(e, stackTrace: s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
void shareResult() {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
2022-10-18 15:16:04 +02:00
|
|
|
_patcherAPI.sharePatchedFile(_app.name, _app.version);
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
Sentry.captureException(e, stackTrace: s);
|
|
|
|
}
|
2022-09-06 15:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void shareLog() {
|
2022-09-14 11:40:45 +02:00
|
|
|
_patcherAPI.sharePatcherLog(logs);
|
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-10-14 20:05:33 +02:00
|
|
|
try {
|
|
|
|
_patcherAPI.cleanPatcher();
|
|
|
|
locator<PatcherViewModel>().selectedApp = null;
|
|
|
|
locator<PatcherViewModel>().selectedPatches.clear();
|
|
|
|
locator<PatcherViewModel>().notifyListeners();
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
|
|
|
}
|
2022-08-14 04:07:28 +02:00
|
|
|
}
|
2022-08-15 04:31:36 +02:00
|
|
|
|
|
|
|
void openApp() {
|
2022-09-06 15:40:49 +02:00
|
|
|
DeviceApps.openApp(_app.packageName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onMenuSelection(int value) {
|
|
|
|
switch (value) {
|
|
|
|
case 0:
|
|
|
|
shareResult();
|
|
|
|
break;
|
|
|
|
case 1:
|
2022-11-02 12:52:40 +01:00
|
|
|
exportResult();
|
|
|
|
break;
|
|
|
|
case 2:
|
2022-09-06 15:40:49 +02:00
|
|
|
shareLog();
|
|
|
|
break;
|
2022-08-15 04:31:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-19 18:40:06 +02:00
|
|
|
|
|
|
|
Future<bool> onWillPop(BuildContext context) async {
|
|
|
|
if (isPatching) {
|
2022-11-12 16:55:33 +01:00
|
|
|
_toast.showBottom('installerView.noExit');
|
2022-09-19 18:40:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cleanPatcher();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|