From fc926420e856ca8dba7c3d1e8577f75217c3fd66 Mon Sep 17 00:00:00 2001 From: alexban011 Date: Tue, 14 Jun 2022 20:34:30 +0300 Subject: [PATCH] added cutout support (#7044) * added support * now toggleable in settings * translation * added note that it requires restart * made it enabled by default * padded buttons to the right if there is a cutout * checking for cutout instead of android * reverted button changes * moved option to advancedTab --- .../jsons/translations/template.properties | 1 + android/src/com/unciv/app/AndroidLauncher.kt | 8 ++++++- .../app/PlatformSpecificHelpersAndroid.kt | 23 +++++++++++++++++++ .../com/unciv/models/metadata/GameSettings.kt | 2 ++ core/src/com/unciv/ui/options/AdvancedTab.kt | 3 +++ .../utils/GeneralPlatformSpecificHelpers.kt | 3 +++ 6 files changed, 39 insertions(+), 1 deletion(-) diff --git a/android/assets/jsons/translations/template.properties b/android/assets/jsons/translations/template.properties index 8506105374..09dc6b9a5d 100644 --- a/android/assets/jsons/translations/template.properties +++ b/android/assets/jsons/translations/template.properties @@ -689,6 +689,7 @@ Show pixel improvements = Enable Nuclear Weapons = Experimental Demographics scoreboard = Show zoom buttons in world screen = +Enable display cutout (requires restart) = Show tile yields = Show unit movement arrows = Continuous rendering = diff --git a/android/src/com/unciv/app/AndroidLauncher.kt b/android/src/com/unciv/app/AndroidLauncher.kt index 0b848a79e3..c2ff5feffa 100644 --- a/android/src/com/unciv/app/AndroidLauncher.kt +++ b/android/src/com/unciv/app/AndroidLauncher.kt @@ -2,7 +2,9 @@ package com.unciv.app import android.content.Intent import android.net.Uri +import android.os.Build import android.os.Bundle +import androidx.annotation.RequiresApi import androidx.core.app.NotificationManagerCompat import androidx.work.WorkManager import com.badlogic.gdx.backends.android.AndroidApplication @@ -33,10 +35,14 @@ open class AndroidLauncher : AndroidApplication() { val settings = GameSaver.getSettingsForPlatformLaunchers(filesDir.path) val fontFamily = settings.fontFamily - // Manage orientation lock + // Manage orientation lock and display cutout val platformSpecificHelper = PlatformSpecificHelpersAndroid(this) platformSpecificHelper.allowPortrait(settings.allowAndroidPortrait) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + platformSpecificHelper.toggleDisplayCutout(settings.androidCutout) + } + val androidParameters = UncivGameParameters( version = BuildConfig.VERSION_NAME, crashReportSysInfo = CrashReportSysInfoAndroid, diff --git a/android/src/com/unciv/app/PlatformSpecificHelpersAndroid.kt b/android/src/com/unciv/app/PlatformSpecificHelpersAndroid.kt index 4956f27432..a885366aeb 100644 --- a/android/src/com/unciv/app/PlatformSpecificHelpersAndroid.kt +++ b/android/src/com/unciv/app/PlatformSpecificHelpersAndroid.kt @@ -2,6 +2,10 @@ package com.unciv.app import android.app.Activity import android.content.pm.ActivityInfo +import android.os.Build +import android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER +import android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES +import androidx.annotation.RequiresApi import com.unciv.ui.utils.GeneralPlatformSpecificHelpers import kotlin.concurrent.thread @@ -31,6 +35,25 @@ Sources for Info about current orientation in case need: if (activity.requestedOrientation != orientation) activity.requestedOrientation = orientation } + override fun hasDisplayCutout(): Boolean { + val displayCutout = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + activity.windowManager.defaultDisplay.cutout + } else { + TODO("VERSION.SDK_INT < Q") + } + return displayCutout != null + } + + @RequiresApi(Build.VERSION_CODES.P) + override fun toggleDisplayCutout(androidCutout: Boolean) { + val layoutParams = activity.window.attributes + if (androidCutout) { + layoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES + } else { + layoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER + } + } + /** * On Android, local is some android-internal data directory which may or may not be accessible by the user. * External is probably on an SD-card or similar which is always accessible by the user. diff --git a/core/src/com/unciv/models/metadata/GameSettings.kt b/core/src/com/unciv/models/metadata/GameSettings.kt index d2e7180dbd..fb0ebbaa51 100644 --- a/core/src/com/unciv/models/metadata/GameSettings.kt +++ b/core/src/com/unciv/models/metadata/GameSettings.kt @@ -55,6 +55,8 @@ class GameSettings { var useDemographics: Boolean = false var showZoomButtons: Boolean = false + var androidCutout: Boolean = false + var multiplayer = GameSettingsMultiplayer() var showExperimentalWorldWrap = false // We're keeping this as a config due to ANR problems on Android phones for people who don't know what they're doing :/ diff --git a/core/src/com/unciv/ui/options/AdvancedTab.kt b/core/src/com/unciv/ui/options/AdvancedTab.kt index 80c4a39283..95fea89fe4 100644 --- a/core/src/com/unciv/ui/options/AdvancedTab.kt +++ b/core/src/com/unciv/ui/options/AdvancedTab.kt @@ -47,6 +47,9 @@ fun advancedTab( settings.showExperimentalWorldWrap = it } + if (UncivGame.Current.platformSpecificHelper?.hasDisplayCutout() == true) + optionsPopup.addCheckbox(this, "Enable display cutout (requires restart)", settings.androidCutout, false) { settings.androidCutout = it } + addMaxZoomSlider(this, settings) val helper = UncivGame.Current.platformSpecificHelper diff --git a/core/src/com/unciv/ui/utils/GeneralPlatformSpecificHelpers.kt b/core/src/com/unciv/ui/utils/GeneralPlatformSpecificHelpers.kt index 3cdc3d8249..1a015b5f64 100644 --- a/core/src/com/unciv/ui/utils/GeneralPlatformSpecificHelpers.kt +++ b/core/src/com/unciv/ui/utils/GeneralPlatformSpecificHelpers.kt @@ -16,6 +16,9 @@ interface GeneralPlatformSpecificHelpers { */ fun allowPortrait(allow: Boolean) {} + fun hasDisplayCutout(): Boolean { return false } + fun toggleDisplayCutout(androidCutout: Boolean) {} + /** * Notifies the user that it's their turn while the game is running */