mirror of
https://github.com/yuliskov/SmartTube.git
synced 2026-10-02 01:54:34 +08:00
UpdateChannelsWorker: skip sync while app foregrounded, fixes playback stutters #6234
This commit is contained in:
+9
-1
@@ -11,6 +11,7 @@ import androidx.work.WorkManager;
|
||||
import androidx.work.Worker;
|
||||
import androidx.work.WorkerParameters;
|
||||
|
||||
import com.liskovsoft.sharedutils.helpers.Helpers;
|
||||
import com.liskovsoft.sharedutils.mylogger.Log;
|
||||
import com.liskovsoft.sharedutils.prefs.GlobalPreferences;
|
||||
|
||||
@@ -31,6 +32,7 @@ public class UpdateChannelsWorker extends Worker {
|
||||
private static final String WORK_NAME = "Update channels";
|
||||
private static final long REPEAT_INTERVAL_MINUTES = 15; // 15 - minimal interval
|
||||
private final UpdateChannelsTask mTask;
|
||||
private static volatile boolean sIsWorkScheduled;
|
||||
|
||||
public UpdateChannelsWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
||||
super(context, workerParams);
|
||||
@@ -40,6 +42,8 @@ public class UpdateChannelsWorker extends Worker {
|
||||
|
||||
public static void schedule(Context context) {
|
||||
if (VERSION.SDK_INT >= 23 && GlobalPreferences.instance(context).isChannelsServiceEnabled()) {
|
||||
sIsWorkScheduled = true;
|
||||
|
||||
WorkManager workManager = WorkManager.getInstance(context);
|
||||
|
||||
// https://stackoverflow.com/questions/50943056/avoiding-duplicating-periodicworkrequest-from-workmanager
|
||||
@@ -68,7 +72,11 @@ public class UpdateChannelsWorker extends Worker {
|
||||
public Result doWork() {
|
||||
Log.d(TAG, "Starting worker %s...", this);
|
||||
|
||||
mTask.run();
|
||||
// Foreground ⇒ possible playback; skip sync to avoid CPU contention during video decode (#6234)
|
||||
if (!Helpers.isAppInForeground() || sIsWorkScheduled) {
|
||||
sIsWorkScheduled = false;
|
||||
mTask.run();
|
||||
}
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user