config.rs (1691B) - raw


      1 use std::{error::Error, sync::Mutex};
      2 use jni::{objects::JObject, JNIEnv};
      3 use crate::util::get_jni_string;
      4 
      5 static NATIVE_CONFIG: Mutex<Option<NativeConfig>> = Mutex::new(None);
      6 
      7 pub fn native_config() -> NativeConfig {
      8     NATIVE_CONFIG.lock().unwrap().as_ref().expect("NativeConfig not loaded").clone()
      9 }
     10 
     11 #[derive(Debug, Clone)]
     12 pub(crate) struct NativeConfig {
     13     pub disable_bitmoji: bool,
     14     pub disable_metrics: bool,
     15     pub composer_hooks: bool,
     16     pub custom_emoji_font_path: Option<String>,
     17 }
     18 
     19 impl NativeConfig {
     20     fn new(env: &mut JNIEnv, obj: JObject) -> Result<Self, Box<dyn Error>> {
     21         macro_rules! get_boolean {
     22             ($field:expr) => {
     23                 env.get_field(&obj, $field, "Z")?.z()?
     24             };
     25         }
     26 
     27         macro_rules! get_string {
     28             ($field:expr) => {
     29                 match env.get_field(&obj, $field, "Ljava/lang/String;")?.l()? {
     30                     jstring => if !jstring.is_null() {
     31                         Some(get_jni_string(env, jstring.into())?)
     32                     } else {
     33                         None
     34                     },
     35                 }
     36             };
     37         }
     38 
     39         Ok(Self {
     40             disable_bitmoji: get_boolean!("disableBitmoji"),
     41             disable_metrics: get_boolean!("disableMetrics"),
     42             composer_hooks: get_boolean!("composerHooks"),
     43             custom_emoji_font_path: get_string!("customEmojiFontPath"),
     44         })
     45     }
     46 }
     47 
     48 pub fn load_config(mut env: JNIEnv, _class: JObject, obj: JObject)  {
     49     NATIVE_CONFIG.lock().unwrap().replace(
     50         NativeConfig::new(&mut env, obj).expect("Failed to load NativeConfig")
     51     );
     52     
     53     info!("Config loaded {:?}", native_config());
     54 }