Skip to content

Commit

Permalink
feat: auto select default patches
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunali321 committed Apr 18, 2023
1 parent f0b0282 commit 4c9cb56
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
64 changes: 39 additions & 25 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:revanced_manager/models/patched_application.dart';
import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/services/revanced_api.dart';
import 'package:revanced_manager/services/root_api.dart';
import 'package:revanced_manager/utils/check_for_supported_patch.dart';
import 'package:shared_preferences/shared_preferences.dart';

@lazySingleton
Expand All @@ -22,7 +23,8 @@ class ManagerAPI {
final String cliRepo = 'revanced-cli';
late SharedPreferences _prefs;
String storedPatchesFile = '/selected-patches.json';
String keystoreFile = '/sdcard/Android/data/app.revanced.manager.flutter/files/revanced-manager.keystore';
String keystoreFile =
'/sdcard/Android/data/app.revanced.manager.flutter/files/revanced-manager.keystore';
String defaultKeystorePassword = 's3cur3p@ssw0rd';
String defaultApiUrl = 'https://releases.revanced.app/';
String defaultRepoUrl = 'https://api.github.com';
Expand All @@ -39,8 +41,7 @@ class ManagerAPI {
Future<void> initialize() async {
_prefs = await SharedPreferences.getInstance();
storedPatchesFile =
(await getApplicationDocumentsDirectory()).path +
storedPatchesFile;
(await getApplicationDocumentsDirectory()).path + storedPatchesFile;
}

String getApiUrl() {
Expand Down Expand Up @@ -79,8 +80,7 @@ class ManagerAPI {
}

String getIntegrationsRepo() {
return _prefs.getString('integrationsRepo') ??
defaultIntegrationsRepo;
return _prefs.getString('integrationsRepo') ?? defaultIntegrationsRepo;
}

Future<void> setIntegrationsRepo(String value) async {
Expand Down Expand Up @@ -148,9 +148,7 @@ class ManagerAPI {

List<PatchedApplication> getPatchedApps() {
final List<String> apps = _prefs.getStringList('patchedApps') ?? [];
return apps
.map((a) => PatchedApplication.fromJson(jsonDecode(a)))
.toList();
return apps.map((a) => PatchedApplication.fromJson(jsonDecode(a))).toList();
}

Future<void> setPatchedApps(
Expand Down Expand Up @@ -328,12 +326,10 @@ class ManagerAPI {
final List<PatchedApplication> unsavedApps = [];
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (hasRootPermissions) {
final List<String> installedApps =
await _rootAPI.getInstalledApps();
final List<String> installedApps = await _rootAPI.getInstalledApps();
for (final String packageName in installedApps) {
if (!patchedApps.any((app) => app.packageName == packageName)) {
final ApplicationWithIcon? application =
await DeviceApps.getApp(
final ApplicationWithIcon? application = await DeviceApps.getApp(
packageName,
true,
) as ApplicationWithIcon?;
Expand All @@ -359,10 +355,8 @@ class ManagerAPI {
for (final Application app in userApps) {
if (app.packageName.startsWith('app.revanced') &&
!app.packageName.startsWith('app.revanced.manager.') &&
!patchedApps
.any((uapp) => uapp.packageName == app.packageName)) {
final ApplicationWithIcon? application =
await DeviceApps.getApp(
!patchedApps.any((uapp) => uapp.packageName == app.packageName)) {
final ApplicationWithIcon? application = await DeviceApps.getApp(
app.packageName,
true,
) as ApplicationWithIcon?;
Expand Down Expand Up @@ -419,11 +413,9 @@ class ManagerAPI {

Future<bool> isAppUninstalled(PatchedApplication app) async {
bool existsRoot = false;
final bool existsNonRoot =
await DeviceApps.isAppInstalled(app.packageName);
final bool existsNonRoot = await DeviceApps.isAppInstalled(app.packageName);
if (app.isRooted) {
final bool hasRootPermissions =
await _rootAPI.hasRootPermissions();
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (hasRootPermissions) {
existsRoot = await _rootAPI.isAppInstalled(app.packageName);
}
Expand Down Expand Up @@ -478,8 +470,7 @@ class ManagerAPI {
List<String> patches,
) async {
final File selectedPatchesFile = File(storedPatchesFile);
final Map<String, dynamic> patchesMap =
await readSelectedPatchesFile();
final Map<String, dynamic> patchesMap = await readSelectedPatchesFile();
if (patches.isEmpty) {
patchesMap.remove(app);
} else {
Expand All @@ -488,10 +479,33 @@ class ManagerAPI {
selectedPatchesFile.writeAsString(jsonEncode(patchesMap));
}

// get default patches for app
Future<List<String>> getDefaultPatches() async {
final List<Patch> patches = await getPatches();
final List<String> defaultPatches = [];
if (areExperimentalPatchesEnabled() == false) {
defaultPatches.addAll(
patches
.where(
(element) =>
element.excluded == false && isPatchSupported(element),
)
.map((p) => p.name),
);
} else {
defaultPatches.addAll(
patches
.where((element) => isPatchSupported(element))
.map((p) => p.name),
);
}
return defaultPatches;
}

Future<List<String>> getSelectedPatches(String app) async {
final Map<String, dynamic> patchesMap =
await readSelectedPatchesFile();
return List.from(patchesMap.putIfAbsent(app, () => List.empty()));
final Map<String, dynamic> patchesMap = await readSelectedPatchesFile();
final List<String> defaultPatches = await getDefaultPatches();
return List.from(patchesMap.putIfAbsent(app, () => defaultPatches));
}

Future<Map<String, dynamic>> readSelectedPatchesFile() async {
Expand Down
30 changes: 10 additions & 20 deletions lib/services/patcher_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ class PatcherAPI {
onlyAppsWithLaunchIntent: true,
);
for (final pkg in allPackages) {
if (!filteredApps
.any((app) => app.packageName == pkg.packageName)) {
if (!filteredApps.any((app) => app.packageName == pkg.packageName)) {
final appInfo = await DeviceApps.getApp(
pkg.packageName,
true,
Expand All @@ -84,8 +83,7 @@ class PatcherAPI {
for (final Patch patch in _patches) {
for (final Package package in patch.compatiblePackages) {
try {
if (!filteredApps
.any((app) => app.packageName == package.name)) {
if (!filteredApps.any((app) => app.packageName == package.name)) {
final ApplicationWithIcon? app = await DeviceApps.getApp(
package.name,
true,
Expand Down Expand Up @@ -151,8 +149,7 @@ class PatcherAPI {
String originalFilePath,
) async {
try {
final bool hasRootPermissions =
await _rootAPI.hasRootPermissions();
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (hasRootPermissions) {
originalFilePath = await _rootAPI.getOriginalFilePath(
packageName,
Expand All @@ -173,15 +170,13 @@ class PatcherAPI {
String originalFilePath,
List<Patch> selectedPatches,
) async {
final bool includeSettings =
await needsSettingsPatch(selectedPatches);
final bool includeSettings = await needsSettingsPatch(selectedPatches);
if (includeSettings) {
try {
final Patch? settingsPatch = _patches.firstWhereOrNull(
(patch) =>
patch.name.contains('settings') &&
patch.compatiblePackages
.any((pack) => pack.name == packageName),
patch.compatiblePackages.any((pack) => pack.name == packageName),
);
if (settingsPatch != null) {
selectedPatches.add(settingsPatch);
Expand All @@ -193,8 +188,7 @@ class PatcherAPI {
}
}
final File? patchBundleFile = await _managerAPI.downloadPatches();
final File? integrationsFile =
await _managerAPI.downloadIntegrations();
final File? integrationsFile = await _managerAPI.downloadIntegrations();
if (patchBundleFile != null) {
_dataDir.createSync();
_tmpDir.createSync();
Expand All @@ -217,8 +211,7 @@ class PatcherAPI {
'patchedFilePath': patchedFile.path,
'outFilePath': _outFile!.path,
'integrationsPath': integrationsFile!.path,
'selectedPatches':
selectedPatches.map((p) => p.name).toList(),
'selectedPatches': selectedPatches.map((p) => p.name).toList(),
'cacheDirPath': cacheDir.path,
'keyStoreFilePath': _keyStoreFile.path,
'keystorePassword': _managerAPI.getKeystorePassword(),
Expand All @@ -236,8 +229,7 @@ class PatcherAPI {
if (_outFile != null) {
try {
if (patchedApp.isRooted) {
final bool hasRootPermissions =
await _rootAPI.hasRootPermissions();
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (hasRootPermissions) {
return _rootAPI.installApp(
patchedApp.packageName,
Expand Down Expand Up @@ -321,8 +313,7 @@ class PatcherAPI {
String getRecommendedVersion(String packageName) {
final Map<String, int> versions = {};
for (final Patch patch in _patches) {
final Package? package =
patch.compatiblePackages.firstWhereOrNull(
final Package? package = patch.compatiblePackages.firstWhereOrNull(
(pack) => pack.name == packageName,
);
if (package != null) {
Expand All @@ -341,8 +332,7 @@ class PatcherAPI {
versions
..clear()
..addEntries(entries);
versions
.removeWhere((key, value) => value != versions.values.last);
versions.removeWhere((key, value) => value != versions.values.last);
return (versions.keys.toList()..sort()).last;
}
return '';
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/views/patches_selector/patches_selector_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:revanced_manager/ui/views/patches_selector/patches_selector_view
import 'package:revanced_manager/ui/widgets/patchesSelectorView/patch_item.dart';
import 'package:revanced_manager/ui/widgets/shared/custom_popup_menu.dart';
import 'package:revanced_manager/ui/widgets/shared/search_bar.dart';
import 'package:revanced_manager/utils/check_for_supported_patch.dart';
import 'package:stacked/stacked.dart';

class PatchesSelectorView extends StatefulWidget {
Expand Down Expand Up @@ -168,7 +169,7 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
packageVersion: model.getAppVersion(),
supportedPackageVersions:
model.getSupportedVersions(patch),
isUnsupported: !model.isPatchSupported(patch),
isUnsupported: !isPatchSupported(patch),
isSelected: model.isSelected(patch),
onChanged: (value) =>
model.selectPatch(patch, value),
Expand Down
11 changes: 1 addition & 10 deletions lib/ui/views/patches_selector/patches_selector_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/services/toast.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:revanced_manager/utils/check_for_supported_patch.dart';
import 'package:stacked/stacked.dart';

class PatchesSelectorViewModel extends BaseViewModel {
Expand Down Expand Up @@ -118,16 +119,6 @@ class PatchesSelectorViewModel extends BaseViewModel {
}
}

bool isPatchSupported(Patch patch) {
final PatchedApplication app = locator<PatcherViewModel>().selectedApp!;
return patch.compatiblePackages.isEmpty ||
patch.compatiblePackages.any(
(pack) =>
pack.name == app.packageName &&
(pack.versions.isEmpty || pack.versions.contains(app.version)),
);
}

void onMenuSelection(value) {
switch (value) {
case 0:
Expand Down
14 changes: 14 additions & 0 deletions lib/utils/check_for_supported_patch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/models/patched_application.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';

bool isPatchSupported(Patch patch) {
final PatchedApplication app = locator<PatcherViewModel>().selectedApp!;
return patch.compatiblePackages.isEmpty ||
patch.compatiblePackages.any(
(pack) =>
pack.name == app.packageName &&
(pack.versions.isEmpty || pack.versions.contains(app.version)),
);
}

0 comments on commit 4c9cb56

Please sign in to comment.