Fix missing foreground notification on old Android versions
This commit is contained in:
parent
5ae766b1a1
commit
9be6562b4e
@ -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()) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user