summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorByteHamster <info@bytehamster.com>2023-07-18 16:49:17 +0200
committerByteHamster <ByteHamster@users.noreply.github.com>2023-07-18 17:57:40 +0200
commit9be6562b4e4bd4a1487a02a92c68db3625e19d99 (patch)
treef3583a0aa37f1007aae37fe0d1d750f97a89e128
parent5ae766b1a1557e2a54627e6b3c83c54c6578995d (diff)
downloadAntennaPod-9be6562b4e4bd4a1487a02a92c68db3625e19d99.zip
Fix missing foreground notification on old Android versions
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java22
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java29
2 files changed, 41 insertions, 10 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java b/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java
index 59d9df8f5..b207db83b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java
@@ -4,13 +4,17 @@ import android.app.Notification;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
+import androidx.work.ForegroundInfo;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import de.danoeh.antennapod.core.ClientConfigurator;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.feed.LocalFeedUpdater;
@@ -88,11 +92,15 @@ public class FeedUpdateWorker extends Worker {
}
@NonNull
- private Notification createNotification(List<Feed> toUpdate) {
+ private Notification createNotification(@Nullable List<Feed> toUpdate) {
Context context = getApplicationContext();
- String contentText = context.getResources().getQuantityString(R.plurals.downloads_left,
- toUpdate.size(), toUpdate.size());
- String bigText = Stream.of(toUpdate).map(feed -> "• " + feed.getTitle()).collect(Collectors.joining("\n"));
+ String contentText = "";
+ String bigText = "";
+ if (toUpdate != null) {
+ contentText = context.getResources().getQuantityString(R.plurals.downloads_left,
+ toUpdate.size(), toUpdate.size());
+ bigText = Stream.of(toUpdate).map(feed -> "• " + feed.getTitle()).collect(Collectors.joining("\n"));
+ }
return new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID_DOWNLOADING)
.setContentTitle(context.getString(R.string.download_notification_title_feeds))
.setContentText(contentText)
@@ -104,6 +112,12 @@ public class FeedUpdateWorker extends Worker {
.build();
}
+ @NonNull
+ @Override
+ public ListenableFuture<ForegroundInfo> getForegroundInfoAsync() {
+ return Futures.immediateFuture(new ForegroundInfo(R.id.notification_updating_feeds, createNotification(null)));
+ }
+
private void refreshFeeds(List<Feed> toUpdate, boolean force) {
while (!toUpdate.isEmpty()) {
if (isStopped()) {
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java
index 62956894c..2489ba2ba 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java
@@ -1,5 +1,6 @@
package de.danoeh.antennapod.core.service.download;
+import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
@@ -9,8 +10,11 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.Data;
+import androidx.work.ForegroundInfo;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import de.danoeh.antennapod.core.ClientConfigurator;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.service.download.handler.MediaDownloadedHandler;
@@ -67,7 +71,9 @@ public class EpisodeDownloadWorker extends Worker {
.putInt(DownloadServiceInterface.WORK_DATA_PROGRESS, request.getProgressPercent())
.build())
.get();
- sendProgressNotification();
+ NotificationManager nm = (NotificationManager) getApplicationContext()
+ .getSystemService(Context.NOTIFICATION_SERVICE);
+ nm.notify(R.id.notification_downloading, generateProgressNotification());
} catch (InterruptedException | ExecutionException e) {
return;
}
@@ -75,7 +81,13 @@ public class EpisodeDownloadWorker extends Worker {
}
};
progressUpdaterThread.start();
- final Result result = performDownload(media, request);
+ Result result;
+ try {
+ result = performDownload(media, request);
+ } catch (Exception e) {
+ e.printStackTrace();
+ result = Result.failure();
+ }
progressUpdaterThread.interrupt();
try {
progressUpdaterThread.join();
@@ -100,6 +112,13 @@ public class EpisodeDownloadWorker extends Worker {
}
}
+ @NonNull
+ @Override
+ public ListenableFuture<ForegroundInfo> getForegroundInfoAsync() {
+ return Futures.immediateFuture(
+ new ForegroundInfo(R.id.notification_downloading, generateProgressNotification()));
+ }
+
private Result performDownload(FeedMedia media, DownloadRequest request) {
File dest = new File(request.getDestination());
if (!dest.exists()) {
@@ -242,7 +261,7 @@ public class EpisodeDownloadWorker extends Worker {
nm.notify(R.id.notification_download_report, builder.build());
}
- private void sendProgressNotification() {
+ private Notification generateProgressNotification() {
StringBuilder bigTextB = new StringBuilder();
Map<String, Integer> progressCopy = new HashMap<>(notificationProgress);
for (Map.Entry<String, Integer> entry : progressCopy.entrySet()) {
@@ -270,8 +289,6 @@ public class EpisodeDownloadWorker extends Worker {
.setShowWhen(false)
.setSmallIcon(R.drawable.ic_notification_sync)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
- NotificationManager nm = (NotificationManager) getApplicationContext()
- .getSystemService(Context.NOTIFICATION_SERVICE);
- nm.notify(R.id.notification_downloading, builder.build());
+ return builder.build();
}
}