From 4fc75f5c46b4ce230425d7e9bd3f9e563f60833c Mon Sep 17 00:00:00 2001 From: antonio Date: Fri, 15 Sep 2023 22:02:31 +0200 Subject: [PATCH] refactor: code cleanup --- .../tempo/service/DownloaderService.java | 27 ++++++++++--------- .../tempo/util/DownloadUtil.java | 3 ++- 2 files changed, 16 insertions(+), 14 deletions(-) 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 420b9d07..e34867a0 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java @@ -51,19 +51,17 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa } private static final class TerminalStateNotificationHelper implements DownloadManager.Listener { - private static final String TAG = "TerminalStateNotificatinHelper"; - private static final String SUCCESSFUL_DOWNLOAD_GROUP = "com.cappielloantonio.tempo.SuccessfulDownload"; - private static final String FAILED_DOWNLOAD_GROUP = "com.cappielloantonio.tempo.FailedDownload"; - - private final Context context; private final DownloadNotificationHelper notificationHelper; - private int nextNotificationId; + private final Notification successfulDownloadGroupNotification; private final Notification failedDownloadGroupNotification; + private final int successfulDownloadGroupNotificationId; private final int failedDownloadGroupNotificationId; + private int nextNotificationId; + public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) { this.context = context.getApplicationContext(); this.notificationHelper = notificationHelper; @@ -72,15 +70,18 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification( this.context, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, - SUCCESSFUL_DOWNLOAD_GROUP, + DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP, R.drawable.ic_check_circle, - "Downloads completed"); + "Downloads completed" + ); + failedDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification( this.context, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, - FAILED_DOWNLOAD_GROUP, - R.drawable.ic_check_circle, - "Downloads failed"); + DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP, + R.drawable.ic_error, + "Downloads failed" + ); successfulDownloadGroupNotificationId = nextNotificationId++; failedDownloadGroupNotificationId = nextNotificationId++; @@ -92,12 +93,12 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa if (download.state == Download.STATE_COMPLETED) { notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); - notification = Notification.Builder.recoverBuilder(context, notification).setGroup(SUCCESSFUL_DOWNLOAD_GROUP).build(); + notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP).build(); NotificationUtil.setNotification(this.context, successfulDownloadGroupNotificationId, successfulDownloadGroupNotification); DownloaderManager.updateDatabase(download.request.id); } 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(FAILED_DOWNLOAD_GROUP).build(); + notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP).build(); NotificationUtil.setNotification(this.context, failedDownloadGroupNotificationId, failedDownloadGroupNotification); } else { return; diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java b/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java index 8ab29442..433a4efd 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java +++ b/app/src/main/java/com/cappielloantonio/tempo/util/DownloadUtil.java @@ -32,6 +32,8 @@ import java.util.concurrent.Executors; public final class DownloadUtil { public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel"; + public static final String DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP = "com.cappielloantonio.tempo.SuccessfulDownload"; + public static final String DOWNLOAD_NOTIFICATION_FAILED_GROUP = "com.cappielloantonio.tempo.FailedDownload"; private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads"; @@ -193,5 +195,4 @@ public final class DownloadUtil { .setGroupSummary(true) .build(); } - }