Move StringPool to libandroidfw

Test: verified affected tests pass
Bug: 232940948
Change-Id: I22089893d7e5013f759c39ce190bec07fa6435db
diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp
index e27b9aa..0409f73 100644
--- a/tools/aapt2/cmd/Compile.cpp
+++ b/tools/aapt2/cmd/Compile.cpp
@@ -17,19 +17,17 @@
 #include "Compile.h"
 
 #include <dirent.h>
+
 #include <string>
 
+#include "ResourceParser.h"
+#include "ResourceTable.h"
 #include "android-base/errors.h"
 #include "android-base/file.h"
 #include "android-base/utf8.h"
 #include "androidfw/ConfigDescription.h"
+#include "androidfw/IDiagnostics.h"
 #include "androidfw/StringPiece.h"
-#include "google/protobuf/io/coded_stream.h"
-#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
-
-#include "Diagnostics.h"
-#include "ResourceParser.h"
-#include "ResourceTable.h"
 #include "cmd/Util.h"
 #include "compile/IdAssigner.h"
 #include "compile/InlineXmlFormatParser.h"
@@ -39,6 +37,8 @@
 #include "format/Archive.h"
 #include "format/Container.h"
 #include "format/proto/ProtoSerialize.h"
+#include "google/protobuf/io/coded_stream.h"
+#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
 #include "io/BigBufferStream.h"
 #include "io/FileStream.h"
 #include "io/FileSystem.h"
@@ -61,7 +61,7 @@
 namespace aapt {
 
 struct ResourcePathData {
-  Source source;
+  android::Source source;
   std::string resource_dir;
   std::string name;
   std::string extension;
@@ -122,9 +122,8 @@
     }
   }
 
-  const Source res_path = options.source_path
-      ? StringPiece(options.source_path.value())
-      : StringPiece(path);
+  const android::Source res_path =
+      options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path);
 
   return ResourcePathData{res_path, dir_str.to_string(), name.to_string(),
                           extension.to_string(), config_str.to_string(), config};
@@ -154,8 +153,8 @@
   {
     auto fin = file->OpenInputStream();
     if (fin->HadError()) {
-      context->GetDiagnostics()->Error(DiagMessage(path_data.source)
-          << "failed to open file: " << fin->GetError());
+      context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
+                                       << "failed to open file: " << fin->GetError());
       return false;
     }
 
@@ -191,7 +190,7 @@
 
   // Create the file/zip entry.
   if (!writer->StartEntry(output_path, 0)) {
-    context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
+    context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
     return false;
   }
 
@@ -204,13 +203,13 @@
     pb::ResourceTable pb_table;
     SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
     if (!container_writer.AddResTableEntry(pb_table)) {
-      context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
+      context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
       return false;
     }
   }
 
   if (!writer->FinishEntry()) {
-    context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
+    context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
     return false;
   }
 
@@ -218,7 +217,7 @@
     io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
 
     if (fout_text.HadError()) {
-      context->GetDiagnostics()->Error(DiagMessage()
+      context->GetDiagnostics()->Error(android::DiagMessage()
                                        << "failed writing to'"
                                        << options.generate_text_symbols_path.value()
                                        << "': " << fout_text.GetError());
@@ -282,11 +281,11 @@
 
 static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
                                        io::KnownSizeInputStream* in, IArchiveWriter* writer,
-                                       IDiagnostics* diag) {
+                                       android::IDiagnostics* diag) {
   TRACE_CALL();
   // Start the entry so we can write the header.
   if (!writer->StartEntry(output_path, 0)) {
-    diag->Error(DiagMessage(output_path) << "failed to open file");
+    diag->Error(android::DiagMessage(output_path) << "failed to open file");
     return false;
   }
 
@@ -300,20 +299,20 @@
     SerializeCompiledFileToPb(file, &pb_compiled_file);
 
     if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
-      diag->Error(DiagMessage(output_path) << "failed to write entry data");
+      diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
       return false;
     }
   }
 
   if (!writer->FinishEntry()) {
-    diag->Error(DiagMessage(output_path) << "failed to finish writing data");
+    diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
     return false;
   }
   return true;
 }
 
 static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
-                                  ContainerWriter* container_writer, IDiagnostics* diag) {
+                                  ContainerWriter* container_writer, android::IDiagnostics* diag) {
   pb::internal::CompiledFile pb_compiled_file;
   SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
 
@@ -324,7 +323,7 @@
   io::StringInputStream serialized_in(serialized_xml);
 
   if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
-    diag->Error(DiagMessage(output_path) << "failed to write entry data");
+    diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
     return false;
   }
   return true;
@@ -334,12 +333,12 @@
   const file::FileType file_type = file::GetFileType(input_path);
   if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
     if (file_type == file::FileType::kDirectory) {
-      context->GetDiagnostics()->Error(DiagMessage(input_path)
+      context->GetDiagnostics()->Error(android::DiagMessage(input_path)
                                        << "resource file cannot be a directory");
     } else if (file_type == file::FileType::kNonExistant) {
-      context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
+      context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
     } else {
-      context->GetDiagnostics()->Error(DiagMessage(input_path)
+      context->GetDiagnostics()->Error(android::DiagMessage(input_path)
                                        << "not a valid resource file");
     }
     return false;
@@ -352,14 +351,14 @@
                        const std::string& output_path) {
   TRACE_CALL();
   if (context->IsVerbose()) {
-    context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
+    context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
   }
 
   std::unique_ptr<xml::XmlResource> xmlres;
   {
     auto fin = file->OpenInputStream();
     if (fin->HadError()) {
-      context->GetDiagnostics()->Error(DiagMessage(path_data.source)
+      context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
                                        << "failed to open file: " << fin->GetError());
       return false;
     }
@@ -389,7 +388,7 @@
 
   // Start the entry so we can write the header.
   if (!writer->StartEntry(output_path, 0)) {
-    context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
+    context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
     return false;
   }
 
@@ -416,7 +415,8 @@
   }
 
   if (!writer->FinishEntry()) {
-    context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
+    context->GetDiagnostics()->Error(android::DiagMessage(output_path)
+                                     << "failed to finish writing data");
     return false;
   }
 
@@ -424,7 +424,7 @@
     io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
 
     if (fout_text.HadError()) {
-      context->GetDiagnostics()->Error(DiagMessage()
+      context->GetDiagnostics()->Error(android::DiagMessage()
                                        << "failed writing to'"
                                        << options.generate_text_symbols_path.value()
                                        << "': " << fout_text.GetError());
@@ -452,10 +452,10 @@
                        const std::string& output_path) {
   TRACE_CALL();
   if (context->IsVerbose()) {
-    context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
+    context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
   }
 
-  BigBuffer buffer(4096);
+  android::BigBuffer buffer(4096);
   ResourceFile res_file;
   res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
   res_file.config = path_data.config;
@@ -465,11 +465,12 @@
   {
     auto data = file->OpenAsData();
     if (!data) {
-      context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
+      context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
+                                       << "failed to open file ");
       return false;
     }
 
-    BigBuffer crunched_png_buffer(4096);
+    android::BigBuffer crunched_png_buffer(4096);
     io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
 
     // Ensure that we only keep the chunks we care about if we end up
@@ -486,7 +487,7 @@
       std::string err;
       nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
       if (!nine_patch) {
-        context->GetDiagnostics()->Error(DiagMessage() << err);
+        context->GetDiagnostics()->Error(android::DiagMessage() << err);
         return false;
       }
 
@@ -503,8 +504,8 @@
       }
 
       if (context->IsVerbose()) {
-        context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
-                                                                      << *nine_patch);
+        context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
+                                        << "9-patch: " << *nine_patch);
       }
     }
 
@@ -522,13 +523,13 @@
       // The re-encoded PNG is larger than the original, and there is
       // no mandatory transformation. Use the original.
       if (context->IsVerbose()) {
-        context->GetDiagnostics()->Note(DiagMessage(path_data.source)
+        context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
                                         << "original PNG is smaller than crunched PNG"
                                         << ", using original");
       }
 
       png_chunk_filter.Rewind();
-      BigBuffer filtered_png_buffer(4096);
+      android::BigBuffer filtered_png_buffer(4096);
       io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
       io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
       buffer.AppendBuffer(std::move(filtered_png_buffer));
@@ -538,13 +539,13 @@
       // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
       // This will help catch exotic cases where the new code may generate larger PNGs.
       std::stringstream legacy_stream(content.to_string());
-      BigBuffer legacy_buffer(4096);
+      android::BigBuffer legacy_buffer(4096);
       Png png(context->GetDiagnostics());
       if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
         return false;
       }
 
-      context->GetDiagnostics()->Note(DiagMessage(path_data.source)
+      context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
                                       << "legacy=" << legacy_buffer.size()
                                       << " new=" << buffer.size());
     }
@@ -560,7 +561,7 @@
                         const std::string& output_path) {
   TRACE_CALL();
   if (context->IsVerbose()) {
-    context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
+    context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
   }
 
   ResourceFile res_file;
@@ -571,7 +572,8 @@
 
   auto data = file->OpenAsData();
   if (!data) {
-    context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
+    context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
+                                     << "failed to open file ");
     return false;
   }
 
@@ -581,7 +583,7 @@
 
 class CompileContext : public IAaptContext {
  public:
-  explicit CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
+  explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
   }
 
   PackageType GetPackageType() override {
@@ -597,7 +599,7 @@
     return verbose_;
   }
 
-  IDiagnostics* GetDiagnostics() override {
+  android::IDiagnostics* GetDiagnostics() override {
     return diagnostics_;
   }
 
@@ -633,7 +635,7 @@
  private:
   DISALLOW_COPY_AND_ASSIGN(CompileContext);
 
-  IDiagnostics* diagnostics_;
+  android::IDiagnostics* diagnostics_;
   bool verbose_ = false;
 };
 
@@ -665,7 +667,7 @@
         path, inputs->GetDirSeparator(), &err_str, options)) {
       path_data = maybe_path_data.value();
     } else {
-      context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << err_str);
+      context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
       error = true;
       continue;
     }
@@ -688,8 +690,8 @@
         }
       }
     } else {
-      context->GetDiagnostics()->Error(DiagMessage()
-          << "invalid file path '" << path_data.source << "'");
+      context->GetDiagnostics()->Error(android::DiagMessage()
+                                       << "invalid file path '" << path_data.source << "'");
       error = true;
       continue;
     }
@@ -699,15 +701,16 @@
     if (compile_func != &CompileFile && !options.legacy_mode
         && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
       error = true;
-      context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
-                                                    << "file name cannot contain '.' other than for"
-                                                    << " specifying the extension");
+      context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
+                                       << "file name cannot contain '.' other than for"
+                                       << " specifying the extension");
       continue;
     }
 
     const std::string out_path = BuildIntermediateContainerFilename(path_data);
     if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
-      context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "file failed to compile");
+      context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
+                                       << "file failed to compile");
       error = true;
     }
   }
@@ -728,9 +731,10 @@
     } else if (visibility_.value() == "default") {
       options_.visibility = Visibility::Level::kUndefined;
     } else {
-      context.GetDiagnostics()->Error(
-          DiagMessage() << "Unrecognized visibility level passes to --visibility: '"
-                        << visibility_.value() << "'. Accepted levels: public, private, default");
+      context.GetDiagnostics()->Error(android::DiagMessage()
+                                      << "Unrecognized visibility level passes to --visibility: '"
+                                      << visibility_.value()
+                                      << "'. Accepted levels: public, private, default");
       return 1;
     }
   }
@@ -739,17 +743,17 @@
 
   // Collect the resources files to compile
   if (options_.res_dir && options_.res_zip) {
-    context.GetDiagnostics()->Error(DiagMessage()
-                                      << "only one of --dir and --zip can be specified");
+    context.GetDiagnostics()->Error(android::DiagMessage()
+                                    << "only one of --dir and --zip can be specified");
     return 1;
   } else if ((options_.res_dir || options_.res_zip) &&
               options_.source_path && args.size() > 1) {
-      context.GetDiagnostics()->Error(DiagMessage(kPath)
-      << "Cannot use an overriding source path with multiple files.");
-      return 1;
+    context.GetDiagnostics()->Error(android::DiagMessage(kPath)
+                                    << "Cannot use an overriding source path with multiple files.");
+    return 1;
   } else if (options_.res_dir) {
     if (!args.empty()) {
-      context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
+      context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
       Usage(&std::cerr);
       return 1;
     }
@@ -758,12 +762,12 @@
     std::string err;
     file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
     if (!file_collection) {
-      context.GetDiagnostics()->Error(DiagMessage(options_.res_dir.value()) << err);
+      context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
       return 1;
     }
   } else if (options_.res_zip) {
     if (!args.empty()) {
-      context.GetDiagnostics()->Error(DiagMessage() << "files given but --zip specified");
+      context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
       Usage(&std::cerr);
       return 1;
     }
@@ -772,7 +776,7 @@
     std::string err;
     file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
     if (!file_collection) {
-      context.GetDiagnostics()->Error(DiagMessage(options_.res_zip.value()) << err);
+      context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
       return 1;
     }
   } else {