1use tauri::{plugin::TauriPlugin, Runtime};
2
3#[cfg(target_os = "android")]
4use std::sync::atomic::{AtomicBool, Ordering};
5
6#[cfg(target_os = "android")]
7const PLUGIN_IDENTIFIER: &str = "org.jakebot.blew";
8
9#[cfg(target_os = "android")]
10static AUTO_REQUEST_PERMISSIONS: AtomicBool = AtomicBool::new(true);
11
12#[derive(Clone, Debug)]
14pub struct BlewPluginConfig {
15 pub auto_request_permissions: bool,
20}
21
22impl Default for BlewPluginConfig {
23 fn default() -> Self {
24 Self {
25 auto_request_permissions: true,
26 }
27 }
28}
29
30pub fn are_ble_permissions_granted() -> bool {
34 #[cfg(target_os = "android")]
35 {
36 blew::platform::android::are_ble_permissions_granted()
37 }
38 #[cfg(not(target_os = "android"))]
39 {
40 true
41 }
42}
43
44pub fn request_ble_permissions() {
55 #[cfg(target_os = "android")]
56 {
57 blew::platform::android::request_ble_permissions();
58 }
59}
60
61pub fn is_emulator() -> bool {
66 #[cfg(target_os = "android")]
67 {
68 blew::platform::android::is_emulator()
69 }
70 #[cfg(not(target_os = "android"))]
71 {
72 std::env::var("SIMULATOR_DEVICE_NAME").is_ok()
73 }
74}
75
76pub fn init<R: Runtime>() -> TauriPlugin<R> {
79 init_with_config(BlewPluginConfig::default())
80}
81
82pub fn init_with_config<R: Runtime>(config: BlewPluginConfig) -> TauriPlugin<R> {
84 #[cfg(target_os = "android")]
85 {
86 AUTO_REQUEST_PERMISSIONS.store(config.auto_request_permissions, Ordering::Relaxed);
87 }
88 #[cfg(not(target_os = "android"))]
89 {
90 let _ = config;
91 }
92
93 tauri::plugin::Builder::<R>::new("blew")
94 .setup(|_app, api| {
95 #[cfg(target_os = "android")]
96 {
97 let ctx = ndk_context::android_context();
98 let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) };
99 blew::platform::android::init_jvm(vm);
100 api.register_android_plugin(PLUGIN_IDENTIFIER, "BlewPlugin")?;
101 }
102 let _ = api;
103 Ok(())
104 })
105 .build()
106}
107
108#[cfg(target_os = "android")]
116#[unsafe(no_mangle)]
117pub unsafe extern "C" fn Java_org_jakebot_blew_BlewPluginNative_autoRequestPermissionsEnabled(
118 _env: jni::EnvUnowned,
119 _class: jni::objects::JClass,
120) -> jni::sys::jboolean {
121 AUTO_REQUEST_PERMISSIONS.load(Ordering::Relaxed)
122}