From ee4e809d4c0c17918a9a5bd24067040be653718b Mon Sep 17 00:00:00 2001 From: Anish Mishra Date: Fri, 21 Feb 2025 15:52:39 +0530 Subject: [PATCH] Android: Fix excessive getRotation calls --- .../java/lib/src/org/godotengine/godot/Godot.kt | 2 ++ .../godot/input/GodotInputHandler.java | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt index 32e5ad3fa76..2e043745c43 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt @@ -664,6 +664,8 @@ class Godot(private val context: Context) { * Configuration change callback */ fun onConfigurationChanged(newConfig: Configuration) { + renderView?.inputHandler?.onConfigurationChanged(newConfig) + val newDarkMode = newConfig.uiMode.and(Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES if (darkMode != newDarkMode) { darkMode = newDarkMode diff --git a/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java b/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java index 36bee6ef224..6f036c15a32 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java +++ b/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java @@ -37,6 +37,7 @@ import org.godotengine.godot.GodotLib; import org.godotengine.godot.GodotRenderView; import android.content.Context; +import android.content.res.Configuration; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; @@ -86,6 +87,8 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens private int rotaryInputAxis = ROTARY_INPUT_VERTICAL_AXIS; + private int cachedRotation = -1; + public GodotInputHandler(Context context, Godot godot) { this.godot = godot; mInputManager = (InputManager)context.getSystemService(Context.INPUT_SERVICE); @@ -741,10 +744,14 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens return; } + if (cachedRotation == -1) { + updateCachedRotation(); + } + float rotatedValue0 = 0f; float rotatedValue1 = 0f; float rotatedValue2 = 0f; - switch (windowManager.getDefaultDisplay().getRotation()) { + switch (cachedRotation) { case Surface.ROTATION_0: rotatedValue0 = values[0]; rotatedValue1 = values[1]; @@ -776,4 +783,12 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} + + private void updateCachedRotation() { + cachedRotation = windowManager.getDefaultDisplay().getRotation(); + } + + public void onConfigurationChanged(Configuration newConfig) { + updateCachedRotation(); + } }