-
Notifications
You must be signed in to change notification settings - Fork 2
/
status_updater.rs
67 lines (55 loc) · 1.84 KB
/
status_updater.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use jni::{objects::JObject, objects::JValue};
use jni::sys::jint;
use crate::log_debug;
use crate::logging::android_log;
use crate::constants::TAG;
use crate::jni_globals::{with_env, JniResult};
#[repr(i32)]
#[derive(Clone, Copy, Debug)]
#[allow(dead_code)]
pub enum SnowbirdServiceStatus {
BackendInitializing = 0,
BackendRunning = 1,
WebServerInitializing = 2,
WebServerRunning = 3,
Processing = 4,
Idle = 5,
Error = 6,
}
#[allow(dead_code)]
pub fn update_status(status: SnowbirdServiceStatus) -> JniResult<()> {
log_debug!(TAG, "Updating status: {:?}", status);
update_extended_status(status, Some("hi"))?;
log_debug!(TAG, "Status update complete");
Ok(())
}
pub fn update_extended_status(status: SnowbirdServiceStatus, error_message: Option<&str>) -> JniResult<()> {
let class_name = "net/opendasharchive/openarchive/services/snowbird/SnowbirdBridge";
let method_name = "updateStatusFromRust";
let method_signature = "(ILjava/lang/String;)V";
let status_code: jint = status as jint;
// Assume we have a function to get the JavaVM
// let vm = get_java_vm()?;
// Attach the current thread to get a JNIEnv
// let env = vm.attach_current_thread()?;
with_env(|mut env| {
// Create the error string
let error_jstring = error_message
.map(|msg| env.new_string(msg))
.transpose()?;
let null_obj = JObject::null();
let error_jvalue = error_jstring
.as_ref()
.map_or(JValue::Object(&null_obj), |s| JValue::Object(s.as_ref()));
// Find the class
let class = env.find_class(class_name)?;
env.call_static_method(
class,
method_name,
method_signature,
&[JValue::Int(status_code), error_jvalue]
)?;
Ok(())
})?;
Ok(())
}