blob: 2db27d6a77394da276523190d08facf1c275df6e [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
Tao Baoefb49ad2017-01-31 23:03:10 -080017#include "screen_ui.h"
18
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
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070045#include "device.h"
Tao Bao6cd81682018-05-03 21:53:11 -070046#include "minui/minui.h"
47#include "otautil/paths.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070048#include "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 Xu5fe5eb62018-03-20 16:07:39 -070072 menu_start_(0),
Tianjie Xu66dbf632018-10-11 16:54:50 -070073 char_height_(char_height) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070074 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070075
76 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
Tao Bao1fe1afe2018-05-01 15:56:05 -070077 size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_);
78 for (size_t i = 0; i < items_count; ++i) {
79 text_items_.emplace_back(items[i].substr(0, max_item_length_));
Tao Baoe02a5b22018-05-02 15:46:11 -070080 }
81
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());
159 // Show the current menu item number in relation to total number if items don't fit on the
160 // screen.
161 std::string cur_selection_str;
162 if (ItemsOverflow(&cur_selection_str)) {
163 offset += draw_funcs_.DrawTextLine(x, y + offset, cur_selection_str, true);
164 }
165 }
166
167 return offset;
168}
169
170int TextMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
171 int offset = 0;
172
173 draw_funcs_.SetColor(UIElement::MENU);
174 // Do not draw the horizontal rule for wear devices.
175 if (!scrollable()) {
176 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
177 }
178 for (size_t i = MenuStart(); i < MenuEnd(); ++i) {
179 bool bold = false;
180 if (i == selection()) {
181 // Draw the highlight bar.
182 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
183
184 int bar_height = char_height_ + 4;
185 draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height);
186
187 // Bold white text for the selected item.
188 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
189 bold = true;
190 }
191 offset += draw_funcs_.DrawTextLine(x, y + offset, TextItem(i), bold);
192
193 draw_funcs_.SetColor(UIElement::MENU);
194 }
195 offset += draw_funcs_.DrawHorizontalRule(y + offset);
196
197 return offset;
198}
199
Tianjie Xub99e6062018-10-16 15:13:09 -0700200GraphicMenu::GraphicMenu(GRSurface* graphic_headers, const std::vector<GRSurface*>& graphic_items,
201 size_t initial_selection, const DrawInterface& draw_funcs)
Tianjie Xu66dbf632018-10-11 16:54:50 -0700202 : Menu(initial_selection, draw_funcs),
Tianjie Xu66dbf632018-10-11 16:54:50 -0700203 graphic_headers_(graphic_headers),
204 graphic_items_(graphic_items) {}
205
206int GraphicMenu::Select(int sel) {
207 CHECK_LE(graphic_items_.size(), static_cast<size_t>(std::numeric_limits<int>::max()));
208 int count = graphic_items_.size();
209
210 // Wraps the selection at boundary if the menu is not scrollable.
211 if (sel < 0) {
212 selection_ = count - 1;
213 } else if (sel >= count) {
214 selection_ = 0;
215 } else {
216 selection_ = sel;
217 }
218
219 return selection_;
220}
221
222int GraphicMenu::DrawHeader(int x, int y) const {
Tianjie Xub99e6062018-10-16 15:13:09 -0700223 draw_funcs_.SetColor(UIElement::HEADER);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700224 draw_funcs_.DrawTextIcon(x, y, graphic_headers_);
225 return graphic_headers_->height;
226}
227
228int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
229 int offset = 0;
230
231 draw_funcs_.SetColor(UIElement::MENU);
232 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
233
234 for (size_t i = 0; i < graphic_items_.size(); i++) {
235 auto& item = graphic_items_[i];
236 if (i == selection_) {
237 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
238
239 int bar_height = item->height + 4;
240 draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height);
241
242 // Bold white text for the selected item.
243 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
244 }
245 draw_funcs_.DrawTextIcon(x, y + offset, item);
246 offset += item->height;
247
248 draw_funcs_.SetColor(UIElement::MENU);
249 }
250
251 return offset;
252}
253
Tianjie Xub99e6062018-10-16 15:13:09 -0700254bool GraphicMenu::Validate(size_t max_width, size_t max_height, GRSurface* graphic_headers,
255 const std::vector<GRSurface*>& graphic_items) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700256 int offset = 0;
Tianjie Xub99e6062018-10-16 15:13:09 -0700257 if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700258 return false;
259 }
Tianjie Xub99e6062018-10-16 15:13:09 -0700260 offset += graphic_headers->height;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700261
Tianjie Xub99e6062018-10-16 15:13:09 -0700262 for (const auto& item : graphic_items) {
263 if (!ValidateGraphicSurface(max_width, max_height, offset, item)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700264 return false;
265 }
266 offset += item->height;
267 }
268
269 return true;
270}
271
Tianjie Xub99e6062018-10-16 15:13:09 -0700272bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y,
273 const GRSurface* surface) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700274 if (!surface) {
275 fprintf(stderr, "Graphic surface can not be null");
276 return false;
277 }
278
279 if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) {
280 fprintf(stderr, "Invalid graphic surface, pixel bytes: %d, width: %d row_bytes: %d",
281 surface->pixel_bytes, surface->width, surface->row_bytes);
282 return false;
283 }
284
Tianjie Xub99e6062018-10-16 15:13:09 -0700285 if (surface->width > max_width || surface->height > max_height - y) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700286 fprintf(stderr,
287 "Graphic surface doesn't fit into the screen. width: %d, height: %d, max_width: %zu,"
288 " max_height: %zu, vertical offset: %d\n",
Tianjie Xub99e6062018-10-16 15:13:09 -0700289 surface->width, surface->height, max_width, max_height, y);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700290 return false;
291 }
292
293 return true;
294}
295
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700296ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
297
Tao Bao0bc88de2018-07-31 14:53:16 -0700298constexpr int kDefaultMarginHeight = 0;
299constexpr int kDefaultMarginWidth = 0;
300constexpr int kDefaultAnimationFps = 30;
301
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700302ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao0bc88de2018-07-31 14:53:16 -0700303 : margin_width_(
304 android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)),
305 margin_height_(
306 android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)),
307 animation_fps_(
308 android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)),
309 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -0700310 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -0800311 progressBarType(EMPTY),
312 progressScopeStart(0),
313 progressScopeSize(0),
314 progress(0),
315 pagesIdentical(false),
316 text_cols_(0),
317 text_rows_(0),
318 text_(nullptr),
319 text_col_(0),
320 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800321 show_text(false),
322 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700323 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800324 file_viewer_text_(nullptr),
325 intro_frames(0),
326 loop_frames(0),
327 current_frame(0),
328 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800329 stage(-1),
330 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800331 locale_(""),
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700332 rtl_locale_(false) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700333
Tao Bao26ea9592018-05-09 16:32:02 -0700334ScreenRecoveryUI::~ScreenRecoveryUI() {
335 progress_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -0700336 if (progress_thread_.joinable()) {
337 progress_thread_.join();
338 }
Tao Bao60ac6222018-06-13 14:33:51 -0700339 // No-op if gr_init() (via Init()) was not called or had failed.
340 gr_exit();
Tao Bao26ea9592018-05-09 16:32:02 -0700341}
342
Tao Bao99b2d772017-06-23 22:47:03 -0700343GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700344 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
345 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
346 }
347 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700348}
349
Tao Bao99b2d772017-06-23 22:47:03 -0700350GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700351 switch (currentIcon) {
352 case ERASING:
353 return erasing_text;
354 case ERROR:
355 return error_text;
356 case INSTALLING_UPDATE:
357 return installing_text;
358 case NO_COMMAND:
359 return no_command_text;
360 case NONE:
361 abort();
362 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700363}
364
Mikhail Lappob49767c2017-03-23 21:44:26 +0100365int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao0bc88de2018-07-31 14:53:16 -0700366 return dp * density_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700367}
368
369// Here's the intended layout:
370
Elliott Hughes6d089a92016-07-08 17:23:41 -0700371// | portrait large landscape large
372// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700373// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700374// icon | (200dp)
375// gap | 68dp 68dp 56dp 112dp
376// text | (14sp)
377// gap | 32dp 32dp 26dp 52dp
378// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700379// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700380
Tao Bao3250f722017-06-29 14:32:05 -0700381// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
382// 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 -0700383
Elliott Hughes6d089a92016-07-08 17:23:41 -0700384enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700385enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700386static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700387 { 32, 68, }, // PORTRAIT
388 { 32, 68, }, // PORTRAIT_LARGE
389 { 26, 56, }, // LANDSCAPE
390 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700391};
392
Tao Bao99b2d772017-06-23 22:47:03 -0700393int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700394 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700395}
396
Tao Bao99b2d772017-06-23 22:47:03 -0700397int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700398 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
399 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700400}
401
Tao Bao99b2d772017-06-23 22:47:03 -0700402int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700403 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
404 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
405 gr_get_height(progressBarFill);
Luke Song92eda4d2017-09-19 10:51:35 -0700406 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
407 return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700408}
409
Doug Zongker211aebc2011-10-28 15:13:10 -0700410// Clear the screen and draw the currently selected background icon (if any).
411// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700412void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700413 pagesIdentical = false;
414 gr_color(0, 0, 0, 255);
415 gr_clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700416 if (currentIcon != NONE) {
417 if (max_stage != -1) {
418 int stage_height = gr_get_height(stageMarkerEmpty);
419 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700420 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
Tao Bao0bc88de2018-07-31 14:53:16 -0700421 int y = ScreenHeight() - stage_height - margin_height_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700422 for (int i = 0; i < max_stage; ++i) {
423 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700424 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700425 x += stage_width;
426 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700427 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700428
429 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700430 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700431 int text_y = GetTextBaseline();
432 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700433 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700434 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700435}
436
Tao Baoea78d862017-06-28 14:52:17 -0700437// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
438// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700439void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800440 if (currentIcon != NONE) {
441 GRSurface* frame = GetCurrentFrame();
442 int frame_width = gr_get_width(frame);
443 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700444 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800445 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700446 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800447 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700448
Tao Bao736d59c2017-01-03 10:15:33 -0800449 if (progressBarType != EMPTY) {
450 int width = gr_get_width(progressBarEmpty);
451 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700452
Luke Song92eda4d2017-09-19 10:51:35 -0700453 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800454 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700455
Tao Bao736d59c2017-01-03 10:15:33 -0800456 // Erase behind the progress bar (in case this was a progress-only update)
457 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700458 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700459
Tao Bao736d59c2017-01-03 10:15:33 -0800460 if (progressBarType == DETERMINATE) {
461 float p = progressScopeStart + progress * progressScopeSize;
462 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700463
Tao Bao736d59c2017-01-03 10:15:33 -0800464 if (rtl_locale_) {
465 // Fill the progress bar from right to left.
466 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700467 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
468 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700469 }
Tao Bao736d59c2017-01-03 10:15:33 -0800470 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700471 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800472 }
473 } else {
474 // Fill the progress bar from left to right.
475 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700476 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800477 }
478 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700479 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800480 }
481 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700482 }
Tao Bao736d59c2017-01-03 10:15:33 -0800483 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700484}
485
Tao Bao99b2d772017-06-23 22:47:03 -0700486void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700487 switch (e) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700488 case UIElement::INFO:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700489 gr_color(249, 194, 0, 255);
490 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700491 case UIElement::HEADER:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700492 gr_color(247, 0, 6, 255);
493 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700494 case UIElement::MENU:
495 case UIElement::MENU_SEL_BG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700496 gr_color(0, 106, 157, 255);
497 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700498 case UIElement::MENU_SEL_BG_ACTIVE:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700499 gr_color(0, 156, 100, 255);
500 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700501 case UIElement::MENU_SEL_FG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700502 gr_color(255, 255, 255, 255);
503 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700504 case UIElement::LOG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700505 gr_color(196, 196, 196, 255);
506 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700507 case UIElement::TEXT_FILL:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700508 gr_color(0, 0, 0, 160);
509 break;
510 default:
511 gr_color(255, 255, 255, 255);
512 break;
513 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700514}
Doug Zongker211aebc2011-10-28 15:13:10 -0700515
Tianjie Xu29d55752017-09-20 17:53:46 -0700516void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
517 size_t sel) {
518 SetLocale(locales_entries[sel]);
519 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
520 "installing_security_text", "no_command_text" };
521 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
522 for (const auto& name : text_name) {
523 GRSurface* text_image = nullptr;
524 LoadLocalizedBitmap(name.c_str(), &text_image);
525 if (!text_image) {
526 Print("Failed to load %s\n", name.c_str());
527 return;
528 }
529 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
530 }
531
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700532 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu29d55752017-09-20 17:53:46 -0700533 gr_color(0, 0, 0, 255);
534 gr_clear();
535
Tao Bao0bc88de2018-07-31 14:53:16 -0700536 int text_y = margin_height_;
537 int text_x = margin_width_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700538 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
539 // Write the header and descriptive texts.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700540 SetColor(UIElement::INFO);
Tianjie Xu29d55752017-09-20 17:53:46 -0700541 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700542 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700543 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700544 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700545 // clang-format off
546 std::vector<std::string> instruction = {
547 locale_selection,
548 "Use volume up/down to switch locales and power to exit."
549 };
550 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700551 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
552
553 // Iterate through the text images and display them in order for the current locale.
554 for (const auto& p : surfaces) {
555 text_y += line_spacing;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700556 SetColor(UIElement::LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700557 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700558 gr_color(255, 255, 255, 255);
559 gr_texticon(text_x, text_y, p.second.get());
560 text_y += gr_get_height(p.second.get());
561 }
562 // Update the whole screen.
563 gr_flip();
Tianjie Xu29d55752017-09-20 17:53:46 -0700564}
565
Tao Bao39c49182018-05-07 22:50:33 -0700566void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700567 // Load a list of locales embedded in one of the resource files.
568 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
569 if (locales_entries.empty()) {
570 Print("Failed to load locales from the resource files\n");
571 return;
572 }
Tao Bao39c49182018-05-07 22:50:33 -0700573 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700574 size_t selected = 0;
575 SelectAndShowBackgroundText(locales_entries, selected);
576
577 FlushKeys();
578 while (true) {
579 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -0700580 if (key == static_cast<int>(KeyError::INTERRUPTED)) break;
Tianjie Xu29d55752017-09-20 17:53:46 -0700581 if (key == KEY_POWER || key == KEY_ENTER) {
582 break;
583 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
584 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
585 SelectAndShowBackgroundText(locales_entries, selected);
586 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
587 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
588 SelectAndShowBackgroundText(locales_entries, selected);
589 }
590 }
591
592 SetLocale(saved_locale);
593}
594
Luke Song92eda4d2017-09-19 10:51:35 -0700595int ScreenRecoveryUI::ScreenWidth() const {
596 return gr_fb_width();
597}
598
599int ScreenRecoveryUI::ScreenHeight() const {
600 return gr_fb_height();
601}
602
Tao Bao65815b62018-10-23 10:54:02 -0700603void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx,
Luke Song92eda4d2017-09-19 10:51:35 -0700604 int dy) const {
605 gr_blit(surface, sx, sy, w, h, dx, dy);
606}
607
Tao Baoea78d862017-06-28 14:52:17 -0700608int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700609 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700610 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700611}
612
Luke Songe2bd8762017-06-12 16:08:33 -0700613void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700614 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700615}
616
Luke Song92eda4d2017-09-19 10:51:35 -0700617void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
618 gr_fill(x, y, w, h);
619}
620
Tao Bao65815b62018-10-23 10:54:02 -0700621void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700622 gr_texticon(x, y, surface);
623}
624
Tao Bao93e46ad2018-05-02 14:57:21 -0700625int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
626 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700627 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700628}
629
Tao Bao93e46ad2018-05-02 14:57:21 -0700630int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700631 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700632 for (const auto& line : lines) {
633 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700634 }
Tao Baoea78d862017-06-28 14:52:17 -0700635 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700636}
637
Tao Bao93e46ad2018-05-02 14:57:21 -0700638int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
639 const std::vector<std::string>& lines) const {
Tao Bao452b4872018-05-09 11:52:09 -0700640 // Keep symmetrical margins based on the given offset (i.e. x).
641 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700642 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700643 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700644 size_t next_start = 0;
645 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700646 std::string sub = line.substr(next_start, text_cols + 1);
647 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700648 next_start += sub.size();
649 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700650 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700651 size_t last_space = sub.find_last_of(" \t\n");
652 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700653 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700654 sub.resize(text_cols);
655 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700656 } else {
657 sub.resize(last_space);
658 next_start += last_space + 1;
659 }
660 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700661 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700662 }
663 }
664 return offset;
665}
666
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700667void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
668 title_lines_ = lines;
669}
670
Tao Bao171b4c42017-06-19 23:10:44 -0700671// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
672// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700673void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700674 if (!show_text) {
675 draw_background_locked();
676 draw_foreground_locked();
677 return;
678 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700679
Tao Bao171b4c42017-06-19 23:10:44 -0700680 gr_color(0, 0, 0, 255);
681 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700682
Tao Bao93e46ad2018-05-02 14:57:21 -0700683 // clang-format off
Tao Bao1fe1afe2018-05-01 15:56:05 -0700684 static std::vector<std::string> REGULAR_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700685 "Use volume up/down and power.",
686 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700687 static std::vector<std::string> LONG_PRESS_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700688 "Any button cycles highlight.",
689 "Long-press activates.",
690 };
691 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700692 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
693}
694
695// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700696void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
697 const std::vector<std::string>& help_message) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700698 int y = margin_height_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700699 if (menu_) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700700 int x = margin_width_ + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700701
Tianjie Xu66dbf632018-10-11 16:54:50 -0700702 SetColor(UIElement::INFO);
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700703
704 for (size_t i = 0; i < title_lines_.size(); i++) {
705 y += DrawTextLine(x, y, title_lines_[i], i == 0);
Doug Zongker211aebc2011-10-28 15:13:10 -0700706 }
Tao Bao171b4c42017-06-19 23:10:44 -0700707
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700708 y += DrawTextLines(x, y, help_message);
709
Tianjie Xu66dbf632018-10-11 16:54:50 -0700710 y += menu_->DrawHeader(x, y);
711 y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress());
Tao Bao171b4c42017-06-19 23:10:44 -0700712 }
713
714 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
715 // we've displayed the entire text buffer.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700716 SetColor(UIElement::LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700717 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700718 size_t count = 0;
Tao Bao0bc88de2018-07-31 14:53:16 -0700719 for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700720 ty -= char_height_, ++count) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700721 DrawTextLine(margin_width_, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700722 --row;
723 if (row < 0) row = text_rows_ - 1;
724 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700725}
726
727// Redraw everything on the screen and flip the screen (make it visible).
728// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700729void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700730 draw_screen_locked();
731 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700732}
733
734// Updates only the progress bar, if possible, otherwise redraws the screen.
735// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700736void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700737 if (show_text || !pagesIdentical) {
738 draw_screen_locked(); // Must redraw the whole screen
739 pagesIdentical = true;
740 } else {
741 draw_foreground_locked(); // Draw only the progress bar and overlays
742 }
743 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700744}
745
Elliott Hughes985022a2015-04-13 13:04:32 -0700746void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0bc88de2018-07-31 14:53:16 -0700747 double interval = 1.0 / animation_fps_;
Tao Bao26ea9592018-05-09 16:32:02 -0700748 while (!progress_thread_stopped_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700749 double start = now();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700750 bool redraw = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700751 {
752 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700753
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700754 // update the installation animation, if active
755 // skip this if we have a text overlay (too expensive to update)
756 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
757 if (!intro_done) {
758 if (current_frame == intro_frames - 1) {
759 intro_done = true;
760 current_frame = 0;
761 } else {
762 ++current_frame;
763 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700764 } else {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700765 current_frame = (current_frame + 1) % loop_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700766 }
767
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700768 redraw = true;
769 }
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700770
771 // move the progress bar forward on timed intervals, if configured
772 int duration = progressScopeDuration;
773 if (progressBarType == DETERMINATE && duration > 0) {
774 double elapsed = now() - progressScopeTime;
775 float p = 1.0 * elapsed / duration;
776 if (p > 1.0) p = 1.0;
777 if (p > progress) {
778 progress = p;
779 redraw = true;
780 }
781 }
782
783 if (redraw) update_progress_locked();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700784 }
785
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700786 double end = now();
787 // minimum of 20ms delay between frames
788 double delay = interval - (end - start);
789 if (delay < 0.02) delay = 0.02;
790 usleep(static_cast<useconds_t>(delay * 1000000));
791 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700792}
793
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700794void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700795 int result = res_create_display_surface(filename, surface);
796 if (result < 0) {
797 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
798 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800799}
800
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700801void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800802 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
803 if (result < 0) {
804 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
805 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700806}
807
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700808static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700809 char** result = new char*[rows];
810 for (size_t i = 0; i < rows; ++i) {
811 result[i] = new char[cols];
812 memset(result[i], 0, cols);
813 }
814 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700815}
816
Tianjie Xu35926c42016-04-28 18:06:26 -0700817// Choose the right background string to display during update.
818void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700819 if (security_update) {
820 LoadLocalizedBitmap("installing_security_text", &installing_text);
821 } else {
822 LoadLocalizedBitmap("installing_text", &installing_text);
823 }
824 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700825}
826
Sen Jiangd5304492016-12-09 16:20:49 -0800827bool ScreenRecoveryUI::InitTextParams() {
Tao Baoba4edb32018-06-13 11:22:50 -0700828 // gr_init() would return successfully on font initialization failure.
829 if (gr_sys_font() == nullptr) {
Tao Bao171b4c42017-06-19 23:10:44 -0700830 return false;
831 }
Tao Bao171b4c42017-06-19 23:10:44 -0700832 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao0bc88de2018-07-31 14:53:16 -0700833 text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_;
834 text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700835 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700836}
837
Tianjie Xub99e6062018-10-16 15:13:09 -0700838// TODO(xunchang) load localized text icons for the menu. (Init for screenRecoveryUI but
839// not wearRecoveryUI).
840bool ScreenRecoveryUI::LoadWipeDataMenuText() {
841 wipe_data_menu_header_text_ = nullptr;
842 factory_data_reset_text_ = nullptr;
843 try_again_text_ = nullptr;
844
845 return true;
846}
847
Tao Bao736d59c2017-01-03 10:15:33 -0800848bool ScreenRecoveryUI::Init(const std::string& locale) {
849 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800850
Tao Baoba4edb32018-06-13 11:22:50 -0700851 if (gr_init() == -1) {
852 return false;
853 }
854
Tao Bao736d59c2017-01-03 10:15:33 -0800855 if (!InitTextParams()) {
856 return false;
857 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700858
Tao Bao736d59c2017-01-03 10:15:33 -0800859 // Are we portrait or landscape?
860 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
861 // Are we the large variant of our base layout?
862 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700863
Tao Bao736d59c2017-01-03 10:15:33 -0800864 text_ = Alloc2d(text_rows_, text_cols_ + 1);
865 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800866
Tao Bao736d59c2017-01-03 10:15:33 -0800867 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700868
Tao Baoefb49ad2017-01-31 23:03:10 -0800869 // Set up the locale info.
870 SetLocale(locale);
871
Tao Bao736d59c2017-01-03 10:15:33 -0800872 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700873
Tao Bao736d59c2017-01-03 10:15:33 -0800874 LoadBitmap("progress_empty", &progressBarEmpty);
875 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700876
Tao Bao736d59c2017-01-03 10:15:33 -0800877 LoadBitmap("stage_empty", &stageMarkerEmpty);
878 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700879
Tao Bao736d59c2017-01-03 10:15:33 -0800880 // Background text for "installing_update" could be "installing update"
881 // or "installing security update". It will be set after UI init according
882 // to commands in BCB.
883 installing_text = nullptr;
884 LoadLocalizedBitmap("erasing_text", &erasing_text);
885 LoadLocalizedBitmap("no_command_text", &no_command_text);
886 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700887
Tianjie Xub99e6062018-10-16 15:13:09 -0700888 LoadWipeDataMenuText();
889
Tao Bao736d59c2017-01-03 10:15:33 -0800890 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700891
Tao Bao26ea9592018-05-09 16:32:02 -0700892 // Keep the progress bar updated, even when the process is otherwise busy.
893 progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800894
Tao Bao736d59c2017-01-03 10:15:33 -0800895 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700896}
897
Tao Bao551d2c32018-05-09 20:53:13 -0700898std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700899 return locale_;
900}
901
Elliott Hughes498cda62016-04-14 16:49:04 -0700902void ScreenRecoveryUI::LoadAnimation() {
Tao Bao6cd81682018-05-03 21:53:11 -0700903 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
904 closedir);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700905 dirent* de;
906 std::vector<std::string> intro_frame_names;
907 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700908
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700909 while ((de = readdir(dir.get())) != nullptr) {
910 int value, num_chars;
911 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
912 intro_frame_names.emplace_back(de->d_name, num_chars);
913 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
914 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700915 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700916 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700917
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700918 intro_frames = intro_frame_names.size();
919 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700920
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700921 // It's okay to not have an intro.
922 if (intro_frames == 0) intro_done = true;
923 // But you must have an animation.
924 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700925
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700926 std::sort(intro_frame_names.begin(), intro_frame_names.end());
927 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700928
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700929 introFrames = new GRSurface*[intro_frames];
930 for (size_t i = 0; i < intro_frames; i++) {
931 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
932 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700933
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700934 loopFrames = new GRSurface*[loop_frames];
935 for (size_t i = 0; i < loop_frames; i++) {
936 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
937 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700938}
939
Elliott Hughes8de52072015-04-08 20:06:50 -0700940void ScreenRecoveryUI::SetBackground(Icon icon) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700941 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700942
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700943 currentIcon = icon;
944 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700945}
946
Elliott Hughes8de52072015-04-08 20:06:50 -0700947void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700948 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700949 if (progressBarType != type) {
950 progressBarType = type;
951 }
952 progressScopeStart = 0;
953 progressScopeSize = 0;
954 progress = 0;
955 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700956}
957
Elliott Hughes8de52072015-04-08 20:06:50 -0700958void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700959 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700960 progressBarType = DETERMINATE;
961 progressScopeStart += progressScopeSize;
962 progressScopeSize = portion;
963 progressScopeTime = now();
964 progressScopeDuration = seconds;
965 progress = 0;
966 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700967}
968
Elliott Hughes8de52072015-04-08 20:06:50 -0700969void ScreenRecoveryUI::SetProgress(float fraction) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700970 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700971 if (fraction < 0.0) fraction = 0.0;
972 if (fraction > 1.0) fraction = 1.0;
973 if (progressBarType == DETERMINATE && fraction > progress) {
974 // Skip updates that aren't visibly different.
975 int width = gr_get_width(progressBarEmpty);
976 float scale = width * progressScopeSize;
977 if ((int)(progress * scale) != (int)(fraction * scale)) {
978 progress = fraction;
979 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700980 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700981 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700982}
983
Doug Zongkerc87bab12013-11-25 13:53:25 -0800984void ScreenRecoveryUI::SetStage(int current, int max) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700985 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700986 stage = current;
987 max_stage = max;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800988}
989
Tao Baob6918c72015-05-19 17:02:16 -0700990void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700991 std::string str;
992 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700993
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700994 if (copy_to_stdout) {
995 fputs(str.c_str(), stdout);
996 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700997
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700998 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700999 if (text_rows_ > 0 && text_cols_ > 0) {
1000 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
1001 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -07001002 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001003 text_col_ = 0;
1004 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001005 }
1006 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001007 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001008 text_[text_row_][text_col_] = '\0';
1009 update_screen_locked();
1010 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001011}
1012
Tao Baob6918c72015-05-19 17:02:16 -07001013void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001014 va_list ap;
1015 va_start(ap, fmt);
1016 PrintV(fmt, true, ap);
1017 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001018}
1019
1020void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001021 va_list ap;
1022 va_start(ap, fmt);
1023 PrintV(fmt, false, ap);
1024 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001025}
1026
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001027void ScreenRecoveryUI::PutChar(char ch) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001028 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001029 if (ch != '\n') text_[text_row_][text_col_++] = ch;
1030 if (ch == '\n' || text_col_ >= text_cols_) {
1031 text_col_ = 0;
1032 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001033 }
Elliott Hughes8de52072015-04-08 20:06:50 -07001034}
1035
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001036void ScreenRecoveryUI::ClearText() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001037 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001038 text_col_ = 0;
1039 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001040 for (size_t i = 0; i < text_rows_; ++i) {
1041 memset(text_[i], 0, text_cols_ + 1);
1042 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001043}
1044
1045void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001046 std::vector<off_t> offsets;
1047 offsets.push_back(ftello(fp));
1048 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001049
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001050 struct stat sb;
1051 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001052
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001053 bool show_prompt = false;
1054 while (true) {
1055 if (show_prompt) {
1056 PrintOnScreenOnly("--(%d%% of %d bytes)--",
1057 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
1058 static_cast<int>(sb.st_size));
1059 Redraw();
1060 while (show_prompt) {
1061 show_prompt = false;
1062 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001063 if (key == static_cast<int>(KeyError::INTERRUPTED)) return;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001064 if (key == KEY_POWER || key == KEY_ENTER) {
1065 return;
1066 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
1067 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001068 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001069 } else {
1070 offsets.pop_back();
1071 fseek(fp, offsets.back(), SEEK_SET);
1072 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001073 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001074 if (feof(fp)) {
1075 return;
1076 }
1077 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001078 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001079 }
1080 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001081 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001082
1083 int ch = getc(fp);
1084 if (ch == EOF) {
1085 while (text_row_ < text_rows_ - 1) PutChar('\n');
1086 show_prompt = true;
1087 } else {
1088 PutChar(ch);
1089 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
1090 show_prompt = true;
1091 }
1092 }
1093 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001094}
1095
Tao Bao1d156b92018-05-02 12:43:18 -07001096void ScreenRecoveryUI::ShowFile(const std::string& filename) {
1097 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
1098 if (!fp) {
1099 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001100 return;
1101 }
Elliott Hughesc0491632015-05-06 12:40:05 -07001102
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001103 char** old_text = text_;
1104 size_t old_text_col = text_col_;
1105 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -07001106
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001107 // Swap in the alternate screen and clear it.
1108 text_ = file_viewer_text_;
1109 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -07001110
Tao Bao1d156b92018-05-02 12:43:18 -07001111 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -07001112
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001113 text_ = old_text;
1114 text_col_ = old_text_col;
1115 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -07001116}
1117
Tianjie Xub99e6062018-10-16 15:13:09 -07001118std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(GRSurface* graphic_header,
1119 const std::vector<GRSurface*>& graphic_items,
1120 const std::vector<std::string>& text_headers,
1121 const std::vector<std::string>& text_items,
1122 size_t initial_selection) const {
1123 // horizontal unusable area: margin width + menu indent
1124 size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent;
1125 // vertical unusable area: margin height + title lines + helper message + high light bar.
1126 // It is safe to reserve more space.
1127 size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3);
1128 if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) {
1129 return std::make_unique<GraphicMenu>(graphic_header, graphic_items, initial_selection, *this);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001130 }
Tianjie Xub99e6062018-10-16 15:13:09 -07001131
1132 fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n");
1133
1134 return CreateMenu(text_headers, text_items, initial_selection);
1135}
1136
1137std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(const std::vector<std::string>& text_headers,
1138 const std::vector<std::string>& text_items,
1139 size_t initial_selection) const {
1140 if (text_rows_ > 0 && text_cols_ > 1) {
1141 return std::make_unique<TextMenu>(scrollable_menu_, text_rows_, text_cols_ - 1, text_headers,
1142 text_items, initial_selection, char_height_, *this);
1143 }
1144
1145 fprintf(stderr, "Failed to create text menu, text_rows %zu, text_cols %zu.\n", text_rows_,
1146 text_cols_);
1147 return nullptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001148}
1149
1150int ScreenRecoveryUI::SelectMenu(int sel) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001151 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001152 if (menu_) {
1153 int old_sel = menu_->selection();
1154 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001155
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001156 if (sel != old_sel) {
1157 update_screen_locked();
1158 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001159 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001160 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001161}
1162
Tianjie Xub99e6062018-10-16 15:13:09 -07001163size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr<Menu>&& menu, bool menu_only,
Tao Bao1fe1afe2018-05-01 15:56:05 -07001164 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001165 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1166 FlushKeys();
1167
Jerry Zhangb76af932018-05-22 12:08:35 -07001168 // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the
1169 // menu.
1170 if (IsKeyInterrupted()) return static_cast<size_t>(KeyError::INTERRUPTED);
1171
Tianjie Xub99e6062018-10-16 15:13:09 -07001172 CHECK(menu != nullptr);
Tao Bao3aec6962018-04-20 09:24:58 -07001173
Tianjie Xub99e6062018-10-16 15:13:09 -07001174 // Starts and displays the menu
1175 menu_ = std::move(menu);
1176 Redraw();
1177
1178 int selected = menu_->selection();
Tao Bao3aec6962018-04-20 09:24:58 -07001179 int chosen_item = -1;
1180 while (chosen_item < 0) {
1181 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001182 if (key == static_cast<int>(KeyError::INTERRUPTED)) { // WaitKey() was interrupted.
1183 return static_cast<size_t>(KeyError::INTERRUPTED);
1184 }
1185 if (key == static_cast<int>(KeyError::TIMED_OUT)) { // WaitKey() timed out.
Tao Bao3aec6962018-04-20 09:24:58 -07001186 if (WasTextEverVisible()) {
1187 continue;
1188 } else {
1189 LOG(INFO) << "Timed out waiting for key input; rebooting.";
Tianjie Xub99e6062018-10-16 15:13:09 -07001190 menu_.reset();
1191 Redraw();
Jerry Zhangb76af932018-05-22 12:08:35 -07001192 return static_cast<size_t>(KeyError::TIMED_OUT);
Tao Bao3aec6962018-04-20 09:24:58 -07001193 }
1194 }
1195
1196 bool visible = IsTextVisible();
1197 int action = key_handler(key, visible);
1198 if (action < 0) {
1199 switch (action) {
1200 case Device::kHighlightUp:
1201 selected = SelectMenu(--selected);
1202 break;
1203 case Device::kHighlightDown:
1204 selected = SelectMenu(++selected);
1205 break;
1206 case Device::kInvokeItem:
1207 chosen_item = selected;
1208 break;
1209 case Device::kNoAction:
1210 break;
1211 }
1212 } else if (!menu_only) {
1213 chosen_item = action;
1214 }
1215 }
1216
Tianjie Xub99e6062018-10-16 15:13:09 -07001217 menu_.reset();
1218 Redraw();
1219
Tao Bao3aec6962018-04-20 09:24:58 -07001220 return chosen_item;
1221}
1222
Tianjie Xub99e6062018-10-16 15:13:09 -07001223size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1224 const std::vector<std::string>& items, size_t initial_selection,
1225 bool menu_only,
1226 const std::function<int(int, bool)>& key_handler) {
1227 auto menu = CreateMenu(headers, items, initial_selection);
1228 if (menu == nullptr) {
1229 return initial_selection;
1230 }
1231
1232 return ShowMenu(CreateMenu(headers, items, initial_selection), menu_only, key_handler);
1233}
1234
1235size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector<std::string>& backup_headers,
1236 const std::vector<std::string>& backup_items,
1237 const std::function<int(int, bool)>& key_handler) {
1238 auto wipe_data_menu =
1239 CreateMenu(wipe_data_menu_header_text_, { try_again_text_, factory_data_reset_text_ },
1240 backup_headers, backup_items, 0);
1241 if (wipe_data_menu == nullptr) {
1242 return 0;
1243 }
1244
1245 return ShowMenu(std::move(wipe_data_menu), true, key_handler);
1246}
1247
Elliott Hughes8de52072015-04-08 20:06:50 -07001248bool ScreenRecoveryUI::IsTextVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001249 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001250 int visible = show_text;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001251 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001252}
1253
Elliott Hughes8de52072015-04-08 20:06:50 -07001254bool ScreenRecoveryUI::WasTextEverVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001255 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001256 int ever_visible = show_text_ever;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001257 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001258}
1259
Elliott Hughes8de52072015-04-08 20:06:50 -07001260void ScreenRecoveryUI::ShowText(bool visible) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001261 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001262 show_text = visible;
1263 if (show_text) show_text_ever = true;
1264 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001265}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001266
Elliott Hughes8de52072015-04-08 20:06:50 -07001267void ScreenRecoveryUI::Redraw() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001268 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001269 update_screen_locked();
Doug Zongkerc0441d12013-07-31 11:28:24 -07001270}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001271
1272void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001273 // Redraw so that if we're in the menu, the highlight
1274 // will change color to indicate a successful long press.
1275 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001276}
Tao Baoefb49ad2017-01-31 23:03:10 -08001277
1278void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1279 locale_ = new_locale;
1280 rtl_locale_ = false;
1281
1282 if (!new_locale.empty()) {
Tao Bao347a6592018-05-08 15:58:29 -07001283 size_t separator = new_locale.find('-');
1284 // lang has the language prefix prior to the separator, or full string if none exists.
1285 std::string lang = new_locale.substr(0, separator);
Tao Baoefb49ad2017-01-31 23:03:10 -08001286
1287 // A bit cheesy: keep an explicit list of supported RTL languages.
1288 if (lang == "ar" || // Arabic
1289 lang == "fa" || // Persian (Farsi)
1290 lang == "he" || // Hebrew (new language code)
1291 lang == "iw" || // Hebrew (old language code)
1292 lang == "ur") { // Urdu
1293 rtl_locale_ = true;
1294 }
1295 }
1296}