Skip to main content

ferroui_android/
lib.rs

1//! The Android platform.
2//!
3//! An application is a shared library that the application class of the
4//! Java layer of this crate loads (`java/org/ferroui/android`). There are
5//! no windows: content is shown by a [`FerroView`] in an activity
6//! ([`FerroActivity`]; the main activity shows the main view of the
7//! [`ApplicationLifetime`]). The system drives everything: the dispatcher
8//! is the main looper, rendering follows the choreographer on a render
9//! thread, and the surface of the view is drawn through EGL or into the
10//! buffer of its native window.
11//!
12//! [`interop`] is the only place that crosses to Java and to the NDK. The
13//! parts that call the system exist on Android only; on another system the
14//! crate is the logic of the backend, which its tests run there. The design
15//! is in `docs/porting/android-platform.md`.
16
17// On another system than Android the logic is compiled for its tests, and nothing of the crate
18// calls it there.
19#![cfg_attr(not(target_os = "android"), allow(dead_code))]
20
21pub mod automation;
22pub mod interop;
23pub mod log;
24pub mod platform;
25
26mod android_platform;
27mod android_runtime_platform;
28mod application_lifetime;
29mod cursor_factory;
30mod explore_by_touch_helper;
31mod ferro_access_helper;
32mod i_activity_result_handler;
33mod i_android_navigation_service;
34mod i_ferro_activity;
35mod i_init_editor_info;
36mod platform_icon_loader;
37mod stubs;
38
39#[cfg(target_os = "android")]
40mod android_dispatcher_impl;
41#[cfg(target_os = "android")]
42mod android_egl;
43#[cfg(target_os = "android")]
44mod android_view_control_handle;
45#[cfg(target_os = "android")]
46mod back_pressed_callback;
47#[cfg(target_os = "android")]
48mod choreographer_timer;
49#[cfg(target_os = "android")]
50mod ferro_activity;
51#[cfg(target_os = "android")]
52mod ferro_android_application;
53#[cfg(target_os = "android")]
54mod ferro_main_activity;
55#[cfg(target_os = "android")]
56mod ferro_view;
57#[cfg(target_os = "android")]
58mod ferro_view_input;
59
60pub use android_platform::{AndroidPlatformOptions, AndroidRenderingMode};
61pub use application_lifetime::ApplicationLifetime;
62pub use cursor_factory::CursorFactory;
63pub use i_activity_result_handler::{
64    ActivityResultHandler, IActivityResultHandler, Intent, RequestPermissionsResultHandler, PERMISSION_DENIED,
65    PERMISSION_GRANTED, RESULT_CANCELED, RESULT_FIRST_USER, RESULT_OK,
66};
67pub use i_android_navigation_service::{AndroidBackRequestedEventArgs, IActivityNavigationService};
68pub use i_ferro_activity::IFerroActivity;
69pub use platform_icon_loader::PlatformIconLoader;
70pub use stubs::{PlatformIconLoaderStub, WindowingPlatformStub};
71
72#[cfg(target_os = "android")]
73pub use android_dispatcher_impl::AndroidDispatcherImpl;
74#[cfg(target_os = "android")]
75pub use android_platform::{AndroidApplicationExtensions, AndroidPlatform};
76#[cfg(target_os = "android")]
77pub use android_runtime_platform::{AndroidRuntimePlatform, AndroidRuntimePlatformServices};
78#[cfg(target_os = "android")]
79pub use android_view_control_handle::AndroidViewControlHandle;
80#[cfg(target_os = "android")]
81pub use choreographer_timer::ChoreographerTimer;
82#[cfg(target_os = "android")]
83pub use ferro_activity::FerroActivity;
84#[cfg(target_os = "android")]
85pub use ferro_android_application::FerroAndroidApplication;
86#[cfg(target_os = "android")]
87pub use ferro_main_activity::FerroMainActivity;
88#[cfg(target_os = "android")]
89pub use ferro_view::FerroView;
90#[cfg(target_os = "android")]
91pub use interop::natives::on_load;
92
93/// Makes a shared library the native side of an Android application.
94///
95/// `$build` is a function that returns the application builder
96/// (`fn() -> AppBuilder`), usually
97/// `AppBuilder::configure::<App>().use_android()`: what an application
98/// overrides `CreateAppBuilder` and `CustomizeAppBuilder` for in the
99/// reference. The macro writes the one symbol the library exports,
100/// `JNI_OnLoad`, which the virtual machine calls when the application class
101/// of the Java layer loads the library.
102#[macro_export]
103macro_rules! android_application {
104    ($build:expr) => {
105        /// Called by the Java virtual machine when the library is loaded.
106        ///
107        /// # Safety
108        /// Called by the virtual machine only, with its own pointer.
109        #[cfg(target_os = "android")]
110        #[no_mangle]
111        pub unsafe extern "system" fn JNI_OnLoad(
112            vm: *mut ::std::ffi::c_void,
113            _reserved: *mut ::std::ffi::c_void,
114        ) -> i32 {
115            // SAFETY: `vm` is the pointer the virtual machine passes to this function.
116            unsafe { $crate::on_load(vm, $build) }
117        }
118    };
119}