2022-08-06 15:04:18 +02:00
|
|
|
import 'dart:io';
|
2023-01-30 13:35:06 +01:00
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:app_installer/app_installer.dart';
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:collection/collection.dart';
|
2023-01-30 13:35:06 +01:00
|
|
|
import 'package:cr_file_saver/file_saver.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:device_apps/device_apps.dart';
|
2023-01-30 13:35:06 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-06 15:04:18 +02:00
|
|
|
import 'package:flutter/services.dart';
|
2022-08-09 01:01:06 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2022-08-13 11:56:30 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-08-25 01:51:47 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-08-06 23:35:35 +02:00
|
|
|
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-18 16:33:33 +02:00
|
|
|
import 'package:revanced_manager/services/manager_api.dart';
|
2022-08-14 20:40:34 +02:00
|
|
|
import 'package:revanced_manager/services/root_api.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:share_extend/share_extend.dart';
|
2022-08-06 15:04:18 +02:00
|
|
|
|
2022-08-09 01:01:06 +02:00
|
|
|
@lazySingleton
|
2022-08-09 02:20:50 +02:00
|
|
|
class PatcherAPI {
|
2022-09-12 14:53:34 +02:00
|
|
|
static const patcherChannel =
|
|
|
|
MethodChannel('app.revanced.manager.flutter/patcher');
|
2022-08-25 01:51:47 +02:00
|
|
|
final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
2022-08-18 16:33:33 +02:00
|
|
|
final RootAPI _rootAPI = RootAPI();
|
2022-10-09 20:35:43 +02:00
|
|
|
late Directory _dataDir;
|
2022-08-30 03:18:20 +02:00
|
|
|
late Directory _tmpDir;
|
2022-09-07 19:25:12 +02:00
|
|
|
late File _keyStoreFile;
|
2022-08-29 18:44:45 +02:00
|
|
|
List<Patch> _patches = [];
|
2022-12-15 19:05:45 +01:00
|
|
|
Map filteredPatches = <String, List<Patch>>{};
|
2022-08-13 11:56:30 +02:00
|
|
|
File? _outFile;
|
2022-08-09 01:01:06 +02:00
|
|
|
|
2022-08-29 18:44:45 +02:00
|
|
|
Future<void> initialize() async {
|
|
|
|
await _loadPatches();
|
2023-01-30 13:35:06 +01:00
|
|
|
final Directory appCache = await getTemporaryDirectory();
|
2022-10-09 20:35:43 +02:00
|
|
|
_dataDir = await getExternalStorageDirectory() ?? appCache;
|
2022-08-30 03:18:20 +02:00
|
|
|
_tmpDir = Directory('${appCache.path}/patcher');
|
2022-10-09 20:35:43 +02:00
|
|
|
_keyStoreFile = File('${_dataDir.path}/revanced-manager.keystore');
|
2022-08-30 03:18:20 +02:00
|
|
|
cleanPatcher();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanPatcher() {
|
|
|
|
if (_tmpDir.existsSync()) {
|
|
|
|
_tmpDir.deleteSync(recursive: true);
|
|
|
|
}
|
2022-08-19 20:13:43 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:44:45 +02:00
|
|
|
Future<void> _loadPatches() async {
|
|
|
|
try {
|
|
|
|
if (_patches.isEmpty) {
|
2022-09-11 03:01:06 +02:00
|
|
|
_patches = await _managerAPI.getPatches();
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-08-29 18:44:45 +02:00
|
|
|
_patches = List.empty();
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 13:35:06 +01:00
|
|
|
Future<List<ApplicationWithIcon>> getFilteredInstalledApps(
|
|
|
|
bool showUniversalPatches,
|
|
|
|
) async {
|
|
|
|
final List<ApplicationWithIcon> filteredApps = [];
|
|
|
|
final bool allAppsIncluded =
|
|
|
|
_patches.any((patch) => patch.compatiblePackages.isEmpty) &&
|
|
|
|
showUniversalPatches;
|
2022-12-11 13:30:44 +01:00
|
|
|
if (allAppsIncluded) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final allPackages = await DeviceApps.getInstalledApplications(
|
2022-12-11 13:30:44 +01:00
|
|
|
includeAppIcons: true,
|
|
|
|
onlyAppsWithLaunchIntent: true,
|
|
|
|
);
|
2023-01-30 13:35:06 +01:00
|
|
|
for (final pkg in allPackages) {
|
2022-12-11 13:30:44 +01:00
|
|
|
if (!filteredApps.any((app) => app.packageName == pkg.packageName)) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final appInfo = await DeviceApps.getApp(
|
2022-12-11 13:30:44 +01:00
|
|
|
pkg.packageName,
|
|
|
|
true,
|
|
|
|
) as ApplicationWithIcon?;
|
|
|
|
if (appInfo != null) {
|
|
|
|
filteredApps.add(appInfo);
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 13:35:06 +01:00
|
|
|
}
|
2022-12-11 13:30:44 +01:00
|
|
|
}
|
2023-01-30 13:35:06 +01:00
|
|
|
for (final Patch patch in _patches) {
|
|
|
|
for (final Package package in patch.compatiblePackages) {
|
2022-08-22 02:31:27 +02:00
|
|
|
try {
|
2022-08-29 18:44:45 +02:00
|
|
|
if (!filteredApps.any((app) => app.packageName == package.name)) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final ApplicationWithIcon? app = await DeviceApps.getApp(
|
2022-09-20 02:07:36 +02:00
|
|
|
package.name,
|
|
|
|
true,
|
|
|
|
) as ApplicationWithIcon?;
|
2022-08-29 18:44:45 +02:00
|
|
|
if (app != null) {
|
|
|
|
filteredApps.add(app);
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-08-29 18:44:45 +02:00
|
|
|
continue;
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
2022-08-17 19:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-29 18:44:45 +02:00
|
|
|
return filteredApps;
|
2022-08-17 19:44:27 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 19:05:45 +01:00
|
|
|
List<Patch> getFilteredPatches(String packageName) {
|
|
|
|
if (!filteredPatches.keys.contains(packageName)) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<Patch> patches = _patches
|
|
|
|
.where(
|
|
|
|
(patch) =>
|
|
|
|
patch.compatiblePackages.isEmpty ||
|
|
|
|
!patch.name.contains('settings') &&
|
|
|
|
patch.compatiblePackages
|
|
|
|
.any((pack) => pack.name == packageName),
|
|
|
|
)
|
2022-12-15 19:05:45 +01:00
|
|
|
.toList();
|
|
|
|
filteredPatches[packageName] = patches;
|
|
|
|
}
|
|
|
|
return filteredPatches[packageName];
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:44:45 +02:00
|
|
|
Future<List<Patch>> getAppliedPatches(List<String> appliedPatches) async {
|
|
|
|
return _patches
|
|
|
|
.where((patch) => appliedPatches.contains(patch.name))
|
|
|
|
.toList();
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:20:19 +02:00
|
|
|
bool dependencyNeedsIntegrations(String name) {
|
|
|
|
return name.contains('integrations') ||
|
|
|
|
_patches.any(
|
|
|
|
(patch) =>
|
|
|
|
patch.name == name &&
|
|
|
|
(patch.dependencies.any(
|
|
|
|
(dep) => dependencyNeedsIntegrations(dep),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-15 02:15:13 +02:00
|
|
|
Future<bool> needsIntegrations(List<Patch> selectedPatches) async {
|
|
|
|
return selectedPatches.any(
|
2022-09-22 15:02:07 +02:00
|
|
|
(patch) => patch.dependencies.any(
|
2022-09-23 18:20:19 +02:00
|
|
|
(dep) => dependencyNeedsIntegrations(dep),
|
2022-09-22 15:02:07 +02:00
|
|
|
),
|
2022-09-15 02:15:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> needsResourcePatching(List<Patch> selectedPatches) async {
|
|
|
|
return selectedPatches.any(
|
2022-09-22 15:02:07 +02:00
|
|
|
(patch) => patch.dependencies.any(
|
|
|
|
(dep) => dep.contains('resource-'),
|
|
|
|
),
|
2022-09-15 02:15:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> needsSettingsPatch(List<Patch> selectedPatches) async {
|
|
|
|
return selectedPatches.any(
|
2022-09-22 15:02:07 +02:00
|
|
|
(patch) => patch.dependencies.any(
|
|
|
|
(dep) => dep.contains('settings'),
|
|
|
|
),
|
2022-09-15 02:15:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:59:56 +02:00
|
|
|
Future<String> getOriginalFilePath(
|
2022-08-29 18:44:45 +02:00
|
|
|
String packageName,
|
2022-08-17 13:48:03 +02:00
|
|
|
String originalFilePath,
|
2022-09-13 17:54:43 +02:00
|
|
|
) async {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
|
2022-10-14 20:05:33 +02:00
|
|
|
if (hasRootPermissions) {
|
|
|
|
originalFilePath = await _rootAPI.getOriginalFilePath(
|
|
|
|
packageName,
|
|
|
|
originalFilePath,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return originalFilePath;
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
|
|
|
return originalFilePath;
|
2022-09-13 17:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> runPatcher(
|
|
|
|
String packageName,
|
2022-09-14 10:59:56 +02:00
|
|
|
String originalFilePath,
|
2022-08-17 13:48:03 +02:00
|
|
|
List<Patch> selectedPatches,
|
|
|
|
) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool mergeIntegrations = await needsIntegrations(selectedPatches);
|
|
|
|
final bool includeSettings = await needsSettingsPatch(selectedPatches);
|
2022-08-29 18:44:45 +02:00
|
|
|
if (includeSettings) {
|
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Patch? settingsPatch = _patches.firstWhereOrNull(
|
2022-08-29 18:44:45 +02:00
|
|
|
(patch) =>
|
|
|
|
patch.name.contains('settings') &&
|
|
|
|
patch.compatiblePackages.any((pack) => pack.name == packageName),
|
|
|
|
);
|
2022-09-11 03:01:06 +02:00
|
|
|
if (settingsPatch != null) {
|
|
|
|
selectedPatches.add(settingsPatch);
|
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-08-29 18:44:45 +02:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 13:35:06 +01:00
|
|
|
final File? patchBundleFile = await _managerAPI.downloadPatches();
|
2022-08-29 18:44:45 +02:00
|
|
|
File? integrationsFile;
|
|
|
|
if (mergeIntegrations) {
|
2022-09-11 03:01:06 +02:00
|
|
|
integrationsFile = await _managerAPI.downloadIntegrations();
|
2022-08-29 18:44:45 +02:00
|
|
|
}
|
|
|
|
if (patchBundleFile != null) {
|
2022-10-09 20:35:43 +02:00
|
|
|
_dataDir.createSync();
|
2022-08-30 03:18:20 +02:00
|
|
|
_tmpDir.createSync();
|
2023-01-30 13:35:06 +01:00
|
|
|
final Directory workDir = _tmpDir.createTempSync('tmp-');
|
|
|
|
final File inputFile = File('${workDir.path}/base.apk');
|
|
|
|
final File patchedFile = File('${workDir.path}/patched.apk');
|
2022-08-29 18:44:45 +02:00
|
|
|
_outFile = File('${workDir.path}/out.apk');
|
2023-01-30 13:35:06 +01:00
|
|
|
final Directory cacheDir = Directory('${workDir.path}/cache');
|
2022-08-29 18:44:45 +02:00
|
|
|
cacheDir.createSync();
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
|
|
|
await patcherChannel.invokeMethod(
|
|
|
|
'runPatcher',
|
|
|
|
{
|
|
|
|
'patchBundleFilePath': patchBundleFile.path,
|
|
|
|
'originalFilePath': await getOriginalFilePath(
|
|
|
|
packageName,
|
|
|
|
originalFilePath,
|
|
|
|
),
|
|
|
|
'inputFilePath': inputFile.path,
|
|
|
|
'patchedFilePath': patchedFile.path,
|
|
|
|
'outFilePath': _outFile!.path,
|
|
|
|
'integrationsPath': mergeIntegrations ? integrationsFile!.path : '',
|
|
|
|
'selectedPatches': selectedPatches.map((p) => p.name).toList(),
|
|
|
|
'cacheDirPath': cacheDir.path,
|
|
|
|
'mergeIntegrations': mergeIntegrations,
|
|
|
|
'keyStoreFilePath': _keyStoreFile.path,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on Exception catch (e, s) {
|
2023-01-30 13:35:06 +01:00
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
throw await Sentry.captureException(e, stackTrace: s);
|
|
|
|
}
|
2022-08-29 18:44:45 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 20:40:34 +02:00
|
|
|
Future<bool> installPatchedFile(PatchedApplication patchedApp) async {
|
2022-08-13 11:56:30 +02:00
|
|
|
if (_outFile != null) {
|
|
|
|
try {
|
2022-09-05 14:43:13 +02:00
|
|
|
if (patchedApp.isRooted) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
|
2022-09-06 15:40:49 +02:00
|
|
|
if (hasRootPermissions) {
|
|
|
|
return _rootAPI.installApp(
|
|
|
|
patchedApp.packageName,
|
|
|
|
patchedApp.apkFilePath,
|
|
|
|
_outFile!.path,
|
|
|
|
);
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
} else {
|
|
|
|
await AppInstaller.installApk(_outFile!.path);
|
2022-08-17 18:07:00 +02:00
|
|
|
return await DeviceApps.isAppInstalled(patchedApp.packageName);
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
} on Exception catch (e, s) {
|
|
|
|
await Sentry.captureException(e, stackTrace: s);
|
2022-08-13 11:56:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-02 12:52:40 +01:00
|
|
|
void exportPatchedFile(String appName, String version) {
|
|
|
|
try {
|
|
|
|
if (_outFile != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String newName = _getFileName(appName, version);
|
2022-11-02 12:52:40 +01:00
|
|
|
CRFileSaver.saveFileWithDialog(SaveFileDialogParams(
|
2023-01-30 13:35:06 +01:00
|
|
|
sourceFilePath: _outFile!.path, destinationFileName: newName,),);
|
2022-11-02 12:52:40 +01:00
|
|
|
}
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
Sentry.captureException(e, stackTrace: s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:40:49 +02:00
|
|
|
void sharePatchedFile(String appName, String version) {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
|
|
|
if (_outFile != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String newName = _getFileName(appName, version);
|
|
|
|
final int lastSeparator = _outFile!.path.lastIndexOf('/');
|
|
|
|
final String newPath =
|
2022-10-14 20:05:33 +02:00
|
|
|
_outFile!.path.substring(0, lastSeparator + 1) + newName;
|
2023-01-30 13:35:06 +01:00
|
|
|
final File shareFile = _outFile!.copySync(newPath);
|
2022-10-14 20:05:33 +02:00
|
|
|
ShareExtend.share(shareFile.path, 'file');
|
|
|
|
}
|
|
|
|
} on Exception catch (e, s) {
|
|
|
|
Sentry.captureException(e, stackTrace: s);
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
2022-08-15 04:31:36 +02:00
|
|
|
|
2022-11-02 12:52:40 +01:00
|
|
|
String _getFileName(String appName, String version) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String prefix = appName.toLowerCase().replaceAll(' ', '-');
|
|
|
|
final String newName = '$prefix-revanced_v$version.apk';
|
2022-12-11 13:30:44 +01:00
|
|
|
return newName;
|
2022-11-02 12:52:40 +01:00
|
|
|
}
|
|
|
|
|
2022-09-14 11:40:45 +02:00
|
|
|
Future<void> sharePatcherLog(String logs) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Directory appCache = await getTemporaryDirectory();
|
|
|
|
final Directory logDir = Directory('${appCache.path}/logs');
|
2022-09-14 11:40:45 +02:00
|
|
|
logDir.createSync();
|
2023-01-30 13:35:06 +01:00
|
|
|
final String dateTime = DateTime.now()
|
2022-09-14 11:40:45 +02:00
|
|
|
.toIso8601String()
|
|
|
|
.replaceAll('-', '')
|
|
|
|
.replaceAll(':', '')
|
|
|
|
.replaceAll('T', '')
|
|
|
|
.replaceAll('.', '');
|
2023-01-30 13:35:06 +01:00
|
|
|
final File log =
|
|
|
|
File('${logDir.path}/revanced-manager_patcher_$dateTime.log');
|
2022-09-14 11:40:45 +02:00
|
|
|
log.writeAsStringSync(logs);
|
|
|
|
ShareExtend.share(log.path, 'file');
|
2022-09-06 15:40:49 +02:00
|
|
|
}
|
2022-09-18 04:14:48 +02:00
|
|
|
|
|
|
|
String getRecommendedVersion(String packageName) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Map<String, int> versions = {};
|
|
|
|
for (final Patch patch in _patches) {
|
|
|
|
final Package? package = patch.compatiblePackages.firstWhereOrNull(
|
2022-09-18 04:14:48 +02:00
|
|
|
(pack) => pack.name == packageName,
|
|
|
|
);
|
|
|
|
if (package != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
for (final String version in package.versions) {
|
2022-09-18 04:14:48 +02:00
|
|
|
versions.update(
|
|
|
|
version,
|
|
|
|
(value) => versions[version]! + 1,
|
|
|
|
ifAbsent: () => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (versions.isNotEmpty) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final entries = versions.entries.toList()
|
2022-09-18 04:14:48 +02:00
|
|
|
..sort((a, b) => a.value.compareTo(b.value));
|
|
|
|
versions
|
|
|
|
..clear()
|
|
|
|
..addEntries(entries);
|
|
|
|
versions.removeWhere((key, value) => value != versions.values.last);
|
|
|
|
return (versions.keys.toList()..sort()).last;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|