blob: c3605161a713c654e6d8721955e2ea86dcc854c1 [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
20#include <pthread.h>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070021#include <stdio.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070022
Tao Bao26ea9592018-05-09 16:32:02 -070023#include <atomic>
Tao Bao3aec6962018-04-20 09:24:58 -070024#include <functional>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070025#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080026#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070027#include <thread>
Tao Baoe15d7a52017-09-07 13:38:51 -070028#include <vector>
Tao Bao736d59c2017-01-03 10:15:33 -080029
Doug Zongker211aebc2011-10-28 15:13:10 -070030#include "ui.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080031
32// From minui/minui.h.
33struct GRSurface;
Doug Zongker211aebc2011-10-28 15:13:10 -070034
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070035// This class maintains the menu selection and display of the screen ui.
36class Menu {
37 public:
Tao Baoe02a5b22018-05-02 15:46:11 -070038 // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial
39 // selection to |initial_selection|.
Tao Bao1fe1afe2018-05-01 15:56:05 -070040 Menu(bool scrollable, size_t max_items, size_t max_length,
41 const std::vector<std::string>& headers, const std::vector<std::string>& items,
42 size_t initial_selection);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070043
44 bool scrollable() const {
45 return scrollable_;
46 }
47
Tao Bao1fe1afe2018-05-01 15:56:05 -070048 size_t selection() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070049 return selection_;
50 }
51
52 // Returns count of menu items.
53 size_t ItemsCount() const;
Tao Baoe02a5b22018-05-02 15:46:11 -070054
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070055 // Returns the index of the first menu item.
56 size_t MenuStart() const;
Tao Baoe02a5b22018-05-02 15:46:11 -070057
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070058 // Returns the index of the last menu item + 1.
59 size_t MenuEnd() const;
60
61 // Menu example:
62 // info: Android Recovery
63 // ....
64 // help messages: Swipe up/down to move
65 // Swipe left/right to select
66 // empty line (horizontal rule):
67 // menu headers: Select file to view
68 // menu items: /cache/recovery/last_log
69 // /cache/recovery/last_log.1
70 // /cache/recovery/last_log.2
71 // ...
Tao Bao1fe1afe2018-05-01 15:56:05 -070072 const std::vector<std::string>& text_headers() const;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070073 std::string TextItem(size_t index) const;
74
75 // Checks if the menu items fit vertically on the screen. Returns true and set the
76 // |cur_selection_str| if the items exceed the screen limit.
77 bool ItemsOverflow(std::string* cur_selection_str) const;
78
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070079 // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is
80 // scrollable.
81 int Select(int sel);
82
83 private:
84 // The menu is scrollable to display more items. Used on wear devices who have smaller screens.
85 const bool scrollable_;
86 // The max number of menu items to fit vertically on a screen.
87 const size_t max_display_items_;
88 // The length of each item to fit horizontally on a screen.
89 const size_t max_item_length_;
Tao Bao1fe1afe2018-05-01 15:56:05 -070090 // The menu headers.
91 std::vector<std::string> text_headers_;
92 // The actual menu items trimmed to fit the given properties.
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070093 std::vector<std::string> text_items_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070094 // The first item to display on the screen.
95 size_t menu_start_;
96 // Current menu selection.
Tao Bao1fe1afe2018-05-01 15:56:05 -070097 size_t selection_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070098};
99
Doug Zongker211aebc2011-10-28 15:13:10 -0700100// Implementation of RecoveryUI appropriate for devices with a screen
101// (shows an icon + a progress bar, text logging, menu, etc.)
102class ScreenRecoveryUI : public RecoveryUI {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700103 public:
Tao Bao75779652017-09-10 11:28:32 -0700104 enum UIElement {
105 HEADER,
106 MENU,
107 MENU_SEL_BG,
108 MENU_SEL_BG_ACTIVE,
109 MENU_SEL_FG,
110 LOG,
111 TEXT_FILL,
112 INFO
113 };
114
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700115 ScreenRecoveryUI();
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700116 explicit ScreenRecoveryUI(bool scrollable_menu);
Tao Bao26ea9592018-05-09 16:32:02 -0700117 ~ScreenRecoveryUI() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700118
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700119 bool Init(const std::string& locale) override;
Tao Bao551d2c32018-05-09 20:53:13 -0700120 std::string GetLocale() const override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700121
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700122 // overall recovery state ("background image")
Tao Bao99b2d772017-06-23 22:47:03 -0700123 void SetBackground(Icon icon) override;
124 void SetSystemUpdateText(bool security_update) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700125
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700126 // progress indicator
127 void SetProgressType(ProgressType type) override;
128 void ShowProgress(float portion, float seconds) override;
129 void SetProgress(float fraction) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700130
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700131 void SetStage(int current, int max) override;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800132
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700133 // text log
134 void ShowText(bool visible) override;
135 bool IsTextVisible() override;
136 bool WasTextEverVisible() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700137
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700138 // printing messages
Tao Bao99b2d772017-06-23 22:47:03 -0700139 void Print(const char* fmt, ...) override __printflike(2, 3);
140 void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3);
Tao Bao1d156b92018-05-02 12:43:18 -0700141 void ShowFile(const std::string& filename) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700142
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700143 // menu display
Tao Bao1fe1afe2018-05-01 15:56:05 -0700144 size_t ShowMenu(const std::vector<std::string>& headers, const std::vector<std::string>& items,
145 size_t initial_selection, bool menu_only,
146 const std::function<int(int, bool)>& key_handler) override;
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700147 void SetTitle(const std::vector<std::string>& lines) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700148
Tao Bao99b2d772017-06-23 22:47:03 -0700149 void KeyLongPress(int) override;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700150
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700151 void Redraw();
Doug Zongkerc0441d12013-07-31 11:28:24 -0700152
Tao Bao99b2d772017-06-23 22:47:03 -0700153 void SetColor(UIElement e) const;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700154
Tao Bao39c49182018-05-07 22:50:33 -0700155 // Checks the background text image, for debugging purpose. It iterates the locales embedded in
156 // the on-device resource files and shows the localized text, for manual inspection.
157 void CheckBackgroundTextImages();
Tianjie Xu29d55752017-09-20 17:53:46 -0700158
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700159 protected:
160 // The margin that we don't want to use for showing texts (e.g. round screen, or screen with
161 // rounded corners).
162 const int kMarginWidth;
163 const int kMarginHeight;
Tao Bao4521b702017-06-20 18:11:21 -0700164
Tao Bao0470cee2017-08-02 17:11:04 -0700165 // Number of frames per sec (default: 30) for both parts of the animation.
166 const int kAnimationFps;
167
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700168 // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi.
Tao Bao75779652017-09-10 11:28:32 -0700169 const float kDensity;
170
171 virtual bool InitTextParams();
172
Tao Bao3aec6962018-04-20 09:24:58 -0700173 // Displays some header text followed by a menu of items, which appears at the top of the screen
174 // (in place of any scrolling ui_print() output, if necessary).
Tao Bao1fe1afe2018-05-01 15:56:05 -0700175 virtual void StartMenu(const std::vector<std::string>& headers,
176 const std::vector<std::string>& items, size_t initial_selection);
Tao Bao3aec6962018-04-20 09:24:58 -0700177
178 // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item
179 // selected.
180 virtual int SelectMenu(int sel);
181
182 // Ends menu mode, resetting the text overlay so that ui_print() statements will be displayed.
183 virtual void EndMenu();
184
Tao Bao75779652017-09-10 11:28:32 -0700185 virtual void draw_background_locked();
186 virtual void draw_foreground_locked();
187 virtual void draw_screen_locked();
Tao Bao93e46ad2018-05-02 14:57:21 -0700188 virtual void draw_menu_and_text_buffer_locked(const std::vector<std::string>& help_message);
Tao Bao75779652017-09-10 11:28:32 -0700189 virtual void update_screen_locked();
190 virtual void update_progress_locked();
191
192 GRSurface* GetCurrentFrame() const;
193 GRSurface* GetCurrentText() const;
194
195 static void* ProgressThreadStartRoutine(void* data);
196 void ProgressThreadLoop();
197
198 virtual void ShowFile(FILE*);
199 virtual void PrintV(const char*, bool, va_list);
200 void PutChar(char);
201 void ClearText();
202
203 void LoadAnimation();
204 void LoadBitmap(const char* filename, GRSurface** surface);
205 void LoadLocalizedBitmap(const char* filename, GRSurface** surface);
206
207 int PixelsFromDp(int dp) const;
208 virtual int GetAnimationBaseline() const;
209 virtual int GetProgressBaseline() const;
210 virtual int GetTextBaseline() const;
211
Luke Song92eda4d2017-09-19 10:51:35 -0700212 // Returns pixel width of draw buffer.
213 virtual int ScreenWidth() const;
214 // Returns pixel height of draw buffer.
215 virtual int ScreenHeight() const;
216
Tao Bao75779652017-09-10 11:28:32 -0700217 // Draws a highlight bar at (x, y) - (x + width, y + height).
218 virtual void DrawHighlightBar(int x, int y, int width, int height) const;
219 // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis.
220 virtual int DrawHorizontalRule(int y) const;
221 // Draws a line of text. Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700222 virtual int DrawTextLine(int x, int y, const std::string& line, bool bold) const;
Luke Song92eda4d2017-09-19 10:51:35 -0700223 // Draws surface portion (sx, sy, w, h) at screen location (dx, dy).
224 virtual void DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, int dy) const;
225 // Draws rectangle at (x, y) - (x + w, y + h).
226 virtual void DrawFill(int x, int y, int w, int h) const;
227 // Draws given surface (surface->pixel_bytes = 1) as text at (x, y).
228 virtual void DrawTextIcon(int x, int y, GRSurface* surface) const;
Tao Bao75779652017-09-10 11:28:32 -0700229 // Draws multiple text lines. Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700230 int DrawTextLines(int x, int y, const std::vector<std::string>& lines) const;
Tao Bao452b4872018-05-09 11:52:09 -0700231 // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines. It
232 // keeps symmetrical margins of 'x' at each end of a line. Returns the offset it should be moving
233 // along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700234 int DrawWrappedTextLines(int x, int y, const std::vector<std::string>& lines) const;
Tao Bao171b4c42017-06-19 23:10:44 -0700235
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700236 Icon currentIcon;
Doug Zongker211aebc2011-10-28 15:13:10 -0700237
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700238 // The layout to use.
239 int layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700240
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700241 GRSurface* error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700242
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700243 GRSurface* erasing_text;
244 GRSurface* error_text;
245 GRSurface* installing_text;
246 GRSurface* no_command_text;
Elliott Hughes498cda62016-04-14 16:49:04 -0700247
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700248 GRSurface** introFrames;
249 GRSurface** loopFrames;
Elliott Hughes498cda62016-04-14 16:49:04 -0700250
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700251 GRSurface* progressBarEmpty;
252 GRSurface* progressBarFill;
253 GRSurface* stageMarkerEmpty;
254 GRSurface* stageMarkerFill;
Doug Zongker211aebc2011-10-28 15:13:10 -0700255
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700256 ProgressType progressBarType;
Doug Zongker211aebc2011-10-28 15:13:10 -0700257
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700258 float progressScopeStart, progressScopeSize, progress;
259 double progressScopeTime, progressScopeDuration;
Doug Zongker211aebc2011-10-28 15:13:10 -0700260
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700261 // true when both graphics pages are the same (except for the progress bar).
262 bool pagesIdentical;
Doug Zongker211aebc2011-10-28 15:13:10 -0700263
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700264 size_t text_cols_, text_rows_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700265
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700266 // Log text overlay, displayed when a magic key is pressed.
267 char** text_;
Tao Baocb5524c2017-09-08 21:25:32 -0700268 size_t text_col_, text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700269
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700270 bool show_text;
271 bool show_text_ever; // has show_text ever been true?
Doug Zongker211aebc2011-10-28 15:13:10 -0700272
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700273 std::vector<std::string> title_lines_;
274
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700275 bool scrollable_menu_;
276 std::unique_ptr<Menu> menu_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700277
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700278 // An alternate text screen, swapped with 'text_' when we're viewing a log file.
279 char** file_viewer_text_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700280
Tao Bao26ea9592018-05-09 16:32:02 -0700281 std::thread progress_thread_;
282 std::atomic<bool> progress_thread_stopped_{ false };
Doug Zongker32a0a472011-11-01 11:00:20 -0700283
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700284 // Number of intro frames and loop frames in the animation.
285 size_t intro_frames;
286 size_t loop_frames;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700287
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700288 size_t current_frame;
289 bool intro_done;
Elliott Hughes498cda62016-04-14 16:49:04 -0700290
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700291 int stage, max_stage;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800292
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700293 int char_width_;
294 int char_height_;
Tao Bao171b4c42017-06-19 23:10:44 -0700295
Tao Baoefb49ad2017-01-31 23:03:10 -0800296 // The locale that's used to show the rendered texts.
297 std::string locale_;
298 bool rtl_locale_;
299
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700300 pthread_mutex_t updateMutex;
Tao Baoefb49ad2017-01-31 23:03:10 -0800301
302 private:
303 void SetLocale(const std::string&);
Tianjie Xu29d55752017-09-20 17:53:46 -0700304
305 // Display the background texts for "erasing", "error", "no_command" and "installing" for the
306 // selected locale.
307 void SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries, size_t sel);
Doug Zongker211aebc2011-10-28 15:13:10 -0700308};
309
310#endif // RECOVERY_UI_H