blob: b76d4706ec63eaaaae6bc132dbc674de678f7d96 [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.
32struct GRSurface;
Doug Zongker211aebc2011-10-28 15:13:10 -070033
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070034// This class maintains the menu selection and display of the screen ui.
35class Menu {
36 public:
Tao Baoe02a5b22018-05-02 15:46:11 -070037 // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial
38 // selection to |initial_selection|.
Tao Bao1fe1afe2018-05-01 15:56:05 -070039 Menu(bool scrollable, size_t max_items, size_t max_length,
40 const std::vector<std::string>& headers, const std::vector<std::string>& items,
41 size_t initial_selection);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070042
43 bool scrollable() const {
44 return scrollable_;
45 }
46
Tao Bao1fe1afe2018-05-01 15:56:05 -070047 size_t selection() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070048 return selection_;
49 }
50
51 // Returns count of menu items.
52 size_t ItemsCount() const;
Tao Baoe02a5b22018-05-02 15:46:11 -070053
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070054 // Returns the index of the first menu item.
55 size_t MenuStart() const;
Tao Baoe02a5b22018-05-02 15:46:11 -070056
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070057 // Returns the index of the last menu item + 1.
58 size_t MenuEnd() const;
59
60 // Menu example:
61 // info: Android Recovery
62 // ....
63 // help messages: Swipe up/down to move
64 // Swipe left/right to select
65 // empty line (horizontal rule):
66 // menu headers: Select file to view
67 // menu items: /cache/recovery/last_log
68 // /cache/recovery/last_log.1
69 // /cache/recovery/last_log.2
70 // ...
Tao Bao1fe1afe2018-05-01 15:56:05 -070071 const std::vector<std::string>& text_headers() const;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070072 std::string TextItem(size_t index) const;
73
74 // Checks if the menu items fit vertically on the screen. Returns true and set the
75 // |cur_selection_str| if the items exceed the screen limit.
76 bool ItemsOverflow(std::string* cur_selection_str) const;
77
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070078 // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is
79 // scrollable.
80 int Select(int sel);
81
82 private:
83 // The menu is scrollable to display more items. Used on wear devices who have smaller screens.
84 const bool scrollable_;
85 // The max number of menu items to fit vertically on a screen.
86 const size_t max_display_items_;
87 // The length of each item to fit horizontally on a screen.
88 const size_t max_item_length_;
Tao Bao1fe1afe2018-05-01 15:56:05 -070089 // The menu headers.
90 std::vector<std::string> text_headers_;
91 // The actual menu items trimmed to fit the given properties.
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070092 std::vector<std::string> text_items_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070093 // The first item to display on the screen.
94 size_t menu_start_;
95 // Current menu selection.
Tao Bao1fe1afe2018-05-01 15:56:05 -070096 size_t selection_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070097};
98
Doug Zongker211aebc2011-10-28 15:13:10 -070099// Implementation of RecoveryUI appropriate for devices with a screen
100// (shows an icon + a progress bar, text logging, menu, etc.)
101class ScreenRecoveryUI : public RecoveryUI {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700102 public:
Tao Bao75779652017-09-10 11:28:32 -0700103 enum UIElement {
104 HEADER,
105 MENU,
106 MENU_SEL_BG,
107 MENU_SEL_BG_ACTIVE,
108 MENU_SEL_FG,
109 LOG,
110 TEXT_FILL,
111 INFO
112 };
113
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700114 ScreenRecoveryUI();
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700115 explicit ScreenRecoveryUI(bool scrollable_menu);
Tao Bao26ea9592018-05-09 16:32:02 -0700116 ~ScreenRecoveryUI() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700117
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700118 bool Init(const std::string& locale) override;
Tao Bao551d2c32018-05-09 20:53:13 -0700119 std::string GetLocale() const override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700120
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700121 // overall recovery state ("background image")
Tao Bao99b2d772017-06-23 22:47:03 -0700122 void SetBackground(Icon icon) override;
123 void SetSystemUpdateText(bool security_update) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700124
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700125 // progress indicator
126 void SetProgressType(ProgressType type) override;
127 void ShowProgress(float portion, float seconds) override;
128 void SetProgress(float fraction) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700129
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700130 void SetStage(int current, int max) override;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800131
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700132 // text log
133 void ShowText(bool visible) override;
134 bool IsTextVisible() override;
135 bool WasTextEverVisible() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700136
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700137 // printing messages
Tao Bao99b2d772017-06-23 22:47:03 -0700138 void Print(const char* fmt, ...) override __printflike(2, 3);
139 void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3);
Tao Bao1d156b92018-05-02 12:43:18 -0700140 void ShowFile(const std::string& filename) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700141
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700142 // menu display
Tao Bao1fe1afe2018-05-01 15:56:05 -0700143 size_t ShowMenu(const std::vector<std::string>& headers, const std::vector<std::string>& items,
144 size_t initial_selection, bool menu_only,
145 const std::function<int(int, bool)>& key_handler) override;
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700146 void SetTitle(const std::vector<std::string>& lines) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700147
Tao Bao99b2d772017-06-23 22:47:03 -0700148 void KeyLongPress(int) override;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700149
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700150 void Redraw();
Doug Zongkerc0441d12013-07-31 11:28:24 -0700151
Tao Bao99b2d772017-06-23 22:47:03 -0700152 void SetColor(UIElement e) const;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700153
Tao Bao39c49182018-05-07 22:50:33 -0700154 // Checks the background text image, for debugging purpose. It iterates the locales embedded in
155 // the on-device resource files and shows the localized text, for manual inspection.
156 void CheckBackgroundTextImages();
Tianjie Xu29d55752017-09-20 17:53:46 -0700157
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700158 protected:
159 // The margin that we don't want to use for showing texts (e.g. round screen, or screen with
160 // rounded corners).
161 const int kMarginWidth;
162 const int kMarginHeight;
Tao Bao4521b702017-06-20 18:11:21 -0700163
Tao Bao0470cee2017-08-02 17:11:04 -0700164 // Number of frames per sec (default: 30) for both parts of the animation.
165 const int kAnimationFps;
166
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700167 // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi.
Tao Bao75779652017-09-10 11:28:32 -0700168 const float kDensity;
169
170 virtual bool InitTextParams();
171
Tao Bao3aec6962018-04-20 09:24:58 -0700172 // Displays some header text followed by a menu of items, which appears at the top of the screen
173 // (in place of any scrolling ui_print() output, if necessary).
Tao Bao1fe1afe2018-05-01 15:56:05 -0700174 virtual void StartMenu(const std::vector<std::string>& headers,
175 const std::vector<std::string>& items, size_t initial_selection);
Tao Bao3aec6962018-04-20 09:24:58 -0700176
177 // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item
178 // selected.
179 virtual int SelectMenu(int sel);
180
181 // Ends menu mode, resetting the text overlay so that ui_print() statements will be displayed.
182 virtual void EndMenu();
183
Tao Bao75779652017-09-10 11:28:32 -0700184 virtual void draw_background_locked();
185 virtual void draw_foreground_locked();
186 virtual void draw_screen_locked();
Tao Bao93e46ad2018-05-02 14:57:21 -0700187 virtual void draw_menu_and_text_buffer_locked(const std::vector<std::string>& help_message);
Tao Bao75779652017-09-10 11:28:32 -0700188 virtual void update_screen_locked();
189 virtual void update_progress_locked();
190
191 GRSurface* GetCurrentFrame() const;
192 GRSurface* GetCurrentText() const;
193
Tao Bao75779652017-09-10 11:28:32 -0700194 void ProgressThreadLoop();
195
196 virtual void ShowFile(FILE*);
197 virtual void PrintV(const char*, bool, va_list);
198 void PutChar(char);
199 void ClearText();
200
201 void LoadAnimation();
202 void LoadBitmap(const char* filename, GRSurface** surface);
203 void LoadLocalizedBitmap(const char* filename, GRSurface** surface);
204
205 int PixelsFromDp(int dp) const;
206 virtual int GetAnimationBaseline() const;
207 virtual int GetProgressBaseline() const;
208 virtual int GetTextBaseline() const;
209
Luke Song92eda4d2017-09-19 10:51:35 -0700210 // Returns pixel width of draw buffer.
211 virtual int ScreenWidth() const;
212 // Returns pixel height of draw buffer.
213 virtual int ScreenHeight() const;
214
Tao Bao75779652017-09-10 11:28:32 -0700215 // Draws a highlight bar at (x, y) - (x + width, y + height).
216 virtual void DrawHighlightBar(int x, int y, int width, int height) const;
217 // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis.
218 virtual int DrawHorizontalRule(int y) const;
219 // Draws a line of text. Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700220 virtual int DrawTextLine(int x, int y, const std::string& line, bool bold) const;
Luke Song92eda4d2017-09-19 10:51:35 -0700221 // Draws surface portion (sx, sy, w, h) at screen location (dx, dy).
222 virtual void DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, int dy) const;
223 // Draws rectangle at (x, y) - (x + w, y + h).
224 virtual void DrawFill(int x, int y, int w, int h) const;
225 // Draws given surface (surface->pixel_bytes = 1) as text at (x, y).
226 virtual void DrawTextIcon(int x, int y, GRSurface* surface) const;
Tao Bao75779652017-09-10 11:28:32 -0700227 // Draws multiple text lines. Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700228 int DrawTextLines(int x, int y, const std::vector<std::string>& lines) const;
Tao Bao452b4872018-05-09 11:52:09 -0700229 // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines. It
230 // keeps symmetrical margins of 'x' at each end of a line. Returns the offset it should be moving
231 // along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700232 int DrawWrappedTextLines(int x, int y, const std::vector<std::string>& lines) const;
Tao Bao171b4c42017-06-19 23:10:44 -0700233
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700234 Icon currentIcon;
Doug Zongker211aebc2011-10-28 15:13:10 -0700235
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700236 // The layout to use.
237 int layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700238
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700239 GRSurface* error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700240
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700241 GRSurface* erasing_text;
242 GRSurface* error_text;
243 GRSurface* installing_text;
244 GRSurface* no_command_text;
Elliott Hughes498cda62016-04-14 16:49:04 -0700245
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700246 GRSurface** introFrames;
247 GRSurface** loopFrames;
Elliott Hughes498cda62016-04-14 16:49:04 -0700248
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700249 GRSurface* progressBarEmpty;
250 GRSurface* progressBarFill;
251 GRSurface* stageMarkerEmpty;
252 GRSurface* stageMarkerFill;
Doug Zongker211aebc2011-10-28 15:13:10 -0700253
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700254 ProgressType progressBarType;
Doug Zongker211aebc2011-10-28 15:13:10 -0700255
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700256 float progressScopeStart, progressScopeSize, progress;
257 double progressScopeTime, progressScopeDuration;
Doug Zongker211aebc2011-10-28 15:13:10 -0700258
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700259 // true when both graphics pages are the same (except for the progress bar).
260 bool pagesIdentical;
Doug Zongker211aebc2011-10-28 15:13:10 -0700261
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700262 size_t text_cols_, text_rows_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700263
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700264 // Log text overlay, displayed when a magic key is pressed.
265 char** text_;
Tao Baocb5524c2017-09-08 21:25:32 -0700266 size_t text_col_, text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700267
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700268 bool show_text;
269 bool show_text_ever; // has show_text ever been true?
Doug Zongker211aebc2011-10-28 15:13:10 -0700270
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700271 std::vector<std::string> title_lines_;
272
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700273 bool scrollable_menu_;
274 std::unique_ptr<Menu> menu_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700275
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700276 // An alternate text screen, swapped with 'text_' when we're viewing a log file.
277 char** file_viewer_text_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700278
Tao Bao26ea9592018-05-09 16:32:02 -0700279 std::thread progress_thread_;
280 std::atomic<bool> progress_thread_stopped_{ false };
Doug Zongker32a0a472011-11-01 11:00:20 -0700281
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700282 // Number of intro frames and loop frames in the animation.
283 size_t intro_frames;
284 size_t loop_frames;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700285
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700286 size_t current_frame;
287 bool intro_done;
Elliott Hughes498cda62016-04-14 16:49:04 -0700288
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700289 int stage, max_stage;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800290
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700291 int char_width_;
292 int char_height_;
Tao Bao171b4c42017-06-19 23:10:44 -0700293
Tao Baoefb49ad2017-01-31 23:03:10 -0800294 // The locale that's used to show the rendered texts.
295 std::string locale_;
296 bool rtl_locale_;
297
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700298 std::mutex updateMutex;
Tao Baoefb49ad2017-01-31 23:03:10 -0800299
300 private:
301 void SetLocale(const std::string&);
Tianjie Xu29d55752017-09-20 17:53:46 -0700302
303 // Display the background texts for "erasing", "error", "no_command" and "installing" for the
304 // selected locale.
305 void SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries, size_t sel);
Doug Zongker211aebc2011-10-28 15:13:10 -0700306};
307
308#endif // RECOVERY_UI_H