os_android.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. Error OS_Android::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path) {
  140. String path = p_path;
  141. bool so_file_exists = true;
  142. if (!FileAccess::exists(path)) {
  143. path = p_path.get_file();
  144. so_file_exists = false;
  145. }
  146. p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
  147. if (!p_library_handle && so_file_exists) {
  148. // The library may be on the sdcard and thus inaccessible. Try to copy it to the internal
  149. // directory.
  150. uint64_t so_modified_time = FileAccess::get_modified_time(p_path);
  151. String dynamic_library_path = get_dynamic_libraries_path().path_join(String::num_uint64(so_modified_time));
  152. String internal_path = dynamic_library_path.path_join(p_path.get_file());
  153. bool internal_so_file_exists = FileAccess::exists(internal_path);
  154. if (!internal_so_file_exists) {
  155. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
  156. if (da_ref.is_valid()) {
  157. Error create_dir_result = da_ref->make_dir_recursive(dynamic_library_path);
  158. if (create_dir_result == OK || create_dir_result == ERR_ALREADY_EXISTS) {
  159. internal_so_file_exists = da_ref->copy(path, internal_path) == OK;
  160. }
  161. }
  162. }
  163. if (internal_so_file_exists) {
  164. p_library_handle = dlopen(internal_path.utf8().get_data(), RTLD_NOW);
  165. if (p_library_handle) {
  166. path = internal_path;
  167. }
  168. }
  169. }
  170. ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
  171. if (r_resolved_path != nullptr) {
  172. *r_resolved_path = path;
  173. }
  174. return OK;
  175. }
  176. String OS_Android::get_name() const {
  177. return "Android";
  178. }
  179. String OS_Android::get_system_property(const char *key) const {
  180. String value;
  181. char value_str[PROP_VALUE_MAX];
  182. if (__system_property_get(key, value_str)) {
  183. value = String(value_str);
  184. }
  185. return value;
  186. }
  187. String OS_Android::get_distribution_name() const {
  188. if (!get_system_property("ro.havoc.version").is_empty()) {
  189. return "Havoc OS";
  190. } else if (!get_system_property("org.pex.version").is_empty()) { // Putting before "Pixel Experience", because it's derivating from it.
  191. return "Pixel Extended";
  192. } else if (!get_system_property("org.pixelexperience.version").is_empty()) {
  193. return "Pixel Experience";
  194. } else if (!get_system_property("ro.potato.version").is_empty()) {
  195. return "POSP";
  196. } else if (!get_system_property("ro.xtended.version").is_empty()) {
  197. return "Project-Xtended";
  198. } else if (!get_system_property("org.evolution.version").is_empty()) {
  199. return "Evolution X";
  200. } else if (!get_system_property("ro.corvus.version").is_empty()) {
  201. return "Corvus-Q";
  202. } else if (!get_system_property("ro.pa.version").is_empty()) {
  203. return "Paranoid Android";
  204. } else if (!get_system_property("ro.crdroid.version").is_empty()) {
  205. return "crDroid Android";
  206. } else if (!get_system_property("ro.syberia.version").is_empty()) {
  207. return "Syberia Project";
  208. } else if (!get_system_property("ro.arrow.version").is_empty()) {
  209. return "ArrowOS";
  210. } else if (!get_system_property("ro.lineage.version").is_empty()) { // Putting LineageOS last, just in case any derivative writes to "ro.lineage.version".
  211. return "LineageOS";
  212. }
  213. if (!get_system_property("ro.modversion").is_empty()) { // Handles other Android custom ROMs.
  214. return vformat("%s %s", get_name(), "Custom ROM");
  215. }
  216. // Handles stock Android.
  217. return get_name();
  218. }
  219. String OS_Android::get_version() const {
  220. const Vector<const char *> roms = { "ro.havoc.version", "org.pex.version", "org.pixelexperience.version",
  221. "ro.potato.version", "ro.xtended.version", "org.evolution.version", "ro.corvus.version", "ro.pa.version",
  222. "ro.crdroid.version", "ro.syberia.version", "ro.arrow.version", "ro.lineage.version" };
  223. for (int i = 0; i < roms.size(); i++) {
  224. String rom_version = get_system_property(roms[i]);
  225. if (!rom_version.is_empty()) {
  226. return rom_version;
  227. }
  228. }
  229. String mod_version = get_system_property("ro.modversion"); // Handles other Android custom ROMs.
  230. if (!mod_version.is_empty()) {
  231. return mod_version;
  232. }
  233. // Handles stock Android.
  234. String sdk_version = get_system_property("ro.build.version.sdk_int");
  235. String build = get_system_property("ro.build.version.incremental");
  236. if (!sdk_version.is_empty()) {
  237. if (!build.is_empty()) {
  238. return vformat("%s.%s", sdk_version, build);
  239. }
  240. return sdk_version;
  241. }
  242. return "";
  243. }
  244. MainLoop *OS_Android::get_main_loop() const {
  245. return main_loop;
  246. }
  247. void OS_Android::main_loop_begin() {
  248. if (main_loop) {
  249. main_loop->initialize();
  250. }
  251. }
  252. bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
  253. if (!main_loop) {
  254. return false;
  255. }
  256. DisplayServerAndroid::get_singleton()->reset_swap_buffers_flag();
  257. DisplayServerAndroid::get_singleton()->process_events();
  258. uint64_t current_frames_drawn = Engine::get_singleton()->get_frames_drawn();
  259. bool exit = Main::iteration();
  260. if (r_should_swap_buffers) {
  261. *r_should_swap_buffers = !is_in_low_processor_usage_mode() ||
  262. DisplayServerAndroid::get_singleton()->should_swap_buffers() ||
  263. RenderingServer::get_singleton()->has_changed() ||
  264. current_frames_drawn != Engine::get_singleton()->get_frames_drawn();
  265. }
  266. return exit;
  267. }
  268. void OS_Android::main_loop_end() {
  269. if (main_loop) {
  270. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  271. if (scene_tree) {
  272. scene_tree->quit();
  273. }
  274. main_loop->finalize();
  275. }
  276. }
  277. void OS_Android::main_loop_focusout() {
  278. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  279. audio_driver_android.set_pause(true);
  280. }
  281. void OS_Android::main_loop_focusin() {
  282. DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  283. audio_driver_android.set_pause(false);
  284. }
  285. Error OS_Android::shell_open(String p_uri) {
  286. return godot_io_java->open_uri(p_uri);
  287. }
  288. String OS_Android::get_resource_dir() const {
  289. #ifdef TOOLS_ENABLED
  290. return OS_Unix::get_resource_dir();
  291. #else
  292. if (remote_fs_dir.is_empty()) {
  293. return "/"; // Android has its own filesystem for resources inside the APK
  294. } else {
  295. return remote_fs_dir;
  296. }
  297. #endif
  298. }
  299. String OS_Android::get_locale() const {
  300. String locale = godot_io_java->get_locale();
  301. if (!locale.is_empty()) {
  302. return locale;
  303. }
  304. return OS_Unix::get_locale();
  305. }
  306. String OS_Android::get_model_name() const {
  307. String model = godot_io_java->get_model();
  308. if (!model.is_empty()) {
  309. return model;
  310. }
  311. return OS_Unix::get_model_name();
  312. }
  313. String OS_Android::get_data_path() const {
  314. return get_user_data_dir();
  315. }
  316. void OS_Android::_load_system_font_config() {
  317. font_aliases.clear();
  318. fonts.clear();
  319. font_names.clear();
  320. Ref<XMLParser> parser;
  321. parser.instantiate();
  322. Error err = parser->open(String(getenv("ANDROID_ROOT")).path_join("/etc/fonts.xml"));
  323. if (err == OK) {
  324. bool in_font_node = false;
  325. String fb, fn;
  326. FontInfo fi;
  327. while (parser->read() == OK) {
  328. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  329. in_font_node = false;
  330. if (parser->get_node_name() == "familyset") {
  331. int ver = parser->has_attribute("version") ? parser->get_named_attribute_value("version").to_int() : 0;
  332. if (ver < 21) {
  333. ERR_PRINT(vformat("Unsupported font config version %s", ver));
  334. break;
  335. }
  336. } else if (parser->get_node_name() == "alias") {
  337. String name = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
  338. String to = parser->has_attribute("to") ? parser->get_named_attribute_value("to").strip_edges() : String();
  339. if (!name.is_empty() && !to.is_empty()) {
  340. font_aliases[name] = to;
  341. }
  342. } else if (parser->get_node_name() == "family") {
  343. fn = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
  344. String lang_code = parser->has_attribute("lang") ? parser->get_named_attribute_value("lang").strip_edges() : String();
  345. Vector<String> lang_codes = lang_code.split(",");
  346. for (int i = 0; i < lang_codes.size(); i++) {
  347. Vector<String> lang_code_elements = lang_codes[i].split("-");
  348. if (lang_code_elements.size() >= 1 && lang_code_elements[0] != "und") {
  349. // Add missing script codes.
  350. if (lang_code_elements[0] == "ko") {
  351. fi.script.insert("Hani");
  352. fi.script.insert("Hang");
  353. }
  354. if (lang_code_elements[0] == "ja") {
  355. fi.script.insert("Hani");
  356. fi.script.insert("Kana");
  357. fi.script.insert("Hira");
  358. }
  359. if (!lang_code_elements[0].is_empty()) {
  360. fi.lang.insert(lang_code_elements[0]);
  361. }
  362. }
  363. if (lang_code_elements.size() >= 2) {
  364. // Add common codes for variants and remove variants not supported by HarfBuzz/ICU.
  365. if (lang_code_elements[1] == "Aran") {
  366. fi.script.insert("Arab");
  367. }
  368. if (lang_code_elements[1] == "Cyrs") {
  369. fi.script.insert("Cyrl");
  370. }
  371. if (lang_code_elements[1] == "Hanb") {
  372. fi.script.insert("Hani");
  373. fi.script.insert("Bopo");
  374. }
  375. if (lang_code_elements[1] == "Hans" || lang_code_elements[1] == "Hant") {
  376. fi.script.insert("Hani");
  377. }
  378. if (lang_code_elements[1] == "Syrj" || lang_code_elements[1] == "Syre" || lang_code_elements[1] == "Syrn") {
  379. fi.script.insert("Syrc");
  380. }
  381. if (!lang_code_elements[1].is_empty() && lang_code_elements[1] != "Zsym" && lang_code_elements[1] != "Zsye" && lang_code_elements[1] != "Zmth") {
  382. fi.script.insert(lang_code_elements[1]);
  383. }
  384. }
  385. }
  386. } else if (parser->get_node_name() == "font") {
  387. in_font_node = true;
  388. fb = parser->has_attribute("fallbackFor") ? parser->get_named_attribute_value("fallbackFor").strip_edges() : String();
  389. fi.weight = parser->has_attribute("weight") ? parser->get_named_attribute_value("weight").to_int() : 400;
  390. fi.italic = parser->has_attribute("style") && parser->get_named_attribute_value("style").strip_edges() == "italic";
  391. }
  392. }
  393. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  394. if (in_font_node) {
  395. fi.filename = parser->get_node_data().strip_edges();
  396. fi.font_name = fn;
  397. if (!fb.is_empty() && fn.is_empty()) {
  398. fi.font_name = fb;
  399. fi.priority = 2;
  400. }
  401. if (fi.font_name.is_empty()) {
  402. fi.font_name = "sans-serif";
  403. fi.priority = 5;
  404. }
  405. if (fi.font_name.ends_with("-condensed")) {
  406. fi.stretch = 75;
  407. fi.font_name = fi.font_name.trim_suffix("-condensed");
  408. }
  409. fonts.push_back(fi);
  410. font_names.insert(fi.font_name);
  411. }
  412. }
  413. if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) {
  414. in_font_node = false;
  415. if (parser->get_node_name() == "font") {
  416. fb = String();
  417. fi.font_name = String();
  418. fi.priority = 0;
  419. fi.weight = 400;
  420. fi.stretch = 100;
  421. fi.italic = false;
  422. } else if (parser->get_node_name() == "family") {
  423. fi = FontInfo();
  424. fn = String();
  425. }
  426. }
  427. }
  428. parser->close();
  429. } else {
  430. ERR_PRINT("Unable to load font config");
  431. }
  432. font_config_loaded = true;
  433. }
  434. Vector<String> OS_Android::get_system_fonts() const {
  435. if (!font_config_loaded) {
  436. const_cast<OS_Android *>(this)->_load_system_font_config();
  437. }
  438. Vector<String> ret;
  439. for (const String &E : font_names) {
  440. ret.push_back(E);
  441. }
  442. return ret;
  443. }
  444. 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 {
  445. if (!font_config_loaded) {
  446. const_cast<OS_Android *>(this)->_load_system_font_config();
  447. }
  448. String font_name = p_font_name.to_lower();
  449. if (font_aliases.has(font_name)) {
  450. font_name = font_aliases[font_name];
  451. }
  452. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  453. String lang_prefix = p_locale.split("_")[0];
  454. Vector<String> ret;
  455. int best_score = 0;
  456. for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
  457. int score = 0;
  458. if (!E->get().script.is_empty() && !p_script.is_empty() && !E->get().script.has(p_script)) {
  459. continue;
  460. }
  461. float sim = E->get().font_name.similarity(font_name);
  462. if (sim > 0.0) {
  463. score += (60 * sim + 5 - E->get().priority);
  464. }
  465. if (E->get().lang.has(p_locale)) {
  466. score += 120;
  467. } else if (E->get().lang.has(lang_prefix)) {
  468. score += 115;
  469. }
  470. if (E->get().script.has(p_script)) {
  471. score += 240;
  472. }
  473. score += (20 - Math::abs(E->get().weight - p_weight) / 50);
  474. score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
  475. if (E->get().italic == p_italic) {
  476. score += 30;
  477. }
  478. if (score > best_score) {
  479. best_score = score;
  480. if (ret.find(root.path_join(E->get().filename)) < 0) {
  481. ret.insert(0, root.path_join(E->get().filename));
  482. }
  483. } else if (score == best_score || E->get().script.is_empty()) {
  484. if (ret.find(root.path_join(E->get().filename)) < 0) {
  485. ret.push_back(root.path_join(E->get().filename));
  486. }
  487. }
  488. if (score >= 490) {
  489. break; // Perfect match.
  490. }
  491. }
  492. return ret;
  493. }
  494. String OS_Android::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  495. if (!font_config_loaded) {
  496. const_cast<OS_Android *>(this)->_load_system_font_config();
  497. }
  498. String font_name = p_font_name.to_lower();
  499. if (font_aliases.has(font_name)) {
  500. font_name = font_aliases[font_name];
  501. }
  502. String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
  503. int best_score = 0;
  504. const List<FontInfo>::Element *best_match = nullptr;
  505. for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
  506. int score = 0;
  507. if (E->get().font_name == font_name) {
  508. score += (65 - E->get().priority);
  509. }
  510. score += (20 - Math::abs(E->get().weight - p_weight) / 50);
  511. score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
  512. if (E->get().italic == p_italic) {
  513. score += 30;
  514. }
  515. if (score >= 60 && score > best_score) {
  516. best_score = score;
  517. best_match = E;
  518. }
  519. if (score >= 140) {
  520. break; // Perfect match.
  521. }
  522. }
  523. if (best_match) {
  524. return root.path_join(best_match->get().filename);
  525. }
  526. return String();
  527. }
  528. String OS_Android::get_executable_path() const {
  529. // Since unix process creation is restricted on Android, we bypass
  530. // OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
  531. // Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
  532. // manner.
  533. return OS::get_executable_path();
  534. }
  535. String OS_Android::get_user_data_dir() const {
  536. if (!data_dir_cache.is_empty()) {
  537. return data_dir_cache;
  538. }
  539. String data_dir = godot_io_java->get_user_data_dir();
  540. if (!data_dir.is_empty()) {
  541. data_dir_cache = _remove_symlink(data_dir);
  542. return data_dir_cache;
  543. }
  544. return ".";
  545. }
  546. String OS_Android::get_dynamic_libraries_path() const {
  547. return get_cache_path().path_join("dynamic_libraries");
  548. }
  549. String OS_Android::get_cache_path() const {
  550. if (!cache_dir_cache.is_empty()) {
  551. return cache_dir_cache;
  552. }
  553. String cache_dir = godot_io_java->get_cache_dir();
  554. if (!cache_dir.is_empty()) {
  555. cache_dir_cache = _remove_symlink(cache_dir);
  556. return cache_dir_cache;
  557. }
  558. return ".";
  559. }
  560. String OS_Android::get_unique_id() const {
  561. String unique_id = godot_io_java->get_unique_id();
  562. if (!unique_id.is_empty()) {
  563. return unique_id;
  564. }
  565. return OS::get_unique_id();
  566. }
  567. String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  568. return godot_io_java->get_system_dir(p_dir, p_shared_storage);
  569. }
  570. Error OS_Android::move_to_trash(const String &p_path) {
  571. Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
  572. if (da_ref.is_null()) {
  573. return FAILED;
  574. }
  575. // Check if it's a directory
  576. if (da_ref->dir_exists(p_path)) {
  577. Error err = da_ref->change_dir(p_path);
  578. if (err) {
  579. return err;
  580. }
  581. // This is directory, let's erase its contents
  582. err = da_ref->erase_contents_recursive();
  583. if (err) {
  584. return err;
  585. }
  586. // Remove the top directory
  587. return da_ref->remove(p_path);
  588. } else if (da_ref->file_exists(p_path)) {
  589. // This is a file, let's remove it.
  590. return da_ref->remove(p_path);
  591. } else {
  592. return FAILED;
  593. }
  594. }
  595. void OS_Android::set_display_size(const Size2i &p_size) {
  596. display_size = p_size;
  597. }
  598. Size2i OS_Android::get_display_size() const {
  599. return display_size;
  600. }
  601. void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
  602. #if defined(GLES3_ENABLED)
  603. ERR_FAIL_NULL(p_gl_extensions);
  604. gl_extensions = p_gl_extensions;
  605. #endif
  606. }
  607. void OS_Android::set_native_window(ANativeWindow *p_native_window) {
  608. #if defined(VULKAN_ENABLED)
  609. native_window = p_native_window;
  610. #endif
  611. }
  612. ANativeWindow *OS_Android::get_native_window() const {
  613. #if defined(VULKAN_ENABLED)
  614. return native_window;
  615. #else
  616. return nullptr;
  617. #endif
  618. }
  619. void OS_Android::vibrate_handheld(int p_duration_ms) {
  620. godot_java->vibrate(p_duration_ms);
  621. }
  622. String OS_Android::get_config_path() const {
  623. return get_user_data_dir().path_join("config");
  624. }
  625. void OS_Android::benchmark_begin_measure(const String &p_what) {
  626. #ifdef TOOLS_ENABLED
  627. godot_java->begin_benchmark_measure(p_what);
  628. #endif
  629. }
  630. void OS_Android::benchmark_end_measure(const String &p_what) {
  631. #ifdef TOOLS_ENABLED
  632. godot_java->end_benchmark_measure(p_what);
  633. #endif
  634. }
  635. void OS_Android::benchmark_dump() {
  636. #ifdef TOOLS_ENABLED
  637. if (!is_use_benchmark_set()) {
  638. return;
  639. }
  640. godot_java->dump_benchmark(get_benchmark_file());
  641. #endif
  642. }
  643. bool OS_Android::_check_internal_feature_support(const String &p_feature) {
  644. if (p_feature == "system_fonts") {
  645. return true;
  646. }
  647. if (p_feature == "mobile") {
  648. return true;
  649. }
  650. #if defined(__aarch64__)
  651. if (p_feature == "arm64-v8a" || p_feature == "arm64") {
  652. return true;
  653. }
  654. #elif defined(__ARM_ARCH_7A__)
  655. if (p_feature == "armeabi-v7a" || p_feature == "armeabi" || p_feature == "arm32") {
  656. return true;
  657. }
  658. #elif defined(__arm__)
  659. if (p_feature == "armeabi" || p_feature == "arm") {
  660. return true;
  661. }
  662. #endif
  663. if (godot_java->has_feature(p_feature)) {
  664. return true;
  665. }
  666. return false;
  667. }
  668. OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
  669. display_size.width = DEFAULT_WINDOW_WIDTH;
  670. display_size.height = DEFAULT_WINDOW_HEIGHT;
  671. use_apk_expansion = p_use_apk_expansion;
  672. main_loop = nullptr;
  673. #if defined(GLES3_ENABLED)
  674. gl_extensions = nullptr;
  675. #endif
  676. #if defined(VULKAN_ENABLED)
  677. native_window = nullptr;
  678. #endif
  679. godot_java = p_godot_java;
  680. godot_io_java = p_godot_io_java;
  681. Vector<Logger *> loggers;
  682. loggers.push_back(memnew(AndroidLogger));
  683. _set_logger(memnew(CompositeLogger(loggers)));
  684. AudioDriverManager::add_driver(&audio_driver_android);
  685. DisplayServerAndroid::register_android_driver();
  686. }
  687. 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) {
  688. if (p_path == ANDROID_EXEC_PATH) {
  689. return create_instance(p_arguments);
  690. } else {
  691. return OS_Unix::execute(p_path, p_arguments, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
  692. }
  693. }
  694. Error OS_Android::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
  695. if (p_path == ANDROID_EXEC_PATH) {
  696. return create_instance(p_arguments, r_child_id);
  697. } else {
  698. return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console);
  699. }
  700. }
  701. Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
  702. int instance_id = godot_java->create_new_godot_instance(p_arguments);
  703. if (r_child_id) {
  704. *r_child_id = instance_id;
  705. }
  706. return OK;
  707. }
  708. Error OS_Android::kill(const ProcessID &p_pid) {
  709. if (godot_java->force_quit(nullptr, p_pid)) {
  710. return OK;
  711. }
  712. return OS_Unix::kill(p_pid);
  713. }
  714. String OS_Android::get_system_ca_certificates() {
  715. return godot_java->get_ca_certificates();
  716. }
  717. Error OS_Android::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
  718. r_project_path = get_user_data_dir();
  719. Error err = OS_Unix::setup_remote_filesystem(p_server_host, p_port, p_password, r_project_path);
  720. if (err == OK) {
  721. remote_fs_dir = r_project_path;
  722. FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_RESOURCES);
  723. }
  724. return err;
  725. }
  726. void OS_Android::load_platform_gdextensions() const {
  727. Vector<String> extension_list_config_file = godot_java->get_gdextension_list_config_file();
  728. for (String config_file_path : extension_list_config_file) {
  729. GDExtensionManager::LoadStatus err = GDExtensionManager::get_singleton()->load_extension(config_file_path);
  730. ERR_CONTINUE_MSG(err == GDExtensionManager::LOAD_STATUS_FAILED, "Error loading platform extension: " + config_file_path);
  731. }
  732. }
  733. OS_Android::~OS_Android() {
  734. }