Skip to content

Commit

Permalink
Fix the compatibility issue with Android 12
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kim committed Mar 19, 2024
1 parent a181c3e commit e719d69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
27 changes: 14 additions & 13 deletions rsbinder/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,22 @@ pub enum Stability {
impl From<Stability> 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
}
}

Expand Down
18 changes: 9 additions & 9 deletions rsbinder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> = 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)]
Expand Down

0 comments on commit e719d69

Please sign in to comment.