From 226518fab93b7acb62fb453c10c6eb3f5adfead5 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Mon, 31 Oct 2022 12:55:56 +0000 Subject: [PATCH] android: Use ndk-context instead of ndk-glue to access JVM This makes webbrowser-rs more compatible with different Android application frameworks by not imposing that applications must use ndk-glue. This also bumps the `jni` dependency to 0.20 and uses the `JObject::from_raw` API. Fixes: #51 --- Cargo.toml | 7 +++++-- src/android.rs | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4b4f673..5b75ade 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" } \ No newline at end of file diff --git a/src/android.rs b/src/android.rs index ca21dd7..32e1684 100644 --- a/src/android.rs +++ b/src/android.rs @@ -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") })?; @@ -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)],