Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef AAPT_LINKER_LINKERS_H |
| 18 | #define AAPT_LINKER_LINKERS_H |
| 19 | |
| 20 | #include "process/IResourceTableConsumer.h" |
| 21 | |
| 22 | #include <set> |
| 23 | |
| 24 | namespace aapt { |
| 25 | |
| 26 | class ResourceTable; |
| 27 | struct ResourceEntry; |
| 28 | struct ConfigDescription; |
| 29 | |
| 30 | /** |
| 31 | * Determines whether a versioned resource should be created. If a versioned resource already |
| 32 | * exists, it takes precedence. |
| 33 | */ |
| 34 | bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config, |
| 35 | const int sdkVersionToGenerate); |
| 36 | |
| 37 | struct AutoVersioner : public IResourceTableConsumer { |
| 38 | bool consume(IAaptContext* context, ResourceTable* table) override; |
| 39 | }; |
| 40 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | struct XmlAutoVersioner : public IXmlResourceConsumer { |
| 42 | bool consume(IAaptContext* context, XmlResource* resource) override; |
| 43 | }; |
| 44 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 45 | /** |
| 46 | * If any attribute resource values are defined as public, this consumer will move all private |
| 47 | * attribute resource values to a private ^private-attr type, avoiding backwards compatibility |
| 48 | * issues with new apps running on old platforms. |
| 49 | * |
| 50 | * The Android platform ignores resource attributes it doesn't recognize, so an app developer can |
| 51 | * use new attributes in their layout XML files without worrying about versioning. This assumption |
| 52 | * actually breaks on older platforms. OEMs may add private attributes that are used internally. |
| 53 | * AAPT originally assigned all private attributes IDs immediately proceeding the public attributes' |
| 54 | * IDs. |
| 55 | * |
| 56 | * This means that on a newer Android platform, an ID previously assigned to a private attribute |
| 57 | * may end up assigned to a public attribute. |
| 58 | * |
| 59 | * App developers assume using the newer attribute is safe on older platforms because it will |
| 60 | * be ignored. Instead, the platform thinks the new attribute is an older, private attribute and |
| 61 | * will interpret it as such. This leads to unintended styling and exceptions thrown due to |
| 62 | * unexpected types. |
| 63 | * |
| 64 | * By moving the private attributes to a completely different type, this ID conflict will never |
| 65 | * occur. |
| 66 | */ |
| 67 | struct PrivateAttributeMover : public IResourceTableConsumer { |
| 68 | bool consume(IAaptContext* context, ResourceTable* table) override; |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * Resolves all references to resources in the ResourceTable and assigns them IDs. |
| 73 | * The ResourceTable must already have IDs assigned to each resource. |
| 74 | * Once the ResourceTable is processed by this linker, it is ready to be flattened. |
| 75 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | struct ReferenceLinker : public IResourceTableConsumer { |
| 77 | bool consume(IAaptContext* context, ResourceTable* table) override; |
| 78 | }; |
| 79 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Resolves attributes in the XmlResource and compiles string values to resource values. |
| 82 | * Once an XmlResource is processed by this linker, it is ready to be flattened. |
| 83 | */ |
| 84 | class XmlReferenceLinker : public IXmlResourceConsumer { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | private: |
| 86 | std::set<int> mSdkLevelsFound; |
| 87 | |
| 88 | public: |
| 89 | bool consume(IAaptContext* context, XmlResource* resource) override; |
| 90 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Once the XmlResource has been consumed, this returns the various SDK levels in which |
| 93 | * framework attributes used within the XML document were defined. |
| 94 | */ |
| 95 | inline const std::set<int>& getSdkLevels() const { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | return mSdkLevelsFound; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | } // namespace aapt |
| 101 | |
| 102 | #endif /* AAPT_LINKER_LINKERS_H */ |