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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
 * Copyright (c) 2024. The RigelA open source project team and
 * its contributors reserve all rights.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

pub use jni::{
    errors::Error,
    objects::{GlobalRef, JClass, JObject, JObjectArray, JValueGen, ReleaseMode},
    sys::{jboolean, jchar, jdouble, jfloat, jint, jlong, jshort, jsize},
    AttachGuard, JNIEnv, JavaVM, NativeMethod,
};
use log::{debug, error, warn};
use parking_lot::ReentrantMutex;
use std::{
    cell::RefCell,
    collections::HashMap,
    fmt::Debug,
    hash::{DefaultHasher, Hash, Hasher},
    str::FromStr,
    sync::{Arc, LazyLock, OnceLock},
};

// Rust 代理对象的哈希值映射到 Rust 函数
static HOOK_OBJECTS: LazyLock<
    ReentrantMutex<
        RefCell<
            HashMap<
                i32,
                Arc<
                    dyn Fn(&mut JNIEnv<'_>, &JObject<'_>, &JObjectArray<'_>) -> GlobalRef
                        + Send
                        + Sync,
                >,
            >,
        >,
    >,
> = LazyLock::new(|| ReentrantMutex::new(RefCell::new(HashMap::new())));
// Rust 代理对象的哈希值映射到 Java 被代理的对象的哈希值
static HOOK_OBJECTS_OTHER: LazyLock<ReentrantMutex<RefCell<HashMap<u64, i32>>>> =
    LazyLock::new(|| ReentrantMutex::new(RefCell::new(HashMap::new())));

/**
定义必要的trait,以便于在本地为任何数据类型实现JAVA对象所需的功能。
*/
#[macro_export]
macro_rules! import {
    () => {
        use std::{
            rc::Rc,
            sync::{Arc, Mutex},
        };
        use $crate::{null_value, unbind_proxy_handler, vm_attach, GlobalRef, JObject};

        /**
        JObjectRef trait提供从任何数据类型获取java对象的全局引用。
        rust使用Arc管理,无须手动释放java的全局引用。
        */
        pub trait JObjRef {
            /**
            获取java对象引用。
            */
            fn java_ref(&self) -> GlobalRef;
        }

        /**
        用于从java对象创建本地对象。
        */
        pub trait JObjNew {
            /// 字段类型
            type Fields: Default;

            /**
            从java对象创建本地对象。
            `this` java对象引用。
            */
            fn _new(this: &GlobalRef, fields: Self::Fields) -> Self;

            /**
            创建空对象。
            */
            fn null() -> Self
            where
                Self: Sized,
                Self::Fields: Default,
            {
                vm_attach!(mut env);
                Self::_new(&null_value(&mut env), Default::default())
            }
        }

        /**
        用于描述java类的信息。
        */
        pub trait JType: JObjRef + JObjNew {
            /// 错误类型。
            type Error;

            /// java类的名称。
            const CLASS: &'static str;

            /// 对象的签名描述。
            const OBJECT_SIG: &'static str;
        }

        /**
        用于Java动态代理的创建和删除。
        */
        pub trait JProxy: JObjNew + JObjRef {
            /**
            创建一个代理对象。
            `fields` 传递给struct的自定义字段。
            */
            fn new(fields: Self::Fields) -> std::sync::Arc<Self>;

            /**
            释放代理对象,解除绑定的handler。
            */
            fn release(&self) {
                unbind_proxy_handler(&self.java_ref());
            }
        }

        impl<T: JObjRef> JObjRef for &T {
            fn java_ref(&self) -> GlobalRef {
                self.java_ref()
            }
        }

        impl<T: JObjNew> JObjNew for &T {
            type Fields = T::Fields;

            fn _new(this: &GlobalRef, fields: Self::Fields) -> Self {
                panic!("Reference types cannot be constructed.")
            }
        }

        impl<T: JObjRef + JObjNew> JObjRef for Option<T>
        where
            <T as JObjNew>::Fields: Default,
        {
            fn java_ref(&self) -> GlobalRef {
                match self {
                    None => T::null().java_ref(),
                    Some(v) => v.java_ref(),
                }
            }
        }

        impl<T: JObjRef + JObjNew> JObjNew for Option<T> {
            type Fields = T::Fields;

            fn _new(this: &GlobalRef, fields: Self::Fields) -> Self {
                match this.is_null() {
                    true => None,
                    false => Some(T::_new(this, fields)),
                }
            }
        }

        impl<T: JType> JType for Arc<T> {
            const CLASS: &'static str = T::CLASS;
            type Error = T::Error;
            const OBJECT_SIG: &'static str = T::OBJECT_SIG;
        }

        impl<T: JObjRef> JObjRef for Arc<T> {
            fn java_ref(&self) -> GlobalRef {
                self.as_ref().java_ref()
            }
        }

        impl<T: JObjNew> JObjNew for Arc<T> {
            type Fields = T::Fields;

            fn _new(this: &GlobalRef, fields: Self::Fields) -> Self {
                T::_new(this, fields).into()
            }
        }

        impl<T: JType> JType for Rc<T> {
            const CLASS: &'static str = T::CLASS;
            type Error = T::Error;
            const OBJECT_SIG: &'static str = T::OBJECT_SIG;
        }

        impl<T: JObjRef> JObjRef for Rc<T> {
            fn java_ref(&self) -> GlobalRef {
                self.as_ref().java_ref()
            }
        }

        impl<T: JObjNew> JObjNew for Rc<T> {
            type Fields = T::Fields;

            fn _new(this: &GlobalRef, fields: Self::Fields) -> Self {
                T::_new(this, fields).into()
            }
        }

        impl<T: JType> JType for Mutex<T> {
            const CLASS: &'static str = T::CLASS;
            type Error = T::Error;
            const OBJECT_SIG: &'static str = T::OBJECT_SIG;
        }

        impl<T: JObjNew> JObjNew for Mutex<T> {
            type Fields = T::Fields;

            fn _new(this: &GlobalRef, fields: Self::Fields) -> Self {
                T::_new(this, fields).into()
            }
        }

        impl<T: JObjRef> JObjRef for Mutex<T> {
            fn java_ref(&self) -> GlobalRef {
                self.java_ref()
            }
        }
    };
}

/**
获取android系统的java虚拟机。
*/
pub fn android_vm<'a>() -> JavaVM {
    let ctx = ndk_context::android_context();
    unsafe { JavaVM::from_raw(ctx.vm().cast()) }.unwrap()
}

/// 获取vm,将vm附加到当前线程,随后操作java虚拟机。
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::vm_attach;
/// vm_attach!(_ env);
/// // or: vm_attach!(mut env);
/// let class = env.find_class("java/lang/String");
/// dbg!(class);
/// ```
#[macro_export]
macro_rules! vm_attach {
    (mut $var:ident) => {
        let vm = $crate::android_vm();
        let mut $var = vm.attach_current_thread().unwrap();
    };
    (_ $var:ident) => {
        let vm = android_vm();
        let $var = vm.attach_current_thread().unwrap()
    };
}

/**
获取安卓的Context对象,这通常是NativeActivity对象的引用。
*/
pub fn android_context<'a>() -> JObject<'a> {
    let ctx = ndk_context::android_context();
    unsafe { JObject::from_raw(ctx.context().cast()) }
}

/// 创建一个java动态代理,用于在rust层实现java接口的方法。
///
/// # Arguments
///
/// * `interfaces`: 要实现的java接口。
/// * `handler`: 代理的处理函数。
///
/// returns: GlobalRef 代理对象
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::new_proxy;
/// let proxy = new_proxy(&["java.lang.Runnable"]);
/// ```
//noinspection SpellCheckingInspection
pub fn new_proxy(interfaces: &[&str]) -> GlobalRef {
    let class = load_rust_call_method_hook_class();
    vm_attach!(mut env);
    let obj = env.new_object(class, "()V", &[]).unwrap();
    let faces = env
        .new_object_array(
            interfaces.len() as jsize,
            "java/lang/Class",
            &JObject::null(),
        )
        .unwrap();
    for i in 0..interfaces.len() {
        let class = env.new_string(interfaces[i]).unwrap();
        let face = env
            .call_static_method(
                "java/lang/Class",
                "forName",
                "(Ljava/lang/String;)Ljava/lang/Class;",
                &[(&class).into()],
            )
            .unwrap()
            .l()
            .unwrap();
        env.set_object_array_element(&faces, i as jsize, &face)
            .unwrap();
    }
    let hash_code = env
        .call_method(&obj, "hashCode", "()I", &[])
        .unwrap()
        .i()
        .unwrap();
    let res = env.call_static_method(
        "java/lang/reflect/Proxy",
        "newProxyInstance",
        "(Ljava/lang/ClassLoader;[Ljava/lang/Class;Ljava/lang/reflect/InvocationHandler;)Ljava/lang/Object;",
        &[
            (&JObject::null()).into(),
            (&faces).into(),
            (&obj).into()
        ]
    ).unwrap()
        .l()
        .unwrap();
    let res = env.new_global_ref(&res).unwrap();
    let lock = HOOK_OBJECTS_OTHER.lock();
    let mut hasher = DefaultHasher::new();
    res.hash(&mut hasher);
    lock.borrow_mut().insert(hasher.finish(), hash_code);
    drop(lock);
    res
}

//noinspection SpellCheckingInspection
/// java动态代理绑定rust函数。
///
/// # Arguments
///
/// * `proxy`: 代理对象。
/// * `handler`: 一个处理函数。
///
/// returns: ()
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{bind_proxy_handler, new_proxy, vm_attach};
/// let proxy = new_proxy(&["java.lang.Runnable"]);
/// bind_proxy_handler(&proxy, |mut env, method, args| {
///     let name = env.call_method(&method, "getName", "()Ljava/lang/String;", &[]).unwrap().l().unwrap();
///     let name = env.get_string((&name).into()).unwrap();
///     println!("Method `{}` is called with proxy.", name.to_str().unwrap());
///     droid_wrap_utils::null_value(env)
/// });
/// vm_attach!(mut env);
/// env.call_method(&proxy, "run", "()V", &[]).unwrap();
/// ```
pub fn bind_proxy_handler(
    proxy: &GlobalRef,
    handler: impl Fn(&mut JNIEnv<'_>, &JObject<'_>, &JObjectArray<'_>) -> GlobalRef
        + Send
        + Sync
        + 'static,
) {
    let hash_code = get_proxy_hash_code(proxy);
    let lock = match HOOK_OBJECTS.try_lock() {
        Some(lock) => lock,
        None => {
            error!("Can't bind proxy handler.");
            return;
        }
    };
    lock.borrow_mut().insert(hash_code, Arc::new(handler));
}

/// 获取java代理对象的哈希值。
///
/// # Arguments
///
/// * `proxy`: 代理对象。
///
/// returns: i32
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{new_proxy, get_proxy_hash_code};
/// let proxy = new_proxy(&["java.lang.Runnable"]);
/// let hash_code = get_proxy_hash_code(&proxy);
/// ```
pub fn get_proxy_hash_code(proxy: &GlobalRef) -> i32 {
    let mut hasher = DefaultHasher::new();
    proxy.hash(&mut hasher);
    let proxy_hash_code = hasher.finish();
    let lock = HOOK_OBJECTS_OTHER.lock();
    if let Some(code) = lock.borrow().get(&proxy_hash_code) {
        return *code;
    };
    0
}

//noinspection SpellCheckingInspection
/// 删除java动态代理绑定的rust函数。
///
/// # Arguments
///
/// * `proxy`: Java 代理对象引用。
///
/// returns: ()
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{unbind_proxy_handler, new_proxy};
/// let proxy = new_proxy(&["java.lang.Runnable"]);
/// unbind_proxy_handler(&proxy);
/// ```
pub fn unbind_proxy_handler(proxy: &GlobalRef) {
    let mut hasher = DefaultHasher::new();
    proxy.hash(&mut hasher);
    let proxy_hash_code = hasher.finish();
    let lock = HOOK_OBJECTS_OTHER.lock();
    if let Some(code) = lock.borrow().get(&proxy_hash_code) {
        let lock = HOOK_OBJECTS.lock();
        lock.borrow_mut().remove_entry(code);
        debug!("Proxy `{}` is dropped.", proxy_hash_code);
    };
}

/**
解析JObject类型。
*/
pub trait ParseJObjectType<T: FromStr> {
    fn parse(&self, env: &mut JNIEnv) -> T
    where
        <T as FromStr>::Err: Debug;
}

impl<T: FromStr> ParseJObjectType<T> for JObject<'_> {
    //noinspection SpellCheckingInspection
    fn parse(&self, env: &mut JNIEnv) -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let s = env
            .call_method(self, "toString", "()Ljava/lang/String;", &[])
            .unwrap()
            .l()
            .unwrap();
        let s = env.get_string((&s).into()).unwrap();
        s.to_str().unwrap().parse().unwrap()
    }
}

//noinspection SpellCheckingInspection
fn load_rust_call_method_hook_class<'a>() -> &'a GlobalRef {
    #[cfg(target_os = "android")]
    const BYTECODE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/classes.dex"));
    #[cfg(not(target_os = "android"))]
    const BYTECODE: &[u8] = &[];
    const LOADER_CLASS: &str = "dalvik/system/InMemoryDexClassLoader";
    static INSTANCE: OnceLock<GlobalRef> = OnceLock::new();

    INSTANCE.get_or_init(|| {
        vm_attach!(mut env);
        let byte_buffer = unsafe { env.new_direct_byte_buffer(BYTECODE.as_ptr() as *mut u8, BYTECODE.len()) }.unwrap();

        let dex_class_loader = env
            .new_object(
                LOADER_CLASS,
                "(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V",
                &[
                    JValueGen::Object(&JObject::from(byte_buffer)),
                    JValueGen::Object(&JObject::null()),
                ],
            ).unwrap();

        let class = env.new_string("rust/CallMethodHook").unwrap();
        let class = env
            .call_method(
                &dex_class_loader,
                "loadClass",
                "(Ljava/lang/String;)Ljava/lang/Class;",
                &[(&class).into()],
            )
            .unwrap()
            .l()
            .unwrap();
        let m = NativeMethod {
            name: "invoke".into(),
            sig: "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;".into(),
            fn_ptr: rust_callback as *mut _,
        };
        env.register_native_methods(Into::<&JClass<'_>>::into(&class), &[m]).unwrap();

        env.new_global_ref(&class).unwrap()
    })
}

//noinspection SpellCheckingInspection
unsafe extern "C" fn rust_callback<'a>(
    mut env: JNIEnv<'a>,
    this: JObject<'a>,
    _: JObject<'a>,
    method: JObject<'a>,
    args: JObjectArray<'a>,
) -> JObject<'a> {
    let hash_code = env
        .call_method(&this, "hashCode", "()I", &[])
        .unwrap()
        .i()
        .unwrap_or(0);

    let name = match env.call_method(&method, "getName", "()Ljava/lang/String;", &[]) {
        Ok(name) => name.l(),
        Err(e) => {
            error!("{}", e);
            return JObject::null();
        }
    };
    let name = match name {
        Ok(name) => name,
        Err(e) => {
            error!("{}", e);
            return JObject::null();
        }
    };
    let name = match env.get_string((&name).into()) {
        Ok(name) => name,
        Err(e) => {
            error!("{}", e);
            return JObject::null();
        }
    };
    match name.to_str() {
        Ok(name) => match name {
            "toString" => {
                return env
                    .new_string(format!("Proxy@{:x}", hash_code).as_str())
                    .unwrap()
                    .into()
            }
            "equals" | "hashCode" => {
                return env
                    .call_method(
                        &method,
                        "invoke",
                        "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;",
                        &[(&this).into(), (&args).into()],
                    )
                    .unwrap()
                    .l()
                    .unwrap()
            }
            _ => (),
        },
        Err(e) => {
            error!("{}", e);
            return JObject::null();
        }
    }

    let lock = HOOK_OBJECTS.lock();
    let func = if let Some(f) = lock.borrow().get(&hash_code) {
        f.to_owned()
    } else {
        warn!(
            "The method call has reached, but it appears that the proxy object has been dropped."
        );
        return JObject::null();
    };
    drop(lock);
    let ret = func(&mut env, &method, &args);
    env.new_local_ref(ret.as_obj()).unwrap()
}

/// 把java对象数组转换成Vec
///
/// # Arguments
///
/// * `env`: java环境
/// * `arr`: java数组
///
/// returns: Vec<JObject, Global>
///
/// # Examples
///
/// ```
/// use jni::objects::JObjectArray;
/// use droid_wrap_utils::{to_vec, vm_attach};
/// vm_attach!(mut env);
/// unsafe {
///     let arr=JObjectArray::from_raw(0 as _);
///     to_vec(&mut env, &arr);
/// }
/// ```
pub fn to_vec<'a>(env: &mut JNIEnv<'a>, arr: &JObjectArray) -> Vec<JObject<'a>> {
    let Ok(size) = env.get_array_length(arr) else {
        return vec![];
    };
    let mut arr2 = Vec::with_capacity(size as usize);
    for i in 0..size {
        arr2.push(env.get_object_array_element(arr, i).unwrap());
    }
    arr2
}

/// 获取null的全局引用值。
///
/// # Arguments
///
/// * `env`: jni环境。
///
/// returns: GlobalRef
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{null_value, vm_attach};
/// vm_attach!(mut env);
/// let null_value = null_value(&mut env);
/// ```
pub fn null_value(env: &mut JNIEnv) -> GlobalRef {
    let obj = JObject::null();
    env.new_global_ref(&obj).unwrap()
}

/// 获取boolean的包装对象的全局引用值。
///
/// # Arguments
///
/// * `value`: 数据。
/// * `env`: jni环境。
///
/// returns: GlobalRef
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{vm_attach, wrapper_bool_value};
/// vm_attach!(mut env);
/// let true_value = wrapper_bool_value(true, &mut env);
/// ```
pub fn wrapper_bool_value(value: bool, env: &mut JNIEnv) -> GlobalRef {
    let obj = env
        .new_object("java/lang/Boolean", "(Z)V", &[(value as jboolean).into()])
        .unwrap();
    env.new_global_ref(&obj).unwrap()
}

/// 获取int的包装对象的全局引用值。
///
/// # Arguments
///
/// * `value`: 数据。
/// * `env`: jni环境。
///
/// returns: GlobalRef
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{vm_attach, wrapper_integer_value};
/// vm_attach!(mut env);
/// let zero_value = wrapper_integer_value(0, &mut env);
/// ```
pub fn wrapper_integer_value(value: i32, env: &mut JNIEnv) -> GlobalRef {
    let obj = env
        .new_object("java/lang/Integer", "(I)V", &[value.into()])
        .unwrap();
    env.new_global_ref(&obj).unwrap()
}

/// 获取long的包装对象的全局引用值。
///
/// # Arguments
///
/// * `value`: 数据。
/// * `env`: jni环境。
///
/// returns: GlobalRef
///
/// # Examples
///
/// ```
/// use droid_wrap_utils::{vm_attach, wrapper_long_value};
/// vm_attach!(mut env);
/// let zero_value = wrapper_long_value(0, &mut env);
/// ```
pub fn wrapper_long_value(value: i64, env: &mut JNIEnv) -> GlobalRef {
    let obj = env
        .new_object("java/lang/Long", "(J)V", &[value.into()])
        .unwrap();
    env.new_global_ref(&obj).unwrap()
}