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 val fontFamily = settings.fontFamily
// Manage orientation lock // Manage orientation lock
val limitOrientationsHelper = LimitOrientationsHelperAndroid(this) val platformSpecificHelper = PlatformSpecificHelpersAndroid(this)
limitOrientationsHelper.allowPortrait(settings.allowAndroidPortrait) platformSpecificHelper.allowPortrait(settings.allowAndroidPortrait)
val androidParameters = UncivGameParameters( val androidParameters = UncivGameParameters(
version = BuildConfig.VERSION_NAME, version = BuildConfig.VERSION_NAME,
crashReportSysInfo = CrashReportSysInfoAndroid, crashReportSysInfo = CrashReportSysInfoAndroid,
fontImplementation = NativeFontAndroid(Fonts.ORIGINAL_FONT_SIZE.toInt(), fontFamily), fontImplementation = NativeFontAndroid(Fonts.ORIGINAL_FONT_SIZE.toInt(), fontFamily),
customSaveLocationHelper = customSaveLocationHelper, customSaveLocationHelper = customSaveLocationHelper,
limitOrientationsHelper = limitOrientationsHelper platformSpecificHelper = platformSpecificHelper
) )
game = UncivGame(androidParameters) game = UncivGame(androidParameters)

View File

@ -1,15 +1,19 @@
package com.unciv.app package com.unciv.app
import android.app.Activity import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo 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 * The Android implementation (currently the only one) effectively ends up doing
* [Activity.setRequestedOrientation] * [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: Sources for Info about current orientation in case need:
val windowManager = (activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager) 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 // Comparison ensures ActivityTaskManager.getService().setRequestedOrientation isn't called unless necessary
if (activity.requestedOrientation != orientation) activity.requestedOrientation = orientation 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
}
} }

View File

@ -36,7 +36,7 @@ class UncivGame(parameters: UncivGameParameters) : Game() {
val fontImplementation = parameters.fontImplementation val fontImplementation = parameters.fontImplementation
val consoleMode = parameters.consoleMode val consoleMode = parameters.consoleMode
val customSaveLocationHelper = parameters.customSaveLocationHelper val customSaveLocationHelper = parameters.customSaveLocationHelper
val limitOrientationsHelper = parameters.limitOrientationsHelper val platformSpecificHelper = parameters.platformSpecificHelper
private val audioExceptionHelper = parameters.audioExceptionHelper private val audioExceptionHelper = parameters.audioExceptionHelper
var deepLinkedMultiplayerGame: String? = null var deepLinkedMultiplayerGame: String? = null

View File

@ -3,7 +3,7 @@ package com.unciv
import com.unciv.logic.CustomSaveLocationHelper import com.unciv.logic.CustomSaveLocationHelper
import com.unciv.ui.crashhandling.CrashReportSysInfo import com.unciv.ui.crashhandling.CrashReportSysInfo
import com.unciv.ui.utils.AudioExceptionHelper import com.unciv.ui.utils.AudioExceptionHelper
import com.unciv.ui.utils.LimitOrientationsHelper import com.unciv.ui.utils.GeneralPlatformSpecificHelpers
import com.unciv.ui.utils.NativeFontImplementation import com.unciv.ui.utils.NativeFontImplementation
class UncivGameParameters(val version: String, class UncivGameParameters(val version: String,
@ -12,6 +12,6 @@ class UncivGameParameters(val version: String,
val fontImplementation: NativeFontImplementation? = null, val fontImplementation: NativeFontImplementation? = null,
val consoleMode: Boolean = false, val consoleMode: Boolean = false,
val customSaveLocationHelper: CustomSaveLocationHelper? = null, val customSaveLocationHelper: CustomSaveLocationHelper? = null,
val limitOrientationsHelper: LimitOrientationsHelper? = null, val platformSpecificHelper: GeneralPlatformSpecificHelpers? = null,
val audioExceptionHelper: AudioExceptionHelper? = null val audioExceptionHelper: AudioExceptionHelper? = null
) )

View File

@ -71,6 +71,13 @@ class NewGameScreen(
rightSideButton.setText("Start game!".tr()) rightSideButton.setText("Start game!".tr())
rightSideButton.onClick { rightSideButton.onClick {
if (gameSetupInfo.gameParameters.isOnlineMultiplayer) { if (gameSetupInfo.gameParameters.isOnlineMultiplayer) {
if (UncivGame.Current.platformSpecificHelper?.isInternetConnected() != true) {
val noInternetConnectionPopup = Popup(this)
noInternetConnectionPopup.addGoodSizedLabel("No internet connection!".tr()).row()
noInternetConnectionPopup.addCloseButton()
noInternetConnectionPopup.open()
return@onClick
}
for (player in gameSetupInfo.gameParameters.players.filter { it.playerType == PlayerType.Human }) { for (player in gameSetupInfo.gameParameters.players.filter { it.playerType == PlayerType.Human }) {
try { try {
UUID.fromString(IdChecker.checkAndReturnPlayerUuid(player.playerId)) UUID.fromString(IdChecker.checkAndReturnPlayerUuid(player.playerId))

View File

@ -2,15 +2,16 @@ package com.unciv.ui.utils
import com.unciv.models.metadata.GameSettings import com.unciv.models.metadata.GameSettings
/** Interface to support managing orientations /** Interface to support various platform-specific tools */
* interface GeneralPlatformSpecificHelpers {
* You can turn a mobile device on its side or upside down, and a mobile OS may or may not allow the
* position changes to automatically result in App orientation changes. This is about limiting that feature.
*/
interface LimitOrientationsHelper {
/** Pass a Boolean setting as used in [allowAndroidPortrait][GameSettings.allowAndroidPortrait] to the OS. /** Pass a Boolean setting as used in [allowAndroidPortrait][GameSettings.allowAndroidPortrait] to the OS.
*
* You can turn a mobile device on its side or upside down, and a mobile OS may or may not allow the
* position changes to automatically result in App orientation changes. This is about limiting that feature.
* @param allow `true`: allow all orientations (follows sensor as limited by OS settings) * @param allow `true`: allow all orientations (follows sensor as limited by OS settings)
* `false`: allow only landscape orientations (both if supported, otherwise default landscape only) * `false`: allow only landscape orientations (both if supported, otherwise default landscape only)
*/ */
fun allowPortrait(allow: Boolean) fun allowPortrait(allow: Boolean)
fun isInternetConnected(): Boolean
} }

View File

@ -108,7 +108,7 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
addCloseButton { addCloseButton {
previousScreen.game.musicController.onChange(null) previousScreen.game.musicController.onChange(null)
previousScreen.game.limitOrientationsHelper?.allowPortrait(settings.allowAndroidPortrait) previousScreen.game.platformSpecificHelper?.allowPortrait(settings.allowAndroidPortrait)
if (previousScreen is WorldScreen) if (previousScreen is WorldScreen)
previousScreen.enableNextTurnButtonAfterOptions() previousScreen.enableNextTurnButtonAfterOptions()
}.padBottom(10f) }.padBottom(10f)
@ -318,11 +318,11 @@ class OptionsPopup(val previousScreen: BaseScreen) : Popup(previousScreen) {
addMaxZoomSlider() addMaxZoomSlider()
if (previousScreen.game.limitOrientationsHelper != null) { if (previousScreen.game.platformSpecificHelper != null) {
addCheckbox("Enable portrait orientation", settings.allowAndroidPortrait) { addCheckbox("Enable portrait orientation", settings.allowAndroidPortrait) {
settings.allowAndroidPortrait = it settings.allowAndroidPortrait = it
// Note the following might close the options screen indirectly and delayed // Note the following might close the options screen indirectly and delayed
previousScreen.game.limitOrientationsHelper.allowPortrait(it) previousScreen.game.platformSpecificHelper.allowPortrait(it)
} }
} }

View File

@ -52,6 +52,7 @@ internal object DesktopLauncher {
fontImplementation = NativeFontDesktop(Fonts.ORIGINAL_FONT_SIZE.toInt(), settings.fontFamily), fontImplementation = NativeFontDesktop(Fonts.ORIGINAL_FONT_SIZE.toInt(), settings.fontFamily),
customSaveLocationHelper = CustomSaveLocationHelperDesktop(), customSaveLocationHelper = CustomSaveLocationHelperDesktop(),
crashReportSysInfo = CrashReportSysInfoDesktop(), crashReportSysInfo = CrashReportSysInfoDesktop(),
platformSpecificHelper = PlatformSpecificHelpersDesktop(),
audioExceptionHelper = HardenGdxAudio() audioExceptionHelper = HardenGdxAudio()
) )

View File

@ -0,0 +1,18 @@
package com.unciv.app.desktop
import com.unciv.ui.utils.GeneralPlatformSpecificHelpers
import java.net.InetAddress
class PlatformSpecificHelpersDesktop : GeneralPlatformSpecificHelpers {
override fun allowPortrait(allow: Boolean) {
// No need to do anything
}
override fun isInternetConnected(): Boolean {
return try {
InetAddress.getByName("8.8.8.8").isReachable(500) // Parameter timeout in milliseconds
} catch (ex: Throwable) {
false
}
}
}