diff --git a/rsbinder/src/binder.rs b/rsbinder/src/binder.rs index 116324b..8923a51 100644 --- a/rsbinder/src/binder.rs +++ b/rsbinder/src/binder.rs @@ -282,21 +282,22 @@ pub enum Stability { impl From for i32 { fn from(stability: Stability) -> i32 { use Stability::*; - if crate::is_new_stability() { - match stability { - Local => 0, - Vendor => 0x0c000003, - System => 0x0c00000c, - Vintf => 0x0c00003f, - } + + let stability = match stability { + Local => 0, + Vendor => 0b000011, + System => 0b001100, + Vintf => 0b111111, + }; + + #[cfg(target_os = "android")] + if crate::get_android_version() == 12 { + stability | 0x0c000000 } else { - match stability { - Local => 0, - Vendor => 0b000011, - System => 0b001100, - Vintf => 0b111111, - } + stability } + #[cfg(not(target_os = "android"))] + stability } } diff --git a/rsbinder/src/lib.rs b/rsbinder/src/lib.rs index 180f497..a37c65f 100644 --- a/rsbinder/src/lib.rs +++ b/rsbinder/src/lib.rs @@ -55,24 +55,24 @@ pub const DEFAULT_BINDER_CONTROL_PATH: &str = "/dev/binderfs/binder-control"; pub const DEFAULT_BINDER_PATH: &str = "/dev/binderfs/binder"; pub const DEFAULT_BINDERFS_PATH: &str = "/dev/binderfs"; + #[cfg(target_os = "android")] static ANDROID_VERSION: std::sync::OnceLock = std::sync::OnceLock::new(); /// Set the Android version for compatibility. +/// There are compatibility issues with Binder IPC depending on the Android version. +/// The current findings indicate there are compatibility issues before and after Android 12. +/// If your software needs to work on both Android 11 and 12, +/// you must set the Android version using the rsbinder::set_android_version() API. #[cfg(target_os = "android")] pub fn set_android_version(version: i32) { ANDROID_VERSION.set(version).expect("Android version is already set."); } -/// Get binder stability version. -pub fn is_new_stability() -> bool { - #[cfg(target_os = "android")] - match ANDROID_VERSION.get() { - Some(version) => *version == 12, - None => false, // Support the latest version by default. - } - #[cfg(not(target_os = "android"))] - false +/// Get the Android version. +#[cfg(target_os = "android")] +pub fn get_android_version() -> i32 { + *ANDROID_VERSION.get().unwrap_or(&0) } #[cfg(test)]