Skip to content

Commit 6222def

Browse files
style: formatted
1 parent 208a070 commit 6222def

File tree

1 file changed

+29
-65
lines changed

1 file changed

+29
-65
lines changed

lib/android.dart

+29-65
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ List<AndroidIconTemplate> androidIcons = <AndroidIconTemplate>[
2929
AndroidIconTemplate(directoryName: 'mipmap-xxxhdpi', size: 192),
3030
];
3131

32-
void createDefaultIcons(
33-
Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
32+
void createDefaultIcons(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
3433
printStatus('Creating default icons Android');
3534
final String filePath = getAndroidIconPath(flutterLauncherIconsConfig);
3635
final Image? image = decodeImageFile(filePath);
@@ -48,51 +47,42 @@ void createDefaultIcons(
4847
}
4948
overwriteAndroidManifestWithNewLauncherIcon(iconName, androidManifestFile);
5049
} else {
51-
printStatus(
52-
'Overwriting the default Android launcher icon with a new icon');
50+
printStatus('Overwriting the default Android launcher icon with a new icon');
5351
for (AndroidIconTemplate template in androidIcons) {
54-
overwriteExistingIcons(
55-
template, image, constants.androidFileName, flavor);
52+
overwriteExistingIcons(template, image, constants.androidFileName, flavor);
5653
}
57-
overwriteAndroidManifestWithNewLauncherIcon(
58-
constants.androidDefaultIconName, androidManifestFile);
54+
overwriteAndroidManifestWithNewLauncherIcon(constants.androidDefaultIconName, androidManifestFile);
5955
}
6056
}
6157

6258
/// Ensures that the Android icon name is in the correct format
6359
bool isAndroidIconNameCorrectFormat(String iconName) {
6460
// assure the icon only consists of lowercase letters, numbers and underscore
6561
if (!RegExp(r'^[a-z0-9_]+$').hasMatch(iconName)) {
66-
throw const InvalidAndroidIconNameException(
67-
constants.errorIncorrectIconName);
62+
throw const InvalidAndroidIconNameException(constants.errorIncorrectIconName);
6863
}
6964
return true;
7065
}
7166

72-
void createAdaptiveIcons(
73-
Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
67+
void createAdaptiveIcons(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
7468
printStatus('Creating adaptive icons Android');
7569

7670
// Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file
77-
final String backgroundConfig =
78-
flutterLauncherIconsConfig['adaptive_icon_background'];
79-
final String foregroundImagePath =
80-
flutterLauncherIconsConfig['adaptive_icon_foreground'];
71+
final String backgroundConfig = flutterLauncherIconsConfig['adaptive_icon_background'];
72+
final String foregroundImagePath = flutterLauncherIconsConfig['adaptive_icon_foreground'];
8173
final Image? foregroundImage = decodeImageFile(foregroundImagePath);
8274
if (foregroundImage == null) {
8375
return;
8476
}
8577

8678
// Create adaptive icon foreground images
8779
for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
88-
overwriteExistingIcons(androidIcon, foregroundImage,
89-
constants.androidAdaptiveForegroundFileName, flavor);
80+
overwriteExistingIcons(androidIcon, foregroundImage, constants.androidAdaptiveForegroundFileName, flavor);
9081
}
9182

9283
// Create adaptive icon background
9384
if (isAdaptiveIconConfigPngFile(backgroundConfig)) {
94-
createAdaptiveBackgrounds(
95-
flutterLauncherIconsConfig, backgroundConfig, flavor);
85+
createAdaptiveBackgrounds(flutterLauncherIconsConfig, backgroundConfig, flavor);
9686
} else {
9787
createAdaptiveIconMipmapXmlFile(flutterLauncherIconsConfig, flavor);
9888
updateColorsXmlFile(backgroundConfig, flavor);
@@ -113,28 +103,22 @@ void updateColorsXmlFile(String backgroundConfig, String? flavor) {
113103
updateColorsFile(colorsXml, backgroundConfig);
114104
} else {
115105
printStatus('No colors.xml file found in your Android project');
116-
printStatus(
117-
'Creating colors.xml file and adding it to your Android project');
106+
printStatus('Creating colors.xml file and adding it to your Android project');
118107
createNewColorsFile(backgroundConfig, flavor);
119108
}
120109
}
121110

122111
/// Creates the xml file required for the adaptive launcher icon
123112
/// FILE LOCATED HERE: res/mipmap-anydpi/{icon-name-from-yaml-config}.xml
124-
void createAdaptiveIconMipmapXmlFile(
125-
Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
113+
void createAdaptiveIconMipmapXmlFile(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
126114
if (isCustomAndroidFile(flutterLauncherIconsConfig)) {
127-
File(constants.androidAdaptiveXmlFolder(flavor) +
128-
getNewIconName(flutterLauncherIconsConfig) +
129-
'.xml')
115+
File(constants.androidAdaptiveXmlFolder(flavor) + getNewIconName(flutterLauncherIconsConfig) + '.xml')
130116
.create(recursive: true)
131117
.then((File adaptiveIcon) {
132118
adaptiveIcon.writeAsString(xml_template.icLauncherXml);
133119
});
134120
} else {
135-
File(constants.androidAdaptiveXmlFolder(flavor) +
136-
constants.androidDefaultIconName +
137-
'.xml')
121+
File(constants.androidAdaptiveXmlFolder(flavor) + constants.androidDefaultIconName + '.xml')
138122
.create(recursive: true)
139123
.then((File adaptiveIcon) {
140124
adaptiveIcon.writeAsString(xml_template.icLauncherXml);
@@ -143,8 +127,8 @@ void createAdaptiveIconMipmapXmlFile(
143127
}
144128

145129
/// creates adaptive background using png image
146-
void createAdaptiveBackgrounds(Map<String, dynamic> yamlConfig,
147-
String adaptiveIconBackgroundImagePath, String? flavor) {
130+
void createAdaptiveBackgrounds(
131+
Map<String, dynamic> yamlConfig, String adaptiveIconBackgroundImagePath, String? flavor) {
148132
final String filePath = adaptiveIconBackgroundImagePath;
149133
final Image? image = decodeImageFile(filePath);
150134
if (image == null) {
@@ -154,24 +138,19 @@ void createAdaptiveBackgrounds(Map<String, dynamic> yamlConfig,
154138
// creates a png image (ic_adaptive_background.png) for the adaptive icon background in each of the locations
155139
// it is required
156140
for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
157-
saveNewImages(androidIcon, image,
158-
constants.androidAdaptiveBackgroundFileName, flavor);
141+
saveNewImages(androidIcon, image, constants.androidAdaptiveBackgroundFileName, flavor);
159142
}
160143

161144
// Creates the xml file required for the adaptive launcher icon
162145
// FILE LOCATED HERE: res/mipmap-anydpi/{icon-name-from-yaml-config}.xml
163146
if (isCustomAndroidFile(yamlConfig)) {
164-
File(constants.androidAdaptiveXmlFolder(flavor) +
165-
getNewIconName(yamlConfig) +
166-
'.xml')
147+
File(constants.androidAdaptiveXmlFolder(flavor) + getNewIconName(yamlConfig) + '.xml')
167148
.create(recursive: true)
168149
.then((File adaptiveIcon) {
169150
adaptiveIcon.writeAsString(xml_template.icLauncherDrawableBackgroundXml);
170151
});
171152
} else {
172-
File(constants.androidAdaptiveXmlFolder(flavor) +
173-
constants.androidDefaultIconName +
174-
'.xml')
153+
File(constants.androidAdaptiveXmlFolder(flavor) + constants.androidDefaultIconName + '.xml')
175154
.create(recursive: true)
176155
.then((File adaptiveIcon) {
177156
adaptiveIcon.writeAsString(xml_template.icLauncherDrawableBackgroundXml);
@@ -181,9 +160,7 @@ void createAdaptiveBackgrounds(Map<String, dynamic> yamlConfig,
181160

182161
/// Creates a colors.xml file if it was missing from android/app/src/main/res/values/colors.xml
183162
void createNewColorsFile(String backgroundColor, String? flavor) {
184-
File(constants.androidColorsFile(flavor))
185-
.create(recursive: true)
186-
.then((File colorsFile) {
163+
File(constants.androidColorsFile(flavor)).create(recursive: true).then((File colorsFile) {
187164
colorsFile.writeAsString(xml_template.colorsXml).then((File file) {
188165
updateColorsFile(colorsFile, backgroundColor);
189166
});
@@ -208,8 +185,7 @@ void updateColorsFile(File colorsFile, String backgroundColor) {
208185

209186
// Add new line if we didn't find an existing value
210187
if (!foundExisting) {
211-
lines.insert(lines.length - 1,
212-
'\t<color name="ic_launcher_background">$backgroundColor</color>');
188+
lines.insert(lines.length - 1, '\t<color name="ic_launcher_background">$backgroundColor</color>');
213189
}
214190

215191
colorsFile.writeAsStringSync(lines.join('\n'));
@@ -239,10 +215,7 @@ void overwriteExistingIcons(
239215
String? flavor,
240216
) {
241217
final Image newFile = createResizedImage(template.size, image);
242-
File(constants.androidResFolder(flavor) +
243-
template.directoryName +
244-
'/' +
245-
filename)
218+
File(constants.androidResFolder(flavor) + template.directoryName + '/' + filename)
246219
.create(recursive: true)
247220
.then((File file) {
248221
file.writeAsBytesSync(encodePng(newFile));
@@ -252,13 +225,9 @@ void overwriteExistingIcons(
252225
/// Saves new launcher icons to the project, keeping the old launcher icons.
253226
/// Note: Do not change interpolation unless you end up with better results
254227
/// https://github.com/fluttercommunity/flutter_launcher_icons/issues/101#issuecomment-495528733
255-
void saveNewImages(AndroidIconTemplate template, Image image,
256-
String iconFilePath, String? flavor) {
228+
void saveNewImages(AndroidIconTemplate template, Image image, String iconFilePath, String? flavor) {
257229
final Image newFile = createResizedImage(template.size, image);
258-
File(constants.androidResFolder(flavor) +
259-
template.directoryName +
260-
'/' +
261-
iconFilePath)
230+
File(constants.androidResFolder(flavor) + template.directoryName + '/' + iconFilePath)
262231
.create(recursive: true)
263232
.then((File file) {
264233
file.writeAsBytesSync(encodePng(newFile));
@@ -269,19 +238,15 @@ void saveNewImages(AndroidIconTemplate template, Image image,
269238
/// with the new icon name (only if it has changed)
270239
///
271240
/// Note: default iconName = "ic_launcher"
272-
Future<void> overwriteAndroidManifestWithNewLauncherIcon(
273-
String iconName, File androidManifestFile) async {
241+
Future<void> overwriteAndroidManifestWithNewLauncherIcon(String iconName, File androidManifestFile) async {
274242
// we do not use `file.readAsLinesSync()` here because that always gets rid of the last empty newline
275-
final List<String> oldManifestLines =
276-
(await androidManifestFile.readAsString()).split('\n');
277-
final List<String> transformedLines =
278-
transformAndroidManifestWithNewLauncherIcon(oldManifestLines, iconName);
243+
final List<String> oldManifestLines = (await androidManifestFile.readAsString()).split('\n');
244+
final List<String> transformedLines = transformAndroidManifestWithNewLauncherIcon(oldManifestLines, iconName);
279245
await androidManifestFile.writeAsString(transformedLines.join('\n'));
280246
}
281247

282248
/// Updates only the line containing android:icon with the specified iconName
283-
List<String> transformAndroidManifestWithNewLauncherIcon(
284-
List<String> oldManifestLines, String iconName) {
249+
List<String> transformAndroidManifestWithNewLauncherIcon(List<String> oldManifestLines, String iconName) {
285250
return oldManifestLines.map((String line) {
286251
if (line.contains('android:icon')) {
287252
// Using RegExp replace the value of android:icon to point to the new icon
@@ -291,8 +256,7 @@ List<String> transformAndroidManifestWithNewLauncherIcon(
291256
// repeat as often as wanted with no quote at start: [^"]*(\"[^"]*)*
292257
// escaping the slash to place in string: [^"]*(\\"[^"]*)*"
293258
// result: any string which does only include escaped quotes
294-
return line.replaceAll(RegExp(r'android:icon="[^"]*(\\"[^"]*)*"'),
295-
'android:icon="@mipmap/$iconName"');
259+
return line.replaceAll(RegExp(r'android:icon="[^"]*(\\"[^"]*)*"'), 'android:icon="@mipmap/$iconName"');
296260
} else {
297261
return line;
298262
}

0 commit comments

Comments
 (0)