diff options
| -rw-r--r-- | mullvad-jni/src/from_java.rs | 51 | ||||
| -rw-r--r-- | mullvad-jni/src/lib.rs | 3 |
2 files changed, 53 insertions, 1 deletions
diff --git a/mullvad-jni/src/from_java.rs b/mullvad-jni/src/from_java.rs index 7a87e10884..186079ca77 100644 --- a/mullvad-jni/src/from_java.rs +++ b/mullvad-jni/src/from_java.rs @@ -3,7 +3,7 @@ use jni::{ objects::{JObject, JString}, JNIEnv, }; -use mullvad_types::relay_constraints::Constraint; +use mullvad_types::relay_constraints::{Constraint, LocationConstraint}; use std::{fmt::Debug, ops::Deref}; pub trait FromJava<'env> { @@ -59,6 +59,42 @@ where } } +impl<'env> FromJava<'env> for LocationConstraint { + type JavaType = JObject<'env>; + + fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self { + let country_class = "net/mullvad/mullvadvpn/model/LocationConstraint$Country"; + let city_class = "net/mullvad/mullvadvpn/model/LocationConstraint$City"; + let hostname_class = "net/mullvad/mullvadvpn/model/LocationConstraint$Hostname"; + + if is_instance_of(env, source, country_class) { + let country = get_string_field(env, source, "countryCode"); + + LocationConstraint::Country(String::from_java(env, country)) + } else if is_instance_of(env, source, city_class) { + let country = get_string_field(env, source, "countryCode"); + let city = get_string_field(env, source, "cityCode"); + + LocationConstraint::City( + String::from_java(env, country), + String::from_java(env, city), + ) + } else if is_instance_of(env, source, hostname_class) { + let country = get_string_field(env, source, "countryCode"); + let city = get_string_field(env, source, "cityCode"); + let hostname = get_string_field(env, source, "hostname"); + + LocationConstraint::Hostname( + String::from_java(env, country), + String::from_java(env, city), + String::from_java(env, hostname), + ) + } else { + panic!("Invalid LocationConstraint Java sub-class"); + } + } +} + fn is_instance_of<'env>( env: &JNIEnv<'env>, object: JObject<'env>, @@ -70,6 +106,19 @@ fn is_instance_of<'env>( .expect("Failed to check if an object is an instance of a specified class") } +fn get_string_field<'env>( + env: &JNIEnv<'env>, + object: JObject<'env>, + field_name: &str, +) -> JString<'env> { + JString::from(get_object_field( + env, + object, + field_name, + "Ljava/lang/String;", + )) +} + fn get_object_field<'env>( env: &JNIEnv<'env>, object: JObject<'env>, diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs index fdd6062cc4..91a83f09cd 100644 --- a/mullvad-jni/src/lib.rs +++ b/mullvad-jni/src/lib.rs @@ -23,6 +23,9 @@ const CLASSES_TO_LOAD: &[&str] = &[ "net/mullvad/mullvadvpn/model/AccountData", "net/mullvad/mullvadvpn/model/Constraint$Any", "net/mullvad/mullvadvpn/model/Constraint$Only", + "net/mullvad/mullvadvpn/model/LocationConstraint$City", + "net/mullvad/mullvadvpn/model/LocationConstraint$Country", + "net/mullvad/mullvadvpn/model/LocationConstraint$Hostname", "net/mullvad/mullvadvpn/model/Relay", "net/mullvad/mullvadvpn/model/RelayList", "net/mullvad/mullvadvpn/model/RelayListCity", |
