proxy_watch/android.rs
1//! Android: hand the crate the host's `JavaVM` and a `Context`.
2//!
3//! Every read goes through the Java framework, so the crate needs the process's `JavaVM`
4//! and a `Context`. A host built on `android-activity` registers both with `ndk-context`,
5//! which the crate reads without further setup, and so does `tao` from 0.36 (Tauri 2.12):
6//! it registers the application `Context` in the Activity's `onCreate`, before any app
7//! code runs. `tao` before 0.36 registers neither; such a host calls [`init`] once,
8//! before the first [`read`](crate::read) or
9//! [`ProxyWatcher`](crate::ProxyWatcher). Before then, in a host that registered nothing,
10//! a read returns [`Error::Io`](crate::Error::Io) after the panic hook reports
11//! `ndk-context`'s panic, and a build with `panic = "abort"` aborts the process there.
12//!
13//! `tao` keeps both for each Activity it creates, and `main_android_context` hands them
14//! out, so a Tauri app makes the call in `setup`, with no Kotlin of its own. On `tao` 0.36
15//! and later the call is redundant and harmless; `init` wins, and it keeps the same
16//! application `Context`:
17//!
18//! ```ignore
19//! tauri::Builder::default().setup(|_app| {
20//! #[cfg(target_os = "android")]
21//! if let Some(ctx) = tauri::tao::platform::android::prelude::main_android_context() {
22//! // SAFETY: tao holds a global reference to the Activity while it exists.
23//! unsafe { proxy_watch::android::init(ctx.java_vm, ctx.context_jobject) }?;
24//! }
25//! Ok(())
26//! })
27//! ```
28//!
29//! Elsewhere, `JNI_OnLoad` receives the `JavaVM` but no `Context`, so the call usually sits
30//! in a JNI function the app's Kotlin or Java side calls with one, which reaches the
31//! `JavaVM` through its `JNIEnv`.
32
33pub use crate::sys::android::init;