-
Notifications
You must be signed in to change notification settings - Fork 6
/
s002_callback.rs
51 lines (47 loc) · 1.55 KB
/
s002_callback.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use jni::objects::{GlobalRef, JClass, JObject, JValueGen};
use jni::sys::jlong;
use jni::JNIEnv;
use std::sync::Mutex;
static GLOBAL_REF_STORE: Mutex<Option<GlobalRef>> = Mutex::new(None);
#[no_mangle]
pub extern "system" fn Java_sample_s002_Caller_receive<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
j_val: jlong,
) {
if let Ok(guard) = GLOBAL_REF_STORE.lock() {
let global_ref = guard.as_ref().unwrap();
let _ = env.call_method(
global_ref,
"onReceived",
"(J)V",
&[JValueGen::Long(j_val); 1],
);
}
}
#[no_mangle]
pub extern "system" fn Java_sample_s002_Caller_defReceivedAction<'local>(
env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
j_callback: JObject<'local>,
) {
if let Ok(global_callback) = env.new_global_ref(j_callback) {
def_java_callback(global_callback);
} else {
panic!("failed to create global reference to callback object");
}
}
fn def_java_callback(global_callback: GlobalRef) {
match GLOBAL_REF_STORE.lock() {
Ok(mut guard) => {
guard.replace(global_callback);
}
Err(e) => panic!("failed to get global ref: {}", e),
}
}