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: android bitmap skip dynamic and handle exceptions #337

Merged
merged 4 commits into from Jun 14, 2019
Merged
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
38 changes: 31 additions & 7 deletions src/Splat/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Android.App;
using Android.Graphics;
Expand All @@ -19,12 +20,14 @@ namespace Splat
public class PlatformBitmapLoader : IBitmapLoader
{
private static readonly Dictionary<string, int> _drawableList;
private static readonly IFullLogger _log;

/// <summary>
/// Initializes static members of the <see cref="PlatformBitmapLoader"/> class.
/// </summary>
static PlatformBitmapLoader()
{
_log = Locator.Current.GetService<ILogManager>().GetLogger(typeof(PlatformBitmapLoader));
_drawableList = GetDrawableList();
}

Expand Down Expand Up @@ -99,33 +102,54 @@ public IBitmap Create(float width, float height)

internal static Dictionary<string, int> GetDrawableList()
{
var log = Splat.LogHost.Default;

// VS2019 onward
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.SelectMany(GetTypesFromAssembly)
.Where(x => x.Name == "Resource" && x.GetNestedType("Drawable") != null)
.Select(x => x.GetNestedType("Drawable"))
.ToArray();

log.Debug(() => "DrawableList. Got " + assemblies.Length + " assemblies.");
_log.Debug(() => "DrawableList. Got " + assemblies.Length + " assemblies.");
foreach (var assembly in assemblies)
{
log.Debug(() => "DrawableList Assembly: " + assembly.Name);
_log.Debug(() => "DrawableList Assembly: " + assembly.Name);
}

var result = assemblies
.SelectMany(x => x.GetFields())
.Where(x => x.FieldType == typeof(int) && x.IsLiteral)
.ToDictionary(k => k.Name, v => (int)v.GetRawConstantValue());

log.Debug(() => "DrawableList. Got " + result.Count + " items.");
_log.Debug(() => "DrawableList. Got " + result.Count + " items.");
foreach (var keyValuePair in result)
{
log.Debug(() => "DrawableList Item: " + keyValuePair.Key);
_log.Debug(() => "DrawableList Item: " + keyValuePair.Key);
}

return result;
}

internal static Type[] GetTypesFromAssembly(Assembly assembly)
{
try
{
return assembly.GetTypes();
dpvreony marked this conversation as resolved.
Show resolved Hide resolved
}
catch (ReflectionTypeLoadException e)
{
// The array returned by the Types property of this exception contains a Type
// object for each type that was loaded and null for each type that could not
// be loaded, while the LoaderExceptions property contains an exception for
// each type that could not be loaded.
_log.Warn(e, "Exception while detecting drawing types.");

foreach (var loaderException in e.LoaderExceptions)
{
_log.Warn(loaderException, "Inner Exception for detecting drawing types.");
}

return e.Types.Where(x => x != null).ToArray();
}
}
}
}