os_android.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /**************************************************************************/
  2. /* os_android.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "os_android.h"
  31. #include "dir_access_jandroid.h"
  32. #include "display_server_android.h"
  33. #include "file_access_android.h"
  34. #include "file_access_filesystem_jandroid.h"
  35. #include "java_godot_io_wrapper.h"
  36. #include "java_godot_wrapper.h"
  37. #include "net_socket_android.h"
  38. #include "core/config/project_settings.h"
  39. #include "core/extension/gdextension_manager.h"
  40. #include "core/io/xml_parser.h"
  41. #include "drivers/unix/dir_access_unix.h"
  42. #include "drivers/unix/file_access_unix.h"
  43. #include "main/main.h"
  44. #include "scene/main/scene_tree.h"
  45. #include "servers/rendering_server.h"
  46. #include <dlfcn.h>
  47. #include <sys/system_properties.h>
  48. const char *OS_Android::ANDROID_EXEC_PATH = "apk";
  49. String _remove_symlink(const String &dir) {
  50. // Workaround for Android 6.0+ using a symlink.
  51. // Save the current directory.
  52. char current_dir_name[2048];
  53. getcwd(current_dir_name, 2048);
  54. // Change directory to the external data directory.
  55. chdir(dir.utf8().get_data());
  56. // Get the actual directory without the potential symlink.
  57. char dir_name_without_symlink[2048];
  58. getcwd(dir_name_without_symlink, 2048);
  59. // Convert back to a String.
  60. String dir_without_symlink(dir_name_without_symlink);
  61. // Restore original current directory.
  62. chdir(current_dir_name);
  63. return dir_without_symlink;
  64. }
  65. class AndroidLogger : public Logger {
  66. public:
  67. virtual void logv(const char *p_format, va_list p_list, bool p_err) {
  68. __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
  69. }
  70. virtual ~AndroidLogger() {}
  71. };
  72. void OS_Android::alert(const String &p_alert, const String &p_title) {
  73. ERR_FAIL_NULL(godot_java);
  74. godot_java->alert(p_alert, p_title);
  75. }
  76. void OS_Android::initialize_core() {
  77. OS_Unix::initialize_core();
  78. #ifdef TOOLS_ENABLED
  79. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  80. #else
  81. if (use_apk_expansion) {
  82. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  83. } else {
  84. FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
  85. }
  86. #endif
  87. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  88. FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_FILESYSTEM);
  89. #ifdef TOOLS_ENABLED
  90. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  91. #else
  92. if (use_apk_expansion) {
  93. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  94. } else {
  95. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
  96. }
  97. #endif
  98. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  99. DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_FILESYSTEM);
  100. NetSocketAndroid::make_default();
  101. }
  102. void OS_Android::initialize() {
  103. initialize_core();
  104. }
  105. void OS_Android::initialize_joypads() {
  106. Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
  107. // This queries/updates the currently connected devices/joypads.
  108. godot_java->init_input_devices();
  109. }
  110. void OS_Android::set_main_loop(MainLoop *p_main_loop) {
  111. main_loop = p_main_loop;
  112. }
  113. void OS_Android::delete_main_loop() {
  114. if (main_loop) {
  115. memdelete(main_loop);
  116. main_loop = nullptr;
  117. }
  118. }
  119. void OS_Android::finalize() {
  120. }
  121. OS_Android *OS_Android::get_singleton() {
  122. return static_cast<OS_Android *>(OS::get_singleton());
  123. }
  124. GodotJavaWrapper *OS_Android::get_godot_java() {
  125. return godot_java;
  126. }
  127. GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
  128. return godot_io_java;
  129. }
  130. bool OS_Android::request_permission(const String &p_name) {
  131. return godot_java->request_permission(p_name);
  132. }
  133. bool OS_Android::request_permissions() {
  134. return godot_java->request_permissions();
  135. }
  136. Vector<String> OS_Android::get_granted_permissions() const {
  137. return godot_java->get_granted_permissions();
  138. }
  139. bool OS_Android::copy_dynamic_library(const String &p_library_path, const String &p_target_dir, String *r_copy_path) {
  140. if (!FileAccess::exists(p_library_path)) {
  141. return false;
  142. }
  143. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_library_path);
  144. if (!da_ref.is_valid()) {
  145. return false;
  146. }
  147. String copy_path = p_target_dir.path_join(p_library_path.get_file());
  148. bool copy_exists = FileAccess::exists(copy_path);
  149. if (copy_exists) {
  150. print_verbose("Deleting existing library copy " + copy_path);
  151. if (da_ref->remove(copy_path) != OK) {
  152. print_verbose("Unable to delete " + copy_path);
  153. }
  154. }
  155. print_verbose("Copying " + p_library_path + " to " + p_target_dir);
  156. Error create_dir_result = da_ref->make_dir_recursive(p_target_dir);
  157. if (create_dir_result == OK || create_dir_result == ERR_ALREADY_EXISTS) {
  158. copy_exists = da_ref->copy(p_library_path, copy_path) == OK;
  159. }
  160. if (copy_exists && r_copy_path != nullptr) {
  161. *r_copy_path = copy_path;
  162. }
  163. return copy_exists;
  164. }
  165. Error OS_Android::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
  166. String path = p_path;
  167. bool so_file_exists = true;
  168. if (!FileAccess::exists(path)) {
  169. path = p_path.get_file();
  170. so_file_exists = false;
  171. }
  172. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  173. if (!p_library_handle && so_file_exists) {
  174. // The library (and its dependencies) may be on the sdcard and thus inaccessible.
  175. // Try to copy to the internal directory for access.
  176. const String dynamic_library_path = get_dynamic_libraries_path();
  177. if (p_data != nullptr && p_data->library_dependencies != nullptr && !p_data->library_dependencies->is_empty()) {
  178. // Copy the library dependencies
  179. print_verbose("Copying library dependencies..");
  180. for (const String &library_dependency_path : *p_data->library_dependencies) {
  181. String internal_library_dependency_path;
  182. if (!copy_dynamic_library(library_dependency_path, dynamic_library_path.path_join(library_dependency_path.get_base_dir()), &internal_library_dependency_path)) {
  183. ERR_PRINT(vformat("Unable to copy library dependency %s", library_dependency_path));
  184. } else {
  185. void *lib_dependency_handle = dlopen(internal_library_dependency_path.utf8().get_data(), RTLD_NOW);
  186. if (!lib_dependency_handle) {
  187. ERR_PRINT(vformat("Can't open dynamic library dependency: %s. Error: %s.", internal_library_dependency_path, dlerror()));
  188. }
  189. }
  190. }
  191. }
  192. String internal_path;
  193. print_verbose("Copying library " + p_path);
  194. const bool internal_so_file_exists = copy_dynamic_library(p_path, dynamic_library_path.path_join(p_path.get_base_dir()), &internal_path);
  195. if (internal_so_file_exists) {
  196. print_verbose("Opening library " + internal_path);
  197. p_library_handle = dlopen(internal_path.utf8().get_data(), RTLD_NOW);
  198. if (p_library_handle) {
  199. path = internal_path;
  200. }
  201. }
  202. }
  203. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
  204. if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
  205. *p_data->r_resolved_path = path;
  206. }
  207. return OK;
  208. }
  209. String OS_Android::get_name() const {
  210. return "Android";
  211. }
  212. String OS_Android::get_system_property(const char *key) const {
  213. String value;
  214. char value_str[PROP_VALUE_MAX];
  215. if (__system_property_get(key, value_str)) {
  216. value = String(value_str);
  217. }
  218. return value;
  219. }
  220. String OS_Android::get_distribution_name() const {
  221. if (!get_system_property("ro.havoc.version").is_empty()) {
  222. return "Havoc OS";
  223. } else if (!get_system_property("org.pex.version").is_empty()) { // Putting before "Pixel Experience", because it's derivating from it.
  224. return "Pixel Extended";
  225. } else if (!get_system_property("org.pixelexperience.version").is_empty()) {
  226. return "Pixel Experience";
  227. } else if (!get_system_property("ro.potato.version").is_empty()) {
  228. return "POSP";
  229. } else if (!get_system_property("ro.xtended.version").is_empty()) {
  230. return "Project-Xtended";
  231. } else if (!get_system_property("org.evolution.version").is_empty()) {
  232. return "Evolution X";
  233. } else if (!get_system_property("ro.corvus.version").is_empty()) {
  234. return "Corvus-Q";
  235. } else if (!get_system_property("ro.pa.version").is_empty()) {
  236. return "Paranoid Android";
  237. } else if (!get_system_property("ro.crdroid.version").is_empty()) {
  238. return "crDroid Android";
  239. } else if (!get_system_property("ro.syberia.version").is_empty()) {
  240. return "Syberia Project";
  241. } else if (!get_system_property("ro.arrow.version").is_empty()) {
  242. return "ArrowOS";
  243. } else if (!get_system_property("ro.lineage.version").is_empty()) { // Putting LineageOS last, just in case any derivative writes to "ro.lineage.version".
  244. return "LineageOS";
  245. }
  246. if (!get_system_property("ro.modversion").is_empty()) { // Handles other Android custom ROMs.
  247. return vformat("%s %s", get_name(), "Custom ROM");
  248. }
  249. // Handles stock Android.
  250. return get_name();
  251. }
  252. String OS_Android::get_version() const {
  253. const Vector<const char *> roms = { "ro.havoc.version", "org.pex.version", "org.pixelexperience.version",
  254. "ro.potato.version", "ro.xtended.version", "org.evolution.version", "ro.corvus.version", "ro.pa.version",
  255. "ro.crdroid.version", "ro.syberia.version", "ro.arrow.version", "ro.lineage.version" };
  256. for (int i = 0; i < roms.size(); i++) {
  257. String rom_version = get_system_property(roms[i]);
  258. if (!rom_version.is_empty()) {
  259. return rom_version;
  260. }
  261. }
  262. String mod_version = get_system_property("ro.modversion"); // Handles other Android custom ROMs.
  263. if (!mod_version.is_empty()) {
  264. return mod_version;
  265. }
  266. // Handles stock Android.
  267. String sdk_version = get_system_property("ro.build.version.sdk_int");
  268. String build = get_system_property("ro.build.version.incremental");
  269. if (!sdk_version.is_empty()) {
  270. if (!build.is_empty()) {
  271. return vformat("%s.%s", sdk_version, build);
  272. }
  273. return sdk_version;
  274. }
  275. return "";
  276. }
  277. MainLoop *OS_Android::get_main_loop() const {
  278. return main_loop;
  279. }
  280. void OS_Android::main_loop_begin() {
  281. if (main_loop) {
  282. main_loop->initialize();
  283. }
  284. }
  285. bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
  286. if (!main_loop) {
  287. return false;
  288. }
  289. DisplayServerAndroid::get_singleton()->reset_swap_buffers_flag();
  290. DisplayServerAndroid::get_singleton()->process_events();
  291. uint64_t current_frames_drawn = Engine::get_singleton()->get_frames_drawn();
  292. bool exit = Main::iteration();
  293. if (r_should_swap_buffers) {
  294. *r_should_swap_buffers = !is_in_low_processor_usage_mode() ||
  295. DisplayServerAndroid::get_singleton()->should_swap_buffers() ||
  296. RenderingServer::get_singleton()->has_changed() ||
  297. current_frames_drawn != Engine::get_singleton()->get_frames_drawn();
  298. }
  299. return exit;
  300. }
  301. void OS_Android::main_loop_end() {
  302. if (main_loop) {
  303. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  304. if (scene_tree) {
  305. scene_tree->quit();
  306. }
  307. main_loop->finalize();
  308. }
  309. }
  310. void OS_Android::main_loop_focusout() {
  311. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  312. if (OS::get_singleton()->get_main_loop()) {
  313. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  314. }
  315. audio_driver_android.set_pause(true);
  316. }
  317. void OS_Android::main_loop_focusin() {
  318. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  319. if (OS::get_singleton()->get_main_loop()) {
  320. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  321. }
  322. audio_driver_android.set_pause(false);
  323. }
  324. Error OS_Android::shell_open(const String &p_uri) {
  325. return godot_io_java->open_uri(p_uri);
  326. }
  327. String OS_Android::get_resource_dir() const {
  328. #ifdef TOOLS_ENABLED
  329. return OS_Unix::get_resource_dir();
  330. #else
  331. if (remote_fs_dir.is_empty()) {
  332. return "/"; // Android has its own filesystem for resources inside the APK
  333. } else {
  334. return remote_fs_dir;
  335. }
  336. #endif
  337. }
  338. String OS_Android::get_locale() const {
  339. String locale = godot_io_java->get_locale();
  340. if (!locale.is_empty()) {
  341. return locale;
  342. }
  343. return OS_Unix::get_locale();
  344. }
  345. String OS_Android::get_model_name() const {
  346. String model = godot_io_java->get_model();
  347. if (!model.is_empty()) {
  348. return model;
  349. }
  350. return OS_Unix::get_model_name();
  351. }
  352. String OS_Android::get_data_path() const {
  353. return get_user_data_dir();
  354. }
  355. void OS_Android::_load_system_font_config() {
  356. font_aliases.clear();
  357. fonts.clear();
  358. font_names.clear();
  359. Ref<XMLParser> parser;
  360. parser.instantiate();
  361. Error err = parser->open(String(getenv("ANDROID_ROOT")).path_join("/etc/fonts.xml"));
  362. if (err == OK) {
  363. bool in_font_node = false;
  364. String fb, fn;
  365. FontInfo fi;
  366. while (parser->read() == OK) {
  367. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  368. in_font_node = false;
  369. if (parser->get_node_name() == "familyset") {
  370. int ver = parser->has_attribute("version") ? parser->get_named_attribute_value("version").to_int() : 0;
  371. if (ver < 21) {
  372. ERR_PRINT(vformat("Unsupported font config version %s", ver));
  373. break;
  374. }
  375. } else if (parser->get_node_name() == "alias") {
  376. String name = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
  377. String to = parser->has_attribute("to") ? parser->get_named_attribute_value("to").strip_edges() : String();
  378. if (!name.is_empty() && !to.is_empty()) {
  379. font_aliases[name] = to;
  380. }
  381. } else if (parser->get_node_name() == "family") {
  382. fn = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
  383. String lang_code = parser->has_attribute("lang") ? parser->get_named_attribute_value("lang").strip_edges() : String();
  384. Vector<String> lang_codes = lang_code.split(",");
  385. for (int i = 0; i < lang_codes.size(); i++) {
  386. Vector<String> lang_code_elements = lang_codes[i].split("-");
  387. if (lang_code_elements.size() >= 1 && lang_code_elements[0] != "und") {
  388. // Add missing script codes.
  389. if (lang_code_elements[0] == "ko") {
  390. fi.script.insert("Hani");
  391. fi.script.insert("Hang");
  392. }
  393. if (lang_code_elements[0] == "ja") {
  394. fi.script.insert("Hani");
  395. fi.script.insert("Kana");
  396. fi.script.insert("Hira");
  397. }
  398. if (!lang_code_elements[0].is_empty()) {
  399. fi.lang.insert(lang_code_elements[0]);
  400. }
  401. }
  402. if (lang_code_elements.size() >= 2) {
  403. // Add common codes for variants and remove variants not supported by HarfBuzz/ICU.
  404. if (lang_code_elements[1] == "Aran") {
  405. fi.script.insert("Arab");
  406. }
  407. if (lang_code_elements[1] == "Cyrs") {
  408. fi.script.insert("Cyrl");
  409. }
  410. if (lang_code_elements[1] == "Hanb") {
  411. fi.script.insert("Hani");
  412. fi.script.insert("Bopo");
  413. }
  414. if (lang_code_elements[1] == "Hans" || lang_code_elements[1] == "Hant") {
  415. fi.script.insert("Hani");
  416. }
  417. if (lang_code_elements[1] == "Syrj" || lang_code_elements[1] == "Syre" || lang_code_elements[1] == "Syrn") {
  418. fi.script.insert("Syrc");
  419. }
  420. if (!lang_code_elements[1].is_empty() && lang_code_elements[1] != "Zsym" && lang_code_elements[1] != "Zsye" && lang_code_elements[1] != "Zmth") {
  421. fi.script.insert(lang_code_elements[1]);
  422. }
  423. }
  424. }
  425. } else if (parser->get_node_name() == "font") {
  426. in_font_node = true;
  427. fb = parser->has_attribute("fallbackFor") ? parser->get_named_attribute_value("fallbackFor").strip_edges() : String();
  428. fi.weight = parser->has_attribute("weight") ? parser->get_named_attribute_value("weight").to_int() : 400;
  429. fi.italic = parser->has_attribute("style") && parser->get_named_attribute_value("style").strip_edges() == "italic";
  430. }
  431. }
  432. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  433. if (in_font_node) {
  434. fi.filename = parser->get_node_data().strip_edges();
  435. fi.font_name = fn;
  436. if (!fb.is_empty() && fn.is_empty()) {
  437. fi.font_name = fb;
  438. fi.priority = 2;
  439. }
  440. if (fi.font_name.is_empty()) {
  441. fi.font_name = "sans-serif";
  442. fi.priority = 5;
  443. }
  444. if (fi.font_name.ends_with("-condensed")) {
  445. fi.stretch = 75;
  446. fi.font_name = fi.font_name.trim_suffix("-condensed");
  447. }
  448. fonts.push_back(fi);
  449. font_names.insert(fi.font_name);
  450. }
  451. }
  452. if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) {
  453. in_font_node = false;
  454. if (parser->get_node_name() == "font") {
  455. fb = String();
  456. fi.font_name = String();
  457. fi.priority = 0;
  458. fi.weight = 400;
  459. fi.stretch = 100;
  460. fi.italic = false;
  461. } else if (parser->get_node_name() == "family") {
  462. fi = FontInfo();
  463. fn = String();
  464. }
  465. }
  466. }
  467. parser->close();
  468. } else {
  469. ERR_PRINT("Unable to load font config");
  470. }
  471. font_config_loaded = true;
  472. }
  473. Vector<String> OS_Android::get_system_fonts() const {
  474. if (!font_config_loaded) {
  475. const_cast<OS_Android *>(this)->_load_system_font_config();
  476. }
  477. Vector<String> ret;
  478. for (const String &E : font_names) {
  479. ret.push_back(E);
  480. }
  481. return ret;
  482. }
  483. Vector<String> OS_Android::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const {
  484. if (!font_config_loaded) {
  485. const_cast<OS_Android *>(this)->_load_system_font_config();
  486. }
  487. String font_name = p_font_name.to_lower();
  488. if (font_aliases.has(font_name)) {
  489. font_name = font_aliases[font_name];
  490. }
  491. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  492. String lang_prefix = p_locale.split("_")[0];
  493. Vector<String> ret;
  494. int best_score = 0;
  495. for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
  496. int score = 0;
  497. if (!E->get().script.is_empty() && !p_script.is_empty() && !E->get().script.has(p_script)) {
  498. continue;
  499. }
  500. float sim = E->get().font_name.similarity(font_name);
  501. if (sim > 0.0) {
  502. score += (60 * sim + 5 - E->get().priority);
  503. }
  504. if (E->get().lang.has(p_locale)) {
  505. score += 120;
  506. } else if (E->get().lang.has(lang_prefix)) {
  507. score += 115;
  508. }
  509. if (E->get().script.has(p_script)) {
  510. score += 240;
  511. }
  512. score += (20 - Math::abs(E->get().weight - p_weight) / 50);
  513. score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
  514. if (E->get().italic == p_italic) {
  515. score += 30;
  516. }
  517. if (score > best_score) {
  518. best_score = score;
  519. if (!ret.has(root.path_join(E->get().filename))) {
  520. ret.insert(0, root.path_join(E->get().filename));
  521. }
  522. } else if (score == best_score || E->get().script.is_empty()) {
  523. if (!ret.has(root.path_join(E->get().filename))) {
  524. ret.push_back(root.path_join(E->get().filename));
  525. }
  526. }
  527. if (score >= 490) {
  528. break; // Perfect match.
  529. }
  530. }
  531. return ret;
  532. }
  533. String OS_Android::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  534. if (!font_config_loaded) {
  535. const_cast<OS_Android *>(this)->_load_system_font_config();
  536. }
  537. String font_name = p_font_name.to_lower();
  538. if (font_aliases.has(font_name)) {
  539. font_name = font_aliases[font_name];
  540. }
  541. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  542. int best_score = 0;
  543. const List<FontInfo>::Element *best_match = nullptr;
  544. for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
  545. int score = 0;
  546. if (E->get().font_name == font_name) {
  547. score += (65 - E->get().priority);
  548. }
  549. score += (20 - Math::abs(E->get().weight - p_weight) / 50);
  550. score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
  551. if (E->get().italic == p_italic) {
  552. score += 30;
  553. }
  554. if (score >= 60 && score > best_score) {
  555. best_score = score;
  556. best_match = E;
  557. }
  558. if (score >= 140) {
  559. break; // Perfect match.
  560. }
  561. }
  562. if (best_match) {
  563. return root.path_join(best_match->get().filename);
  564. }
  565. return String();
  566. }
  567. String OS_Android::get_executable_path() const {
  568. // Since unix process creation is restricted on Android, we bypass
  569. // OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
  570. // Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
  571. // manner.
  572. return OS::get_executable_path();
  573. }
  574. String OS_Android::get_user_data_dir() const {
  575. if (!data_dir_cache.is_empty()) {
  576. return data_dir_cache;
  577. }
  578. String data_dir = godot_io_java->get_user_data_dir();
  579. if (!data_dir.is_empty()) {
  580. data_dir_cache = _remove_symlink(data_dir);
  581. return data_dir_cache;
  582. }
  583. return ".";
  584. }
  585. String OS_Android::get_dynamic_libraries_path() const {
  586. return get_cache_path().path_join("dynamic_libraries");
  587. }
  588. String OS_Android::get_cache_path() const {
  589. if (!cache_dir_cache.is_empty()) {
  590. return cache_dir_cache;
  591. }
  592. String cache_dir = godot_io_java->get_cache_dir();
  593. if (!cache_dir.is_empty()) {
  594. cache_dir_cache = _remove_symlink(cache_dir);
  595. return cache_dir_cache;
  596. }
  597. return ".";
  598. }
  599. String OS_Android::get_unique_id() const {
  600. String unique_id = godot_io_java->get_unique_id();
  601. if (!unique_id.is_empty()) {
  602. return unique_id;
  603. }
  604. return OS::get_unique_id();
  605. }
  606. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  607. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  608. }
  609. Error OS_Android::move_to_trash(const String &p_path) {
  610. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
  611. if (da_ref.is_null()) {
  612. return FAILED;
  613. }
  614. // Check if it's a directory
  615. if (da_ref->dir_exists(p_path)) {
  616. Error err = da_ref->change_dir(p_path);
  617. if (err) {
  618. return err;
  619. }
  620. // This is directory, let's erase its contents
  621. err = da_ref->erase_contents_recursive();
  622. if (err) {
  623. return err;
  624. }
  625. // Remove the top directory
  626. return da_ref->remove(p_path);
  627. } else if (da_ref->file_exists(p_path)) {
  628. // This is a file, let's remove it.
  629. return da_ref->remove(p_path);
  630. } else {
  631. return FAILED;
  632. }
  633. }
  634. void OS_Android::set_display_size(const Size2i &p_size) {
  635. display_size = p_size;
  636. }
  637. Size2i OS_Android::get_display_size() const {
  638. return display_size;
  639. }
  640. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  641. #if defined(GLES3_ENABLED)
  642. ERR_FAIL_NULL(p_gl_extensions);
  643. gl_extensions = p_gl_extensions;
  644. #endif
  645. }
  646. void OS_Android::set_native_window(ANativeWindow *p_native_window) {
  647. #if defined(VULKAN_ENABLED)
  648. native_window = p_native_window;
  649. #endif
  650. }
  651. ANativeWindow *OS_Android::get_native_window() const {
  652. #if defined(VULKAN_ENABLED)
  653. return native_window;
  654. #else
  655. return nullptr;
  656. #endif
  657. }
  658. void OS_Android::vibrate_handheld(int p_duration_ms, float p_amplitude) {
  659. godot_java->vibrate(p_duration_ms, p_amplitude);
  660. }
  661. String OS_Android::get_config_path() const {
  662. return get_user_data_dir().path_join("config");
  663. }
  664. void OS_Android::benchmark_begin_measure(const String &p_context, const String &p_what) {
  665. #ifdef TOOLS_ENABLED
  666. godot_java->begin_benchmark_measure(p_context, p_what);
  667. #endif
  668. }
  669. void OS_Android::benchmark_end_measure(const String &p_context, const String &p_what) {
  670. #ifdef TOOLS_ENABLED
  671. godot_java->end_benchmark_measure(p_context, p_what);
  672. #endif
  673. }
  674. void OS_Android::benchmark_dump() {
  675. #ifdef TOOLS_ENABLED
  676. if (!is_use_benchmark_set()) {
  677. return;
  678. }
  679. godot_java->dump_benchmark(get_benchmark_file());
  680. #endif
  681. }
  682. #ifdef TOOLS_ENABLED
  683. Error OS_Android::sign_apk(const String &p_input_path, const String &p_output_path, const String &p_keystore_path, const String &p_keystore_user, const String &p_keystore_password) {
  684. return godot_java->sign_apk(p_input_path, p_output_path, p_keystore_path, p_keystore_user, p_keystore_password);
  685. }
  686. Error OS_Android::verify_apk(const String &p_apk_path) {
  687. return godot_java->verify_apk(p_apk_path);
  688. }
  689. #endif
  690. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  691. if (p_feature == "macos" || p_feature == "web_ios" || p_feature == "web_macos" || p_feature == "windows") {
  692. return false;
  693. }
  694. if (p_feature == "system_fonts") {
  695. return true;
  696. }
  697. if (p_feature == "mobile") {
  698. return true;
  699. }
  700. #if defined(__aarch64__)
  701. if (p_feature == "arm64-v8a" || p_feature == "arm64") {
  702. return true;
  703. }
  704. #elif defined(__ARM_ARCH_7A__)
  705. if (p_feature == "armeabi-v7a" || p_feature == "armeabi" || p_feature == "arm32") {
  706. return true;
  707. }
  708. #elif defined(__arm__)
  709. if (p_feature == "armeabi" || p_feature == "arm") {
  710. return true;
  711. }
  712. #endif
  713. if (godot_java->has_feature(p_feature)) {
  714. return true;
  715. }
  716. return false;
  717. }
  718. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  719. display_size.width = DEFAULT_WINDOW_WIDTH;
  720. display_size.height = DEFAULT_WINDOW_HEIGHT;
  721. use_apk_expansion = p_use_apk_expansion;
  722. main_loop = nullptr;
  723. #if defined(GLES3_ENABLED)
  724. gl_extensions = nullptr;
  725. #endif
  726. #if defined(VULKAN_ENABLED)
  727. native_window = nullptr;
  728. #endif
  729. godot_java = p_godot_java;
  730. godot_io_java = p_godot_io_java;
  731. Vector<Logger *> loggers;
  732. loggers.push_back(memnew(AndroidLogger));
  733. _set_logger(memnew(CompositeLogger(loggers)));
  734. AudioDriverManager::add_driver(&audio_driver_android);
  735. DisplayServerAndroid::register_android_driver();
  736. }
  737. Error OS_Android::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
  738. if (p_path == ANDROID_EXEC_PATH) {
  739. return create_instance(p_arguments);
  740. } else {
  741. return OS_Unix::execute(p_path, p_arguments, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
  742. }
  743. }
  744. Error OS_Android::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
  745. if (p_path == ANDROID_EXEC_PATH) {
  746. return create_instance(p_arguments, r_child_id);
  747. } else {
  748. return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console);
  749. }
  750. }
  751. Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
  752. int instance_id = godot_java->create_new_godot_instance(p_arguments);
  753. if (r_child_id) {
  754. *r_child_id = instance_id;
  755. }
  756. return OK;
  757. }
  758. Error OS_Android::kill(const ProcessID &p_pid) {
  759. if (godot_java->force_quit(nullptr, p_pid)) {
  760. return OK;
  761. }
  762. return OS_Unix::kill(p_pid);
  763. }
  764. String OS_Android::get_system_ca_certificates() {
  765. return godot_java->get_ca_certificates();
  766. }
  767. Error OS_Android::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
  768. r_project_path = get_user_data_dir();
  769. Error err = OS_Unix::setup_remote_filesystem(p_server_host, p_port, p_password, r_project_path);
  770. if (err == OK) {
  771. remote_fs_dir = r_project_path;
  772. FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_RESOURCES);
  773. }
  774. return err;
  775. }
  776. void OS_Android::load_platform_gdextensions() const {
  777. Vector<String> extension_list_config_file = godot_java->get_gdextension_list_config_file();
  778. for (String config_file_path : extension_list_config_file) {
  779. GDExtensionManager::LoadStatus err = GDExtensionManager::get_singleton()->load_extension(config_file_path);
  780. ERR_CONTINUE_MSG(err == GDExtensionManager::LOAD_STATUS_FAILED, "Error loading platform extension: " + config_file_path);
  781. }
  782. }
  783. OS_Android::~OS_Android() {
  784. }