mod.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use miniquad::native::android::{self, ndk_utils};
  19. use std::path::PathBuf;
  20. pub mod insets;
  21. pub mod textinput;
  22. pub(self) mod util;
  23. pub mod vid;
  24. pub use util::get_jni_env;
  25. macro_rules! call_mainactivity_int_method {
  26. ($method:expr, $sig:expr $(, $args:expr)*) => {{
  27. unsafe {
  28. let env = get_jni_env();
  29. ndk_utils::call_int_method!(env, android::ACTIVITY, $method, $sig $(, $args)*)
  30. }
  31. }};
  32. }
  33. macro_rules! call_mainactivity_str_method {
  34. ($method:expr) => {{
  35. unsafe {
  36. let env = get_jni_env();
  37. let text = ndk_utils::call_object_method!(
  38. env,
  39. android::ACTIVITY,
  40. $method,
  41. "()Ljava/lang/String;"
  42. );
  43. ndk_utils::get_utf_str!(env, text)
  44. }
  45. }};
  46. }
  47. macro_rules! call_mainactivity_bool_method {
  48. ($method:expr) => {{
  49. unsafe {
  50. let env = get_jni_env();
  51. ndk_utils::call_method!(CallBooleanMethod, env, android::ACTIVITY, $method, "()Z") !=
  52. 0u8
  53. }
  54. }};
  55. }
  56. pub fn get_appdata_path() -> PathBuf {
  57. call_mainactivity_str_method!("getAppDataPath").into()
  58. }
  59. pub fn get_external_storage_path() -> PathBuf {
  60. call_mainactivity_str_method!("getExternalStoragePath").into()
  61. }
  62. pub fn get_keyboard_height() -> usize {
  63. call_mainactivity_int_method!("getKeyboardHeight", "()I") as usize
  64. }
  65. pub fn get_long_press_timeout() -> u32 {
  66. call_mainactivity_int_method!("getLongPressTimeout", "()I") as u32
  67. }
  68. pub fn is_ime_visible() -> bool {
  69. call_mainactivity_bool_method!("isImeVisible")
  70. }
  71. /// Ask Android to redispatch the current window insets to ResizingLayout,
  72. /// which triggers the onApplyInsets callback with fresh values.
  73. pub fn request_apply_insets() {
  74. unsafe {
  75. let env = get_jni_env();
  76. ndk_utils::call_void_method!(env, android::ACTIVITY, "requestApplyInsets", "()V");
  77. }
  78. }
  79. /// Open `url` in the platform's default handler (e.g. the browser) by calling the
  80. /// `openUrl` method on MainActivity, which fires an `ACTION_VIEW` intent. Android
  81. /// only. The URL string is converted to a Java `String` via `NewStringUTF` and the
  82. /// local reference is released after the call.
  83. pub fn open_url(url: &str) {
  84. unsafe {
  85. let env = get_jni_env();
  86. let curl = std::ffi::CString::new(url).unwrap();
  87. let jurl = (**env).NewStringUTF.unwrap()(env, curl.as_ptr());
  88. ndk_utils::call_void_method!(
  89. env,
  90. android::ACTIVITY,
  91. "openUrl",
  92. "(Ljava/lang/String;)V",
  93. jurl
  94. );
  95. let delete_local_ref = (**env).DeleteLocalRef.unwrap();
  96. delete_local_ref(env, jurl);
  97. }
  98. }