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

Commit e08e0379 authored by Jeremy Meyer's avatar Jeremy Meyer
Browse files

Add support for flagging xml and png files

This extends the previous change that added the ability to flag resource
directories so that xml and png files are now supported.

Test: Automated
Bug: 329436914
Flag: EXEMPT Aconfig not supported on host tools
Change-Id: I9f2b6b15ba0078ea33188f1a554377784cff9786
parent 988be5d9
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
@@ -132,6 +133,24 @@ public class ResourceFlaggingTest {
        assertThat((View) ll.findViewById(R.id.text2)).isNotNull();
    }

    @Test
    public void testEnabledFlagLayoutOverrides() {
        LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.layout3, null);
        assertThat(ll).isNotNull();
        assertThat((View) ll.findViewById(R.id.text1)).isNotNull();
        assertThat(((TextView) ll.findViewById(R.id.text1)).getText()).isEqualTo("foobar");
    }

    @Test(expected = Resources.NotFoundException.class)
    public void testDisabledLayout() {
        getLayoutInflater().inflate(R.layout.layout2, null);
    }

    @Test(expected = Resources.NotFoundException.class)
    public void testDisabledDrawable() {
        mResources.getDrawable(R.drawable.removedpng);
    }

    private LayoutInflater getLayoutInflater() {
        ContextWrapper c = new ContextWrapper(mContext) {
            private LayoutInflater mInflater;
+6 −0
Original line number Diff line number Diff line
@@ -243,6 +243,12 @@ struct ResourceFile {

  // Exported symbols
  std::vector<SourcedResourceName> exported_symbols;

  // Flag status
  FlagStatus flag_status = FlagStatus::NoFlag;

  // Flag
  std::optional<FeatureFlagAttribute> flag;
};

/**
+2 −18
Original line number Diff line number Diff line
@@ -547,7 +547,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
  });

  std::string_view resource_type = parser->element_name();
  if (auto flag = GetFlag(parser)) {
  if (auto flag = ParseFlag(xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag"))) {
    if (options_.flag) {
      diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
                   << "Resource flag are not allowed both in the path and in the file");
@@ -747,22 +747,6 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
  return false;
}

std::optional<FeatureFlagAttribute> ResourceParser::GetFlag(xml::XmlPullParser* parser) {
  auto name = xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag");
  if (name) {
    FeatureFlagAttribute flag;
    if (name->starts_with('!')) {
      flag.negated = true;
      flag.name = name->substr(1);
    } else {
      flag.name = name.value();
    }
    return flag;
  } else {
    return {};
  }
}

bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
                               ParsedResource* out_resource,
                               const uint32_t format) {
@@ -1669,7 +1653,7 @@ bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
    const std::string& element_namespace = parser->element_namespace();
    const std::string& element_name = parser->element_name();
    if (element_namespace.empty() && element_name == "item") {
      auto flag = GetFlag(parser);
      auto flag = ParseFlag(xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag"));
      std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
      if (!item) {
        diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
+0 −2
Original line number Diff line number Diff line
@@ -90,8 +90,6 @@ class ResourceParser {
 private:
  DISALLOW_COPY_AND_ASSIGN(ResourceParser);

  std::optional<FeatureFlagAttribute> GetFlag(xml::XmlPullParser* parser);

  std::optional<FlattenedXmlSubTree> CreateFlattenSubTree(xml::XmlPullParser* parser);

  // Parses the XML subtree as a StyleString (flattened XML representation for strings with
+5 −0
Original line number Diff line number Diff line
@@ -49,4 +49,9 @@ message CompiledFile {

  // Any symbols this file auto-generates/exports (eg. @+id/foo in an XML file).
  repeated Symbol exported_symbol = 5;

  // The status of the flag the file is behind if any
  uint32 flag_status = 6;
  bool flag_negated = 7;
  string flag_name = 8;
}
Loading