tempus/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java

116 lines
5.4 KiB
Java
Raw Normal View History

2023-06-17 15:30:23 +02:00
package com.cappielloantonio.tempo.service;
2021-12-31 21:36:50 +01:00
import android.app.Notification;
import android.content.Context;
import androidx.annotation.NonNull;
2021-12-31 21:36:50 +01:00
import androidx.annotation.Nullable;
import androidx.media3.common.util.NotificationUtil;
import androidx.media3.common.util.UnstableApi;
2021-12-31 21:36:50 +01:00
import androidx.media3.exoplayer.offline.Download;
import androidx.media3.exoplayer.offline.DownloadManager;
import androidx.media3.exoplayer.offline.DownloadNotificationHelper;
import androidx.media3.exoplayer.scheduler.PlatformScheduler;
import androidx.media3.exoplayer.scheduler.Requirements;
import androidx.media3.exoplayer.scheduler.Scheduler;
2023-06-17 15:30:23 +02:00
import com.cappielloantonio.tempo.R;
import com.cappielloantonio.tempo.util.DownloadUtil;
2021-12-31 21:36:50 +01:00
import java.util.List;
@UnstableApi
2021-12-31 21:36:50 +01:00
public class DownloaderService extends androidx.media3.exoplayer.offline.DownloadService {
private static final int JOB_ID = 1;
private static final int FOREGROUND_NOTIFICATION_ID = 1;
public DownloaderService() {
super(FOREGROUND_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID, R.string.exo_download_notification_channel_name, 0);
}
@NonNull
2021-12-31 21:36:50 +01:00
@Override
protected DownloadManager getDownloadManager() {
DownloadManager downloadManager = DownloadUtil.getDownloadManager(this);
DownloadNotificationHelper downloadNotificationHelper = DownloadUtil.getDownloadNotificationHelper(this);
downloadManager.addListener(new TerminalStateNotificationHelper(this, downloadNotificationHelper, FOREGROUND_NOTIFICATION_ID + 1));
return downloadManager;
}
@NonNull
2021-12-31 21:36:50 +01:00
@Override
protected Scheduler getScheduler() {
return new PlatformScheduler(this, JOB_ID);
}
@NonNull
2021-12-31 21:36:50 +01:00
@Override
protected Notification getForegroundNotification(@NonNull List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
2021-12-31 21:36:50 +01:00
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_download, null, null, downloads, notMetRequirements);
}
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener {
private final Context context;
private final DownloadNotificationHelper notificationHelper;
2023-09-15 22:02:31 +02:00
2023-09-15 13:09:48 +02:00
private final Notification successfulDownloadGroupNotification;
private final Notification failedDownloadGroupNotification;
2023-09-15 22:02:31 +02:00
2023-09-15 13:09:48 +02:00
private final int successfulDownloadGroupNotificationId;
private final int failedDownloadGroupNotificationId;
2021-12-31 21:36:50 +01:00
2023-09-15 22:02:31 +02:00
private int nextNotificationId;
2021-12-31 21:36:50 +01:00
public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) {
this.context = context.getApplicationContext();
this.notificationHelper = notificationHelper;
nextNotificationId = firstNotificationId;
2023-09-15 13:09:48 +02:00
successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification(
this.context,
DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID,
2023-09-15 22:02:31 +02:00
DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP,
2023-09-15 13:09:48 +02:00
R.drawable.ic_check_circle,
2023-09-15 22:02:31 +02:00
"Downloads completed"
);
2023-09-15 13:09:48 +02:00
failedDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification(
this.context,
DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID,
2023-09-15 22:02:31 +02:00
DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP,
R.drawable.ic_error,
"Downloads failed"
);
2023-09-15 13:09:48 +02:00
successfulDownloadGroupNotificationId = nextNotificationId++;
failedDownloadGroupNotificationId = nextNotificationId++;
2021-12-31 21:36:50 +01:00
}
@Override
public void onDownloadChanged(@NonNull DownloadManager downloadManager, Download download, @Nullable Exception finalException) {
2021-12-31 21:36:50 +01:00
Notification notification;
if (download.state == Download.STATE_COMPLETED) {
notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, DownloaderManager.getDownloadNotificationMessage(download.request.id));
2023-09-15 22:02:31 +02:00
notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP).build();
2023-09-15 13:09:48 +02:00
NotificationUtil.setNotification(this.context, successfulDownloadGroupNotificationId, successfulDownloadGroupNotification);
DownloaderManager.updateDatabase(download.request.id);
2021-12-31 21:36:50 +01:00
} else if (download.state == Download.STATE_FAILED) {
notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, DownloaderManager.getDownloadNotificationMessage(download.request.id));
2023-09-15 22:02:31 +02:00
notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP).build();
2023-09-15 13:09:48 +02:00
NotificationUtil.setNotification(this.context, failedDownloadGroupNotificationId, failedDownloadGroupNotification);
2021-12-31 21:36:50 +01:00
} else {
return;
}
NotificationUtil.setNotification(context, nextNotificationId++, notification);
}
@Override
public void onDownloadRemoved(@NonNull DownloadManager downloadManager, Download download) {
DownloaderManager.deleteDatabase(download.request.id);
}
2021-12-31 21:36:50 +01:00
}
}