diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index 448fa7ec..85900b55 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -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 @@ -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'; @@ -39,8 +41,7 @@ class ManagerAPI { Future initialize() async { _prefs = await SharedPreferences.getInstance(); storedPatchesFile = - (await getApplicationDocumentsDirectory()).path + - storedPatchesFile; + (await getApplicationDocumentsDirectory()).path + storedPatchesFile; } String getApiUrl() { @@ -79,8 +80,7 @@ class ManagerAPI { } String getIntegrationsRepo() { - return _prefs.getString('integrationsRepo') ?? - defaultIntegrationsRepo; + return _prefs.getString('integrationsRepo') ?? defaultIntegrationsRepo; } Future setIntegrationsRepo(String value) async { @@ -148,9 +148,7 @@ class ManagerAPI { List getPatchedApps() { final List apps = _prefs.getStringList('patchedApps') ?? []; - return apps - .map((a) => PatchedApplication.fromJson(jsonDecode(a))) - .toList(); + return apps.map((a) => PatchedApplication.fromJson(jsonDecode(a))).toList(); } Future setPatchedApps( @@ -328,12 +326,10 @@ class ManagerAPI { final List unsavedApps = []; final bool hasRootPermissions = await _rootAPI.hasRootPermissions(); if (hasRootPermissions) { - final List installedApps = - await _rootAPI.getInstalledApps(); + final List 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?; @@ -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?; @@ -419,11 +413,9 @@ class ManagerAPI { Future 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); } @@ -478,8 +470,7 @@ class ManagerAPI { List patches, ) async { final File selectedPatchesFile = File(storedPatchesFile); - final Map patchesMap = - await readSelectedPatchesFile(); + final Map patchesMap = await readSelectedPatchesFile(); if (patches.isEmpty) { patchesMap.remove(app); } else { @@ -488,10 +479,33 @@ class ManagerAPI { selectedPatchesFile.writeAsString(jsonEncode(patchesMap)); } + // get default patches for app + Future> getDefaultPatches() async { + final List patches = await getPatches(); + final List 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> getSelectedPatches(String app) async { - final Map patchesMap = - await readSelectedPatchesFile(); - return List.from(patchesMap.putIfAbsent(app, () => List.empty())); + final Map patchesMap = await readSelectedPatchesFile(); + final List defaultPatches = await getDefaultPatches(); + return List.from(patchesMap.putIfAbsent(app, () => defaultPatches)); } Future> readSelectedPatchesFile() async { diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index b7c1f9ff..a4d80280 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -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, @@ -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, @@ -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, @@ -173,15 +170,13 @@ class PatcherAPI { String originalFilePath, List 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); @@ -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(); @@ -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(), @@ -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, @@ -321,8 +313,7 @@ class PatcherAPI { String getRecommendedVersion(String packageName) { final Map 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) { @@ -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 ''; diff --git a/lib/ui/views/patches_selector/patches_selector_view.dart b/lib/ui/views/patches_selector/patches_selector_view.dart index a303e45f..d616f9fe 100644 --- a/lib/ui/views/patches_selector/patches_selector_view.dart +++ b/lib/ui/views/patches_selector/patches_selector_view.dart @@ -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 { @@ -168,7 +169,7 @@ class _PatchesSelectorViewState extends State { packageVersion: model.getAppVersion(), supportedPackageVersions: model.getSupportedVersions(patch), - isUnsupported: !model.isPatchSupported(patch), + isUnsupported: !isPatchSupported(patch), isSelected: model.isSelected(patch), onChanged: (value) => model.selectPatch(patch, value), diff --git a/lib/ui/views/patches_selector/patches_selector_viewmodel.dart b/lib/ui/views/patches_selector/patches_selector_viewmodel.dart index 75d24c5c..61cd9b81 100644 --- a/lib/ui/views/patches_selector/patches_selector_viewmodel.dart +++ b/lib/ui/views/patches_selector/patches_selector_viewmodel.dart @@ -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 { @@ -118,16 +119,6 @@ class PatchesSelectorViewModel extends BaseViewModel { } } - bool isPatchSupported(Patch patch) { - final PatchedApplication app = locator().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: diff --git a/lib/utils/check_for_supported_patch.dart b/lib/utils/check_for_supported_patch.dart new file mode 100644 index 00000000..11c665d0 --- /dev/null +++ b/lib/utils/check_for_supported_patch.dart @@ -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().selectedApp!; + return patch.compatiblePackages.isEmpty || + patch.compatiblePackages.any( + (pack) => + pack.name == app.packageName && + (pack.versions.isEmpty || pack.versions.contains(app.version)), + ); +}