From 695c85f58e4f6fa554a445748021cdb1717135d1 Mon Sep 17 00:00:00 2001 From: Mark Barbone Date: Fri, 25 Oct 2019 22:23:14 -0400 Subject: [PATCH] Add DPI info, content_rect, and various small changes --- android-ndk/src/android_app.rs | 4 ++ android-ndk/src/configuration.rs | 47 ++++++++++++++++++++++- android-ndk/src/event.rs | 64 +++++++++++++++++++++++++------- 3 files changed, 101 insertions(+), 14 deletions(-) diff --git a/android-ndk/src/android_app.rs b/android-ndk/src/android_app.rs index 5acd933..7d94db4 100644 --- a/android-ndk/src/android_app.rs +++ b/android-ndk/src/android_app.rs @@ -102,6 +102,10 @@ impl AndroidApp { } } + pub fn content_rect(&self) -> ffi::ARect { + unsafe { self.ptr.as_ref().contentRect } + } + // The looper will also never change pub fn looper(&self) -> ForeignLooper { unsafe { ForeignLooper::from_ptr(NonNull::new(self.ptr.as_ref().looper).unwrap()) } diff --git a/android-ndk/src/configuration.rs b/android-ndk/src/configuration.rs index 390877a..180e7f5 100644 --- a/android-ndk/src/configuration.rs +++ b/android-ndk/src/configuration.rs @@ -14,7 +14,8 @@ use std::ptr::NonNull; /// A native `AConfiguration *`. /// -/// This stores information about configuration. +/// This stores information about configuration. See [the NDK +/// docs](https://developer.android.com/ndk/reference/group/configuration) pub struct Configuration { ptr: NonNull, } @@ -392,6 +393,50 @@ pub enum Density { None = ffi::ACONFIGURATION_DENSITY_NONE, } +impl Density { + /// The DPI associated with the density class. + /// See [the Android screen density + /// docs](https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp) + /// + /// There are some `Density` values that have no associated DPI; these values return `None`. + pub fn approx_dpi(self) -> Option { + match self { + Self::Default => Some(160), // Or should it be None? + Self::Low => Some(120), + Self::Medium => Some(160), + Self::High => Some(240), + Self::XHigh => Some(320), + Self::XXHigh => Some(480), + Self::XXXHigh => Some(640), + Self::TV => Some(213), + Self::Any => None, + Self::None => None, + } + } + + /// The Hi-DPI factor associated with the density class. This is the factor by which an + /// image/resource should be scaled to match its size across devices. The baseline is a 160dpi + /// screen (i.e., Hi-DPI factor = DPI / 160). + /// See [the Android screen density + /// docs](https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp) + /// + /// There are some `Density` values that have no associated DPI; these values return `None`. + pub fn approx_hidpi_factor(self) -> Option { + match self { + Self::Default => Some(1.), // Or should it be None? + Self::Low => Some(0.75), + Self::Medium => Some(1.), + Self::High => Some(1.5), + Self::XHigh => Some(2.), + Self::XXHigh => Some(3.), + Self::XXXHigh => Some(4.), + Self::TV => Some(4. / 3.), + Self::Any => None, + Self::None => None, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] #[repr(u32)] pub enum Keyboard { diff --git a/android-ndk/src/event.rs b/android-ndk/src/event.rs index b1c5276..80d75d0 100644 --- a/android-ndk/src/event.rs +++ b/android-ndk/src/event.rs @@ -354,6 +354,25 @@ impl MotionEvent { self.ptr } + /// Get the source of the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource) + #[inline] + pub fn source(&self) -> Source { + let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) as u32 }; + source.try_into().unwrap_or(Source::Unknown) + } + + /// Get the device id associated with the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid) + #[inline] + pub fn device_id(&self) -> i32 { + unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) } + } + /// Returns the motion action associated with this event. /// /// See [the NDK @@ -1301,8 +1320,27 @@ impl KeyEvent { action.try_into().unwrap() } + /// Get the source of the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource) + #[inline] + pub fn source(&self) -> Source { + let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) as u32 }; + source.try_into().unwrap_or(Source::Unknown) + } + + /// Get the device id associated with the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid) + #[inline] + pub fn device_id(&self) -> i32 { + unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) } + } + /// Returns the last time the key was pressed. This is on the scale of - /// `java.lang.System.nanoTime()`, which has nanosecond accuracy, but no defined start time. + /// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time. /// /// See [the NDK /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getdowntime) @@ -1312,7 +1350,7 @@ impl KeyEvent { } /// Returns the time this event occured. This is on the scale of - /// `java.lang.System.nanoTime()`, which has nanosecond accuracy, but no defined start time. + /// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time. /// /// See [the NDK /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_geteventtime) @@ -1358,47 +1396,47 @@ pub struct KeyEventFlags(pub u32); impl KeyEventFlags { #[inline] - pub fn cancelled_flag(&self) -> bool { + pub fn cancelled(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_CANCELED != 0 } #[inline] - pub fn cancelled_long_press_flag(&self) -> bool { + pub fn cancelled_long_press(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_CANCELED_LONG_PRESS != 0 } #[inline] - pub fn editor_action_flag(&self) -> bool { + pub fn editor_action(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_EDITOR_ACTION != 0 } #[inline] - pub fn fallback_flag(&self) -> bool { + pub fn fallback(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_FALLBACK != 0 } #[inline] - pub fn from_system_flag(&self) -> bool { + pub fn from_system(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_FROM_SYSTEM != 0 } #[inline] - pub fn keep_touch_mode_flag(&self) -> bool { + pub fn keep_touch_mode(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_KEEP_TOUCH_MODE != 0 } #[inline] - pub fn long_press_flag(&self) -> bool { + pub fn long_press(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_LONG_PRESS != 0 } #[inline] - pub fn soft_keyboard_flag(&self) -> bool { + pub fn soft_keyboard(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_SOFT_KEYBOARD != 0 } #[inline] - pub fn tracking_flag(&self) -> bool { + pub fn tracking(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_TRACKING != 0 } #[inline] - pub fn virtual_hard_key_flag(&self) -> bool { + pub fn virtual_hard_key(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY != 0 } #[inline] - pub fn woke_here_flag(&self) -> bool { + pub fn woke_here(&self) -> bool { self.0 & ffi::AKEY_EVENT_FLAG_WOKE_HERE != 0 } }