Skip to content

Commit 545ca25

Browse files
committed
Merge pull request godotengine#103122 from syntaxerror247/fix-crash
Android: Fix excessive `getRotation` calls
2 parents b99a971 + ee4e809 commit 545ca25

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

platform/android/java/lib/src/org/godotengine/godot/Godot.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ class Godot(private val context: Context) {
664664
* Configuration change callback
665665
*/
666666
fun onConfigurationChanged(newConfig: Configuration) {
667+
renderView?.inputHandler?.onConfigurationChanged(newConfig)
668+
667669
val newDarkMode = newConfig.uiMode.and(Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
668670
if (darkMode != newDarkMode) {
669671
darkMode = newDarkMode

platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.godotengine.godot.GodotRenderView;
3838

3939
import android.content.Context;
40+
import android.content.res.Configuration;
4041
import android.hardware.Sensor;
4142
import android.hardware.SensorEvent;
4243
import android.hardware.SensorEventListener;
@@ -86,6 +87,8 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens
8687

8788
private int rotaryInputAxis = ROTARY_INPUT_VERTICAL_AXIS;
8889

90+
private int cachedRotation = -1;
91+
8992
public GodotInputHandler(Context context, Godot godot) {
9093
this.godot = godot;
9194
mInputManager = (InputManager)context.getSystemService(Context.INPUT_SERVICE);
@@ -741,10 +744,14 @@ public void onSensorChanged(SensorEvent event) {
741744
return;
742745
}
743746

747+
if (cachedRotation == -1) {
748+
updateCachedRotation();
749+
}
750+
744751
float rotatedValue0 = 0f;
745752
float rotatedValue1 = 0f;
746753
float rotatedValue2 = 0f;
747-
switch (windowManager.getDefaultDisplay().getRotation()) {
754+
switch (cachedRotation) {
748755
case Surface.ROTATION_0:
749756
rotatedValue0 = values[0];
750757
rotatedValue1 = values[1];
@@ -776,4 +783,12 @@ public void onSensorChanged(SensorEvent event) {
776783

777784
@Override
778785
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
786+
787+
private void updateCachedRotation() {
788+
cachedRotation = windowManager.getDefaultDisplay().getRotation();
789+
}
790+
791+
public void onConfigurationChanged(Configuration newConfig) {
792+
updateCachedRotation();
793+
}
779794
}

0 commit comments

Comments
 (0)