mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
fix: migrate Android JNI bridge to jni 0.22 API
This commit is contained in:
1 parent
39e0ef2cb4
commit
f7d1a45bbf
1 file changed
+46
-41
+46
-41
@@ -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<JavaVM> = 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())
|
||||
}
|
||||
Reference in new issue
Block a user