@@ -27,6 +27,9 @@ import android.widget.Toast
2727import androidx.preference.PreferenceManager
2828import kotlinx.coroutines.CoroutineScope
2929import kotlinx.coroutines.Dispatchers
30+ import kotlinx.coroutines.Job
31+ import kotlinx.coroutines.cancel
32+ import kotlinx.coroutines.delay
3033import kotlinx.coroutines.launch
3134import kotlinx.coroutines.withContext
3235import retrofit2.Call
@@ -41,6 +44,7 @@ import androidx.core.graphics.drawable.toDrawable
4144import androidx.core.net.toUri
4245
4346class ScreenSaverService : DreamService () {
47+ private var webViewRetryScope: CoroutineScope ? = null
4448 private var wakeLock: PowerManager .WakeLock ? = null
4549 private lateinit var webView: WebView
4650 private lateinit var imageView1: ImageView
@@ -83,6 +87,7 @@ class ScreenSaverService : DreamService() {
8387 @SuppressLint(" ClickableViewAccessibility" )
8488 override fun onDreamingStarted () {
8589 super .onDreamingStarted()
90+ webViewRetryScope = CoroutineScope (Dispatchers .Main + Job ())
8691 isFullscreen = true
8792 isInteractive = true
8893 setContentView(R .layout.screen_saver_view)
@@ -105,6 +110,8 @@ class ScreenSaverService : DreamService() {
105110
106111 override fun onDreamingStopped () {
107112 super .onDreamingStopped()
113+ webViewRetryScope?.cancel()
114+ webViewRetryScope = null
108115 stopImageTimer()
109116 releaseWakeLock()
110117 handler.removeCallbacksAndMessages(null )
@@ -614,32 +621,35 @@ class ScreenSaverService : DreamService() {
614621 wakeLock = null
615622 }
616623
617- private fun loadWebViewWithRetry (url : String , attempt : Int = 1, maxAttempts : Int = 36) {
618- CoroutineScope (Dispatchers .IO ).launch {
619- val reachable = Helpers .isServerReachable(url)
620- withContext(Dispatchers .Main ) {
621- if (reachable) {
622- webView.loadUrl(url)
623- } else {
624- if (attempt <= maxAttempts) {
625- Toast .makeText(
626- this @ScreenSaverService,
627- " Connecting to server... Attempt $attempt of $maxAttempts " ,
628- Toast .LENGTH_SHORT
629- ).show()
630- handler.postDelayed({
631- loadWebViewWithRetry(url, attempt + 1 , maxAttempts)
632- }, 5000 )
633- } else {
634- Toast .makeText(
635- this @ScreenSaverService,
636- " Could not connect to server after $maxAttempts attempts" ,
637- Toast .LENGTH_LONG
638- ).show()
639- // Load anyway as a last resort - maybe the server will respond
640- webView.loadUrl(url)
641- }
642- }
624+ private fun loadWebViewWithRetry (
625+ url : String ,
626+ attempt : Int = 1,
627+ maxAttempts : Int = 36
628+ ) {
629+ webViewRetryScope?.launch {
630+ val reachable = withContext(Dispatchers .IO ) {
631+ Helpers .isServerReachable(url)
632+ }
633+
634+ if (reachable) {
635+ webView.loadUrl(url)
636+ } else if (attempt <= maxAttempts) {
637+ Toast .makeText(
638+ this @ScreenSaverService,
639+ " Connecting to server... Attempt $attempt of $maxAttempts " ,
640+ Toast .LENGTH_SHORT
641+ ).show()
642+
643+ delay(5_000 )
644+ loadWebViewWithRetry(url, attempt + 1 , maxAttempts)
645+ } else {
646+ Toast .makeText(
647+ this @ScreenSaverService,
648+ " Could not connect to server after $maxAttempts attempts" ,
649+ Toast .LENGTH_LONG
650+ ).show()
651+
652+ webView.loadUrl(url)
643653 }
644654 }
645655 }
0 commit comments