Remove re-orientation for OptionsPopup (#6466)

This commit is contained in:
SomeTroglodyte
2022-03-31 22:03:57 +02:00
committed by GitHub
parent 6315a16d98
commit 90a172ab02
4 changed files with 26 additions and 91 deletions

View File

@ -1,7 +1,6 @@
package com.unciv.app
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Bundle
import androidx.core.app.NotificationManagerCompat
@ -25,18 +24,19 @@ open class AndroidLauncher : AndroidApplication() {
MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
copyMods()
val externalfilesDir = getExternalFilesDir(null)
if (externalfilesDir != null) GameSaver.externalFilesDirForAndroid = externalfilesDir.path
val externalFilesDir = getExternalFilesDir(null)
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
val limitOrientationsHelper = LimitOrientationsHelperAndroid(this)
limitOrientationsHelper.limitOrientations(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
val config = AndroidApplicationConfiguration().apply {
useImmersiveMode = true;
}
val fontFamily = GameSettings.getSettingsForPlatformLaunchers(filesDir.path).fontFamily
limitOrientationsHelper.allowPortrait(settings.allowAndroidPortrait)
val androidParameters = UncivGameParameters(
version = BuildConfig.VERSION_NAME,
@ -49,14 +49,7 @@ open class AndroidLauncher : AndroidApplication() {
game = UncivGame(androidParameters)
initialize(game, config)
// This is also needed in onCreate to open links and notifications
// 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
}
setDeepLinkedGame(intent)
}
/**
@ -97,7 +90,7 @@ open class AndroidLauncher : AndroidApplication() {
}
if (deepLinkedMultiplayerGame != null) {
game?.deepLinkedMultiplayerGame = deepLinkedMultiplayerGame;
game?.deepLinkedMultiplayerGame = deepLinkedMultiplayerGame
deepLinkedMultiplayerGame = null
}
@ -109,11 +102,15 @@ open class AndroidLauncher : AndroidApplication() {
if (intent == null)
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
deepLinkedMultiplayerGame = uri?.getQueryParameter("id")
} else {
deepLinkedMultiplayerGame = null
uri?.getQueryParameter("id")
}
}

View File

@ -2,11 +2,7 @@ package com.unciv.app
import android.app.Activity
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 java.io.File
/** See also interface [LimitOrientationsHelper].
*
@ -15,51 +11,21 @@ import java.io.File
*/
class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrientationsHelper {
/*
companion object {
// from android.content.res.Configuration.java
// applicable to activity.resources.configuration
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
const val ORIENTATION_UNDEFINED = 0
const val ORIENTATION_PORTRAIT = 1
const val ORIENTATION_LANDSCAPE = 2
}
*/
private class GameSettingsPreview(var allowAndroidPortrait: Boolean = false)
override fun allowPortrait(allow: Boolean) {
val orientation = when {
allow -> ActivityInfo.SCREEN_ORIENTATION_USER
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
else -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
else -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
}
// Comparison ensures ActivityTaskManager.getService().setRequestedOrientation isn't called unless necessary
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
}
}
}

View File

@ -10,12 +10,10 @@ import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.utils.Drawable
import com.badlogic.gdx.utils.viewport.ExtendViewport
import com.unciv.MainMenuScreen
import com.unciv.CrashHandlingStage
import com.unciv.UncivGame
import com.unciv.models.Tutorial
import com.unciv.ui.tutorials.TutorialController
import com.unciv.ui.worldscreen.WorldScreen
import com.unciv.ui.worldscreen.mainmenu.OptionsPopup
abstract class BaseScreen : Screen {
@ -125,26 +123,6 @@ abstract class BaseScreen : Screen {
fun isNarrowerThan4to3() = stage.viewport.screenHeight * 4 > stage.viewport.screenWidth * 3
fun openOptionsPopup() {
val limitOrientationsHelper = game.limitOrientationsHelper
if (limitOrientationsHelper == null || !game.settings.allowAndroidPortrait || !isCrampedPortrait()) {
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
}
}
}
}

View File

@ -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.
*/
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.
* @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)