Factor out a menu class for screen ui

Also consolidate the duplicate codes to draw the menu in ScreenRecoveryUI
and WearRecoveryUI. This helps us to support text icons as menu in the
future.

Bug: 74397117
Test: Check the menu under recovery on bullhead and a wear device.
Change-Id: Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b
(cherry picked from commit Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b)
diff --git a/screen_ui.h b/screen_ui.h
index f05761c..c1222a5 100644
--- a/screen_ui.h
+++ b/screen_ui.h
@@ -20,6 +20,7 @@
 #include <pthread.h>
 #include <stdio.h>
 
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -28,6 +29,70 @@
 // From minui/minui.h.
 struct GRSurface;
 
+// This class maintains the menu selection and display of the screen ui.
+class Menu {
+ public:
+  Menu(bool scrollable, size_t max_items, size_t max_length);
+
+  bool scrollable() const {
+    return scrollable_;
+  }
+
+  int selection() const {
+    return selection_;
+  }
+
+  // Returns count of menu items.
+  size_t ItemsCount() const;
+  // Returns the index of the first menu item.
+  size_t MenuStart() const;
+  // Returns the index of the last menu item + 1.
+  size_t MenuEnd() const;
+
+  // Menu example:
+  // info:                           Android Recovery
+  //                                 ....
+  // help messages:                  Swipe up/down to move
+  //                                 Swipe left/right to select
+  // empty line (horizontal rule):
+  // menu headers:                   Select file to view
+  // menu items:                     /cache/recovery/last_log
+  //                                 /cache/recovery/last_log.1
+  //                                 /cache/recovery/last_log.2
+  //                                 ...
+  const char* const* text_headers() const;
+  std::string TextItem(size_t index) const;
+
+  // Checks if the menu items fit vertically on the screen. Returns true and set the
+  // |cur_selection_str| if the items exceed the screen limit.
+  bool ItemsOverflow(std::string* cur_selection_str) const;
+
+  // Starts the menu with |headers| and |items| in text. Sets the default selection to
+  // |initial_selection|.
+  void Start(const char* const* headers, const char* const* items, int initial_selection);
+
+  // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is
+  // scrollable.
+  int Select(int sel);
+
+ private:
+  // The menu is scrollable to display more items. Used on wear devices who have smaller screens.
+  const bool scrollable_;
+  // The max number of menu items to fit vertically on a screen.
+  const size_t max_display_items_;
+  // The length of each item to fit horizontally on a screen.
+  const size_t max_item_length_;
+
+  // Internal storage for the menu headers and items in text.
+  const char* const* text_headers_;
+  std::vector<std::string> text_items_;
+
+  // The first item to display on the screen.
+  size_t menu_start_;
+  // Current menu selection.
+  int selection_;
+};
+
 // Implementation of RecoveryUI appropriate for devices with a screen
 // (shows an icon + a progress bar, text logging, menu, etc.)
 class ScreenRecoveryUI : public RecoveryUI {
@@ -44,6 +109,7 @@
   };
 
   ScreenRecoveryUI();
+  explicit ScreenRecoveryUI(bool scrollable_menu);
 
   bool Init(const std::string& locale) override;
 
@@ -101,6 +167,7 @@
   virtual void draw_background_locked();
   virtual void draw_foreground_locked();
   virtual void draw_screen_locked();
+  virtual void draw_menu_and_text_buffer_locked(const char* const* help_message);
   virtual void update_screen_locked();
   virtual void update_progress_locked();
 
@@ -184,10 +251,8 @@
   bool show_text;
   bool show_text_ever;  // has show_text ever been true?
 
-  std::vector<std::string> menu_;
-  const char* const* menu_headers_;
-  bool show_menu;
-  int menu_items, menu_sel;
+  bool scrollable_menu_;
+  std::unique_ptr<Menu> menu_;
 
   // An alternate text screen, swapped with 'text_' when we're viewing a log file.
   char** file_viewer_text_;