From 423ba07fad561e4cbe53b7af5f716735e4efe169 Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Fri, 23 Jun 2023 11:52:49 -0400 Subject: [PATCH] Allow correcting inferred IDs (#103) --- lib/custom_errors.dart | 2 +- lib/pages/app.dart | 9 ++++++--- lib/providers/apps_provider.dart | 8 +++++--- lib/providers/source_provider.dart | 19 ++++++++++++++----- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/custom_errors.dart b/lib/custom_errors.dart index 8f535cf..5d0bb20 100644 --- a/lib/custom_errors.dart +++ b/lib/custom_errors.dart @@ -56,7 +56,7 @@ class InstallError extends ObtainiumError { } class IDChangedError extends ObtainiumError { - IDChangedError() : super(tr('appIdMismatch')); + IDChangedError(String newId) : super('${tr('appIdMismatch')} - $newId'); } class NotImplementedError extends ObtainiumError { diff --git a/lib/pages/app.dart b/lib/pages/app.dart index 3e2e993..096787c 100644 --- a/lib/pages/app.dart +++ b/lib/pages/app.dart @@ -329,7 +329,8 @@ class _AppPageState extends State { try { HapticFeedback.heavyImpact(); var res = await appsProvider.downloadAndInstallLatestApps( - [app!.app.id], globalNavigatorKey.currentContext); + app?.app.id != null ? [app!.app.id] : [], + globalNavigatorKey.currentContext); if (res.isNotEmpty && mounted) { Navigator.of(context).pop(); } @@ -426,8 +427,10 @@ class _AppPageState extends State { onPressed: app?.downloadProgress != null ? null : () { - appsProvider.removeAppsWithModal( - context, [app!.app]).then((value) { + appsProvider + .removeAppsWithModal( + context, app != null ? [app.app] : []) + .then((value) { if (value == true) { Navigator.of(context).pop(); } diff --git a/lib/providers/apps_provider.dart b/lib/providers/apps_provider.dart index 47dfbde..ba8b8c4 100644 --- a/lib/providers/apps_provider.dart +++ b/lib/providers/apps_provider.dart @@ -202,16 +202,18 @@ class AppsProvider with ChangeNotifier { // The former case should be handled (give the App its real ID), the latter is a security issue if (app.id != newInfo.packageName) { var isTempId = SourceProvider().isTempId(app); - if (apps[app.id] != null && !isTempId) { - throw IDChangedError(); + if (apps[app.id] != null && !isTempId && !app.allowIdChange) { + throw IDChangedError(newInfo.packageName); } + var idChangeWasAllowed = app.allowIdChange; + app.allowIdChange = false; var originalAppId = app.id; app.id = newInfo.packageName; downloadedFile = downloadedFile.renameSync( '${downloadedFile.parent.path}/${app.id}-${downloadUrl.hashCode}.${downloadedFile.path.split('.').last}'); if (apps[originalAppId] != null) { await removeApps([originalAppId]); - await saveApps([app], onlyIfExists: !isTempId); + await saveApps([app], onlyIfExists: !isTempId && !idChangeWasAllowed); } } return downloadedFile; diff --git a/lib/providers/source_provider.dart b/lib/providers/source_provider.dart index 41caa43..f8f31ff 100644 --- a/lib/providers/source_provider.dart +++ b/lib/providers/source_provider.dart @@ -163,6 +163,7 @@ class App { late DateTime? releaseDate; late String? changeLog; late String? overrideSource; + bool allowIdChange = false; App( this.id, this.url, @@ -178,7 +179,8 @@ class App { {this.categories = const [], this.releaseDate, this.changeLog, - this.overrideSource}); + this.overrideSource, + this.allowIdChange = false}); @override String toString() { @@ -209,7 +211,8 @@ class App { categories: categories, changeLog: changeLog, releaseDate: releaseDate, - overrideSource: overrideSource); + overrideSource: overrideSource, + allowIdChange: allowIdChange); factory App.fromJson(Map json) { json = appJSONCompatibilityModifiers(json); @@ -241,7 +244,8 @@ class App { : DateTime.fromMicrosecondsSinceEpoch(json['releaseDate']), changeLog: json['changeLog'] == null ? null : json['changeLog'] as String, - overrideSource: json['overrideSource']); + overrideSource: json['overrideSource'], + allowIdChange: json['allowIdChange'] ?? false); } Map toJson() => { @@ -259,7 +263,8 @@ class App { 'categories': categories, 'releaseDate': releaseDate?.microsecondsSinceEpoch, 'changeLog': changeLog, - 'overrideSource': overrideSource + 'overrideSource': overrideSource, + 'allowIdChange': allowIdChange }; } @@ -613,7 +618,11 @@ class SourceProvider { categories: currentApp?.categories ?? const [], releaseDate: apk.releaseDate, changeLog: apk.changeLog, - overrideSource: overrideSource ?? currentApp?.overrideSource); + overrideSource: overrideSource ?? currentApp?.overrideSource, + allowIdChange: currentApp?.allowIdChange ?? + source.appIdInferIsOptional && + inferAppIdIfOptional // Optional ID inferring may be incorrect - allow correction on first install + ); } // Returns errors in [results, errors] instead of throwing them