2
0

os_android.cpp 26 KB

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