use super::types::{AndroidHandleKind, AndroidPlatform};
use crate::core::PlatformFamily;
use crate::platform::{DropEvent, Platform, WidgetTriggerEvent, WidgetTriggerKind};
use std::sync::atomic::Ordering;
use std::thread;
use std::time::Duration;
impl AndroidPlatform {}
impl Platform for AndroidPlatform {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn backend_name(&self) -> &'static str {
"android-state-backend"
}
fn family(&self) -> PlatformFamily {
PlatformFamily::Mobile
}
fn total_memory_mb(&self) -> Option<u64> {
crate::platform::os_probes::total_memory_mb()
}
fn is_on_battery(&self) -> bool {
crate::platform::os_probes::is_on_battery()
}
fn process_memory_utilization(&self) -> Option<f32> {
crate::platform::os_probes::process_memory_utilization()
}
fn process_cpu_utilization(&self) -> Option<f32> {
crate::platform::os_probes::process_cpu_utilization()
}
fn spawn_print_job(&self, _job_file: &std::path::Path) -> Result<(), String> {
Err("Android printing requires the platform print framework (not bound)".to_string())
}
#[cfg(feature = "mobile-api")]
fn mobile_extension(&self) -> Option<&dyn crate::platform::contract::MobilePlatformExtension> {
Some(self)
}
fn init(&self) {
let _ = self.android_runtime_marker();
self.runtime.initialized.store(true, Ordering::SeqCst);
}
fn run(&self) {
if !self.runtime.initialized.load(Ordering::SeqCst) {
self.init();
}
self.runtime.running.store(true, Ordering::SeqCst);
while self.runtime.running.load(Ordering::SeqCst) {
thread::sleep(Duration::from_millis(16));
}
}
fn quit(&self) {
self.runtime.running.store(false, Ordering::SeqCst);
}
fn destroy_widget(&self, widget_id: u64) -> bool {
{
let mut menus = self.menus.lock().expect("android menus lock poisoned");
menus.attached_menu_bar.remove(&widget_id);
menus.menu_children.remove(&widget_id);
for children in menus.menu_children.values_mut() {
children.retain(|child| *child != widget_id);
}
menus.pending_menu_events.retain(|queued| *queued != widget_id);
}
self.state.destroy_widget(widget_id)
}
fn create_window(&self, title: &str, x: i32, y: i32, width: u32, height: u32) -> u64 {
self.insert_widget(AndroidHandleKind::Window, title, x, y, width, height)
}
fn create_menu_bar(&self, parent: u64, x: i32, y: i32, width: u32, height: u32) -> u64 {
if !matches!(self.kind_of(parent), Some(AndroidHandleKind::Window)) {
return 0;
}
self.insert_widget(AndroidHandleKind::MenuBar, "MenuBar", x, y, width, height)
}
fn create_menu(&self, parent: u64, text: &str, x: i32, y: i32, width: u32, height: u32) -> u64 {
if !matches!(
self.kind_of(parent),
Some(AndroidHandleKind::MenuBar) | Some(AndroidHandleKind::Menu)
) {
return 0;
}
self.insert_widget(AndroidHandleKind::Menu, text, x, y, width, height)
}
fn attach_menu_bar_to_window(&self, window: u64, menu_bar: u64) -> bool {
if !matches!(self.kind_of(window), Some(AndroidHandleKind::Window)) {
return false;
}
if !matches!(self.kind_of(menu_bar), Some(AndroidHandleKind::MenuBar)) {
return false;
}
let mut menus = self.menus.lock().expect("android menus lock poisoned");
menus.attached_menu_bar.insert(window, menu_bar);
true
}
fn menu_add_item(&self, parent_menu: u64, text: &str, shortcut: Option<&str>) -> u64 {
if !matches!(self.kind_of(parent_menu), Some(AndroidHandleKind::Menu)) {
return 0;
}
let id = self.insert_widget(AndroidHandleKind::MenuItem, text, 0, 0, 0, 0);
let display_text = match shortcut {
Some(shortcut) => format!("{} ({})", text, shortcut),
None => text.to_string(),
};
self.state.set_text(id, &display_text);
let mut menus = self.menus.lock().expect("android menus lock poisoned");
menus.menu_children.entry(parent_menu).or_default().push(id);
id
}
fn poll_menu_triggered(&self) -> Option<u64> {
let mut menus = self.menus.lock().expect("android menus lock poisoned");
menus.pending_menu_events.pop_front()
}
fn inject_menu_trigger(&self, menu_item_id: u64) -> bool {
if !matches!(self.kind_of(menu_item_id), Some(AndroidHandleKind::MenuItem)) {
return false;
}
let mut menus = self.menus.lock().expect("android menus lock poisoned");
menus.pending_menu_events.push_back(menu_item_id);
true
}
fn poll_widget_triggered(&self) -> Option<u64> {
self.state.pop_widget_trigger()
}
fn poll_widget_trigger_event(&self) -> Option<WidgetTriggerEvent> {
self.state.pop_widget_trigger_event()
}
fn inject_widget_trigger_event(&self, widget_id: u64, kind: WidgetTriggerKind) -> bool {
self.state.inject_widget_trigger_event(widget_id, kind)
}
fn show_widget(&self, widget_id: u64) {
self.state.set_visible(widget_id, true);
}
fn hide_widget(&self, widget_id: u64) {
self.state.set_visible(widget_id, false);
}
fn set_widget_geometry(&self, widget_id: u64, x: i32, y: i32, width: u32, height: u32) {
self.state.set_geometry(widget_id, x, y, width, height);
}
fn set_widget_text(&self, widget_id: u64, text: &str) {
self.state.set_text(widget_id, text);
}
fn get_widget_text(&self, widget_id: u64) -> String {
self.state.text(widget_id)
}
fn set_widget_enabled(&self, widget_id: u64, enabled: bool) {
self.state.set_enabled(widget_id, enabled);
}
fn is_widget_enabled(&self, widget_id: u64) -> bool {
self.state.enabled(widget_id)
}
fn set_widget_visible(&self, widget_id: u64, visible: bool) {
self.state.set_visible(widget_id, visible);
}
fn is_widget_visible(&self, widget_id: u64) -> bool {
self.state.visible(widget_id)
}
fn set_clipboard_text(&self, text: &str) -> bool {
self.state.set_clipboard_text(text)
}
fn get_clipboard_text(&self) -> String {
self.state.clipboard_text()
}
fn begin_drag(&self, source_widget_id: u64, mime: &str, payload: &[u8]) -> bool {
self.state.begin_drag(source_widget_id, mime, payload)
}
fn poll_drop_event(&self) -> Option<DropEvent> {
self.state.pop_drop_event()
}
fn inject_drop_event(&self, event: DropEvent) -> bool {
self.state.inject_drop_event(event)
}
fn set_widget_ime_enabled(&self, widget_id: u64, enabled: bool) -> bool {
self.state.set_ime_enabled(widget_id, enabled)
}
fn is_widget_ime_enabled(&self, widget_id: u64) -> bool {
self.state.ime_enabled(widget_id)
}
fn set_widget_accessibility_name(&self, widget_id: u64, name: &str) -> bool {
self.state.set_accessibility_name(widget_id, name)
}
fn get_widget_accessibility_name(&self, widget_id: u64) -> String {
self.state.accessibility_name(widget_id)
}
}
impl crate::platform::contract::MobilePlatformExtension for AndroidPlatform {
fn mobile_backend(&self) -> crate::platform::MobileBackend {
crate::platform::MobileBackend::Android
}
fn attach_to_native_view(&self, native_handle: usize) -> bool {
#[cfg(feature = "android-jni")]
{
if native_handle == 0 || !crate::platform::android_jni::is_initialized() {
return false;
}
let context_obj: jni::objects::JObject<'_> =
unsafe { jni::objects::JObject::from_raw(native_handle as jni::sys::jobject) };
crate::platform::android_jni::with_jni_env(|env| {
crate::platform::android_jni::set_activity_context(env, &context_obj)
})
.unwrap_or(false)
}
#[cfg(not(feature = "android-jni"))]
{
let _ = native_handle;
false
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn android_platform_window_creation() {
let platform = AndroidPlatform::new();
platform.init();
let window_id = platform.create_window("Test Window", 0, 0, 320, 568);
assert_ne!(window_id, 0);
assert_eq!(platform.backend_name(), "android-state-backend");
assert_eq!(platform.family(), PlatformFamily::Mobile);
}
#[test]
fn control_members_report_absence_for_every_parent() {
let platform = AndroidPlatform::new();
platform.init();
assert_eq!(platform.create_button(999, "Button", 0, 0, 80, 44), 0);
let window_id = platform.create_window("Window", 0, 0, 320, 568);
assert_ne!(window_id, 0);
assert_eq!(platform.create_button(window_id, "Button", 0, 0, 80, 44), 0);
}
#[test]
fn combo_box_data_members_report_absence() {
let platform = AndroidPlatform::new();
platform.init();
let window_id = platform.create_window("Window", 0, 0, 320, 568);
assert_eq!(platform.create_combo_box(window_id, 0, 0, 200, 40), 0);
assert!(!platform.combo_box_add_item(window_id, "Item 1"));
assert_eq!(platform.combo_box_item_count(window_id), 0);
assert!(!platform.combo_box_clear_items(window_id));
}
#[test]
fn android_menu_requires_correct_parent_kind() {
let platform = AndroidPlatform::new();
platform.init();
let window = platform.create_window("Window", 0, 0, 320, 568);
let button = platform.create_button(window, "Button", 0, 0, 80, 44);
assert_eq!(platform.create_menu_bar(button, 0, 0, 320, 24), 0);
let menu_bar = platform.create_menu_bar(window, 0, 0, 320, 24);
assert_ne!(menu_bar, 0);
assert_eq!(platform.create_menu(window, "Bad", 0, 0, 80, 24), 0);
assert_eq!(platform.create_menu(button, "Bad", 0, 0, 80, 24), 0);
let menu = platform.create_menu(menu_bar, "File", 0, 0, 80, 24);
assert_ne!(menu, 0);
let submenu = platform.create_menu(menu, "Recent", 0, 0, 80, 24);
assert_ne!(submenu, 0, "a menu may nest under another menu");
assert_eq!(platform.menu_add_item(window, "Bad", None), 0);
assert_eq!(platform.menu_add_item(menu_bar, "Bad", None), 0);
let item = platform.menu_add_item(menu, "Open", Some("Ctrl+O"));
assert_ne!(item, 0);
assert!(!platform.inject_menu_trigger(window));
assert!(!platform.inject_menu_trigger(menu_bar));
assert!(platform.inject_menu_trigger(item));
assert_eq!(platform.poll_menu_triggered(), Some(item));
assert!(!platform.attach_menu_bar_to_window(9999, menu_bar));
assert!(!platform.attach_menu_bar_to_window(window, item));
assert!(platform.attach_menu_bar_to_window(window, menu_bar));
}
#[test]
fn android_menu_item_retains_shortcut() {
let platform = AndroidPlatform::new();
platform.init();
let window = platform.create_window("Window", 0, 0, 320, 568);
let menu_bar = platform.create_menu_bar(window, 0, 0, 320, 24);
let menu = platform.create_menu(menu_bar, "File", 0, 0, 80, 24);
let plain = platform.menu_add_item(menu, "Open", None);
assert_eq!(platform.get_widget_text(plain), "Open");
let with_shortcut = platform.menu_add_item(menu, "Save", Some("Ctrl+S"));
assert_eq!(platform.get_widget_text(with_shortcut), "Save (Ctrl+S)");
}
}