Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adding sensor delay setting for android #22644

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ ProjectSettings::ProjectSettings() {
Compression::gzip_level = GLOBAL_DEF("compression/formats/gzip/compression_level", Z_DEFAULT_COMPRESSION);
custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");

// this is only for android but needs a place, setting sensor precision
GLOBAL_DEF("input_devices/sensors/sensor_delay", "Game");
set_custom_property_info("input_devices/sensors/sensor_delay", PropertyInfo(Variant::STRING, "input_devices/sensors/sensor_delay", PROPERTY_HINT_ENUM, "Fastest,Game,Normal,UI"));

using_datapack = false;
}

Expand Down
30 changes: 22 additions & 8 deletions platform/android/java/src/org/godotengine/godot/Godot.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
private boolean use_debug_opengl = false;
private boolean mStatePaused;
private int mState;
private int sensor_delay = SensorManager.SENSOR_DELAY_GAME;

static private Intent mCurrentIntent;

Expand Down Expand Up @@ -407,16 +408,28 @@ private void initializeGodot() {
GodotLib.io = io;
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME);

GodotLib.initialize(this, io.needsReloadHooks(), getAssets(), use_apk_expansion);

String sensor_delay_string = GodotLib.getGlobal("input_devices/sensors/sensor_delay");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call seems to be crashing android for me, I don't know if getGlobal is unavailable at this point of time, or whether our project file hasn't been loaded yet, or if there is some other reason... Anyone have any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea but I got several reports related to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty much in the dark how this whole thing ties together, it all feels pretty messy to me. So yeah I still don't know if I'm doing this too early, if there is a better place to call this from, or if there is something wrong with getting the setting.

it's a shame because this is the final bit to get simple VR going well on Android :)

Copy link
Contributor

@m4gr3d m4gr3d Apr 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call is crashing because GodotLib.getGlobal relies on ProjectSettings which is not initialized until GodotLib.setup has been invoked here.

I'm adding another comment below about how to possibly resolve the issue.

if (sensor_delay_string.equals("Fastest")) {
sensor_delay = SensorManager.SENSOR_DELAY_FASTEST;
} else if (sensor_delay_string.equals("Game")) {
sensor_delay = SensorManager.SENSOR_DELAY_GAME;
} else if (sensor_delay_string.equals("Normal")) {
sensor_delay = SensorManager.SENSOR_DELAY_NORMAL;
} else if (sensor_delay_string.equals("UI")) {
sensor_delay = SensorManager.SENSOR_DELAY_UI;
}

mSensorManager.registerListener(this, mAccelerometer, sensor_delay);
mSensorManager.registerListener(this, mGravity, sensor_delay);
mSensorManager.registerListener(this, mMagnetometer, sensor_delay);
mSensorManager.registerListener(this, mGyroscope, sensor_delay);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't look correct as the sensor manager is registering listeners here in 'onCreate', and it's also doing the same thing below in 'onResume'.
Based on the Android activity lifecycle, 'onResume' always follows 'onCreate' so the operation is just being repeated twice for no reasons.

I'd recommend deleting the section here in favor of registering the listener in onResume. This would also allow to resolve the crashing issue commented above, as by the time 'onResume' is invoked, GodotLib.setup would have already been invoked, preventing GodotLib.getGlobal from crashing.


result_callback = null;

mPaymentsManager = PaymentsManager.createManager(this).initService();
Expand Down Expand Up @@ -641,10 +654,11 @@ public void run() {
GodotLib.focusin();
}
});
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME);

mSensorManager.registerListener(this, mAccelerometer, sensor_delay);
mSensorManager.registerListener(this, mGravity, sensor_delay);
mSensorManager.registerListener(this, mMagnetometer, sensor_delay);
mSensorManager.registerListener(this, mGyroscope, sensor_delay);

if (use_immersive && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // check if the application runs on an android 4.4+
Window window = getWindow();
Expand Down