Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/os_android.cpp
10277 views
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
31
#include "os_android.h"
32
33
#include "dir_access_jandroid.h"
34
#include "display_server_android.h"
35
#include "file_access_android.h"
36
#include "file_access_filesystem_jandroid.h"
37
#include "java_godot_io_wrapper.h"
38
#include "java_godot_wrapper.h"
39
#include "net_socket_android.h"
40
41
#include "core/config/project_settings.h"
42
#include "core/extension/gdextension_manager.h"
43
#include "core/io/xml_parser.h"
44
#include "drivers/unix/dir_access_unix.h"
45
#include "drivers/unix/file_access_unix.h"
46
#ifdef TOOLS_ENABLED
47
#include "editor/editor_node.h"
48
#include "editor/run/game_view_plugin.h"
49
#endif
50
#include "main/main.h"
51
#include "scene/main/scene_tree.h"
52
#include "servers/rendering_server.h"
53
54
#include <dlfcn.h>
55
#include <sys/system_properties.h>
56
57
const char *OS_Android::ANDROID_EXEC_PATH = "apk";
58
59
String _remove_symlink(const String &dir) {
60
// Workaround for Android 6.0+ using a symlink.
61
// Save the current directory.
62
char current_dir_name[2048];
63
getcwd(current_dir_name, 2048);
64
// Change directory to the external data directory.
65
chdir(dir.utf8().get_data());
66
// Get the actual directory without the potential symlink.
67
char dir_name_without_symlink[2048];
68
getcwd(dir_name_without_symlink, 2048);
69
// Convert back to a String.
70
String dir_without_symlink(dir_name_without_symlink);
71
// Restore original current directory.
72
chdir(current_dir_name);
73
return dir_without_symlink;
74
}
75
76
class AndroidLogger : public Logger {
77
public:
78
virtual void logv(const char *p_format, va_list p_list, bool p_err) {
79
__android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "godot", p_format, p_list);
80
}
81
82
virtual ~AndroidLogger() {}
83
};
84
85
void OS_Android::alert(const String &p_alert, const String &p_title) {
86
ERR_FAIL_NULL(godot_java);
87
godot_java->alert(p_alert, p_title);
88
}
89
90
void OS_Android::initialize_core() {
91
OS_Unix::initialize_core();
92
93
#ifdef TOOLS_ENABLED
94
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
95
#else
96
if (use_apk_expansion) {
97
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
98
} else {
99
FileAccess::make_default<FileAccessAndroid>(FileAccess::ACCESS_RESOURCES);
100
}
101
#endif
102
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
103
FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_FILESYSTEM);
104
105
#ifdef TOOLS_ENABLED
106
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
107
#else
108
if (use_apk_expansion) {
109
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
110
} else {
111
DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
112
}
113
#endif
114
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
115
DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_FILESYSTEM);
116
117
NetSocketAndroid::make_default();
118
}
119
120
void OS_Android::initialize() {
121
initialize_core();
122
}
123
124
void OS_Android::initialize_joypads() {
125
Input::get_singleton()->set_fallback_mapping(godot_java->get_input_fallback_mapping());
126
127
// This queries/updates the currently connected devices/joypads.
128
godot_java->init_input_devices();
129
}
130
131
void OS_Android::set_main_loop(MainLoop *p_main_loop) {
132
main_loop = p_main_loop;
133
}
134
135
void OS_Android::delete_main_loop() {
136
if (main_loop) {
137
memdelete(main_loop);
138
main_loop = nullptr;
139
}
140
}
141
142
void OS_Android::finalize() {
143
}
144
145
OS_Android *OS_Android::get_singleton() {
146
return static_cast<OS_Android *>(OS::get_singleton());
147
}
148
149
GodotJavaWrapper *OS_Android::get_godot_java() {
150
return godot_java;
151
}
152
153
GodotIOJavaWrapper *OS_Android::get_godot_io_java() {
154
return godot_io_java;
155
}
156
157
bool OS_Android::request_permission(const String &p_name) {
158
return godot_java->request_permission(p_name);
159
}
160
161
bool OS_Android::request_permissions() {
162
return godot_java->request_permissions();
163
}
164
165
Vector<String> OS_Android::get_granted_permissions() const {
166
return godot_java->get_granted_permissions();
167
}
168
169
bool OS_Android::copy_dynamic_library(const String &p_library_path, const String &p_target_dir, String *r_copy_path) {
170
if (!FileAccess::exists(p_library_path)) {
171
return false;
172
}
173
174
Ref<DirAccess> da_ref = DirAccess::create_for_path(p_library_path);
175
if (da_ref.is_null()) {
176
return false;
177
}
178
179
String copy_path = p_target_dir.path_join(p_library_path.get_file());
180
bool copy_exists = FileAccess::exists(copy_path);
181
if (copy_exists) {
182
print_verbose("Deleting existing library copy " + copy_path);
183
if (da_ref->remove(copy_path) != OK) {
184
print_verbose("Unable to delete " + copy_path);
185
}
186
}
187
188
print_verbose("Copying " + p_library_path + " to " + p_target_dir);
189
Error create_dir_result = da_ref->make_dir_recursive(p_target_dir);
190
if (create_dir_result == OK || create_dir_result == ERR_ALREADY_EXISTS) {
191
copy_exists = da_ref->copy(p_library_path, copy_path) == OK;
192
}
193
194
if (copy_exists && r_copy_path != nullptr) {
195
*r_copy_path = copy_path;
196
}
197
198
return copy_exists;
199
}
200
201
Error OS_Android::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
202
String path = p_path;
203
bool so_file_exists = true;
204
if (!FileAccess::exists(path)) {
205
path = p_path.get_file();
206
so_file_exists = false;
207
}
208
209
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
210
if (!p_library_handle && so_file_exists) {
211
// The library (and its dependencies) may be on the sdcard and thus inaccessible.
212
// Try to copy to the internal directory for access.
213
const String dynamic_library_path = get_dynamic_libraries_path();
214
215
if (p_data != nullptr && p_data->library_dependencies != nullptr && !p_data->library_dependencies->is_empty()) {
216
// Copy the library dependencies
217
print_verbose("Copying library dependencies..");
218
for (const String &library_dependency_path : *p_data->library_dependencies) {
219
String internal_library_dependency_path;
220
if (!copy_dynamic_library(library_dependency_path, dynamic_library_path.path_join(library_dependency_path.get_base_dir()), &internal_library_dependency_path)) {
221
ERR_PRINT(vformat("Unable to copy library dependency %s", library_dependency_path));
222
} else {
223
void *lib_dependency_handle = dlopen(internal_library_dependency_path.utf8().get_data(), RTLD_NOW);
224
if (!lib_dependency_handle) {
225
ERR_PRINT(vformat("Can't open dynamic library dependency: %s. Error: %s.", internal_library_dependency_path, dlerror()));
226
}
227
}
228
}
229
}
230
231
String internal_path;
232
print_verbose("Copying library " + p_path);
233
const bool internal_so_file_exists = copy_dynamic_library(p_path, dynamic_library_path.path_join(p_path.get_base_dir()), &internal_path);
234
235
if (internal_so_file_exists) {
236
print_verbose("Opening library " + internal_path);
237
p_library_handle = dlopen(internal_path.utf8().get_data(), RTLD_NOW);
238
if (p_library_handle) {
239
path = internal_path;
240
}
241
}
242
}
243
244
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
245
246
if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
247
*p_data->r_resolved_path = path;
248
}
249
250
return OK;
251
}
252
253
String OS_Android::get_name() const {
254
return "Android";
255
}
256
257
String OS_Android::get_system_property(const char *key) const {
258
String value;
259
char value_str[PROP_VALUE_MAX];
260
if (__system_property_get(key, value_str)) {
261
value = String(value_str);
262
}
263
return value;
264
}
265
266
String OS_Android::get_distribution_name() const {
267
if (!get_system_property("ro.havoc.version").is_empty()) {
268
return "Havoc OS";
269
} else if (!get_system_property("org.pex.version").is_empty()) { // Putting before "Pixel Experience", because it's derivating from it.
270
return "Pixel Extended";
271
} else if (!get_system_property("org.pixelexperience.version").is_empty()) {
272
return "Pixel Experience";
273
} else if (!get_system_property("ro.potato.version").is_empty()) {
274
return "POSP";
275
} else if (!get_system_property("ro.xtended.version").is_empty()) {
276
return "Project-Xtended";
277
} else if (!get_system_property("org.evolution.version").is_empty()) {
278
return "Evolution X";
279
} else if (!get_system_property("ro.corvus.version").is_empty()) {
280
return "Corvus-Q";
281
} else if (!get_system_property("ro.pa.version").is_empty()) {
282
return "Paranoid Android";
283
} else if (!get_system_property("ro.crdroid.version").is_empty()) {
284
return "crDroid Android";
285
} else if (!get_system_property("ro.syberia.version").is_empty()) {
286
return "Syberia Project";
287
} else if (!get_system_property("ro.arrow.version").is_empty()) {
288
return "ArrowOS";
289
} else if (!get_system_property("ro.lineage.version").is_empty()) { // Putting LineageOS last, just in case any derivative writes to "ro.lineage.version".
290
return "LineageOS";
291
}
292
293
if (!get_system_property("ro.modversion").is_empty()) { // Handles other Android custom ROMs.
294
return vformat("%s %s", get_name(), "Custom ROM");
295
}
296
297
// Handles stock Android.
298
return get_name();
299
}
300
301
String OS_Android::get_version() const {
302
const Vector<const char *> roms = { "ro.havoc.version", "org.pex.version", "org.pixelexperience.version",
303
"ro.potato.version", "ro.xtended.version", "org.evolution.version", "ro.corvus.version", "ro.pa.version",
304
"ro.crdroid.version", "ro.syberia.version", "ro.arrow.version", "ro.lineage.version" };
305
for (int i = 0; i < roms.size(); i++) {
306
String rom_version = get_system_property(roms[i]);
307
if (!rom_version.is_empty()) {
308
return rom_version;
309
}
310
}
311
312
String mod_version = get_system_property("ro.modversion"); // Handles other Android custom ROMs.
313
if (!mod_version.is_empty()) {
314
return mod_version;
315
}
316
317
// Handles stock Android.
318
String sdk_version = get_system_property("ro.build.version.sdk");
319
String build = get_system_property("ro.build.version.incremental");
320
if (!sdk_version.is_empty()) {
321
if (!build.is_empty()) {
322
return vformat("%s.%s", sdk_version, build);
323
}
324
return sdk_version;
325
}
326
327
return "";
328
}
329
330
String OS_Android::get_version_alias() const {
331
String release = get_system_property("ro.build.version.release_or_codename");
332
String sdk_version = get_system_property("ro.build.version.sdk");
333
String build = get_system_property("ro.build.version.incremental");
334
335
return vformat("%s (SDK %s build %s)", release, sdk_version, build);
336
}
337
338
MainLoop *OS_Android::get_main_loop() const {
339
return main_loop;
340
}
341
342
void OS_Android::main_loop_begin() {
343
if (main_loop) {
344
main_loop->initialize();
345
}
346
347
#ifdef TOOLS_ENABLED
348
if (Engine::get_singleton()->is_editor_hint()) {
349
GameViewPlugin *game_view_plugin = Object::cast_to<GameViewPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_plugin_by_name("Game"));
350
if (game_view_plugin != nullptr) {
351
game_view_plugin->connect("main_screen_changed", callable_mp_static(&OS_Android::_on_main_screen_changed));
352
}
353
}
354
#endif
355
}
356
357
bool OS_Android::main_loop_iterate(bool *r_should_swap_buffers) {
358
if (!main_loop) {
359
return false;
360
}
361
DisplayServerAndroid::get_singleton()->reset_swap_buffers_flag();
362
DisplayServerAndroid::get_singleton()->process_events();
363
uint64_t current_frames_drawn = Engine::get_singleton()->get_frames_drawn();
364
bool exit = Main::iteration();
365
366
if (r_should_swap_buffers) {
367
*r_should_swap_buffers = !is_in_low_processor_usage_mode() ||
368
DisplayServerAndroid::get_singleton()->should_swap_buffers() ||
369
RenderingServer::get_singleton()->has_changed() ||
370
current_frames_drawn != Engine::get_singleton()->get_frames_drawn();
371
}
372
373
return exit;
374
}
375
376
void OS_Android::main_loop_end() {
377
#ifdef TOOLS_ENABLED
378
if (Engine::get_singleton()->is_editor_hint()) {
379
GameViewPlugin *game_view_plugin = Object::cast_to<GameViewPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_plugin_by_name("Game"));
380
if (game_view_plugin != nullptr) {
381
game_view_plugin->disconnect("main_screen_changed", callable_mp_static(&OS_Android::_on_main_screen_changed));
382
}
383
}
384
#endif
385
386
if (main_loop) {
387
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
388
if (scene_tree) {
389
scene_tree->quit();
390
}
391
main_loop->finalize();
392
}
393
}
394
395
#ifdef TOOLS_ENABLED
396
void OS_Android::_on_main_screen_changed(const String &p_screen_name) {
397
if (OS_Android::get_singleton() != nullptr && OS_Android::get_singleton()->get_godot_java() != nullptr) {
398
OS_Android::get_singleton()->get_godot_java()->on_editor_workspace_selected(p_screen_name);
399
}
400
}
401
#endif
402
403
void OS_Android::main_loop_focusout() {
404
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
405
if (OS::get_singleton()->get_main_loop()) {
406
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
407
}
408
audio_driver_android.set_pause(true);
409
}
410
411
void OS_Android::main_loop_focusin() {
412
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
413
if (OS::get_singleton()->get_main_loop()) {
414
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
415
}
416
audio_driver_android.set_pause(false);
417
}
418
419
Error OS_Android::shell_open(const String &p_uri) {
420
return godot_io_java->open_uri(p_uri);
421
}
422
423
String OS_Android::get_resource_dir() const {
424
#ifdef TOOLS_ENABLED
425
return OS_Unix::get_resource_dir();
426
#else
427
if (remote_fs_dir.is_empty()) {
428
return "/"; // Android has its own filesystem for resources inside the APK
429
} else {
430
return remote_fs_dir;
431
}
432
#endif
433
}
434
435
String OS_Android::get_locale() const {
436
String locale = godot_io_java->get_locale();
437
if (!locale.is_empty()) {
438
return locale;
439
}
440
441
return OS_Unix::get_locale();
442
}
443
444
String OS_Android::get_model_name() const {
445
String model = godot_io_java->get_model();
446
if (!model.is_empty()) {
447
return model;
448
}
449
450
return OS_Unix::get_model_name();
451
}
452
453
String OS_Android::get_data_path() const {
454
return OS::get_user_data_dir();
455
}
456
457
void OS_Android::_load_system_font_config() const {
458
font_aliases.clear();
459
fonts.clear();
460
font_names.clear();
461
462
Ref<XMLParser> parser;
463
parser.instantiate();
464
465
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
466
String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
467
468
Error err = parser->open(String(getenv("ANDROID_ROOT")).path_join("/etc/fonts.xml"));
469
if (err == OK) {
470
bool in_font_node = false;
471
String fb, fn;
472
FontInfo fi;
473
474
while (parser->read() == OK) {
475
if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
476
in_font_node = false;
477
if (parser->get_node_name() == "familyset") {
478
int ver = parser->has_attribute("version") ? parser->get_named_attribute_value("version").to_int() : 0;
479
if (ver < 21) {
480
ERR_PRINT(vformat("Unsupported font config version %s", ver));
481
break;
482
}
483
} else if (parser->get_node_name() == "alias") {
484
String name = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
485
String to = parser->has_attribute("to") ? parser->get_named_attribute_value("to").strip_edges() : String();
486
if (!name.is_empty() && !to.is_empty()) {
487
font_aliases[name] = to;
488
}
489
} else if (parser->get_node_name() == "family") {
490
fn = parser->has_attribute("name") ? parser->get_named_attribute_value("name").strip_edges() : String();
491
String lang_code = parser->has_attribute("lang") ? parser->get_named_attribute_value("lang").strip_edges() : String();
492
Vector<String> lang_codes = lang_code.split(",");
493
for (int i = 0; i < lang_codes.size(); i++) {
494
Vector<String> lang_code_elements = lang_codes[i].split("-");
495
if (lang_code_elements.size() >= 1 && lang_code_elements[0] != "und") {
496
// Add missing script codes.
497
if (lang_code_elements[0] == "ko") {
498
fi.script.insert("Hani");
499
fi.script.insert("Hang");
500
}
501
if (lang_code_elements[0] == "ja") {
502
fi.script.insert("Hani");
503
fi.script.insert("Kana");
504
fi.script.insert("Hira");
505
}
506
if (!lang_code_elements[0].is_empty()) {
507
fi.lang.insert(lang_code_elements[0]);
508
}
509
}
510
if (lang_code_elements.size() >= 2) {
511
// Add common codes for variants and remove variants not supported by HarfBuzz/ICU.
512
if (lang_code_elements[1] == "Aran") {
513
fi.script.insert("Arab");
514
}
515
if (lang_code_elements[1] == "Cyrs") {
516
fi.script.insert("Cyrl");
517
}
518
if (lang_code_elements[1] == "Hanb") {
519
fi.script.insert("Hani");
520
fi.script.insert("Bopo");
521
}
522
if (lang_code_elements[1] == "Hans" || lang_code_elements[1] == "Hant") {
523
fi.script.insert("Hani");
524
}
525
if (lang_code_elements[1] == "Syrj" || lang_code_elements[1] == "Syre" || lang_code_elements[1] == "Syrn") {
526
fi.script.insert("Syrc");
527
}
528
if (!lang_code_elements[1].is_empty() && lang_code_elements[1] != "Zsym" && lang_code_elements[1] != "Zsye" && lang_code_elements[1] != "Zmth") {
529
fi.script.insert(lang_code_elements[1]);
530
}
531
}
532
}
533
} else if (parser->get_node_name() == "font") {
534
in_font_node = true;
535
fb = parser->has_attribute("fallbackFor") ? parser->get_named_attribute_value("fallbackFor").strip_edges() : String();
536
fi.weight = parser->has_attribute("weight") ? parser->get_named_attribute_value("weight").to_int() : 400;
537
fi.italic = parser->has_attribute("style") && parser->get_named_attribute_value("style").strip_edges() == "italic";
538
}
539
}
540
if (parser->get_node_type() == XMLParser::NODE_TEXT) {
541
if (in_font_node) {
542
fi.filename = parser->get_node_data().strip_edges();
543
fi.font_name = fn;
544
if (da->file_exists(root.path_join(fi.filename))) {
545
if (!fb.is_empty() && fn.is_empty()) {
546
fi.font_name = fb;
547
fi.priority = 2;
548
}
549
if (fi.font_name.is_empty()) {
550
fi.font_name = "sans-serif";
551
fi.priority = 5;
552
}
553
if (fi.font_name.ends_with("-condensed")) {
554
fi.stretch = 75;
555
fi.font_name = fi.font_name.trim_suffix("-condensed");
556
}
557
fonts.push_back(fi);
558
font_names.insert(fi.font_name);
559
}
560
}
561
}
562
if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) {
563
in_font_node = false;
564
if (parser->get_node_name() == "font") {
565
fb = String();
566
fi.font_name = String();
567
fi.priority = 0;
568
fi.weight = 400;
569
fi.stretch = 100;
570
fi.italic = false;
571
} else if (parser->get_node_name() == "family") {
572
fi = FontInfo();
573
fn = String();
574
}
575
}
576
}
577
parser->close();
578
} else {
579
ERR_PRINT("Unable to load font config");
580
}
581
582
font_config_loaded = true;
583
}
584
585
Vector<String> OS_Android::get_system_fonts() const {
586
if (!font_config_loaded) {
587
_load_system_font_config();
588
}
589
Vector<String> ret;
590
for (const String &E : font_names) {
591
ret.push_back(E);
592
}
593
return ret;
594
}
595
596
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 {
597
if (!font_config_loaded) {
598
_load_system_font_config();
599
}
600
String font_name = p_font_name.to_lower();
601
if (font_aliases.has(font_name)) {
602
font_name = font_aliases[font_name];
603
}
604
String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
605
String lang_prefix = p_locale.split("_")[0];
606
Vector<String> ret;
607
int best_score = 0;
608
for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
609
int score = 0;
610
if (!E->get().script.is_empty() && !p_script.is_empty() && !E->get().script.has(p_script)) {
611
continue;
612
}
613
float sim = E->get().font_name.similarity(font_name);
614
if (sim > 0.0) {
615
score += (60 * sim + 5 - E->get().priority);
616
}
617
if (E->get().lang.has(p_locale)) {
618
score += 120;
619
} else if (E->get().lang.has(lang_prefix)) {
620
score += 115;
621
}
622
if (E->get().script.has(p_script)) {
623
score += 240;
624
}
625
score += (20 - Math::abs(E->get().weight - p_weight) / 50);
626
score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
627
if (E->get().italic == p_italic) {
628
score += 30;
629
}
630
if (score > best_score) {
631
best_score = score;
632
if (!ret.has(root.path_join(E->get().filename))) {
633
ret.insert(0, root.path_join(E->get().filename));
634
}
635
} else if (score == best_score || E->get().script.is_empty()) {
636
if (!ret.has(root.path_join(E->get().filename))) {
637
ret.push_back(root.path_join(E->get().filename));
638
}
639
}
640
if (score >= 490) {
641
break; // Perfect match.
642
}
643
}
644
645
return ret;
646
}
647
648
String OS_Android::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
649
if (!font_config_loaded) {
650
_load_system_font_config();
651
}
652
String font_name = p_font_name.to_lower();
653
if (font_aliases.has(font_name)) {
654
font_name = font_aliases[font_name];
655
}
656
String root = String(getenv("ANDROID_ROOT")).path_join("fonts");
657
658
int best_score = 0;
659
const List<FontInfo>::Element *best_match = nullptr;
660
661
for (const List<FontInfo>::Element *E = fonts.front(); E; E = E->next()) {
662
int score = 0;
663
if (E->get().font_name == font_name) {
664
score += (65 - E->get().priority);
665
}
666
score += (20 - Math::abs(E->get().weight - p_weight) / 50);
667
score += (20 - Math::abs(E->get().stretch - p_stretch) / 10);
668
if (E->get().italic == p_italic) {
669
score += 30;
670
}
671
if (score >= 60 && score > best_score) {
672
best_score = score;
673
best_match = E;
674
}
675
if (score >= 140) {
676
break; // Perfect match.
677
}
678
}
679
if (best_match) {
680
return root.path_join(best_match->get().filename);
681
}
682
return String();
683
}
684
685
String OS_Android::get_executable_path() const {
686
// Since unix process creation is restricted on Android, we bypass
687
// OS_Unix::get_executable_path() so we can return ANDROID_EXEC_PATH.
688
// Detection of ANDROID_EXEC_PATH allows to handle process creation in an Android compliant
689
// manner.
690
return OS::get_executable_path();
691
}
692
693
String OS_Android::get_user_data_dir(const String &p_user_dir) const {
694
if (!data_dir_cache.is_empty()) {
695
return data_dir_cache;
696
}
697
698
String data_dir = godot_io_java->get_user_data_dir(p_user_dir);
699
if (!data_dir.is_empty()) {
700
data_dir_cache = _remove_symlink(data_dir);
701
return data_dir_cache;
702
}
703
return ".";
704
}
705
706
String OS_Android::get_dynamic_libraries_path() const {
707
return get_cache_path().path_join("dynamic_libraries");
708
}
709
710
String OS_Android::get_cache_path() const {
711
if (!cache_dir_cache.is_empty()) {
712
return cache_dir_cache;
713
}
714
715
String cache_dir = godot_io_java->get_cache_dir();
716
if (!cache_dir.is_empty()) {
717
cache_dir_cache = _remove_symlink(cache_dir);
718
return cache_dir_cache;
719
}
720
return ".";
721
}
722
723
String OS_Android::get_temp_path() const {
724
if (!temp_dir_cache.is_empty()) {
725
return temp_dir_cache;
726
}
727
728
String temp_dir = godot_io_java->get_temp_dir();
729
if (!temp_dir.is_empty()) {
730
temp_dir_cache = _remove_symlink(temp_dir);
731
return temp_dir_cache;
732
}
733
return ".";
734
}
735
736
String OS_Android::get_unique_id() const {
737
String unique_id = godot_io_java->get_unique_id();
738
if (!unique_id.is_empty()) {
739
return unique_id;
740
}
741
742
return OS::get_unique_id();
743
}
744
745
String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
746
return godot_io_java->get_system_dir(p_dir, p_shared_storage);
747
}
748
749
Error OS_Android::move_to_trash(const String &p_path) {
750
Ref<DirAccess> da_ref = DirAccess::create_for_path(p_path);
751
if (da_ref.is_null()) {
752
return FAILED;
753
}
754
755
// Check if it's a directory
756
if (da_ref->dir_exists(p_path)) {
757
Error err = da_ref->change_dir(p_path);
758
if (err) {
759
return err;
760
}
761
// This is directory, let's erase its contents
762
err = da_ref->erase_contents_recursive();
763
if (err) {
764
return err;
765
}
766
// Remove the top directory
767
return da_ref->remove(p_path);
768
} else if (da_ref->file_exists(p_path)) {
769
// This is a file, let's remove it.
770
return da_ref->remove(p_path);
771
} else {
772
return FAILED;
773
}
774
}
775
776
void OS_Android::set_display_size(const Size2i &p_size) {
777
display_size = p_size;
778
}
779
780
Size2i OS_Android::get_display_size() const {
781
return display_size;
782
}
783
784
void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {
785
#if defined(GLES3_ENABLED)
786
ERR_FAIL_NULL(p_gl_extensions);
787
gl_extensions = p_gl_extensions;
788
#endif
789
}
790
791
void OS_Android::set_native_window(ANativeWindow *p_native_window) {
792
#if defined(VULKAN_ENABLED)
793
native_window = p_native_window;
794
#endif
795
}
796
797
ANativeWindow *OS_Android::get_native_window() const {
798
#if defined(VULKAN_ENABLED)
799
return native_window;
800
#else
801
return nullptr;
802
#endif
803
}
804
805
void OS_Android::vibrate_handheld(int p_duration_ms, float p_amplitude) {
806
godot_java->vibrate(p_duration_ms, p_amplitude);
807
}
808
809
String OS_Android::get_config_path() const {
810
return OS::get_user_data_dir().path_join("config");
811
}
812
813
void OS_Android::benchmark_begin_measure(const String &p_context, const String &p_what) {
814
#ifdef TOOLS_ENABLED
815
godot_java->begin_benchmark_measure(p_context, p_what);
816
#endif
817
}
818
819
void OS_Android::benchmark_end_measure(const String &p_context, const String &p_what) {
820
#ifdef TOOLS_ENABLED
821
godot_java->end_benchmark_measure(p_context, p_what);
822
#endif
823
}
824
825
void OS_Android::benchmark_dump() {
826
#ifdef TOOLS_ENABLED
827
if (!is_use_benchmark_set()) {
828
return;
829
}
830
godot_java->dump_benchmark(get_benchmark_file());
831
#endif
832
}
833
834
#ifdef TOOLS_ENABLED
835
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) {
836
return godot_java->sign_apk(p_input_path, p_output_path, p_keystore_path, p_keystore_user, p_keystore_password);
837
}
838
839
Error OS_Android::verify_apk(const String &p_apk_path) {
840
return godot_java->verify_apk(p_apk_path);
841
}
842
#endif
843
844
bool OS_Android::_check_internal_feature_support(const String &p_feature) {
845
if (p_feature == "macos" || p_feature == "web_ios" || p_feature == "web_macos" || p_feature == "windows") {
846
return false;
847
}
848
849
if (p_feature == "system_fonts") {
850
return true;
851
}
852
if (p_feature == "mobile") {
853
return true;
854
}
855
#if defined(__aarch64__)
856
if (p_feature == "arm64-v8a" || p_feature == "arm64") {
857
return true;
858
}
859
#elif defined(__ARM_ARCH_7A__)
860
if (p_feature == "armeabi-v7a" || p_feature == "armeabi" || p_feature == "arm32") {
861
return true;
862
}
863
#elif defined(__arm__)
864
if (p_feature == "armeabi" || p_feature == "arm") {
865
return true;
866
}
867
#endif
868
869
if (godot_java->has_feature(p_feature)) {
870
return true;
871
}
872
873
return false;
874
}
875
876
OS_Android::OS_Android(GodotJavaWrapper *p_godot_java, GodotIOJavaWrapper *p_godot_io_java, bool p_use_apk_expansion) {
877
display_size.width = DEFAULT_WINDOW_WIDTH;
878
display_size.height = DEFAULT_WINDOW_HEIGHT;
879
880
use_apk_expansion = p_use_apk_expansion;
881
882
main_loop = nullptr;
883
884
#if defined(GLES3_ENABLED)
885
gl_extensions = nullptr;
886
#endif
887
888
#if defined(VULKAN_ENABLED)
889
native_window = nullptr;
890
#endif
891
892
godot_java = p_godot_java;
893
godot_io_java = p_godot_io_java;
894
895
Vector<Logger *> loggers;
896
loggers.push_back(memnew(AndroidLogger));
897
_set_logger(memnew(CompositeLogger(loggers)));
898
899
AudioDriverManager::add_driver(&audio_driver_android);
900
901
DisplayServerAndroid::register_android_driver();
902
}
903
904
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) {
905
if (p_path == ANDROID_EXEC_PATH) {
906
return create_instance(p_arguments);
907
} else {
908
return OS_Unix::execute(p_path, p_arguments, r_pipe, r_exitcode, read_stderr, p_pipe_mutex, p_open_console);
909
}
910
}
911
912
Error OS_Android::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
913
if (p_path == ANDROID_EXEC_PATH) {
914
return create_instance(p_arguments, r_child_id);
915
} else {
916
return OS_Unix::create_process(p_path, p_arguments, r_child_id, p_open_console);
917
}
918
}
919
920
Error OS_Android::create_instance(const List<String> &p_arguments, ProcessID *r_child_id) {
921
int instance_id = godot_java->create_new_godot_instance(p_arguments);
922
if (instance_id == -1) {
923
return FAILED;
924
}
925
if (r_child_id) {
926
*r_child_id = instance_id;
927
}
928
return OK;
929
}
930
931
Error OS_Android::kill(const ProcessID &p_pid) {
932
if (godot_java->force_quit(nullptr, p_pid)) {
933
return OK;
934
}
935
return OS_Unix::kill(p_pid);
936
}
937
938
String OS_Android::get_system_ca_certificates() {
939
return godot_java->get_ca_certificates();
940
}
941
942
Error OS_Android::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
943
r_project_path = OS::get_user_data_dir();
944
Error err = OS_Unix::setup_remote_filesystem(p_server_host, p_port, p_password, r_project_path);
945
if (err == OK) {
946
remote_fs_dir = r_project_path;
947
FileAccess::make_default<FileAccessFilesystemJAndroid>(FileAccess::ACCESS_RESOURCES);
948
}
949
return err;
950
}
951
952
void OS_Android::load_platform_gdextensions() const {
953
Vector<String> extension_list_config_file = godot_java->get_gdextension_list_config_file();
954
for (String config_file_path : extension_list_config_file) {
955
GDExtensionManager::LoadStatus err = GDExtensionManager::get_singleton()->load_extension(config_file_path);
956
ERR_CONTINUE_MSG(err == GDExtensionManager::LOAD_STATUS_FAILED, "Error loading platform extension: " + config_file_path);
957
}
958
}
959
960
OS_Android::~OS_Android() {
961
}
962
963