expose battery on hmd xdev

This commit is contained in:
galister
2024-08-10 22:54:41 +02:00
committed by xytovl
parent 2a50237142
commit 225bf9d30f
7 changed files with 146 additions and 1 deletions
+53
View File
@@ -23,6 +23,10 @@
#include <spdlog/spdlog.h>
#include <thread>
#ifdef __ANDROID__
#include "jnipp.h"
#endif
static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time)
{
XrSpaceVelocity velocity{
@@ -138,6 +142,25 @@ void scenes::stream::tracking()
#ifdef __ANDROID__
// Runtime may use JNI and needs the thread to be attached
application::instance().setup_jni();
jni::object<""> act(application::native_app()->activity->clazz);
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
jni::string filter_jstr("android.intent.action.BATTERY_CHANGED");
jni::string level_jstr("level");
jni::string scale_jstr("scale");
jni::string plugged_jstr("plugged");
jni::Int default_jint(-1);
auto receiver_jobj = jni::object<"android/content/BroadcastReceiver">(NULL);
auto filter_jobj = jni::new_object<"android/content/IntentFilter">(filter_jstr);
auto register_receiver = jni::klass("android/content/Context")
.method<jni::object<"android/content/Intent">>("registerReceiver", receiver_jobj, filter_jobj);
auto get_int_extra = jni::klass("android/content/Intent").method<jni::Int>("getIntExtra", level_jstr, default_jint);
XrTime next_battery_check = 0;
const XrDuration battery_check_interval = 5'000'000'000; // 5s
#endif
std::vector<std::pair<device_id, XrSpace>> spaces = {
{device_id::HEAD, application::view()},
@@ -223,6 +246,36 @@ void scenes::stream::tracking()
XrDuration busy_time = t.count();
// Target: polling between 1 and 5ms, with 20% busy time
tracking_period = std::clamp<XrDuration>(std::lerp(tracking_period, busy_time * 5, 0.2), 1'000'000, 5'000'000);
#ifdef __ANDROID__
if (next_battery_check < now)
{
timer t2(instance);
from_headset::battery battery{};
auto intent = ctx.call<jni::object<"android/content/Intent">>(register_receiver, receiver_jobj, filter_jobj);
if (intent)
{
auto level_jint = intent.call<jni::Int>(get_int_extra, level_jstr, default_jint);
auto scale_jint = intent.call<jni::Int>(get_int_extra, scale_jstr, default_jint);
if (level_jint && level_jint.value >= 0 && scale_jint && scale_jint.value >= 0)
{
battery.present = true;
battery.charge = (float)(level_jint.value) / (float)(scale_jint.value);
}
auto plugged_jint = intent.call<jni::Int>(get_int_extra, plugged_jstr, default_jint);
battery.charging = plugged_jint && plugged_jint.value > 0;
}
network_session->send_stream(battery);
next_battery_check = now + battery_check_interval;
XrDuration battery_dur = t2.count();
spdlog::info("Battery check took: {}", battery_dur);
}
#endif
}
catch (const std::system_error & e)
{