-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Multiple Fixes for Resizetizer (esp. Android App Icons) #8020
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 81 additions & 90 deletions
171
src/SingleProject/Resizetizer/src/AndroidAdaptiveIconGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,150 +1,141 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace Microsoft.Maui.Resizetizer | ||
{ | ||
internal class AndroidAdaptiveIconGenerator | ||
{ | ||
public AndroidAdaptiveIconGenerator(ResizeImageInfo info, string appIconName, string intermediateOutputPath, ILogger logger, bool useVectors) | ||
public AndroidAdaptiveIconGenerator(ResizeImageInfo info, string appIconName, string intermediateOutputPath, ILogger logger) | ||
{ | ||
Info = info; | ||
Logger = logger; | ||
UseVectors = useVectors; | ||
IntermediateOutputPath = intermediateOutputPath; | ||
AppIconName = appIconName; | ||
} | ||
|
||
public ResizeImageInfo Info { get; } | ||
|
||
public string IntermediateOutputPath { get; } | ||
|
||
public ILogger Logger { get; private set; } | ||
public bool UseVectors { get; } | ||
|
||
public string AppIconName { get; } | ||
|
||
const string AdaptiveIconDrawableXml = | ||
@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<adaptive-icon xmlns:android=""http://schemas.android.com/apk/res/android""> | ||
<background android:drawable=""@{backgroundType}/{name}_background""/> | ||
<foreground android:drawable=""@{foregroundType}/{name}_foreground""/> | ||
<background android:drawable=""@mipmap/{name}_background""/> | ||
<foreground android:drawable=""@mipmap/{name}_foreground""/> | ||
</adaptive-icon>"; | ||
|
||
const string EmptyVectorDrawable = | ||
@"<vector xmlns:android=""http://schemas.android.com/apk/res/android"" xmlns:aapt=""http://schemas.android.com/aapt"" | ||
android:viewportWidth=""1024"" | ||
android:viewportHeight=""1024"" | ||
android:width=""1024dp"" | ||
android:height=""1024dp"" /> | ||
"; | ||
|
||
public IEnumerable<ResizedImageInfo> Generate() | ||
{ | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
|
||
var results = new List<ResizedImageInfo>(); | ||
|
||
var fullIntermediateOutputPath = new DirectoryInfo(IntermediateOutputPath); | ||
|
||
ProcessBackground(results, fullIntermediateOutputPath); | ||
ProcessForeground(results, fullIntermediateOutputPath); | ||
ProcessAdaptiveIcon(results, fullIntermediateOutputPath); | ||
|
||
sw.Stop(); | ||
Logger?.Log($"Generating app icon took {sw.ElapsedMilliseconds}ms"); | ||
|
||
return results; | ||
} | ||
|
||
void ProcessBackground(List<ResizedImageInfo> results, DirectoryInfo fullIntermediateOutputPath) | ||
{ | ||
var backgroundFile = Info.Filename; | ||
var backgroundIsVector = UseVectors && Info.IsVector; | ||
var backgroundExt = backgroundIsVector ? ".xml" : ".png"; | ||
var backgroundDestFilename = AppIconName + "_background" + backgroundExt; | ||
var backgroundExists = File.Exists(backgroundFile); | ||
var backgroundDestFilename = AppIconName + "_background.png"; | ||
|
||
var foregroundFile = Info.ForegroundFilename; | ||
var foregroundExists = File.Exists(foregroundFile); | ||
var foregroundIsVector = !foregroundExists || (UseVectors && Info.ForegroundIsVector); | ||
var foregroundExt = foregroundIsVector ? ".xml" : ".png"; | ||
var foregroundDestFilename = AppIconName + "_foreground" + foregroundExt; | ||
if (backgroundExists) | ||
Logger.Log("Converting Background SVG to PNG: " + backgroundFile); | ||
else | ||
Logger.Log("Background was not found (will manufacture): " + backgroundFile); | ||
|
||
if (backgroundIsVector) | ||
foreach (var dpi in DpiPath.Android.AppIconParts) | ||
{ | ||
Logger.Log("Converting Background SVG to Android Drawable Vector: " + backgroundFile); | ||
|
||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, "drawable-v24"); | ||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, dpi.Path); | ||
var destination = Path.Combine(dir, backgroundDestFilename); | ||
Directory.CreateDirectory(dir); | ||
|
||
Svg2VectorDrawable.Svg2Vector.Convert(backgroundFile, destination); | ||
|
||
results.Add(new ResizedImageInfo { Dpi = new DpiPath("drawable-v24", 1, "_background"), Filename = destination }); | ||
} | ||
else | ||
{ | ||
Logger.Log("Converting Background SVG to PNG: " + backgroundFile); | ||
Logger.Log($"App Icon Background Part: " + destination); | ||
|
||
foreach (var dpi in DpiPath.Android.AppIconParts) | ||
if (backgroundExists) | ||
{ | ||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, dpi.Path); | ||
var destination = Path.Combine(dir, backgroundDestFilename); | ||
Directory.CreateDirectory(dir); | ||
|
||
Logger.Log($"App Icon Background Part: " + destination); | ||
|
||
var tools = SkiaSharpTools.Create(Info.IsVector, Info.Filename, dpi.Size, null, Logger); | ||
// resize the background | ||
var tools = SkiaSharpTools.Create(Info.IsVector, Info.Filename, dpi.Size, Info.Color, null, Logger); | ||
tools.Resize(dpi, destination, dpiSizeIsAbsolute: true); | ||
} | ||
else | ||
{ | ||
// manufacture | ||
var tools = SkiaSharpTools.CreateImaginary(Info.Color, Logger); | ||
tools.Resize(dpi, destination); | ||
|
||
results.Add(new ResizedImageInfo { Dpi = dpi, Filename = destination }); | ||
} | ||
} | ||
|
||
Logger.Log("Looking for Foreground File: " + foregroundFile); | ||
results.Add(new ResizedImageInfo { Dpi = dpi, Filename = destination }); | ||
} | ||
} | ||
|
||
var foregroundDestinationDir = Path.Combine(fullIntermediateOutputPath.FullName, "drawable"); | ||
var foregroundDestination = Path.Combine(foregroundDestinationDir, foregroundDestFilename); | ||
Directory.CreateDirectory(foregroundDestinationDir); | ||
void ProcessForeground(List<ResizedImageInfo> results, DirectoryInfo fullIntermediateOutputPath) | ||
{ | ||
var foregroundFile = Info.ForegroundFilename; | ||
var foregroundExists = File.Exists(foregroundFile); | ||
var foregroundDestFilename = AppIconName + "_foreground.png"; | ||
|
||
if (foregroundExists) | ||
Logger.Log("Converting Foreground SVG to PNG: " + foregroundFile); | ||
else | ||
Logger.Log("Foreground was not found (will manufacture): " + foregroundFile); | ||
|
||
foreach (var dpi in DpiPath.Android.AppIconParts) | ||
{ | ||
if (foregroundIsVector) | ||
{ | ||
Logger.Log("Converting Foreground SVG to Android Drawable Vector: " + foregroundFile); | ||
Svg2VectorDrawable.Svg2Vector.Convert(foregroundFile, foregroundDestination); | ||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, dpi.Path); | ||
var destination = Path.Combine(dir, foregroundDestFilename); | ||
Directory.CreateDirectory(dir); | ||
|
||
results.Add(new ResizedImageInfo { Dpi = new DpiPath("drawable", 1, "_foreground"), Filename = foregroundDestination }); | ||
Logger.Log($"App Icon Foreground Part: " + destination); | ||
|
||
if (foregroundExists) | ||
{ | ||
// resize the forground | ||
var tools = SkiaSharpTools.Create(Info.ForegroundIsVector, Info.ForegroundFilename, dpi.Size, null, Info.TintColor, Logger); | ||
tools.Resize(dpi, destination, Info.ForegroundScale, dpiSizeIsAbsolute: true); | ||
} | ||
else | ||
{ | ||
Logger.Log("Converting Foreground SVG to PNG: " + foregroundFile); | ||
|
||
foreach (var dpi in DpiPath.Android.AppIconParts) | ||
{ | ||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, dpi.Path); | ||
var destination = Path.Combine(dir, foregroundDestFilename); | ||
Directory.CreateDirectory(dir); | ||
|
||
Logger.Log($"App Icon Foreground Part: " + destination); | ||
|
||
var tools = SkiaSharpTools.Create(Info.ForegroundIsVector, Info.ForegroundFilename, dpi.Size, null, Logger); | ||
tools.Resize(dpi, destination); | ||
|
||
results.Add(new ResizedImageInfo { Dpi = dpi, Filename = destination }); | ||
} | ||
// manufacture | ||
var tools = SkiaSharpTools.CreateImaginary(null, Logger); | ||
tools.Resize(dpi, destination); | ||
} | ||
} | ||
else | ||
{ | ||
Logger.Log("Foreground was not found: " + foregroundFile); | ||
|
||
File.WriteAllText(foregroundDestination, EmptyVectorDrawable); | ||
results.Add(new ResizedImageInfo { Dpi = dpi, Filename = destination }); | ||
} | ||
} | ||
|
||
// process adaptive icon xml | ||
{ | ||
var adaptiveIconXmlStr = AdaptiveIconDrawableXml | ||
.Replace("{name}", AppIconName) | ||
.Replace("{backgroundType}", backgroundIsVector ? "drawable" : "mipmap") | ||
.Replace("{foregroundType}", foregroundIsVector ? "drawable" : "mipmap"); | ||
|
||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, "mipmap-anydpi-v26"); | ||
var adaptiveIconDestination = Path.Combine(dir, AppIconName + ".xml"); | ||
var adaptiveIconRoundDestination = Path.Combine(dir, AppIconName + "_round.xml"); | ||
Directory.CreateDirectory(dir); | ||
void ProcessAdaptiveIcon(List<ResizedImageInfo> results, DirectoryInfo fullIntermediateOutputPath) | ||
{ | ||
var adaptiveIconXmlStr = AdaptiveIconDrawableXml | ||
.Replace("{name}", AppIconName); | ||
|
||
// Write out the adaptive icon xml drawables | ||
File.WriteAllText(adaptiveIconDestination, adaptiveIconXmlStr); | ||
File.WriteAllText(adaptiveIconRoundDestination, adaptiveIconXmlStr); | ||
var dir = Path.Combine(fullIntermediateOutputPath.FullName, "mipmap-anydpi-v26"); | ||
var adaptiveIconDestination = Path.Combine(dir, AppIconName + ".xml"); | ||
var adaptiveIconRoundDestination = Path.Combine(dir, AppIconName + "_round.xml"); | ||
Directory.CreateDirectory(dir); | ||
|
||
results.Add(new ResizedImageInfo { Dpi = new DpiPath("mipmap-anydpi-v26", 1), Filename = adaptiveIconDestination }); | ||
results.Add(new ResizedImageInfo { Dpi = new DpiPath("mipmap-anydpi-v26", 1, "_round"), Filename = adaptiveIconRoundDestination }); | ||
} | ||
// Write out the adaptive icon xml drawables | ||
File.WriteAllText(adaptiveIconDestination, adaptiveIconXmlStr); | ||
File.WriteAllText(adaptiveIconRoundDestination, adaptiveIconXmlStr); | ||
|
||
return results; | ||
results.Add(new ResizedImageInfo { Dpi = new DpiPath("mipmap-anydpi-v26", 1), Filename = adaptiveIconDestination }); | ||
results.Add(new ResizedImageInfo { Dpi = new DpiPath("mipmap-anydpi-v26", 1, "_round"), Filename = adaptiveIconRoundDestination }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this svg2vector as the library is not currently used nor is it super useful if it is.