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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use crate::{error::*, utils::*};
use jni::{
    objects::{JObject, JValue},
    signature::{JavaType, Primitive},
    JNIEnv,
};
use std::{collections::HashMap, fmt::Display};

#[derive(Debug, Clone)]
pub enum JniRustType {
    Void,
    String(String),
    StringArray(Vec<String>),
    Boolean(bool),
    Int(i64),
    IntArray(Vec<i64>),
    ByteArray(Vec<u8>),
    Float(f32),
    Double(f64),
    FloatArray(Vec<f32>),
    DoubleArray(Vec<f64>),
    ObjectArray(Vec<JniRustType>),
    Map(HashMap<String, JniRustType>),
}

impl JniRustType {
    /// Try to unwrap to Void.
    pub fn into_void(self) -> Option<()> {
        match self {
            Self::Void => Some(()),
            _ => None,
        }
    }

    /// Try to unwrap to Boolean.
    pub fn into_bool(self) -> Option<bool> {
        match self {
            Self::Boolean(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to String.
    pub fn into_string(self) -> Option<String> {
        match self {
            Self::String(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to StringArray.
    pub fn into_string_array(self) -> Option<Vec<String>> {
        match self {
            Self::StringArray(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to Int.
    pub fn into_int(self) -> Option<i64> {
        match self {
            Self::Int(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to IntArray.
    pub fn into_int_array(self) -> Option<Vec<i64>> {
        match self {
            Self::IntArray(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to ByteArray.
    pub fn into_byte_array(self) -> Option<Vec<u8>> {
        match self {
            Self::ByteArray(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to Float.
    pub fn into_float(self) -> Option<f32> {
        match self {
            Self::Float(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to Double.
    pub fn into_double(self) -> Option<f64> {
        match self {
            Self::Double(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to FloatArray.
    pub fn into_float_array(self) -> Option<Vec<f32>> {
        match self {
            Self::FloatArray(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to DoubleArray.
    pub fn into_double_array(self) -> Option<Vec<f64>> {
        match self {
            Self::DoubleArray(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to ObjectArray.
    pub fn into_object_array(self) -> Option<Vec<Self>> {
        match self {
            Self::ObjectArray(val) => Some(val),
            _ => None,
        }
    }

    /// Try to unwrap to Map.
    pub fn into_map(self) -> Option<HashMap<String, Self>> {
        match self {
            Self::Map(val) => Some(val),
            _ => None,
        }
    }

    pub fn len(&self) -> usize {
        match self {
            Self::Void => 0,
            Self::String(s) => s.len(),
            Self::StringArray(s) => s.len(),
            Self::Boolean(_) => 1,
            Self::Int(_) => 1,
            Self::IntArray(i) => i.len(),
            Self::ByteArray(b) => b.len(),
            Self::Float(_) => 1,
            Self::Double(_) => 1,
            Self::FloatArray(f) => f.len(),
            Self::DoubleArray(d) => d.len(),
            Self::ObjectArray(j) => j.len(),
            Self::Map(m) => m.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            Self::Void => true,
            _ => self.len() > 0,
        }
    }

    // TODO: Test this function. It's not tested yet and possibly can fall with errors.
    pub fn from_jobject(env: &JNIEnv, obj: JObject) -> Result<Self> {
        if obj.is_null() {
            return Ok(Self::Void);
        }
        let class = env.get_object_class(obj)?;
        let name = get_class_name(env, class)?;

        let result = match name.as_str() {
            "V" => Self::Void,
            "java.lang.String" => {
                let val = jstring_to_string(env, obj.into())?;
                Self::String(val)
            }
            "[Ljava.lang.String;" => {
                let count = env.get_array_length(obj.into_inner())?;
                let mut arr = Vec::new();
                for i in 0..count {
                    let val = env.get_object_array_element(obj.into_inner(), i)?;
                    arr.push(jstring_to_string(env, val.into())?);
                }
                Self::StringArray(arr)
            }
            "java.lang.Boolean" => {
                let bool_value = env.get_method_id(class, "booleanValue", "()Z")?;
                let val = env.call_method_unchecked(
                    obj,
                    bool_value,
                    JavaType::Primitive(Primitive::Boolean),
                    &[],
                )?;
                Self::Boolean(val.z()?)
            }
            "java.lang.Integer" | "java.lang.Long" => {
                let nclass = env.find_class("java/lang/Number")?;
                let long_value = env.get_method_id(nclass, "longValue", "()J")?;
                let val = env.call_method_unchecked(
                    obj,
                    long_value,
                    JavaType::Primitive(Primitive::Long),
                    &[],
                )?;
                Self::Int(val.j()?)
            }
            "[I" => {
                let count = env.get_array_length(obj.into_inner())?;
                let mut integers = Vec::new();
                for i in 0..count {
                    let val = env.get_object_array_element(obj.into_inner(), i)?;
                    integers.push(JValue::from(val).j()?);
                }
                Self::IntArray(integers)
            }
            "[B" => {
                let arr = env.convert_byte_array(obj.into_inner())?;
                Self::ByteArray(arr)
            }
            "java.lang.Float" => {
                let nclass = env.find_class("java/lang/Number")?;
                let long_value = env.get_method_id(nclass, "floatValue", "()F")?;
                let res = env.call_method_unchecked(
                    obj,
                    long_value,
                    JavaType::Primitive(Primitive::Float),
                    &[],
                )?;
                Self::Float(res.f()?)
            }
            "java.lang.Double" => {
                let nclass = env.find_class("java/lang/Number")?;
                let long_value = env.get_method_id(nclass, "doubleValue", "()D")?;
                let res = env.call_method_unchecked(
                    obj,
                    long_value,
                    JavaType::Primitive(Primitive::Double),
                    &[],
                )?;
                Self::Double(res.d()?)
            }
            "[D" => {
                let count = env.get_array_length(obj.into_inner())?;
                let mut arr = Vec::new();
                for i in 0..count {
                    let val = env.get_object_array_element(obj.into_inner(), i)?;
                    arr.push(JValue::from(val).d()?);
                }
                Self::DoubleArray(arr)
            }
            "[F" => {
                let count = env.get_array_length(obj.into_inner())?;
                let mut arr = Vec::new();
                for i in 0..count {
                    let val = env.get_object_array_element(obj.into_inner(), i)?;
                    arr.push(JValue::from(val).f()?);
                }
                Self::FloatArray(arr)
            }
            "[Ljava.lang.Object;" => {
                let count = env.get_array_length(obj.into_inner())?;
                let mut arr = Vec::new();
                for i in 0..count {
                    let val = env.get_object_array_element(obj.into_inner(), i)?;
                    let inner = Self::from_jobject(env, val)?;
                    arr.push(inner);
                }
                Self::ObjectArray(arr)
            }
            "java.util.HashMap" | "com.crossbow.library.Dictionary" => {
                let get_keys = env.get_method_id(class, "get_keys", "()[Ljava/lang/String;")?;
                let arr =
                    env.call_method_unchecked(obj, get_keys, JavaType::Object("".to_owned()), &[])?;
                let keys = Self::from_jobject(env, arr.l()?)?
                    .into_string_array()
                    .ok_or(AndroidError::WrongJniRustType)?;

                let get_values = env.get_method_id(class, "get_values", "()[Ljava/lang/Object;")?;
                let arr = env.call_method_unchecked(
                    obj,
                    get_values,
                    JavaType::Object("".to_owned()),
                    &[],
                )?;
                let vals = Self::from_jobject(env, arr.l()?)?;

                let mut map = HashMap::new();
                let values = vals
                    .into_object_array()
                    .ok_or(AndroidError::WrongJniRustType)?;
                map.extend(keys.into_iter().zip(values.into_iter()));
                Self::Map(map)
            }
            _ => {
                return Err(AndroidError::UnsupportedJniRustType(name.to_owned()));
            }
        };
        Ok(result)
    }
}

impl Display for JniRustType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let val = match self {
            Self::Void => "".to_owned(),
            Self::String(s) => s.to_owned(),
            Self::StringArray(arr) => {
                let mut result = "".to_owned();
                for s in arr {
                    result = format!("{}{},", result, s);
                }
                result
            }
            Self::Boolean(b) => b.to_string(),
            Self::Int(i) => i.to_string(),
            Self::IntArray(arr) => {
                let mut result = "".to_owned();
                for i in arr {
                    result = format!("{}{},", result, i);
                }
                result
            }
            Self::ByteArray(arr) => std::str::from_utf8(arr).unwrap().to_owned(),
            Self::Float(f) => f.to_string(),
            Self::Double(d) => d.to_string(),
            Self::DoubleArray(arr) => {
                let mut result = "".to_owned();
                for i in arr {
                    result = format!("{}{},", result, i);
                }
                result
            }
            Self::FloatArray(arr) => {
                let mut result = "".to_owned();
                for i in arr {
                    result = format!("{}{},", result, i);
                }
                result
            }
            Self::ObjectArray(arr) => {
                let mut result = "".to_owned();
                for i in arr {
                    result = format!("{}{},", result, i);
                }
                result
            }
            Self::Map(map) => {
                let mut result = "".to_owned();
                for (k, v) in map {
                    result = format!("{}{}:{},", result, k, v);
                }
                result
            }
        };
        write!(f, "{}", val)
    }
}