wingy_android/lib.rs
1//! # Android
2//!
3//! The Android backend builds on (and exposes types from) the [`ndk`](https://docs.rs/ndk/) crate.
4//!
5//! Native Android applications need some form of "glue" crate that is responsible
6//! for defining the main entry point for your Rust application as well as tracking
7//! various life-cycle events and synchronizing with the main JVM thread.
8//!
9//! Wingy uses the [android-activity](https://docs.rs/android-activity/) as a
10//! glue crate (prior to `0.28` it used
11//! [ndk-glue](https://github.com/rust-windowing/android-ndk-rs/tree/master/ndk-glue)).
12//!
13//! The version of the glue crate that your application depends on _must_ match the
14//! version that Wingy depends on because the glue crate is responsible for your
15//! application's main entry point. If Cargo resolves multiple versions, they will
16//! clash.
17//!
18//! `wingy` glue compatibility table:
19//!
20//! | wingy | ndk-glue |
21//! | :---: | :--------------------------: |
22//! | 0.30 | `android-activity = "0.6"` |
23//! | 0.29 | `android-activity = "0.5"` |
24//! | 0.28 | `android-activity = "0.4"` |
25//! | 0.27 | `ndk-glue = "0.7"` |
26//! | 0.26 | `ndk-glue = "0.5"` |
27//! | 0.25 | `ndk-glue = "0.3"` |
28//! | 0.24 | `ndk-glue = "0.2"` |
29//!
30//! The recommended way to avoid a conflict with the glue version is to avoid explicitly
31//! depending on the `android-activity` crate, and instead consume the API that
32//! is re-exported by Wingy under `wingy::platform::android::activity::*`
33//!
34//! Running on an Android device needs a dynamic system library. Add this to Cargo.toml:
35//!
36//! ```toml
37//! [lib]
38//! name = "main"
39//! crate-type = ["cdylib"]
40//! ```
41//!
42//! All Android applications are based on an `Activity` subclass, and the
43//! `android-activity` crate is designed to support different choices for this base
44//! class. Your application _must_ specify the base class it needs via a feature flag:
45//!
46//! | Base Class | Feature Flag | Notes |
47//! | :--------------: | :---------------: | :-----: |
48//! | `NativeActivity` | `android-native-activity` | Built-in to Android - it is possible to use without compiling any Java or Kotlin code. Java or Kotlin code may be needed to subclass `NativeActivity` to access some platform features. It does not derive from the [`AndroidAppCompat`] base class.|
49//! | [`GameActivity`] | `android-game-activity` | Derives from [`AndroidAppCompat`], a defacto standard `Activity` base class that helps support a wider range of Android versions. Requires a build system that can compile Java or Kotlin and fetch Android dependencies from a [Maven repository][agdk_jetpack] (or link with an embedded [release][agdk_releases] of [`GameActivity`]) |
50//!
51//! [`GameActivity`]: https://developer.android.com/games/agdk/game-activity
52//! [`GameTextInput`]: https://developer.android.com/games/agdk/add-support-for-text-input
53//! [`AndroidAppCompat`]: https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity
54//! [agdk_jetpack]: https://developer.android.com/jetpack/androidx/releases/games
55//! [agdk_releases]: https://developer.android.com/games/agdk/download#agdk-libraries
56//! [Gradle]: https://developer.android.com/studio/build
57//!
58//! For more details, refer to these `android-activity` [example applications](https://github.com/rust-mobile/android-activity/tree/main/examples).
59//!
60//! ## Converting from `ndk-glue` to `android-activity`
61//!
62//! If your application is currently based on `NativeActivity` via the `ndk-glue` crate and building
63//! with `cargo apk`, then the minimal changes would be:
64//! 1. Remove `ndk-glue` from your `Cargo.toml`
65//! 2. Enable the `"android-native-activity"` feature for Wingy: `wingy = { version =
66//! "0.31.0-beta.3", features = [ "android-native-activity" ] }`
67//! 3. Add an `android_main` entrypoint (as above), instead of using the '`[ndk_glue::main]` proc
68//! macro from `ndk-macros` (optionally add a dependency on `android_logger` and initialize
69//! logging as above).
70//! 4. Pass a clone of the `AndroidApp` that your application receives to Wingy when building your
71//! event loop (as shown above).
72#![cfg(target_os = "android")]
73#![warn(clippy::exhaustive_enums)]
74
75mod event_loop;
76mod event_loop_lifetime;
77#[cfg(feature = "game-activity")]
78mod game_ime;
79mod keycodes;
80#[cfg(feature = "game-activity")]
81mod text_input_model;
82
83use wingy_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
84use wingy_core::window::Window as CoreWindow;
85
86use self::activity::{AndroidApp, ConfigurationRef, Rect};
87pub use crate::event_loop::{
88 ActiveEventLoop, EventLoop, EventLoopProxy, PlatformSpecificEventLoopAttributes,
89 PlatformSpecificWindowAttributes, Window,
90};
91
92/// Additional methods on [`EventLoop`] that are specific to Android.
93pub trait EventLoopExtAndroid {
94 /// Get the [`AndroidApp`] which was used to create this event loop.
95 fn android_app(&self) -> &AndroidApp;
96}
97
98/// Additional methods on [`ActiveEventLoop`] that are specific to Android.
99pub trait ActiveEventLoopExtAndroid {
100 /// Get the [`AndroidApp`] which was used to create this event loop.
101 fn android_app(&self) -> &AndroidApp;
102}
103
104/// Additional methods on [`Window`] that are specific to Android.
105pub trait WindowExtAndroid {
106 fn content_rect(&self) -> Rect;
107
108 fn config(&self) -> ConfigurationRef;
109}
110
111impl WindowExtAndroid for dyn CoreWindow + '_ {
112 fn content_rect(&self) -> Rect {
113 let window = self.cast_ref::<Window>().unwrap();
114 window.content_rect()
115 }
116
117 fn config(&self) -> ConfigurationRef {
118 let window = self.cast_ref::<Window>().unwrap();
119 window.config()
120 }
121}
122
123impl ActiveEventLoopExtAndroid for dyn CoreActiveEventLoop + '_ {
124 fn android_app(&self) -> &AndroidApp {
125 let event_loop = self.cast_ref::<ActiveEventLoop>().unwrap();
126 &event_loop.app
127 }
128}
129
130pub trait EventLoopBuilderExtAndroid {
131 /// Associates the [`AndroidApp`] that was passed to `android_main()` with the event loop
132 ///
133 /// This must be called on Android since the [`AndroidApp`] is not global state.
134 fn with_android_app(&mut self, app: AndroidApp) -> &mut Self;
135
136 /// Calling this will mark the volume keys to be manually handled by the application
137 ///
138 /// Default is to let the operating system handle the volume keys
139 fn handle_volume_keys(&mut self) -> &mut Self;
140}
141
142/// Re-export of the `android_activity` API
143///
144/// Wingy re-exports the `android_activity` API for convenience so that most
145/// applications can rely on the Wingy crate to resolve the required version of
146/// `android_activity` and avoid any chance of a conflict between Wingy and the
147/// application crate.
148///
149/// Unlike most libraries there can only be a single implementation
150/// of the `android_activity` glue crate linked with an application because
151/// it is responsible for the application's `android_main()` entry point.
152///
153/// Since Wingy depends on a specific version of `android_activity` the simplest
154/// way to avoid creating a conflict is for applications to avoid explicitly
155/// depending on the `android_activity` crate, and instead consume the API that
156/// is re-exported by Wingy.
157///
158/// For compatibility applications should then import the [`AndroidApp`] type for
159/// their `android_main(app: AndroidApp)` function like:
160/// ```rust
161/// #[cfg(target_os = "android")]
162/// use wingy::platform::android::activity::AndroidApp;
163/// ```
164pub mod activity {
165 // We enable the `"native-activity"` feature just so that we can build the
166 // docs, but it'll be very confusing for users to see the docs with that
167 // feature enabled, so we avoid inlining it so that they're forced to view
168 // it on the crate's own docs.rs page.
169 #[doc(no_inline)]
170 pub use android_activity::*;
171}