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