revanced-manager/lib/services/patcher_api.dart

107 lines
3.6 KiB
Dart
Raw Normal View History

2022-08-06 15:04:18 +02:00
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
2022-08-09 01:01:06 +02:00
import 'package:injectable/injectable.dart';
2022-08-06 15:04:18 +02:00
import 'package:installed_apps/app_info.dart';
import 'package:installed_apps/installed_apps.dart';
2022-08-06 23:35:35 +02:00
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/utils/string.dart';
2022-08-06 15:04:18 +02:00
2022-08-09 01:01:06 +02:00
@lazySingleton
class PatcherAPI {
2022-08-09 01:01:06 +02:00
final GithubAPI githubAPI = GithubAPI();
2022-08-06 15:04:18 +02:00
final List<AppInfo> _filteredPackages = [];
final Map<String, List<Patch>> _filteredPatches = <String, List<Patch>>{};
2022-08-09 01:01:06 +02:00
File? _patchBundleFile;
2022-08-06 15:04:18 +02:00
static const platform = MethodChannel('app.revanced/patcher');
2022-08-09 01:01:06 +02:00
2022-08-06 15:04:18 +02:00
Future<void> loadPatches() async {
if (_patchBundleFile == null) {
String? dexFileUrl =
await githubAPI.latestRelease('revanced', 'revanced-patches');
if (dexFileUrl != null && dexFileUrl.isNotEmpty) {
2022-08-06 15:04:18 +02:00
_patchBundleFile =
await DefaultCacheManager().getSingleFile(dexFileUrl);
try {
await platform.invokeMethod(
'loadPatches',
{
'pathBundlesPaths': <String>[_patchBundleFile!.absolute.path],
},
);
} on PlatformException {
_patchBundleFile = null;
}
}
}
}
Future<List<AppInfo>> getFilteredInstalledApps() async {
if (_patchBundleFile != null && _filteredPackages.isEmpty) {
try {
List<String>? patchesPackages =
await platform.invokeListMethod<String>('getCompatiblePackages');
if (patchesPackages != null) {
for (String package in patchesPackages) {
try {
AppInfo app = await InstalledApps.getAppInfo(package);
2022-08-06 15:04:18 +02:00
_filteredPackages.add(app);
} catch (e) {
continue;
2022-08-06 15:04:18 +02:00
}
}
}
} on PlatformException {
_filteredPackages.clear();
2022-08-06 15:04:18 +02:00
return List.empty();
}
}
return _filteredPackages;
}
Future<List<Patch>?> getFilteredPatches(AppInfo? targetApp) async {
if (_patchBundleFile != null && targetApp != null) {
if (_filteredPatches[targetApp.packageName] == null ||
_filteredPatches[targetApp.packageName]!.isEmpty) {
_filteredPatches[targetApp.packageName!] = [];
try {
var patches = await platform.invokeListMethod<Map<dynamic, dynamic>>(
'getFilteredPatches',
{
'targetPackage': targetApp.packageName,
'targetVersion': targetApp.versionName,
'ignoreVersion': true,
},
);
if (patches != null) {
for (var patch in patches) {
if (!_filteredPatches[targetApp.packageName]!
.any((element) => element.name == patch['name'])) {
_filteredPatches[targetApp.packageName]!.add(
Patch(
name: patch['name'],
simpleName: (patch['name'] as String)
.replaceAll('-', ' ')
.split('-')
.join(' ')
.toTitleCase(),
version: patch['version'] ?? '?.?.?',
description: patch['description'] ?? 'N/A',
),
);
}
2022-08-06 15:04:18 +02:00
}
}
} on PlatformException {
_filteredPatches[targetApp.packageName]!.clear();
2022-08-06 15:04:18 +02:00
return List.empty();
}
}
} else {
return List.empty();
}
return _filteredPatches[targetApp.packageName];
}
}