Don't try to start foreground service, Android doesn't let us anyway (#6386)

This commit is contained in:
ByteHamster 2023-03-31 22:17:49 +02:00 committed by GitHub
parent 548f9e021e
commit d5321a147b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 5 deletions

View File

@ -5,7 +5,7 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.work.ForegroundInfo; import androidx.core.app.NotificationManagerCompat;
import androidx.work.WorkManager; import androidx.work.WorkManager;
import androidx.work.Worker; import androidx.work.Worker;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
@ -38,10 +38,12 @@ public class FeedUpdateWorker extends Worker {
private static final String TAG = "FeedUpdateWorker"; private static final String TAG = "FeedUpdateWorker";
private final NewEpisodesNotification newEpisodesNotification; private final NewEpisodesNotification newEpisodesNotification;
private final NotificationManagerCompat notificationManager;
public FeedUpdateWorker(@NonNull Context context, @NonNull WorkerParameters params) { public FeedUpdateWorker(@NonNull Context context, @NonNull WorkerParameters params) {
super(context, params); super(context, params);
newEpisodesNotification = new NewEpisodesNotification(); newEpisodesNotification = new NewEpisodesNotification();
notificationManager = NotificationManagerCompat.from(context);
} }
@Override @Override
@ -77,16 +79,17 @@ public class FeedUpdateWorker extends Worker {
toUpdate.add(feed); toUpdate.add(feed);
refreshFeeds(toUpdate, true); refreshFeeds(toUpdate, true);
} }
notificationManager.cancel(R.id.notification_updating_feeds);
return Result.success(); return Result.success();
} }
@NonNull @NonNull
private ForegroundInfo createForegroundInfo(List<Feed> toUpdate) { private Notification createNotification(List<Feed> toUpdate) {
Context context = getApplicationContext(); Context context = getApplicationContext();
String contentText = context.getResources().getQuantityString(R.plurals.downloads_left, String contentText = context.getResources().getQuantityString(R.plurals.downloads_left,
toUpdate.size(), toUpdate.size()); toUpdate.size(), toUpdate.size());
String bigText = Stream.of(toUpdate).map(feed -> "" + feed.getTitle()).collect(Collectors.joining("\n")); String bigText = Stream.of(toUpdate).map(feed -> "" + feed.getTitle()).collect(Collectors.joining("\n"));
Notification notification = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID_DOWNLOADING) return new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID_DOWNLOADING)
.setContentTitle(context.getString(R.string.download_notification_title_feeds)) .setContentTitle(context.getString(R.string.download_notification_title_feeds))
.setContentText(contentText) .setContentText(contentText)
.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText)) .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
@ -95,7 +98,6 @@ public class FeedUpdateWorker extends Worker {
.addAction(R.drawable.ic_cancel, context.getString(R.string.cancel_label), .addAction(R.drawable.ic_cancel, context.getString(R.string.cancel_label),
WorkManager.getInstance(context).createCancelPendingIntent(getId())) WorkManager.getInstance(context).createCancelPendingIntent(getId()))
.build(); .build();
return new ForegroundInfo(R.id.notification_updating_feeds, notification);
} }
private void refreshFeeds(List<Feed> toUpdate, boolean force) { private void refreshFeeds(List<Feed> toUpdate, boolean force) {
@ -103,7 +105,7 @@ public class FeedUpdateWorker extends Worker {
if (isStopped()) { if (isStopped()) {
return; return;
} }
setForegroundAsync(createForegroundInfo(toUpdate)); notificationManager.notify(R.id.notification_updating_feeds, createNotification(toUpdate));
Feed feed = toUpdate.get(0); Feed feed = toUpdate.get(0);
try { try {
if (feed.isLocalFeed()) { if (feed.isLocalFeed()) {