[Sync] Fix auto sync when screen is off/device is in Doze mode.

This commit is contained in:
Kuba Szczodrzyński 2019-11-03 16:10:30 +01:00
parent 07863fed6f
commit d789d08f31
5 changed files with 22 additions and 12 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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)
}
}
}
}

View File

@ -535,7 +535,7 @@ public class SettingsNewFragment extends MaterialAboutFragment {
List<CharSequence> 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<Integer> 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);