blob: 2d0d97d8dd5b07841f3f34fe644bc32be33fc40d [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef RECOVERY_SCREEN_UI_H
18#define RECOVERY_SCREEN_UI_H
19
Elliott Hughes95fc63e2015-04-10 19:12:01 -070020#include <stdio.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070021
Tao Bao26ea9592018-05-09 16:32:02 -070022#include <atomic>
Tao Bao3aec6962018-04-20 09:24:58 -070023#include <functional>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070024#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080025#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070026#include <thread>
Tao Baoe15d7a52017-09-07 13:38:51 -070027#include <vector>
Tao Bao736d59c2017-01-03 10:15:33 -080028
Doug Zongker211aebc2011-10-28 15:13:10 -070029#include "ui.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080030
31// From minui/minui.h.
Tao Bao92bdb5a2018-10-21 12:12:37 -070032class GRSurface;
Doug Zongker211aebc2011-10-28 15:13:10 -070033
Tianjie Xu66dbf632018-10-11 16:54:50 -070034enum class UIElement {
35 HEADER,
36 MENU,
37 MENU_SEL_BG,
38 MENU_SEL_BG_ACTIVE,
39 MENU_SEL_FG,
40 LOG,
41 TEXT_FILL,
42 INFO
43};
44
45// Interface to draw the UI elements on the screen.
46class DrawInterface {
47 public:
48 virtual ~DrawInterface() = default;
49
50 // Sets the color to the predefined value for |element|.
51 virtual void SetColor(UIElement element) const = 0;
52
53 // Draws a highlight bar at (x, y) - (x + width, y + height).
54 virtual void DrawHighlightBar(int x, int y, int width, int height) const = 0;
55
56 // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis.
57 virtual int DrawHorizontalRule(int y) const = 0;
58
59 // Draws a line of text. Returns the offset it should be moving along Y-axis.
60 virtual int DrawTextLine(int x, int y, const std::string& line, bool bold) const = 0;
61
62 // Draws surface portion (sx, sy, w, h) at screen location (dx, dy).
Tao Bao65815b62018-10-23 10:54:02 -070063 virtual void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx,
Tianjie Xu66dbf632018-10-11 16:54:50 -070064 int dy) const = 0;
65
66 // Draws rectangle at (x, y) - (x + w, y + h).
67 virtual void DrawFill(int x, int y, int w, int h) const = 0;
68
69 // Draws given surface (surface->pixel_bytes = 1) as text at (x, y).
Tao Bao65815b62018-10-23 10:54:02 -070070 virtual void DrawTextIcon(int x, int y, const GRSurface* surface) const = 0;
Tianjie Xu66dbf632018-10-11 16:54:50 -070071
72 // Draws multiple text lines. Returns the offset it should be moving along Y-axis.
73 virtual int DrawTextLines(int x, int y, const std::vector<std::string>& lines) const = 0;
74
75 // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines. It
76 // keeps symmetrical margins of 'x' at each end of a line. Returns the offset it should be moving
77 // along Y-axis.
78 virtual int DrawWrappedTextLines(int x, int y, const std::vector<std::string>& lines) const = 0;
79};
80
81// Interface for classes that maintain the menu selection and display.
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070082class Menu {
83 public:
Tianjie Xu66dbf632018-10-11 16:54:50 -070084 virtual ~Menu() = default;
85 // Returns the current menu selection.
86 size_t selection() const;
87 // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is
88 // scrollable.
89 virtual int Select(int sel) = 0;
90 // Displays the menu headers on the screen at offset x, y
91 virtual int DrawHeader(int x, int y) const = 0;
92 // Iterates over the menu items and displays each of them at offset x, y.
93 virtual int DrawItems(int x, int y, int screen_width, bool long_press) const = 0;
94
95 protected:
96 Menu(size_t initial_selection, const DrawInterface& draw_func);
97 // Current menu selection.
98 size_t selection_;
99 // Reference to the class that implements all the draw functions.
100 const DrawInterface& draw_funcs_;
101};
102
103// This class uses strings as the menu header and items.
104class TextMenu : public Menu {
105 public:
Tao Baoe02a5b22018-05-02 15:46:11 -0700106 // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial
107 // selection to |initial_selection|.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700108 TextMenu(bool scrollable, size_t max_items, size_t max_length,
109 const std::vector<std::string>& headers, const std::vector<std::string>& items,
110 size_t initial_selection, int char_height, const DrawInterface& draw_funcs);
111
112 int Select(int sel) override;
113 int DrawHeader(int x, int y) const override;
114 int DrawItems(int x, int y, int screen_width, bool long_press) const override;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700115
116 bool scrollable() const {
117 return scrollable_;
118 }
119
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700120 // Returns count of menu items.
121 size_t ItemsCount() const;
Tao Baoe02a5b22018-05-02 15:46:11 -0700122
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700123 // Returns the index of the first menu item.
124 size_t MenuStart() const;
Tao Baoe02a5b22018-05-02 15:46:11 -0700125
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700126 // Returns the index of the last menu item + 1.
127 size_t MenuEnd() const;
128
129 // Menu example:
130 // info: Android Recovery
131 // ....
132 // help messages: Swipe up/down to move
133 // Swipe left/right to select
134 // empty line (horizontal rule):
135 // menu headers: Select file to view
136 // menu items: /cache/recovery/last_log
137 // /cache/recovery/last_log.1
138 // /cache/recovery/last_log.2
139 // ...
Tao Bao1fe1afe2018-05-01 15:56:05 -0700140 const std::vector<std::string>& text_headers() const;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700141 std::string TextItem(size_t index) const;
142
143 // Checks if the menu items fit vertically on the screen. Returns true and set the
144 // |cur_selection_str| if the items exceed the screen limit.
145 bool ItemsOverflow(std::string* cur_selection_str) const;
146
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700147 private:
148 // The menu is scrollable to display more items. Used on wear devices who have smaller screens.
149 const bool scrollable_;
150 // The max number of menu items to fit vertically on a screen.
151 const size_t max_display_items_;
152 // The length of each item to fit horizontally on a screen.
153 const size_t max_item_length_;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700154 // The menu headers.
155 std::vector<std::string> text_headers_;
156 // The actual menu items trimmed to fit the given properties.
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700157 std::vector<std::string> text_items_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700158 // The first item to display on the screen.
159 size_t menu_start_;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700160
161 // Height in pixels of each character.
162 int char_height_;
163};
164
165// This class uses GRSurfaces* as the menu header and items.
166class GraphicMenu : public Menu {
167 public:
168 // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial
169 // selection to |initial_selection|.
Tianjie Xub99e6062018-10-16 15:13:09 -0700170 GraphicMenu(GRSurface* graphic_headers, const std::vector<GRSurface*>& graphic_items,
171 size_t initial_selection, const DrawInterface& draw_funcs);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700172
173 int Select(int sel) override;
174 int DrawHeader(int x, int y) const override;
175 int DrawItems(int x, int y, int screen_width, bool long_press) const override;
176
177 // Checks if all the header and items are valid GRSurfaces; and that they can fit in the area
Tianjie Xub99e6062018-10-16 15:13:09 -0700178 // defined by |max_width| and |max_height|.
179 static bool Validate(size_t max_width, size_t max_height, GRSurface* graphic_headers,
180 const std::vector<GRSurface*>& graphic_items);
181
182 // Returns true if |surface| fits on the screen with a vertical offset |y|.
183 static bool ValidateGraphicSurface(size_t max_width, size_t max_height, int y,
184 const GRSurface* surface);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700185
186 private:
Tianjie Xu66dbf632018-10-11 16:54:50 -0700187 // Pointers to the menu headers and items in graphic icons. This class does not have the ownership
188 // of the these objects.
189 GRSurface* graphic_headers_;
190 std::vector<GRSurface*> graphic_items_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700191};
192
Doug Zongker211aebc2011-10-28 15:13:10 -0700193// Implementation of RecoveryUI appropriate for devices with a screen
194// (shows an icon + a progress bar, text logging, menu, etc.)
Tianjie Xu66dbf632018-10-11 16:54:50 -0700195class ScreenRecoveryUI : public RecoveryUI, public DrawInterface {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700196 public:
197 ScreenRecoveryUI();
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700198 explicit ScreenRecoveryUI(bool scrollable_menu);
Tao Bao26ea9592018-05-09 16:32:02 -0700199 ~ScreenRecoveryUI() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700200
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700201 bool Init(const std::string& locale) override;
Tao Bao551d2c32018-05-09 20:53:13 -0700202 std::string GetLocale() const override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700203
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700204 // overall recovery state ("background image")
Tao Bao99b2d772017-06-23 22:47:03 -0700205 void SetBackground(Icon icon) override;
206 void SetSystemUpdateText(bool security_update) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700207
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700208 // progress indicator
209 void SetProgressType(ProgressType type) override;
210 void ShowProgress(float portion, float seconds) override;
211 void SetProgress(float fraction) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700212
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700213 void SetStage(int current, int max) override;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800214
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700215 // text log
216 void ShowText(bool visible) override;
217 bool IsTextVisible() override;
218 bool WasTextEverVisible() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700219
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700220 // printing messages
Tao Bao99b2d772017-06-23 22:47:03 -0700221 void Print(const char* fmt, ...) override __printflike(2, 3);
222 void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3);
Tao Bao1d156b92018-05-02 12:43:18 -0700223 void ShowFile(const std::string& filename) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700224
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700225 // menu display
Tao Bao1fe1afe2018-05-01 15:56:05 -0700226 size_t ShowMenu(const std::vector<std::string>& headers, const std::vector<std::string>& items,
227 size_t initial_selection, bool menu_only,
228 const std::function<int(int, bool)>& key_handler) override;
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700229 void SetTitle(const std::vector<std::string>& lines) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700230
Tao Bao99b2d772017-06-23 22:47:03 -0700231 void KeyLongPress(int) override;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700232
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700233 void Redraw();
Doug Zongkerc0441d12013-07-31 11:28:24 -0700234
Tao Bao39c49182018-05-07 22:50:33 -0700235 // Checks the background text image, for debugging purpose. It iterates the locales embedded in
236 // the on-device resource files and shows the localized text, for manual inspection.
237 void CheckBackgroundTextImages();
Tianjie Xu29d55752017-09-20 17:53:46 -0700238
Tianjie Xub99e6062018-10-16 15:13:09 -0700239 // Displays the localized wipe data menu.
240 size_t ShowPromptWipeDataMenu(const std::vector<std::string>& backup_headers,
241 const std::vector<std::string>& backup_items,
242 const std::function<int(int, bool)>& key_handler) override;
243
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700244 protected:
Tianjie Xub99e6062018-10-16 15:13:09 -0700245 static constexpr int kMenuIndent = 4;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700246 // The margin that we don't want to use for showing texts (e.g. round screen, or screen with
247 // rounded corners).
Tao Bao0bc88de2018-07-31 14:53:16 -0700248 const int margin_width_;
249 const int margin_height_;
Tao Bao4521b702017-06-20 18:11:21 -0700250
Tao Bao0470cee2017-08-02 17:11:04 -0700251 // Number of frames per sec (default: 30) for both parts of the animation.
Tao Bao0bc88de2018-07-31 14:53:16 -0700252 const int animation_fps_;
Tao Bao0470cee2017-08-02 17:11:04 -0700253
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700254 // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi.
Tao Bao0bc88de2018-07-31 14:53:16 -0700255 const float density_;
Tao Bao75779652017-09-10 11:28:32 -0700256
257 virtual bool InitTextParams();
258
Tianjie Xub99e6062018-10-16 15:13:09 -0700259 virtual bool LoadWipeDataMenuText();
260
261 // Creates a GraphicMenu with |graphic_header| and |graphic_items|. If the GraphicMenu isn't
262 // valid or it doesn't fit on the screen; falls back to create a TextMenu instead. If succeeds,
263 // returns a unique pointer to the created menu; otherwise returns nullptr.
264 virtual std::unique_ptr<Menu> CreateMenu(GRSurface* graphic_header,
265 const std::vector<GRSurface*>& graphic_items,
266 const std::vector<std::string>& text_headers,
267 const std::vector<std::string>& text_items,
268 size_t initial_selection) const;
269
270 // Creates a TextMenu with |text_headers| and |text_items|; and sets the menu selection to
271 // |initial_selection|.
272 virtual std::unique_ptr<Menu> CreateMenu(const std::vector<std::string>& text_headers,
273 const std::vector<std::string>& text_items,
274 size_t initial_selection) const;
275
276 // Takes the ownership of |menu| and displays it.
277 virtual size_t ShowMenu(std::unique_ptr<Menu>&& menu, bool menu_only,
278 const std::function<int(int, bool)>& key_handler);
Tao Bao3aec6962018-04-20 09:24:58 -0700279
280 // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item
281 // selected.
282 virtual int SelectMenu(int sel);
283
Tao Bao75779652017-09-10 11:28:32 -0700284 virtual void draw_background_locked();
285 virtual void draw_foreground_locked();
286 virtual void draw_screen_locked();
Tao Bao93e46ad2018-05-02 14:57:21 -0700287 virtual void draw_menu_and_text_buffer_locked(const std::vector<std::string>& help_message);
Tao Bao75779652017-09-10 11:28:32 -0700288 virtual void update_screen_locked();
289 virtual void update_progress_locked();
290
291 GRSurface* GetCurrentFrame() const;
292 GRSurface* GetCurrentText() const;
293
Tao Bao75779652017-09-10 11:28:32 -0700294 void ProgressThreadLoop();
295
296 virtual void ShowFile(FILE*);
297 virtual void PrintV(const char*, bool, va_list);
298 void PutChar(char);
299 void ClearText();
300
301 void LoadAnimation();
302 void LoadBitmap(const char* filename, GRSurface** surface);
303 void LoadLocalizedBitmap(const char* filename, GRSurface** surface);
304
305 int PixelsFromDp(int dp) const;
306 virtual int GetAnimationBaseline() const;
307 virtual int GetProgressBaseline() const;
308 virtual int GetTextBaseline() const;
309
Luke Song92eda4d2017-09-19 10:51:35 -0700310 // Returns pixel width of draw buffer.
311 virtual int ScreenWidth() const;
312 // Returns pixel height of draw buffer.
313 virtual int ScreenHeight() const;
314
Tianjie Xu66dbf632018-10-11 16:54:50 -0700315 // Implementation of the draw functions in DrawInterface.
316 void SetColor(UIElement e) const override;
317 void DrawHighlightBar(int x, int y, int width, int height) const override;
318 int DrawHorizontalRule(int y) const override;
Tao Bao65815b62018-10-23 10:54:02 -0700319 void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx,
320 int dy) const override;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700321 void DrawFill(int x, int y, int w, int h) const override;
Tao Bao65815b62018-10-23 10:54:02 -0700322 void DrawTextIcon(int x, int y, const GRSurface* surface) const override;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700323 int DrawTextLine(int x, int y, const std::string& line, bool bold) const override;
324 int DrawTextLines(int x, int y, const std::vector<std::string>& lines) const override;
325 int DrawWrappedTextLines(int x, int y, const std::vector<std::string>& lines) const override;
Tao Bao171b4c42017-06-19 23:10:44 -0700326
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700327 Icon currentIcon;
Doug Zongker211aebc2011-10-28 15:13:10 -0700328
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700329 // The layout to use.
330 int layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700331
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700332 GRSurface* error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700333
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700334 GRSurface* erasing_text;
335 GRSurface* error_text;
336 GRSurface* installing_text;
337 GRSurface* no_command_text;
Elliott Hughes498cda62016-04-14 16:49:04 -0700338
Tianjie Xub99e6062018-10-16 15:13:09 -0700339 // Graphs for the wipe data menu
340 GRSurface* wipe_data_menu_header_text_;
341 GRSurface* try_again_text_;
342 GRSurface* factory_data_reset_text_;
343
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700344 GRSurface** introFrames;
345 GRSurface** loopFrames;
Elliott Hughes498cda62016-04-14 16:49:04 -0700346
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700347 GRSurface* progressBarEmpty;
348 GRSurface* progressBarFill;
349 GRSurface* stageMarkerEmpty;
350 GRSurface* stageMarkerFill;
Doug Zongker211aebc2011-10-28 15:13:10 -0700351
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700352 ProgressType progressBarType;
Doug Zongker211aebc2011-10-28 15:13:10 -0700353
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700354 float progressScopeStart, progressScopeSize, progress;
355 double progressScopeTime, progressScopeDuration;
Doug Zongker211aebc2011-10-28 15:13:10 -0700356
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700357 // true when both graphics pages are the same (except for the progress bar).
358 bool pagesIdentical;
Doug Zongker211aebc2011-10-28 15:13:10 -0700359
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700360 size_t text_cols_, text_rows_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700361
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700362 // Log text overlay, displayed when a magic key is pressed.
363 char** text_;
Tao Baocb5524c2017-09-08 21:25:32 -0700364 size_t text_col_, text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700365
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700366 bool show_text;
367 bool show_text_ever; // has show_text ever been true?
Doug Zongker211aebc2011-10-28 15:13:10 -0700368
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700369 std::vector<std::string> title_lines_;
370
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700371 bool scrollable_menu_;
372 std::unique_ptr<Menu> menu_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700373
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700374 // An alternate text screen, swapped with 'text_' when we're viewing a log file.
375 char** file_viewer_text_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700376
Tao Bao26ea9592018-05-09 16:32:02 -0700377 std::thread progress_thread_;
378 std::atomic<bool> progress_thread_stopped_{ false };
Doug Zongker32a0a472011-11-01 11:00:20 -0700379
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700380 // Number of intro frames and loop frames in the animation.
381 size_t intro_frames;
382 size_t loop_frames;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700383
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700384 size_t current_frame;
385 bool intro_done;
Elliott Hughes498cda62016-04-14 16:49:04 -0700386
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700387 int stage, max_stage;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800388
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700389 int char_width_;
390 int char_height_;
Tao Bao171b4c42017-06-19 23:10:44 -0700391
Tao Baoefb49ad2017-01-31 23:03:10 -0800392 // The locale that's used to show the rendered texts.
393 std::string locale_;
394 bool rtl_locale_;
395
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700396 std::mutex updateMutex;
Tao Baoefb49ad2017-01-31 23:03:10 -0800397
398 private:
399 void SetLocale(const std::string&);
Tianjie Xu29d55752017-09-20 17:53:46 -0700400
401 // Display the background texts for "erasing", "error", "no_command" and "installing" for the
402 // selected locale.
403 void SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries, size_t sel);
Doug Zongker211aebc2011-10-28 15:13:10 -0700404};
405
406#endif // RECOVERY_UI_H