From 45eb0a756d9646666535591ea7c31d883b814fc1 Mon Sep 17 00:00:00 2001 From: Tuomas Airaksinen Date: Sat, 11 Jan 2025 15:11:27 +0200 Subject: [PATCH 1/2] Reapply "Syncronize automatically when network state is restored" This reverts commit 1422e901276eabc0e5f51974f4206ab748fe4301. --- app/src/main/AndroidManifest.xml | 1 + .../view/activity/page/MainBibleActivity.kt | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c6ca2650d6..6d6712c4aa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,6 +22,7 @@ android:versionCode="845" android:versionName="5.0.845"> + diff --git a/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt b/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt index 0a5c95f655..937b3b35f5 100644 --- a/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt +++ b/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt @@ -26,6 +26,8 @@ import android.content.pm.PackageManager import android.content.res.Configuration import android.graphics.Color import android.media.AudioManager +import android.net.ConnectivityManager +import android.net.Network import android.net.Uri import android.os.Build import android.os.Bundle @@ -311,11 +313,21 @@ class MainBibleActivity : CustomTitlebarActivityBase() { } initialized = true } - syncScope.launch { startSync() } if(intent.hasExtra("openLink")) { val uri = Uri.parse(intent.getStringExtra("openLink")) openLink(uri) } + val connManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + connManager.registerDefaultNetworkCallback(networkCallback) + } + } + + var networkCallback = object: ConnectivityManager.NetworkCallback() { + override fun onAvailable(network: Network) { + super.onAvailable(network) + syncScope.launch { startSync() } + } } override fun fixNightMode() {} // handle this manually here @@ -1418,10 +1430,7 @@ class MainBibleActivity : CustomTitlebarActivityBase() { syncScope.launch { synchronize(true) } } else { updateActions() - syncScope.launch { - delay(5000) // Wait a little bit as wifi might be auto-turned on after returning from sleep - startSync() - } + syncScope.launch { startSync() } } } From 842701e6d48f6dfa9dc0da2f81781d69931294f0 Mon Sep 17 00:00:00 2001 From: Tuomas Airaksinen Date: Sat, 11 Jan 2025 15:19:32 +0200 Subject: [PATCH 2/2] Try to fix sync start when online state resumes --- .../bible/android/view/activity/page/MainBibleActivity.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt b/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt index 937b3b35f5..b65bda57fa 100644 --- a/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt +++ b/app/src/main/java/net/bible/android/view/activity/page/MainBibleActivity.kt @@ -1408,7 +1408,6 @@ class MainBibleActivity : CustomTitlebarActivityBase() { windowRepository.saveIntoDb(false) if (force || (now - max(lastSynchronized, lastTouched) > syncInterval && CloudSync.hasChanges())) { Log.i(TAG, "Performing periodic sync") - CommonUtils.settings.setLong("globalLastSynchronized", now) if(!CloudSync.signedIn) { CloudSync.signIn(this@MainBibleActivity) } @@ -1418,6 +1417,12 @@ class MainBibleActivity : CustomTitlebarActivityBase() { } } + fun onEvent(event: CloudSyncEvent) { + if (!event.running) { + CommonUtils.settings.setLong("globalLastSynchronized", now) + } + } + private fun stopPeriodicSync() { syncJob?.cancel() syncJob = null