blob: fb811ce701a966a27e249a68ba41124c467ce6df [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 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 Baoe15d7a52017-09-07 13:38:51 -070026#include <vector>
Tao Bao736d59c2017-01-03 10:15:33 -080027
Doug Zongker211aebc2011-10-28 15:13:10 -070028#include "ui.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080029
30// From minui/minui.h.
31struct GRSurface;
Doug Zongker211aebc2011-10-28 15:13:10 -070032
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070033// This class maintains the menu selection and display of the screen ui.
34class Menu {
35 public:
Tao Baoe02a5b22018-05-02 15:46:11 -070036 // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial
37 // selection to |initial_selection|.
Tao Bao1fe1afe2018-05-01 15:56:05 -070038 Menu(bool scrollable, size_t max_items, size_t max_length,
39 const std::vector<std::string>& headers, const std::vector<std::string>& items,
40 size_t initial_selection);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070041
42 bool scrollable() const {
43 return scrollable_;
44 }
45
Tao Bao1fe1afe2018-05-01 15:56:05 -070046 size_t selection() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070047 return selection_;
48 }
49
50 // Returns count of menu items.
51 size_t ItemsCount() const;
Tao Baoe02a5b22018-05-02 15:46:11 -070052
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070053 // Returns the index of the first menu item.
54 size_t MenuStart() const;
Tao Baoe02a5b22018-05-02 15:46:11 -070055
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070056 // Returns the index of the last menu item + 1.
57 size_t MenuEnd() const;
58
59 // Menu example:
60 // info: Android Recovery
61 // ....
62 // help messages: Swipe up/down to move
63 // Swipe left/right to select
64 // empty line (horizontal rule):
65 // menu headers: Select file to view
66 // menu items: /cache/recovery/last_log
67 // /cache/recovery/last_log.1
68 // /cache/recovery/last_log.2
69 // ...
Tao Bao1fe1afe2018-05-01 15:56:05 -070070 const std::vector<std::string>& text_headers() const;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070071 std::string TextItem(size_t index) const;
72
73 // Checks if the menu items fit vertically on the screen. Returns true and set the
74 // |cur_selection_str| if the items exceed the screen limit.
75 bool ItemsOverflow(std::string* cur_selection_str) const;
76
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070077 // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is
78 // scrollable.
79 int Select(int sel);
80
81 private:
82 // The menu is scrollable to display more items. Used on wear devices who have smaller screens.
83 const bool scrollable_;
84 // The max number of menu items to fit vertically on a screen.
85 const size_t max_display_items_;
86 // The length of each item to fit horizontally on a screen.
87 const size_t max_item_length_;
Tao Bao1fe1afe2018-05-01 15:56:05 -070088 // The menu headers.
89 std::vector<std::string> text_headers_;
90 // The actual menu items trimmed to fit the given properties.
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070091 std::vector<std::string> text_items_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070092 // The first item to display on the screen.
93 size_t menu_start_;
94 // Current menu selection.
Tao Bao1fe1afe2018-05-01 15:56:05 -070095 size_t selection_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070096};
97
Doug Zongker211aebc2011-10-28 15:13:10 -070098// Implementation of RecoveryUI appropriate for devices with a screen
99// (shows an icon + a progress bar, text logging, menu, etc.)
100class ScreenRecoveryUI : public RecoveryUI {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700101 public:
Tao Bao75779652017-09-10 11:28:32 -0700102 enum UIElement {
103 HEADER,
104 MENU,
105 MENU_SEL_BG,
106 MENU_SEL_BG_ACTIVE,
107 MENU_SEL_FG,
108 LOG,
109 TEXT_FILL,
110 INFO
111 };
112
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700113 ScreenRecoveryUI();
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700114 explicit ScreenRecoveryUI(bool scrollable_menu);
Doug Zongker211aebc2011-10-28 15:13:10 -0700115
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700116 bool Init(const std::string& locale) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700117
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700118 // overall recovery state ("background image")
Tao Bao99b2d772017-06-23 22:47:03 -0700119 void SetBackground(Icon icon) override;
120 void SetSystemUpdateText(bool security_update) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700121
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700122 // progress indicator
123 void SetProgressType(ProgressType type) override;
124 void ShowProgress(float portion, float seconds) override;
125 void SetProgress(float fraction) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700126
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700127 void SetStage(int current, int max) override;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800128
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700129 // text log
130 void ShowText(bool visible) override;
131 bool IsTextVisible() override;
132 bool WasTextEverVisible() override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700133
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700134 // printing messages
Tao Bao99b2d772017-06-23 22:47:03 -0700135 void Print(const char* fmt, ...) override __printflike(2, 3);
136 void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3);
Tao Bao1d156b92018-05-02 12:43:18 -0700137 void ShowFile(const std::string& filename) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700138
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700139 // menu display
Tao Bao1fe1afe2018-05-01 15:56:05 -0700140 size_t ShowMenu(const std::vector<std::string>& headers, const std::vector<std::string>& items,
141 size_t initial_selection, bool menu_only,
142 const std::function<int(int, bool)>& key_handler) override;
Doug Zongker211aebc2011-10-28 15:13:10 -0700143
Tao Bao99b2d772017-06-23 22:47:03 -0700144 void KeyLongPress(int) override;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700145
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700146 void Redraw();
Doug Zongkerc0441d12013-07-31 11:28:24 -0700147
Tao Bao99b2d772017-06-23 22:47:03 -0700148 void SetColor(UIElement e) const;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700149
Tianjie Xu29d55752017-09-20 17:53:46 -0700150 // Check the background text image. Use volume up/down button to cycle through the locales
151 // embedded in the png file, and power button to go back to recovery main menu.
152 void CheckBackgroundTextImages(const std::string& saved_locale);
153
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700154 protected:
155 // The margin that we don't want to use for showing texts (e.g. round screen, or screen with
156 // rounded corners).
157 const int kMarginWidth;
158 const int kMarginHeight;
Tao Bao4521b702017-06-20 18:11:21 -0700159
Tao Bao0470cee2017-08-02 17:11:04 -0700160 // Number of frames per sec (default: 30) for both parts of the animation.
161 const int kAnimationFps;
162
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700163 // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi.
Tao Bao75779652017-09-10 11:28:32 -0700164 const float kDensity;
165
166 virtual bool InitTextParams();
167
Tao Bao3aec6962018-04-20 09:24:58 -0700168 // Displays some header text followed by a menu of items, which appears at the top of the screen
169 // (in place of any scrolling ui_print() output, if necessary).
Tao Bao1fe1afe2018-05-01 15:56:05 -0700170 virtual void StartMenu(const std::vector<std::string>& headers,
171 const std::vector<std::string>& items, size_t initial_selection);
Tao Bao3aec6962018-04-20 09:24:58 -0700172
173 // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item
174 // selected.
175 virtual int SelectMenu(int sel);
176
177 // Ends menu mode, resetting the text overlay so that ui_print() statements will be displayed.
178 virtual void EndMenu();
179
Tao Bao75779652017-09-10 11:28:32 -0700180 virtual void draw_background_locked();
181 virtual void draw_foreground_locked();
182 virtual void draw_screen_locked();
Tao Bao93e46ad2018-05-02 14:57:21 -0700183 virtual void draw_menu_and_text_buffer_locked(const std::vector<std::string>& help_message);
Tao Bao75779652017-09-10 11:28:32 -0700184 virtual void update_screen_locked();
185 virtual void update_progress_locked();
186
187 GRSurface* GetCurrentFrame() const;
188 GRSurface* GetCurrentText() const;
189
190 static void* ProgressThreadStartRoutine(void* data);
191 void ProgressThreadLoop();
192
193 virtual void ShowFile(FILE*);
194 virtual void PrintV(const char*, bool, va_list);
195 void PutChar(char);
196 void ClearText();
197
198 void LoadAnimation();
199 void LoadBitmap(const char* filename, GRSurface** surface);
200 void LoadLocalizedBitmap(const char* filename, GRSurface** surface);
201
202 int PixelsFromDp(int dp) const;
203 virtual int GetAnimationBaseline() const;
204 virtual int GetProgressBaseline() const;
205 virtual int GetTextBaseline() const;
206
Luke Song92eda4d2017-09-19 10:51:35 -0700207 // Returns pixel width of draw buffer.
208 virtual int ScreenWidth() const;
209 // Returns pixel height of draw buffer.
210 virtual int ScreenHeight() const;
211
Tao Bao75779652017-09-10 11:28:32 -0700212 // Draws a highlight bar at (x, y) - (x + width, y + height).
213 virtual void DrawHighlightBar(int x, int y, int width, int height) const;
214 // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis.
215 virtual int DrawHorizontalRule(int y) const;
216 // Draws a line of text. Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700217 virtual int DrawTextLine(int x, int y, const std::string& line, bool bold) const;
Luke Song92eda4d2017-09-19 10:51:35 -0700218 // Draws surface portion (sx, sy, w, h) at screen location (dx, dy).
219 virtual void DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, int dy) const;
220 // Draws rectangle at (x, y) - (x + w, y + h).
221 virtual void DrawFill(int x, int y, int w, int h) const;
222 // Draws given surface (surface->pixel_bytes = 1) as text at (x, y).
223 virtual void DrawTextIcon(int x, int y, GRSurface* surface) const;
Tao Bao75779652017-09-10 11:28:32 -0700224 // Draws multiple text lines. Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700225 int DrawTextLines(int x, int y, const std::vector<std::string>& lines) const;
Tao Bao75779652017-09-10 11:28:32 -0700226 // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines.
227 // Returns the offset it should be moving along Y-axis.
Tao Bao93e46ad2018-05-02 14:57:21 -0700228 int DrawWrappedTextLines(int x, int y, const std::vector<std::string>& lines) const;
Tao Bao171b4c42017-06-19 23:10:44 -0700229
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700230 Icon currentIcon;
Doug Zongker211aebc2011-10-28 15:13:10 -0700231
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700232 // The layout to use.
233 int layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700234
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700235 GRSurface* error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700236
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700237 GRSurface* erasing_text;
238 GRSurface* error_text;
239 GRSurface* installing_text;
240 GRSurface* no_command_text;
Elliott Hughes498cda62016-04-14 16:49:04 -0700241
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700242 GRSurface** introFrames;
243 GRSurface** loopFrames;
Elliott Hughes498cda62016-04-14 16:49:04 -0700244
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700245 GRSurface* progressBarEmpty;
246 GRSurface* progressBarFill;
247 GRSurface* stageMarkerEmpty;
248 GRSurface* stageMarkerFill;
Doug Zongker211aebc2011-10-28 15:13:10 -0700249
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700250 ProgressType progressBarType;
Doug Zongker211aebc2011-10-28 15:13:10 -0700251
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700252 float progressScopeStart, progressScopeSize, progress;
253 double progressScopeTime, progressScopeDuration;
Doug Zongker211aebc2011-10-28 15:13:10 -0700254
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700255 // true when both graphics pages are the same (except for the progress bar).
256 bool pagesIdentical;
Doug Zongker211aebc2011-10-28 15:13:10 -0700257
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700258 size_t text_cols_, text_rows_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700259
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700260 // Log text overlay, displayed when a magic key is pressed.
261 char** text_;
Tao Baocb5524c2017-09-08 21:25:32 -0700262 size_t text_col_, text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700263
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700264 bool show_text;
265 bool show_text_ever; // has show_text ever been true?
Doug Zongker211aebc2011-10-28 15:13:10 -0700266
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700267 bool scrollable_menu_;
268 std::unique_ptr<Menu> menu_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700269
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700270 // An alternate text screen, swapped with 'text_' when we're viewing a log file.
271 char** file_viewer_text_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700272
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700273 pthread_t progress_thread_;
Doug Zongker32a0a472011-11-01 11:00:20 -0700274
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700275 // Number of intro frames and loop frames in the animation.
276 size_t intro_frames;
277 size_t loop_frames;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700278
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700279 size_t current_frame;
280 bool intro_done;
Elliott Hughes498cda62016-04-14 16:49:04 -0700281
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700282 int stage, max_stage;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800283
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700284 int char_width_;
285 int char_height_;
Tao Bao171b4c42017-06-19 23:10:44 -0700286
Tao Baoefb49ad2017-01-31 23:03:10 -0800287 // The locale that's used to show the rendered texts.
288 std::string locale_;
289 bool rtl_locale_;
290
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700291 pthread_mutex_t updateMutex;
Tao Baoefb49ad2017-01-31 23:03:10 -0800292
293 private:
294 void SetLocale(const std::string&);
Tianjie Xu29d55752017-09-20 17:53:46 -0700295
296 // Display the background texts for "erasing", "error", "no_command" and "installing" for the
297 // selected locale.
298 void SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries, size_t sel);
Doug Zongker211aebc2011-10-28 15:13:10 -0700299};
300
301#endif // RECOVERY_UI_H