blob: ed71888d128150a9ff9065654d3b2d1856e2c5e6 [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
Tao Baoda409fb2018-10-21 23:36:26 -0700200GraphicMenu::GraphicMenu(const GRSurface* graphic_headers,
201 const std::vector<const GRSurface*>& graphic_items,
Tianjie Xub99e6062018-10-16 15:13:09 -0700202 size_t initial_selection, const DrawInterface& draw_funcs)
Tao Baoda409fb2018-10-21 23:36:26 -0700203 : Menu(initial_selection, draw_funcs) {
204 graphic_headers_ = graphic_headers->Clone();
205 graphic_items_.reserve(graphic_items.size());
206 for (const auto& item : graphic_items) {
207 graphic_items_.emplace_back(item->Clone());
208 }
209}
Tianjie Xu66dbf632018-10-11 16:54:50 -0700210
211int GraphicMenu::Select(int sel) {
212 CHECK_LE(graphic_items_.size(), static_cast<size_t>(std::numeric_limits<int>::max()));
213 int count = graphic_items_.size();
214
215 // Wraps the selection at boundary if the menu is not scrollable.
216 if (sel < 0) {
217 selection_ = count - 1;
218 } else if (sel >= count) {
219 selection_ = 0;
220 } else {
221 selection_ = sel;
222 }
223
224 return selection_;
225}
226
227int GraphicMenu::DrawHeader(int x, int y) const {
Tianjie Xub99e6062018-10-16 15:13:09 -0700228 draw_funcs_.SetColor(UIElement::HEADER);
Tao Baoda409fb2018-10-21 23:36:26 -0700229 draw_funcs_.DrawTextIcon(x, y, graphic_headers_.get());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700230 return graphic_headers_->height;
231}
232
233int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
234 int offset = 0;
235
236 draw_funcs_.SetColor(UIElement::MENU);
237 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
238
239 for (size_t i = 0; i < graphic_items_.size(); i++) {
240 auto& item = graphic_items_[i];
241 if (i == selection_) {
242 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
243
244 int bar_height = item->height + 4;
245 draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height);
246
247 // Bold white text for the selected item.
248 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
249 }
Tao Baoda409fb2018-10-21 23:36:26 -0700250 draw_funcs_.DrawTextIcon(x, y + offset, item.get());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700251 offset += item->height;
252
253 draw_funcs_.SetColor(UIElement::MENU);
254 }
255
256 return offset;
257}
258
Tao Baoda409fb2018-10-21 23:36:26 -0700259bool GraphicMenu::Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers,
260 const std::vector<const GRSurface*>& graphic_items) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700261 int offset = 0;
Tianjie Xub99e6062018-10-16 15:13:09 -0700262 if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700263 return false;
264 }
Tianjie Xub99e6062018-10-16 15:13:09 -0700265 offset += graphic_headers->height;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700266
Tianjie Xub99e6062018-10-16 15:13:09 -0700267 for (const auto& item : graphic_items) {
268 if (!ValidateGraphicSurface(max_width, max_height, offset, item)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700269 return false;
270 }
271 offset += item->height;
272 }
273
274 return true;
275}
276
Tianjie Xub99e6062018-10-16 15:13:09 -0700277bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y,
278 const GRSurface* surface) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700279 if (!surface) {
280 fprintf(stderr, "Graphic surface can not be null");
281 return false;
282 }
283
284 if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) {
285 fprintf(stderr, "Invalid graphic surface, pixel bytes: %d, width: %d row_bytes: %d",
286 surface->pixel_bytes, surface->width, surface->row_bytes);
287 return false;
288 }
289
Tianjie Xub99e6062018-10-16 15:13:09 -0700290 if (surface->width > max_width || surface->height > max_height - y) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700291 fprintf(stderr,
292 "Graphic surface doesn't fit into the screen. width: %d, height: %d, max_width: %zu,"
293 " max_height: %zu, vertical offset: %d\n",
Tianjie Xub99e6062018-10-16 15:13:09 -0700294 surface->width, surface->height, max_width, max_height, y);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700295 return false;
296 }
297
298 return true;
299}
300
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700301ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
302
Tao Bao0bc88de2018-07-31 14:53:16 -0700303constexpr int kDefaultMarginHeight = 0;
304constexpr int kDefaultMarginWidth = 0;
305constexpr int kDefaultAnimationFps = 30;
306
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700307ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao0bc88de2018-07-31 14:53:16 -0700308 : margin_width_(
309 android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)),
310 margin_height_(
311 android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)),
312 animation_fps_(
313 android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)),
314 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Baoda409fb2018-10-21 23:36:26 -0700315 current_icon_(NONE),
316 current_frame_(0),
317 intro_done_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800318 progressBarType(EMPTY),
319 progressScopeStart(0),
320 progressScopeSize(0),
321 progress(0),
322 pagesIdentical(false),
323 text_cols_(0),
324 text_rows_(0),
325 text_(nullptr),
326 text_col_(0),
327 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800328 show_text(false),
329 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700330 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800331 file_viewer_text_(nullptr),
Tao Bao736d59c2017-01-03 10:15:33 -0800332 stage(-1),
333 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800334 locale_(""),
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700335 rtl_locale_(false) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700336
Tao Bao26ea9592018-05-09 16:32:02 -0700337ScreenRecoveryUI::~ScreenRecoveryUI() {
338 progress_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -0700339 if (progress_thread_.joinable()) {
340 progress_thread_.join();
341 }
Tao Bao60ac6222018-06-13 14:33:51 -0700342 // No-op if gr_init() (via Init()) was not called or had failed.
343 gr_exit();
Tao Bao26ea9592018-05-09 16:32:02 -0700344}
345
Tao Baoda409fb2018-10-21 23:36:26 -0700346const GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
347 if (current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) {
348 return intro_done_ ? loop_frames_[current_frame_].get() : intro_frames_[current_frame_].get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700349 }
Tao Baoda409fb2018-10-21 23:36:26 -0700350 return error_icon_.get();
Elliott Hughes498cda62016-04-14 16:49:04 -0700351}
352
Tao Baoda409fb2018-10-21 23:36:26 -0700353const GRSurface* ScreenRecoveryUI::GetCurrentText() const {
354 switch (current_icon_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700355 case ERASING:
Tao Baoda409fb2018-10-21 23:36:26 -0700356 return erasing_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700357 case ERROR:
Tao Baoda409fb2018-10-21 23:36:26 -0700358 return error_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700359 case INSTALLING_UPDATE:
Tao Baoda409fb2018-10-21 23:36:26 -0700360 return installing_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700361 case NO_COMMAND:
Tao Baoda409fb2018-10-21 23:36:26 -0700362 return no_command_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700363 case NONE:
364 abort();
365 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700366}
367
Mikhail Lappob49767c2017-03-23 21:44:26 +0100368int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao0bc88de2018-07-31 14:53:16 -0700369 return dp * density_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700370}
371
372// Here's the intended layout:
373
Elliott Hughes6d089a92016-07-08 17:23:41 -0700374// | portrait large landscape large
375// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700376// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700377// icon | (200dp)
378// gap | 68dp 68dp 56dp 112dp
379// text | (14sp)
380// gap | 32dp 32dp 26dp 52dp
381// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700382// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700383
Tao Bao3250f722017-06-29 14:32:05 -0700384// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
385// 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 -0700386
Elliott Hughes6d089a92016-07-08 17:23:41 -0700387enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700388enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700389static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700390 { 32, 68, }, // PORTRAIT
391 { 32, 68, }, // PORTRAIT_LARGE
392 { 26, 56, }, // LANDSCAPE
393 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700394};
395
Tao Bao99b2d772017-06-23 22:47:03 -0700396int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Baoda409fb2018-10-21 23:36:26 -0700397 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) -
398 gr_get_height(loop_frames_[0].get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700399}
400
Tao Bao99b2d772017-06-23 22:47:03 -0700401int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700402 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
Tao Baoda409fb2018-10-21 23:36:26 -0700403 gr_get_height(installing_text_.get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700404}
405
Tao Bao99b2d772017-06-23 22:47:03 -0700406int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Baoda409fb2018-10-21 23:36:26 -0700407 int elements_sum = gr_get_height(loop_frames_[0].get()) + PixelsFromDp(kLayouts[layout_][ICON]) +
408 gr_get_height(installing_text_.get()) + PixelsFromDp(kLayouts[layout_][TEXT]) +
409 gr_get_height(progress_bar_fill_.get());
Luke Song92eda4d2017-09-19 10:51:35 -0700410 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
Tao Baoda409fb2018-10-21 23:36:26 -0700411 return ScreenHeight() - bottom_gap - gr_get_height(progress_bar_fill_.get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700412}
413
Doug Zongker211aebc2011-10-28 15:13:10 -0700414// Clear the screen and draw the currently selected background icon (if any).
415// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700416void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700417 pagesIdentical = false;
418 gr_color(0, 0, 0, 255);
419 gr_clear();
Tao Baoda409fb2018-10-21 23:36:26 -0700420 if (current_icon_ != NONE) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700421 if (max_stage != -1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700422 int stage_height = gr_get_height(stage_marker_empty_.get());
423 int stage_width = gr_get_width(stage_marker_empty_.get());
424 int x = (ScreenWidth() - max_stage * gr_get_width(stage_marker_empty_.get())) / 2;
Tao Bao0bc88de2018-07-31 14:53:16 -0700425 int y = ScreenHeight() - stage_height - margin_height_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700426 for (int i = 0; i < max_stage; ++i) {
Tao Baoda409fb2018-10-21 23:36:26 -0700427 const auto& stage_surface = (i < stage) ? stage_marker_fill_ : stage_marker_empty_;
428 DrawSurface(stage_surface.get(), 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700429 x += stage_width;
430 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700431 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700432
Tao Baoda409fb2018-10-21 23:36:26 -0700433 const auto& text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700434 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700435 int text_y = GetTextBaseline();
436 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700437 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700438 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700439}
440
Tao Baoea78d862017-06-28 14:52:17 -0700441// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
442// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700443void ScreenRecoveryUI::draw_foreground_locked() {
Tao Baoda409fb2018-10-21 23:36:26 -0700444 if (current_icon_ != NONE) {
445 const auto& frame = GetCurrentFrame();
Tao Bao736d59c2017-01-03 10:15:33 -0800446 int frame_width = gr_get_width(frame);
447 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700448 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800449 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700450 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800451 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700452
Tao Bao736d59c2017-01-03 10:15:33 -0800453 if (progressBarType != EMPTY) {
Tao Baoda409fb2018-10-21 23:36:26 -0700454 int width = gr_get_width(progress_bar_empty_.get());
455 int height = gr_get_height(progress_bar_empty_.get());
Doug Zongker211aebc2011-10-28 15:13:10 -0700456
Luke Song92eda4d2017-09-19 10:51:35 -0700457 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800458 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700459
Tao Bao736d59c2017-01-03 10:15:33 -0800460 // Erase behind the progress bar (in case this was a progress-only update)
461 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700462 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700463
Tao Bao736d59c2017-01-03 10:15:33 -0800464 if (progressBarType == DETERMINATE) {
465 float p = progressScopeStart + progress * progressScopeSize;
466 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700467
Tao Bao736d59c2017-01-03 10:15:33 -0800468 if (rtl_locale_) {
469 // Fill the progress bar from right to left.
470 if (pos > 0) {
Tao Baoda409fb2018-10-21 23:36:26 -0700471 DrawSurface(progress_bar_fill_.get(), width - pos, 0, pos, height,
472 progress_x + width - pos, progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700473 }
Tao Bao736d59c2017-01-03 10:15:33 -0800474 if (pos < width - 1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700475 DrawSurface(progress_bar_empty_.get(), 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800476 }
477 } else {
478 // Fill the progress bar from left to right.
479 if (pos > 0) {
Tao Baoda409fb2018-10-21 23:36:26 -0700480 DrawSurface(progress_bar_fill_.get(), 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800481 }
482 if (pos < width - 1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700483 DrawSurface(progress_bar_empty_.get(), pos, 0, width - pos, height, progress_x + pos,
484 progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800485 }
486 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700487 }
Tao Bao736d59c2017-01-03 10:15:33 -0800488 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700489}
490
Tao Bao99b2d772017-06-23 22:47:03 -0700491void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700492 switch (e) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700493 case UIElement::INFO:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700494 gr_color(249, 194, 0, 255);
495 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700496 case UIElement::HEADER:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700497 gr_color(247, 0, 6, 255);
498 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700499 case UIElement::MENU:
500 case UIElement::MENU_SEL_BG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700501 gr_color(0, 106, 157, 255);
502 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700503 case UIElement::MENU_SEL_BG_ACTIVE:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700504 gr_color(0, 156, 100, 255);
505 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700506 case UIElement::MENU_SEL_FG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700507 gr_color(255, 255, 255, 255);
508 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700509 case UIElement::LOG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700510 gr_color(196, 196, 196, 255);
511 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700512 case UIElement::TEXT_FILL:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700513 gr_color(0, 0, 0, 160);
514 break;
515 default:
516 gr_color(255, 255, 255, 255);
517 break;
518 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700519}
Doug Zongker211aebc2011-10-28 15:13:10 -0700520
Tianjie Xu29d55752017-09-20 17:53:46 -0700521void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
522 size_t sel) {
523 SetLocale(locales_entries[sel]);
524 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
525 "installing_security_text", "no_command_text" };
Tao Baoda409fb2018-10-21 23:36:26 -0700526 std::unordered_map<std::string, std::unique_ptr<GRSurface>> surfaces;
Tianjie Xu29d55752017-09-20 17:53:46 -0700527 for (const auto& name : text_name) {
Tao Baoda409fb2018-10-21 23:36:26 -0700528 auto text_image = LoadLocalizedBitmap(name);
Tianjie Xu29d55752017-09-20 17:53:46 -0700529 if (!text_image) {
530 Print("Failed to load %s\n", name.c_str());
531 return;
532 }
Tao Baoda409fb2018-10-21 23:36:26 -0700533 surfaces.emplace(name, std::move(text_image));
Tianjie Xu29d55752017-09-20 17:53:46 -0700534 }
535
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700536 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu29d55752017-09-20 17:53:46 -0700537 gr_color(0, 0, 0, 255);
538 gr_clear();
539
Tao Bao0bc88de2018-07-31 14:53:16 -0700540 int text_y = margin_height_;
541 int text_x = margin_width_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700542 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
543 // Write the header and descriptive texts.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700544 SetColor(UIElement::INFO);
Tianjie Xu29d55752017-09-20 17:53:46 -0700545 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700546 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700547 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700548 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700549 // clang-format off
550 std::vector<std::string> instruction = {
551 locale_selection,
552 "Use volume up/down to switch locales and power to exit."
553 };
554 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700555 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
556
557 // Iterate through the text images and display them in order for the current locale.
558 for (const auto& p : surfaces) {
559 text_y += line_spacing;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700560 SetColor(UIElement::LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700561 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700562 gr_color(255, 255, 255, 255);
563 gr_texticon(text_x, text_y, p.second.get());
564 text_y += gr_get_height(p.second.get());
565 }
566 // Update the whole screen.
567 gr_flip();
Tianjie Xu29d55752017-09-20 17:53:46 -0700568}
569
Tao Bao39c49182018-05-07 22:50:33 -0700570void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700571 // Load a list of locales embedded in one of the resource files.
572 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
573 if (locales_entries.empty()) {
574 Print("Failed to load locales from the resource files\n");
575 return;
576 }
Tao Bao39c49182018-05-07 22:50:33 -0700577 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700578 size_t selected = 0;
579 SelectAndShowBackgroundText(locales_entries, selected);
580
581 FlushKeys();
582 while (true) {
583 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -0700584 if (key == static_cast<int>(KeyError::INTERRUPTED)) break;
Tianjie Xu29d55752017-09-20 17:53:46 -0700585 if (key == KEY_POWER || key == KEY_ENTER) {
586 break;
587 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
588 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
589 SelectAndShowBackgroundText(locales_entries, selected);
590 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
591 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
592 SelectAndShowBackgroundText(locales_entries, selected);
593 }
594 }
595
596 SetLocale(saved_locale);
597}
598
Luke Song92eda4d2017-09-19 10:51:35 -0700599int ScreenRecoveryUI::ScreenWidth() const {
600 return gr_fb_width();
601}
602
603int ScreenRecoveryUI::ScreenHeight() const {
604 return gr_fb_height();
605}
606
Tao Bao65815b62018-10-23 10:54:02 -0700607void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx,
Luke Song92eda4d2017-09-19 10:51:35 -0700608 int dy) const {
609 gr_blit(surface, sx, sy, w, h, dx, dy);
610}
611
Tao Baoea78d862017-06-28 14:52:17 -0700612int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700613 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700614 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700615}
616
Luke Songe2bd8762017-06-12 16:08:33 -0700617void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700618 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700619}
620
Luke Song92eda4d2017-09-19 10:51:35 -0700621void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
622 gr_fill(x, y, w, h);
623}
624
Tao Bao65815b62018-10-23 10:54:02 -0700625void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700626 gr_texticon(x, y, surface);
627}
628
Tao Bao93e46ad2018-05-02 14:57:21 -0700629int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
630 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700631 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700632}
633
Tao Bao93e46ad2018-05-02 14:57:21 -0700634int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700635 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700636 for (const auto& line : lines) {
637 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700638 }
Tao Baoea78d862017-06-28 14:52:17 -0700639 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700640}
641
Tao Bao93e46ad2018-05-02 14:57:21 -0700642int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
643 const std::vector<std::string>& lines) const {
Tao Bao452b4872018-05-09 11:52:09 -0700644 // Keep symmetrical margins based on the given offset (i.e. x).
645 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700646 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700647 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700648 size_t next_start = 0;
649 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700650 std::string sub = line.substr(next_start, text_cols + 1);
651 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700652 next_start += sub.size();
653 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700654 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700655 size_t last_space = sub.find_last_of(" \t\n");
656 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700657 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700658 sub.resize(text_cols);
659 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700660 } else {
661 sub.resize(last_space);
662 next_start += last_space + 1;
663 }
664 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700665 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700666 }
667 }
668 return offset;
669}
670
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700671void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
672 title_lines_ = lines;
673}
674
Tao Bao171b4c42017-06-19 23:10:44 -0700675// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
676// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700677void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700678 if (!show_text) {
679 draw_background_locked();
680 draw_foreground_locked();
681 return;
682 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700683
Tao Bao171b4c42017-06-19 23:10:44 -0700684 gr_color(0, 0, 0, 255);
685 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700686
Tao Bao93e46ad2018-05-02 14:57:21 -0700687 // clang-format off
Tao Bao1fe1afe2018-05-01 15:56:05 -0700688 static std::vector<std::string> REGULAR_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700689 "Use volume up/down and power.",
690 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700691 static std::vector<std::string> LONG_PRESS_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700692 "Any button cycles highlight.",
693 "Long-press activates.",
694 };
695 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700696 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
697}
698
699// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700700void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
701 const std::vector<std::string>& help_message) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700702 int y = margin_height_;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700703 if (menu_) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700704 int x = margin_width_ + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700705
Tianjie Xu66dbf632018-10-11 16:54:50 -0700706 SetColor(UIElement::INFO);
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700707
708 for (size_t i = 0; i < title_lines_.size(); i++) {
709 y += DrawTextLine(x, y, title_lines_[i], i == 0);
Doug Zongker211aebc2011-10-28 15:13:10 -0700710 }
Tao Bao171b4c42017-06-19 23:10:44 -0700711
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700712 y += DrawTextLines(x, y, help_message);
713
Tianjie Xu66dbf632018-10-11 16:54:50 -0700714 y += menu_->DrawHeader(x, y);
715 y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress());
Tao Bao171b4c42017-06-19 23:10:44 -0700716 }
717
718 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
719 // we've displayed the entire text buffer.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700720 SetColor(UIElement::LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700721 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700722 size_t count = 0;
Tao Bao0bc88de2018-07-31 14:53:16 -0700723 for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700724 ty -= char_height_, ++count) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700725 DrawTextLine(margin_width_, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700726 --row;
727 if (row < 0) row = text_rows_ - 1;
728 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700729}
730
731// Redraw everything on the screen and flip the screen (make it visible).
732// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700733void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700734 draw_screen_locked();
735 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700736}
737
738// Updates only the progress bar, if possible, otherwise redraws the screen.
739// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700740void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700741 if (show_text || !pagesIdentical) {
742 draw_screen_locked(); // Must redraw the whole screen
743 pagesIdentical = true;
744 } else {
745 draw_foreground_locked(); // Draw only the progress bar and overlays
746 }
747 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700748}
749
Elliott Hughes985022a2015-04-13 13:04:32 -0700750void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0bc88de2018-07-31 14:53:16 -0700751 double interval = 1.0 / animation_fps_;
Tao Bao26ea9592018-05-09 16:32:02 -0700752 while (!progress_thread_stopped_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700753 double start = now();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700754 bool redraw = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700755 {
756 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700757
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700758 // update the installation animation, if active
759 // skip this if we have a text overlay (too expensive to update)
Tao Baoda409fb2018-10-21 23:36:26 -0700760 if ((current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) && !show_text) {
761 if (!intro_done_) {
762 if (current_frame_ == intro_frames_.size() - 1) {
763 intro_done_ = true;
764 current_frame_ = 0;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700765 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700766 ++current_frame_;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700767 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700768 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700769 current_frame_ = (current_frame_ + 1) % loop_frames_.size();
Doug Zongker211aebc2011-10-28 15:13:10 -0700770 }
771
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700772 redraw = true;
773 }
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700774
775 // move the progress bar forward on timed intervals, if configured
776 int duration = progressScopeDuration;
777 if (progressBarType == DETERMINATE && duration > 0) {
778 double elapsed = now() - progressScopeTime;
779 float p = 1.0 * elapsed / duration;
780 if (p > 1.0) p = 1.0;
781 if (p > progress) {
782 progress = p;
783 redraw = true;
784 }
785 }
786
787 if (redraw) update_progress_locked();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700788 }
789
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700790 double end = now();
791 // minimum of 20ms delay between frames
792 double delay = interval - (end - start);
793 if (delay < 0.02) delay = 0.02;
794 usleep(static_cast<useconds_t>(delay * 1000000));
795 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700796}
797
Tao Baoda409fb2018-10-21 23:36:26 -0700798std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadBitmap(const std::string& filename) {
799 GRSurface* surface;
800 if (auto result = res_create_display_surface(filename.c_str(), &surface); result < 0) {
801 LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")";
802 return nullptr;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700803 }
Tao Baoda409fb2018-10-21 23:36:26 -0700804 return std::unique_ptr<GRSurface>(surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800805}
806
Tao Baoda409fb2018-10-21 23:36:26 -0700807std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) {
808 GRSurface* surface;
809 if (auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface);
810 result < 0) {
811 LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")";
812 return nullptr;
Tao Bao736d59c2017-01-03 10:15:33 -0800813 }
Tao Baoda409fb2018-10-21 23:36:26 -0700814 return std::unique_ptr<GRSurface>(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700815}
816
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700817static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700818 char** result = new char*[rows];
819 for (size_t i = 0; i < rows; ++i) {
820 result[i] = new char[cols];
821 memset(result[i], 0, cols);
822 }
823 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700824}
825
Tianjie Xu35926c42016-04-28 18:06:26 -0700826// Choose the right background string to display during update.
827void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700828 if (security_update) {
Tao Baoda409fb2018-10-21 23:36:26 -0700829 installing_text_ = LoadLocalizedBitmap("installing_security_text");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700830 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700831 installing_text_ = LoadLocalizedBitmap("installing_text");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700832 }
833 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700834}
835
Sen Jiangd5304492016-12-09 16:20:49 -0800836bool ScreenRecoveryUI::InitTextParams() {
Tao Baoba4edb32018-06-13 11:22:50 -0700837 // gr_init() would return successfully on font initialization failure.
838 if (gr_sys_font() == nullptr) {
Tao Bao171b4c42017-06-19 23:10:44 -0700839 return false;
840 }
Tao Bao171b4c42017-06-19 23:10:44 -0700841 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao0bc88de2018-07-31 14:53:16 -0700842 text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_;
843 text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700844 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700845}
846
Tianjie Xub99e6062018-10-16 15:13:09 -0700847// TODO(xunchang) load localized text icons for the menu. (Init for screenRecoveryUI but
848// not wearRecoveryUI).
849bool ScreenRecoveryUI::LoadWipeDataMenuText() {
Tianjie Xub99e6062018-10-16 15:13:09 -0700850 return true;
851}
852
Tao Bao736d59c2017-01-03 10:15:33 -0800853bool ScreenRecoveryUI::Init(const std::string& locale) {
854 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800855
Tao Baoba4edb32018-06-13 11:22:50 -0700856 if (gr_init() == -1) {
857 return false;
858 }
859
Tao Bao736d59c2017-01-03 10:15:33 -0800860 if (!InitTextParams()) {
861 return false;
862 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700863
Tao Bao736d59c2017-01-03 10:15:33 -0800864 // Are we portrait or landscape?
865 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
866 // Are we the large variant of our base layout?
867 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700868
Tao Bao736d59c2017-01-03 10:15:33 -0800869 text_ = Alloc2d(text_rows_, text_cols_ + 1);
870 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800871
Tao Bao736d59c2017-01-03 10:15:33 -0800872 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700873
Tao Baoefb49ad2017-01-31 23:03:10 -0800874 // Set up the locale info.
875 SetLocale(locale);
876
Tao Baoda409fb2018-10-21 23:36:26 -0700877 error_icon_ = LoadBitmap("icon_error");
Doug Zongker02ec6b82012-08-22 17:26:40 -0700878
Tao Baoda409fb2018-10-21 23:36:26 -0700879 progress_bar_empty_ = LoadBitmap("progress_empty");
880 progress_bar_fill_ = LoadBitmap("progress_fill");
881 stage_marker_empty_ = LoadBitmap("stage_empty");
882 stage_marker_fill_ = LoadBitmap("stage_fill");
Elliott Hughes498cda62016-04-14 16:49:04 -0700883
Tao Baoda409fb2018-10-21 23:36:26 -0700884 erasing_text_ = LoadLocalizedBitmap("erasing_text");
885 no_command_text_ = LoadLocalizedBitmap("no_command_text");
886 error_text_ = LoadLocalizedBitmap("error_text");
Doug Zongker211aebc2011-10-28 15:13:10 -0700887
Tao Baoda409fb2018-10-21 23:36:26 -0700888 // Background text for "installing_update" could be "installing update" or
889 // "installing security update". It will be set after Init() according to the commands in BCB.
890 installing_text_.reset();
Elliott Hughes498cda62016-04-14 16:49:04 -0700891
Tianjie Xub99e6062018-10-16 15:13:09 -0700892 LoadWipeDataMenuText();
893
Tao Bao736d59c2017-01-03 10:15:33 -0800894 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700895
Tao Bao26ea9592018-05-09 16:32:02 -0700896 // Keep the progress bar updated, even when the process is otherwise busy.
897 progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800898
Tao Bao736d59c2017-01-03 10:15:33 -0800899 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700900}
901
Tao Bao551d2c32018-05-09 20:53:13 -0700902std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700903 return locale_;
904}
905
Elliott Hughes498cda62016-04-14 16:49:04 -0700906void ScreenRecoveryUI::LoadAnimation() {
Tao Bao6cd81682018-05-03 21:53:11 -0700907 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
908 closedir);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700909 dirent* de;
910 std::vector<std::string> intro_frame_names;
911 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700912
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700913 while ((de = readdir(dir.get())) != nullptr) {
914 int value, num_chars;
915 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
916 intro_frame_names.emplace_back(de->d_name, num_chars);
917 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
918 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700919 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700920 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700921
Tao Baoda409fb2018-10-21 23:36:26 -0700922 size_t intro_frames = intro_frame_names.size();
923 size_t loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700924
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700925 // It's okay to not have an intro.
Tao Baoda409fb2018-10-21 23:36:26 -0700926 if (intro_frames == 0) intro_done_ = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700927 // But you must have an animation.
928 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700929
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700930 std::sort(intro_frame_names.begin(), intro_frame_names.end());
931 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700932
Tao Baoda409fb2018-10-21 23:36:26 -0700933 intro_frames_.clear();
934 intro_frames_.reserve(intro_frames);
935 for (const auto& frame_name : intro_frame_names) {
936 intro_frames_.emplace_back(LoadBitmap(frame_name));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700937 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700938
Tao Baoda409fb2018-10-21 23:36:26 -0700939 loop_frames_.clear();
940 loop_frames_.reserve(loop_frames);
941 for (const auto& frame_name : loop_frame_names) {
942 loop_frames_.emplace_back(LoadBitmap(frame_name));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700943 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700944}
945
Elliott Hughes8de52072015-04-08 20:06:50 -0700946void ScreenRecoveryUI::SetBackground(Icon icon) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700947 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700948
Tao Baoda409fb2018-10-21 23:36:26 -0700949 current_icon_ = icon;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700950 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700951}
952
Elliott Hughes8de52072015-04-08 20:06:50 -0700953void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700954 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700955 if (progressBarType != type) {
956 progressBarType = type;
957 }
958 progressScopeStart = 0;
959 progressScopeSize = 0;
960 progress = 0;
961 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700962}
963
Elliott Hughes8de52072015-04-08 20:06:50 -0700964void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700965 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700966 progressBarType = DETERMINATE;
967 progressScopeStart += progressScopeSize;
968 progressScopeSize = portion;
969 progressScopeTime = now();
970 progressScopeDuration = seconds;
971 progress = 0;
972 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700973}
974
Elliott Hughes8de52072015-04-08 20:06:50 -0700975void ScreenRecoveryUI::SetProgress(float fraction) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700976 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700977 if (fraction < 0.0) fraction = 0.0;
978 if (fraction > 1.0) fraction = 1.0;
979 if (progressBarType == DETERMINATE && fraction > progress) {
980 // Skip updates that aren't visibly different.
Tao Baoda409fb2018-10-21 23:36:26 -0700981 int width = gr_get_width(progress_bar_empty_.get());
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700982 float scale = width * progressScopeSize;
983 if ((int)(progress * scale) != (int)(fraction * scale)) {
984 progress = fraction;
985 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700986 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700987 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700988}
989
Doug Zongkerc87bab12013-11-25 13:53:25 -0800990void ScreenRecoveryUI::SetStage(int current, int max) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700991 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700992 stage = current;
993 max_stage = max;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800994}
995
Tao Baob6918c72015-05-19 17:02:16 -0700996void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700997 std::string str;
998 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700999
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001000 if (copy_to_stdout) {
1001 fputs(str.c_str(), stdout);
1002 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001003
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001004 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001005 if (text_rows_ > 0 && text_cols_ > 0) {
1006 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
1007 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -07001008 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001009 text_col_ = 0;
1010 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001011 }
1012 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001013 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001014 text_[text_row_][text_col_] = '\0';
1015 update_screen_locked();
1016 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001017}
1018
Tao Baob6918c72015-05-19 17:02:16 -07001019void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001020 va_list ap;
1021 va_start(ap, fmt);
1022 PrintV(fmt, true, ap);
1023 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001024}
1025
1026void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001027 va_list ap;
1028 va_start(ap, fmt);
1029 PrintV(fmt, false, ap);
1030 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001031}
1032
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001033void ScreenRecoveryUI::PutChar(char ch) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001034 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001035 if (ch != '\n') text_[text_row_][text_col_++] = ch;
1036 if (ch == '\n' || text_col_ >= text_cols_) {
1037 text_col_ = 0;
1038 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001039 }
Elliott Hughes8de52072015-04-08 20:06:50 -07001040}
1041
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001042void ScreenRecoveryUI::ClearText() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001043 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001044 text_col_ = 0;
1045 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001046 for (size_t i = 0; i < text_rows_; ++i) {
1047 memset(text_[i], 0, text_cols_ + 1);
1048 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001049}
1050
1051void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001052 std::vector<off_t> offsets;
1053 offsets.push_back(ftello(fp));
1054 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001055
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001056 struct stat sb;
1057 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001058
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001059 bool show_prompt = false;
1060 while (true) {
1061 if (show_prompt) {
1062 PrintOnScreenOnly("--(%d%% of %d bytes)--",
1063 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
1064 static_cast<int>(sb.st_size));
1065 Redraw();
1066 while (show_prompt) {
1067 show_prompt = false;
1068 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001069 if (key == static_cast<int>(KeyError::INTERRUPTED)) return;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001070 if (key == KEY_POWER || key == KEY_ENTER) {
1071 return;
1072 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
1073 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001074 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001075 } else {
1076 offsets.pop_back();
1077 fseek(fp, offsets.back(), SEEK_SET);
1078 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001079 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001080 if (feof(fp)) {
1081 return;
1082 }
1083 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001084 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001085 }
1086 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001087 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001088
1089 int ch = getc(fp);
1090 if (ch == EOF) {
1091 while (text_row_ < text_rows_ - 1) PutChar('\n');
1092 show_prompt = true;
1093 } else {
1094 PutChar(ch);
1095 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
1096 show_prompt = true;
1097 }
1098 }
1099 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001100}
1101
Tao Bao1d156b92018-05-02 12:43:18 -07001102void ScreenRecoveryUI::ShowFile(const std::string& filename) {
1103 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
1104 if (!fp) {
1105 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001106 return;
1107 }
Elliott Hughesc0491632015-05-06 12:40:05 -07001108
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001109 char** old_text = text_;
1110 size_t old_text_col = text_col_;
1111 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -07001112
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001113 // Swap in the alternate screen and clear it.
1114 text_ = file_viewer_text_;
1115 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -07001116
Tao Bao1d156b92018-05-02 12:43:18 -07001117 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -07001118
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001119 text_ = old_text;
1120 text_col_ = old_text_col;
1121 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -07001122}
1123
Tao Baoda409fb2018-10-21 23:36:26 -07001124std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(
1125 const GRSurface* graphic_header, const std::vector<const GRSurface*>& graphic_items,
1126 const std::vector<std::string>& text_headers, const std::vector<std::string>& text_items,
1127 size_t initial_selection) const {
Tianjie Xub99e6062018-10-16 15:13:09 -07001128 // horizontal unusable area: margin width + menu indent
1129 size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent;
1130 // vertical unusable area: margin height + title lines + helper message + high light bar.
1131 // It is safe to reserve more space.
1132 size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3);
1133 if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) {
1134 return std::make_unique<GraphicMenu>(graphic_header, graphic_items, initial_selection, *this);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001135 }
Tianjie Xub99e6062018-10-16 15:13:09 -07001136
1137 fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n");
1138
1139 return CreateMenu(text_headers, text_items, initial_selection);
1140}
1141
1142std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(const std::vector<std::string>& text_headers,
1143 const std::vector<std::string>& text_items,
1144 size_t initial_selection) const {
1145 if (text_rows_ > 0 && text_cols_ > 1) {
1146 return std::make_unique<TextMenu>(scrollable_menu_, text_rows_, text_cols_ - 1, text_headers,
1147 text_items, initial_selection, char_height_, *this);
1148 }
1149
1150 fprintf(stderr, "Failed to create text menu, text_rows %zu, text_cols %zu.\n", text_rows_,
1151 text_cols_);
1152 return nullptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001153}
1154
1155int ScreenRecoveryUI::SelectMenu(int sel) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001156 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001157 if (menu_) {
1158 int old_sel = menu_->selection();
1159 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001160
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001161 if (sel != old_sel) {
1162 update_screen_locked();
1163 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001164 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001165 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001166}
1167
Tianjie Xub99e6062018-10-16 15:13:09 -07001168size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr<Menu>&& menu, bool menu_only,
Tao Bao1fe1afe2018-05-01 15:56:05 -07001169 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001170 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1171 FlushKeys();
1172
Jerry Zhangb76af932018-05-22 12:08:35 -07001173 // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the
1174 // menu.
1175 if (IsKeyInterrupted()) return static_cast<size_t>(KeyError::INTERRUPTED);
1176
Tianjie Xub99e6062018-10-16 15:13:09 -07001177 CHECK(menu != nullptr);
Tao Bao3aec6962018-04-20 09:24:58 -07001178
Tianjie Xub99e6062018-10-16 15:13:09 -07001179 // Starts and displays the menu
1180 menu_ = std::move(menu);
1181 Redraw();
1182
1183 int selected = menu_->selection();
Tao Bao3aec6962018-04-20 09:24:58 -07001184 int chosen_item = -1;
1185 while (chosen_item < 0) {
1186 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001187 if (key == static_cast<int>(KeyError::INTERRUPTED)) { // WaitKey() was interrupted.
1188 return static_cast<size_t>(KeyError::INTERRUPTED);
1189 }
1190 if (key == static_cast<int>(KeyError::TIMED_OUT)) { // WaitKey() timed out.
Tao Bao3aec6962018-04-20 09:24:58 -07001191 if (WasTextEverVisible()) {
1192 continue;
1193 } else {
1194 LOG(INFO) << "Timed out waiting for key input; rebooting.";
Tianjie Xub99e6062018-10-16 15:13:09 -07001195 menu_.reset();
1196 Redraw();
Jerry Zhangb76af932018-05-22 12:08:35 -07001197 return static_cast<size_t>(KeyError::TIMED_OUT);
Tao Bao3aec6962018-04-20 09:24:58 -07001198 }
1199 }
1200
1201 bool visible = IsTextVisible();
1202 int action = key_handler(key, visible);
1203 if (action < 0) {
1204 switch (action) {
1205 case Device::kHighlightUp:
1206 selected = SelectMenu(--selected);
1207 break;
1208 case Device::kHighlightDown:
1209 selected = SelectMenu(++selected);
1210 break;
1211 case Device::kInvokeItem:
1212 chosen_item = selected;
1213 break;
1214 case Device::kNoAction:
1215 break;
1216 }
1217 } else if (!menu_only) {
1218 chosen_item = action;
1219 }
1220 }
1221
Tianjie Xub99e6062018-10-16 15:13:09 -07001222 menu_.reset();
1223 Redraw();
1224
Tao Bao3aec6962018-04-20 09:24:58 -07001225 return chosen_item;
1226}
1227
Tianjie Xub99e6062018-10-16 15:13:09 -07001228size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1229 const std::vector<std::string>& items, size_t initial_selection,
1230 bool menu_only,
1231 const std::function<int(int, bool)>& key_handler) {
1232 auto menu = CreateMenu(headers, items, initial_selection);
1233 if (menu == nullptr) {
1234 return initial_selection;
1235 }
1236
1237 return ShowMenu(CreateMenu(headers, items, initial_selection), menu_only, key_handler);
1238}
1239
1240size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector<std::string>& backup_headers,
1241 const std::vector<std::string>& backup_items,
1242 const std::function<int(int, bool)>& key_handler) {
Tao Baoda409fb2018-10-21 23:36:26 -07001243 auto wipe_data_menu = CreateMenu(wipe_data_menu_header_text_.get(),
1244 { try_again_text_.get(), factory_data_reset_text_.get() },
1245 backup_headers, backup_items, 0);
Tianjie Xub99e6062018-10-16 15:13:09 -07001246 if (wipe_data_menu == nullptr) {
1247 return 0;
1248 }
1249
1250 return ShowMenu(std::move(wipe_data_menu), true, key_handler);
1251}
1252
Elliott Hughes8de52072015-04-08 20:06:50 -07001253bool ScreenRecoveryUI::IsTextVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001254 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001255 int visible = show_text;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001256 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001257}
1258
Elliott Hughes8de52072015-04-08 20:06:50 -07001259bool ScreenRecoveryUI::WasTextEverVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001260 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001261 int ever_visible = show_text_ever;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001262 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001263}
1264
Elliott Hughes8de52072015-04-08 20:06:50 -07001265void ScreenRecoveryUI::ShowText(bool visible) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001266 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001267 show_text = visible;
1268 if (show_text) show_text_ever = true;
1269 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001270}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001271
Elliott Hughes8de52072015-04-08 20:06:50 -07001272void ScreenRecoveryUI::Redraw() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001273 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001274 update_screen_locked();
Doug Zongkerc0441d12013-07-31 11:28:24 -07001275}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001276
1277void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001278 // Redraw so that if we're in the menu, the highlight
1279 // will change color to indicate a successful long press.
1280 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001281}
Tao Baoefb49ad2017-01-31 23:03:10 -08001282
1283void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1284 locale_ = new_locale;
1285 rtl_locale_ = false;
1286
1287 if (!new_locale.empty()) {
Tao Bao347a6592018-05-08 15:58:29 -07001288 size_t separator = new_locale.find('-');
1289 // lang has the language prefix prior to the separator, or full string if none exists.
1290 std::string lang = new_locale.substr(0, separator);
Tao Baoefb49ad2017-01-31 23:03:10 -08001291
1292 // A bit cheesy: keep an explicit list of supported RTL languages.
1293 if (lang == "ar" || // Arabic
1294 lang == "fa" || // Persian (Farsi)
1295 lang == "he" || // Hebrew (new language code)
1296 lang == "iw" || // Hebrew (old language code)
1297 lang == "ur") { // Urdu
1298 rtl_locale_ = true;
1299 }
1300 }
1301}