Read manifest values using resource id in idmap2

Idmap2 currently reads android:targetPackage and android:targetName
from overlay manifests by looking for an attribute with the name of
the attribute resource.

This fixes the divergence from package parsing by finding the
attributes using their resource ids.

Bug: 175060836
Test: libidmap2_tests
Change-Id: I09965e5880a1e6c48c3f8077db0c595484804ce7
diff --git a/cmds/idmap2/libidmap2/XmlParser.cpp b/cmds/idmap2/libidmap2/XmlParser.cpp
index 4030b83..00baea4 100644
--- a/cmds/idmap2/libidmap2/XmlParser.cpp
+++ b/cmds/idmap2/libidmap2/XmlParser.cpp
@@ -90,15 +90,27 @@
   return String8(key16).c_str();
 }
 
-Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& name) const {
-  auto value = GetAttributeValue(name);
-  if (!value) {
-    return value.GetError();
+template <typename Func>
+Result<Res_value> FindAttribute(const ResXMLParser& parser, const std::string& label,
+                                Func&& predicate) {
+  for (size_t i = 0; i < parser.getAttributeCount(); i++) {
+    if (!predicate(i)) {
+      continue;
+    }
+    Res_value res_value{};
+    if (parser.getAttributeValue(i, &res_value) == BAD_TYPE) {
+      return Error(R"(Bad value for attribute "%s")", label.c_str());
+    }
+    return res_value;
   }
+  return Error(R"(Failed to find attribute "%s")", label.c_str());
+}
 
-  switch ((*value).dataType) {
+Result<std::string> GetStringValue(const ResXMLParser& parser, const Res_value& value,
+                                   const std::string& label) {
+  switch (value.dataType) {
     case Res_value::TYPE_STRING: {
-      if (auto str = parser_.getStrings().string8ObjectAt((*value).data); str.ok()) {
+      if (auto str = parser.getStrings().string8ObjectAt(value.data); str.ok()) {
         return std::string(str->string());
       }
       break;
@@ -106,31 +118,37 @@
     case Res_value::TYPE_INT_DEC:
     case Res_value::TYPE_INT_HEX:
     case Res_value::TYPE_INT_BOOLEAN: {
-      return std::to_string((*value).data);
+      return std::to_string(value.data);
     }
   }
+  return Error(R"(Failed to convert attribute "%s" value to a string)", label.c_str());
+}
 
-  return Error(R"(Failed to convert attribute "%s" value to a string)", name.c_str());
+Result<Res_value> XmlParser::Node::GetAttributeValue(ResourceId attr,
+                                                     const std::string& label) const {
+  return FindAttribute(parser_, label, [&](size_t index) -> bool {
+    return parser_.getAttributeNameResID(index) == attr;
+  });
 }
 
 Result<Res_value> XmlParser::Node::GetAttributeValue(const std::string& name) const {
-  size_t len;
-  for (size_t i = 0; i < parser_.getAttributeCount(); i++) {
-    const String16 key16(parser_.getAttributeName(i, &len));
+  return FindAttribute(parser_, name, [&](size_t index) -> bool {
+    size_t len;
+    const String16 key16(parser_.getAttributeName(index, &len));
     std::string key = String8(key16).c_str();
-    if (key != name) {
-      continue;
-    }
+    return key == name;
+  });
+}
 
-    Res_value res_value{};
-    if (parser_.getAttributeValue(i, &res_value) == BAD_TYPE) {
-      return Error(R"(Bad value for attribute "%s")", name.c_str());
-    }
+Result<std::string> XmlParser::Node::GetAttributeStringValue(ResourceId attr,
+                                                             const std::string& label) const {
+  auto value = GetAttributeValue(attr, label);
+  return value ? GetStringValue(parser_, *value, label) : value.GetError();
+}
 
-    return res_value;
-  }
-
-  return Error(R"(Failed to find attribute "%s")", name.c_str());
+Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& name) const {
+  auto value = GetAttributeValue(name);
+  return value ? GetStringValue(parser_, *value, name) : value.GetError();
 }
 
 Result<std::unique_ptr<const XmlParser>> XmlParser::Create(const void* data, size_t size,