OSDN Git Service

Fix fraction and dimension type in Resource.proto
authorMichael Wachenschwanz <mwachens@google.com>
Thu, 19 Apr 2018 01:52:47 +0000 (18:52 -0700)
committerMichael Wachenschwanz <mwachens@google.com>
Fri, 20 Apr 2018 17:46:47 +0000 (10:46 -0700)
Change fraction and dimnesion type from float to uint32

Change-Id: I641dc42cf7b6bdd8bc16c8cdbd573ec3281a084a
Fixes: 78182701
Test: aapt2_tests

tools/aapt2/Resources.proto
tools/aapt2/format/proto/ProtoDeserialize.cpp
tools/aapt2/format/proto/ProtoSerialize.cpp
tools/aapt2/format/proto/ProtoSerialize_test.cpp

index df483b2..d7a3771 100644 (file)
@@ -306,6 +306,7 @@ message FileReference {
 }
 
 // A value that represents a primitive data type (float, int, boolean, etc.).
+// Refer to Res_value in ResourceTypes.h for info on types and formatting
 message Primitive {
   message NullType {
   }
@@ -315,8 +316,8 @@ message Primitive {
     NullType null_value = 1;
     EmptyType empty_value = 2;
     float float_value = 3;
-    float dimension_value = 4;
-    float fraction_value = 5;
+    uint32 dimension_value = 13;
+    uint32 fraction_value = 14;
     int32 int_decimal_value = 6;
     uint32 int_hexadecimal_value = 7;
     bool boolean_value = 8;
@@ -324,6 +325,8 @@ message Primitive {
     uint32 color_rgb8_value = 10;
     uint32 color_argb4_value = 11;
     uint32 color_rgb4_value = 12;
+    float dimension_value_deprecated = 4 [deprecated=true];
+    float fraction_value_deprecated = 5 [deprecated=true];
   }
 }
 
index f1eb952..3b101b7 100644 (file)
@@ -780,13 +780,11 @@ std::unique_ptr<Item> DeserializeItemFromPb(const pb::Item& pb_item,
         } break;
         case pb::Primitive::kDimensionValue: {
           val.dataType = android::Res_value::TYPE_DIMENSION;
-          float dimen_val = pb_prim.dimension_value();
-          val.data = *(uint32_t*)&dimen_val;
+          val.data  = pb_prim.dimension_value();
         } break;
         case pb::Primitive::kFractionValue: {
           val.dataType = android::Res_value::TYPE_FRACTION;
-          float fraction_val = pb_prim.fraction_value();
-          val.data = *(uint32_t*)&fraction_val;
+          val.data  = pb_prim.fraction_value();
         } break;
         case pb::Primitive::kIntDecimalValue: {
           val.dataType = android::Res_value::TYPE_INT_DEC;
@@ -816,6 +814,16 @@ std::unique_ptr<Item> DeserializeItemFromPb(const pb::Item& pb_item,
           val.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
           val.data = pb_prim.color_rgb4_value();
         } break;
+        case pb::Primitive::kDimensionValueDeprecated: {  // DEPRECATED
+          val.dataType = android::Res_value::TYPE_DIMENSION;
+          float dimen_val = pb_prim.dimension_value_deprecated();
+          val.data = *(uint32_t*)&dimen_val;
+        } break;
+        case pb::Primitive::kFractionValueDeprecated: {  // DEPRECATED
+          val.dataType = android::Res_value::TYPE_FRACTION;
+          float fraction_val = pb_prim.fraction_value_deprecated();
+          val.data = *(uint32_t*)&fraction_val;
+        } break;
         default: {
           LOG(FATAL) << "Unexpected Primitive type: "
                      << static_cast<uint32_t>(pb_prim.oneof_value_case());
index 2e56359..411cc29 100644 (file)
@@ -452,10 +452,10 @@ class ValueSerializer : public ConstValueVisitor {
         pb_prim->set_float_value(*(float*)&val.data);
       } break;
       case android::Res_value::TYPE_DIMENSION: {
-        pb_prim->set_dimension_value(*(float*)&val.data);
+        pb_prim->set_dimension_value(val.data);
       } break;
       case android::Res_value::TYPE_FRACTION: {
-        pb_prim->set_fraction_value(*(float*)&val.data);
+        pb_prim->set_fraction_value(val.data);
       } break;
       case android::Res_value::TYPE_INT_DEC: {
         pb_prim->set_int_decimal_value(static_cast<int32_t>(val.data));
index 6366a3d..21fdbd8 100644 (file)
@@ -271,6 +271,7 @@ TEST(ProtoSerializeTest, SerializeAndDeserializePrimitives) {
           .AddValue("android:integer/hex_int_abcd", ResourceUtils::TryParseInt("0xABCD"))
           .AddValue("android:dimen/dimen_1.39mm", ResourceUtils::TryParseFloat("1.39mm"))
           .AddValue("android:fraction/fraction_27", ResourceUtils::TryParseFloat("27%"))
+          .AddValue("android:dimen/neg_2.3in", ResourceUtils::TryParseFloat("-2.3in"))
           .AddValue("android:integer/null", ResourceUtils::MakeEmpty())
           .Build();
 
@@ -353,6 +354,12 @@ TEST(ProtoSerializeTest, SerializeAndDeserializePrimitives) {
   EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_FRACTION));
   EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseFloat("27%")->value.data));
 
+  bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:dimen/neg_2.3in",
+                                                          ConfigDescription::DefaultConfig(), "");
+  ASSERT_THAT(bp, NotNull());
+  EXPECT_THAT(bp->value.dataType, Eq(android::Res_value::TYPE_DIMENSION));
+  EXPECT_THAT(bp->value.data, Eq(ResourceUtils::TryParseFloat("-2.3in")->value.data));
+
   bp = test::GetValueForConfigAndProduct<BinaryPrimitive>(&new_table, "android:integer/null",
                                                           ConfigDescription::DefaultConfig(), "");
   ASSERT_THAT(bp, NotNull());