[go: up one dir, main page]

android-activity 0.1.1

Glue for building Rust applications on Android with NativeActivity or GameActivity
Documentation

trait Configuration {

    /// Returns the country code. It will always be two letters.
    pub fn country(&self) -> String {
        let mut result = "  ".to_owned();
        unsafe {
            ffi::AConfiguration_getCountry(self.ptr.as_ptr(), result.as_mut_ptr() as *mut _);
        }
        result
    }

    /// Returns the screen density in dpi.
    ///
    /// On some devices it can return values outside of the density enum.
    pub fn density(&self) -> Option<u32> {
        let density = unsafe { ffi::AConfiguration_getDensity(self.ptr.as_ptr()) as u32 };
        match density {
            ffi::ACONFIGURATION_DENSITY_DEFAULT => Some(160),
            ffi::ACONFIGURATION_DENSITY_ANY => None,
            ffi::ACONFIGURATION_DENSITY_NONE => None,
            density => Some(density),
        }
    }

    /// Returns the keyboard type.
    pub fn keyboard(&self) -> Keyboard {
        unsafe {
            (ffi::AConfiguration_getKeyboard(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    /// Returns keyboard visibility/availability.
    pub fn keys_hidden(&self) -> KeysHidden {
        unsafe {
            (ffi::AConfiguration_getKeysHidden(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    /// Returns the language, as a `String` of two characters, if a language is set
    pub fn language(&self) -> Option<String> {
        let mut chars = [0u8; 2];
        unsafe {
            ffi::AConfiguration_getLanguage(self.ptr.as_ptr(), chars[..].as_mut_ptr() as *mut _);
        }
        if chars[0] == 0 {
            None
        } else {
            Some(std::str::from_utf8(&chars[..]).unwrap().to_owned())
        }
    }

    /// Returns the layout direction
    pub fn layout_direction(&self) -> LayoutDir {
        unsafe {
            (ffi::AConfiguration_getLayoutDirection(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    /// Returns the mobile country code.
    pub fn mcc(&self) -> i32 {
        unsafe { ffi::AConfiguration_getMcc(self.ptr.as_ptr()) }
    }

    /// Returns the mobile network code, if one is defined
    pub fn mnc(&self) -> Option<i32> {
        unsafe {
            match ffi::AConfiguration_getMnc(self.ptr.as_ptr()) {
                0 => None,
                x if x == ffi::ACONFIGURATION_MNC_ZERO as i32 => Some(0),
                x => Some(x),
            }
        }
    }

    pub fn nav_hidden(&self) -> NavHidden {
        unsafe {
            (ffi::AConfiguration_getNavHidden(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn navigation(&self) -> Navigation {
        unsafe {
            (ffi::AConfiguration_getNavigation(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn orientation(&self) -> Orientation {
        unsafe {
            (ffi::AConfiguration_getOrientation(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn screen_height_dp(&self) -> Option<i32> {
        unsafe {
            let height = ffi::AConfiguration_getScreenHeightDp(self.ptr.as_ptr());
            if height == ffi::ACONFIGURATION_SCREEN_HEIGHT_DP_ANY as i32 {
                None
            } else {
                Some(height)
            }
        }
    }

    pub fn screen_width_dp(&self) -> Option<i32> {
        unsafe {
            let width = ffi::AConfiguration_getScreenWidthDp(self.ptr.as_ptr());
            if width == ffi::ACONFIGURATION_SCREEN_WIDTH_DP_ANY as i32 {
                None
            } else {
                Some(width)
            }
        }
    }

    pub fn screen_long(&self) -> ScreenLong {
        unsafe {
            (ffi::AConfiguration_getScreenLong(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    #[cfg(feature = "api-level-30")]
    pub fn screen_round(&self) -> ScreenRound {
        unsafe {
            (ffi::AConfiguration_getScreenRound(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn screen_size(&self) -> ScreenSize {
        unsafe {
            (ffi::AConfiguration_getScreenSize(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn sdk_version(&self) -> i32 {
        unsafe { ffi::AConfiguration_getSdkVersion(self.ptr.as_ptr()) }
    }

    pub fn smallest_screen_width_dp(&self) -> Option<i32> {
        unsafe {
            let width = ffi::AConfiguration_getSmallestScreenWidthDp(self.ptr.as_ptr());
            if width == ffi::ACONFIGURATION_SMALLEST_SCREEN_WIDTH_DP_ANY as i32 {
                None
            } else {
                Some(width)
            }
        }
    }

    pub fn touchscreen(&self) -> Touchscreen {
        unsafe {
            (ffi::AConfiguration_getTouchscreen(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn ui_mode_night(&self) -> UiModeNight {
        unsafe {
            (ffi::AConfiguration_getUiModeNight(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn ui_mode_type(&self) -> UiModeType {
        unsafe {
            (ffi::AConfiguration_getUiModeType(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }
}