Replace deprecated JobIntentService with WorkManager
This commit is contained in:
parent
449f1ca339
commit
149a1532cd
|
@ -12,7 +12,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||||
import de.danoeh.antennapod.R;
|
import de.danoeh.antennapod.R;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.receiver.PlayerWidget;
|
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 {
|
public class WidgetConfigActivity extends AppCompatActivity {
|
||||||
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
||||||
|
@ -109,7 +109,7 @@ public class WidgetConfigActivity extends AppCompatActivity {
|
||||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
||||||
setResult(RESULT_OK, resultValue);
|
setResult(RESULT_OK, resultValue);
|
||||||
finish();
|
finish();
|
||||||
WidgetUpdaterJobService.performBackgroundUpdate(this);
|
WidgetUpdaterWorker.enqueueWork(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getColorWithAlpha(int color, int opacity) {
|
private int getColorWithAlpha(int color, int opacity) {
|
||||||
|
|
|
@ -46,11 +46,6 @@
|
||||||
android:label="@string/feed_update_receiver_name"
|
android:label="@string/feed_update_receiver_name"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
tools:ignore="ExportedReceiver" /> <!-- allow feeds update to be triggered by external apps -->
|
tools:ignore="ExportedReceiver" /> <!-- allow feeds update to be triggered by external apps -->
|
||||||
|
|
||||||
<service
|
|
||||||
android:name=".widget.WidgetUpdaterJobService"
|
|
||||||
android:permission="android.permission.BIND_JOB_SERVICE"
|
|
||||||
android:exported="true"/>
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
<queries>
|
<queries>
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,7 +9,7 @@ import android.util.Log;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.widget.WidgetUpdaterJobService;
|
import de.danoeh.antennapod.core.widget.WidgetUpdaterWorker;
|
||||||
|
|
||||||
public class PlayerWidget extends AppWidgetProvider {
|
public class PlayerWidget extends AppWidgetProvider {
|
||||||
private static final String TAG = "PlayerWidget";
|
private static final String TAG = "PlayerWidget";
|
||||||
|
@ -25,7 +25,7 @@ public class PlayerWidget extends AppWidgetProvider {
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Log.d(TAG, "onReceive");
|
Log.d(TAG, "onReceive");
|
||||||
super.onReceive(context, intent);
|
super.onReceive(context, intent);
|
||||||
WidgetUpdaterJobService.performBackgroundUpdate(context);
|
WidgetUpdaterWorker.enqueueWork(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -33,14 +33,14 @@ public class PlayerWidget extends AppWidgetProvider {
|
||||||
super.onEnabled(context);
|
super.onEnabled(context);
|
||||||
Log.d(TAG, "Widget enabled");
|
Log.d(TAG, "Widget enabled");
|
||||||
setEnabled(context, true);
|
setEnabled(context, true);
|
||||||
WidgetUpdaterJobService.performBackgroundUpdate(context);
|
WidgetUpdaterWorker.enqueueWork(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = ["
|
Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = ["
|
||||||
+ appWidgetManager + "], appWidgetIds = [" + Arrays.toString(appWidgetIds) + "]");
|
+ appWidgetManager + "], appWidgetIds = [" + Arrays.toString(appWidgetIds) + "]");
|
||||||
WidgetUpdaterJobService.performBackgroundUpdate(context);
|
WidgetUpdaterWorker.enqueueWork(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue