Home | History | Annotate | Download | only in link
      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 "Resource.h"
     21 #include "process/IResourceTableConsumer.h"
     22 #include "xml/XmlDom.h"
     23 
     24 #include <set>
     25 
     26 namespace aapt {
     27 
     28 class ResourceTable;
     29 class ResourceEntry;
     30 struct ConfigDescription;
     31 
     32 /**
     33  * Defines the location in which a value exists. This determines visibility of other
     34  * package's private symbols.
     35  */
     36 struct CallSite {
     37     ResourceNameRef resource;
     38 };
     39 
     40 /**
     41  * Determines whether a versioned resource should be created. If a versioned resource already
     42  * exists, it takes precedence.
     43  */
     44 bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
     45                                      const int sdkVersionToGenerate);
     46 
     47 struct AutoVersioner : public IResourceTableConsumer {
     48     bool consume(IAaptContext* context, ResourceTable* table) override;
     49 };
     50 
     51 struct XmlAutoVersioner : public IXmlResourceConsumer {
     52     bool consume(IAaptContext* context, xml::XmlResource* resource) override;
     53 };
     54 
     55 /**
     56  * If any attribute resource values are defined as public, this consumer will move all private
     57  * attribute resource values to a private ^private-attr type, avoiding backwards compatibility
     58  * issues with new apps running on old platforms.
     59  *
     60  * The Android platform ignores resource attributes it doesn't recognize, so an app developer can
     61  * use new attributes in their layout XML files without worrying about versioning. This assumption
     62  * actually breaks on older platforms. OEMs may add private attributes that are used internally.
     63  * AAPT originally assigned all private attributes IDs immediately proceeding the public attributes'
     64  * IDs.
     65  *
     66  * This means that on a newer Android platform, an ID previously assigned to a private attribute
     67  * may end up assigned to a public attribute.
     68  *
     69  * App developers assume using the newer attribute is safe on older platforms because it will
     70  * be ignored. Instead, the platform thinks the new attribute is an older, private attribute and
     71  * will interpret it as such. This leads to unintended styling and exceptions thrown due to
     72  * unexpected types.
     73  *
     74  * By moving the private attributes to a completely different type, this ID conflict will never
     75  * occur.
     76  */
     77 struct PrivateAttributeMover : public IResourceTableConsumer {
     78     bool consume(IAaptContext* context, ResourceTable* table) override;
     79 };
     80 
     81 /**
     82  * Resolves attributes in the XmlResource and compiles string values to resource values.
     83  * Once an XmlResource is processed by this linker, it is ready to be flattened.
     84  */
     85 class XmlReferenceLinker : public IXmlResourceConsumer {
     86 private:
     87     std::set<int> mSdkLevelsFound;
     88 
     89 public:
     90     bool consume(IAaptContext* context, xml::XmlResource* resource) override;
     91 
     92     /**
     93      * Once the XmlResource has been consumed, this returns the various SDK levels in which
     94      * framework attributes used within the XML document were defined.
     95      */
     96     inline const std::set<int>& getSdkLevels() const {
     97         return mSdkLevelsFound;
     98     }
     99 };
    100 
    101 } // namespace aapt
    102 
    103 #endif /* AAPT_LINKER_LINKERS_H */
    104