From 8714402864258388912a35520cb4a041d5ee349d Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 13 Jun 2019 13:57:30 -0600 Subject: [PATCH] [Android] Retrieve PackageName from the correct Platform instance (#6420) * Fix getting packagename and pass context into RM - fixes #6327 * spacing --- .../AppCompat/Platform.cs | 4 +++- Xamarin.Forms.Platform.Android/Platform.cs | 2 ++ .../Renderers/FileImageSourceHandler.cs | 2 +- Xamarin.Forms.Platform.Android/ResourceManager.cs | 22 +++++++++++++++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Xamarin.Forms.Platform.Android/AppCompat/Platform.cs b/Xamarin.Forms.Platform.Android/AppCompat/Platform.cs index 2436d9f..34b09c6 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/Platform.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/Platform.cs @@ -23,11 +23,13 @@ namespace Xamarin.Forms.Platform.Android.AppCompat bool _navAnimationInProgress; NavigationModel _navModel = new NavigationModel(); Page _pendingRootChange = null; + internal static string PackageName { get; private set; } + internal static string GetPackageName() => PackageName ?? Android.Platform.PackageName; public Platform(Context context) { _context = context; - + PackageName = context?.PackageName; _renderer = new PlatformRenderer(context, this); FormsAppCompatActivity.BackPressed += HandleBackPressed; diff --git a/Xamarin.Forms.Platform.Android/Platform.cs b/Xamarin.Forms.Platform.Android/Platform.cs index ad10a23..2923a56 100644 --- a/Xamarin.Forms.Platform.Android/Platform.cs +++ b/Xamarin.Forms.Platform.Android/Platform.cs @@ -28,6 +28,8 @@ namespace Xamarin.Forms.Platform.Android { internal static string PackageName { get; private set; } + internal static string GetPackageName() => PackageName ?? AppCompat.Platform.PackageName; + internal const string CloseContextActionsSignalName = "Xamarin.CloseContextActions"; internal static readonly BindableProperty RendererProperty = BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer), typeof(Platform), default(IVisualElementRenderer), diff --git a/Xamarin.Forms.Platform.Android/Renderers/FileImageSourceHandler.cs b/Xamarin.Forms.Platform.Android/Renderers/FileImageSourceHandler.cs index 1c8025e..38b795d 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/FileImageSourceHandler.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/FileImageSourceHandler.cs @@ -24,7 +24,7 @@ namespace Xamarin.Forms.Platform.Android if (File.Exists (file)) bitmap = !DecodeSynchronously ? (await BitmapFactory.DecodeFileAsync (file).ConfigureAwait (false)) : BitmapFactory.DecodeFile (file); else - bitmap = !DecodeSynchronously ? (await context.Resources.GetBitmapAsync (file).ConfigureAwait (false)) : context.Resources.GetBitmap (file); + bitmap = !DecodeSynchronously ? (await context.Resources.GetBitmapAsync (file, context).ConfigureAwait (false)) : context.Resources.GetBitmap (file, context); if (bitmap == null) { diff --git a/Xamarin.Forms.Platform.Android/ResourceManager.cs b/Xamarin.Forms.Platform.Android/ResourceManager.cs index a4de2a0..2eae3db 100644 --- a/Xamarin.Forms.Platform.Android/ResourceManager.cs +++ b/Xamarin.Forms.Platform.Android/ResourceManager.cs @@ -254,11 +254,21 @@ namespace Xamarin.Forms.Platform.Android return BitmapFactory.DecodeResource(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource)); } + public static Bitmap GetBitmap(this Resources resource, string name, Context context) + { + return BitmapFactory.DecodeResource(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource, context.PackageName)); + } + public static Task GetBitmapAsync(this Resources resource, string name) { return BitmapFactory.DecodeResourceAsync(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource)); } + public static Task GetBitmapAsync(this Resources resource, string name, Context context) + { + return BitmapFactory.DecodeResourceAsync(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource, context.PackageName)); + } + [Obsolete("GetDrawable(this Resources, string) is obsolete as of version 2.5. " + "Please use GetDrawable(this Context, string) instead.")] [EditorBrowsable(EditorBrowsableState.Never)] @@ -310,11 +320,21 @@ namespace Xamarin.Forms.Platform.Android return IdFromTitle(name, LayoutClass); } + public static int GetLayout(this Context context, string name) + { + return IdFromTitle(name, LayoutClass, "layout", context); + } + public static int GetStyleByName(string name) { return IdFromTitle(name, StyleClass); } + public static int GetStyle(this Context context, string name) + { + return IdFromTitle(name, StyleClass, "style", context); + } + public static void Init(Assembly masterAssembly) { DrawableClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Drawable" || x.Name == "Resource_Drawable"); @@ -335,7 +355,7 @@ namespace Xamarin.Forms.Platform.Android static int IdFromTitle(string title, Type resourceType, string defType, Resources resource) { - return IdFromTitle(title, resourceType, defType, resource, Platform.PackageName); + return IdFromTitle(title, resourceType, defType, resource, AppCompat.Platform.GetPackageName()); } static int IdFromTitle(string title, Type resourceType, string defType, Context context) -- 2.7.4