From 149a1532cd6cafdc53de5ab740c27fe771b614f6 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Wed, 23 Feb 2022 11:39:37 -0500 Subject: [PATCH] Replace deprecated JobIntentService with WorkManager --- .../activity/WidgetConfigActivity.java | 4 +- core/src/main/AndroidManifest.xml | 5 - .../core/app/SafeJobIntentService.java | 118 ------------------ .../core/receiver/PlayerWidget.java | 8 +- .../core/widget/WidgetUpdaterJobService.java | 33 ----- .../core/widget/WidgetUpdaterWorker.java | 59 +++++++++ 6 files changed, 65 insertions(+), 162 deletions(-) delete mode 100644 core/src/main/java/androidx/core/app/SafeJobIntentService.java delete mode 100644 core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterJobService.java create mode 100644 core/src/main/java/de/danoeh/antennapod/core/widget/WidgetUpdaterWorker.java diff --git a/app/src/main/java/de/danoeh/antennapod/activity/WidgetConfigActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/WidgetConfigActivity.java index 674071294..2d2fd643b 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/WidgetConfigActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/WidgetConfigActivity.java @@ -12,7 +12,7 @@ import androidx.appcompat.app.AppCompatActivity; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.receiver.PlayerWidget; -import de.danoeh.antennapod.core.widget.WidgetUpdaterJobService; +import de.danoeh.antennapod.core.widget.WidgetUpdaterWorker; public class WidgetConfigActivity extends AppCompatActivity { private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; @@ -109,7 +109,7 @@ public class WidgetConfigActivity extends AppCompatActivity { resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); setResult(RESULT_OK, resultValue); finish(); - WidgetUpdaterJobService.performBackgroundUpdate(this); + WidgetUpdaterWorker.enqueueWork(this); } private int getColorWithAlpha(int color, int opacity) { diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index 7478eb48a..9b23acd1d 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -46,11 +46,6 @@ android:label="@string/feed_update_receiver_name" android:exported="true" tools:ignore="ExportedReceiver" /> - - diff --git a/core/src/main/java/androidx/core/app/SafeJobIntentService.java b/core/src/main/java/androidx/core/app/SafeJobIntentService.java deleted file mode 100644 index aedc9418b..000000000 --- a/core/src/main/java/androidx/core/app/SafeJobIntentService.java +++ /dev/null @@ -1,118 +0,0 @@ -package androidx.core.app; - -import android.app.job.JobParameters; -import android.app.job.JobServiceEngine; -import android.app.job.JobWorkItem; -import android.content.Intent; -import android.os.Build; -import android.os.IBinder; -import androidx.annotation.RequiresApi; -import android.util.Log; - - -public abstract class SafeJobIntentService extends JobIntentService { - - @Override - public void onCreate() { - super.onCreate(); - if (Build.VERSION.SDK_INT >= 26) { - mJobImpl = new SafeJobServiceEngineImpl(this); - } - } - - /** - * Implementation of a safe JobServiceEngine for interaction with JobIntentService. - */ - @RequiresApi(26) - static final class SafeJobServiceEngineImpl extends JobServiceEngine - implements JobIntentService.CompatJobEngine { - static final String TAG = "JobServiceEngineImpl"; - - static final boolean DEBUG = false; - - final JobIntentService mService; - final Object mLock = new Object(); - JobParameters mParams; - - final class WrapperWorkItem implements JobIntentService.GenericWorkItem { - final JobWorkItem mJobWork; - - WrapperWorkItem(JobWorkItem jobWork) { - mJobWork = jobWork; - } - - @Override - public Intent getIntent() { - return mJobWork.getIntent(); - } - - @Override - public void complete() { - synchronized (mLock) { - if (mParams != null) { - try { - mParams.completeWork(mJobWork); - } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); - } - } - } - } - } - - SafeJobServiceEngineImpl(JobIntentService service) { - super(service); - mService = service; - } - - @Override - public IBinder compatGetBinder() { - return getBinder(); - } - - @Override - public boolean onStartJob(JobParameters params) { - if (DEBUG) Log.d(TAG, "onStartJob: " + params); - mParams = params; - // We can now start dequeuing work! - mService.ensureProcessorRunningLocked(false); - return true; - } - - @Override - public boolean onStopJob(JobParameters params) { - if (DEBUG) Log.d(TAG, "onStartJob: " + params); - boolean result = mService.doStopCurrentWork(); - synchronized (mLock) { - // Once we return, the job is stopped, so its JobParameters are no - // longer valid and we should not be doing anything with them. - mParams = null; - } - return result; - } - - /** - * Dequeue some work. - */ - @Override - public JobIntentService.GenericWorkItem dequeueWork() { - JobWorkItem work = null; - synchronized (mLock) { - if (mParams == null) { - return null; - } - try { - work = mParams.dequeueWork(); - } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); - } - } - if (work != null) { - work.getIntent().setExtrasClassLoader(mService.getClassLoader()); - return new WrapperWorkItem(work); - } else { - return null; - } - } - } -} 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)); + } + } +}