Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GH-1616): Fix monochrome checks #1632

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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