From 36c86e22b1305043d6d19753cbbe1a3dc4df2eca Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 30 Sep 2023 19:59:31 +0200 Subject: [PATCH] fix: Load installed apps --- .../revanced/manager/flutter/MainActivity.kt | 1 - lib/services/manager_api.dart | 71 +++++++++---------- lib/ui/views/home/home_viewmodel.dart | 3 +- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt b/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt index bd33a8b2..691267b3 100644 --- a/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt +++ b/android/app/src/main/kotlin/app/revanced/manager/flutter/MainActivity.kt @@ -94,7 +94,6 @@ class MainActivity : FlutterActivity() { val patchBundleFilePath = call.argument("patchBundleFilePath")!! val cacheDirPath = call.argument("cacheDirPath")!! - JSONArray().apply { try { PatchBundleLoader.Dex( diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index 5882f810..275efc93 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -42,12 +42,14 @@ class ManagerAPI { String defaultManagerRepo = 'revanced/revanced-manager'; String? patchesVersion = ''; String? integrationsVersion = ''; + bool isDefaultPatchesRepo() { return getPatchesRepo().toLowerCase() == 'revanced/revanced-patches'; } bool isDefaultIntegrationsRepo() { - return getIntegrationsRepo().toLowerCase() == 'revanced/revanced-integrations'; + return getIntegrationsRepo().toLowerCase() == + 'revanced/revanced-integrations'; } Future initialize() async { @@ -309,7 +311,7 @@ class ManagerAPI { final Directory appCache = await getTemporaryDirectory(); Directory('${appCache.path}/cache').createSync(); final Directory workDir = - Directory('${appCache.path}/cache').createTempSync('tmp-'); + Directory('${appCache.path}/cache').createTempSync('tmp-'); final Directory cacheDir = Directory('${workDir.path}/cache'); cacheDir.createSync(); @@ -324,7 +326,9 @@ class ManagerAPI { ); final List patchesJsonList = jsonDecode(patchesJson); - patches = patchesJsonList.map((patchJson) => Patch.fromJson(patchJson)).toList(); + patches = patchesJsonList + .map((patchJson) => Patch.fromJson(patchJson)) + .toList(); return patches; } on Exception catch (e) { if (kDebugMode) { @@ -501,48 +505,18 @@ class ManagerAPI { return toRemove; } - Future> getUnsavedApps( - List patchedApps, - ) async { - final List unsavedApps = []; + Future> getMountedApps() async { + final List mountedApps = []; final bool hasRootPermissions = await _rootAPI.hasRootPermissions(); if (hasRootPermissions) { 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( - packageName, - true, - ) as ApplicationWithIcon?; - if (application != null) { - unsavedApps.add( - PatchedApplication( - name: application.appName, - packageName: application.packageName, - originalPackageName: application.packageName, - version: application.versionName!, - apkFilePath: application.apkFilePath, - icon: application.icon, - patchDate: DateTime.now(), - isRooted: true, - ), - ); - } - } - } - } - final List userApps = - await DeviceApps.getInstalledApplications(); - 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( - app.packageName, + packageName, true, ) as ApplicationWithIcon?; if (application != null) { - unsavedApps.add( + mountedApps.add( PatchedApplication( name: application.appName, packageName: application.packageName, @@ -551,12 +525,14 @@ class ManagerAPI { apkFilePath: application.apkFilePath, icon: application.icon, patchDate: DateTime.now(), + isRooted: true, ), ); } } } - return unsavedApps; + + return mountedApps; } Future showPatchesChangeWarningDialog(BuildContext context) { @@ -616,6 +592,25 @@ class ManagerAPI { ); } + Future reAssessSavedApps() async { + final List patchedApps = getPatchedApps(); + + // Remove apps that are not installed anymore. + final List toRemove = + await getAppsToRemove(patchedApps); + patchedApps.removeWhere((a) => toRemove.contains(a)); + + // Determine all apps that are installed by mounting. + final List mountedApps = await getMountedApps(); + mountedApps.removeWhere( + (app) => patchedApps + .any((patchedApp) => patchedApp.packageName == app.packageName), + ); + patchedApps.addAll(mountedApps); + + await setPatchedApps(patchedApps); + } + Future isAppUninstalled(PatchedApplication app) async { bool existsRoot = false; final bool existsNonRoot = await DeviceApps.isAppInstalled(app.packageName); diff --git a/lib/ui/views/home/home_viewmodel.dart b/lib/ui/views/home/home_viewmodel.dart index 69ce644d..40cf85b4 100644 --- a/lib/ui/views/home/home_viewmodel.dart +++ b/lib/ui/views/home/home_viewmodel.dart @@ -81,7 +81,8 @@ class HomeViewModel extends BaseViewModel { _toast.showBottom('homeView.errorDownloadMessage'); } } - _getPatchedApps(); + + _managerAPI.reAssessSavedApps().then((_) => _getPatchedApps()); } void navigateToAppInfo(PatchedApplication app) {