Factorize permission code
This commit is contained in:
committed by
Guillaume Meunier
parent
1623769816
commit
76626b8133
@@ -22,6 +22,9 @@
|
|||||||
#include "application.h"
|
#include "application.h"
|
||||||
#include "jnipp.h"
|
#include "jnipp.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
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);
|
||||||
@@ -30,12 +33,25 @@ static bool check_permission(jni::object<jni::details::string_literal<24>{"andro
|
|||||||
|
|
||||||
bool check_permission(const char * permission)
|
bool check_permission(const char * permission)
|
||||||
{
|
{
|
||||||
|
if (not permission)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Cache positive results to avoid useless jni calls
|
||||||
|
static std::set<const char *> permissions;
|
||||||
|
static std::mutex mutex;
|
||||||
|
std::lock_guard lock(mutex);
|
||||||
|
if (permissions.contains(permission))
|
||||||
|
return true;
|
||||||
|
|
||||||
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");
|
||||||
|
|
||||||
jni::string jpermission(permission);
|
jni::string jpermission(permission);
|
||||||
return check_permission(ctx, jpermission);
|
bool res = check_permission(ctx, jpermission);
|
||||||
|
if (res)
|
||||||
|
permissions.insert(permission);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void request_permission(const char * permission, int requestCode)
|
void request_permission(const char * permission, int requestCode)
|
||||||
|
|||||||
@@ -19,5 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// 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, int requestCode);
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
#include "android/permissions.h"
|
|
||||||
#include "utils/named_thread.h"
|
#include "utils/named_thread.h"
|
||||||
#include "wivrn_client.h"
|
#include "wivrn_client.h"
|
||||||
#include "xr/instance.h"
|
#include "xr/instance.h"
|
||||||
@@ -309,16 +308,3 @@ void wivrn::android::audio::get_audio_description(xrt::drivers::wivrn::from_head
|
|||||||
|
|
||||||
AAudioStreamBuilder_delete(builder);
|
AAudioStreamBuilder_delete(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wivrn::android::audio::check_mic_permission()
|
|
||||||
{
|
|
||||||
static std::atomic<bool> allowed = false;
|
|
||||||
if (not allowed)
|
|
||||||
allowed = check_permission("android.permission.RECORD_AUDIO");
|
|
||||||
return allowed;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wivrn::android::audio::request_mic_permission()
|
|
||||||
{
|
|
||||||
request_permission("android.permission.RECORD_AUDIO", 0);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -73,7 +73,5 @@ public:
|
|||||||
void operator()(xrt::drivers::wivrn::audio_data &&);
|
void operator()(xrt::drivers::wivrn::audio_data &&);
|
||||||
|
|
||||||
static void get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet & info);
|
static void get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet & info);
|
||||||
static bool check_mic_permission();
|
|
||||||
static void request_mic_permission();
|
|
||||||
};
|
};
|
||||||
} // namespace wivrn::android
|
} // namespace wivrn::android
|
||||||
|
|||||||
@@ -39,12 +39,6 @@ public:
|
|||||||
void operator()(xrt::drivers::wivrn::audio_data &&) {}
|
void operator()(xrt::drivers::wivrn::audio_data &&) {}
|
||||||
|
|
||||||
static void get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet & info) {}
|
static void get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet & info) {}
|
||||||
|
|
||||||
static bool check_mic_permission()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
static void request_mic_permission() {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,9 +20,15 @@
|
|||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
|
#include "hardware.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <magic_enum.hpp>
|
||||||
#include <simdjson.h>
|
#include <simdjson.h>
|
||||||
|
|
||||||
|
#ifdef __ANDROID__
|
||||||
|
#include "android/permissions.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
static std::string json_string(const std::string & in)
|
static std::string json_string(const std::string & in)
|
||||||
{
|
{
|
||||||
std::string out;
|
std::string out;
|
||||||
@@ -73,6 +79,31 @@ static std::string json_string(const std::string & in)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool configuration::check_feature(feature f)
|
||||||
|
{
|
||||||
|
auto it = features.find(f);
|
||||||
|
if (it == features.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Skip permission checks if not requested
|
||||||
|
if (not it->second)
|
||||||
|
return false;
|
||||||
|
#ifdef __ANDROID__
|
||||||
|
return check_permission(permission_name(f));
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void configuration::set_feature(feature f, bool state)
|
||||||
|
{
|
||||||
|
#ifdef __ANDROID__
|
||||||
|
if (state)
|
||||||
|
request_permission(permission_name(f), 0);
|
||||||
|
#endif
|
||||||
|
features[f] = state;
|
||||||
|
}
|
||||||
|
|
||||||
configuration::configuration(xr::system & system)
|
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;
|
||||||
@@ -123,11 +154,19 @@ configuration::configuration(const std::string & path)
|
|||||||
resolution_scale = val.get_double();
|
resolution_scale = val.get_double();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto val = root["microphone"]; val.is_bool())
|
|
||||||
microphone = val.get_bool();
|
|
||||||
|
|
||||||
if (auto val = root["passthrough_enabled"]; val.is_bool())
|
if (auto val = root["passthrough_enabled"]; val.is_bool())
|
||||||
passthrough_enabled = val.get_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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::ostream & operator<<(std::ostream & stream, feature f)
|
||||||
|
{
|
||||||
|
return stream << "\"" << magic_enum::enum_name(f) << "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
void configuration::save()
|
void configuration::save()
|
||||||
@@ -161,7 +200,8 @@ void configuration::save()
|
|||||||
if (preferred_refresh_rate != 0.)
|
if (preferred_refresh_rate != 0.)
|
||||||
json << ",\"preferred_refresh_rate\":" << preferred_refresh_rate;
|
json << ",\"preferred_refresh_rate\":" << preferred_refresh_rate;
|
||||||
json << ",\"resolution_scale\":" << resolution_scale;
|
json << ",\"resolution_scale\":" << resolution_scale;
|
||||||
json << ",\"microphone\":" << std::boolalpha << microphone;
|
|
||||||
json << ",\"passthrough_enabled\":" << std::boolalpha << passthrough_enabled;
|
json << ",\"passthrough_enabled\":" << std::boolalpha << passthrough_enabled;
|
||||||
|
for (auto & [key, value]: features)
|
||||||
|
json << "," << key << ":" << std::boolalpha << value;
|
||||||
json << "}";
|
json << "}";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "hardware.h"
|
||||||
#include "wivrn_discover.h"
|
#include "wivrn_discover.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace xr
|
namespace xr
|
||||||
{
|
{
|
||||||
class system;
|
class system;
|
||||||
@@ -43,10 +47,13 @@ public:
|
|||||||
float preferred_refresh_rate = 0;
|
float preferred_refresh_rate = 0;
|
||||||
float resolution_scale = 1.4;
|
float resolution_scale = 1.4;
|
||||||
bool show_performance_metrics = false;
|
bool show_performance_metrics = false;
|
||||||
bool microphone = false;
|
|
||||||
bool passthrough_enabled = false;
|
bool passthrough_enabled = false;
|
||||||
|
|
||||||
|
bool check_feature(feature f);
|
||||||
|
void set_feature(feature f, bool state);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::map<feature, bool> features;
|
||||||
configuration(const std::string &);
|
configuration(const std::string &);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -152,3 +152,46 @@ bool need_srgb_conversion(model m)
|
|||||||
}
|
}
|
||||||
throw std::range_error("invalid model " + std::to_string((int)m));
|
throw std::range_error("invalid model " + std::to_string((int)m));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char * permission_name(feature f)
|
||||||
|
{
|
||||||
|
switch (f)
|
||||||
|
{
|
||||||
|
case feature::microphone:
|
||||||
|
return "android.permission.RECORD_AUDIO";
|
||||||
|
case feature::eye_gaze:
|
||||||
|
switch (guess_model())
|
||||||
|
{
|
||||||
|
case model::oculus_quest:
|
||||||
|
case model::oculus_quest_2:
|
||||||
|
case model::meta_quest_pro:
|
||||||
|
case model::meta_quest_3:
|
||||||
|
return "com.oculus.permission.EYE_TRACKING";
|
||||||
|
case model::pico_neo_3:
|
||||||
|
case model::pico_4:
|
||||||
|
return "com.picovr.permission.EYE_TRACKING";
|
||||||
|
case model::htc_vive_focus_3:
|
||||||
|
case model::htc_vive_xr_elite:
|
||||||
|
case model::lynx_r1:
|
||||||
|
case model::unknown:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
case feature::face_tracking:
|
||||||
|
switch (guess_model())
|
||||||
|
{
|
||||||
|
case model::oculus_quest:
|
||||||
|
case model::oculus_quest_2:
|
||||||
|
case model::meta_quest_pro:
|
||||||
|
case model::meta_quest_3:
|
||||||
|
return "com.oculus.permission.FACE_TRACKING";
|
||||||
|
case model::pico_neo_3:
|
||||||
|
case model::pico_4:
|
||||||
|
case model::htc_vive_focus_3:
|
||||||
|
case model::htc_vive_xr_elite:
|
||||||
|
case model::lynx_r1:
|
||||||
|
case model::unknown:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,8 +34,18 @@ enum class model
|
|||||||
unknown
|
unknown
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class feature
|
||||||
|
{
|
||||||
|
microphone,
|
||||||
|
eye_gaze,
|
||||||
|
face_tracking,
|
||||||
|
};
|
||||||
|
|
||||||
model guess_model();
|
model guess_model();
|
||||||
|
|
||||||
XrViewConfigurationView override_view(XrViewConfigurationView, model);
|
XrViewConfigurationView override_view(XrViewConfigurationView, model);
|
||||||
|
|
||||||
bool need_srgb_conversion(model);
|
bool need_srgb_conversion(model);
|
||||||
|
|
||||||
|
// Return nullptr if no permission is required
|
||||||
|
const char * permission_name(feature f);
|
||||||
|
|||||||
@@ -380,12 +380,10 @@ void scenes::lobby::gui_settings()
|
|||||||
vibrate_on_hover();
|
vibrate_on_hover();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool enabled = config.microphone and audio::check_mic_permission();
|
bool enabled = config.check_feature(feature::microphone);
|
||||||
if (ImGui::Checkbox(_S("Enable microphone"), &enabled))
|
if (ImGui::Checkbox(_S("Enable microphone"), &enabled))
|
||||||
{
|
{
|
||||||
config.microphone = enabled;
|
config.set_feature(feature::microphone, enabled);
|
||||||
if (config.microphone)
|
|
||||||
audio::request_mic_permission();
|
|
||||||
config.save();
|
config.save();
|
||||||
}
|
}
|
||||||
vibrate_on_hover();
|
vibrate_on_hover();
|
||||||
@@ -590,7 +588,7 @@ static void ScrollWhenDraggingOnVoid(ImVec2 delta)
|
|||||||
void scenes::lobby::draw_mic_status()
|
void scenes::lobby::draw_mic_status()
|
||||||
{
|
{
|
||||||
auto & config = application::get_config();
|
auto & config = application::get_config();
|
||||||
bool enabled = config.microphone and audio::check_mic_permission();
|
const bool enabled = config.check_feature(feature::microphone);
|
||||||
const char * text = enabled ? ICON_FA_MICROPHONE : ICON_FA_MICROPHONE_SLASH;
|
const char * text = enabled ? ICON_FA_MICROPHONE : ICON_FA_MICROPHONE_SLASH;
|
||||||
float win_width = ImGui::GetWindowSize().x;
|
float win_width = ImGui::GetWindowSize().x;
|
||||||
float text_width = ImGui::CalcTextSize(text).x;
|
float text_width = ImGui::CalcTextSize(text).x;
|
||||||
@@ -601,9 +599,8 @@ void scenes::lobby::draw_mic_status()
|
|||||||
ImGui::PushStyleColor(ImGuiCol_Text, enabled ? ImGui::GetColorU32(ImGuiCol_Text) : IM_COL32(255, 0, 0, 255));
|
ImGui::PushStyleColor(ImGuiCol_Text, enabled ? ImGui::GetColorU32(ImGuiCol_Text) : IM_COL32(255, 0, 0, 255));
|
||||||
if (ImGui::Button(text))
|
if (ImGui::Button(text))
|
||||||
{
|
{
|
||||||
config.microphone = not enabled;
|
// button doesn't alter the bool
|
||||||
if (config.microphone)
|
config.set_feature(feature::microphone, not enabled);
|
||||||
audio::request_mic_permission();
|
|
||||||
config.save();
|
config.save();
|
||||||
}
|
}
|
||||||
vibrate_on_hover();
|
vibrate_on_hover();
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
|
|||||||
info.face_tracking2_fb = application::get_fb_face_tracking2_supported();
|
info.face_tracking2_fb = application::get_fb_face_tracking2_supported();
|
||||||
|
|
||||||
audio::get_audio_description(info);
|
audio::get_audio_description(info);
|
||||||
if (not(application::get_config().microphone and audio::check_mic_permission()))
|
if (not(application::get_config().check_feature(feature::microphone)))
|
||||||
info.microphone = {};
|
info.microphone = {};
|
||||||
|
|
||||||
info.supported_codecs = decoder_impl::supported_codecs();
|
info.supported_codecs = decoder_impl::supported_codecs();
|
||||||
|
|||||||
Reference in New Issue
Block a user