blob: 33aad03dc0b249f418cecf69052b73faaae4ea80 [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
Tianjie Xub5108c32018-08-20 13:40:47 -070017#include "recovery_ui/screen_ui.h"
Tao Baoefb49ad2017-01-31 23:03:10 -080018
Elliott Hughes498cda62016-04-14 16:49:04 -070019#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070020#include <errno.h>
21#include <fcntl.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070022#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
31
Tao Bao1fe1afe2018-05-01 15:56:05 -070032#include <algorithm>
Tao Bao26ea9592018-05-09 16:32:02 -070033#include <chrono>
Tianjie Xu29d55752017-09-20 17:53:46 -070034#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080035#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070036#include <thread>
Tianjie Xu29d55752017-09-20 17:53:46 -070037#include <unordered_map>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070038#include <vector>
39
Tianjie Xuc21edd42016-08-05 18:00:04 -070040#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070041#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070043#include <android-base/strings.h>
Tao Baob6918c72015-05-19 17:02:16 -070044
Tao Bao6cd81682018-05-03 21:53:11 -070045#include "minui/minui.h"
46#include "otautil/paths.h"
Tianjie Xub5108c32018-08-20 13:40:47 -070047#include "recovery_ui/device.h"
48#include "recovery_ui/ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070049
Doug Zongker211aebc2011-10-28 15:13:10 -070050// Return the current time as a double (including fractions of a second).
51static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070052 struct timeval tv;
53 gettimeofday(&tv, nullptr);
54 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070055}
56
Tianjie Xu66dbf632018-10-11 16:54:50 -070057Menu::Menu(size_t initial_selection, const DrawInterface& draw_func)
58 : selection_(initial_selection), draw_funcs_(draw_func) {}
59
60size_t Menu::selection() const {
61 return selection_;
62}
63
64TextMenu::TextMenu(bool scrollable, size_t max_items, size_t max_length,
65 const std::vector<std::string>& headers, const std::vector<std::string>& items,
66 size_t initial_selection, int char_height, const DrawInterface& draw_funcs)
67 : Menu(initial_selection, draw_funcs),
68 scrollable_(scrollable),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070069 max_display_items_(max_items),
70 max_item_length_(max_length),
Tao Baoe02a5b22018-05-02 15:46:11 -070071 text_headers_(headers),
Tianjie Xu66dbf632018-10-11 16:54:50 -070072 char_height_(char_height) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070073 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070074
75 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
Tao Bao1fe1afe2018-05-01 15:56:05 -070076 size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_);
77 for (size_t i = 0; i < items_count; ++i) {
78 text_items_.emplace_back(items[i].substr(0, max_item_length_));
Tao Baoe02a5b22018-05-02 15:46:11 -070079 }
Alessandro Astonec6ddbac2020-03-09 23:17:50 +010080 menu_start_ = std::max(0, (int)selection_ - (int)max_display_items_ + 1);
Tao Baoe02a5b22018-05-02 15:46:11 -070081
82 CHECK(!text_items_.empty());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070083}
84
Tianjie Xu66dbf632018-10-11 16:54:50 -070085const std::vector<std::string>& TextMenu::text_headers() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070086 return text_headers_;
87}
88
Tianjie Xu66dbf632018-10-11 16:54:50 -070089std::string TextMenu::TextItem(size_t index) const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070090 CHECK_LT(index, text_items_.size());
91
92 return text_items_[index];
93}
94
Tianjie Xu66dbf632018-10-11 16:54:50 -070095size_t TextMenu::MenuStart() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070096 return menu_start_;
97}
98
Tianjie Xu66dbf632018-10-11 16:54:50 -070099size_t TextMenu::MenuEnd() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700100 return std::min(ItemsCount(), menu_start_ + max_display_items_);
101}
102
Tianjie Xu66dbf632018-10-11 16:54:50 -0700103size_t TextMenu::ItemsCount() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700104 return text_items_.size();
105}
106
Tianjie Xu66dbf632018-10-11 16:54:50 -0700107bool TextMenu::ItemsOverflow(std::string* cur_selection_str) const {
Tao Baoe02a5b22018-05-02 15:46:11 -0700108 if (!scrollable_ || ItemsCount() <= max_display_items_) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700109 return false;
110 }
111
112 *cur_selection_str =
Tao Bao1fe1afe2018-05-01 15:56:05 -0700113 android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700114 return true;
115}
116
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700117// TODO(xunchang) modify the function parameters to button up & down.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700118int TextMenu::Select(int sel) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700119 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
120 int count = ItemsCount();
121
122 // Wraps the selection at boundary if the menu is not scrollable.
123 if (!scrollable_) {
124 if (sel < 0) {
125 selection_ = count - 1;
126 } else if (sel >= count) {
127 selection_ = 0;
128 } else {
129 selection_ = sel;
130 }
131
132 return selection_;
133 }
134
135 if (sel < 0) {
136 selection_ = 0;
137 } else if (sel >= count) {
138 selection_ = count - 1;
139 } else {
140 if (static_cast<size_t>(sel) < menu_start_) {
141 menu_start_--;
142 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
143 menu_start_++;
144 }
145 selection_ = sel;
146 }
147
148 return selection_;
149}
150
Tianjie Xu66dbf632018-10-11 16:54:50 -0700151int TextMenu::DrawHeader(int x, int y) const {
152 int offset = 0;
153
154 draw_funcs_.SetColor(UIElement::HEADER);
155 if (!scrollable()) {
156 offset += draw_funcs_.DrawWrappedTextLines(x, y + offset, text_headers());
157 } else {
158 offset += draw_funcs_.DrawTextLines(x, y + offset, text_headers());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700159 }
160
161 return offset;
162}
163
164int TextMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
165 int offset = 0;
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100166 int padding = draw_funcs_.MenuItemPadding();
Tianjie Xu66dbf632018-10-11 16:54:50 -0700167
168 draw_funcs_.SetColor(UIElement::MENU);
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100169 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
170
171 int item_container_offset = offset; // store it for drawing scrollbar on most top
172
Tianjie Xu66dbf632018-10-11 16:54:50 -0700173 for (size_t i = MenuStart(); i < MenuEnd(); ++i) {
174 bool bold = false;
175 if (i == selection()) {
176 // Draw the highlight bar.
177 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
178
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100179 int bar_height = padding + char_height_ + padding;
180 draw_funcs_.DrawHighlightBar(0, y + offset, screen_width, bar_height);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700181
182 // Bold white text for the selected item.
183 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
184 bold = true;
185 }
186 offset += draw_funcs_.DrawTextLine(x, y + offset, TextItem(i), bold);
187
188 draw_funcs_.SetColor(UIElement::MENU);
189 }
190 offset += draw_funcs_.DrawHorizontalRule(y + offset);
191
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100192 std::string unused;
193 if (ItemsOverflow(&unused)) {
194 int container_height = max_display_items_ * (2 * padding + char_height_);
195 int bar_height = container_height / (text_items_.size() - max_display_items_ + 1);
196 int start_y = y + item_container_offset + bar_height * menu_start_;
197 draw_funcs_.SetColor(UIElement::SCROLLBAR);
198 draw_funcs_.DrawScrollBar(start_y, bar_height);
199 }
200
Tianjie Xu66dbf632018-10-11 16:54:50 -0700201 return offset;
202}
203
Tao Baoda409fb2018-10-21 23:36:26 -0700204GraphicMenu::GraphicMenu(const GRSurface* graphic_headers,
205 const std::vector<const GRSurface*>& graphic_items,
Tianjie Xub99e6062018-10-16 15:13:09 -0700206 size_t initial_selection, const DrawInterface& draw_funcs)
Tao Baoda409fb2018-10-21 23:36:26 -0700207 : Menu(initial_selection, draw_funcs) {
208 graphic_headers_ = graphic_headers->Clone();
209 graphic_items_.reserve(graphic_items.size());
210 for (const auto& item : graphic_items) {
211 graphic_items_.emplace_back(item->Clone());
212 }
213}
Tianjie Xu66dbf632018-10-11 16:54:50 -0700214
215int GraphicMenu::Select(int sel) {
216 CHECK_LE(graphic_items_.size(), static_cast<size_t>(std::numeric_limits<int>::max()));
217 int count = graphic_items_.size();
218
219 // Wraps the selection at boundary if the menu is not scrollable.
220 if (sel < 0) {
221 selection_ = count - 1;
222 } else if (sel >= count) {
223 selection_ = 0;
224 } else {
225 selection_ = sel;
226 }
227
228 return selection_;
229}
230
231int GraphicMenu::DrawHeader(int x, int y) const {
Tianjie Xub99e6062018-10-16 15:13:09 -0700232 draw_funcs_.SetColor(UIElement::HEADER);
Tao Baoda409fb2018-10-21 23:36:26 -0700233 draw_funcs_.DrawTextIcon(x, y, graphic_headers_.get());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700234 return graphic_headers_->height;
235}
236
237int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
238 int offset = 0;
239
240 draw_funcs_.SetColor(UIElement::MENU);
241 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
242
243 for (size_t i = 0; i < graphic_items_.size(); i++) {
244 auto& item = graphic_items_[i];
245 if (i == selection_) {
246 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
247
248 int bar_height = item->height + 4;
249 draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height);
250
251 // Bold white text for the selected item.
252 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
253 }
Tao Baoda409fb2018-10-21 23:36:26 -0700254 draw_funcs_.DrawTextIcon(x, y + offset, item.get());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700255 offset += item->height;
256
257 draw_funcs_.SetColor(UIElement::MENU);
258 }
xunchangc7dbc732018-12-20 11:31:18 -0800259 offset += draw_funcs_.DrawHorizontalRule(y + offset);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700260
261 return offset;
262}
263
Tao Baoda409fb2018-10-21 23:36:26 -0700264bool GraphicMenu::Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers,
265 const std::vector<const GRSurface*>& graphic_items) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700266 int offset = 0;
Tianjie Xub99e6062018-10-16 15:13:09 -0700267 if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700268 return false;
269 }
Tianjie Xub99e6062018-10-16 15:13:09 -0700270 offset += graphic_headers->height;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700271
Tianjie Xub99e6062018-10-16 15:13:09 -0700272 for (const auto& item : graphic_items) {
273 if (!ValidateGraphicSurface(max_width, max_height, offset, item)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700274 return false;
275 }
276 offset += item->height;
277 }
278
279 return true;
280}
281
Tianjie Xub99e6062018-10-16 15:13:09 -0700282bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y,
283 const GRSurface* surface) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700284 if (!surface) {
Tao Baoa00b4492019-01-16 09:29:47 -0800285 fprintf(stderr, "Graphic surface can not be null\n");
Tianjie Xu66dbf632018-10-11 16:54:50 -0700286 return false;
287 }
288
289 if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) {
Tao Baoa00b4492019-01-16 09:29:47 -0800290 fprintf(stderr, "Invalid graphic surface, pixel bytes: %zu, width: %zu row_bytes: %zu\n",
Tianjie Xu66dbf632018-10-11 16:54:50 -0700291 surface->pixel_bytes, surface->width, surface->row_bytes);
292 return false;
293 }
294
Tianjie Xub99e6062018-10-16 15:13:09 -0700295 if (surface->width > max_width || surface->height > max_height - y) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700296 fprintf(stderr,
Tao Baodd789822018-11-26 16:28:07 -0800297 "Graphic surface doesn't fit into the screen. width: %zu, height: %zu, max_width: %zu,"
Tianjie Xu66dbf632018-10-11 16:54:50 -0700298 " max_height: %zu, vertical offset: %d\n",
Tianjie Xub99e6062018-10-16 15:13:09 -0700299 surface->width, surface->height, max_width, max_height, y);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700300 return false;
301 }
302
303 return true;
304}
305
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100306MenuDrawFunctions::MenuDrawFunctions(const DrawInterface& wrappee)
307 : wrappee_(wrappee) {
308}
309
310int MenuDrawFunctions::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
311 gr_text(gr_menu_font(), x, y + MenuItemPadding(), line.c_str(), bold);
312 return 2 * MenuItemPadding() + MenuCharHeight();
313}
314
315int MenuDrawFunctions::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
316 int offset = 0;
317 for (const auto& line : lines) {
318 offset += DrawTextLine(x, y + offset, line, false);
319 }
320 return offset;
321}
322
323int MenuDrawFunctions::DrawWrappedTextLines(int x, int y, const std::vector<std::string>& lines) const {
324 // Keep symmetrical margins based on the given offset (i.e. x).
325 size_t text_cols = (gr_fb_width() - x * 2) / MenuCharWidth();
326 int offset = 0;
327 for (const auto& line : lines) {
328 size_t next_start = 0;
329 while (next_start < line.size()) {
330 std::string sub = line.substr(next_start, text_cols + 1);
331 if (sub.size() <= text_cols) {
332 next_start += sub.size();
333 } else {
334 // Line too long and must be wrapped to text_cols columns.
335 size_t last_space = sub.find_last_of(" \t\n");
336 if (last_space == std::string::npos) {
337 // No space found, just draw as much as we can.
338 sub.resize(text_cols);
339 next_start += text_cols;
340 } else {
341 sub.resize(last_space);
342 next_start += last_space + 1;
343 }
344 }
345 offset += DrawTextLine(x, y + offset, sub, false);
346 }
347 }
348 return offset;
349}
350
351ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(true) {}
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700352
Tao Bao0bc88de2018-07-31 14:53:16 -0700353constexpr int kDefaultMarginHeight = 0;
354constexpr int kDefaultMarginWidth = 0;
355constexpr int kDefaultAnimationFps = 30;
356
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700357ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao0bc88de2018-07-31 14:53:16 -0700358 : margin_width_(
359 android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)),
360 margin_height_(
361 android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)),
362 animation_fps_(
363 android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)),
364 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Michael Bestas47bc4be2019-03-23 17:28:22 +0200365 blank_unblank_on_init_(
366 android::base::GetBoolProperty("ro.recovery.ui.blank_unblank_on_init", false)),
Tao Baoda409fb2018-10-21 23:36:26 -0700367 current_icon_(NONE),
368 current_frame_(0),
369 intro_done_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800370 progressBarType(EMPTY),
371 progressScopeStart(0),
372 progressScopeSize(0),
373 progress(0),
374 pagesIdentical(false),
375 text_cols_(0),
376 text_rows_(0),
377 text_(nullptr),
378 text_col_(0),
379 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800380 show_text(false),
381 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700382 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800383 file_viewer_text_(nullptr),
Tao Bao736d59c2017-01-03 10:15:33 -0800384 stage(-1),
385 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800386 locale_(""),
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700387 rtl_locale_(false) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700388
Tao Bao26ea9592018-05-09 16:32:02 -0700389ScreenRecoveryUI::~ScreenRecoveryUI() {
390 progress_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -0700391 if (progress_thread_.joinable()) {
392 progress_thread_.join();
393 }
Tao Bao60ac6222018-06-13 14:33:51 -0700394 // No-op if gr_init() (via Init()) was not called or had failed.
395 gr_exit();
Tao Bao26ea9592018-05-09 16:32:02 -0700396}
397
Tao Baoda409fb2018-10-21 23:36:26 -0700398const GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
399 if (current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) {
400 return intro_done_ ? loop_frames_[current_frame_].get() : intro_frames_[current_frame_].get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700401 }
Tao Baoda409fb2018-10-21 23:36:26 -0700402 return error_icon_.get();
Elliott Hughes498cda62016-04-14 16:49:04 -0700403}
404
Tao Baoda409fb2018-10-21 23:36:26 -0700405const GRSurface* ScreenRecoveryUI::GetCurrentText() const {
406 switch (current_icon_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700407 case ERASING:
Tao Baoda409fb2018-10-21 23:36:26 -0700408 return erasing_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700409 case ERROR:
Tao Baoda409fb2018-10-21 23:36:26 -0700410 return error_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700411 case INSTALLING_UPDATE:
Tao Baoda409fb2018-10-21 23:36:26 -0700412 return installing_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700413 case NO_COMMAND:
Tao Baoda409fb2018-10-21 23:36:26 -0700414 return no_command_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700415 case NONE:
416 abort();
417 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700418}
419
Mikhail Lappob49767c2017-03-23 21:44:26 +0100420int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao0bc88de2018-07-31 14:53:16 -0700421 return dp * density_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700422}
423
424// Here's the intended layout:
425
Elliott Hughes6d089a92016-07-08 17:23:41 -0700426// | portrait large landscape large
427// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700428// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700429// icon | (200dp)
430// gap | 68dp 68dp 56dp 112dp
431// text | (14sp)
432// gap | 32dp 32dp 26dp 52dp
433// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700434// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700435
Tao Bao3250f722017-06-29 14:32:05 -0700436// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
437// work), so that's the more useful measurement for calling code. We use even top and bottom gaps.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700438
Elliott Hughes6d089a92016-07-08 17:23:41 -0700439enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700440enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700441static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tianjie Xub5108c32018-08-20 13:40:47 -0700442 { 32, 68 }, // PORTRAIT
443 { 32, 68 }, // PORTRAIT_LARGE
444 { 26, 56 }, // LANDSCAPE
445 { 52, 112 }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700446};
447
Tao Bao99b2d772017-06-23 22:47:03 -0700448int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Baoda409fb2018-10-21 23:36:26 -0700449 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) -
450 gr_get_height(loop_frames_[0].get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700451}
452
Tao Bao99b2d772017-06-23 22:47:03 -0700453int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700454 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
Tao Baoda409fb2018-10-21 23:36:26 -0700455 gr_get_height(installing_text_.get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700456}
457
Tao Bao99b2d772017-06-23 22:47:03 -0700458int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Baoda409fb2018-10-21 23:36:26 -0700459 int elements_sum = gr_get_height(loop_frames_[0].get()) + PixelsFromDp(kLayouts[layout_][ICON]) +
460 gr_get_height(installing_text_.get()) + PixelsFromDp(kLayouts[layout_][TEXT]) +
461 gr_get_height(progress_bar_fill_.get());
Luke Song92eda4d2017-09-19 10:51:35 -0700462 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
Tao Baoda409fb2018-10-21 23:36:26 -0700463 return ScreenHeight() - bottom_gap - gr_get_height(progress_bar_fill_.get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700464}
465
Doug Zongker211aebc2011-10-28 15:13:10 -0700466// Clear the screen and draw the currently selected background icon (if any).
467// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700468void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700469 pagesIdentical = false;
470 gr_color(0, 0, 0, 255);
471 gr_clear();
Tao Baoda409fb2018-10-21 23:36:26 -0700472 if (current_icon_ != NONE) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700473 if (max_stage != -1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700474 int stage_height = gr_get_height(stage_marker_empty_.get());
475 int stage_width = gr_get_width(stage_marker_empty_.get());
476 int x = (ScreenWidth() - max_stage * gr_get_width(stage_marker_empty_.get())) / 2;
Tao Bao0bc88de2018-07-31 14:53:16 -0700477 int y = ScreenHeight() - stage_height - margin_height_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700478 for (int i = 0; i < max_stage; ++i) {
Tao Baoda409fb2018-10-21 23:36:26 -0700479 const auto& stage_surface = (i < stage) ? stage_marker_fill_ : stage_marker_empty_;
480 DrawSurface(stage_surface.get(), 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700481 x += stage_width;
482 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700483 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700484
Tao Baoda409fb2018-10-21 23:36:26 -0700485 const auto& text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700486 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700487 int text_y = GetTextBaseline();
488 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700489 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700490 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700491}
492
Tao Baoea78d862017-06-28 14:52:17 -0700493// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
494// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700495void ScreenRecoveryUI::draw_foreground_locked() {
Tao Baoda409fb2018-10-21 23:36:26 -0700496 if (current_icon_ != NONE) {
497 const auto& frame = GetCurrentFrame();
Tao Bao736d59c2017-01-03 10:15:33 -0800498 int frame_width = gr_get_width(frame);
499 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700500 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800501 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700502 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800503 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700504
Tao Bao736d59c2017-01-03 10:15:33 -0800505 if (progressBarType != EMPTY) {
Tao Baoda409fb2018-10-21 23:36:26 -0700506 int width = gr_get_width(progress_bar_empty_.get());
507 int height = gr_get_height(progress_bar_empty_.get());
Doug Zongker211aebc2011-10-28 15:13:10 -0700508
Luke Song92eda4d2017-09-19 10:51:35 -0700509 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800510 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700511
Tao Bao736d59c2017-01-03 10:15:33 -0800512 // Erase behind the progress bar (in case this was a progress-only update)
513 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700514 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700515
Tao Bao736d59c2017-01-03 10:15:33 -0800516 if (progressBarType == DETERMINATE) {
517 float p = progressScopeStart + progress * progressScopeSize;
518 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700519
Tao Bao736d59c2017-01-03 10:15:33 -0800520 if (rtl_locale_) {
521 // Fill the progress bar from right to left.
522 if (pos > 0) {
Tao Baoda409fb2018-10-21 23:36:26 -0700523 DrawSurface(progress_bar_fill_.get(), width - pos, 0, pos, height,
524 progress_x + width - pos, progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700525 }
Tao Bao736d59c2017-01-03 10:15:33 -0800526 if (pos < width - 1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700527 DrawSurface(progress_bar_empty_.get(), 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800528 }
529 } else {
530 // Fill the progress bar from left to right.
531 if (pos > 0) {
Tao Baoda409fb2018-10-21 23:36:26 -0700532 DrawSurface(progress_bar_fill_.get(), 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800533 }
534 if (pos < width - 1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700535 DrawSurface(progress_bar_empty_.get(), pos, 0, width - pos, height, progress_x + pos,
536 progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800537 }
538 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700539 }
Tao Bao736d59c2017-01-03 10:15:33 -0800540 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700541}
542
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100543/* Lineage teal: #167c80 */
Tao Bao99b2d772017-06-23 22:47:03 -0700544void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700545 switch (e) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700546 case UIElement::INFO:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700547 gr_color(249, 194, 0, 255);
548 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700549 case UIElement::HEADER:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700550 gr_color(247, 0, 6, 255);
551 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700552 case UIElement::MENU:
553 case UIElement::MENU_SEL_BG:
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100554 gr_color(0xd8, 0xd8, 0xd8, 255);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700555 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700556 case UIElement::MENU_SEL_BG_ACTIVE:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700557 gr_color(0, 156, 100, 255);
558 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700559 case UIElement::MENU_SEL_FG:
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100560 case UIElement::SCROLLBAR:
561 gr_color(0x16, 0x7c, 0x80, 255);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700562 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700563 case UIElement::LOG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700564 gr_color(196, 196, 196, 255);
565 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700566 case UIElement::TEXT_FILL:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700567 gr_color(0, 0, 0, 160);
568 break;
569 default:
570 gr_color(255, 255, 255, 255);
571 break;
572 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700573}
Doug Zongker211aebc2011-10-28 15:13:10 -0700574
Tianjie Xu29d55752017-09-20 17:53:46 -0700575void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
576 size_t sel) {
577 SetLocale(locales_entries[sel]);
578 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
579 "installing_security_text", "no_command_text" };
Tao Baoda409fb2018-10-21 23:36:26 -0700580 std::unordered_map<std::string, std::unique_ptr<GRSurface>> surfaces;
Tianjie Xu29d55752017-09-20 17:53:46 -0700581 for (const auto& name : text_name) {
Tao Baoda409fb2018-10-21 23:36:26 -0700582 auto text_image = LoadLocalizedBitmap(name);
Tianjie Xu29d55752017-09-20 17:53:46 -0700583 if (!text_image) {
584 Print("Failed to load %s\n", name.c_str());
585 return;
586 }
Tao Baoda409fb2018-10-21 23:36:26 -0700587 surfaces.emplace(name, std::move(text_image));
Tianjie Xu29d55752017-09-20 17:53:46 -0700588 }
589
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700590 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu29d55752017-09-20 17:53:46 -0700591 gr_color(0, 0, 0, 255);
592 gr_clear();
593
Tao Bao0bc88de2018-07-31 14:53:16 -0700594 int text_y = margin_height_;
595 int text_x = margin_width_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700596 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
597 // Write the header and descriptive texts.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700598 SetColor(UIElement::INFO);
Tianjie Xu29d55752017-09-20 17:53:46 -0700599 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700600 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700601 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700602 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700603 // clang-format off
604 std::vector<std::string> instruction = {
605 locale_selection,
606 "Use volume up/down to switch locales and power to exit."
607 };
608 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700609 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
610
611 // Iterate through the text images and display them in order for the current locale.
612 for (const auto& p : surfaces) {
613 text_y += line_spacing;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700614 SetColor(UIElement::LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700615 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700616 gr_color(255, 255, 255, 255);
617 gr_texticon(text_x, text_y, p.second.get());
618 text_y += gr_get_height(p.second.get());
619 }
620 // Update the whole screen.
621 gr_flip();
Tianjie Xu29d55752017-09-20 17:53:46 -0700622}
623
Tao Bao39c49182018-05-07 22:50:33 -0700624void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700625 // Load a list of locales embedded in one of the resource files.
626 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
627 if (locales_entries.empty()) {
628 Print("Failed to load locales from the resource files\n");
629 return;
630 }
Tao Bao39c49182018-05-07 22:50:33 -0700631 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700632 size_t selected = 0;
633 SelectAndShowBackgroundText(locales_entries, selected);
634
635 FlushKeys();
636 while (true) {
637 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -0700638 if (key == static_cast<int>(KeyError::INTERRUPTED)) break;
Tianjie Xu29d55752017-09-20 17:53:46 -0700639 if (key == KEY_POWER || key == KEY_ENTER) {
640 break;
641 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
642 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
643 SelectAndShowBackgroundText(locales_entries, selected);
644 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
645 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
646 SelectAndShowBackgroundText(locales_entries, selected);
647 }
648 }
649
650 SetLocale(saved_locale);
651}
652
Luke Song92eda4d2017-09-19 10:51:35 -0700653int ScreenRecoveryUI::ScreenWidth() const {
654 return gr_fb_width();
655}
656
657int ScreenRecoveryUI::ScreenHeight() const {
658 return gr_fb_height();
659}
660
Tao Bao65815b62018-10-23 10:54:02 -0700661void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx,
Luke Song92eda4d2017-09-19 10:51:35 -0700662 int dy) const {
663 gr_blit(surface, sx, sy, w, h, dx, dy);
664}
665
Tao Baoea78d862017-06-28 14:52:17 -0700666int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700667 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700668 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700669}
670
Luke Songe2bd8762017-06-12 16:08:33 -0700671void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100672 if (y + height > ScreenHeight())
673 height = ScreenHeight() - y;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700674 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700675}
676
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100677void ScreenRecoveryUI::DrawScrollBar(int y, int height) const {
678 int x = ScreenWidth() - margin_width_;
679 int width = 8;
680 gr_fill(x - width, y, x, y + height);
681}
682
Luke Song92eda4d2017-09-19 10:51:35 -0700683void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
684 gr_fill(x, y, w, h);
685}
686
Tao Bao65815b62018-10-23 10:54:02 -0700687void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700688 gr_texticon(x, y, surface);
689}
690
Tao Bao93e46ad2018-05-02 14:57:21 -0700691int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
692 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700693 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700694}
695
Tao Bao93e46ad2018-05-02 14:57:21 -0700696int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700697 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700698 for (const auto& line : lines) {
699 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700700 }
Tao Baoea78d862017-06-28 14:52:17 -0700701 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700702}
703
Tao Bao93e46ad2018-05-02 14:57:21 -0700704int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
705 const std::vector<std::string>& lines) const {
Tao Bao452b4872018-05-09 11:52:09 -0700706 // Keep symmetrical margins based on the given offset (i.e. x).
707 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700708 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700709 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700710 size_t next_start = 0;
711 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700712 std::string sub = line.substr(next_start, text_cols + 1);
713 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700714 next_start += sub.size();
715 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700716 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700717 size_t last_space = sub.find_last_of(" \t\n");
718 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700719 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700720 sub.resize(text_cols);
721 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700722 } else {
723 sub.resize(last_space);
724 next_start += last_space + 1;
725 }
726 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700727 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700728 }
729 }
730 return offset;
731}
732
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700733void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
734 title_lines_ = lines;
735}
736
Tao Bao171b4c42017-06-19 23:10:44 -0700737// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
738// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700739void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700740 if (!show_text) {
741 draw_background_locked();
742 draw_foreground_locked();
743 return;
744 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700745
Tao Bao171b4c42017-06-19 23:10:44 -0700746 gr_color(0, 0, 0, 255);
747 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700748
Tao Bao93e46ad2018-05-02 14:57:21 -0700749 // clang-format off
Tao Bao1fe1afe2018-05-01 15:56:05 -0700750 static std::vector<std::string> REGULAR_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700751 "Use volume up/down and power.",
752 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700753 static std::vector<std::string> LONG_PRESS_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700754 "Any button cycles highlight.",
755 "Long-press activates.",
756 };
757 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700758 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
759}
760
761// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700762void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
763 const std::vector<std::string>& help_message) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700764 int y = margin_height_;
David Anderson983e2d52019-01-02 11:35:38 -0800765
766 if (fastbootd_logo_ && fastbootd_logo_enabled_) {
767 // Try to get this centered on screen.
768 auto width = gr_get_width(fastbootd_logo_.get());
769 auto height = gr_get_height(fastbootd_logo_.get());
770 auto centered_x = ScreenWidth() / 2 - width / 2;
771 DrawSurface(fastbootd_logo_.get(), 0, 0, width, height, centered_x, y);
772 y += height;
773 }
774
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700775 if (menu_) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700776 int x = margin_width_ + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700777
Tianjie Xu66dbf632018-10-11 16:54:50 -0700778 SetColor(UIElement::INFO);
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700779
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100780 if (lineage_logo_) {
781 auto logo_width = gr_get_width(lineage_logo_.get());
782 auto logo_height = gr_get_height(lineage_logo_.get());
783 auto centered_x = ScreenWidth() / 2 - logo_width / 2;
784 DrawSurface(lineage_logo_.get(), 0, 0, logo_width, logo_height, centered_x, y);
785 y += logo_height;
786 } else {
787 for (size_t i = 0; i < title_lines_.size(); i++) {
788 y += DrawTextLine(x, y, title_lines_[i], i == 0);
789 }
790 y += DrawTextLines(x, y, help_message);
Doug Zongker211aebc2011-10-28 15:13:10 -0700791 }
Tao Bao171b4c42017-06-19 23:10:44 -0700792
Tianjie Xu66dbf632018-10-11 16:54:50 -0700793 y += menu_->DrawHeader(x, y);
794 y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress());
Tao Bao171b4c42017-06-19 23:10:44 -0700795 }
796
797 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
798 // we've displayed the entire text buffer.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700799 SetColor(UIElement::LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700800 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700801 size_t count = 0;
Tao Bao0bc88de2018-07-31 14:53:16 -0700802 for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700803 ty -= char_height_, ++count) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700804 DrawTextLine(margin_width_, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700805 --row;
806 if (row < 0) row = text_rows_ - 1;
807 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700808}
809
810// Redraw everything on the screen and flip the screen (make it visible).
811// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700812void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700813 draw_screen_locked();
814 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700815}
816
817// Updates only the progress bar, if possible, otherwise redraws the screen.
818// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700819void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700820 if (show_text || !pagesIdentical) {
821 draw_screen_locked(); // Must redraw the whole screen
822 pagesIdentical = true;
823 } else {
824 draw_foreground_locked(); // Draw only the progress bar and overlays
825 }
826 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700827}
828
Elliott Hughes985022a2015-04-13 13:04:32 -0700829void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0bc88de2018-07-31 14:53:16 -0700830 double interval = 1.0 / animation_fps_;
Tao Bao26ea9592018-05-09 16:32:02 -0700831 while (!progress_thread_stopped_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700832 double start = now();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700833 bool redraw = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700834 {
835 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700836
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700837 // update the installation animation, if active
838 // skip this if we have a text overlay (too expensive to update)
Tao Baoda409fb2018-10-21 23:36:26 -0700839 if ((current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) && !show_text) {
840 if (!intro_done_) {
841 if (current_frame_ == intro_frames_.size() - 1) {
842 intro_done_ = true;
843 current_frame_ = 0;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700844 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700845 ++current_frame_;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700846 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700847 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700848 current_frame_ = (current_frame_ + 1) % loop_frames_.size();
Doug Zongker211aebc2011-10-28 15:13:10 -0700849 }
850
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700851 redraw = true;
852 }
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700853
854 // move the progress bar forward on timed intervals, if configured
855 int duration = progressScopeDuration;
856 if (progressBarType == DETERMINATE && duration > 0) {
857 double elapsed = now() - progressScopeTime;
858 float p = 1.0 * elapsed / duration;
859 if (p > 1.0) p = 1.0;
860 if (p > progress) {
861 progress = p;
862 redraw = true;
863 }
864 }
865
866 if (redraw) update_progress_locked();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700867 }
868
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700869 double end = now();
870 // minimum of 20ms delay between frames
871 double delay = interval - (end - start);
872 if (delay < 0.02) delay = 0.02;
873 usleep(static_cast<useconds_t>(delay * 1000000));
874 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700875}
876
Tao Baoda409fb2018-10-21 23:36:26 -0700877std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadBitmap(const std::string& filename) {
878 GRSurface* surface;
879 if (auto result = res_create_display_surface(filename.c_str(), &surface); result < 0) {
880 LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")";
881 return nullptr;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700882 }
Tao Baoda409fb2018-10-21 23:36:26 -0700883 return std::unique_ptr<GRSurface>(surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800884}
885
Tao Baoda409fb2018-10-21 23:36:26 -0700886std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) {
887 GRSurface* surface;
888 if (auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface);
889 result < 0) {
890 LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")";
891 return nullptr;
Tao Bao736d59c2017-01-03 10:15:33 -0800892 }
Tao Baoda409fb2018-10-21 23:36:26 -0700893 return std::unique_ptr<GRSurface>(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700894}
895
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700896static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700897 char** result = new char*[rows];
898 for (size_t i = 0; i < rows; ++i) {
899 result[i] = new char[cols];
900 memset(result[i], 0, cols);
901 }
902 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700903}
904
Tianjie Xu35926c42016-04-28 18:06:26 -0700905// Choose the right background string to display during update.
906void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700907 if (security_update) {
Tao Baoda409fb2018-10-21 23:36:26 -0700908 installing_text_ = LoadLocalizedBitmap("installing_security_text");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700909 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700910 installing_text_ = LoadLocalizedBitmap("installing_text");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700911 }
912 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700913}
914
Sen Jiangd5304492016-12-09 16:20:49 -0800915bool ScreenRecoveryUI::InitTextParams() {
Tao Baoba4edb32018-06-13 11:22:50 -0700916 // gr_init() would return successfully on font initialization failure.
917 if (gr_sys_font() == nullptr) {
Tao Bao171b4c42017-06-19 23:10:44 -0700918 return false;
919 }
Tao Bao171b4c42017-06-19 23:10:44 -0700920 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100921 gr_font_size(gr_menu_font(), &menu_char_width_, &menu_char_height_);
Tao Bao0bc88de2018-07-31 14:53:16 -0700922 text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_;
923 text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700924 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700925}
926
Tianjie Xub99e6062018-10-16 15:13:09 -0700927bool ScreenRecoveryUI::LoadWipeDataMenuText() {
Tianjie Xu1a0a30a2018-10-25 15:22:07 -0700928 // Ignores the errors since the member variables will stay as nullptr.
929 cancel_wipe_data_text_ = LoadLocalizedBitmap("cancel_wipe_data_text");
930 factory_data_reset_text_ = LoadLocalizedBitmap("factory_data_reset_text");
931 try_again_text_ = LoadLocalizedBitmap("try_again_text");
932 wipe_data_confirmation_text_ = LoadLocalizedBitmap("wipe_data_confirmation_text");
933 wipe_data_menu_header_text_ = LoadLocalizedBitmap("wipe_data_menu_header_text");
Tianjie Xub99e6062018-10-16 15:13:09 -0700934 return true;
935}
936
Tao Bao736d59c2017-01-03 10:15:33 -0800937bool ScreenRecoveryUI::Init(const std::string& locale) {
938 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800939
Tao Baoba4edb32018-06-13 11:22:50 -0700940 if (gr_init() == -1) {
941 return false;
942 }
943
Tao Bao736d59c2017-01-03 10:15:33 -0800944 if (!InitTextParams()) {
945 return false;
946 }
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100947 menu_draw_funcs_ = std::make_unique<MenuDrawFunctions>(*this);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700948
Michael Bestas47bc4be2019-03-23 17:28:22 +0200949 if (blank_unblank_on_init_) {
950 gr_fb_blank(true);
951 gr_fb_blank(false);
952 }
953
Tao Bao736d59c2017-01-03 10:15:33 -0800954 // Are we portrait or landscape?
955 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
956 // Are we the large variant of our base layout?
957 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700958
Tao Bao736d59c2017-01-03 10:15:33 -0800959 text_ = Alloc2d(text_rows_, text_cols_ + 1);
960 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800961
Tao Bao736d59c2017-01-03 10:15:33 -0800962 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700963
Tao Baoefb49ad2017-01-31 23:03:10 -0800964 // Set up the locale info.
965 SetLocale(locale);
966
Tao Baoda409fb2018-10-21 23:36:26 -0700967 error_icon_ = LoadBitmap("icon_error");
Doug Zongker02ec6b82012-08-22 17:26:40 -0700968
Tao Baoda409fb2018-10-21 23:36:26 -0700969 progress_bar_empty_ = LoadBitmap("progress_empty");
970 progress_bar_fill_ = LoadBitmap("progress_fill");
971 stage_marker_empty_ = LoadBitmap("stage_empty");
972 stage_marker_fill_ = LoadBitmap("stage_fill");
Elliott Hughes498cda62016-04-14 16:49:04 -0700973
Tao Baoda409fb2018-10-21 23:36:26 -0700974 erasing_text_ = LoadLocalizedBitmap("erasing_text");
975 no_command_text_ = LoadLocalizedBitmap("no_command_text");
976 error_text_ = LoadLocalizedBitmap("error_text");
Doug Zongker211aebc2011-10-28 15:13:10 -0700977
Alessandro Astonec6ddbac2020-03-09 23:17:50 +0100978 lineage_logo_ = LoadBitmap("logo_image");
Alessandro Astone2effe772020-02-26 17:25:54 +0100979 if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) ||
980 android::base::GetBoolProperty("ro.fastbootd.available", false)) {
David Anderson983e2d52019-01-02 11:35:38 -0800981 fastbootd_logo_ = LoadBitmap("fastbootd");
982 }
983
Tao Baoda409fb2018-10-21 23:36:26 -0700984 // Background text for "installing_update" could be "installing update" or
985 // "installing security update". It will be set after Init() according to the commands in BCB.
986 installing_text_.reset();
Elliott Hughes498cda62016-04-14 16:49:04 -0700987
Tianjie Xub99e6062018-10-16 15:13:09 -0700988 LoadWipeDataMenuText();
989
Tao Bao736d59c2017-01-03 10:15:33 -0800990 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700991
Tao Bao26ea9592018-05-09 16:32:02 -0700992 // Keep the progress bar updated, even when the process is otherwise busy.
993 progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800994
Tao Bao736d59c2017-01-03 10:15:33 -0800995 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700996}
997
Tao Bao551d2c32018-05-09 20:53:13 -0700998std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700999 return locale_;
1000}
1001
Elliott Hughes498cda62016-04-14 16:49:04 -07001002void ScreenRecoveryUI::LoadAnimation() {
Tao Bao6cd81682018-05-03 21:53:11 -07001003 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
1004 closedir);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001005 dirent* de;
1006 std::vector<std::string> intro_frame_names;
1007 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -07001008
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001009 while ((de = readdir(dir.get())) != nullptr) {
1010 int value, num_chars;
1011 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
1012 intro_frame_names.emplace_back(de->d_name, num_chars);
1013 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
1014 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -07001015 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001016 }
Elliott Hughes498cda62016-04-14 16:49:04 -07001017
Tao Baoda409fb2018-10-21 23:36:26 -07001018 size_t intro_frames = intro_frame_names.size();
1019 size_t loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -07001020
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001021 // It's okay to not have an intro.
Tao Baoda409fb2018-10-21 23:36:26 -07001022 if (intro_frames == 0) intro_done_ = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001023 // But you must have an animation.
1024 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -07001025
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001026 std::sort(intro_frame_names.begin(), intro_frame_names.end());
1027 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -07001028
Tao Baoda409fb2018-10-21 23:36:26 -07001029 intro_frames_.clear();
1030 intro_frames_.reserve(intro_frames);
1031 for (const auto& frame_name : intro_frame_names) {
1032 intro_frames_.emplace_back(LoadBitmap(frame_name));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001033 }
Elliott Hughes498cda62016-04-14 16:49:04 -07001034
Tao Baoda409fb2018-10-21 23:36:26 -07001035 loop_frames_.clear();
1036 loop_frames_.reserve(loop_frames);
1037 for (const auto& frame_name : loop_frame_names) {
1038 loop_frames_.emplace_back(LoadBitmap(frame_name));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001039 }
Elliott Hughes498cda62016-04-14 16:49:04 -07001040}
1041
Elliott Hughes8de52072015-04-08 20:06:50 -07001042void ScreenRecoveryUI::SetBackground(Icon icon) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001043 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001044
Tao Baoda409fb2018-10-21 23:36:26 -07001045 current_icon_ = icon;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001046 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001047}
1048
Elliott Hughes8de52072015-04-08 20:06:50 -07001049void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001050 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001051 if (progressBarType != type) {
1052 progressBarType = type;
1053 }
1054 progressScopeStart = 0;
1055 progressScopeSize = 0;
1056 progress = 0;
1057 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001058}
1059
Elliott Hughes8de52072015-04-08 20:06:50 -07001060void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001061 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001062 progressBarType = DETERMINATE;
1063 progressScopeStart += progressScopeSize;
1064 progressScopeSize = portion;
1065 progressScopeTime = now();
1066 progressScopeDuration = seconds;
1067 progress = 0;
1068 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001069}
1070
Elliott Hughes8de52072015-04-08 20:06:50 -07001071void ScreenRecoveryUI::SetProgress(float fraction) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001072 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001073 if (fraction < 0.0) fraction = 0.0;
1074 if (fraction > 1.0) fraction = 1.0;
1075 if (progressBarType == DETERMINATE && fraction > progress) {
1076 // Skip updates that aren't visibly different.
Tao Baoda409fb2018-10-21 23:36:26 -07001077 int width = gr_get_width(progress_bar_empty_.get());
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001078 float scale = width * progressScopeSize;
1079 if ((int)(progress * scale) != (int)(fraction * scale)) {
1080 progress = fraction;
1081 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001082 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001083 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001084}
1085
Doug Zongkerc87bab12013-11-25 13:53:25 -08001086void ScreenRecoveryUI::SetStage(int current, int max) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001087 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001088 stage = current;
1089 max_stage = max;
Doug Zongkerc87bab12013-11-25 13:53:25 -08001090}
1091
Tao Baob6918c72015-05-19 17:02:16 -07001092void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001093 std::string str;
1094 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -07001095
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001096 if (copy_to_stdout) {
1097 fputs(str.c_str(), stdout);
1098 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001099
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001100 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001101 if (text_rows_ > 0 && text_cols_ > 0) {
1102 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
1103 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -07001104 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001105 text_col_ = 0;
1106 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001107 }
1108 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001109 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001110 text_[text_row_][text_col_] = '\0';
1111 update_screen_locked();
1112 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001113}
1114
Tao Baob6918c72015-05-19 17:02:16 -07001115void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001116 va_list ap;
1117 va_start(ap, fmt);
1118 PrintV(fmt, true, ap);
1119 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001120}
1121
Tianjie Xub5108c32018-08-20 13:40:47 -07001122void ScreenRecoveryUI::PrintOnScreenOnly(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001123 va_list ap;
1124 va_start(ap, fmt);
1125 PrintV(fmt, false, ap);
1126 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001127}
1128
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001129void ScreenRecoveryUI::PutChar(char ch) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001130 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001131 if (ch != '\n') text_[text_row_][text_col_++] = ch;
1132 if (ch == '\n' || text_col_ >= text_cols_) {
1133 text_col_ = 0;
1134 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001135 }
Elliott Hughes8de52072015-04-08 20:06:50 -07001136}
1137
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001138void ScreenRecoveryUI::ClearText() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001139 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001140 text_col_ = 0;
1141 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001142 for (size_t i = 0; i < text_rows_; ++i) {
1143 memset(text_[i], 0, text_cols_ + 1);
1144 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001145}
1146
1147void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001148 std::vector<off_t> offsets;
1149 offsets.push_back(ftello(fp));
1150 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001151
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001152 struct stat sb;
1153 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001154
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001155 bool show_prompt = false;
1156 while (true) {
1157 if (show_prompt) {
1158 PrintOnScreenOnly("--(%d%% of %d bytes)--",
1159 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
1160 static_cast<int>(sb.st_size));
1161 Redraw();
1162 while (show_prompt) {
1163 show_prompt = false;
1164 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001165 if (key == static_cast<int>(KeyError::INTERRUPTED)) return;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001166 if (key == KEY_POWER || key == KEY_ENTER) {
1167 return;
1168 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
1169 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001170 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001171 } else {
1172 offsets.pop_back();
1173 fseek(fp, offsets.back(), SEEK_SET);
1174 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001175 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001176 if (feof(fp)) {
1177 return;
1178 }
1179 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001180 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001181 }
1182 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001183 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001184
1185 int ch = getc(fp);
1186 if (ch == EOF) {
1187 while (text_row_ < text_rows_ - 1) PutChar('\n');
1188 show_prompt = true;
1189 } else {
1190 PutChar(ch);
1191 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
1192 show_prompt = true;
1193 }
1194 }
1195 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001196}
1197
Tao Bao1d156b92018-05-02 12:43:18 -07001198void ScreenRecoveryUI::ShowFile(const std::string& filename) {
1199 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
1200 if (!fp) {
1201 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001202 return;
1203 }
Elliott Hughesc0491632015-05-06 12:40:05 -07001204
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001205 char** old_text = text_;
1206 size_t old_text_col = text_col_;
1207 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -07001208
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001209 // Swap in the alternate screen and clear it.
1210 text_ = file_viewer_text_;
1211 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -07001212
Tao Bao1d156b92018-05-02 12:43:18 -07001213 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -07001214
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001215 text_ = old_text;
1216 text_col_ = old_text_col;
1217 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -07001218}
1219
Tao Baoda409fb2018-10-21 23:36:26 -07001220std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(
1221 const GRSurface* graphic_header, const std::vector<const GRSurface*>& graphic_items,
1222 const std::vector<std::string>& text_headers, const std::vector<std::string>& text_items,
1223 size_t initial_selection) const {
Tianjie Xub99e6062018-10-16 15:13:09 -07001224 // horizontal unusable area: margin width + menu indent
1225 size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent;
1226 // vertical unusable area: margin height + title lines + helper message + high light bar.
1227 // It is safe to reserve more space.
1228 size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3);
1229 if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) {
1230 return std::make_unique<GraphicMenu>(graphic_header, graphic_items, initial_selection, *this);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001231 }
Tianjie Xub99e6062018-10-16 15:13:09 -07001232
1233 fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n");
1234
1235 return CreateMenu(text_headers, text_items, initial_selection);
1236}
1237
1238std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(const std::vector<std::string>& text_headers,
1239 const std::vector<std::string>& text_items,
1240 size_t initial_selection) const {
Alessandro Astonec6ddbac2020-03-09 23:17:50 +01001241 int menu_char_width = MenuCharWidth();
1242 int menu_char_height = MenuCharHeight();
1243 int menu_item_padding = MenuItemPadding();
1244 int menu_rows = (ScreenHeight() - margin_height_*2 - gr_get_height(lineage_logo_.get()))
1245 / (menu_char_height + 2 * menu_item_padding) - text_headers.size();
1246 int menu_cols = (ScreenWidth() - margin_width_*2 - kMenuIndent) / menu_char_width;
1247 return std::make_unique<TextMenu>(scrollable_menu_, menu_rows, menu_cols, text_headers, text_items,
1248 initial_selection, menu_char_height, *menu_draw_funcs_);
Doug Zongker211aebc2011-10-28 15:13:10 -07001249}
1250
1251int ScreenRecoveryUI::SelectMenu(int sel) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001252 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001253 if (menu_) {
1254 int old_sel = menu_->selection();
1255 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001256
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001257 if (sel != old_sel) {
1258 update_screen_locked();
1259 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001260 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001261 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001262}
1263
Tianjie Xub99e6062018-10-16 15:13:09 -07001264size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr<Menu>&& menu, bool menu_only,
Tao Bao1fe1afe2018-05-01 15:56:05 -07001265 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001266 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1267 FlushKeys();
1268
Jerry Zhangb76af932018-05-22 12:08:35 -07001269 // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the
1270 // menu.
1271 if (IsKeyInterrupted()) return static_cast<size_t>(KeyError::INTERRUPTED);
1272
Tianjie Xub99e6062018-10-16 15:13:09 -07001273 CHECK(menu != nullptr);
Tao Bao3aec6962018-04-20 09:24:58 -07001274
Tianjie Xub99e6062018-10-16 15:13:09 -07001275 // Starts and displays the menu
1276 menu_ = std::move(menu);
1277 Redraw();
1278
1279 int selected = menu_->selection();
Tao Bao3aec6962018-04-20 09:24:58 -07001280 int chosen_item = -1;
1281 while (chosen_item < 0) {
1282 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001283 if (key == static_cast<int>(KeyError::INTERRUPTED)) { // WaitKey() was interrupted.
1284 return static_cast<size_t>(KeyError::INTERRUPTED);
1285 }
1286 if (key == static_cast<int>(KeyError::TIMED_OUT)) { // WaitKey() timed out.
Tao Bao3aec6962018-04-20 09:24:58 -07001287 if (WasTextEverVisible()) {
1288 continue;
1289 } else {
1290 LOG(INFO) << "Timed out waiting for key input; rebooting.";
Tianjie Xub99e6062018-10-16 15:13:09 -07001291 menu_.reset();
1292 Redraw();
Jerry Zhangb76af932018-05-22 12:08:35 -07001293 return static_cast<size_t>(KeyError::TIMED_OUT);
Tao Bao3aec6962018-04-20 09:24:58 -07001294 }
1295 }
1296
1297 bool visible = IsTextVisible();
1298 int action = key_handler(key, visible);
1299 if (action < 0) {
1300 switch (action) {
1301 case Device::kHighlightUp:
1302 selected = SelectMenu(--selected);
1303 break;
1304 case Device::kHighlightDown:
1305 selected = SelectMenu(++selected);
1306 break;
1307 case Device::kInvokeItem:
1308 chosen_item = selected;
1309 break;
1310 case Device::kNoAction:
1311 break;
Tom Marshalla9ac9552018-12-17 15:57:44 -08001312 case Device::kRefresh:
1313 chosen_item = Device::kRefresh;
1314 break;
Tom Marshalld23b5102017-08-24 13:50:01 +00001315 case Device::kGoBack:
1316 chosen_item = Device::kGoBack;
1317 break;
1318 case Device::kGoHome:
1319 chosen_item = Device::kGoHome;
1320 break;
Tao Bao3aec6962018-04-20 09:24:58 -07001321 }
1322 } else if (!menu_only) {
1323 chosen_item = action;
1324 }
Tom Marshalla9ac9552018-12-17 15:57:44 -08001325
1326 if (chosen_item == Device::kGoBack || chosen_item == Device::kGoHome ||
1327 chosen_item == Device::kRefresh) {
Tom Marshalld23b5102017-08-24 13:50:01 +00001328 break;
1329 }
Tao Bao3aec6962018-04-20 09:24:58 -07001330 }
1331
Tianjie Xub99e6062018-10-16 15:13:09 -07001332 menu_.reset();
1333 Redraw();
1334
Tao Bao3aec6962018-04-20 09:24:58 -07001335 return chosen_item;
1336}
1337
Tianjie Xub99e6062018-10-16 15:13:09 -07001338size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1339 const std::vector<std::string>& items, size_t initial_selection,
1340 bool menu_only,
1341 const std::function<int(int, bool)>& key_handler) {
1342 auto menu = CreateMenu(headers, items, initial_selection);
1343 if (menu == nullptr) {
1344 return initial_selection;
1345 }
1346
1347 return ShowMenu(CreateMenu(headers, items, initial_selection), menu_only, key_handler);
1348}
1349
1350size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector<std::string>& backup_headers,
1351 const std::vector<std::string>& backup_items,
1352 const std::function<int(int, bool)>& key_handler) {
Tao Baoda409fb2018-10-21 23:36:26 -07001353 auto wipe_data_menu = CreateMenu(wipe_data_menu_header_text_.get(),
1354 { try_again_text_.get(), factory_data_reset_text_.get() },
1355 backup_headers, backup_items, 0);
Tianjie Xub99e6062018-10-16 15:13:09 -07001356 if (wipe_data_menu == nullptr) {
1357 return 0;
1358 }
1359
1360 return ShowMenu(std::move(wipe_data_menu), true, key_handler);
1361}
1362
Tianjie Xu1a0a30a2018-10-25 15:22:07 -07001363size_t ScreenRecoveryUI::ShowPromptWipeDataConfirmationMenu(
1364 const std::vector<std::string>& backup_headers, const std::vector<std::string>& backup_items,
1365 const std::function<int(int, bool)>& key_handler) {
1366 auto confirmation_menu =
1367 CreateMenu(wipe_data_confirmation_text_.get(),
1368 { cancel_wipe_data_text_.get(), factory_data_reset_text_.get() }, backup_headers,
1369 backup_items, 0);
1370 if (confirmation_menu == nullptr) {
1371 return 0;
1372 }
1373
1374 return ShowMenu(std::move(confirmation_menu), true, key_handler);
1375}
1376
Elliott Hughes8de52072015-04-08 20:06:50 -07001377bool ScreenRecoveryUI::IsTextVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001378 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001379 int visible = show_text;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001380 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001381}
1382
Elliott Hughes8de52072015-04-08 20:06:50 -07001383bool ScreenRecoveryUI::WasTextEverVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001384 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001385 int ever_visible = show_text_ever;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001386 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001387}
1388
Elliott Hughes8de52072015-04-08 20:06:50 -07001389void ScreenRecoveryUI::ShowText(bool visible) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001390 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001391 show_text = visible;
1392 if (show_text) show_text_ever = true;
1393 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001394}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001395
Elliott Hughes8de52072015-04-08 20:06:50 -07001396void ScreenRecoveryUI::Redraw() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001397 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001398 update_screen_locked();
Doug Zongkerc0441d12013-07-31 11:28:24 -07001399}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001400
1401void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001402 // Redraw so that if we're in the menu, the highlight
1403 // will change color to indicate a successful long press.
1404 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001405}
Tao Baoefb49ad2017-01-31 23:03:10 -08001406
1407void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1408 locale_ = new_locale;
1409 rtl_locale_ = false;
1410
1411 if (!new_locale.empty()) {
Tao Bao347a6592018-05-08 15:58:29 -07001412 size_t separator = new_locale.find('-');
1413 // lang has the language prefix prior to the separator, or full string if none exists.
1414 std::string lang = new_locale.substr(0, separator);
Tao Baoefb49ad2017-01-31 23:03:10 -08001415
1416 // A bit cheesy: keep an explicit list of supported RTL languages.
1417 if (lang == "ar" || // Arabic
1418 lang == "fa" || // Persian (Farsi)
1419 lang == "he" || // Hebrew (new language code)
1420 lang == "iw" || // Hebrew (old language code)
1421 lang == "ur") { // Urdu
1422 rtl_locale_ = true;
1423 }
1424 }
1425}