@@ -12,6 +12,7 @@ import android.os.StrictMode.VmPolicy
1212import android.provider.Settings
1313import androidx.appcompat.app.AppCompatDelegate
1414import androidx.work.ExistingPeriodicWorkPolicy
15+ import androidx.annotation.WorkerThread
1516import androidx.work.PeriodicWorkRequest
1617import androidx.work.WorkManager
1718import dagger.hilt.android.EntryPointAccessors
@@ -37,6 +38,7 @@ import org.ole.planet.myplanet.datamanager.DatabaseService
3738import org.ole.planet.myplanet.di.ApiClientEntryPoint
3839import org.ole.planet.myplanet.di.AppPreferences
3940import org.ole.planet.myplanet.di.DefaultPreferences
41+ import org.ole.planet.myplanet.di.HealthCheckEntryPoint
4042import org.ole.planet.myplanet.di.WorkerDependenciesEntryPoint
4143import org.ole.planet.myplanet.model.RealmApkLog
4244import org.ole.planet.myplanet.service.AutoSyncWorker
@@ -51,6 +53,7 @@ import org.ole.planet.myplanet.utilities.NetworkUtils.isNetworkConnectedFlow
5153import org.ole.planet.myplanet.utilities.NetworkUtils.startListenNetworkState
5254import org.ole.planet.myplanet.utilities.NetworkUtils.stopListenNetworkState
5355import org.ole.planet.myplanet.utilities.ServerUrlMapper
56+ import org.ole.planet.myplanet.utilities.ServerUrlMapper
5457import org.ole.planet.myplanet.utilities.ThemeMode
5558import org.ole.planet.myplanet.utilities.VersionUtils.getVersionName
5659
@@ -126,39 +129,37 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
126129 }
127130 }
128131
132+ @WorkerThread
129133 suspend fun isServerReachable (urlString : String ): Boolean {
134+ if (urlString.isBlank()) return false
135+
130136 val serverUrlMapper = ServerUrlMapper ()
131137 val mapping = serverUrlMapper.processUrl(urlString)
132- val urlsToTry = mutableListOf (urlString )
138+ val urlsToTry = mutableListOf (mapping.primaryUrl )
133139 mapping.alternativeUrl?.let { urlsToTry.add(it) }
134140
135- return try {
136- if (urlString.isBlank()) return false
137-
138- val formattedUrl = if (! urlString.startsWith(" http://" ) && ! urlString.startsWith(" https://" )) {
139- " http://$urlString "
140- } else {
141- urlString
142- }
143-
144- val url = URL (formattedUrl)
145- val connection = withContext(Dispatchers .IO ) {
146- url.openConnection()
147- } as HttpURLConnection
148- connection.requestMethod = " GET"
149- connection.connectTimeout = 5000
150- connection.readTimeout = 5000
151- withContext(Dispatchers .IO ) {
152- connection.connect()
141+ val entryPoint = EntryPointAccessors .fromApplication(
142+ context,
143+ HealthCheckEntryPoint ::class .java
144+ )
145+ val healthCheckApi = entryPoint.healthCheckApi()
146+
147+ for (url in urlsToTry) {
148+ try {
149+ val formattedUrl = if (! url.startsWith(" http://" ) && ! url.startsWith(" https://" )) {
150+ " http://$url "
151+ } else {
152+ url
153+ }
154+ val response = withContext(Dispatchers .IO ) {
155+ healthCheckApi.checkServerHealth(formattedUrl)
156+ }
157+ if (response.isSuccessful) return true
158+ } catch (e: Exception ) {
159+ e.printStackTrace()
153160 }
154- val responseCode = connection.responseCode
155- connection.disconnect()
156- responseCode in 200 .. 299
157-
158- } catch (e: Exception ) {
159- e.printStackTrace()
160- false
161161 }
162+ return false
162163 }
163164
164165 fun handleUncaughtException (e : Throwable ) {
0 commit comments