mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
feat: add notification groups
This commit is contained in:
parent
c0f5abdfae
commit
383fbd1c49
2 changed files with 39 additions and 1 deletions
|
|
@ -52,16 +52,38 @@ 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 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 int nextNotificationId;
|
||||||
|
private final Notification successfulDownloadGroupNotification;
|
||||||
|
private final Notification failedDownloadGroupNotification;
|
||||||
|
private final int successfulDownloadGroupNotificationId;
|
||||||
|
private final int failedDownloadGroupNotificationId;
|
||||||
|
|
||||||
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;
|
||||||
nextNotificationId = firstNotificationId;
|
nextNotificationId = firstNotificationId;
|
||||||
|
|
||||||
|
successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification(
|
||||||
|
this.context,
|
||||||
|
DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID,
|
||||||
|
SUCCESSFUL_DOWNLOAD_GROUP,
|
||||||
|
R.drawable.ic_check_circle,
|
||||||
|
"Downloads completed");
|
||||||
|
failedDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification(
|
||||||
|
this.context,
|
||||||
|
DownloadUtil.DOWNLOAD_NOTIFICATION_CHANNEL_ID,
|
||||||
|
FAILED_DOWNLOAD_GROUP,
|
||||||
|
R.drawable.ic_check_circle,
|
||||||
|
"Downloads failed");
|
||||||
|
|
||||||
|
successfulDownloadGroupNotificationId = nextNotificationId++;
|
||||||
|
failedDownloadGroupNotificationId = nextNotificationId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -70,9 +92,13 @@ 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();
|
||||||
|
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();
|
||||||
|
NotificationUtil.setNotification(this.context, failedDownloadGroupNotificationId, failedDownloadGroupNotification);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package com.cappielloantonio.tempo.util;
|
package com.cappielloantonio.tempo.util;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.media3.common.util.UnstableApi;
|
import androidx.media3.common.util.UnstableApi;
|
||||||
import androidx.media3.database.DatabaseProvider;
|
import androidx.media3.database.DatabaseProvider;
|
||||||
import androidx.media3.database.StandaloneDatabaseProvider;
|
import androidx.media3.database.StandaloneDatabaseProvider;
|
||||||
|
|
@ -182,4 +184,14 @@ public final class DownloadUtil {
|
||||||
|
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Notification buildGroupSummaryNotification(Context context, String channelId, String groupId, int icon, String title) {
|
||||||
|
return new NotificationCompat.Builder(context, channelId)
|
||||||
|
.setContentTitle(title)
|
||||||
|
.setSmallIcon(icon)
|
||||||
|
.setGroup(groupId)
|
||||||
|
.setGroupSummary(true)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue