Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 50a6d53

Browse files
committed
Merge branch '4.8.0' into 5.0.0
# Conflicts: # Xamarin.Forms.Platform.Android.UnitTests/Xamarin.Forms.Platform.Android.UnitTests.csproj
2 parents bca6f23 + e5604ee commit 50a6d53

File tree

10 files changed

+138
-48
lines changed

10 files changed

+138
-48
lines changed

Xamarin.Forms.ControlGallery.Android/CustomRenderers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected override void UpdateMenuItemIcon(Context context, IMenuItem menuItem,
133133
if (toolBarItem.IconImageSource is FileImageSource fileImageSource)
134134
{
135135
var name = IOPath.GetFileNameWithoutExtension(fileImageSource.File);
136-
var id = Xamarin.Forms.Platform.Android.ResourceManager.GetDrawableByName(name);
136+
var id = context.GetDrawableId(name);
137137
if (id != 0)
138138
{
139139
if ((int)Build.VERSION.SdkInt >= 21)

Xamarin.Forms.ControlGallery.Android/FormsAppCompatActivity.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ protected override void OnCreate(Bundle bundle)
5151
#endif
5252
Forms.Init(this, bundle);
5353

54+
// null out the assembly on the Resource Manager
55+
// so all of our tests run without using the reflection APIs
56+
// At some point the Resources class types will go away so
57+
// reflection will stop working
58+
ResourceManager.Init(null);
59+
5460
FormsMaps.Init(this, bundle);
5561
FormsMaterial.Init(this, bundle);
5662
AndroidAppLinks.Init(this);

Xamarin.Forms.Platform.Android.UnitTests/ButtonTests.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Xamarin.Forms;
1010
using Xamarin.Forms.CustomAttributes;
1111
using AColor = Android.Graphics.Color;
12+
using AContextThemeWrapper = Android.Support.V7.View.ContextThemeWrapper;
1213

1314
namespace Xamarin.Forms.Platform.Android.UnitTests
1415
{
@@ -92,25 +93,26 @@ public void StyleTextAllCapsSettingIsRespected()
9293
}
9394

9495
// This is the ideal test for Issue11703. It's currently being tabled due to a Resource linking bug that we're working out with Android team
95-
//[Category("Button")]
96-
//[Description("Account for user's setting of styles property textAllCaps")]
97-
//[Issue(IssueTracker.Github, 11703, "[Bug] Android textAllCaps no longer works", issueTestNumber: 1)]
98-
//[TestCase(false)]
99-
//[TestCase(true)]
100-
//public void StyleTextAllCapsSettingIsRespected(bool allCaps)
101-
//{
102-
// ContextThemeWrapper contextThemeWrapper = null;
103-
// if (allCaps)
104-
// contextThemeWrapper = new ContextThemeWrapper(Context, Resource.Style.TextAllCapsStyleTrue);
105-
// else
106-
// contextThemeWrapper = new ContextThemeWrapper(Context, Resource.Style.TextAllCapsStyleFalse);
96+
/*[Category("Button")]
97+
[Description("Account for user's setting of styles property textAllCaps")]
98+
[Issue(IssueTracker.Github, 11703, "[Bug] Android textAllCaps no longer works", issueTestNumber: 1)]
99+
[TestCase(false)]
100+
[TestCase(true)]
101+
public async Task StyleTextAllCapsSettingIsRespected(bool allCaps)
102+
{
103+
AContextThemeWrapper contextThemeWrapper = null;
104+
if (allCaps)
105+
contextThemeWrapper = new AContextThemeWrapper(Context, Context.GetStyle("TextAllCapsStyleTrue"));
106+
else
107+
contextThemeWrapper = new AContextThemeWrapper(Context, Context.GetStyle("TextAllCapsStyleFalse"));
107108
108-
// var button = new Button { Text = "foo" };
109-
// var buttonControl = GetRenderer(button, contextThemeWrapper).View as AppCompatButton;
110-
// var initialTextTransform = buttonControl.TransformationMethod;
109+
var button = new Button { Text = "foo" };
110+
var initialTextTransform = await GetControlProperty(button, x => x.TransformationMethod);
111111
112-
// Assert.AreEqual(allCaps, initialTextTransform is AllCapsTransformationMethod);
113-
//}
112+
// when set through a style the type is an internal version of AllCapsTransformationMethod
113+
string typeName = $"{initialTextTransform}";
114+
Assert.AreEqual(allCaps, typeName.Contains("AllCapsTransformationMethod"));
115+
}*/
114116

115117
}
116118
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Android.Views;
4+
using NUnit.Framework;
5+
6+
namespace Xamarin.Forms.Platform.Android.UnitTests
7+
{
8+
[TestFixture]
9+
public class ResourceManagerTests : PlatformTestFixture
10+
{
11+
[Test, Category("Resource")]
12+
[Description("Retrieve Resources by Name")]
13+
public void RetrieveResourcesByName()
14+
{
15+
ResourceManager.Init(null);
16+
ResourceManager.DrawableClass = null;
17+
ResourceManager.LayoutClass = null;
18+
ResourceManager.ResourceClass = null;
19+
ResourceManager.StyleClass = null;
20+
21+
Assert.Greater(ResourceManager.GetDrawableId(this.Context, "DrawableTEST"), 0);
22+
Assert.Greater(ResourceManager.GetDrawableId(this.Context, "DrawableTEST.png"), 0);
23+
Assert.Greater(ResourceManager.GetLayout(this.Context, "LayoutTest"), 0);
24+
Assert.Greater(ResourceManager.GetStyle(this.Context, "TextAllCapsStyleTrue"), 0);
25+
Assert.Greater(ResourceManager.GetResource(this.Context, "namewith.adot"), 0);
26+
Assert.Greater(ResourceManager.GetResource(this.Context, "namewith_adot"), 0);
27+
28+
}
29+
}
30+
}
1.41 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.design.widget.CoordinatorLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
>
8+
9+
<android.support.design.widget.AppBarLayout
10+
android:id="@+id/namewith.adot"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
14+
>
15+
16+
<android.support.v7.widget.Toolbar
17+
android:layout_width="match_parent"
18+
android:layout_height="?attr/actionBarSize"
19+
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
20+
21+
</android.support.design.widget.AppBarLayout>
22+
23+
</android.support.design.widget.CoordinatorLayout>

Xamarin.Forms.Platform.Android.UnitTests/Xamarin.Forms.Platform.Android.UnitTests.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="ButtonTests.cs" />
7171
<Compile Include="BackgroundTests.cs" />
7272
<Compile Include="TestActivity.cs" />
73+
<Compile Include="ResourceManagerTests.cs" />
7374
<Compile Include="TransformationTests.cs" />
7475
</ItemGroup>
7576
<ItemGroup>
@@ -97,6 +98,12 @@
9798
</ItemGroup>
9899
<ItemGroup>
99100
<AndroidResource Include="Resources\values\styles.xml" />
101+
<AndroidResource Include="Resources\layout\LayoutTest.axml" />
102+
<AndroidResource Include="Resources\drawable\DrawableTest.png" />
103+
</ItemGroup>
104+
<ItemGroup>
105+
<Folder Include="Resources\drawable\" />
106+
<Folder Include="Resources\layout\" />
100107
</ItemGroup>
101108
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
102-
</Project>
109+
</Project>

Xamarin.Forms.Platform.Android/Platform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ void GetNewFlyoutPageToggle()
810810
if (fileImageSource == null)
811811
throw new InvalidOperationException("Icon property must be a FileImageSource on Flyout page");
812812

813-
int icon = ResourceManager.GetDrawableByName(fileImageSource);
813+
int icon = _activity.GetDrawableId(fileImageSource);
814814

815815
FastRenderers.AutomationPropertiesProvider.GetDrawerAccessibilityResources(_activity, CurrentFlyoutPage, out int resourceIdOpen, out int resourceIdClose);
816816
#pragma warning disable 618 // Eventually we will need to determine how to handle the v7 ActionBarDrawerToggle for AppCompat

Xamarin.Forms.Platform.Android/Renderers/AndroidGIFImageParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public override bool SelectDrawable(int index)
194194
InJustDecodeBounds = true
195195
};
196196

197-
int drawableIdentifier = ResourceManager.GetDrawableByName(file);
197+
int drawableIdentifier = context.GetDrawableId(file);
198198

199199
if (drawableIdentifier != 0)
200200
{

Xamarin.Forms.Platform.Android/ResourceManager.cs

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static class ResourceManager
2525
static Assembly _assembly;
2626
static Type FindType(string name, string altName)
2727
{
28-
return _assembly.GetTypes().FirstOrDefault(x => x.Name == name || x.Name == altName);
28+
return _assembly?.GetTypes().FirstOrDefault(x => x.Name == name || x.Name == altName);
2929
}
3030
static Type _drawableClass;
3131
static Type _resourceClass;
@@ -97,7 +97,7 @@ public static Type LayoutClass
9797
if (imageSource is FileImageSource fileImageSource)
9898
{
9999
var file = fileImageSource.File;
100-
var id = IdFromTitle(file, DrawableClass);
100+
var id = IdFromTitle(file, DrawableClass, "drawable", context);
101101

102102
// try the drawables via id
103103
if (id != 0)
@@ -357,29 +357,47 @@ public static Drawable GetDrawable(this Context context, string name)
357357
return AndroidAppCompat.GetDrawable(context, id);
358358
}
359359

360+
public static int GetDrawableId(this Context context, string title)
361+
{
362+
return IdFromTitle(title, DrawableClass, _drawableDefType, context);
363+
}
364+
365+
[Obsolete("GetDrawableByName(string) is obsolete as of version 4.8. "
366+
+ "Please use GetDrawableId(string, context) instead.")]
360367
public static int GetDrawableByName(string name)
361368
{
362-
return IdFromTitle(name, DrawableClass);
369+
return IdFromTitle(name, DrawableClass, _drawableDefType, Forms.ApplicationContext);
363370
}
364371

372+
[Obsolete("GetResourceByName(string) is obsolete as of version 4.8. "
373+
+ "Please use GetResource(string, context) instead.")]
365374
public static int GetResourceByName(string name)
366375
{
367-
return IdFromTitle(name, ResourceClass);
376+
return IdFromTitle(name, ResourceClass, "id", Forms.ApplicationContext);
377+
}
378+
379+
public static int GetResource(this Context context, string title)
380+
{
381+
return IdFromTitle(title, ResourceClass, "id", context);
368382
}
369383

384+
[Obsolete("GetLayoutByName(string) is obsolete as of version 4.8. "
385+
+ "Please use GetLayout(string, context) instead.")]
370386
public static int GetLayoutByName(string name)
371387
{
372-
return IdFromTitle(name, LayoutClass);
388+
return IdFromTitle(name, LayoutClass, "layout", Forms.ApplicationContext);
373389
}
374390

375391
public static int GetLayout(this Context context, string name)
376392
{
377393
return IdFromTitle(name, LayoutClass, "layout", context);
378394
}
379395

396+
[Obsolete("GetStyleByName(string) is obsolete as of version 4.8. "
397+
+ "Please use GetStyle(string, context) instead.")]
380398
public static int GetStyleByName(string name)
381399
{
382-
return IdFromTitle(name, StyleClass);
400+
return IdFromTitle(name, StyleClass, "style", Forms.ApplicationContext);
383401
}
384402

385403
public static int GetStyle(this Context context, string name)
@@ -392,24 +410,14 @@ public static void Init(Assembly mainAssembly)
392410
_assembly = mainAssembly;
393411
}
394412

395-
static int IdFromTitle(string title, Type type)
396-
{
397-
if (title == null)
398-
return 0;
399-
400-
string name = IOPath.GetFileNameWithoutExtension(title);
401-
int id = GetId(type, name);
402-
return id;
403-
}
404-
405413
static int IdFromTitle(string title, Type resourceType, string defType, Resources resource)
406414
{
407415
return IdFromTitle(title, resourceType, defType, resource, AppCompat.Platform.GetPackageName());
408416
}
409417

410418
static int IdFromTitle(string title, Type resourceType, string defType, Context context)
411419
{
412-
return IdFromTitle(title, resourceType, defType, context.Resources, context.PackageName);
420+
return IdFromTitle(title, resourceType, defType, context?.Resources, context?.PackageName);
413421
}
414422

415423
static int IdFromTitle(string title, Type resourceType, string defType, Resources resource, string packageName)
@@ -418,24 +426,38 @@ static int IdFromTitle(string title, Type resourceType, string defType, Resource
418426
if (title == null)
419427
return id;
420428

421-
string name = IOPath.GetFileNameWithoutExtension(title);
429+
string name;
422430

423-
id = GetId(resourceType, name);
431+
if (defType == "style" || (resourceType != null && resourceType == StyleClass))
432+
name = title;
433+
else
434+
name = title.ToLower();
424435

425-
if (id > 0)
436+
if (defType == _drawableDefType || (resourceType != null && resourceType == DrawableClass))
437+
name = IOPath.GetFileNameWithoutExtension(name);
438+
439+
if ((id = SearchByIdentifier(name, defType, resource, packageName)) > 0)
426440
return id;
427441

428-
if (packageName != null)
442+
// When searching by reflection you would use a "_" instead of a "."
443+
// So this accounts for cases where users were searching with an "_"
444+
if ((id = SearchByIdentifier(name.Replace("_", "."), defType, resource, packageName)) > 0)
445+
return id;
446+
447+
int SearchByIdentifier(string n, string d, Resources r, string p)
429448
{
430-
id = resource.GetIdentifier(name, defType, packageName);
449+
int returnValue = 0;
431450

432-
if (id > 0)
433-
return id;
434-
}
451+
if (p != null)
452+
returnValue = r.GetIdentifier(n, d, p);
435453

436-
id = resource.GetIdentifier(name, defType, null);
454+
if (returnValue == 0)
455+
returnValue = r.GetIdentifier(n, d, null);
456+
457+
return returnValue;
458+
}
437459

438-
return id;
460+
return GetId(resourceType, name);
439461
}
440462

441463
static int GetId(Type type, string memberName)

0 commit comments

Comments
 (0)