Bugfix for previous commit

This commit is contained in:
Imran Remtulla
2023-03-17 22:49:12 -04:00
parent c0120f4e40
commit 0f3e029312

View File

@@ -149,56 +149,63 @@ class AppsProvider with ChangeNotifier {
apps[app.id]!.downloadProgress = 0; apps[app.id]!.downloadProgress = 0;
notifyListeners(); notifyListeners();
} }
var fileName = try {
'${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk'; var fileName =
String downloadUrl = await SourceProvider() '${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk';
.getSource(app.url) String downloadUrl = await SourceProvider()
.apkUrlPrefetchModifier(app.apkUrls[app.preferredApkIndex]); .getSource(app.url)
NotificationsProvider? notificationsProvider = .apkUrlPrefetchModifier(app.apkUrls[app.preferredApkIndex]);
context?.read<NotificationsProvider>(); NotificationsProvider? notificationsProvider =
var notif = DownloadNotification(app.name, 100); context?.read<NotificationsProvider>();
notificationsProvider?.cancel(notif.id); var notif = DownloadNotification(app.name, 100);
int? prevProg; notificationsProvider?.cancel(notif.id);
File downloadedFile = int? prevProg;
await downloadFile(downloadUrl, fileName, (double? progress) { File downloadedFile =
int? prog = progress?.ceil(); await downloadFile(downloadUrl, fileName, (double? progress) {
int? prog = progress?.ceil();
if (apps[app.id] != null) {
apps[app.id]!.downloadProgress = progress;
notifyListeners();
}
notif = DownloadNotification(app.name, prog ?? 100);
if (prog != null && prevProg != prog) {
notificationsProvider?.notify(notif);
}
prevProg = prog;
});
notificationsProvider?.cancel(notif.id);
// Delete older versions of the APK if any
for (var file in downloadedFile.parent.listSync()) {
var fn = file.path.split('/').last;
if (fn.startsWith('${app.id}-') &&
fn.endsWith('.apk') &&
fn != fileName) {
file.delete();
}
}
// If the APK package ID is different from the App ID, it is either new (using a placeholder ID) or the ID has changed
// The former case should be handled (give the App its real ID), the latter is a security issue
var newInfo = await PackageArchiveInfo.fromPath(downloadedFile.path);
if (app.id != newInfo.packageName) {
if (apps[app.id] != null && !SourceProvider().isTempId(app)) {
throw IDChangedError();
}
var originalAppId = app.id;
app.id = newInfo.packageName;
downloadedFile = downloadedFile.renameSync(
'${downloadedFile.parent.path}/${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk');
if (apps[originalAppId] != null) {
await removeApps([originalAppId]);
await saveApps([app]);
}
}
return DownloadedApk(app.id, downloadedFile);
} finally {
if (apps[app.id] != null) { if (apps[app.id] != null) {
apps[app.id]!.downloadProgress = progress; apps[app.id]!.downloadProgress = null;
notifyListeners(); notifyListeners();
} }
notif = DownloadNotification(app.name, prog ?? 100);
if (prog != null && prevProg != prog) {
notificationsProvider?.notify(notif);
}
prevProg = prog;
});
notificationsProvider?.cancel(notif.id);
// Delete older versions of the APK if any
for (var file in downloadedFile.parent.listSync()) {
var fn = file.path.split('/').last;
if (fn.startsWith('${app.id}-') &&
fn.endsWith('.apk') &&
fn != fileName) {
file.delete();
}
} }
// If the APK package ID is different from the App ID, it is either new (using a placeholder ID) or the ID has changed
// The former case should be handled (give the App its real ID), the latter is a security issue
var newInfo = await PackageArchiveInfo.fromPath(downloadedFile.path);
if (app.id != newInfo.packageName) {
if (apps[app.id] != null && !SourceProvider().isTempId(app)) {
throw IDChangedError();
}
var originalAppId = app.id;
app.id = newInfo.packageName;
downloadedFile = downloadedFile.renameSync(
'${downloadedFile.parent.path}/${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk');
if (apps[originalAppId] != null) {
await removeApps([originalAppId]);
await saveApps([app]);
}
}
return DownloadedApk(app.id, downloadedFile);
} }
bool areDownloadsRunning() => apps.values bool areDownloadsRunning() => apps.values