Set feature to true when the user grants the corresponding permission
This commit is contained in:
@@ -22,9 +22,14 @@
|
|||||||
#include "application.h"
|
#include "application.h"
|
||||||
#include "jnipp.h"
|
#include "jnipp.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
|
static std::mutex permission_callbacks_mutex;
|
||||||
|
static std::map<int, std::pair<std::string, std::function<void(bool)>>> permission_callbacks;
|
||||||
|
static int next_permission_request_id;
|
||||||
|
|
||||||
static bool check_permission(jni::object<jni::details::string_literal<24>{"android/content/Context"}> & ctx, jni::string & permission)
|
static bool check_permission(jni::object<jni::details::string_literal<24>{"android/content/Context"}> & ctx, jni::string & permission)
|
||||||
{
|
{
|
||||||
auto result = ctx.call<jni::Int>("checkSelfPermission", permission);
|
auto result = ctx.call<jni::Int>("checkSelfPermission", permission);
|
||||||
@@ -54,10 +59,14 @@ bool check_permission(const char * permission)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void request_permission(const char * permission, int requestCode)
|
void request_permission(const char * permission, std::function<void(bool)> callback)
|
||||||
{
|
{
|
||||||
if (not permission)
|
if (not permission)
|
||||||
|
{
|
||||||
|
callback(true);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
jni::object<""> act(application::native_app()->activity->clazz);
|
jni::object<""> act(application::native_app()->activity->clazz);
|
||||||
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
|
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
|
||||||
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
|
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
|
||||||
@@ -66,30 +75,69 @@ void request_permission(const char * permission, int requestCode)
|
|||||||
if (check_permission(ctx, jpermission))
|
if (check_permission(ctx, jpermission))
|
||||||
{
|
{
|
||||||
spdlog::info("{} permission already granted", permission);
|
spdlog::info("{} permission already granted", permission);
|
||||||
|
callback(true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
spdlog::info("{} permission not granted, requesting it", permission);
|
spdlog::info("{} permission not granted, requesting it", permission);
|
||||||
jni::array permissions(jpermission);
|
jni::array permissions(jpermission);
|
||||||
act.call<void>("requestPermissions", permissions, jni::Int(requestCode));
|
|
||||||
|
int request_code;
|
||||||
|
{
|
||||||
|
std::lock_guard lock(permission_callbacks_mutex);
|
||||||
|
request_code = ++next_permission_request_id;
|
||||||
|
permission_callbacks.emplace(request_code, std::make_pair(permission, callback));
|
||||||
|
}
|
||||||
|
|
||||||
|
act.call<void>("requestPermissions", permissions, jni::Int(request_code));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_MainActivity_onRequestPermissionsResult(
|
extern "C" __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_MainActivity_onRequestPermissionsResult(
|
||||||
JNIEnv * env,
|
JNIEnv * env,
|
||||||
jobject instance,
|
jobject instance,
|
||||||
int requestCode);
|
int requestCode,
|
||||||
|
jobjectArray permissions,
|
||||||
// extern "C" JNIEXPORT void JNICALL
|
jintArray grantResults);
|
||||||
// Java_org_meumeu_wivrn_MainActivity_onRequestPermissionsResult(
|
|
||||||
|
|
||||||
void Java_org_meumeu_wivrn_MainActivity_onRequestPermissionsResult(
|
void Java_org_meumeu_wivrn_MainActivity_onRequestPermissionsResult(
|
||||||
JNIEnv * env,
|
JNIEnv * env,
|
||||||
jobject instance,
|
jobject instance,
|
||||||
int requestCode
|
int request_code,
|
||||||
/*String [] permissions, int[] grantResults*/)
|
jobjectArray permissions,
|
||||||
|
jintArray grant_results)
|
||||||
{
|
{
|
||||||
spdlog::info("Java_org_meumeu_wivrn_MainActivity_onRequestPermissionsResult {}", requestCode);
|
std::pair<std::string, std::function<void(bool)>> callback;
|
||||||
// GetAppEngine()->OnCameraParameterChanged(ACAMERA_SENSOR_EXPOSURE_TIME,
|
{
|
||||||
// exposurePercent);
|
std::lock_guard lock(permission_callbacks_mutex);
|
||||||
|
auto it = permission_callbacks.find(request_code);
|
||||||
|
if (it == permission_callbacks.end())
|
||||||
|
{
|
||||||
|
spdlog::info("Ignoring unexpected request code");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback = it->second;
|
||||||
|
permission_callbacks.erase(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nb_permissions = std::min(env->GetArrayLength(permissions), env->GetArrayLength(grant_results));
|
||||||
|
|
||||||
|
jint * results = env->GetIntArrayElements(grant_results, nullptr);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < nb_permissions; ++i)
|
||||||
|
{
|
||||||
|
jstring string = (jstring)(env->GetObjectArrayElement(permissions, i));
|
||||||
|
const char * permission = env->GetStringUTFChars(string, nullptr);
|
||||||
|
spdlog::info("Permission {} {}", permission, results[i] ? "denied" : "granted");
|
||||||
|
|
||||||
|
if (permission == callback.first)
|
||||||
|
{
|
||||||
|
callback.second(results[i] == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
env->ReleaseStringUTFChars(string, permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
env->ReleaseIntArrayElements(grant_results, results, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
// accepts nullptr (returns true)
|
// accepts nullptr (returns true)
|
||||||
bool check_permission(const char * permission);
|
bool check_permission(const char * permission);
|
||||||
|
|
||||||
void request_permission(const char * permission, int requestCode);
|
void request_permission(const char * permission, std::function<void(bool)> callback);
|
||||||
|
|||||||
+84
-65
@@ -81,25 +81,29 @@ static std::string json_string(const std::string & in)
|
|||||||
|
|
||||||
bool configuration::check_feature(feature f) const
|
bool configuration::check_feature(feature f) const
|
||||||
{
|
{
|
||||||
auto it = features.find(f);
|
|
||||||
if (it == features.end())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Skip permission checks if not requested
|
|
||||||
if (not it->second)
|
|
||||||
return false;
|
|
||||||
switch (f)
|
|
||||||
{
|
{
|
||||||
case feature::microphone:
|
std::lock_guard lock(mutex);
|
||||||
break;
|
|
||||||
case feature::eye_gaze:
|
auto it = features.find(f);
|
||||||
if (not application::get_eye_gaze_supported())
|
if (it == features.end())
|
||||||
return false;
|
return false;
|
||||||
break;
|
|
||||||
case feature::face_tracking:
|
// Skip permission checks if not requested
|
||||||
if (not application::get_fb_face_tracking2_supported())
|
if (not it->second)
|
||||||
return false;
|
return false;
|
||||||
break;
|
switch (f)
|
||||||
|
{
|
||||||
|
case feature::microphone:
|
||||||
|
break;
|
||||||
|
case feature::eye_gaze:
|
||||||
|
if (not application::get_eye_gaze_supported())
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case feature::face_tracking:
|
||||||
|
if (not application::get_fb_face_tracking2_supported())
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
return check_permission(permission_name(f));
|
return check_permission(permission_name(f));
|
||||||
@@ -112,9 +116,23 @@ void configuration::set_feature(feature f, bool state)
|
|||||||
{
|
{
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
if (state)
|
if (state)
|
||||||
request_permission(permission_name(f), 0);
|
{
|
||||||
|
request_permission(permission_name(f), [this, f](bool granted) {
|
||||||
|
{
|
||||||
|
std::lock_guard lock(mutex);
|
||||||
|
features[f] = granted;
|
||||||
|
}
|
||||||
|
save();
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
features[f] = state;
|
{
|
||||||
|
std::lock_guard lock(mutex);
|
||||||
|
features[f] = state;
|
||||||
|
}
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration::configuration(xr::system & system)
|
configuration::configuration(xr::system & system)
|
||||||
@@ -122,58 +140,57 @@ configuration::configuration(xr::system & system)
|
|||||||
passthrough_enabled = system.passthrough_supported() == xr::system::passthrough_type::color;
|
passthrough_enabled = system.passthrough_supported() == xr::system::passthrough_type::color;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
*this = configuration(application::get_config_path() / "client.json");
|
simdjson::dom::parser parser;
|
||||||
|
simdjson::dom::element root = parser.load(application::get_config_path() / "client.json");
|
||||||
|
for (simdjson::dom::object i: simdjson::dom::array(root["servers"]))
|
||||||
|
{
|
||||||
|
server_data data{
|
||||||
|
.autoconnect = i["autoconnect"].get_bool(),
|
||||||
|
.manual = i["manual"].get_bool(),
|
||||||
|
.visible = false,
|
||||||
|
.compatible = true,
|
||||||
|
.service = {
|
||||||
|
.name = (std::string)i["pretty_name"],
|
||||||
|
.hostname = (std::string)i["hostname"],
|
||||||
|
.port = (int)i["port"].get_int64(),
|
||||||
|
.tcp_only = i["tcp_only"].is_bool() && i["tcp_only"].get_bool(),
|
||||||
|
.txt = {{"cookie", (std::string)i["cookie"]}}
|
||||||
|
|
||||||
|
}};
|
||||||
|
servers.emplace(data.service.txt["cookie"], data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto val = root["show_performance_metrics"]; val.is_bool())
|
||||||
|
show_performance_metrics = val.get_bool();
|
||||||
|
|
||||||
|
if (auto val = root["preferred_refresh_rate"]; val.is_double())
|
||||||
|
preferred_refresh_rate = val.get_double();
|
||||||
|
|
||||||
|
if (auto val = root["resolution_scale"]; val.is_double())
|
||||||
|
resolution_scale = val.get_double();
|
||||||
|
|
||||||
|
if (auto val = root["passthrough_enabled"]; val.is_bool())
|
||||||
|
passthrough_enabled = val.get_bool();
|
||||||
|
|
||||||
|
for (const auto & [i, name]: magic_enum::enum_entries<feature>())
|
||||||
|
{
|
||||||
|
if (auto val = root[name]; val.is_bool())
|
||||||
|
features[i] = val.get_bool();
|
||||||
|
}
|
||||||
|
|
||||||
if (system.passthrough_supported() == xr::system::passthrough_type::no_passthrough)
|
if (system.passthrough_supported() == xr::system::passthrough_type::no_passthrough)
|
||||||
passthrough_enabled = false;
|
passthrough_enabled = false;
|
||||||
}
|
}
|
||||||
catch (std::exception & e)
|
catch (std::exception & e)
|
||||||
{
|
{
|
||||||
spdlog::warn("Cannot read configuration: {}", e.what());
|
spdlog::warn("Cannot read configuration: {}", e.what());
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
configuration::configuration(const std::string & path)
|
// Restore default configuration
|
||||||
{
|
servers.clear();
|
||||||
simdjson::dom::parser parser;
|
preferred_refresh_rate = 0;
|
||||||
simdjson::dom::element root = parser.load(path);
|
resolution_scale = 1.4;
|
||||||
for (simdjson::dom::object i: simdjson::dom::array(root["servers"]))
|
show_performance_metrics = false;
|
||||||
{
|
passthrough_enabled = system.passthrough_supported() == xr::system::passthrough_type::color;
|
||||||
server_data data{
|
|
||||||
.autoconnect = i["autoconnect"].get_bool(),
|
|
||||||
.manual = i["manual"].get_bool(),
|
|
||||||
.visible = false,
|
|
||||||
.compatible = true,
|
|
||||||
.service = {
|
|
||||||
.name = (std::string)i["pretty_name"],
|
|
||||||
.hostname = (std::string)i["hostname"],
|
|
||||||
.port = (int)i["port"].get_int64(),
|
|
||||||
.tcp_only = i["tcp_only"].is_bool() && i["tcp_only"].get_bool(),
|
|
||||||
.txt = {{"cookie", (std::string)i["cookie"]}}
|
|
||||||
|
|
||||||
}};
|
|
||||||
servers.emplace(data.service.txt["cookie"], data);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto val = root["show_performance_metrics"]; val.is_bool())
|
|
||||||
show_performance_metrics = val.get_bool();
|
|
||||||
|
|
||||||
if (auto val = root["preferred_refresh_rate"]; val.is_double())
|
|
||||||
{
|
|
||||||
preferred_refresh_rate = val.get_double();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto val = root["resolution_scale"]; val.is_double())
|
|
||||||
{
|
|
||||||
resolution_scale = val.get_double();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto val = root["passthrough_enabled"]; val.is_bool())
|
|
||||||
passthrough_enabled = val.get_bool();
|
|
||||||
|
|
||||||
for (const auto & [i, name]: magic_enum::enum_entries<feature>())
|
|
||||||
{
|
|
||||||
if (auto val = root[name]; val.is_bool())
|
|
||||||
features[i] = val.get_bool();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,6 +201,8 @@ static std::ostream & operator<<(std::ostream & stream, feature f)
|
|||||||
|
|
||||||
void configuration::save()
|
void configuration::save()
|
||||||
{
|
{
|
||||||
|
std::lock_guard lock(mutex);
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
for (auto & [cookie, server_data]: servers)
|
for (auto & [cookie, server_data]: servers)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include "wivrn_discover.h"
|
#include "wivrn_discover.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace xr
|
namespace xr
|
||||||
@@ -53,8 +54,8 @@ public:
|
|||||||
void set_feature(feature f, bool state);
|
void set_feature(feature f, bool state);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
mutable std::mutex mutex;
|
||||||
std::map<feature, bool> features;
|
std::map<feature, bool> features;
|
||||||
configuration(const std::string &);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
configuration(xr::system &);
|
configuration(xr::system &);
|
||||||
|
|||||||
Reference in New Issue
Block a user