2022-08-25 01:51:47 +02:00
|
|
|
import 'dart:convert';
|
2022-08-01 21:05:11 +02:00
|
|
|
import 'dart:io';
|
2022-08-25 01:51:47 +02:00
|
|
|
import 'package:device_apps/device_apps.dart';
|
2022-08-29 16:01:51 +02:00
|
|
|
import 'package:github/github.dart';
|
2022-08-25 01:51:47 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2022-08-18 18:32:58 +02:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2022-08-06 23:35:35 +02:00
|
|
|
import 'package:revanced_manager/constants.dart';
|
2022-08-25 01:51:47 +02:00
|
|
|
import 'package:revanced_manager/models/patched_application.dart';
|
2022-08-09 01:01:06 +02:00
|
|
|
import 'package:revanced_manager/services/github_api.dart';
|
2022-08-25 01:51:47 +02:00
|
|
|
import 'package:revanced_manager/services/root_api.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-07-31 21:46:27 +02:00
|
|
|
|
2022-08-25 01:51:47 +02:00
|
|
|
@lazySingleton
|
2022-08-02 10:06:35 +02:00
|
|
|
class ManagerAPI {
|
2022-08-18 16:33:33 +02:00
|
|
|
final GithubAPI _githubAPI = GithubAPI();
|
2022-08-25 01:51:47 +02:00
|
|
|
final RootAPI _rootAPI = RootAPI();
|
|
|
|
late SharedPreferences _prefs;
|
2022-08-29 16:01:51 +02:00
|
|
|
late List<RepositoryCommit> _commits = [];
|
2022-08-25 01:51:47 +02:00
|
|
|
|
|
|
|
Future<void> initialize() async {
|
|
|
|
_prefs = await SharedPreferences.getInstance();
|
2022-08-29 16:01:51 +02:00
|
|
|
_commits = (await _githubAPI.getCommits(ghOrg, patchesRepo)).toList();
|
2022-08-25 01:51:47 +02:00
|
|
|
}
|
2022-08-02 10:06:35 +02:00
|
|
|
|
2022-08-19 20:13:43 +02:00
|
|
|
Future<File?> downloadPatches(String extension) async {
|
|
|
|
return await _githubAPI.latestReleaseFile(extension, ghOrg, patchesRepo);
|
2022-08-02 10:06:35 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 20:13:43 +02:00
|
|
|
Future<File?> downloadIntegrations(String extension) async {
|
2022-08-18 18:32:58 +02:00
|
|
|
return await _githubAPI.latestReleaseFile(
|
2022-08-19 20:13:43 +02:00
|
|
|
extension,
|
|
|
|
ghOrg,
|
|
|
|
integrationsRepo,
|
2022-08-18 18:32:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-19 20:13:43 +02:00
|
|
|
Future<File?> downloadManager(String extension) async {
|
|
|
|
return await _githubAPI.latestReleaseFile(extension, ghOrg, managerRepo);
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:32:58 +02:00
|
|
|
Future<String?> getLatestPatchesVersion() async {
|
|
|
|
return await _githubAPI.latestReleaseVersion(ghOrg, patchesRepo);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> getLatestManagerVersion() async {
|
2022-08-19 20:13:43 +02:00
|
|
|
return await _githubAPI.latestReleaseVersion(ghOrg, managerRepo);
|
2022-08-18 18:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getCurrentManagerVersion() async {
|
|
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
|
|
return packageInfo.version;
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:51:47 +02:00
|
|
|
bool? isRooted() {
|
|
|
|
return _prefs.getBool('isRooted');
|
|
|
|
}
|
|
|
|
|
|
|
|
List<PatchedApplication> getPatchedApps() {
|
|
|
|
List<String> apps = _prefs.getStringList('patchedApps') ?? [];
|
|
|
|
return apps
|
|
|
|
.map((a) => PatchedApplication.fromJson(json.decode(a)))
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPatchedApps(List<PatchedApplication> patchedApps) {
|
|
|
|
_prefs.setStringList('patchedApps',
|
|
|
|
patchedApps.map((a) => json.encode(a.toJson())).toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
void savePatchedApp(PatchedApplication app) {
|
|
|
|
List<PatchedApplication> patchedApps = getPatchedApps();
|
|
|
|
patchedApps.removeWhere((a) => a.packageName == app.packageName);
|
|
|
|
patchedApps.add(app);
|
|
|
|
setPatchedApps(patchedApps);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> reAssessSavedApps() async {
|
|
|
|
bool isRoot = isRooted() ?? false;
|
2022-08-29 16:01:51 +02:00
|
|
|
List<PatchedApplication> patchedApps = getPatchedApps();
|
2022-08-26 03:01:53 +02:00
|
|
|
List<PatchedApplication> toRemove = [];
|
2022-08-25 01:51:47 +02:00
|
|
|
for (PatchedApplication app in patchedApps) {
|
2022-08-29 16:01:51 +02:00
|
|
|
bool isRemove = await isAppUninstalled(app, isRoot);
|
|
|
|
if (isRemove) {
|
2022-08-26 03:01:53 +02:00
|
|
|
toRemove.add(app);
|
2022-08-29 16:01:51 +02:00
|
|
|
} else {
|
|
|
|
List<String> newChangelog = getAppChangelog(
|
|
|
|
app.packageName,
|
|
|
|
app.patchDate,
|
|
|
|
);
|
|
|
|
if (newChangelog.isNotEmpty) {
|
|
|
|
app.changelog = newChangelog;
|
|
|
|
app.hasUpdates = true;
|
|
|
|
} else {
|
|
|
|
app.hasUpdates = false;
|
2022-08-25 01:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 03:01:53 +02:00
|
|
|
patchedApps.removeWhere((a) => toRemove.contains(a));
|
2022-08-25 01:51:47 +02:00
|
|
|
setPatchedApps(patchedApps);
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:01:51 +02:00
|
|
|
Future<bool> isAppUninstalled(PatchedApplication app, bool isRoot) async {
|
|
|
|
bool existsRoot = false;
|
|
|
|
if (isRoot) {
|
|
|
|
existsRoot = await _rootAPI.isAppInstalled(app.packageName);
|
|
|
|
}
|
|
|
|
bool existsNonRoot = await DeviceApps.isAppInstalled(app.packageName);
|
|
|
|
return !existsRoot && !existsNonRoot;
|
2022-08-18 18:32:58 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 16:01:51 +02:00
|
|
|
List<String> getAppChangelog(String packageName, DateTime patchedDate) {
|
|
|
|
List<String> newCommits = _commits
|
2022-08-26 03:01:53 +02:00
|
|
|
.where((c) =>
|
|
|
|
c.commit != null &&
|
|
|
|
c.commit!.message != null &&
|
|
|
|
c.commit!.author != null &&
|
2022-08-29 16:01:51 +02:00
|
|
|
c.commit!.author!.date != null &&
|
|
|
|
c.commit!.author!.date!.isAfter(patchedDate))
|
|
|
|
.map((c) => c.commit!.message!)
|
|
|
|
.toList();
|
|
|
|
if (newCommits.isNotEmpty) {
|
|
|
|
int firstChore = newCommits.indexWhere((c) => c.startsWith('chore'));
|
|
|
|
int secondChore =
|
|
|
|
newCommits.indexWhere((c) => c.startsWith('chore'), firstChore + 1);
|
|
|
|
if (firstChore >= 0 && secondChore > firstChore) {
|
|
|
|
return newCommits.sublist(firstChore + 1, secondChore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return List.empty();
|
2022-08-02 09:55:01 +02:00
|
|
|
}
|
2022-08-01 21:05:11 +02:00
|
|
|
}
|