Skip to content

Commit

Permalink
fix: Load installed apps
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Sep 30, 2023
1 parent 6bdc0c7 commit 36c86e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class MainActivity : FlutterActivity() {
val patchBundleFilePath = call.argument<String>("patchBundleFilePath")!!
val cacheDirPath = call.argument<String>("cacheDirPath")!!


JSONArray().apply {
try {
PatchBundleLoader.Dex(
Expand Down
71 changes: 33 additions & 38 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> initialize() async {
Expand Down Expand Up @@ -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();

Expand All @@ -324,7 +326,9 @@ class ManagerAPI {
);

final List<dynamic> 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) {
Expand Down Expand Up @@ -501,48 +505,18 @@ class ManagerAPI {
return toRemove;
}

Future<List<PatchedApplication>> getUnsavedApps(
List<PatchedApplication> patchedApps,
) async {
final List<PatchedApplication> unsavedApps = [];
Future<List<PatchedApplication>> getMountedApps() async {
final List<PatchedApplication> mountedApps = [];
final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (hasRootPermissions) {
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(
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<Application> 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,
Expand All @@ -551,12 +525,14 @@ class ManagerAPI {
apkFilePath: application.apkFilePath,
icon: application.icon,
patchDate: DateTime.now(),
isRooted: true,
),
);
}
}
}
return unsavedApps;

return mountedApps;
}

Future<void> showPatchesChangeWarningDialog(BuildContext context) {
Expand Down Expand Up @@ -616,6 +592,25 @@ class ManagerAPI {
);
}

Future<void> reAssessSavedApps() async {
final List<PatchedApplication> patchedApps = getPatchedApps();

// Remove apps that are not installed anymore.
final List<PatchedApplication> toRemove =
await getAppsToRemove(patchedApps);
patchedApps.removeWhere((a) => toRemove.contains(a));

// Determine all apps that are installed by mounting.
final List<PatchedApplication> mountedApps = await getMountedApps();
mountedApps.removeWhere(
(app) => patchedApps
.any((patchedApp) => patchedApp.packageName == app.packageName),
);
patchedApps.addAll(mountedApps);

await setPatchedApps(patchedApps);
}

Future<bool> isAppUninstalled(PatchedApplication app) async {
bool existsRoot = false;
final bool existsNonRoot = await DeviceApps.isAppInstalled(app.packageName);
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/views/home/home_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class HomeViewModel extends BaseViewModel {
_toast.showBottom('homeView.errorDownloadMessage');
}
}
_getPatchedApps();

_managerAPI.reAssessSavedApps().then((_) => _getPatchedApps());
}

void navigateToAppInfo(PatchedApplication app) {
Expand Down

0 comments on commit 36c86e2

Please sign in to comment.