Created resuable DumpApkCommand and added "badger"

This change refactors the dump commands to inherit from a base
DumpApkCommand and adds a command that prints out an ASCII
image of a badger if the user wrote "badger" instead of
"badging". The command is hidden from the help menu.

Bug: 73535002
Test: manual
Change-Id: I9bdd8a7bbf6a4282c4933e5c478f6d1d8e32d99e
diff --git a/tools/aapt2/cmd/Dump.h b/tools/aapt2/cmd/Dump.h
index 9ec820d..cd215b0 100644
--- a/tools/aapt2/cmd/Dump.h
+++ b/tools/aapt2/cmd/Dump.h
@@ -19,14 +19,62 @@
 
 #include "Command.h"
 #include "Debug.h"
+#include "LoadedApk.h"
 #include "dump/DumpManifest.h"
 
 namespace aapt {
 
-/** Command the contents of files generated from the compilation stage. */
+/**
+ * The base command for dumping information about apks. When the command is executed, the command
+ * performs the DumpApkCommand::Dump() operation on each apk provided as a file argument.
+ **/
+class DumpApkCommand : public Command {
+ public:
+  explicit DumpApkCommand(const std::string&& name, text::Printer* printer, IDiagnostics* diag)
+      : Command(name), printer_(printer), diag_(diag) {
+  }
+
+  text::Printer* GetPrinter() {
+    return printer_;
+  }
+
+  IDiagnostics* GetDiagnostics() {
+    return diag_;
+  }
+
+  /** Perform the dump operation on the apk. */
+  virtual int Dump(LoadedApk* apk) = 0;
+
+  int Action(const std::vector<std::string>& args) final {
+    if (args.size() < 1) {
+      diag_->Error(DiagMessage() << "No dump apk specified.");
+      return 1;
+    }
+
+    bool error = false;
+    for (auto apk : args) {
+      auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_);
+      if (!loaded_apk) {
+        error = true;
+        continue;
+      }
+
+      error |= Dump(loaded_apk.get());
+    }
+
+    return error;
+  }
+
+ private:
+  text::Printer* printer_;
+  IDiagnostics* diag_;
+};
+
+/** Command that prints contents of files generated from the compilation stage. */
 class DumpAPCCommand : public Command {
  public:
-  explicit DumpAPCCommand(IDiagnostics* diag) : Command("apc"), diag_(diag) {
+  explicit DumpAPCCommand(text::Printer* printer, IDiagnostics* diag)
+      : Command("apc"), printer_(printer), diag_(diag) {
     SetDescription("Print the contents of the AAPT2 Container (APC) generated fom compilation.");
     AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.",
                       &no_values_);
@@ -36,120 +84,162 @@
   int Action(const std::vector<std::string>& args) override;
 
  private:
+  text::Printer* printer_;
   IDiagnostics* diag_;
-  bool verbose_ = false;
   bool no_values_ = false;
+  bool verbose_ = false;
 };
 
-/** Prints every configuration used by a resource in an APK. */
-class DumpConfigsCommand : public Command {
+/** Easter egg command shown when users enter "badger" instead of "badging". */
+class DumpBadgerCommand : public Command {
  public:
-  explicit DumpConfigsCommand(IDiagnostics* diag) : Command("configurations"), diag_(diag) {
-    SetDescription("Print every configuration used by a resource in the APK.");
+  explicit DumpBadgerCommand(text::Printer* printer) : Command("badger"), printer_(printer) {
   }
 
   int Action(const std::vector<std::string>& args) override;
 
  private:
-  IDiagnostics* diag_;
+  text::Printer* printer_;
+  const static char kBadgerData[2925];
 };
 
-/** Prints the contents of the resource table string pool in the APK. */
-class DumpStringsCommand : public Command {
-  public:
-    explicit DumpStringsCommand(IDiagnostics* diag) : Command("strings"), diag_(diag) {
-      SetDescription("Print the contents of the resource table string pool in the APK.");
-    }
-
-    int Action(const std::vector<std::string>& args) override;
-
-  private:
-    IDiagnostics* diag_;
-};
-
-/** Prints the contents of the resource table from the APK. */
-class DumpTableCommand : public Command {
+class DumpBadgingCommand : public DumpApkCommand {
  public:
-  explicit DumpTableCommand(IDiagnostics* diag) : Command("resources"), diag_(diag) {
+  explicit DumpBadgingCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("badging", printer, diag) {
+    SetDescription("Print information extracted from the manifest of the APK.");
+    AddOptionalSwitch("--include-meta-data", "Include meta-data information.",
+                      &options_.include_meta_data);
+  }
+
+  int Dump(LoadedApk* apk) override {
+    return DumpManifest(apk, options_, GetPrinter(), GetDiagnostics());
+  }
+
+ private:
+  DumpManifestOptions options_;
+};
+
+class DumpConfigsCommand : public DumpApkCommand {
+ public:
+  explicit DumpConfigsCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("configurations", printer, diag) {
+    SetDescription("Print every configuration used by a resource in the APK.");
+  }
+
+  int Dump(LoadedApk* apk) override;
+};
+
+class DumpPackageNameCommand : public DumpApkCommand {
+ public:
+  explicit DumpPackageNameCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("packagename", printer, diag) {
+    SetDescription("Print the package name of the APK.");
+  }
+
+  int Dump(LoadedApk* apk) override;
+};
+
+class DumpPermissionsCommand : public DumpApkCommand {
+ public:
+  explicit DumpPermissionsCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("permissions", printer, diag) {
+    SetDescription("Print the permissions extracted from the manifest of the APK.");
+  }
+
+  int Dump(LoadedApk* apk) override {
+    DumpManifestOptions options;
+    options.only_permissions = true;
+    return DumpManifest(apk, options, GetPrinter(), GetDiagnostics());
+  }
+};
+
+class DumpStringsCommand : public DumpApkCommand {
+ public:
+  explicit DumpStringsCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("strings", printer, diag) {
+    SetDescription("Print the contents of the resource table string pool in the APK.");
+  }
+
+  int Dump(LoadedApk* apk) override;
+};
+
+class DumpTableCommand : public DumpApkCommand {
+ public:
+  explicit DumpTableCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("resources", printer, diag) {
     SetDescription("Print the contents of the resource table from the APK.");
     AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.",
                       &no_values_);
     AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
   }
 
-  int Action(const std::vector<std::string>& args) override;
+  int Dump(LoadedApk* apk) override;
 
  private:
-  IDiagnostics* diag_;
-  bool verbose_ = false;
   bool no_values_ = false;
+  bool verbose_ = false;
 };
 
-/** Prints the string pool of a compiled xml in an APK. */
-class DumpXmlStringsCommand : public Command {
-public:
-    explicit DumpXmlStringsCommand(IDiagnostics* diag) : Command("xmlstrings"), diag_(diag) {
-      SetDescription("Print the string pool of a compiled xml in an APK.");
-      AddRequiredFlagList("--file", "A compiled xml file to print", &files_);
-    }
-
-    int Action(const std::vector<std::string>& args) override;
-
-private:
-    IDiagnostics* diag_;
-    std::vector<std::string> files_;
-};
-
-
-/** Prints the tree of a compiled xml in an APK. */
-class DumpXmlTreeCommand : public Command {
+class DumpXmlStringsCommand : public DumpApkCommand {
  public:
-  explicit DumpXmlTreeCommand(IDiagnostics* diag) : Command("xmltree"), diag_(diag) {
+  explicit DumpXmlStringsCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("xmlstrings", printer, diag) {
+    SetDescription("Print the string pool of a compiled xml in an APK.");
+    AddRequiredFlagList("--file", "A compiled xml file to print", &files_);
+  }
+
+  int Dump(LoadedApk* apk) override;
+
+ private:
+  std::vector<std::string> files_;
+};
+
+class DumpXmlTreeCommand : public DumpApkCommand {
+ public:
+  explicit DumpXmlTreeCommand(text::Printer* printer, IDiagnostics* diag)
+      : DumpApkCommand("xmltree", printer, diag) {
     SetDescription("Print the tree of a compiled xml in an APK.");
     AddRequiredFlagList("--file", "A compiled xml file to print", &files_);
   }
 
-  int Action(const std::vector<std::string>& args) override;
+  int Dump(LoadedApk* apk) override;
 
  private:
-  IDiagnostics* diag_;
   std::vector<std::string> files_;
 };
 
-/** Prints the contents of the resource table from the APK. */
-class DumpPackageNameCommand : public Command {
- public:
-  explicit DumpPackageNameCommand(IDiagnostics* diag) : Command("packagename"), diag_(diag) {
-    SetDescription("Print the package name of the APK.");
-  }
-
-  int Action(const std::vector<std::string>& args) override;
-
- private:
-  IDiagnostics* diag_;
-};
-
 /** The default dump command. Performs no action because a subcommand is required. */
 class DumpCommand : public Command {
  public:
-  explicit DumpCommand(IDiagnostics* diag) : Command("dump", "d"), diag_(diag) {
-    AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpTableCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(diag_));
-    AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(diag_));
+  explicit DumpCommand(text::Printer* printer, IDiagnostics* diag)
+      : Command("dump", "d"), diag_(diag) {
+    AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpTableCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(printer, diag_));
+    AddOptionalSubcommand(util::make_unique<DumpBadgerCommand>(printer), /* hidden */ true);
   }
 
-  int Action(const std::vector<std::string>& args) override;
+  int Action(const std::vector<std::string>& args) override {
+    if (args.size() == 0) {
+      diag_->Error(DiagMessage() << "no subcommand specified");
+    } else {
+      diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'");
+    }
+    Usage(&std::cerr);
+    return 1;
+  }
 
  private:
   IDiagnostics* diag_;
 };
 
-}// namespace aapt
+}  // namespace aapt
 
-#endif //AAPT2_DUMP_H
+#endif  // AAPT2_DUMP_H