|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Android.App; |
| 5 | +using Java.Interop.Tools.Cecil; |
| 6 | +using Java.Interop.Tools.TypeNameMappings; |
| 7 | +using Mono.Cecil; |
| 8 | + |
| 9 | +namespace Xamarin.Android.Tasks; |
| 10 | + |
| 11 | +class ManifestAttributeCecilAdapter |
| 12 | +{ |
| 13 | + public List<string> Assemblies { get; } |
| 14 | + public TypeDefinitionCache Cache { get; } |
| 15 | + public IAssemblyResolver Resolver { get; } |
| 16 | + public IList<TypeDefinition> Subclasses { get; } |
| 17 | + |
| 18 | + public ManifestAttributeCecilAdapter (List<string> assemblies, TypeDefinitionCache cache, IAssemblyResolver resolver, IList<TypeDefinition> subclasses) |
| 19 | + { |
| 20 | + Assemblies = assemblies; |
| 21 | + Cache = cache; |
| 22 | + Resolver = resolver; |
| 23 | + Subclasses = subclasses; |
| 24 | + } |
| 25 | + |
| 26 | + public List<InstrumentationAttribute> GetAssemblyInstrumentationAttributes () |
| 27 | + => Assemblies.SelectMany (path => InstrumentationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 28 | + |
| 29 | + public List<ApplicationAttribute> GetAssemblyApplicationAttributes () |
| 30 | + => Assemblies.Select (path => ApplicationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).WhereNotNull ().ToList (); |
| 31 | + |
| 32 | + public List<MetaDataAttribute> GetAssemblyMetaDataAttributes () |
| 33 | + => Assemblies.SelectMany (path => MetaDataAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 34 | + |
| 35 | + public List<PropertyAttribute> GetAssemblyPropertyAttributes () |
| 36 | + => Assemblies.SelectMany (path => PropertyAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 37 | + |
| 38 | + public List<UsesLibraryAttribute> GetAssemblyUsesLibraryAttributes () |
| 39 | + => Assemblies.SelectMany (path => UsesLibraryAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 40 | + |
| 41 | + public List<UsesConfigurationAttribute> GetAssemblyUsesConfigurationAttributes () |
| 42 | + => Assemblies.SelectMany (path => UsesConfigurationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 43 | + |
| 44 | + public List<PermissionAttribute> GetAssemblyPermissionAttributes () |
| 45 | + => Assemblies.SelectMany (path => PermissionAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 46 | + |
| 47 | + public List<PermissionGroupAttribute> GetAssemblyPermissionGroupAttributes () |
| 48 | + => Assemblies.SelectMany (path => PermissionGroupAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 49 | + |
| 50 | + public List<PermissionTreeAttribute> GetAssemblyPermissionTreeAttributes () |
| 51 | + => Assemblies.SelectMany (path => PermissionTreeAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 52 | + |
| 53 | + public List<UsesPermissionAttribute> GetAssemblyUsesPermissionAttributes () |
| 54 | + => Assemblies.SelectMany (path => UsesPermissionAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 55 | + |
| 56 | + public List<UsesFeatureAttribute> GetAssemblyUsesFeatureAttributes () |
| 57 | + => Assemblies.SelectMany (path => UsesFeatureAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 58 | + |
| 59 | + public List<SupportsGLTextureAttribute> GetAssemblySupportsGLTextureAttributes () |
| 60 | + => Assemblies.SelectMany (path => SupportsGLTextureAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), Cache)).ToList (); |
| 61 | + |
| 62 | + public List<ManifestAttributeWithMetadata<InstrumentationAttribute>> GetTypeInstrumentationAttributes () |
| 63 | + { |
| 64 | + var results = new List<ManifestAttributeWithMetadata<InstrumentationAttribute>> (); |
| 65 | + |
| 66 | + foreach (TypeDefinition type in Subclasses) { |
| 67 | + if (type.IsSubclassOf ("Android.App.Instrumentation", Cache)) { |
| 68 | + var ia = InstrumentationAttribute.FromCustomAttributeProvider (type, Cache).FirstOrDefault (); |
| 69 | + |
| 70 | + if (ia is not null) { |
| 71 | + var attr = new ManifestAttributeWithMetadata<InstrumentationAttribute> (ia, JavaNativeTypeManager.ToJniName (type, Cache).Replace ('/', '.')); |
| 72 | + attr.AddMetadata (type, Cache, addIntentFilters: true); |
| 73 | + results.Add (attr); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return results; |
| 79 | + } |
| 80 | + |
| 81 | + public List<ManifestAttributeWithMetadata<ApplicationAttribute>> GetTypeApplicationAttributes () |
| 82 | + { |
| 83 | + var results = new List<ManifestAttributeWithMetadata<ApplicationAttribute>> (); |
| 84 | + |
| 85 | + foreach (TypeDefinition type in Subclasses) { |
| 86 | + var aa = ApplicationAttribute.FromCustomAttributeProvider (type, Cache); |
| 87 | + |
| 88 | + if (aa is null) |
| 89 | + continue; |
| 90 | + |
| 91 | + if (!type.IsSubclassOf ("Android.App.Application", Cache)) |
| 92 | + throw new InvalidOperationException (string.Format ("Found [Application] on type {0}. [Application] can only be used on subclasses of Application.", type.FullName)); |
| 93 | + |
| 94 | + var attr = new ManifestAttributeWithMetadata<ApplicationAttribute> (aa, JavaNativeTypeManager.ToJniName (type, Cache).Replace ('/', '.')); |
| 95 | + attr.AddMetadata (type, Cache, addUsesLibraries: true); |
| 96 | + results.Add (attr); |
| 97 | + } |
| 98 | + |
| 99 | + return results; |
| 100 | + } |
| 101 | + |
| 102 | + public IEnumerable<string> GetSubclassNamespaces () |
| 103 | + { |
| 104 | + foreach (var type in Subclasses) { |
| 105 | + if (type.IsAbstract) |
| 106 | + yield return type.Namespace; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public IEnumerable<(string name, string compatName)> GetApplicationSubclasses () |
| 111 | + { |
| 112 | + foreach (var type in Subclasses) { |
| 113 | + if (type.IsAbstract) |
| 114 | + continue; |
| 115 | + |
| 116 | + if (type.IsSubclassOf ("Android.App.Application", Cache)) { |
| 117 | + var name = JavaNativeTypeManager.ToJniName (type, Cache).Replace ('/', '.'); |
| 118 | + var compatName = JavaNativeTypeManager.ToCompatJniName (type, Cache).Replace ('/', '.'); |
| 119 | + |
| 120 | + yield return (name, compatName); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class ManifestAttributeWithMetadata<T> |
| 127 | +{ |
| 128 | + public T Attribute { get; } |
| 129 | + public string JniName { get; } |
| 130 | + public List<MetaDataAttribute> Metadata { get; } = []; |
| 131 | + public List<IntentFilterAttribute> IntentFilters { get; } = []; |
| 132 | + public List<PropertyAttribute> Properties { get; } = []; |
| 133 | + public List<UsesLibraryAttribute> UsesLibraries { get; } = []; |
| 134 | + |
| 135 | + public ManifestAttributeWithMetadata (T attribute, string jniName) |
| 136 | + { |
| 137 | + Attribute = attribute; |
| 138 | + JniName = jniName; |
| 139 | + } |
| 140 | + |
| 141 | + public void AddMetadata (TypeDefinition type, TypeDefinitionCache cache, bool addIntentFilters = false, bool addUsesLibraries = false) |
| 142 | + { |
| 143 | + foreach (var m in MetaDataAttribute.FromCustomAttributeProvider (type, cache)) |
| 144 | + Metadata.Add (m); |
| 145 | + |
| 146 | + foreach (var p in PropertyAttribute.FromCustomAttributeProvider (type, cache)) |
| 147 | + Properties.Add (p); |
| 148 | + |
| 149 | + if (addIntentFilters) { |
| 150 | + foreach (var i in IntentFilterAttribute.FromTypeDefinition (type, cache)) |
| 151 | + IntentFilters.Add (i); |
| 152 | + } |
| 153 | + |
| 154 | + if (addUsesLibraries) { |
| 155 | + foreach (var u in UsesLibraryAttribute.FromCustomAttributeProvider (type, cache)) |
| 156 | + UsesLibraries.Add (u); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments