Skip to content

Commit

Permalink
fix(apacheGH-1616): Fix monochrome checks (apache#1632)
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Oct 21, 2023
1 parent e74f806 commit c97043d
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 110 deletions.
52 changes: 36 additions & 16 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const fs = require('fs-extra');
const path = require('path');
const nopt = require('nopt');
const glob = require('fast-glob');
const dedent = require('dedent');
const events = require('cordova-common').events;
const AndroidManifest = require('./AndroidManifest');
const xmlHelpers = require('cordova-common').xmlHelpers;
Expand Down Expand Up @@ -747,9 +748,24 @@ function updateIconResourceForAdaptive (preparedIcons, resourceMap, platformReso
foreground = android_icons[density].foreground;
monochrome = android_icons[density].monochrome;

const isAdaptiveIcon = background && foreground;
const isMonochromeIcon = monochrome && isAdaptiveIcon;
if (!isMonochromeIcon || !isAdaptiveIcon) {
const hasAdaptiveIcons = !!background && !!foreground;
let hasMonochromeIcon = !!monochrome;

if (hasMonochromeIcon && !hasAdaptiveIcons) {
// If we have a monochrome icon, but no adaptive icons,
// then warn that in order to use monochrome, the adaptive icons
// must be supplied. We will ignore monochrome and proceed with the
// icon preparation however.
hasMonochromeIcon = false;
monochrome = undefined;
events.emit('warn', dedent`
Monochrome icon found but without adaptive properties.
Monochrome icon requires the adaptive background and foreground assets.
See https://cordova.apache.org/docs/en/latest/config_ref/images.html fore more information.
`);
}

if (!hasAdaptiveIcons) {
// This icon isn't an adaptive icon, so skip it
continue;
}
Expand Down Expand Up @@ -780,7 +796,7 @@ function updateIconResourceForAdaptive (preparedIcons, resourceMap, platformReso
resourceMap[targetPathForeground] = android_icons[density].foreground;
}

if (monochrome) {
if (hasMonochromeIcon) {
if (path.extname(path.basename(monochrome)) === '.xml') {
// Vector Use Case
targetPathMonochrome = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_monochrome.xml', path.basename(android_icons[density].monochrome));
Expand All @@ -794,19 +810,23 @@ function updateIconResourceForAdaptive (preparedIcons, resourceMap, platformReso

// create an XML for DPI and set color
let icLauncherTemplate = '';
if (monochrome) {
icLauncherTemplate = `<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="` + backgroundVal + `" />
<foreground android:drawable="` + foregroundVal + `" />
<monochrome android:drawable="` + monochromeVal + `" />
</adaptive-icon>`;
if (hasMonochromeIcon) {
icLauncherTemplate = dedent`
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="${backgroundVal}" />
<foreground android:drawable="${foregroundVal}" />
<monochrome android:drawable="${monochromeVal}" />
</adaptive-icon>
`;
} else {
icLauncherTemplate = `<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="` + backgroundVal + `" />
<foreground android:drawable="` + foregroundVal + `" />
</adaptive-icon>`;
icLauncherTemplate = dedent`
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="${backgroundVal}" />
<foreground android:drawable="${foregroundVal}" />
</adaptive-icon>
`;
}

const launcherXmlPath = path.join(platformResourcesDir, 'mipmap-' + density + '-v26', 'ic_launcher.xml');
Expand Down

0 comments on commit c97043d

Please sign in to comment.