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 round icons on android and ability to use without monochrome #35

Merged
merged 4 commits into from
Jul 7, 2023
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
54 changes: 37 additions & 17 deletions lib/src/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ void _createAndroidAdaptiveIcon({
CliLogger.info(message);

_createAdaptiveForeground(adaptiveIcons, foreground);
_createAdaptiveBackground(adaptiveIcons, background);
_createAdaptiveBackground(adaptiveIcons, background, monochrome);
if (round != null) {
_createAdaptiveRound(androidIcons, round, isValidHexaCode(background));
_createAdaptiveRound(
androidIcons, round, isValidHexaCode(background), monochrome != null);
} else {
_removeAdaptiveRound(androidIcons);
}
Expand All @@ -125,12 +126,13 @@ void _createAndroidAdaptiveIcon({
/// Create the adaptive background
void _createAdaptiveBackground(
List<AndroidMipMapIconTemplate> adaptiveIcons,
String background,
) {
String background, [
String? monochrome,
]) {
// Check background is hexa color or image
if (isValidHexaCode(background)) {
_handleColorsXmlFile(background);
_createIcLauncherColorXmlFile();
_createIcLauncherColorXmlFile(monochrome != null);
} else {
try {
final backgroundImage = Icon.loadFile(background);
Expand All @@ -153,7 +155,7 @@ void _createAdaptiveBackground(
'Generated adaptive background images',
level: CliLoggerLevel.two,
);
_createIcLauncherMipMapXmlFile();
_createIcLauncherMipMapXmlFile(monochrome != null);
} catch (e) {
CliLogger.error(
'Incorrect `$background` of `adaptive_background_color` or `adaptive_background_image`',
Expand Down Expand Up @@ -196,6 +198,7 @@ void _createAdaptiveRound(
List<AndroidMipMapIconTemplate> adaptiveIcons,
String round,
bool backgroundIsColor,
bool hasMonochrome,
) {
final roundImage = Icon.loadFile(round);
if (roundImage == null) {
Expand All @@ -213,7 +216,7 @@ void _createAdaptiveRound(
ANDROID_ADAPTIVE_ROUND_ICON_FILE_NAME,
);
}
_createIcLauncherRoundMipMapXmlFile(backgroundIsColor);
_createIcLauncherRoundMipMapXmlFile(backgroundIsColor, hasMonochrome);
_createAndroidManifestIconLauncherRound();
CliLogger.success(
'Generated adaptive round images',
Expand Down Expand Up @@ -349,37 +352,54 @@ void _updateColorsFile(File colorsXml, String backgroundColor) {
}

/// Create ic_launcher_color.xml file
void _createIcLauncherColorXmlFile() {
void _createIcLauncherColorXmlFile(bool hasMonochrome) {
final icLauncherXml = File(
'${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_XML_FILE_NAME');
icLauncherXml.createSync(recursive: true);
icLauncherXml.writeAsStringSync(IC_LAUNCHER_BACKGROUND_COLOR_XML);
icLauncherXml.writeAsStringSync(hasMonochrome
? IC_LAUNCHER_BACKGROUND_COLOR_XML
: IC_LAUNCHER_BACKGROUND_COLOR_NO_MONOCHROME_XML);
CliLogger.success(
'Created `$ANDROID_ADAPTIVE_XML_FILE_NAME`',
level: CliLoggerLevel.two,
);
}

/// Create ic luncher xml file
void _createIcLauncherMipMapXmlFile() {
void _createIcLauncherMipMapXmlFile(bool hasMonochrome) {
final icLauncherXml = File(
'${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_XML_FILE_NAME');
icLauncherXml.createSync(recursive: true);
icLauncherXml.writeAsStringSync(IC_LAUNCHER_MIP_MAP_XML);
icLauncherXml.writeAsStringSync(hasMonochrome
? IC_LAUNCHER_MIP_MAP_XML
: IC_LAUNCHER_MIP_MAP_NO_MONOCHROME_XML);
CliLogger.success(
'Created `$ANDROID_ADAPTIVE_XML_FILE_NAME`',
level: CliLoggerLevel.two,
);
}

/// Create ic_launcher_round.xml file
void _createIcLauncherRoundMipMapXmlFile(bool backgroundIsColor) {
void _createIcLauncherRoundMipMapXmlFile(
bool backgroundIsColor, bool hasMonochrome) {
final icLauncherXml = File(
'${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME');
String contents;
if (backgroundIsColor) {
if (hasMonochrome) {
contents = IC_LAUNCHER_ROUND_BACKGROUND_COLOR_XML;
} else {
contents = IC_LAUNCHER_ROUND_BACKGROUND_COLOR_NO_MONOCHROME_XML;
}
} else {
if (hasMonochrome) {
contents = IC_LAUNCHER_ROUND_MIP_MAP_XML;
} else {
contents = IC_LAUNCHER_ROUND_MIP_MAP_NO_MONOCHROME_XML;
}
}
icLauncherXml.createSync(recursive: true);
icLauncherXml.writeAsStringSync(backgroundIsColor
? IC_LAUNCHER_ROUND_BACKGROUND_COLOR_XML
: IC_LAUNCHER_ROUND_MIP_MAP_XML);
icLauncherXml.writeAsStringSync(contents);
CliLogger.success(
'Created `$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME`',
level: CliLoggerLevel.two,
Expand Down Expand Up @@ -408,7 +428,7 @@ void _createAndroidManifestIconLauncherRound() {
if (index != -1) {
final lineUpdated = manifestLines.elementAt(index).replaceAll(
RegExp(r'android:roundIcon="[^"]*(\\"[^"]*)*"'),
'android:roundIcon="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"',
'android:roundIcon="@mipmap/$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME_WITHOUT_EXTENSION"',
);
manifestLines.replaceRange(index, index + 1, [lineUpdated]);
androidManifestFile.writeAsStringSync(manifestLines.join('\n'));
Expand All @@ -424,7 +444,7 @@ void _createAndroidManifestIconLauncherRound() {
.elementAt(index)
.replaceAll(RegExp(r'android:icon="[^"]*(\\"[^"]*)*"'), '''
android:icon="@mipmap/$ANDROID_ICON_NAME"
\t\tandroid:roundIcon="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"''');
\t\tandroid:roundIcon="@mipmap/$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME_WITHOUT_EXTENSION"''');

manifestLines.replaceRange(index, index + 1, [lineUpdated]);
androidManifestFile.writeAsStringSync(manifestLines.join('\n'));
Expand Down
46 changes: 43 additions & 3 deletions lib/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ const String ANDROID_ADAPTIVE_XML_DIR = 'mipmap-anydpi-v26';
/// Android adaptive icon file
const String ANDROID_ADAPTIVE_XML_FILE_NAME = '$ANDROID_ICON_NAME.xml';

/// Android adapttive round xml file without extension
const String ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME_WITHOUT_EXTENSION =
'ic_launcher_rounded';

/// Android adaptive round icon file
const String ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME =
'$ANDROID_ADAPTIVE_ROUND_ICON_NAME.xml';
'$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME_WITHOUT_EXTENSION.xml';

/// Android launcher background color xml
const String IC_LAUNCHER_BACKGROUND_COLOR_XML = '''
Expand All @@ -92,6 +96,15 @@ const String IC_LAUNCHER_BACKGROUND_COLOR_XML = '''
</adaptive-icon>
''';

/// Android launcher background color with no monochrome xml
const String IC_LAUNCHER_BACKGROUND_COLOR_NO_MONOCHROME_XML = '''
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/$ANDROID_ADAPTIVE_BACKGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_FOREGROUND_ICON_NAME"/>
</adaptive-icon>
''';

/// Android launcher background image xml
const String IC_LAUNCHER_MIP_MAP_XML = '''
<?xml version="1.0" encoding="utf-8"?>
Expand All @@ -102,26 +115,53 @@ const String IC_LAUNCHER_MIP_MAP_XML = '''
</adaptive-icon>
''';

/// Android launcher background image with no monochrome xml
const String IC_LAUNCHER_MIP_MAP_NO_MONOCHROME_XML = '''
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/$ANDROID_ADAPTIVE_BACKGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_FOREGROUND_ICON_NAME"/>
</adaptive-icon>
''';

/// Android launcher background round color xml
const String IC_LAUNCHER_ROUND_BACKGROUND_COLOR_XML = '''
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/$ANDROID_ADAPTIVE_BACKGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_FOREGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"/>
<monochrome android:drawable="@mipmap/$ANDROID_ADAPTIVE_MONOCHROME_ICON_NAME"/>
</adaptive-icon>
''';

/// Android launcher background round color with no monochrome xml
const String IC_LAUNCHER_ROUND_BACKGROUND_COLOR_NO_MONOCHROME_XML = '''
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/$ANDROID_ADAPTIVE_BACKGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"/>
</adaptive-icon>
''';

/// Android launcher background round image xml
const String IC_LAUNCHER_ROUND_MIP_MAP_XML = '''
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/$ANDROID_ADAPTIVE_BACKGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_FOREGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"/>
<monochrome android:drawable="@mipmap/$ANDROID_ADAPTIVE_MONOCHROME_ICON_NAME"/>
</adaptive-icon>
''';

/// Android launcher background round image with no monochrome xml
const String IC_LAUNCHER_ROUND_MIP_MAP_NO_MONOCHROME_XML = '''
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/$ANDROID_ADAPTIVE_BACKGROUND_ICON_NAME"/>
<foreground android:drawable="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"/>
</adaptive-icon>
''';

/// IOS config file location
const String IOS_CONFIG_FILE = 'ios/Runner.xcodeproj/project.pbxproj';

Expand Down