diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2022-02-23 23:11:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-23 23:11:17 +0100 |
commit | b6da9ae32912f378c4a715e63076de98d368bf1a (patch) | |
tree | eec6fde62c3c48dd238554b8d3fc1462ea2d13b3 /core/src/main/java/de/danoeh | |
parent | ecaba7525ff63b7837a370c2ccc3b5f8b1c52aed (diff) | |
parent | 149a1532cd6cafdc53de5ab740c27fe771b614f6 (diff) | |
download | AntennaPod-b6da9ae32912f378c4a715e63076de98d368bf1a.zip |
Merge pull request #5742 from TacoTheDank/widgetUpdaterWorkManager
Replace deprecated JobIntentService with WorkManager
Diffstat (limited to 'core/src/main/java/de/danoeh')
3 files changed, 63 insertions, 37 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/receiver/PlayerWidget.java b/core/src/main/java/de/danoeh/antennapod/core/receiver/PlayerWidget.java index cf0debed2..842dcdf75 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/receiver/PlayerWidget.java +++ b/core/src/main/java/de/danoeh/antennapod/core/receiver/PlayerWidget.java @@ -9,7 +9,7 @@ import android.util.Log; import java.util.Arrays; -import de.danoeh.antennapod.core.widget.WidgetUpdaterJobService; +import de.danoeh.antennapod.core.widget.WidgetUpdaterWorker; public class PlayerWidget extends AppWidgetProvider { private static final String TAG = "PlayerWidget"; @@ -25,7 +25,7 @@ public class PlayerWidget extends AppWidgetProvider { public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive"); super.onReceive(context, intent); - WidgetUpdaterJobService.performBackgroundUpdate(context); + WidgetUpdaterWorker.enqueueWork(context); } @Override @@ -33,14 +33,14 @@ public class PlayerWidget extends AppWidgetProvider { super.onEnabled(context); Log.d(TAG, "Widget enabled"); setEnabled(context, true); - WidgetUpdaterJobService.performBackgroundUpdate(context); + WidgetUpdaterWorker.enqueueWork(context); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = [" + appWidgetManager + "], appWidgetIds = [" + Arrays.toString(appWidgetIds) + "]"); - WidgetUpdaterJobService.performBackgroundUpdate(context); + WidgetUpdaterWorker.enqueueWork(context); } @Override diff --git a/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterJobService.java b/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterJobService.java deleted file mode 100644 index 598544a0c..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterJobService.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.danoeh.antennapod.core.widget; - -import android.content.Context; -import android.content.Intent; -import androidx.annotation.NonNull; -import androidx.core.app.SafeJobIntentService; -import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils; -import de.danoeh.antennapod.model.playback.Playable; -import de.danoeh.antennapod.core.util.playback.PlayableUtils; -import de.danoeh.antennapod.playback.base.PlayerStatus; - -public class WidgetUpdaterJobService extends SafeJobIntentService { - private static final int JOB_ID = -17001; - - /** - * Loads the current media from the database and updates the widget in a background job. - */ - public static void performBackgroundUpdate(Context context) { - enqueueWork(context, WidgetUpdaterJobService.class, - WidgetUpdaterJobService.JOB_ID, new Intent(context, WidgetUpdaterJobService.class)); - } - - @Override - protected void onHandleWork(@NonNull Intent intent) { - Playable media = PlayableUtils.createInstanceFromPreferences(getApplicationContext()); - if (media != null) { - WidgetUpdater.updateWidget(this, new WidgetUpdater.WidgetState(media, PlayerStatus.STOPPED, - media.getPosition(), media.getDuration(), PlaybackSpeedUtils.getCurrentPlaybackSpeed(media))); - } else { - WidgetUpdater.updateWidget(this, new WidgetUpdater.WidgetState(PlayerStatus.STOPPED)); - } - } -}
\ No newline at end of file diff --git a/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterWorker.java b/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterWorker.java new file mode 100644 index 000000000..3b8546a8e --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterWorker.java @@ -0,0 +1,59 @@ +package de.danoeh.antennapod.core.widget; + +import android.content.Context; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; +import androidx.work.WorkRequest; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils; +import de.danoeh.antennapod.core.util.playback.PlayableUtils; +import de.danoeh.antennapod.model.playback.Playable; +import de.danoeh.antennapod.playback.base.PlayerStatus; + +public class WidgetUpdaterWorker extends Worker { + + private static final String TAG = "WidgetUpdaterWorker"; + + public WidgetUpdaterWorker(@NonNull final Context context, + @NonNull final WorkerParameters workerParams) { + super(context, workerParams); + } + + public static void enqueueWork(final Context context) { + final WorkRequest workRequest = new OneTimeWorkRequest.Builder(WidgetUpdaterWorker.class).build(); + WorkManager.getInstance(context).enqueue(workRequest); + } + + @NonNull + @Override + public Result doWork() { + try { + updateWidget(); + } catch (final Exception e) { + Log.d(TAG, "Failed to update AntennaPod widget: ", e); + return Result.failure(); + } + return Result.success(); + } + + /** + * Loads the current media from the database and updates the widget in a background job. + */ + private void updateWidget() { + final Playable media = PlayableUtils.createInstanceFromPreferences(getApplicationContext()); + if (media != null) { + WidgetUpdater.updateWidget(getApplicationContext(), + new WidgetUpdater.WidgetState(media, PlayerStatus.STOPPED, + media.getPosition(), media.getDuration(), + PlaybackSpeedUtils.getCurrentPlaybackSpeed(media))); + } else { + WidgetUpdater.updateWidget(getApplicationContext(), + new WidgetUpdater.WidgetState(PlayerStatus.STOPPED)); + } + } +} |