Skip to content

Commit e8f8cf7

Browse files
committed
Generate mappings used by Xamarin.Android.Build.Tasks.
1 parent bde026f commit e8f8cf7

File tree

43 files changed

+1421
-1089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1421
-1089
lines changed

build-tools/manifest-attribute-codegen/Extensions/StringExtensions.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,6 @@ public static string ToActualName (this string s)
4141
return ret.Length == 0 ? "manifest" : ret;
4242
}
4343

44-
public static bool? GetAsBoolOrNull (this XElement element, string attribute)
45-
{
46-
var value = element.Attribute (attribute)?.Value;
47-
48-
if (value is null)
49-
return null;
50-
51-
if (bool.TryParse (value, out var ret))
52-
return ret;
53-
54-
return null;
55-
}
56-
5744
public static bool GetAttributeBoolOrDefault (this XElement element, string attribute, bool defaultValue)
5845
{
5946
var value = element.Attribute (attribute)?.Value;

build-tools/manifest-attribute-codegen/Models/AttributeDefinition.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public AttributeDefinition (string apiLevel, string name, string format)
1717
Format = format;
1818
}
1919

20+
public string GetAttributeType ()
21+
{
22+
return Format switch {
23+
"boolean" => "bool",
24+
"integer" => "int",
25+
"string" => "string?",
26+
_ => "string?",
27+
};
28+
}
29+
2030
public static AttributeDefinition FromElement (string api, XElement e)
2131
{
2232
var name = e.GetAttributeStringOrEmpty ("name");

build-tools/manifest-attribute-codegen/Models/MetadataSource.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ public MetadataSource (string filename)
1919
var path = element.Attribute ("path")?.Value ?? throw new InvalidDataException ("Missing 'path' attribute.");
2020

2121
Elements.Add (path, new MetadataElement (path) {
22-
Visible = element.GetAsBoolOrNull ("visible"),
22+
Visible = element.GetAttributeBoolOrDefault ("visible", true),
2323
Type = element.Attribute ("type")?.Value,
2424
Name = element.Attribute ("name")?.Value,
2525
Obsolete = element.Attribute ("obsolete")?.Value,
26-
ReadOnly = element.GetAsBoolOrNull ("readonly") ?? false,
26+
ReadOnly = element.GetAttributeBoolOrDefault("readonly", false),
27+
ManualMap = element.GetAttributeBoolOrDefault ("manualMap", false),
2728
});
2829
}
2930

@@ -100,11 +101,12 @@ public void EnsureAllTypesAccountedFor (IEnumerable<ElementDefinition> elements)
100101
class MetadataElement
101102
{
102103
public string Path { get; set; }
103-
public bool? Visible { get; set; }
104+
public bool Visible { get; set; } = true;
104105
public string? Type { get; set; }
105106
public string? Name { get; set; }
106107
public string? Obsolete { get; set; }
107108
public bool ReadOnly { get; set; }
109+
public bool ManualMap { get; set; }
108110
public bool Consumed { get; set; }
109111

110112
public MetadataElement (string path)
@@ -126,7 +128,7 @@ public class MetadataType
126128
public bool HasDefaultConstructor { get; set; }
127129
public bool IsSealed { get; set; }
128130
public bool Consumed { get; set; }
129-
131+
public bool GenerateMapping { get; set; }
130132

131133
public MetadataType (XElement element)
132134
{
@@ -143,6 +145,7 @@ public MetadataType (XElement element)
143145
IsJniNameProvider = element.GetAttributeBoolOrDefault ("jniNameProvider", false);
144146
HasDefaultConstructor = element.GetAttributeBoolOrDefault("defaultConstructor", true);
145147
IsSealed = element.GetAttributeBoolOrDefault ("sealed", true);
146-
ManagedName = element.Attribute ("managedName")?.Value ?? Name.Unhyphenate ().Capitalize () + "Attribute";
148+
ManagedName = element.Attribute ("managedName")?.Value ?? Name.Unhyphenate ().Capitalize () + "Attribute";
149+
GenerateMapping = element.GetAttributeBoolOrDefault ("generateMapping", true);
147150
}
148151
}

build-tools/manifest-attribute-codegen/SourceWriters/AttributeDataClass.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace Xamarin.Android.Tools.ManifestAttributeCodeGenerator;
55
// This is the common data class used by both Mono.Android and Xamarin.Android.Build.Tasks.
66
class AttributeDataClass : ClassWriter
77
{
8+
AttributeMappingField? mapping_field;
9+
AttributeMappingStaticConstructor? static_constructor;
10+
11+
static AttributeMappingManualInitializer manual_mapping_partial = AttributeMappingManualInitializer.Create ();
12+
813
public string Namespace { get; set; }
914

1015
public AttributeDataClass (string ns)
@@ -41,12 +46,12 @@ public static AttributeDataClass Create (ElementDefinition attr, MetadataSource
4146
foreach (var a in attr.Attributes.OrderBy (a => a.Name)) {
4247
var attr_metadata = metadata.GetMetadata ($"{attr.ActualElementName}.{a.Name}");
4348

44-
if (attr_metadata.Visible == false)
49+
if (!attr_metadata.Visible)
4550
continue;
4651

4752
var p = new PropertyWriter {
4853
Name = (attr_metadata.Name ?? a.Name).Capitalize (),
49-
PropertyType = new TypeReferenceWriter (attr_metadata.Type ?? GetAttributeType (a)),
54+
PropertyType = new TypeReferenceWriter (attr_metadata.Type ?? a.GetAttributeType ()),
5055
IsPublic = true,
5156
HasGet = true,
5257
HasSet = true,
@@ -61,17 +66,13 @@ public static AttributeDataClass Create (ElementDefinition attr, MetadataSource
6166
c.Properties.Add (p);
6267
}
6368

64-
return c;
65-
}
69+
// Create mapping field used by Xamarin.Android.Build.Tasks
70+
if (type.GenerateMapping) {
71+
c.mapping_field = AttributeMappingField.Create (type);
72+
c.static_constructor = AttributeMappingStaticConstructor.Create (attr, metadata, type);
73+
}
6674

67-
static string GetAttributeType (AttributeDefinition attr)
68-
{
69-
return attr.Format switch {
70-
"boolean" => "bool",
71-
"integer" => "int",
72-
"string" => "string?",
73-
_ => "string?",
74-
};
75+
return c;
7576
}
7677

7778
public override void Write (CodeWriter writer)
@@ -86,4 +87,19 @@ public override void Write (CodeWriter writer)
8687

8788
base.Write (writer);
8889
}
90+
91+
public override void WriteMembers (CodeWriter writer)
92+
{
93+
base.WriteMembers (writer);
94+
95+
if (mapping_field is not null) {
96+
writer.WriteLineNoIndent ("#if XABT_MANIFEST_EXTENSIONS");
97+
mapping_field?.Write (writer);
98+
writer.WriteLine ();
99+
static_constructor?.Write (writer);
100+
writer.WriteLine ();
101+
manual_mapping_partial?.Write (writer);
102+
writer.WriteLineNoIndent ("#endif");
103+
}
104+
}
89105
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Xamarin.SourceWriter;
2+
3+
namespace Xamarin.Android.Tools.ManifestAttributeCodeGenerator;
4+
5+
class AttributeMappingField : FieldWriter
6+
{
7+
public static AttributeMappingField Create (MetadataType type)
8+
{
9+
var field = new AttributeMappingField {
10+
Name = "mapping",
11+
IsStatic = true,
12+
Type = new TypeReferenceWriter ($"Xamarin.Android.Manifest.ManifestDocumentElement<{type.ManagedName}>"),
13+
Value = $"new (\"{type.Name}\")",
14+
};
15+
16+
return field;
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Xamarin.SourceWriter;
2+
3+
namespace Xamarin.Android.Tools.ManifestAttributeCodeGenerator;
4+
5+
class AttributeMappingManualInitializer : MethodWriter
6+
{
7+
public static AttributeMappingManualInitializer Create ()
8+
{
9+
var method = new AttributeMappingManualInitializer {
10+
Name = "AddManualMapping",
11+
IsStatic = true,
12+
IsPartial = true,
13+
IsDeclaration = true,
14+
ReturnType = TypeReferenceWriter.Void,
15+
};
16+
17+
return method;
18+
}
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Xamarin.SourceWriter;
2+
3+
namespace Xamarin.Android.Tools.ManifestAttributeCodeGenerator;
4+
5+
class AttributeMappingStaticConstructor : ConstructorWriter
6+
{
7+
public static AttributeMappingStaticConstructor Create (ElementDefinition attr, MetadataSource metadata, MetadataType type)
8+
{
9+
var ctor = new AttributeMappingStaticConstructor {
10+
Name = type.ManagedName,
11+
IsStatic = true,
12+
};
13+
14+
// mapping.Add (
15+
// member: "Name",
16+
// attributeName: "name",
17+
// getter: self => self.Name,
18+
// setter: (self, value) => self.Name = (string?) value
19+
// );
20+
foreach (var a in attr.Attributes.OrderBy (a => a.Name)) {
21+
var attr_metadata = metadata.GetMetadata ($"{attr.ActualElementName}.{a.Name}");
22+
23+
if (!attr_metadata.Visible || attr_metadata.ManualMap)
24+
continue;
25+
26+
var name = (attr_metadata.Name ?? a.Name).Capitalize ();
27+
var setter = $"(self, value) => self.{name} = ({attr_metadata.Type ?? a.GetAttributeType ()}) value";
28+
29+
if (attr_metadata.ReadOnly)
30+
setter = "null";
31+
32+
ctor.Body.Add ($"mapping.Add (");
33+
ctor.Body.Add ($" member: \"{name}\",");
34+
ctor.Body.Add ($" attributeName: \"{a.Name}\",");
35+
ctor.Body.Add ($" getter: self => self.{name},");
36+
ctor.Body.Add ($" setter: {setter}");
37+
ctor.Body.Add ($");");
38+
}
39+
40+
ctor.Body.Add (string.Empty);
41+
ctor.Body.Add ($"AddManualMapping ();");
42+
43+
return ctor;
44+
}
45+
}

build-tools/manifest-attribute-codegen/metadata.xml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
<type name="application" namespace="Android.App" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.App\ApplicationAttribute.cs" usage="AttributeTargets.Assembly | AttributeTargets.Class" jniNameProvider="true" />
77
<type name="instrumentation" namespace="Android.App" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.App\InstrumentationAttribute.cs" usage="AttributeTargets.Assembly | AttributeTargets.Class" allowMultiple="true" jniNameProvider="true" />
88
<type name="layout" namespace="Android.App" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.App\LayoutAttribute.cs" usage="AttributeTargets.Class" />
9-
<type name="service" namespace="Android.App" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.App\ServiceAttribute.cs" usage="AttributeTargets.Class" jniNameProvider="true" />
10-
<type name="receiver" managedName="BroadcastReceiverAttribute" namespace="Android.Content" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.Content\BroadcastReceiverAttribute.cs" usage="AttributeTargets.Class" jniNameProvider="true" sealed="false" />
119
<type name="provider" managedName="ContentProviderAttribute" namespace="Android.Content" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.Content\ContentProviderAttribute.cs" usage="AttributeTargets.Class" jniNameProvider="true" defaultConstructor="false" sealed="false" />
10+
<type name="receiver" managedName="BroadcastReceiverAttribute" namespace="Android.Content" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.Content\BroadcastReceiverAttribute.cs" usage="AttributeTargets.Class" jniNameProvider="true" sealed="false" />
11+
<type name="service" namespace="Android.App" outputFile="src\Xamarin.Android.NamingCustomAttributes\Android.App\ServiceAttribute.cs" usage="AttributeTargets.Class" jniNameProvider="true" />
1212

13-
<type name="intent-filter" namespace="Android.App" outputFile="src\Mono.Android\Android.App\IntentFilterAttribute.cs" usage="AttributeTargets.Class" allowMultiple="true" defaultConstructor="false" />
14-
<type name="meta-data" namespace="Android.App" outputFile="src\Mono.Android\Android.App\MetaDataAttribute.cs" usage="AttributeTargets.Assembly | AttributeTargets.Class" allowMultiple="true" defaultConstructor="false" />
1513
<type name="grant-uri-permission" namespace="Android.Content" outputFile="src\Mono.Android\Android.Content\GrantUriPermissionAttribute.cs" usage="AttributeTargets.Class" allowMultiple="true" sealed="false" />
16-
<type name="uses-library" namespace="Android.App" outputFile="src\Mono.Android\Android.App\UsesLibraryAttribute.cs" usage="AttributeTargets.Assembly | AttributeTargets.Class" allowMultiple="true" />
14+
<type name="intent-filter" namespace="Android.App" outputFile="src\Mono.Android\Android.App\IntentFilterAttribute.cs" usage="AttributeTargets.Class" allowMultiple="true" defaultConstructor="false" generateMapping="false" />
15+
<type name="meta-data" namespace="Android.App" outputFile="src\Mono.Android\Android.App\MetaDataAttribute.cs" usage="AttributeTargets.Assembly | AttributeTargets.Class" allowMultiple="true" defaultConstructor="false" />
1716
<type name="permission" namespace="Android.App" outputFile="src\Mono.Android\Android.App\PermissionAttribute.cs" usage="AttributeTargets.Assembly" allowMultiple="true" />
1817
<type name="permission-group" namespace="Android.App" outputFile="src\Mono.Android\Android.App\PermissionGroupAttribute.cs" usage="AttributeTargets.Assembly" allowMultiple="true" />
1918
<type name="permission-tree" namespace="Android.App" outputFile="src\Mono.Android\Android.App\PermissionTreeAttribute.cs" usage="AttributeTargets.Assembly" />
2019
<type name="uses-configuration" namespace="Android.App" outputFile="src\Mono.Android\Android.App\UsesConfigurationAttribute.cs" usage="AttributeTargets.Assembly" allowMultiple="true" />
2120
<type name="uses-feature" namespace="Android.App" outputFile="src\Mono.Android\Android.App\UsesFeatureAttribute.cs" usage="AttributeTargets.Assembly" allowMultiple="true" />
21+
<type name="uses-library" namespace="Android.App" outputFile="src\Mono.Android\Android.App\UsesLibraryAttribute.cs" usage="AttributeTargets.Assembly | AttributeTargets.Class" allowMultiple="true" />
22+
<type name="uses-permission" namespace="Android.App" outputFile="src\Mono.Android\Android.App\UsesPermissionAttribute.cs" usage="AttributeTargets.Assembly" allowMultiple="true" />
2223

2324
<!-- Manifest elements we are ignoring -->
2425
<type name="action" ignore="true" />
@@ -66,7 +67,6 @@
6667
<type name="upgrade-key-set" ignore="true" />
6768
<type name="uses-native-library" ignore="true" />
6869
<type name="uses-package" ignore="true" />
69-
<type name="uses-permission" ignore="true" />
7070
<type name="uses-sdk" ignore="true" />
7171
<type name="uses-sdk-library" ignore="true" />
7272
<type name="uses-split" ignore="true" />
@@ -102,7 +102,7 @@
102102
<element path="activity.minAspectRatio" visible="false" />
103103
<element path="activity.multiprocess" name="MultiProcess" type="bool" />
104104
<element path="activity.noHistory" type="bool" />
105-
<element path="activity.parentActivityName" name="ParentActivity" type="Type?" />
105+
<element path="activity.parentActivityName" name="ParentActivity" type="Type?" manualMap="true" />
106106
<element path="activity.persistableMode" type="Android.Content.PM.ActivityPersistableMode" />
107107
<element path="activity.playHomeTransitionSound" visible="false" />
108108
<element path="activity.preferMinimalPostProcessing" visible="false" />
@@ -140,7 +140,7 @@
140140
<element path="application.attributionsAreUserVisible" visible="false" />
141141
<element path="application.autoRevokePermissions" visible="false" />
142142
<element path="application.cantSaveState" visible="false" />
143-
<element path="application.backupAgent" type="Type?" />
143+
<element path="application.backupAgent" type="Type?" manualMap="true" />
144144
<element path="application.backupInForeground" type="bool" />
145145
<element path="application.classLoader" visible="false" />
146146
<element path="application.crossProfile" visible="false" />
@@ -161,11 +161,12 @@
161161
<element path="application.killAfterRestore" type="bool" />
162162
<element path="application.knownActivityEmbeddingCerts" visible="false" />
163163
<element path="application.localeConfig" visible="false" />
164-
<element path="application.manageSpaceActivity" type="Type?" />
164+
<element path="application.manageSpaceActivity" type="Type?" manualMap="true" />
165165
<element path="application.maxAspectRatio" visible="false" />
166166
<element path="application.memtagMode" visible="false" />
167167
<element path="application.minAspectRatio" visible="false" />
168168
<element path="application.multiArch" visible="false" />
169+
<element path="application.name" manualMap="true" />
169170
<element path="application.nativeHeapZeroInitialized" visible="false" />
170171
<element path="application.neverEncrypt" visible="false" />
171172
<element path="application.persistent" type="bool" />
@@ -239,7 +240,7 @@
239240

240241
<!-- <provider> -->
241242
<element path="provider.attributionTags" visible="false" />
242-
<element path="provider.authorities" type="string[]" readonly="true" />
243+
<element path="provider.authorities" type="string[]" readonly="true" manualMap="true" />
243244
<element path="provider.banner" visible="false" />
244245
<element path="provider.description" visible="false" />
245246
<element path="provider.directBootAware" type="bool" />
@@ -286,11 +287,16 @@
286287
<element path="uses-configuration.reqHardKeyboard" type="bool" />
287288

288289
<!-- <uses-feature> -->
289-
<element path="uses-feature.glEsVersion" name="GLESVersion" />
290+
<element path="uses-feature.glEsVersion" name="GLESVersion" manualMap="true" />
290291
<element path="uses-feature.name" readonly="true" />
291292
<element path="uses-feature.version" readonly="true" />
292293

293294
<!-- <uses-library> -->
294295
<element path="uses-library.required" type="bool" />
295296

297+
<!-- <uses-permission> -->
298+
<element path="uses-permission.minSdkVersion" visible="false" />
299+
<element path="uses-permission.requiredFeature" visible="false" />
300+
<element path="uses-permission.requiredNotFeature" visible="false" />
301+
<element path="uses-permission.usesPermissionFlags" visible="false" />
296302
</metadata>

src/Mono.Android/Android.App/MetaDataAttribute.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,33 @@ public sealed partial class MetaDataAttribute : Attribute {
2222

2323
public string? Value { get; set; }
2424

25+
#if XABT_MANIFEST_EXTENSIONS
26+
static Xamarin.Android.Manifest.ManifestDocumentElement<MetaDataAttribute> mapping = new ("meta-data");
27+
28+
static MetaDataAttribute ()
29+
{
30+
mapping.Add (
31+
member: "Name",
32+
attributeName: "name",
33+
getter: self => self.Name,
34+
setter: null
35+
);
36+
mapping.Add (
37+
member: "Resource",
38+
attributeName: "resource",
39+
getter: self => self.Resource,
40+
setter: (self, value) => self.Resource = (string?) value
41+
);
42+
mapping.Add (
43+
member: "Value",
44+
attributeName: "value",
45+
getter: self => self.Value,
46+
setter: (self, value) => self.Value = (string?) value
47+
);
48+
49+
AddManualMapping ();
50+
}
51+
52+
static partial void AddManualMapping ();
53+
#endif
2554
}

0 commit comments

Comments
 (0)