mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-15 02:09:21 +07:00
Remove re-orientation for OptionsPopup (#6466)
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
package com.unciv.app
|
package com.unciv.app
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ActivityInfo
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
@ -25,18 +24,19 @@ open class AndroidLauncher : AndroidApplication() {
|
|||||||
MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
|
MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
|
||||||
|
|
||||||
copyMods()
|
copyMods()
|
||||||
val externalfilesDir = getExternalFilesDir(null)
|
val externalFilesDir = getExternalFilesDir(null)
|
||||||
if (externalfilesDir != null) GameSaver.externalFilesDirForAndroid = externalfilesDir.path
|
if (externalFilesDir != null) GameSaver.externalFilesDirForAndroid = externalFilesDir.path
|
||||||
|
|
||||||
|
val config = AndroidApplicationConfiguration().apply {
|
||||||
|
useImmersiveMode = true
|
||||||
|
}
|
||||||
|
|
||||||
|
val settings = GameSettings.getSettingsForPlatformLaunchers(filesDir.path)
|
||||||
|
val fontFamily = settings.fontFamily
|
||||||
|
|
||||||
// Manage orientation lock
|
// Manage orientation lock
|
||||||
val limitOrientationsHelper = LimitOrientationsHelperAndroid(this)
|
val limitOrientationsHelper = LimitOrientationsHelperAndroid(this)
|
||||||
limitOrientationsHelper.limitOrientations(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
|
limitOrientationsHelper.allowPortrait(settings.allowAndroidPortrait)
|
||||||
|
|
||||||
val config = AndroidApplicationConfiguration().apply {
|
|
||||||
useImmersiveMode = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
val fontFamily = GameSettings.getSettingsForPlatformLaunchers(filesDir.path).fontFamily
|
|
||||||
|
|
||||||
val androidParameters = UncivGameParameters(
|
val androidParameters = UncivGameParameters(
|
||||||
version = BuildConfig.VERSION_NAME,
|
version = BuildConfig.VERSION_NAME,
|
||||||
@ -49,14 +49,7 @@ open class AndroidLauncher : AndroidApplication() {
|
|||||||
game = UncivGame(androidParameters)
|
game = UncivGame(androidParameters)
|
||||||
initialize(game, config)
|
initialize(game, config)
|
||||||
|
|
||||||
// This is also needed in onCreate to open links and notifications
|
setDeepLinkedGame(intent)
|
||||||
// correctly even if the app was not running
|
|
||||||
if (intent.action == Intent.ACTION_VIEW) {
|
|
||||||
val uri: Uri? = intent.data
|
|
||||||
deepLinkedMultiplayerGame = uri?.getQueryParameter("id")
|
|
||||||
} else {
|
|
||||||
deepLinkedMultiplayerGame = null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,7 +90,7 @@ open class AndroidLauncher : AndroidApplication() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (deepLinkedMultiplayerGame != null) {
|
if (deepLinkedMultiplayerGame != null) {
|
||||||
game?.deepLinkedMultiplayerGame = deepLinkedMultiplayerGame;
|
game?.deepLinkedMultiplayerGame = deepLinkedMultiplayerGame
|
||||||
deepLinkedMultiplayerGame = null
|
deepLinkedMultiplayerGame = null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,11 +102,15 @@ open class AndroidLauncher : AndroidApplication() {
|
|||||||
if (intent == null)
|
if (intent == null)
|
||||||
return
|
return
|
||||||
|
|
||||||
if (intent.action == Intent.ACTION_VIEW) {
|
setDeepLinkedGame(intent)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setDeepLinkedGame(intent: Intent) {
|
||||||
|
// This is needed in onCreate _and_ onNewIntent to open links and notifications
|
||||||
|
// correctly even if the app was not running
|
||||||
|
deepLinkedMultiplayerGame = if (intent.action != Intent.ACTION_VIEW) null else {
|
||||||
val uri: Uri? = intent.data
|
val uri: Uri? = intent.data
|
||||||
deepLinkedMultiplayerGame = uri?.getQueryParameter("id")
|
uri?.getQueryParameter("id")
|
||||||
} else {
|
|
||||||
deepLinkedMultiplayerGame = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,7 @@ package com.unciv.app
|
|||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.pm.ActivityInfo
|
import android.content.pm.ActivityInfo
|
||||||
import android.os.Build
|
|
||||||
import com.badlogic.gdx.files.FileHandle
|
|
||||||
import com.unciv.logic.GameSaver
|
|
||||||
import com.unciv.ui.utils.LimitOrientationsHelper
|
import com.unciv.ui.utils.LimitOrientationsHelper
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
/** See also interface [LimitOrientationsHelper].
|
/** See also interface [LimitOrientationsHelper].
|
||||||
*
|
*
|
||||||
@ -15,51 +11,21 @@ import java.io.File
|
|||||||
*/
|
*/
|
||||||
class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrientationsHelper {
|
class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrientationsHelper {
|
||||||
/*
|
/*
|
||||||
companion object {
|
Sources for Info about current orientation in case need:
|
||||||
// from android.content.res.Configuration.java
|
val windowManager = (activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager)
|
||||||
// applicable to activity.resources.configuration
|
val displayRotation = windowManager.defaultDisplay.rotation
|
||||||
|
val currentOrientation = activity.resources.configuration.orientation
|
||||||
const val ORIENTATION_UNDEFINED = 0
|
const val ORIENTATION_UNDEFINED = 0
|
||||||
const val ORIENTATION_PORTRAIT = 1
|
const val ORIENTATION_PORTRAIT = 1
|
||||||
const val ORIENTATION_LANDSCAPE = 2
|
const val ORIENTATION_LANDSCAPE = 2
|
||||||
}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private class GameSettingsPreview(var allowAndroidPortrait: Boolean = false)
|
|
||||||
|
|
||||||
override fun allowPortrait(allow: Boolean) {
|
override fun allowPortrait(allow: Boolean) {
|
||||||
val orientation = when {
|
val orientation = when {
|
||||||
allow -> ActivityInfo.SCREEN_ORIENTATION_USER
|
allow -> ActivityInfo.SCREEN_ORIENTATION_USER
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
|
else -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
|
||||||
else -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
|
||||||
}
|
}
|
||||||
// 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 limitOrientations(newOrientation: Int) {
|
|
||||||
// Sources for Info about current orientation in case need:
|
|
||||||
// val windowManager = (activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager)
|
|
||||||
// val displayRotation = windowManager.defaultDisplay.rotation
|
|
||||||
// val currentOrientation = activity.resources.configuration.orientation
|
|
||||||
if (newOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
|
|
||||||
// Currently only the AndroidLauncher onCreate calls this with 'unspecified'.
|
|
||||||
// determine whether to allow portrait from our settings file...
|
|
||||||
// Gdx.files at this point is null, UncivGame.Current worse, so we'll do it classically.
|
|
||||||
// Gdx parts used that *do* work: FileHandle (constructor, exists, reader) and Json
|
|
||||||
val settingsPath = activity.applicationContext.filesDir.absolutePath + File.separator + GameSaver.settingsFileName
|
|
||||||
val settingsFile = FileHandle(settingsPath)
|
|
||||||
val setting =
|
|
||||||
if (!settingsFile.exists()) {
|
|
||||||
GameSettingsPreview()
|
|
||||||
} else try {
|
|
||||||
GameSaver.json().fromJson(GameSettingsPreview::class.java, settingsFile.reader())
|
|
||||||
} catch (throwable: Throwable) {
|
|
||||||
GameSettingsPreview()
|
|
||||||
}
|
|
||||||
allowPortrait(setting.allowAndroidPortrait)
|
|
||||||
} else {
|
|
||||||
// Currently unused
|
|
||||||
if (activity.requestedOrientation != newOrientation) activity.requestedOrientation = newOrientation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,10 @@ import com.badlogic.gdx.scenes.scene2d.Stage
|
|||||||
import com.badlogic.gdx.scenes.scene2d.ui.*
|
import com.badlogic.gdx.scenes.scene2d.ui.*
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.Drawable
|
import com.badlogic.gdx.scenes.scene2d.utils.Drawable
|
||||||
import com.badlogic.gdx.utils.viewport.ExtendViewport
|
import com.badlogic.gdx.utils.viewport.ExtendViewport
|
||||||
import com.unciv.MainMenuScreen
|
|
||||||
import com.unciv.CrashHandlingStage
|
import com.unciv.CrashHandlingStage
|
||||||
import com.unciv.UncivGame
|
import com.unciv.UncivGame
|
||||||
import com.unciv.models.Tutorial
|
import com.unciv.models.Tutorial
|
||||||
import com.unciv.ui.tutorials.TutorialController
|
import com.unciv.ui.tutorials.TutorialController
|
||||||
import com.unciv.ui.worldscreen.WorldScreen
|
|
||||||
import com.unciv.ui.worldscreen.mainmenu.OptionsPopup
|
import com.unciv.ui.worldscreen.mainmenu.OptionsPopup
|
||||||
|
|
||||||
abstract class BaseScreen : Screen {
|
abstract class BaseScreen : Screen {
|
||||||
@ -125,26 +123,6 @@ abstract class BaseScreen : Screen {
|
|||||||
fun isNarrowerThan4to3() = stage.viewport.screenHeight * 4 > stage.viewport.screenWidth * 3
|
fun isNarrowerThan4to3() = stage.viewport.screenHeight * 4 > stage.viewport.screenWidth * 3
|
||||||
|
|
||||||
fun openOptionsPopup() {
|
fun openOptionsPopup() {
|
||||||
val limitOrientationsHelper = game.limitOrientationsHelper
|
|
||||||
if (limitOrientationsHelper == null || !game.settings.allowAndroidPortrait || !isCrampedPortrait()) {
|
|
||||||
OptionsPopup(this).open(force = true)
|
OptionsPopup(this).open(force = true)
|
||||||
return
|
|
||||||
}
|
|
||||||
if (!(this is MainMenuScreen || this is WorldScreen)) {
|
|
||||||
throw IllegalArgumentException("openOptionsPopup called on wrong derivative class")
|
|
||||||
}
|
|
||||||
limitOrientationsHelper.allowPortrait(false)
|
|
||||||
crashHandlingThread(name="WaitForRotation") {
|
|
||||||
var waited = 0
|
|
||||||
while (true) {
|
|
||||||
val newScreen = (UncivGame.Current.screen as? BaseScreen)
|
|
||||||
if (waited >= 10000 || newScreen!=null && !newScreen.isPortrait() ) {
|
|
||||||
postCrashHandlingRunnable { OptionsPopup(newScreen ?: this).open(true) }
|
|
||||||
break
|
|
||||||
}
|
|
||||||
Thread.sleep(200)
|
|
||||||
waited += 200
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,6 @@ import com.unciv.models.metadata.GameSettings
|
|||||||
* position changes to automatically result in App orientation changes. This is about limiting that feature.
|
* position changes to automatically result in App orientation changes. This is about limiting that feature.
|
||||||
*/
|
*/
|
||||||
interface LimitOrientationsHelper {
|
interface LimitOrientationsHelper {
|
||||||
/** Set a specific requested orientation or pull the setting from disk and act accordingly
|
|
||||||
* @param newOrientation A SCREEN_ORIENTATION_* value from [ActivityInfo]
|
|
||||||
* or SCREEN_ORIENTATION_UNSPECIFIED to load the setting
|
|
||||||
*/
|
|
||||||
fun limitOrientations(newOrientation: Int)
|
|
||||||
|
|
||||||
/** 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.
|
||||||
* @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)
|
||||||
|
Reference in New Issue
Block a user