From abe83a1c8434e1f0dc401efce9aa4dd1e9638ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Louren=C3=A7o?= Date: Mon, 6 Jan 2020 18:37:39 +0000 Subject: Add support for Android adaptive icons. --- platform/android/export/export.cpp | 148 +++++++++++++-------- platform/android/java/app/AndroidManifest.xml | 2 +- platform/android/java/lib/AndroidManifest.xml | 2 +- .../android/java/lib/res/drawable-nodpi/icon.png | Bin 7569 -> 0 bytes .../java/lib/res/mipmap-anydpi-v26/icon.xml | 5 + platform/android/java/lib/res/mipmap-hdpi/icon.png | Bin 0 -> 3762 bytes .../java/lib/res/mipmap-hdpi/icon_background.png | Bin 0 -> 375 bytes .../java/lib/res/mipmap-hdpi/icon_foreground.png | Bin 0 -> 2998 bytes platform/android/java/lib/res/mipmap-mdpi/icon.png | Bin 0 -> 2672 bytes .../java/lib/res/mipmap-mdpi/icon_background.png | Bin 0 -> 240 bytes .../java/lib/res/mipmap-mdpi/icon_foreground.png | Bin 0 -> 1909 bytes .../android/java/lib/res/mipmap-xhdpi/icon.png | Bin 0 -> 5186 bytes .../java/lib/res/mipmap-xhdpi/icon_background.png | Bin 0 -> 517 bytes .../java/lib/res/mipmap-xhdpi/icon_foreground.png | Bin 0 -> 4490 bytes .../android/java/lib/res/mipmap-xxhdpi/icon.png | Bin 0 -> 8154 bytes .../java/lib/res/mipmap-xxhdpi/icon_background.png | Bin 0 -> 905 bytes .../java/lib/res/mipmap-xxhdpi/icon_foreground.png | Bin 0 -> 7415 bytes .../android/java/lib/res/mipmap-xxxhdpi/icon.png | Bin 0 -> 11749 bytes .../lib/res/mipmap-xxxhdpi/icon_background.png | Bin 0 -> 1360 bytes .../lib/res/mipmap-xxxhdpi/icon_foreground.png | Bin 0 -> 11325 bytes platform/android/java/lib/res/mipmap/icon.png | Bin 0 -> 11749 bytes .../java/lib/res/mipmap/icon_background.png | Bin 0 -> 240 bytes .../java/lib/res/mipmap/icon_foreground.png | Bin 0 -> 1909 bytes 23 files changed, 102 insertions(+), 55 deletions(-) delete mode 100644 platform/android/java/lib/res/drawable-nodpi/icon.png create mode 100644 platform/android/java/lib/res/mipmap-anydpi-v26/icon.xml create mode 100644 platform/android/java/lib/res/mipmap-hdpi/icon.png create mode 100644 platform/android/java/lib/res/mipmap-hdpi/icon_background.png create mode 100644 platform/android/java/lib/res/mipmap-hdpi/icon_foreground.png create mode 100644 platform/android/java/lib/res/mipmap-mdpi/icon.png create mode 100644 platform/android/java/lib/res/mipmap-mdpi/icon_background.png create mode 100644 platform/android/java/lib/res/mipmap-mdpi/icon_foreground.png create mode 100644 platform/android/java/lib/res/mipmap-xhdpi/icon.png create mode 100644 platform/android/java/lib/res/mipmap-xhdpi/icon_background.png create mode 100644 platform/android/java/lib/res/mipmap-xhdpi/icon_foreground.png create mode 100644 platform/android/java/lib/res/mipmap-xxhdpi/icon.png create mode 100644 platform/android/java/lib/res/mipmap-xxhdpi/icon_background.png create mode 100644 platform/android/java/lib/res/mipmap-xxhdpi/icon_foreground.png create mode 100644 platform/android/java/lib/res/mipmap-xxxhdpi/icon.png create mode 100644 platform/android/java/lib/res/mipmap-xxxhdpi/icon_background.png create mode 100644 platform/android/java/lib/res/mipmap-xxxhdpi/icon_foreground.png create mode 100644 platform/android/java/lib/res/mipmap/icon.png create mode 100644 platform/android/java/lib/res/mipmap/icon_background.png create mode 100644 platform/android/java/lib/res/mipmap/icon_foreground.png diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index 8f784dd943..eb461860ed 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -30,6 +30,7 @@ #include "export.h" +#include "core/io/image_loader.h" #include "core/io/marshalls.h" #include "core/io/zip_io.h" #include "core/os/dir_access.h" @@ -37,6 +38,7 @@ #include "core/os/os.h" #include "core/project_settings.h" #include "core/version.h" +#include "drivers/png/png_driver_common.h" #include "editor/editor_export.h" #include "editor/editor_log.h" #include "editor/editor_node.h" @@ -196,16 +198,40 @@ static const char *android_perms[] = { }; struct LauncherIcon { - const char *option_id; const char *export_path; + int dimensions; }; -static const LauncherIcon launcher_icons[] = { - { "launcher_icons/xxxhdpi_192x192", "res/drawable-xxxhdpi-v4/icon.png" }, - { "launcher_icons/xxhdpi_144x144", "res/drawable-xxhdpi-v4/icon.png" }, - { "launcher_icons/xhdpi_96x96", "res/drawable-xhdpi-v4/icon.png" }, - { "launcher_icons/hdpi_72x72", "res/drawable-hdpi-v4/icon.png" }, - { "launcher_icons/mdpi_48x48", "res/drawable-mdpi-v4/icon.png" } +static const int icon_densities_count = 6; +static const char *launcher_icon_option = "launcher_icon/xxxhdpi_192x192"; +static const char *launcher_adaptive_icon_foreground_option = "launcher_adaptive_icon_foreground/xxxhdpi_432x432"; +static const char *launcher_adaptive_icon_background_option = "launcher_adaptive_icon_background/xxxhdpi_432x432"; + +static const LauncherIcon launcher_icons[icon_densities_count] = { + { "res/mipmap-xxxhdpi-v4/icon.png", 192 }, + { "res/mipmap-xxhdpi-v4/icon.png", 144 }, + { "res/mipmap-xhdpi-v4/icon.png", 96 }, + { "res/mipmap-hdpi-v4/icon.png", 72 }, + { "res/mipmap-mdpi-v4/icon.png", 48 }, + { "res/mipmap/icon.png", 192 } +}; + +static const LauncherIcon launcher_adaptive_icon_foregrounds[icon_densities_count] = { + { "res/mipmap-xxxhdpi-v4/icon_foreground.png", 432 }, + { "res/mipmap-xxhdpi-v4/icon_foreground.png", 324 }, + { "res/mipmap-xhdpi-v4/icon_foreground.png", 216 }, + { "res/mipmap-hdpi-v4/icon_foreground.png", 162 }, + { "res/mipmap-mdpi-v4/icon_foreground.png", 108 }, + { "res/mipmap/icon_foreground.png", 432 } +}; + +static const LauncherIcon launcher_adaptive_icon_backgrounds[icon_densities_count] = { + { "res/mipmap-xxxhdpi-v4/icon_background.png", 432 }, + { "res/mipmap-xxhdpi-v4/icon_background.png", 324 }, + { "res/mipmap-xhdpi-v4/icon_background.png", 216 }, + { "res/mipmap-hdpi-v4/icon_background.png", 162 }, + { "res/mipmap-mdpi-v4/icon_background.png", 108 }, + { "res/mipmap/icon_background.png", 432 } }; class EditorExportPlatformAndroid : public EditorExportPlatform { @@ -1298,6 +1324,27 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { //printf("end\n"); } + void _process_launcher_icons(const String &p_processing_file_name, const Ref &p_source_image, const LauncherIcon p_icon, Vector &p_data) { + if (p_processing_file_name == p_icon.export_path) { + Ref working_image = p_source_image; + + if (p_source_image->get_width() != p_icon.dimensions || p_source_image->get_height() != p_icon.dimensions) { + working_image = p_source_image->duplicate(); + working_image->resize(p_icon.dimensions, p_icon.dimensions, Image::Interpolation::INTERPOLATE_LANCZOS); + } + + PoolVector png_buffer; + Error err = PNGDriverCommon::image_to_png(working_image, png_buffer); + if (err == OK) { + p_data.resize(png_buffer.size()); + memcpy(p_data.ptrw(), png_buffer.read().ptr(), p_data.size()); + } else { + String err_str = String("Failed to convert resized icon (") + p_processing_file_name + ") to png."; + WARN_PRINT(err_str.utf8().get_data()); + } + } + } + static Vector get_enabled_abis(const Ref &p_preset) { Vector abis = get_abis(); Vector enabled_abis; @@ -1355,11 +1402,9 @@ public: r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_large"), true)); r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_xlarge"), true)); r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/opengl_debug"), false)); - - for (uint64_t i = 0; i < sizeof(launcher_icons) / sizeof(launcher_icons[0]); ++i) { - r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_icons[i].option_id, PROPERTY_HINT_FILE, "*.png"), "")); - } - + r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_icon_option, PROPERTY_HINT_FILE, "*.png"), "")); + r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_adaptive_icon_foreground_option, PROPERTY_HINT_FILE, "*.png"), "")); + r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, launcher_adaptive_icon_background_option, PROPERTY_HINT_FILE, "*.png"), "")); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug", PROPERTY_HINT_GLOBAL_FILE, "*.keystore"), "")); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_user"), "")); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_password"), "")); @@ -1957,7 +2002,7 @@ public: } } else if (l.strip_edges().begins_with(" enabled_abis = get_enabled_abis(p_preset); + String project_icon_path = ProjectSettings::get_singleton()->get("application/config/icon"); + + // Prepare images to be resized for the icons. If some image ends up being uninitialized, the default image from the export template will be used. + Ref launcher_icon_image; + Ref launcher_adaptive_icon_foreground_image; + Ref launcher_adaptive_icon_background_image; + + launcher_icon_image.instance(); + launcher_adaptive_icon_foreground_image.instance(); + launcher_adaptive_icon_background_image.instance(); + + // Regular icon: user selection -> project icon -> default. + String path = static_cast(p_preset->get(launcher_icon_option)).strip_edges(); + if (path.empty() || ImageLoader::load_image(path, launcher_icon_image) != OK) { + ImageLoader::load_image(project_icon_path, launcher_icon_image); + } + + // Adaptive foreground: user selection -> regular icon (user selection -> project icon -> default). + path = static_cast(p_preset->get(launcher_adaptive_icon_foreground_option)).strip_edges(); + if (path.empty() || ImageLoader::load_image(path, launcher_adaptive_icon_foreground_image) != OK) { + launcher_adaptive_icon_foreground_image = launcher_icon_image; + } + + // Adaptive background: user selection -> default. + path = static_cast(p_preset->get(launcher_adaptive_icon_background_option)).strip_edges(); + if (!path.empty()) { + ImageLoader::load_image(path, launcher_adaptive_icon_background_image); + } + while (ret == UNZ_OK) { //get filename @@ -2158,41 +2232,22 @@ public: //write if (file == "AndroidManifest.xml") { - _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG)); } if (file == "resources.arsc") { - _fix_resources(p_preset, data); } - if (file == "res/drawable-nodpi-v4/icon.png") { - bool found = false; - for (uint64_t i = 0; i < sizeof(launcher_icons) / sizeof(launcher_icons[0]); ++i) { - String icon_path = String(p_preset->get(launcher_icons[i].option_id)).strip_edges(); - if (icon_path != "" && icon_path.ends_with(".png")) { - FileAccess *f = FileAccess::open(icon_path, FileAccess::READ); - if (f) { - data.resize(f->get_len()); - f->get_buffer(data.ptrw(), data.size()); - memdelete(f); - found = true; - break; - } - } + for (int i = 0; i < icon_densities_count; ++i) { + if (launcher_icon_image.is_valid() && !launcher_icon_image->empty()) { + _process_launcher_icons(file, launcher_icon_image, launcher_icons[i], data); } - if (!found) { - - String appicon = ProjectSettings::get_singleton()->get("application/config/icon"); - if (appicon != "" && appicon.ends_with(".png")) { - FileAccess *f = FileAccess::open(appicon, FileAccess::READ); - if (f) { - data.resize(f->get_len()); - f->get_buffer(data.ptrw(), data.size()); - memdelete(f); - } - } + if (launcher_adaptive_icon_foreground_image.is_valid() && !launcher_adaptive_icon_foreground_image->empty()) { + _process_launcher_icons(file, launcher_adaptive_icon_foreground_image, launcher_adaptive_icon_foregrounds[i], data); + } + if (launcher_adaptive_icon_background_image.is_valid() && !launcher_adaptive_icon_background_image->empty()) { + _process_launcher_icons(file, launcher_adaptive_icon_background_image, launcher_adaptive_icon_backgrounds[i], data); } } @@ -2291,19 +2346,6 @@ public: } } - if (!err) { - APKExportData ed; - ed.ep = &ep; - ed.apk = unaligned_apk; - for (uint64_t i = 0; i < sizeof(launcher_icons) / sizeof(launcher_icons[0]); ++i) { - String icon_path = String(p_preset->get(launcher_icons[i].option_id)).strip_edges(); - if (icon_path != "" && icon_path.ends_with(".png") && FileAccess::exists(icon_path)) { - Vector data = FileAccess::get_file_as_array(icon_path); - store_in_apk(&ed, launcher_icons[i].export_path, data); - } - } - } - int xr_mode_index = p_preset->get("xr_features/xr_mode"); if (xr_mode_index == 1 /* XRMode.OVR */) { cl.push_back("--xr_mode_ovr"); diff --git a/platform/android/java/app/AndroidManifest.xml b/platform/android/java/app/AndroidManifest.xml index ba01ec313b..7e9ce70d80 100644 --- a/platform/android/java/app/AndroidManifest.xml +++ b/platform/android/java/app/AndroidManifest.xml @@ -27,7 +27,7 @@ - + diff --git a/platform/android/java/lib/AndroidManifest.xml b/platform/android/java/lib/AndroidManifest.xml index 517fc403b2..b133585f99 100644 --- a/platform/android/java/lib/AndroidManifest.xml +++ b/platform/android/java/lib/AndroidManifest.xml @@ -11,7 +11,7 @@ diff --git a/platform/android/java/lib/res/drawable-nodpi/icon.png b/platform/android/java/lib/res/drawable-nodpi/icon.png deleted file mode 100644 index 6ad9b43117..0000000000 Binary files a/platform/android/java/lib/res/drawable-nodpi/icon.png and /dev/null differ diff --git a/platform/android/java/lib/res/mipmap-anydpi-v26/icon.xml b/platform/android/java/lib/res/mipmap-anydpi-v26/icon.xml new file mode 100644 index 0000000000..1ed4037035 --- /dev/null +++ b/platform/android/java/lib/res/mipmap-anydpi-v26/icon.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/platform/android/java/lib/res/mipmap-hdpi/icon.png b/platform/android/java/lib/res/mipmap-hdpi/icon.png new file mode 100644 index 0000000000..cc6e113e89 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-hdpi/icon.png differ diff --git a/platform/android/java/lib/res/mipmap-hdpi/icon_background.png b/platform/android/java/lib/res/mipmap-hdpi/icon_background.png new file mode 100644 index 0000000000..78445c0181 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-hdpi/icon_background.png differ diff --git a/platform/android/java/lib/res/mipmap-hdpi/icon_foreground.png b/platform/android/java/lib/res/mipmap-hdpi/icon_foreground.png new file mode 100644 index 0000000000..75e409ff74 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-hdpi/icon_foreground.png differ diff --git a/platform/android/java/lib/res/mipmap-mdpi/icon.png b/platform/android/java/lib/res/mipmap-mdpi/icon.png new file mode 100644 index 0000000000..e1968fe142 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-mdpi/icon.png differ diff --git a/platform/android/java/lib/res/mipmap-mdpi/icon_background.png b/platform/android/java/lib/res/mipmap-mdpi/icon_background.png new file mode 100644 index 0000000000..5813f751ed Binary files /dev/null and b/platform/android/java/lib/res/mipmap-mdpi/icon_background.png differ diff --git a/platform/android/java/lib/res/mipmap-mdpi/icon_foreground.png b/platform/android/java/lib/res/mipmap-mdpi/icon_foreground.png new file mode 100644 index 0000000000..982b69be1e Binary files /dev/null and b/platform/android/java/lib/res/mipmap-mdpi/icon_foreground.png differ diff --git a/platform/android/java/lib/res/mipmap-xhdpi/icon.png b/platform/android/java/lib/res/mipmap-xhdpi/icon.png new file mode 100644 index 0000000000..9281d8da48 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xhdpi/icon.png differ diff --git a/platform/android/java/lib/res/mipmap-xhdpi/icon_background.png b/platform/android/java/lib/res/mipmap-xhdpi/icon_background.png new file mode 100644 index 0000000000..4269c822a7 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xhdpi/icon_background.png differ diff --git a/platform/android/java/lib/res/mipmap-xhdpi/icon_foreground.png b/platform/android/java/lib/res/mipmap-xhdpi/icon_foreground.png new file mode 100644 index 0000000000..726b267ad6 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xhdpi/icon_foreground.png differ diff --git a/platform/android/java/lib/res/mipmap-xxhdpi/icon.png b/platform/android/java/lib/res/mipmap-xxhdpi/icon.png new file mode 100644 index 0000000000..7a6b67d273 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xxhdpi/icon.png differ diff --git a/platform/android/java/lib/res/mipmap-xxhdpi/icon_background.png b/platform/android/java/lib/res/mipmap-xxhdpi/icon_background.png new file mode 100644 index 0000000000..5e8b518d17 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xxhdpi/icon_background.png differ diff --git a/platform/android/java/lib/res/mipmap-xxhdpi/icon_foreground.png b/platform/android/java/lib/res/mipmap-xxhdpi/icon_foreground.png new file mode 100644 index 0000000000..b0c727f74c Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xxhdpi/icon_foreground.png differ diff --git a/platform/android/java/lib/res/mipmap-xxxhdpi/icon.png b/platform/android/java/lib/res/mipmap-xxxhdpi/icon.png new file mode 100644 index 0000000000..0881245802 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xxxhdpi/icon.png differ diff --git a/platform/android/java/lib/res/mipmap-xxxhdpi/icon_background.png b/platform/android/java/lib/res/mipmap-xxxhdpi/icon_background.png new file mode 100644 index 0000000000..004b6fd508 Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xxxhdpi/icon_background.png differ diff --git a/platform/android/java/lib/res/mipmap-xxxhdpi/icon_foreground.png b/platform/android/java/lib/res/mipmap-xxxhdpi/icon_foreground.png new file mode 100644 index 0000000000..72e6f92b6e Binary files /dev/null and b/platform/android/java/lib/res/mipmap-xxxhdpi/icon_foreground.png differ diff --git a/platform/android/java/lib/res/mipmap/icon.png b/platform/android/java/lib/res/mipmap/icon.png new file mode 100644 index 0000000000..0881245802 Binary files /dev/null and b/platform/android/java/lib/res/mipmap/icon.png differ diff --git a/platform/android/java/lib/res/mipmap/icon_background.png b/platform/android/java/lib/res/mipmap/icon_background.png new file mode 100644 index 0000000000..5813f751ed Binary files /dev/null and b/platform/android/java/lib/res/mipmap/icon_background.png differ diff --git a/platform/android/java/lib/res/mipmap/icon_foreground.png b/platform/android/java/lib/res/mipmap/icon_foreground.png new file mode 100644 index 0000000000..982b69be1e Binary files /dev/null and b/platform/android/java/lib/res/mipmap/icon_foreground.png differ -- cgit v1.2.3