69 Commits

Author SHA1 Message Date
Robert Bragg 57b5192366 Update examples
This updates both the examples to Gradle 9 and AGP 9.1

The examples are identical, except that `na-mainloop` is based on
NativeActivity and the `agdk-mainloop` based on GameActivity.

The examples demonstrate:
- Using the `jni` API to define enough bindings to be able to send a Toast
- Using an `android_on_create` entry point for logging initialization
  and JNI initialization
- Using `AndroidApp::run_on_java_main_thread()` to send a toast from the
  Java main / UI thread
- Running an `android_main` event loop, including printing historic
  pointer samples (a new 0.6.1 feature)

The examples support two input actions:
- Lifting your finger in the top-left corner of the screen will show the
  onscreen keyboard
- Lifting your finger in the top-right corner of the screen will hide
  the onscreen keyboard

If you edit and disable `configChanges` in `AndroidManifest.xml` then
these examples can also demonstrate that `android-activity` gracefully
handles repeated `Activity` create -> run -> destroy cycles.
2026-03-23 22:37:54 +00:00
Robert Bragg 42af0cccfa examples/agdk-mainloop: pull in games-activity:4.0.0 2025-04-01 15:41:51 +01:00
Robert Bragg 5d7616e30e examples/agdk-mainloop: Use Gradle 8.4 (compatible with Java 21) 2025-04-01 15:41:51 +01:00
Marijn Suijten 6a0197c28f Upgrade to ndk-sys 0.6.0 and ndk 0.9.0
The next breaking `ndk` release puts a lot of emphasis in improving
`enum`s to finally be marked `non_exhaustive`, and carry possible future
values in `__Unknown(i32)` variants.  This removes the lossy conversions
that previously required `android-activity` to redefine its types, which
could all be removed again.

The `repr()` types have also been updated, as `enum` constants in C are
translated to `u32` by default in `bindgen` even though they're commonly
passed as `int` to every API function that consumes them.
2024-04-26 16:36:38 +01:00
Robert Bragg a7dc90d9bb Release 0.5.0 2023-10-17 00:03:48 +01:00
Robert Bragg 6af4d61227 Remove redundant examples/na-mainloop/.idea directory 2023-10-17 00:03:48 +01:00
Robert Bragg 47a073f702 Merge pull request #113 from MarijnS95/ndk-breaking-prep
Upgrade to `ndk-sys 0.5.0-beta.0`, `ndk 0.8.0-beta.0`
2023-08-15 21:55:53 +01:00
Marijn Suijten 499d09595b Upgrade to ndk-sys 0.5.0-beta.0, ndk-0.8.0 beta.0 2023-08-15 22:50:29 +02:00
Robert Bragg 2a61f84c70 Remove examples/agdk-mainloop/.idea 2023-08-15 21:23:29 +01:00
Robert Bragg af331e3bff Rework input_events API and expose KeyCharacterMap bindings
With the way events are delivered via an `InputQueue` with
`NativeActivity` there is no direct access to the underlying KeyEvent
and MotionEvent Java objects and no `ndk` API that supports the
equivalent of `KeyEvent.getUnicodeChar()`

What `getUnicodeChar` does under the hood though is to do lookups into a
`KeyCharacterMap` for the corresponding `InputDevice` based on the
event's `key_code` and `meta_state` - which are things we can do via
some JNI bindings for `KeyCharacterMap`.

Although it's still awkward to expose an API like
`key_event.get_unicode_char()` we can instead provide an API that
lets you look up a `KeyCharacterMap` for any `device_id` and
applications can then use that for character mapping.

This approach is also more general than the `getUnicodeChar` utility
since it exposes other useful state, such as being able to check what
kind of keyboard input events are coming from (such as a full physical
keyboard vs a virtual / 'predictive' keyboard)

For consistency this exposes the same API through the game-activity
backend, even though the game-activity backend is technically able to
support unicode lookups via `getUnicodeChar` (since it has access to the
Java `KeyEvent` object).

This highlighted a need to be able to use other `AndroidApp` APIs while
processing input, which wasn't possible with the `.input_events()` API
design because the `AndroidApp` held a lock over the backend while
iterating events.

This changes `input_events()` to `input_events_iter()` which now returns
a form of lending iterator and instead of taking a callback that gets
called repeatedly by `input_events()` a similar callback is now passed
to `iter.next(callback)`.

The API isn't as ergonomic as I would have liked, considering that
lending iterators aren't a standard feature for Rust yet but also since
we still want to have the handling for each individual event go via a
callback that can report whether an event was "handled". I think the
slightly awkward ergonomics are acceptable though considering that
the API will generally be used as an implementation detail within
middleware frameworks like Winit.

Since this is the first example where we're creating non-trivial Java
bindings for an Android SDK API this adds some JNI utilities and
establishes a pattern for how we can implement a class binding.

It's an implementation detail but with how I wrote the binding I tried
to keep in mind the possibility of creating a procmacro later that would
generate some of the JNI boilerplate involved.
2023-08-07 18:36:50 +01:00
Robert Bragg d0f10a0dd9 agdk-mainloop: update for GameActivity 2.0.2 2023-08-07 16:44:07 +01:00
Robert Bragg 230035526b Update na-mainloop 2023-06-26 00:08:48 +01:00
Robert Bragg cd81420638 Update agdk-mainloop 2023-06-26 00:08:42 +01:00
Robert Bragg 47b2e279e0 Remove Cargo.lock files for examples
When splitting out the rust-android-examples we kept the agdk-mainloop
and na-mainloop examples in part so we would have some simple code we
can build as integration tests.

Since it's less likely that these will be referenced directly as
examples now, compared to those in rust-android-examples this removes
the lock files so we will instead always build against the latest semver
compatible dependencies.

Considering the simplicity of these examples, and minimal dependencies
these lock files probably weren't that worthwhile before either.
2022-12-23 01:00:57 +00:00
Robert Bragg 2e279210a3 Remove most examples
Most of the examples weren't strictly just demonstrating how to use the
android-activity API - rather they demonstrated using other libraries
in conjunction with android-activity.

Most of the examples have now been split into a standalone repository
under: https://github.com/rust-mobile/rust-android-examples

The na-mainloop and agdk-mainloop examples have been kept here since
they can be built against the local/in-tree version of android-activity
and are useful to keep for CI purposes.

This also runs `cargo update` for the na-mainloop and agdk-mainloop.
2022-12-20 19:55:10 +00:00
Robert Bragg 4669508823 examples/*-winit-wgpu: make less verbose
This updates the *-winit-wgpu examples to use `log::info!` instead of `trace!`
and sets the log level to `Info`

The examples now also print info about and Winit Window events.

This makes them more practical to use to see how Winit events
are delivered without lots of tracing spam from dependency crates.
2022-11-11 20:44:18 +00:00
Robert Bragg 649b65c0c0 Update examples
- Updates deps
- Some README updates considering the Winit backend based on
  android-activity has been merged upstream
- runs cargo fmt over examples
- Top-level Cargo.toml simply excludes "examples" instead of
  listing each sub-directory separately
2022-11-10 19:28:01 +00:00
Robert Bragg bd8cc86ec7 Adds a minimal na-winit-glutin example
This example shows how to draw a triangle with GL[ES via the Glutin
crate (the app works on desktop and Android) and on Android it
demonstrates how to re-create the applications surface state when
it is paused and resumed.

The Renderer code and some utilities are from the upstream Glutin example.
2022-10-23 00:51:32 +01:00
Robert Bragg e8ae198653 Update example deps + update for latest winit branch
The winit based examples no longer have an explicit dependency on
android-activity and they instead consume the `android-activity` API via
the Winit crate so there's no need to keep the versions synchronized.
2022-10-22 21:51:27 +01:00
Robert Bragg 8077a4b0da Update Winit-based examples for Release 0.4.0-beta.1 2022-10-11 20:19:15 +01:00
Robert Bragg b13a53f182 Update Winit-based examples for Release 0.4.0-beta 2022-09-20 03:43:34 +01:00
Robert Bragg 47afecda36 Release 0.4.0-beta 2022-09-20 02:56:05 +01:00
Robert Bragg 17f0c674c0 Don't ignore Cargo.lock files for examples 2022-09-20 00:41:21 +01:00
Robert Bragg 8ae1059aec na-openxr-wgpu: don't depend on specific version of android-activity 2022-09-20 00:32:09 +01:00
Robert Bragg 7cdb77eca4 Let applications report if they handled input events
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
2022-09-20 00:14:38 +01:00
Robert Bragg 5c876308c1 examples: update deps (including bump to cpal 0.14) 2022-09-19 15:02:26 +01:00
msiglreith e689461580 na-winit-wgpu: update shader and use native-activity instead of game-activity 2022-09-18 16:10:07 +02:00
Robert Bragg bc177292d2 Release 0.3 2022-09-16 14:07:06 +01:00
Robert Bragg d5ff06ffb2 Add OpenXR + Wgpu example
This tests being able to write an OpenXR application based on
Wgpu (using the Vulkan HAL backend) for the Oculus Quest.
2022-08-31 20:44:14 +01:00
Robert Bragg b7f01a43d9 Build fixes for Winit based examples
The latest branches for Winit (which update the Android backend to
use android-activity) now depend on the android-activity 0.2
release which broke the patching assumption for the Winit examples.
(they were patching based on the git url not crates.io)

It was also noticed that the latest android-activity-0.27 branch
isn't compatible with Egui currently because the (badly named) branch
is based on Winit master and there was recently a breaking API change
adding new window events which breaks Egui.

A note about the badly name branches has been added to the Cargo.toml
files for the Winit examples (they can't currently be fixes since
the -0.27 branch is part of a pull request).

For reference here:
- "android-activity" is based on Winit 0.27 (required for Egui compatibility)
- "android-activity-0.27" is based on Winit master

This patch adds Cargo.lock files for the Winit examples.

Fixes: #22
2022-08-31 20:43:45 +01:00
Robert Bragg f673662316 Add a minimal OpenXR Info example 2022-08-26 17:27:03 +01:00
Robert Bragg 11c3af686f examples: winit simplifications
* Since we can rely on `Resumed` events for all platforms with Winit 0.27
we can remove the alternative `NewEvents` path for initializing state.
* Removes some unnecessary abstraction for the app state.
* Bumps agdk-egui to egui 0.19 (Winit 0.27)
2022-08-24 21:23:29 +01:00
John Kåre Alsaker 58bc3f6de7 Add eframe example
Co-authored-by: Robert Bragg <robert@sixbynine.org>
2022-08-24 20:35:41 +01:00
Robert Bragg 6ea3f82fb8 agdk-egui: revert back to updated android-activity-0.27 Winit branch 2022-08-14 11:06:27 +01:00
Robert Bragg fb9a9f15c8 agdk-egui: use staging branch for winit, for CI build 2022-08-14 11:02:22 +01:00
Robert Bragg a654f72f62 Expose the application Configuration by reference
This provides an API to access `Configuration` state for the application
without having to make deep copies of the large `Configuration` struct.

This should avoid the need for Winit to create a global static copy of
the Configuration whenever it changes - and instead it can just get
a `ConfigurationRef` which will always reflect the latest config for
the application.

Fixes: #5
2022-08-14 11:02:22 +01:00
Robert Bragg b161b24ce4 Make AndroidApp Send + Sync
In Winit then we ideally want to be able to associate a cloned reference to
the AndroidApp with the `Window` and `MonitorHandle` types (since that's the
most practical way to access the native_window and config state for the
application) but for that `AndroidApp` needs to be Send + Sync
2022-08-14 11:02:22 +01:00
Robert Bragg 2a9ed44d93 agdk-cpal: Use cpal git master
I was mistaken recently, thinking that cpal's dev-dependency on ndk-glue
was somehow being inherited by the example. Actually the real issue was
that cpal 0.13.5 has a regular _dependency_ on ndk-glue, which is a much
simpler explanation for why we were seeing a crash.

This updates the example to just use the master branch of cpal, which we'll
need until there is a new release of cpal.
2022-08-14 00:16:21 +01:00
Robert Bragg 3d1b1c5cb9 Supports InputAvailable events with GameActivity
This makes a small change to the C glue code for GameActivity to send
looper wake ups when new input is received (only sending a single wake
up, until the application next handles input).

When a wake up is received and we recognise that new input is available
then an `InputAvailable` event is sent to the application - consistent
with how NativeActivity can deliver `InputAvailable` events.

This addresses a significant feature disparity between GameActivity and
NativeActivity that meant GameActivity was not practically usable for
GUI applications that wouldn't want to render continuously like a game.

Addresses #4
2022-08-13 20:26:00 +01:00
Robert Bragg ecd03edb1a Add minimal Cpal audio library example
Based on the android example that's in the cpal repo, this test plays
a 440hz sine wave and is based on GameActivity

Note: this requires a workaround branch for cpal that comments out
a dev-dependency on ndk-glue that cpal has for its android example.
This is needed because Cargo is spuriously propagating this dev
dependency outside of the cpal package (which leads to a crash)
2022-08-12 19:28:48 +01:00
Robert Bragg 4818de6709 examples: force Winit to use in-tree android-activity
This fixes build errors about not specifying either of the
"native-activity" or "game-activity" features.

The issue came about because the in-tree examples want to
build against the in-tree version of android-activity located
with a relative `path = ` but these particular examples
depend on Winit which would resolve a second implementation
of android-activity, via a github url - where Cargo will treat
them as completely different crates.
2022-08-12 02:08:44 +01:00
Robert Bragg 7a77402279 native-activity: Fix InputAvailable support
"native-activity" builds were recently broken by bb8eeb705c which
this patch fixes.

The na-mainloop example has also been updated to verify this by
reducing the fake "render" timeout and also triggering a fake
render when an InputAvailable event is received.

Fixes: #12
2022-08-12 02:08:44 +01:00
Robert Bragg 0d0c10ebb2 Runs cargo fmt across everything 2022-08-11 23:16:55 +01:00
Robert Bragg e4608b0791 agdk-winit-wgpu: update to wgpu 0.13 2022-08-11 03:37:22 +01:00
Robert Bragg ce36c38934 Update to expect 0.27 based Winit branch
While egui hasn't yet merged its Winit 0.27 support into master
agdk-egui currently points to an egui branch with Winit 0.27 support
2022-08-11 03:36:02 +01:00
Robert Bragg 73a1acb3c7 examples: bump gradle-wrapper to fetch gradle-7.5-bin.zip 2022-08-11 03:12:33 +01:00
John Kåre Alsaker 114e249cbd Fix egui build 2022-08-09 22:44:50 +02:00
John Kåre Alsaker 875952b2a9 Update Gradle to build with Java 18 2022-08-09 22:44:04 +02:00
Robert Bragg 19c989c640 agdk-mainloop: fix features declaration 2022-07-26 02:03:36 +01:00
Robert Bragg 6278b54494 agdk-winit-wgpu: fix use of android_activity
Fixes issue raised in #6 by @tschrpl
2022-07-26 02:00:24 +01:00