Follow screen rotation even to Portrait on Android with Opt-in (#3936)

This commit is contained in:
SomeTroglodyte
2021-05-19 22:27:23 +02:00
committed by GitHub
parent 929c357663
commit 3e3bda42e5
13 changed files with 164 additions and 20 deletions

View File

@ -22,7 +22,6 @@
android:name="com.unciv.app.AndroidLauncher"
android:launchMode="singleTask"
android:label="@string/app_name"
android:screenOrientation="userLandscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
tools:ignore="LockedOrientationActivity">
<intent-filter>

View File

@ -1,6 +1,7 @@
package com.unciv.app
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import androidx.core.app.NotificationManagerCompat
@ -29,12 +30,19 @@ open class AndroidLauncher : AndroidApplication() {
if (externalfilesDir != null) GameSaver.externalFilesDirForAndroid = externalfilesDir.path
}
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true; }
// Manage orientation lock
val limitOrientationsHelper = LimitOrientationsHelperAndroid(this)
limitOrientationsHelper.limitOrientations(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
val config = AndroidApplicationConfiguration().apply {
useImmersiveMode = true;
}
val androidParameters = UncivGameParameters(
version = BuildConfig.VERSION_NAME,
crashReportSender = CrashReportSenderAndroid(this),
fontImplementation = NativeFontAndroid(Fonts.ORIGINAL_FONT_SIZE.toInt()),
customSaveLocationHelper = customSaveLocationHelper
customSaveLocationHelper = customSaveLocationHelper,
limitOrientationsHelper = limitOrientationsHelper
)
val game = UncivGame(androidParameters)
initialize(game, config)
@ -89,4 +97,4 @@ open class AndroidLauncher : AndroidApplication() {
}
}
class AndroidTvLauncher:AndroidLauncher()
class AndroidTvLauncher:AndroidLauncher()

View File

@ -0,0 +1,65 @@
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].
*
* The Android implementation (currently the only one) effectively ends up doing
* [Activity.setRequestedOrientation]
*/
class LimitOrientationsHelperAndroid(private val activity: Activity) : LimitOrientationsHelper {
/*
companion object {
// from android.content.res.Configuration.java
// applicable to activity.resources.configuration
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
}
// 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 (ex: java.lang.Exception) {
GameSettingsPreview()
}
allowPortrait(setting.allowAndroidPortrait)
} else {
// Currently unused
if (activity.requestedOrientation != newOrientation) activity.requestedOrientation = newOrientation
}
}
}