-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
177 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
24 changes: 24 additions & 0 deletions
24
internal/backends/android-activity/java/SlintAndroidJavaHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters