Skip to content

Commit ff48457

Browse files
committed
Android: rework backend to use android_activity crate
This updates the Android backend to use the android_activity create instead of ndk-glue. This solves a few issues: 1. The backend is agnostic of the application's choice of Activity base class 2. Winit is no longer responsible for handling any Java synchronization details, since these are encapsulated by the design of android_activity 3. The backend no longer depends on global / static getters for state such as the native_window() which puts it in a better position to support running multiple activities within a single Android process. 4. Redraw requests are flagged, not queued, in a way that avoids taking priority over user events (resolves rust-windowing#2299) Addresses: PR rust-windowing#1892 Addresses: PR rust-windowing#2307 Addresses: PR rust-windowing#2343 Addresses: rust-windowing#2293 Resolves: rust-windowing#2299
1 parent 2e4338b commit ff48457

File tree

4 files changed

+636
-401
lines changed

4 files changed

+636
-401
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ simple_logger = "2.1.0"
5858
[target.'cfg(target_os = "android")'.dependencies]
5959
# Coordinate the next winit release with android-ndk-rs: https://github.com/rust-windowing/winit/issues/1995
6060
ndk = "0.7.0"
61-
ndk-glue = "0.7.0"
61+
android-activity = { git = "https://github.com/rib/android-activity" }
6262

6363
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
6464
objc = "0.2.7"

src/event_loop.rs

+3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ impl<T> EventLoopBuilder<T> {
9797
/// `WINIT_UNIX_BACKEND`. Legal values are `x11` and `wayland`.
9898
/// If it is not set, winit will try to connect to a Wayland connection, and if that fails,
9999
/// will fall back on X11. If this variable is set with any other value, winit will panic.
100+
/// - **Android:** Must be configured with an `AndroidApp` from `android_main()` by calling
101+
/// [`.with_android_app(app)`] before calling `.build()`.
100102
///
103+
/// [`.with_android_app(app)`]: crate::platform::android::EventLoopBuilderExtAndroid::with_android_app
101104
/// [`platform`]: crate::platform
102105
#[inline]
103106
pub fn build(&mut self) -> EventLoop<T> {

src/platform/android.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![cfg(any(target_os = "android"))]
22

33
use crate::{
4-
event_loop::{EventLoop, EventLoopWindowTarget},
4+
event_loop::{EventLoop, EventLoopBuilder, EventLoopWindowTarget},
55
window::{Window, WindowBuilder},
66
};
7-
use ndk::configuration::Configuration;
8-
use ndk_glue::Rect;
7+
8+
use android_activity::{AndroidApp, ConfigurationRef, Rect};
99

1010
/// Additional methods on [`EventLoop`] that are specific to Android.
1111
pub trait EventLoopExtAndroid {}
@@ -19,15 +19,15 @@ pub trait EventLoopWindowTargetExtAndroid {}
1919
pub trait WindowExtAndroid {
2020
fn content_rect(&self) -> Rect;
2121

22-
fn config(&self) -> Configuration;
22+
fn config(&self) -> ConfigurationRef;
2323
}
2424

2525
impl WindowExtAndroid for Window {
2626
fn content_rect(&self) -> Rect {
2727
self.window.content_rect()
2828
}
2929

30-
fn config(&self) -> Configuration {
30+
fn config(&self) -> ConfigurationRef {
3131
self.window.config()
3232
}
3333
}
@@ -38,3 +38,17 @@ impl<T> EventLoopWindowTargetExtAndroid for EventLoopWindowTarget<T> {}
3838
pub trait WindowBuilderExtAndroid {}
3939

4040
impl WindowBuilderExtAndroid for WindowBuilder {}
41+
42+
pub trait EventLoopBuilderExtAndroid {
43+
/// Associates the `AndroidApp` that was passed to `android_main()` with the event loop
44+
///
45+
/// This must be called on Android since the `AndroidApp` is not global state.
46+
fn with_android_app(&mut self, app: AndroidApp) -> &mut Self;
47+
}
48+
49+
impl<T> EventLoopBuilderExtAndroid for EventLoopBuilder<T> {
50+
fn with_android_app(&mut self, app: AndroidApp) -> &mut Self {
51+
self.platform_specific.android_app = Some(app);
52+
self
53+
}
54+
}

0 commit comments

Comments
 (0)