Android: Use java code to show or hide the keyboard

instead of coding it all in JNI

This uses build.rs to compile the java code into bytecode that is then
embedded in the binary and loaded at runtime
This commit is contained in:
Olivier Goffart 2024-01-12 13:25:48 +01:00
parent a46b70833a
commit daa40f43cd
4 changed files with 178 additions and 38 deletions

View file

@ -0,0 +1,93 @@
use std::path::PathBuf;
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
use std::process::Command;
use std::{env, fs};
fn main() {
if !env::var("TARGET").unwrap().contains("android") {
return;
}
let out_dir = env::var("OUT_DIR").unwrap();
let slint_path = "dev/slint/android-activity";
let java_class = "SlintAndroidJavaHelper";
let out_class = format!("{out_dir}/java/{slint_path}");
let android_home =
PathBuf::from(env_var("ANDROID_HOME").or_else(|_| env_var("ANDROID_SDK_ROOT")).expect(
"Please set the ANDROID_HOME environment variable to the path of the Android SDK",
));
let classpath = find_latest_version(android_home.join("platforms"), "android.jar")
.expect("No Android platforms found");
// Try to locate javac
let javac_path = match env_var("JAVA_HOME") {
Ok(val) => {
if cfg!(windows) {
format!("{}\\bin\\javac.exe", val)
} else {
format!("{}/bin/javac", val)
}
}
Err(_) => String::from("javac"),
};
// Compile the Java file into a .class file
let o = Command::new(&javac_path)
.arg(format!("java/{java_class}.java"))
.arg("-d")
.arg(&out_class)
.arg("-classpath").arg(&classpath)
.arg("--release")
.arg("8")
.output()
.unwrap_or_else(|err| {
if err.kind() == std::io::ErrorKind::NotFound {
panic!("Could not locate the java compiler. Please ensure that the JAVA_HOME environment variable is set correctly.")
} else {
panic!("Could not run {javac_path}: {err}")
}
});
if !o.status.success() {
panic!("Java compilation failed: {}", String::from_utf8_lossy(&o.stderr));
}
// Convert the .class file into a .dex file
let d8_path = find_latest_version(
android_home.join("build-tools"),
if cfg!(windows) { "d8.exe" } else { "d8" },
)
.expect("d8 tool not found");
let o = Command::new(&d8_path)
.args(&["--classpath", &out_class])
.arg(format!("{out_class}/{java_class}.class"))
.arg("--output")
.arg(&out_dir)
.output()
.unwrap_or_else(|err| panic!("Error running {d8_path:?}: {err}"));
if !o.status.success() {
panic!("Dex conversion failed: {}", String::from_utf8_lossy(&o.stderr));
}
println!("cargo:rerun-if-changed=java/{java_class}.java");
}
fn env_var(var: &str) -> Result<String, env::VarError> {
println!("cargo:rerun-if-env-changed={}", var);
env::var(var)
}
fn find_latest_version(base: PathBuf, arg: &str) -> Option<PathBuf> {
fs::read_dir(base)
.ok()?
.filter_map(|entry| Some(entry.ok()?.path().join(arg)))
.filter(|path| path.exists())
.max()
}

View file

@ -0,0 +1,24 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
import android.view.View;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.app.Activity;
public class SlintAndroidJavaHelper {
Activity mActivity;
public SlintAndroidJavaHelper(Activity activity) {
this.mActivity = activity;
}
public void show_keyboard() {
InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mActivity.getWindow().getDecorView(), 0);
}
public void hide_keyboard() {
InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mActivity.getWindow().getDecorView().getWindowToken(), 0);
}
}

View file

@ -44,6 +44,7 @@ impl AndroidPlatform {
/// } /// }
/// ``` /// ```
pub fn new(app: AndroidApp) -> Self { pub fn new(app: AndroidApp) -> Self {
let slint_java_helper = SlintJavaHelper::new(&app).unwrap();
Self { Self {
app: app.clone(), app: app.clone(),
window: Rc::<AndroidWindowAdapter>::new_cyclic(|w| AndroidWindowAdapter { window: Rc::<AndroidWindowAdapter>::new_cyclic(|w| AndroidWindowAdapter {
@ -52,6 +53,7 @@ impl AndroidPlatform {
renderer: i_slint_renderer_skia::SkiaRenderer::default(), renderer: i_slint_renderer_skia::SkiaRenderer::default(),
event_queue: Default::default(), event_queue: Default::default(),
pending_redraw: Default::default(), pending_redraw: Default::default(),
slint_java_helper,
}), }),
event_listener: None, event_listener: None,
} }
@ -166,6 +168,7 @@ struct AndroidWindowAdapter {
renderer: i_slint_renderer_skia::SkiaRenderer, renderer: i_slint_renderer_skia::SkiaRenderer,
event_queue: EventQueue, event_queue: EventQueue,
pending_redraw: Cell<bool>, pending_redraw: Cell<bool>,
slint_java_helper: SlintJavaHelper,
} }
impl WindowAdapter for AndroidWindowAdapter { impl WindowAdapter for AndroidWindowAdapter {
@ -201,7 +204,7 @@ impl i_slint_core::window::WindowAdapterInternal for AndroidWindowAdapter {
#[cfg(not(feature = "native-activity"))] #[cfg(not(feature = "native-activity"))]
self.app.show_soft_input(true); self.app.show_soft_input(true);
#[cfg(feature = "native-activity")] #[cfg(feature = "native-activity")]
show_or_hide_soft_input(&self.app, true).unwrap(); show_or_hide_soft_input(&self.slint_java_helper, &self.app, true).unwrap();
props props
} }
i_slint_core::window::InputMethodRequest::Update(props) => props, i_slint_core::window::InputMethodRequest::Update(props) => props,
@ -209,7 +212,7 @@ impl i_slint_core::window::WindowAdapterInternal for AndroidWindowAdapter {
#[cfg(not(feature = "native-activity"))] #[cfg(not(feature = "native-activity"))]
self.app.hide_soft_input(true); self.app.hide_soft_input(true);
#[cfg(feature = "native-activity")] #[cfg(feature = "native-activity")]
show_or_hide_soft_input(&self.app, false).unwrap(); show_or_hide_soft_input(&self.slint_java_helper, &self.app, false).unwrap();
return; return;
} }
_ => return, _ => return,
@ -724,54 +727,73 @@ fn map_key_code(code: android_activity::input::Keycode) -> Option<SharedString>
} }
} }
struct SlintJavaHelper(#[cfg(feature = "native-activity")] jni::objects::GlobalRef);
impl SlintJavaHelper {
fn new(_app: &AndroidApp) -> Result<Self, jni::errors::Error> {
Ok(Self(
#[cfg(feature = "native-activity")]
load_java_helper(_app)?,
))
}
}
#[cfg(feature = "native-activity")] #[cfg(feature = "native-activity")]
/// Unfortunately, the way that the android-activity crate uses to show or hide the virtual keyboard doesn't /// Unfortunately, the way that the android-activity crate uses to show or hide the virtual keyboard doesn't
/// work with native-activity. So do it manually with JNI /// work with native-activity. So do it manually with JNI
fn show_or_hide_soft_input(app: &AndroidApp, show: bool) -> Result<(), jni::errors::Error> { fn show_or_hide_soft_input(
use jni::objects::{JObject, JValue}; helper: &SlintJavaHelper,
app: &AndroidApp,
show: bool,
) -> Result<(), jni::errors::Error> {
// Safety: as documented in android-activity to obtain a jni::JavaVM // Safety: as documented in android-activity to obtain a jni::JavaVM
let vm = unsafe { jni::JavaVM::from_raw(app.vm_as_ptr() as *mut _) }?; let vm = unsafe { jni::JavaVM::from_raw(app.vm_as_ptr() as *mut _) }?;
let mut env = vm.attach_current_thread()?; let mut env = vm.attach_current_thread()?;
let helper = helper.0.as_obj();
if show {
env.call_method(helper, "show_keyboard", "()V", &[])?;
} else {
env.call_method(helper, "hide_keyboard", "()V", &[])?;
};
Ok(())
}
// https://stackoverflow.com/questions/5864790/how-to-show-the-soft-keyboard-on-native-activity #[cfg(feature = "native-activity")]
fn load_java_helper(app: &AndroidApp) -> Result<jni::objects::GlobalRef, jni::errors::Error> {
use jni::objects::{JObject, JValue};
// Safety: as documented in android-activity to obtain a jni::JavaVM
let vm = unsafe { jni::JavaVM::from_raw(app.vm_as_ptr() as *mut _) }?;
let native_activity = unsafe { JObject::from_raw(app.activity_as_ptr() as *mut _) }; let native_activity = unsafe { JObject::from_raw(app.activity_as_ptr() as *mut _) };
let class_context = env.find_class("android/content/Context")?; let mut env = vm.attach_current_thread()?;
let input_method_service =
env.get_static_field(class_context, "INPUT_METHOD_SERVICE", "Ljava/lang/String;")?.l()?;
let input_method_manager = env let dex_data = include_bytes!(concat!(env!("OUT_DIR"), "/classes.dex"));
// Safety: dex_data is 'static and the InMemoryDexClassLoader will not mutate it it
let dex_buffer =
unsafe { env.new_direct_byte_buffer(dex_data.as_ptr() as *mut _, dex_data.len()).unwrap() };
let dex_loader = env.new_object(
"dalvik/system/InMemoryDexClassLoader",
"(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V",
&[JValue::Object(&dex_buffer), JValue::Object(&JObject::null())],
)?;
let class_name = env.new_string("SlintAndroidJavaHelper").unwrap();
let helper_class = env
.call_method( .call_method(
&native_activity, dex_loader,
"getSystemService", "findClass",
"(Ljava/lang/String;)Ljava/lang/Object;", "(Ljava/lang/String;)Ljava/lang/Class;",
&[JValue::Object(&input_method_service)], &[JValue::Object(&class_name)],
)? )?
.l()?; .l()?;
let window = let helper_class: jni::objects::JClass = helper_class.into();
env.call_method(native_activity, "getWindow", "()Landroid/view/Window;", &[])?.l()?; let helper_instance = env.new_object(
let decor_view = env.call_method(window, "getDecorView", "()Landroid/view/View;", &[])?.l()?; helper_class,
"(Landroid/app/Activity;)V",
if show { &[JValue::Object(&native_activity)],
env.call_method( )?;
input_method_manager, Ok(env.new_global_ref(&helper_instance)?)
"showSoftInput",
"(Landroid/view/View;I)Z",
&[JValue::Object(&decor_view), 0.into()],
)?;
} else {
let binder =
env.call_method(decor_view, "getWindowToken", "()Landroid/os/IBinder;", &[])?.l()?;
env.call_method(
input_method_manager,
"hideSoftInputFromWindow",
"(Landroid/os/IBinder;I)Z",
&[JValue::Object(&binder), 0.into()],
)?;
};
Ok(())
} }

View file

@ -451,6 +451,7 @@ lazy_static! {
("\\.npmignore$", LicenseLocation::NoLicense), ("\\.npmignore$", LicenseLocation::NoLicense),
("\\.h$", LicenseLocation::Tag(LicenseTagStyle::c_style_comment_style())), ("\\.h$", LicenseLocation::Tag(LicenseTagStyle::c_style_comment_style())),
("\\.html$", LicenseLocation::NoLicense), ("\\.html$", LicenseLocation::NoLicense),
("\\.java$", LicenseLocation::Tag(LicenseTagStyle::c_style_comment_style())),
("\\.jpg$", LicenseLocation::NoLicense), ("\\.jpg$", LicenseLocation::NoLicense),
("\\.js$", LicenseLocation::Tag(LicenseTagStyle::c_style_comment_style())), ("\\.js$", LicenseLocation::Tag(LicenseTagStyle::c_style_comment_style())),
("\\.json$", LicenseLocation::NoLicense), ("\\.json$", LicenseLocation::NoLicense),