From bc68872e2d7888dfbf4591f924f81d14e482a586 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Fri, 16 Apr 2021 06:42:44 -0700 Subject: [PATCH] Disable the `requestLegacyExternalStorage` attribute when there are no storage permissions. --- platform/android/export/export.cpp | 12 +++++++++++- platform/android/export/gradle_export_util.h | 9 ++++++--- platform/android/java/app/AndroidManifest.xml | 3 ++- platform/android/java/lib/AndroidManifest.xml | 3 +-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index 8ae12b875e8..a9a46840e6f 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -768,6 +768,10 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { return OK; } + bool _has_storage_permission(const Vector &p_permissions) { + return p_permissions.find("android.permission.READ_EXTERNAL_STORAGE") != -1 || p_permissions.find("android.permission.WRITE_EXTERNAL_STORAGE") != -1; + } + void _get_permissions(const Ref &p_preset, bool p_give_internet, Vector &r_permissions) { const char **aperms = android_perms; while (*aperms) { @@ -819,7 +823,7 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { manifest_text += _get_xr_features_tag(p_preset); manifest_text += _get_instrumentation_tag(p_preset); - manifest_text += _get_application_tag(p_preset); + manifest_text += _get_application_tag(p_preset, _has_storage_permission(perms)); manifest_text += "\n"; String manifest_path = vformat("res://android/build/src/%s/AndroidManifest.xml", (p_debug ? "debug" : "release")); @@ -873,6 +877,7 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { Vector perms; // Write permissions into the perms variable. _get_permissions(p_preset, p_give_internet, perms); + bool has_storage_permission = _has_storage_permission(perms); while (ofs < (uint32_t)p_manifest.size()) { @@ -968,6 +973,11 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { string_table.write[attr_value] = version_name; } + if (tname == "application" && attrname == "requestLegacyExternalStorage") { + + encode_uint32(has_storage_permission ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]); + } + if (tname == "instrumentation" && attrname == "targetPackage") { string_table.write[attr_value] = get_package_name(package_name); } diff --git a/platform/android/export/gradle_export_util.h b/platform/android/export/gradle_export_util.h index 912984be548..02e484d4499 100644 --- a/platform/android/export/gradle_export_util.h +++ b/platform/android/export/gradle_export_util.h @@ -275,13 +275,16 @@ String _get_activity_tag(const Ref &p_preset) { return manifest_activity_text; } -String _get_application_tag(const Ref &p_preset) { +String _get_application_tag(const Ref &p_preset, bool p_has_storage_permission) { bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1; - String manifest_application_text = + String manifest_application_text = vformat( " \n\n" - " \n"; + " \n", + bool_to_string(p_has_storage_permission)); if (uses_xr) { manifest_application_text += " \n"; diff --git a/platform/android/java/app/AndroidManifest.xml b/platform/android/java/app/AndroidManifest.xml index eae2be1cfe2..d5d5bcf99f8 100644 --- a/platform/android/java/app/AndroidManifest.xml +++ b/platform/android/java/app/AndroidManifest.xml @@ -27,7 +27,8 @@ - + + - - +