Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c1676807 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Retain parsed attribute type

If the value of an attribute enum is defined as a hexadecimal integer,
flatten uses of the attribute as with the
android::Res_value::TYPE_INT_HEX type.

This change adds a "type" field to pb::Attribute::Symbol, which if left
unset, will have a default value of android::Res_value::TYPE_INT_DEC
when deserialized by aapt2.

Bug: 124474141
Test: aapt2_tests and manual compilation of files and inspection using
`aapt2 dump chunks`
Change-Id: Ibf12394284fdbe3a8047f7ecf4fe68517dfc3abb
parent e05fdd2f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1387,7 +1387,7 @@ Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(

  return Attribute::Symbol{
      Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
      val.data};
      val.data, val.dataType};
}

bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
+4 −1
Original line number Diff line number Diff line
@@ -401,7 +401,7 @@ TEST_F(ResourceParserTest, ParseEnumAttr) {
  std::string input = R"(
      <attr name="foo">
        <enum name="bar" value="0"/>
        <enum name="bat" value="1"/>
        <enum name="bat" value="0x1"/>
        <enum name="baz" value="2"/>
      </attr>)";
  ASSERT_TRUE(TestParse(input));
@@ -414,14 +414,17 @@ TEST_F(ResourceParserTest, ParseEnumAttr) {
  ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
  EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
  EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
  EXPECT_THAT(enum_attr->symbols[0].type, Eq(Res_value::TYPE_INT_DEC));

  ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
  EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
  EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
  EXPECT_THAT(enum_attr->symbols[1].type, Eq(Res_value::TYPE_INT_HEX));

  ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
  EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
  EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
  EXPECT_THAT(enum_attr->symbols[2].type, Eq(Res_value::TYPE_INT_DEC));
}

TEST_F(ResourceParserTest, ParseFlagAttr) {
+1 −1
Original line number Diff line number Diff line
@@ -378,7 +378,7 @@ std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
    const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
    if (trimmed_str == enum_symbol_resource_name.entry) {
      android::Res_value value = {};
      value.dataType = android::Res_value::TYPE_INT_DEC;
      value.dataType = symbol.type;
      value.data = symbol.value;
      return util::make_unique<BinaryPrimitive>(value);
    }
+1 −0
Original line number Diff line number Diff line
@@ -292,6 +292,7 @@ struct Attribute : public BaseValue<Attribute> {
  struct Symbol {
    Reference symbol;
    uint32_t value;
    uint8_t type;

    friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
  };
+3 −0
Original line number Diff line number Diff line
@@ -388,6 +388,9 @@ message Attribute {

    // The value of the enum/flag.
    uint32 value = 4;

    // The data type of the enum/flag as defined in android::Res_value.
    uint32 type = 5;
  }

  // Bitmask of formats allowed for an attribute.
Loading