Skip to content

Commit

Permalink
android adaptive icons
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed Oct 3, 2019
1 parent 2dfcce0 commit e910136
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/am_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ bool am_conf_icloud_enabled = true;

const char *am_conf_google_play_services_id = NULL;
const char *am_conf_android_app_version_code = NULL;
const char *am_conf_android_target_sdk_version = NULL;
const char *am_conf_android_adaptive_icon_fg = NULL;
const char *am_conf_android_adaptive_icon_bg = NULL;

int am_conf_default_recursion_limit = 8;
const char *am_conf_default_modelview_matrix_name = "MV";
Expand Down Expand Up @@ -143,6 +146,10 @@ static void free_config() {
free_if_not_null((void**)&am_conf_ios_dev_prov_profile_name);
free_if_not_null((void**)&am_conf_ios_dist_prov_profile_name);
free_if_not_null((void**)&am_conf_google_play_services_id);
free_if_not_null((void**)&am_conf_android_app_version_code);
free_if_not_null((void**)&am_conf_android_target_sdk_version);
free_if_not_null((void**)&am_conf_android_adaptive_icon_fg);
free_if_not_null((void**)&am_conf_android_adaptive_icon_bg);
}

bool am_load_config() {
Expand Down Expand Up @@ -219,6 +226,9 @@ bool am_load_config() {

read_string_setting(eng->L, "google_play_services_id", &am_conf_google_play_services_id, "0");
read_string_setting(eng->L, "android_app_version_code", &am_conf_android_app_version_code, "1");
read_string_setting(eng->L, "android_target_sdk_version", &am_conf_android_target_sdk_version, "28");
read_string_setting(eng->L, "android_adaptive_icon_fg", &am_conf_android_adaptive_icon_fg, NULL);
read_string_setting(eng->L, "android_adaptive_icon_bg", &am_conf_android_adaptive_icon_bg, NULL);

read_bool_setting(eng->L, "d3dangle", &am_conf_d3dangle);
#if !defined(AM_WINDOWS)
Expand Down
3 changes: 3 additions & 0 deletions src/am_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extern bool am_conf_icloud_enabled;

extern const char *am_conf_google_play_services_id;
extern const char *am_conf_android_app_version_code;
extern const char *am_conf_android_target_sdk_version;
extern const char *am_conf_android_adaptive_icon_fg;
extern const char *am_conf_android_adaptive_icon_bg;

// graphic driver options
extern bool am_conf_d3dangle;
Expand Down
74 changes: 64 additions & 10 deletions src/am_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ static bool copy_android_app_build_gradle(export_config *conf, char *dir) {
const char *subs[] = {
"AM_APPID", am_conf_app_id_android,
"AM_APPVERSION_CODE", am_conf_android_app_version_code,
"AM_TARGET_SDK_VER", am_conf_android_target_sdk_version,
"AM_APPVERSION", conf->version,
NULL
};
Expand Down Expand Up @@ -1422,37 +1423,90 @@ static bool create_android_proj_icon_files(const char *dir, export_config *conf)
char *filename = (char*)am_conf_app_icon_android;
if (filename == NULL) return false;
void *data = am_read_file(filename, &len);
if (data == NULL) return false;
int components = 4;
stbi_set_flip_vertically_on_load(0);
img_data =
stbi_load_from_memory((stbi_uc const *)data, len, &width, &height, &components, 4);
free(data);
if (img_data == NULL) return false;

char *mdpi_dir = am_format("%s/drawable-mdpi", dir);
char *hdpi_dir = am_format("%s/drawable-hdpi", dir);
char *xhdpi_dir = am_format("%s/drawable-xhdpi", dir);
char *xxhdpi_dir = am_format("%s/drawable-xxhdpi", dir);
char *xxxhdpi_dir = am_format("%s/drawable-xxxhdpi", dir);
stbi_uc *fg_img_data = NULL;
if (am_conf_android_adaptive_icon_fg != NULL) {
data = am_read_file(am_conf_android_adaptive_icon_fg, &len);
if (data == NULL) return false;
int components = 4;
stbi_set_flip_vertically_on_load(0);
fg_img_data =
stbi_load_from_memory((stbi_uc const *)data, len, &width, &height, &components, 4);
free(data);
if (fg_img_data == NULL) return false; // memory leak, but it doesn't matter
}
stbi_uc *bg_img_data = NULL;
if (am_conf_android_adaptive_icon_bg != NULL) {
data = am_read_file(am_conf_android_adaptive_icon_bg, &len);
if (data == NULL) return false;
int components = 4;
stbi_set_flip_vertically_on_load(0);
bg_img_data =
stbi_load_from_memory((stbi_uc const *)data, len, &width, &height, &components, 4);
free(data);
if (bg_img_data == NULL) return false; // memory leak, but it doesn't matter
}

char *templates_dir = get_template_path(conf);
if (templates_dir == NULL) return false;
char *mdpi_dir = am_format("%s/mipmap-mdpi", dir);
char *hdpi_dir = am_format("%s/mipmap-hdpi", dir);
char *xhdpi_dir = am_format("%s/mipmap-xhdpi", dir);
char *xxhdpi_dir = am_format("%s/mipmap-xxhdpi", dir);
char *xxxhdpi_dir = am_format("%s/mipmap-xxxhdpi", dir);
char *anydpi_dir = am_format("%s/mipmap-anydpi-v26", dir);
char *ic_launcher_xml_src = am_format("%sandroid/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml", templates_dir);
char *ic_launcher_xml_dst = am_format("%s/mipmap-anydpi-v26/ic_launcher.xml", dir);
am_make_dir(mdpi_dir);
am_make_dir(hdpi_dir);
am_make_dir(xhdpi_dir);
am_make_dir(xxhdpi_dir);
am_make_dir(xxxhdpi_dir);
am_make_dir(anydpi_dir);

bool error = (
!resize_image(img_data, width, height, mdpi_dir, "icon.png", 48, 48)
|| !resize_image(img_data, width, height, hdpi_dir, "icon.png", 72, 72)
|| !resize_image(img_data, width, height, xhdpi_dir, "icon.png", 96, 96)
|| !resize_image(img_data, width, height, xxhdpi_dir, "icon.png", 144, 144)
|| !resize_image(img_data, width, height, xxxhdpi_dir, "icon.png", 192, 192)
!resize_image(img_data, width, height, mdpi_dir, "ic_launcher.png", 48, 48)
|| !resize_image(img_data, width, height, hdpi_dir, "ic_launcher.png", 72, 72)
|| !resize_image(img_data, width, height, xhdpi_dir, "ic_launcher.png", 96, 96)
|| !resize_image(img_data, width, height, xxhdpi_dir, "ic_launcher.png", 144, 144)
|| !resize_image(img_data, width, height, xxxhdpi_dir, "ic_launcher.png", 192, 192)
);

if (!error && fg_img_data != NULL && bg_img_data != NULL) {
error = (
!resize_image(fg_img_data, width, height, mdpi_dir, "ic_launcher_foreground.png", 108, 108)
|| !resize_image(fg_img_data, width, height, hdpi_dir, "ic_launcher_foreground.png", 162, 162)
|| !resize_image(fg_img_data, width, height, xhdpi_dir, "ic_launcher_foreground.png", 216, 216)
|| !resize_image(fg_img_data, width, height, xxhdpi_dir, "ic_launcher_foreground.png", 324, 324)
|| !resize_image(fg_img_data, width, height, xxxhdpi_dir, "ic_launcher_foreground.png", 432, 432)
|| !resize_image(bg_img_data, width, height, mdpi_dir, "ic_launcher_background.png", 108, 108)
|| !resize_image(bg_img_data, width, height, hdpi_dir, "ic_launcher_background.png", 162, 162)
|| !resize_image(bg_img_data, width, height, xhdpi_dir, "ic_launcher_background.png", 216, 216)
|| !resize_image(bg_img_data, width, height, xxhdpi_dir, "ic_launcher_background.png", 324, 324)
|| !resize_image(bg_img_data, width, height, xxxhdpi_dir, "ic_launcher_background.png", 432, 432)
|| !copy_text_file(ic_launcher_xml_src, ic_launcher_xml_dst, NULL)
);
}

free(img_data);
if (fg_img_data != NULL) free(fg_img_data);
if (bg_img_data != NULL) free(bg_img_data);
free(templates_dir);
free(mdpi_dir);
free(hdpi_dir);
free(xhdpi_dir);
free(xxhdpi_dir);
free(xxxhdpi_dir);
free(anydpi_dir);
free(ic_launcher_xml_src);
free(ic_launcher_xml_dst);

return !error;
}
Expand Down
6 changes: 3 additions & 3 deletions templates/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion AM_TARGET_SDK_VER
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "AM_APPID"
minSdkVersion 23
targetSdkVersion 23
targetSdkVersion AM_TARGET_SDK_VER
versionCode AM_APPVERSION_CODE
versionName "AM_APPVERSION"
}
Expand Down
3 changes: 1 addition & 2 deletions templates/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
android:versionName="AM_APPVERSION"
>

<uses-sdk android:minSdkVersion="23" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" /> <!-- OpenGL ES 2.0 -->

<application android:label="AM_TITLE"
android:icon="@drawable/icon"
android:icon="@mipmap/ic_launcher"
android:hasCode="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:hardwareAccelerated="true"
Expand Down
4 changes: 3 additions & 1 deletion templates/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:3.5.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down

0 comments on commit e910136

Please sign in to comment.