check for internet before starting a mp game to avoid freeze (#6679)

* Fixed issue #6649 by checking for internet connection before starting the game if "online multiplayer" is selected
Many thanks to SomeTroglodyte for most of the code and for explanations

* check for internet before starting a mp game to avoid freeze

Fixed issue #6649 by checking for internet connection before starting the game if "online multiplayer" is selected.

Many thanks to SomeTroglodyte for most of the code and for explanations. A slight modification has been done to the code to catch an error caused by InetAddress

* Revert "Merge remote-tracking branch 'origin/master-noInternet' into master-noInternet"

This reverts commit bd6474b50d, reversing
changes made to f52ad60b51.
This commit is contained in:
alexban011
2022-05-03 00:41:08 +03:00
committed by GitHub
parent 031cb4fc14
commit 898b1ca056
10 changed files with 67 additions and 25 deletions

View File

@ -35,15 +35,15 @@ open class AndroidLauncher : AndroidApplication() {
val fontFamily = settings.fontFamily
// Manage orientation lock
val limitOrientationsHelper = LimitOrientationsHelperAndroid(this)
limitOrientationsHelper.allowPortrait(settings.allowAndroidPortrait)
val platformSpecificHelper = PlatformSpecificHelpersAndroid(this)
platformSpecificHelper.allowPortrait(settings.allowAndroidPortrait)
val androidParameters = UncivGameParameters(
version = BuildConfig.VERSION_NAME,
crashReportSysInfo = CrashReportSysInfoAndroid,
fontImplementation = NativeFontAndroid(Fonts.ORIGINAL_FONT_SIZE.toInt(), fontFamily),
customSaveLocationHelper = customSaveLocationHelper,
limitOrientationsHelper = limitOrientationsHelper
platformSpecificHelper = platformSpecificHelper
)
game = UncivGame(androidParameters)

View File

@ -1,15 +1,19 @@
package com.unciv.app
import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import com.unciv.ui.utils.LimitOrientationsHelper
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import com.unciv.ui.utils.GeneralPlatformSpecificHelpers
/** See also interface [LimitOrientationsHelper].
/** See also interface [GeneralPlatformSpecificHelpers].
*
* The Android implementation (currently the only one) effectively ends up doing
* [Activity.setRequestedOrientation]
*/
class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrientationsHelper {
class PlatformSpecificHelpersAndroid(private val activity: Activity) : GeneralPlatformSpecificHelpers {
/*
Sources for Info about current orientation in case need:
val windowManager = (activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager)
@ -28,4 +32,15 @@ Sources for Info about current orientation in case need:
// Comparison ensures ActivityTaskManager.getService().setRequestedOrientation isn't called unless necessary
if (activity.requestedOrientation != orientation) activity.requestedOrientation = orientation
}
override fun isInternetConnected(): Boolean {
val connectivityManager = activity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
for (network in connectivityManager.allNetworks) {
val networkCapabilities = connectivityManager.getNetworkCapabilities(network) ?: continue
val isInternet = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
val info = connectivityManager.getNetworkInfo(network) ?: continue
if (isInternet && info.isConnected) return true
}
return false
}
}