fix: migrate Android JNI bridge to jni 0.22 API (#1665)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).
<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
Erwan Leboucher authored and GitHub committed 2026-08-07 17:24:02 +00:00
commit 1119eb6ead
1 file changed
+46 -41
+46 -41
View File
@@ -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())
}