blinc_platform_android/
lib.rs1#![allow(unused, dead_code)]
2pub mod activity;
33pub mod assets;
34pub mod event_loop;
35pub mod input;
36pub mod jni_bridge;
37pub mod jni_utils;
38pub mod native_bridge;
39pub mod window;
40
41pub use assets::AndroidAssetLoader;
42pub use event_loop::{AndroidEventLoop, AndroidWakeProxy};
43pub use jni_utils::{get_display_density, get_display_dpi, is_dark_mode};
44pub use window::AndroidWindow;
45
46#[cfg(target_os = "android")]
48pub use native_bridge::{init_android_native_bridge, AndroidNativeBridgeAdapter};
49
50use blinc_platform::{Platform, PlatformError};
51
52pub struct AndroidPlatform {
56 #[cfg(target_os = "android")]
57 app: android_activity::AndroidApp,
58}
59
60#[cfg(target_os = "android")]
61impl AndroidPlatform {
62 pub fn with_app(app: android_activity::AndroidApp) -> Result<Self, PlatformError> {
64 Ok(Self { app })
65 }
66}
67
68impl Platform for AndroidPlatform {
69 type Window = AndroidWindow;
70 type EventLoop = AndroidEventLoop;
71
72 fn new() -> Result<Self, PlatformError> {
73 Err(PlatformError::InitFailed(
75 "AndroidPlatform must be created with AndroidPlatform::with_app()".to_string(),
76 ))
77 }
78
79 #[cfg(target_os = "android")]
80 fn create_event_loop(&self) -> Result<Self::EventLoop, PlatformError> {
81 Ok(AndroidEventLoop::new(self.app.clone()))
82 }
83
84 #[cfg(not(target_os = "android"))]
85 fn create_event_loop(&self) -> Result<Self::EventLoop, PlatformError> {
86 Err(PlatformError::Unsupported(
87 "Android platform only available on Android".to_string(),
88 ))
89 }
90
91 fn name(&self) -> &'static str {
92 "android"
93 }
94
95 #[cfg(target_os = "android")]
96 fn scale_factor(&self) -> f64 {
97 get_display_density(&self.app)
98 }
99
100 #[cfg(not(target_os = "android"))]
101 fn scale_factor(&self) -> f64 {
102 1.0
103 }
104}
105
106#[cfg(not(target_os = "android"))]
108impl AndroidPlatform {
109 pub fn with_app() -> Result<Self, PlatformError> {
111 Err(PlatformError::Unsupported(
112 "Android platform only available on Android".to_string(),
113 ))
114 }
115}
116
117#[cfg(all(target_os = "android", feature = "default-activity"))]
119pub use activity::android_main;
120
121pub mod input_convert {
123 use super::input::{TouchEvent, TouchPointer};
124 use blinc_platform::{InputEvent, TouchEvent as BlincTouchEvent};
125
126 pub fn convert_touch_event(event: &TouchEvent) -> InputEvent {
128 match event {
129 TouchEvent::Down { pointer, .. } => InputEvent::Touch(BlincTouchEvent::Started {
130 id: pointer.id as u64,
131 x: pointer.x,
132 y: pointer.y,
133 pressure: pointer.pressure,
134 }),
135 TouchEvent::Move { pointers } => {
136 if let Some(p) = pointers.first() {
138 InputEvent::Touch(BlincTouchEvent::Moved {
139 id: p.id as u64,
140 x: p.x,
141 y: p.y,
142 pressure: p.pressure,
143 })
144 } else {
145 InputEvent::Touch(BlincTouchEvent::Cancelled { id: 0 })
146 }
147 }
148 TouchEvent::Up { pointer, .. } => InputEvent::Touch(BlincTouchEvent::Ended {
149 id: pointer.id as u64,
150 x: pointer.x,
151 y: pointer.y,
152 }),
153 TouchEvent::Cancel => InputEvent::Touch(BlincTouchEvent::Cancelled { id: 0 }),
154 }
155 }
156
157 pub fn convert_multi_touch_move(pointers: &[TouchPointer]) -> Vec<InputEvent> {
159 pointers
160 .iter()
161 .map(|p| {
162 InputEvent::Touch(BlincTouchEvent::Moved {
163 id: p.id as u64,
164 x: p.x,
165 y: p.y,
166 pressure: p.pressure,
167 })
168 })
169 .collect()
170 }
171}
172
173pub use input::GestureDetector;