diff --git a/src-tauri/src/mobile.rs b/src-tauri/src/mobile.rs index fa2b42dad..04b87c1bd 100644 --- a/src-tauri/src/mobile.rs +++ b/src-tauri/src/mobile.rs @@ -7,19 +7,22 @@ use std::sync::OnceLock; use jni::objects::{JObject, JValue}; -use jni::{JNIEnv, JavaVM}; +use jni::strings::JNIString; +use jni::{jni_sig, jni_str, EnvUnowned, JavaVM}; static JAVA_VM: OnceLock = OnceLock::new(); // Called by MainActivity.onCreate to cache the JavaVM for later callbacks. #[no_mangle] pub extern "system" fn Java_moe_sable_client_MainActivity_nativeInitStatusBar( - env: JNIEnv, + mut env: EnvUnowned, _this: JObject, ) { - if let Ok(vm) = env.get_java_vm() { + let _ = env.with_env(|env| { + let vm = env.get_java_vm()?; let _ = JAVA_VM.set(vm); - } + Ok::<_, jni::errors::Error>(()) + }); } /// `color` is a packed ARGB int (as produced by Android's `Color`). @@ -52,49 +55,51 @@ pub(crate) fn play_notification_sound(kind: String) -> Result<(), String> { _ => 0, }; let vm = JAVA_VM.get().ok_or("java vm not initialized")?; - let mut env = vm.attach_current_thread().map_err(|e| e.to_string())?; - - let result = env.call_static_method( - "moe/sable/client/MainActivity", - "playNotificationSoundNative", - "(I)V", - &[JValue::Int(code)], - ); - if result.is_err() { - let _ = env.exception_clear(); - } - result.map_err(|e| e.to_string())?; - - Ok(()) + vm.attach_current_thread(|env| { + let result = env.call_static_method( + jni_str!("moe/sable/client/MainActivity"), + jni_str!("playNotificationSoundNative"), + jni_sig!("(I)V"), + &[JValue::Int(code)], + ); + if result.is_err() { + let _ = env.exception_clear(); + } + result.map(|_| ()) + }) + .map_err(|e| e.to_string()) } fn call_bar_color(method: &str, color: u32) -> Result<(), String> { let vm = JAVA_VM.get().ok_or("java vm not initialized")?; - let mut env = vm.attach_current_thread().map_err(|e| e.to_string())?; - - let result = env.call_static_method( - "moe/sable/client/MainActivity", - method, - "(I)V", - &[JValue::Int(color as i32)], - ); - if result.is_err() { - let _ = env.exception_clear(); - } - result.map_err(|e| e.to_string())?; - - Ok(()) + vm.attach_current_thread(|env| { + let result = env.call_static_method( + jni_str!("moe/sable/client/MainActivity"), + JNIString::new(method), + jni_sig!("(I)V"), + &[JValue::Int(color as i32)], + ); + if result.is_err() { + let _ = env.exception_clear(); + } + result.map(|_| ()) + }) + .map_err(|e| e.to_string()) } fn call_static_method(method: &str) -> Result<(), String> { let vm = JAVA_VM.get().ok_or("java vm not initialized")?; - let mut env = vm.attach_current_thread().map_err(|e| e.to_string())?; - - let result = env.call_static_method("moe/sable/client/MainActivity", method, "()V", &[]); - if result.is_err() { - let _ = env.exception_clear(); - } - result.map_err(|e| e.to_string())?; - - Ok(()) + vm.attach_current_thread(|env| { + let result = env.call_static_method( + jni_str!("moe/sable/client/MainActivity"), + JNIString::new(method), + jni_sig!("()V"), + &[], + ); + if result.is_err() { + let _ = env.exception_clear(); + } + result.map(|_| ()) + }) + .map_err(|e| e.to_string()) }