refactor: code cleanup

This commit is contained in:
antonio 2023-09-15 22:02:31 +02:00
parent baf1bc48b0
commit 4fc75f5c46
2 changed files with 16 additions and 14 deletions

View file

@ -51,19 +51,17 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa
} }
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener { 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 Context context;
private final DownloadNotificationHelper notificationHelper; private final DownloadNotificationHelper notificationHelper;
private int nextNotificationId;
private final Notification successfulDownloadGroupNotification; private final Notification successfulDownloadGroupNotification;
private final Notification failedDownloadGroupNotification; private final Notification failedDownloadGroupNotification;
private final int successfulDownloadGroupNotificationId; private final int successfulDownloadGroupNotificationId;
private final int failedDownloadGroupNotificationId; private final int failedDownloadGroupNotificationId;
private int nextNotificationId;
public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) { public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
this.notificationHelper = notificationHelper; this.notificationHelper = notificationHelper;
@ -72,15 +70,18 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa
successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification( successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification(
this.context, this.context,
DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID,
SUCCESSFUL_DOWNLOAD_GROUP, DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP,
R.drawable.ic_check_circle, R.drawable.ic_check_circle,
"Downloads completed"); "Downloads completed"
);
failedDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification( failedDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification(
this.context, this.context,
DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID,
FAILED_DOWNLOAD_GROUP, DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP,
R.drawable.ic_check_circle, R.drawable.ic_error,
"Downloads failed"); "Downloads failed"
);
successfulDownloadGroupNotificationId = nextNotificationId++; successfulDownloadGroupNotificationId = nextNotificationId++;
failedDownloadGroupNotificationId = nextNotificationId++; failedDownloadGroupNotificationId = nextNotificationId++;
@ -92,12 +93,12 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa
if (download.state == Download.STATE_COMPLETED) { if (download.state == Download.STATE_COMPLETED) {
notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); 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); NotificationUtil.setNotification(this.context, successfulDownloadGroupNotificationId, successfulDownloadGroupNotification);
DownloaderManager.updateDatabase(download.request.id); DownloaderManager.updateDatabase(download.request.id);
} else if (download.state == Download.STATE_FAILED) { } else if (download.state == Download.STATE_FAILED) {
notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); 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); NotificationUtil.setNotification(this.context, failedDownloadGroupNotificationId, failedDownloadGroupNotification);
} else { } else {
return; return;

View file

@ -32,6 +32,8 @@ import java.util.concurrent.Executors;
public final class DownloadUtil { public final class DownloadUtil {
public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel"; 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"; private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
@ -193,5 +195,4 @@ public final class DownloadUtil {
.setGroupSummary(true) .setGroupSummary(true)
.build(); .build();
} }
} }