diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java index 0465ed02..3a695fef 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java @@ -31,9 +31,10 @@ public class DownloaderManager { private final Context context; private final DataSource.Factory dataSourceFactory; - private final HashMap downloads; private final DownloadIndex downloadIndex; + private static HashMap downloads; + public DownloaderManager(Context context, DataSource.Factory dataSourceFactory, DownloadManager downloadManager) { this.context = context.getApplicationContext(); this.dataSourceFactory = dataSourceFactory; @@ -83,8 +84,8 @@ public class DownloaderManager { public void remove(MediaItem mediaItem, com.cappielloantonio.tempo.model.Download download) { DownloadService.sendRemoveDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem).id, false); - downloads.remove(download.getId()); deleteDatabase(download.getId()); + downloads.remove(download.getId()); } public void remove(List mediaItems, List downloads) { @@ -115,6 +116,16 @@ public class DownloaderManager { return download != null ? download.getTitle() : null; } + public static void updateRequestDownload(Download download) { + updateDatabase(download.request.id); + downloads.put(download.request.id, download); + } + + public static void removeRequestDownload(Download download) { + deleteDatabase(download.request.id); + downloads.remove(download.request.id); + } + private static DownloadRepository getDownloadRepository() { return new DownloadRepository(); } @@ -134,16 +145,4 @@ public class DownloaderManager { private static void updateDatabase(String id) { getDownloadRepository().update(id); } - - public void updateRequestDownload(Download download) { - updateDatabase(download.request.id); - if (download.state == Download.STATE_COMPLETED) { - downloads.put(download.request.id, download); - } - } - - public void removeRequestDownload(Download download) { - deleteDatabase(download.request.id); - downloads.remove(download.request.id); - } } \ No newline at end of file diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java index d75f16a4..b34daf33 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java @@ -53,7 +53,6 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa private static final class TerminalStateNotificationHelper implements DownloadManager.Listener { private final Context context; private final DownloadNotificationHelper notificationHelper; - private final DownloaderManager downloaderManager; private final Notification successfulDownloadGroupNotification; private final Notification failedDownloadGroupNotification; @@ -66,7 +65,6 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) { this.context = context.getApplicationContext(); this.notificationHelper = notificationHelper; - this.downloaderManager = DownloadUtil.getDownloadTracker(context); nextNotificationId = firstNotificationId; successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification( @@ -97,7 +95,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP).build(); NotificationUtil.setNotification(this.context, successfulDownloadGroupNotificationId, successfulDownloadGroupNotification); - downloaderManager.updateRequestDownload(download); + DownloaderManager.updateRequestDownload(download); } else if (download.state == Download.STATE_FAILED) { notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP).build(); @@ -111,7 +109,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa @Override public void onDownloadRemoved(@NonNull DownloadManager downloadManager, Download download) { - downloaderManager.removeRequestDownload(download); + DownloaderManager.removeRequestDownload(download); } } }