Skip to content

Commit

Permalink
android: Use ndk-context instead of ndk-glue to access JVM
Browse files Browse the repository at this point in the history
This makes webbrowser-rs more compatible with different Android
application frameworks by not imposing that applications must use
ndk-glue.

Fixes: amodm#51
  • Loading branch information
rib committed Oct 31, 2022
1 parent edf3814 commit 855bfac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Expand Up @@ -28,8 +28,8 @@ winapi = { version = "0.3", features = ["combaseapi", "objbase", "shellapi", "wi
widestring = { version = ">= 0.5, <=1.0" }

[target.'cfg(target_os = "android")'.dependencies]
jni = "0.19"
ndk-glue = { version = ">= 0.3, <= 0.7" }
jni = "0.20"
ndk-context = "0.1"

[target.'cfg(target_os = "ios")'.dependencies]
raw-window-handle = "0.5.0"
Expand All @@ -41,3 +41,6 @@ actix-files = "0.6"
crossbeam-channel = "0.5"
tokio = { version = "1", features = ["full"] }
urlencoding = "2.1"

[target.'cfg(target_os = "android")'.dev-dependencies]
ndk-glue = { version = ">= 0.3, <= 0.7" }
8 changes: 4 additions & 4 deletions src/android.rs
Expand Up @@ -24,9 +24,9 @@ pub fn open_browser_default(url: &str, options: &BrowserOptions) -> Result<()> {
}

// Create a VM for executing Java calls
let native_activity = ndk_glue::native_activity();
let vm_ptr = native_activity.vm();
let vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }.unwrap();
let ctx = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm() as _).expect("Expected to find JVM via ndk_context crate") };
let activity = unsafe { jni::objects::JObject::from_raw(ctx.context() as _) };
let env = vm.attach_current_thread().map_err(|_| -> Error {
Error::new(ErrorKind::Other, "Failed to attach current thread")
})?;
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn open_browser_default(url: &str, options: &BrowserOptions) -> Result<()> {

// Start the intent activity.
env.call_method(
native_activity.activity(),
activity,
"startActivity",
"(Landroid/content/Intent;)V",
&[JValue::Object(intent)],
Expand Down

0 comments on commit 855bfac

Please sign in to comment.