mirror of
https://github.com/rust-mobile/android-activity.git
synced 2026-07-10 08:44:11 +00:00
7cdb77eca4
The callback given to `AndroidApp::input_events()` is now expected to return `InputStatus::Handled` or `InputStatus::Unhandled`. When running with NativeActivity then if we know an input event hasn't been handled we can notify the InputQueue which may result in fallback handling. Although the status is currently ignored with the GameActivity backend. Since this is a breaking change that also affects the current Winit backend this updates the winit based examples to stick with the 0.3 release of android-activity for now. Fixes: #31
84 lines
3.3 KiB
Rust
84 lines
3.3 KiB
Rust
use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent};
|
|
use log::info;
|
|
|
|
#[no_mangle]
|
|
fn android_main(app: AndroidApp) {
|
|
android_logger::init_once(android_logger::Config::default().with_min_level(log::Level::Info));
|
|
|
|
let mut quit = false;
|
|
let mut redraw_pending = true;
|
|
let mut render_state: Option<()> = Default::default();
|
|
|
|
while !quit {
|
|
app.poll_events(
|
|
Some(std::time::Duration::from_secs(1)), /* timeout */
|
|
|event| {
|
|
match event {
|
|
PollEvent::Wake => {
|
|
info!("Early wake up");
|
|
}
|
|
PollEvent::Timeout => {
|
|
info!("Timed out");
|
|
// Real app would probably rely on vblank sync via graphics API...
|
|
redraw_pending = true;
|
|
}
|
|
PollEvent::Main(main_event) => {
|
|
info!("Main event: {:?}", main_event);
|
|
match main_event {
|
|
MainEvent::SaveState { saver, .. } => {
|
|
saver.store("foo://bar".as_bytes());
|
|
}
|
|
MainEvent::Pause => {}
|
|
MainEvent::Resume { loader, .. } => {
|
|
if let Some(state) = loader.load() {
|
|
if let Ok(uri) = String::from_utf8(state) {
|
|
info!("Resumed with saved state = {uri:#?}");
|
|
}
|
|
}
|
|
}
|
|
MainEvent::InitWindow { .. } => {
|
|
render_state = Some(());
|
|
redraw_pending = true;
|
|
}
|
|
MainEvent::TerminateWindow { .. } => {
|
|
render_state = None;
|
|
}
|
|
MainEvent::WindowResized { .. } => {
|
|
redraw_pending = true;
|
|
}
|
|
MainEvent::RedrawNeeded { .. } => {
|
|
redraw_pending = true;
|
|
}
|
|
MainEvent::InputAvailable { .. } => {
|
|
redraw_pending = true;
|
|
}
|
|
MainEvent::ConfigChanged { .. } => {
|
|
info!("Config Changed: {:#?}", app.config());
|
|
}
|
|
MainEvent::LowMemory => {}
|
|
|
|
MainEvent::Destroy => quit = true,
|
|
_ => { /* ... */ }
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
if redraw_pending {
|
|
if let Some(_rs) = render_state {
|
|
redraw_pending = false;
|
|
|
|
// Handle input
|
|
app.input_events(|event| {
|
|
info!("Input Event: {event:?}");
|
|
InputStatus::Unhandled
|
|
});
|
|
|
|
info!("Render...");
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|