From d789d08f319aa157ededb6c51a78a911500f4841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sun, 3 Nov 2019 16:10:30 +0100 Subject: [PATCH] [Sync] Fix auto sync when screen is off/device is in Doze mode. --- .../java/pl/szczodrzynski/edziennik/App.java | 2 +- .../szczodrzynski/edziennik/MainActivity.kt | 3 ++- .../edziennik/receivers/BootReceiver.java | 2 +- .../edziennik/sync/SyncWorker.kt | 23 +++++++++++++------ .../modules/settings/SettingsNewFragment.java | 4 ++-- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/App.java b/app/src/main/java/pl/szczodrzynski/edziennik/App.java index 230bce79..b72c713e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/App.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/App.java @@ -310,7 +310,7 @@ public class App extends androidx.multidex.MultiDexApplication implements Config //profileLoadById(appSharedPrefs.getInt("current_profile_id", 1)); if (appConfig.registerSyncEnabled) { - SyncWorker.Companion.scheduleNext(this); + SyncWorker.Companion.scheduleNext(this, false); } else { SyncWorker.Companion.cancelNext(this); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index cc04e8d0..b6a56582 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -41,6 +41,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata.* import pl.szczodrzynski.edziennik.databinding.ActivitySzkolnyBinding import pl.szczodrzynski.edziennik.network.ServerRequest import pl.szczodrzynski.edziennik.sync.AppManagerDetectedEvent +import pl.szczodrzynski.edziennik.sync.SyncWorker import pl.szczodrzynski.edziennik.ui.dialogs.changelog.ChangelogDialog import pl.szczodrzynski.edziennik.ui.modules.agenda.AgendaFragment import pl.szczodrzynski.edziennik.ui.modules.announcements.AnnouncementsFragment @@ -369,7 +370,7 @@ class MainActivity : AppCompatActivity() { isStoragePermissionGranted() - //SyncWorker.scheduleNext(app) + SyncWorker.scheduleNext(app) // APP BACKGROUND if (app.appConfig.appBackground != null) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/receivers/BootReceiver.java b/app/src/main/java/pl/szczodrzynski/edziennik/receivers/BootReceiver.java index b1524237..f1427ff3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/receivers/BootReceiver.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/receivers/BootReceiver.java @@ -82,7 +82,7 @@ public class BootReceiver extends BroadcastReceiver { } else { - SyncWorker.Companion.scheduleNext(app); + SyncWorker.Companion.scheduleNext(app, false); if (app.networkUtils.isOnline()) { checkUpdate(context, intent); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/sync/SyncWorker.kt b/app/src/main/java/pl/szczodrzynski/edziennik/sync/SyncWorker.kt index 3c0e032e..4405c10b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/sync/SyncWorker.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/sync/SyncWorker.kt @@ -22,7 +22,7 @@ class SyncWorker(val context: Context, val params: WorkerParameters) : Worker(co * Schedule the sync job only if it's not already scheduled. */ @SuppressLint("RestrictedApi") - fun scheduleNext(app: App) { + fun scheduleNext(app: App, rescheduleIfFailedFound: Boolean = true) { AsyncTask.execute { val workManager = WorkManager.getInstance(app) as WorkManagerImpl val scheduledWork = workManager.workDatabase.workSpecDao().scheduledWork @@ -35,13 +35,22 @@ class SyncWorker(val context: Context, val params: WorkerParameters) : Worker(co // remove all enqueued work that had to (but didn't) run at some point in the past (at least 1min ago) val failedWork = scheduledWork.filter { it.state == WorkInfo.State.ENQUEUED && it.periodStartTime+it.initialDelay < System.currentTimeMillis() - 1*MINUTE*1000 } d(TAG, "${failedWork.size} work requests failed to start (out of ${scheduledWork.size} requests)") - if (failedWork.isNotEmpty()) { - d(TAG, "App Manager detected!") - EventBus.getDefault().postSticky(AppManagerDetectedEvent(failedWork.map { it.periodStartTime+it.initialDelay })) + if (rescheduleIfFailedFound) { + if (failedWork.isNotEmpty()) { + d(TAG, "App Manager detected!") + EventBus.getDefault().postSticky(AppManagerDetectedEvent(failedWork.map { it.periodStartTime + it.initialDelay })) + } + if (scheduledWork.size - failedWork.size < 1) { + d(TAG, "No pending work found, scheduling next:") + rescheduleNext(app) + } } - if (scheduledWork.size-failedWork.size < 1) { - d(TAG, "No pending work found, scheduling next:") - rescheduleNext(app) + else { + d(TAG, "NOT rescheduling: waiting to open the activity") + if (scheduledWork.size < 1) { + d(TAG, "No work found *at all*, scheduling next:") + rescheduleNext(app) + } } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java index c442bdc6..9929ec74 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/settings/SettingsNewFragment.java @@ -535,7 +535,7 @@ public class SettingsNewFragment extends MaterialAboutFragment { List intervalNames = new ArrayList<>(); if (App.devMode) { intervalNames.add(HomeFragment.plural(activity, R.plurals.time_till_seconds, 30)); - intervalNames.add(HomeFragment.plural(activity, R.plurals.time_till_minutes, 5)); + intervalNames.add(HomeFragment.plural(activity, R.plurals.time_till_minutes, 2)); } intervalNames.add(HomeFragment.plural(activity, R.plurals.time_till_minutes, 30)); intervalNames.add(HomeFragment.plural(activity, R.plurals.time_till_minutes, 45)); @@ -547,7 +547,7 @@ public class SettingsNewFragment extends MaterialAboutFragment { List intervals = new ArrayList<>(); if (App.devMode) { intervals.add(30); - intervals.add(5 * 60); + intervals.add(2 * 60); } intervals.add(30 * 60); intervals.add(45 * 60);