Merge "recovery: Remove unneeded include of minui.h."
diff --git a/device.h b/device.h
index 8788b2d..9510fbe 100644
--- a/device.h
+++ b/device.h
@@ -58,6 +58,12 @@
     return ui_;
   }
 
+  // Sets the UI object to the given UI. Used to override the default UI in case initialization
+  // failed, or we want a stub for some reason.
+  virtual void SetUI(RecoveryUI* ui) {
+    ui_ = ui;
+  }
+
   // Called when recovery starts up (after the UI has been obtained and initialized and after the
   // arguments have been parsed, but before anything else).
   virtual void StartRecovery() {};
diff --git a/recovery.cpp b/recovery.cpp
index 8dc04a8..95118ff 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -88,7 +88,6 @@
 // into target_files.zip. Assert the version defined in code and in Android.mk are consistent.
 static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions.");
 
-static std::string locale;
 static bool has_cache = false;
 
 RecoveryUI* ui = nullptr;
@@ -231,7 +230,8 @@
 // Clear the recovery command and prepare to boot a (hopefully working) system,
 // copy our log file to cache as well (for the system to read). This function is
 // idempotent: call it as many times as you like.
-static void finish_recovery() {
+static void finish_recovery(Device* device) {
+  std::string locale = device->GetUI()->GetLocale();
   // Save the locale to cache, so if recovery is next started up without a '--locale' argument
   // (e.g., directly from the bootloader) it will use the last-known locale.
   if (!locale.empty() && has_cache) {
@@ -808,7 +808,7 @@
 // which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery.
 static Device::BuiltinAction prompt_and_wait(Device* device, int status) {
   for (;;) {
-    finish_recovery();
+    finish_recovery(device);
     switch (status) {
       case INSTALL_SUCCESS:
       case INSTALL_NONE:
@@ -896,7 +896,7 @@
 
       case Device::RUN_LOCALE_TEST: {
         ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui);
-        screen_ui->CheckBackgroundTextImages(locale);
+        screen_ui->CheckBackgroundTextImages();
         break;
       }
       case Device::MOUNT_SYSTEM:
@@ -1104,6 +1104,7 @@
   bool shutdown_after = false;
   int retry_count = 0;
   bool security_update = false;
+  std::string locale;
 
   int arg;
   int option_index;
@@ -1181,6 +1182,7 @@
       ui = new StubRecoveryUI();
     }
   }
+  device->SetUI(ui);
 
   // Set background string to "installing security update" for security update,
   // otherwise set it to "installing system update".
@@ -1347,7 +1349,7 @@
   }
 
   // Save logs and clean up before rebooting or shutting down.
-  finish_recovery();
+  finish_recovery(device);
 
   switch (after) {
     case Device::SHUTDOWN:
diff --git a/screen_ui.cpp b/screen_ui.cpp
index 7ae81e5..90e0e30 100644
--- a/screen_ui.cpp
+++ b/screen_ui.cpp
@@ -372,7 +372,7 @@
   std::string header = "Show background text image";
   text_y += DrawTextLine(text_x, text_y, header, true);
   std::string locale_selection = android::base::StringPrintf(
-      "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel, locales_entries.size());
+      "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
   // clang-format off
   std::vector<std::string> instruction = {
     locale_selection,
@@ -395,13 +395,14 @@
   pthread_mutex_unlock(&updateMutex);
 }
 
-void ScreenRecoveryUI::CheckBackgroundTextImages(const std::string& saved_locale) {
+void ScreenRecoveryUI::CheckBackgroundTextImages() {
   // Load a list of locales embedded in one of the resource files.
   std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
   if (locales_entries.empty()) {
     Print("Failed to load locales from the resource files\n");
     return;
   }
+  std::string saved_locale = locale_;
   size_t selected = 0;
   SelectAndShowBackgroundText(locales_entries, selected);
 
@@ -748,6 +749,10 @@
   return true;
 }
 
+std::string ScreenRecoveryUI::GetLocale() {
+  return locale_;
+}
+
 void ScreenRecoveryUI::LoadAnimation() {
   std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
   dirent* de;
diff --git a/screen_ui.h b/screen_ui.h
index fb811ce..d4923f5 100644
--- a/screen_ui.h
+++ b/screen_ui.h
@@ -114,6 +114,7 @@
   explicit ScreenRecoveryUI(bool scrollable_menu);
 
   bool Init(const std::string& locale) override;
+  std::string GetLocale() override;
 
   // overall recovery state ("background image")
   void SetBackground(Icon icon) override;
@@ -147,9 +148,9 @@
 
   void SetColor(UIElement e) const;
 
-  // Check the background text image. Use volume up/down button to cycle through the locales
-  // embedded in the png file, and power button to go back to recovery main menu.
-  void CheckBackgroundTextImages(const std::string& saved_locale);
+  // Checks the background text image, for debugging purpose. It iterates the locales embedded in
+  // the on-device resource files and shows the localized text, for manual inspection.
+  void CheckBackgroundTextImages();
 
  protected:
   // The margin that we don't want to use for showing texts (e.g. round screen, or screen with
diff --git a/stub_ui.h b/stub_ui.h
index 2ccd491..fddf4e7 100644
--- a/stub_ui.h
+++ b/stub_ui.h
@@ -28,6 +28,9 @@
  public:
   StubRecoveryUI() = default;
 
+  std::string GetLocale() override {
+    return "";
+  }
   void SetBackground(Icon /* icon */) override {}
   void SetSystemUpdateText(bool /* security_update */) override {}
 
diff --git a/ui.h b/ui.h
index 35cc36e..f867790 100644
--- a/ui.h
+++ b/ui.h
@@ -57,6 +57,8 @@
   // the given locale. Returns true on success.
   virtual bool Init(const std::string& locale);
 
+  virtual std::string GetLocale() = 0;
+
   // Shows a stage indicator. Called immediately after Init().
   virtual void SetStage(int current, int max) = 0;