Skip to content

Commit 6b6f517

Browse files
authored
Merge pull request #34 from kimsey0/webview-server-reachability-check
Add server reachability check before loading WebView
2 parents 5a3de7f + e53f695 commit 6b6f517

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

app/src/main/java/com/immichframe/immichframe/Helpers.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import retrofit2.Call
1010
import retrofit2.http.GET
1111
import androidx.core.graphics.scale
1212
import okhttp3.OkHttpClient
13+
import okhttp3.Request
1314
import retrofit2.Retrofit
1415
import retrofit2.converter.gson.GsonConverterFactory
16+
import java.util.concurrent.TimeUnit
1517

1618
object Helpers {
1719
fun textSizeMultiplier(context: Context, currentSizeSp: Float, multiplier: Float): Float {
@@ -184,4 +186,23 @@ object Helpers {
184186
.addConverterFactory(GsonConverterFactory.create()).build()
185187
}
186188

189+
private val reachabilityClient = OkHttpClient.Builder()
190+
.connectTimeout(5, TimeUnit.SECONDS)
191+
.readTimeout(5, TimeUnit.SECONDS)
192+
.build()
193+
194+
fun isServerReachable(url: String): Boolean {
195+
return try {
196+
val request = Request.Builder()
197+
.url(url)
198+
.head()
199+
.build()
200+
reachabilityClient.newCall(request).execute().use {
201+
true // any HTTP response = reachable
202+
}
203+
} catch (e: Exception) {
204+
false
205+
}
206+
}
207+
187208
}

app/src/main/java/com/immichframe/immichframe/MainActivity.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import java.text.SimpleDateFormat
4343
import java.util.Calendar
4444
import java.util.Locale
4545
import kotlinx.coroutines.*
46+
import androidx.lifecycle.lifecycleScope
4647
import androidx.core.graphics.toColorInt
4748
import androidx.core.graphics.drawable.toDrawable
4849
import androidx.core.net.toUri
@@ -588,7 +589,7 @@ class MainActivity : AppCompatActivity() {
588589
webView.settings.javaScriptEnabled = true
589590
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
590591
webView.settings.domStorageEnabled = true
591-
webView.loadUrl(savedUrl)
592+
loadWebViewWithRetry(savedUrl)
592593
} else {
593594
retrofit = Helpers.createRetrofit(savedUrl, authSecret)
594595
apiService = retrofit!!.create(Helpers.ApiService::class.java)
@@ -856,4 +857,37 @@ class MainActivity : AppCompatActivity() {
856857
rcpServer.stop()
857858
handler.removeCallbacksAndMessages(null)
858859
}
860+
861+
private fun loadWebViewWithRetry(
862+
url: String,
863+
attempt: Int = 1,
864+
maxAttempts: Int = 36
865+
) {
866+
lifecycleScope.launch {
867+
val reachable = withContext(Dispatchers.IO) {
868+
Helpers.isServerReachable(url)
869+
}
870+
871+
if (reachable) {
872+
webView.loadUrl(url)
873+
} else if (attempt <= maxAttempts) {
874+
Toast.makeText(
875+
this@MainActivity,
876+
"Connecting to server... Attempt $attempt of $maxAttempts",
877+
Toast.LENGTH_SHORT
878+
).show()
879+
880+
delay(5_000)
881+
loadWebViewWithRetry(url, attempt + 1, maxAttempts)
882+
} else {
883+
Toast.makeText(
884+
this@MainActivity,
885+
"Could not connect to server after $maxAttempts attempts",
886+
Toast.LENGTH_LONG
887+
).show()
888+
889+
webView.loadUrl(url)
890+
}
891+
}
892+
}
859893
}

app/src/main/java/com/immichframe/immichframe/ScreenSaverService.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import android.widget.Toast
2727
import androidx.preference.PreferenceManager
2828
import kotlinx.coroutines.CoroutineScope
2929
import kotlinx.coroutines.Dispatchers
30+
import kotlinx.coroutines.Job
31+
import kotlinx.coroutines.cancel
32+
import kotlinx.coroutines.delay
3033
import kotlinx.coroutines.launch
3134
import kotlinx.coroutines.withContext
3235
import retrofit2.Call
@@ -41,6 +44,7 @@ import androidx.core.graphics.drawable.toDrawable
4144
import androidx.core.net.toUri
4245

4346
class 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)
@@ -518,7 +525,7 @@ class ScreenSaverService : DreamService() {
518525
webView.settings.javaScriptEnabled = true
519526
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
520527
webView.settings.domStorageEnabled = true
521-
webView.loadUrl(savedUrl)
528+
loadWebViewWithRetry(savedUrl)
522529
} else {
523530
retrofit = Helpers.createRetrofit(savedUrl, authSecret)
524531
apiService = retrofit!!.create(Helpers.ApiService::class.java)
@@ -613,4 +620,37 @@ class ScreenSaverService : DreamService() {
613620
}
614621
wakeLock = null
615622
}
623+
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)
653+
}
654+
}
655+
}
616656
}

0 commit comments

Comments
 (0)