Add cancel action to download notification (#6353)

This commit is contained in:
ByteHamster 2023-02-25 16:30:58 +01:00 committed by GitHub
parent 34553475d9
commit 3e077e5653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 9 deletions

View File

@ -39,9 +39,8 @@ public class DownloadServiceNotification {
.setOnlyAlertOnce(true)
.setShowWhen(false)
.setContentIntent(getNotificationContentIntent(context))
.setSmallIcon(R.drawable.ic_notification_sync);
notificationCompatBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
.setSmallIcon(R.drawable.ic_notification_sync)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
Log.d(TAG, "Notification set up");
}
@ -62,12 +61,27 @@ public class DownloadServiceNotification {
} else {
contentTitle = context.getString(R.string.download_notification_title);
}
String contentText = (downloads.size() > 0)
? context.getResources().getQuantityString(R.plurals.downloads_left, downloads.size(), downloads.size())
: context.getString(R.string.completing);
String bigText = compileNotificationString(downloads);
if (!bigText.contains("\n")) {
contentText = bigText;
int numDownloads = getNumberOfRunningDownloads(downloads);
String contentText = context.getString(R.string.completing);
String bigText = context.getString(R.string.completing);
notificationCompatBuilder.clearActions();
if (numDownloads > 0) {
bigText = compileNotificationString(downloads);
if (numDownloads == 1) {
contentText = bigText;
} else {
contentText = context.getResources().getQuantityString(R.plurals.downloads_left,
numDownloads, numDownloads);
}
Intent cancelDownloadsIntent = new Intent(DownloadService.ACTION_CANCEL_ALL_DOWNLOADS);
cancelDownloadsIntent.setPackage(context.getPackageName());
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(context,
R.id.pending_intent_download_cancel_all, cancelDownloadsIntent, PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
notificationCompatBuilder.addAction(new NotificationCompat.Action(
R.drawable.ic_notification_cancel, context.getString(R.string.cancel_label), cancelPendingIntent));
}
notificationCompatBuilder.setContentTitle(contentTitle);
@ -76,6 +90,16 @@ public class DownloadServiceNotification {
return notificationCompatBuilder.build();
}
private int getNumberOfRunningDownloads(List<Downloader> downloads) {
int running = 0;
for (Downloader downloader : downloads) {
if (!downloader.cancelled && !downloader.isFinished()) {
running++;
}
}
return running;
}
private boolean typeIsOnly(List<Downloader> downloads, int feedFileType) {
for (Downloader downloader : downloads) {
if (downloader.cancelled) {

View File

@ -2,6 +2,7 @@
<resources>
<item name="pending_intent_download_service_notification" type="id"/>
<item name="pending_intent_download_service_retry" type="id"/>
<item name="pending_intent_download_cancel_all" type="id"/>
<item name="pending_intent_download_service_auth" type="id"/>
<item name="pending_intent_download_service_report" type="id"/>
<item name="pending_intent_download_service_autodownload_report" type="id"/>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFFFF" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/>
</vector>