extern crate std;
pub mod android_runtime;
pub mod components;
pub mod convert;
pub mod ffi_bridge;
pub mod inspector;
pub mod navigation;
pub mod reactive;
pub mod webview_bridge;
pub use convert::JniPrimitive;
pub use jni;
pub use jni::objects::{Global, JClass, JObject, JValue};
pub use jni::sys::{JNI_VERSION_1_6, jboolean, jdouble, jfloat, jint, jlong, jobject};
pub use jni::{Env, EnvUnowned as JNIEnv, JavaVM, jni_sig, jni_str};
pub unsafe fn init(_vm: *mut jni::sys::JavaVM) -> jint {
tracing::debug!("WaterUI JNI module initialized");
JNI_VERSION_1_6
}
pub fn with_env<'local, T: Default>(
env: &mut JNIEnv<'local>,
operation: impl FnOnce(&mut Env<'local>) -> T,
) -> T {
use jni::errors::ThrowRuntimeExAndDefault;
env.with_env(|env| -> jni::errors::Result<T> { Ok(operation(env)) })
.resolve::<ThrowRuntimeExAndDefault>()
}
pub fn with_attached_env<T>(
vm: &JavaVM,
operation: impl FnOnce(&mut Env<'_>) -> T,
) -> jni::errors::Result<T> {
vm.attach_current_thread(|env| -> jni::errors::Result<T> { Ok(operation(env)) })
}
pub fn array_len(len: usize) -> jint {
jint::try_from(len).expect("JNI array length exceeds jint capacity")
}
pub fn type_id_to_java<'local>(
env: &mut Env<'local>,
type_id: crate::WuiTypeId,
) -> JObject<'local> {
let low = type_id.low as jlong;
let high = type_id.high as jlong;
env.new_object(
jni_str!("dev/waterui/android/runtime/TypeIdStruct"),
jni_sig!("(JJ)V"),
&[JValue::Long(low), JValue::Long(high)],
)
.expect("Failed to create TypeIdStruct")
}
pub fn extract_watcher_struct(env: &mut Env<'_>, watcher: &JObject) -> (jlong, jlong, jlong) {
let data_ptr = env
.get_field(watcher, jni_str!("dataPtr"), jni_sig!("J"))
.expect("Failed to get dataPtr")
.j()
.expect("dataPtr is not a long");
let call_ptr = env
.get_field(watcher, jni_str!("callPtr"), jni_sig!("J"))
.expect("Failed to get callPtr")
.j()
.expect("callPtr is not a long");
let drop_ptr = env
.get_field(watcher, jni_str!("dropPtr"), jni_sig!("J"))
.expect("Failed to get dropPtr")
.j()
.expect("dropPtr is not a long");
(data_ptr, call_ptr, drop_ptr)
}