Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 17 | #include "screen_ui.h" |
| 18 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 22 | #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 Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 32 | #include <algorithm> |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 33 | #include <chrono> |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 34 | #include <memory> |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 35 | #include <string> |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 36 | #include <thread> |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 37 | #include <unordered_map> |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 40 | #include <android-base/logging.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 41 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 43 | #include <android-base/strings.h> |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 44 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 45 | #include "device.h" |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 46 | #include "minui/minui.h" |
| 47 | #include "otautil/paths.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 48 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 49 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 50 | // Return the current time as a double (including fractions of a second). |
| 51 | static double now() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 52 | struct timeval tv; |
| 53 | gettimeofday(&tv, nullptr); |
| 54 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 57 | Menu::Menu(size_t initial_selection, const DrawInterface& draw_func) |
| 58 | : selection_(initial_selection), draw_funcs_(draw_func) {} |
| 59 | |
| 60 | size_t Menu::selection() const { |
| 61 | return selection_; |
| 62 | } |
| 63 | |
| 64 | TextMenu::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 Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 69 | max_display_items_(max_items), |
| 70 | max_item_length_(max_length), |
Tao Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 71 | text_headers_(headers), |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 72 | menu_start_(0), |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 73 | char_height_(char_height) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 74 | CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max())); |
Tao Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 75 | |
| 76 | // It's fine to have more entries than text_rows_ if scrollable menu is supported. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 77 | 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 Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | CHECK(!text_items_.empty()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 85 | const std::vector<std::string>& TextMenu::text_headers() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 86 | return text_headers_; |
| 87 | } |
| 88 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 89 | std::string TextMenu::TextItem(size_t index) const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 90 | CHECK_LT(index, text_items_.size()); |
| 91 | |
| 92 | return text_items_[index]; |
| 93 | } |
| 94 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 95 | size_t TextMenu::MenuStart() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 96 | return menu_start_; |
| 97 | } |
| 98 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 99 | size_t TextMenu::MenuEnd() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 100 | return std::min(ItemsCount(), menu_start_ + max_display_items_); |
| 101 | } |
| 102 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 103 | size_t TextMenu::ItemsCount() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 104 | return text_items_.size(); |
| 105 | } |
| 106 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 107 | bool TextMenu::ItemsOverflow(std::string* cur_selection_str) const { |
Tao Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 108 | if (!scrollable_ || ItemsCount() <= max_display_items_) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
| 112 | *cur_selection_str = |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 113 | android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 117 | // TODO(xunchang) modify the function parameters to button up & down. |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 118 | int TextMenu::Select(int sel) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 119 | 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 Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 151 | int 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 | |
| 170 | int 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 | |
| 200 | GraphicMenu::GraphicMenu(size_t max_width, size_t max_height, GRSurface* graphic_headers, |
| 201 | const std::vector<GRSurface*>& graphic_items, size_t initial_selection, |
| 202 | const DrawInterface& draw_funcs) |
| 203 | : Menu(initial_selection, draw_funcs), |
| 204 | max_width_(max_width), |
| 205 | max_height_(max_height), |
| 206 | graphic_headers_(graphic_headers), |
| 207 | graphic_items_(graphic_items) {} |
| 208 | |
| 209 | int GraphicMenu::Select(int sel) { |
| 210 | CHECK_LE(graphic_items_.size(), static_cast<size_t>(std::numeric_limits<int>::max())); |
| 211 | int count = graphic_items_.size(); |
| 212 | |
| 213 | // Wraps the selection at boundary if the menu is not scrollable. |
| 214 | if (sel < 0) { |
| 215 | selection_ = count - 1; |
| 216 | } else if (sel >= count) { |
| 217 | selection_ = 0; |
| 218 | } else { |
| 219 | selection_ = sel; |
| 220 | } |
| 221 | |
| 222 | return selection_; |
| 223 | } |
| 224 | |
| 225 | int GraphicMenu::DrawHeader(int x, int y) const { |
| 226 | draw_funcs_.DrawTextIcon(x, y, graphic_headers_); |
| 227 | return graphic_headers_->height; |
| 228 | } |
| 229 | |
| 230 | int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { |
| 231 | int offset = 0; |
| 232 | |
| 233 | draw_funcs_.SetColor(UIElement::MENU); |
| 234 | offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; |
| 235 | |
| 236 | for (size_t i = 0; i < graphic_items_.size(); i++) { |
| 237 | auto& item = graphic_items_[i]; |
| 238 | if (i == selection_) { |
| 239 | draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); |
| 240 | |
| 241 | int bar_height = item->height + 4; |
| 242 | draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); |
| 243 | |
| 244 | // Bold white text for the selected item. |
| 245 | draw_funcs_.SetColor(UIElement::MENU_SEL_FG); |
| 246 | } |
| 247 | draw_funcs_.DrawTextIcon(x, y + offset, item); |
| 248 | offset += item->height; |
| 249 | |
| 250 | draw_funcs_.SetColor(UIElement::MENU); |
| 251 | } |
| 252 | |
| 253 | return offset; |
| 254 | } |
| 255 | |
| 256 | bool GraphicMenu::Validate() const { |
| 257 | int offset = 0; |
| 258 | if (!ValidateGraphicSurface(offset, graphic_headers_)) { |
| 259 | return false; |
| 260 | } |
| 261 | offset += graphic_headers_->height; |
| 262 | |
| 263 | for (const auto& item : graphic_items_) { |
| 264 | if (!ValidateGraphicSurface(offset, item)) { |
| 265 | return false; |
| 266 | } |
| 267 | offset += item->height; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool GraphicMenu::ValidateGraphicSurface(int y, const GRSurface* surface) const { |
| 274 | 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 | |
| 285 | if (surface->width > max_width_ || surface->height > max_height_ - y) { |
| 286 | 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", |
| 289 | surface->width, surface->height, max_width_, max_height_, y); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 296 | ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} |
| 297 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 298 | constexpr int kDefaultMarginHeight = 0; |
| 299 | constexpr int kDefaultMarginWidth = 0; |
| 300 | constexpr int kDefaultAnimationFps = 30; |
| 301 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 302 | ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 303 | : 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 Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 310 | currentIcon(NONE), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 311 | 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 Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 321 | show_text(false), |
| 322 | show_text_ever(false), |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 323 | scrollable_menu_(scrollable_menu), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 324 | file_viewer_text_(nullptr), |
| 325 | intro_frames(0), |
| 326 | loop_frames(0), |
| 327 | current_frame(0), |
| 328 | intro_done(false), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 329 | stage(-1), |
| 330 | max_stage(-1), |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 331 | locale_(""), |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 332 | rtl_locale_(false) {} |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 333 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 334 | ScreenRecoveryUI::~ScreenRecoveryUI() { |
| 335 | progress_thread_stopped_ = true; |
Tao Bao | 94371fd | 2018-06-06 07:38:54 -0700 | [diff] [blame] | 336 | if (progress_thread_.joinable()) { |
| 337 | progress_thread_.join(); |
| 338 | } |
Tao Bao | 60ac622 | 2018-06-13 14:33:51 -0700 | [diff] [blame] | 339 | // No-op if gr_init() (via Init()) was not called or had failed. |
| 340 | gr_exit(); |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 343 | GRSurface* ScreenRecoveryUI::GetCurrentFrame() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 344 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
| 345 | return intro_done ? loopFrames[current_frame] : introFrames[current_frame]; |
| 346 | } |
| 347 | return error_icon; |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 350 | GRSurface* ScreenRecoveryUI::GetCurrentText() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 351 | 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 Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 365 | int ScreenRecoveryUI::PixelsFromDp(int dp) const { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 366 | return dp * density_; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Here's the intended layout: |
| 370 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 371 | // | portrait large landscape large |
| 372 | // ---------+------------------------------------------------- |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 373 | // gap | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 374 | // icon | (200dp) |
| 375 | // gap | 68dp 68dp 56dp 112dp |
| 376 | // text | (14sp) |
| 377 | // gap | 32dp 32dp 26dp 52dp |
| 378 | // progress | (2dp) |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 379 | // gap | |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 380 | |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 381 | // 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 Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 383 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 384 | enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX }; |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 385 | enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX }; |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 386 | static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 387 | { 32, 68, }, // PORTRAIT |
| 388 | { 32, 68, }, // PORTRAIT_LARGE |
| 389 | { 26, 56, }, // LANDSCAPE |
| 390 | { 52, 112, }, // LANDSCAPE_LARGE |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 391 | }; |
| 392 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 393 | int ScreenRecoveryUI::GetAnimationBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 394 | return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 397 | int ScreenRecoveryUI::GetTextBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 398 | return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - |
| 399 | gr_get_height(installing_text); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 402 | int ScreenRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 403 | 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 Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 406 | int bottom_gap = (ScreenHeight() - elements_sum) / 2; |
| 407 | return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 410 | // Clear the screen and draw the currently selected background icon (if any). |
| 411 | // Should only be called with updateMutex locked. |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 412 | void ScreenRecoveryUI::draw_background_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 413 | pagesIdentical = false; |
| 414 | gr_color(0, 0, 0, 255); |
| 415 | gr_clear(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 416 | 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 Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 420 | int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 421 | int y = ScreenHeight() - stage_height - margin_height_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 422 | for (int i = 0; i < max_stage; ++i) { |
| 423 | GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty; |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 424 | DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 425 | x += stage_width; |
| 426 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 427 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 428 | |
| 429 | GRSurface* text_surface = GetCurrentText(); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 430 | int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 431 | int text_y = GetTextBaseline(); |
| 432 | gr_color(255, 255, 255, 255); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 433 | DrawTextIcon(text_x, text_y, text_surface); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 434 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 437 | // Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be |
| 438 | // called with updateMutex locked. |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 439 | void ScreenRecoveryUI::draw_foreground_locked() { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 440 | if (currentIcon != NONE) { |
| 441 | GRSurface* frame = GetCurrentFrame(); |
| 442 | int frame_width = gr_get_width(frame); |
| 443 | int frame_height = gr_get_height(frame); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 444 | int frame_x = (ScreenWidth() - frame_width) / 2; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 445 | int frame_y = GetAnimationBaseline(); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 446 | DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 447 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 448 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 449 | if (progressBarType != EMPTY) { |
| 450 | int width = gr_get_width(progressBarEmpty); |
| 451 | int height = gr_get_height(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 452 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 453 | int progress_x = (ScreenWidth() - width) / 2; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 454 | int progress_y = GetProgressBaseline(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 455 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 456 | // Erase behind the progress bar (in case this was a progress-only update) |
| 457 | gr_color(0, 0, 0, 255); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 458 | DrawFill(progress_x, progress_y, width, height); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 459 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 460 | if (progressBarType == DETERMINATE) { |
| 461 | float p = progressScopeStart + progress * progressScopeSize; |
| 462 | int pos = static_cast<int>(p * width); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 463 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 464 | if (rtl_locale_) { |
| 465 | // Fill the progress bar from right to left. |
| 466 | if (pos > 0) { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 467 | DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos, |
| 468 | progress_y); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 469 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 470 | if (pos < width - 1) { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 471 | DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 472 | } |
| 473 | } else { |
| 474 | // Fill the progress bar from left to right. |
| 475 | if (pos > 0) { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 476 | DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 477 | } |
| 478 | if (pos < width - 1) { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 479 | DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 482 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 483 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 486 | void ScreenRecoveryUI::SetColor(UIElement e) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 487 | switch (e) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 488 | case UIElement::INFO: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 489 | gr_color(249, 194, 0, 255); |
| 490 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 491 | case UIElement::HEADER: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 492 | gr_color(247, 0, 6, 255); |
| 493 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 494 | case UIElement::MENU: |
| 495 | case UIElement::MENU_SEL_BG: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 496 | gr_color(0, 106, 157, 255); |
| 497 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 498 | case UIElement::MENU_SEL_BG_ACTIVE: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 499 | gr_color(0, 156, 100, 255); |
| 500 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 501 | case UIElement::MENU_SEL_FG: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 502 | gr_color(255, 255, 255, 255); |
| 503 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 504 | case UIElement::LOG: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 505 | gr_color(196, 196, 196, 255); |
| 506 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 507 | case UIElement::TEXT_FILL: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 508 | gr_color(0, 0, 0, 160); |
| 509 | break; |
| 510 | default: |
| 511 | gr_color(255, 255, 255, 255); |
| 512 | break; |
| 513 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 514 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 515 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 516 | void 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 Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 532 | std::lock_guard<std::mutex> lg(updateMutex); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 533 | gr_color(0, 0, 0, 255); |
| 534 | gr_clear(); |
| 535 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 536 | int text_y = margin_height_; |
| 537 | int text_x = margin_width_; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 538 | int line_spacing = gr_sys_font()->char_height; // Put some extra space between images. |
| 539 | // Write the header and descriptive texts. |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 540 | SetColor(UIElement::INFO); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 541 | std::string header = "Show background text image"; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 542 | text_y += DrawTextLine(text_x, text_y, header, true); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 543 | std::string locale_selection = android::base::StringPrintf( |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 544 | "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size()); |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 545 | // 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 Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 551 | 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 Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 556 | SetColor(UIElement::LOG); |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 557 | text_y += DrawTextLine(text_x, text_y, p.first, false); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 558 | 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 Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 566 | void ScreenRecoveryUI::CheckBackgroundTextImages() { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 567 | // 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 Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 573 | std::string saved_locale = locale_; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 574 | size_t selected = 0; |
| 575 | SelectAndShowBackgroundText(locales_entries, selected); |
| 576 | |
| 577 | FlushKeys(); |
| 578 | while (true) { |
| 579 | int key = WaitKey(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 580 | if (key == static_cast<int>(KeyError::INTERRUPTED)) break; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 581 | 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 Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 595 | int ScreenRecoveryUI::ScreenWidth() const { |
| 596 | return gr_fb_width(); |
| 597 | } |
| 598 | |
| 599 | int ScreenRecoveryUI::ScreenHeight() const { |
| 600 | return gr_fb_height(); |
| 601 | } |
| 602 | |
| 603 | void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, |
| 604 | int dy) const { |
| 605 | gr_blit(surface, sx, sy, w, h, dx, dy); |
| 606 | } |
| 607 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 608 | int ScreenRecoveryUI::DrawHorizontalRule(int y) const { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 609 | gr_fill(0, y + 4, ScreenWidth(), y + 6); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 610 | return 8; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 613 | void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 614 | gr_fill(x, y, x + width, y + height); |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 617 | void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const { |
| 618 | gr_fill(x, y, w, h); |
| 619 | } |
| 620 | |
| 621 | void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const { |
| 622 | gr_texticon(x, y, surface); |
| 623 | } |
| 624 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 625 | int 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 Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 627 | return char_height_ + 4; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 630 | int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 631 | int offset = 0; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 632 | for (const auto& line : lines) { |
| 633 | offset += DrawTextLine(x, y + offset, line, false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 634 | } |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 635 | return offset; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 638 | int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, |
| 639 | const std::vector<std::string>& lines) const { |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 640 | // Keep symmetrical margins based on the given offset (i.e. x). |
| 641 | size_t text_cols = (ScreenWidth() - x * 2) / char_width_; |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 642 | int offset = 0; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 643 | for (const auto& line : lines) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 644 | size_t next_start = 0; |
| 645 | while (next_start < line.size()) { |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 646 | std::string sub = line.substr(next_start, text_cols + 1); |
| 647 | if (sub.size() <= text_cols) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 648 | next_start += sub.size(); |
| 649 | } else { |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 650 | // Line too long and must be wrapped to text_cols columns. |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 651 | size_t last_space = sub.find_last_of(" \t\n"); |
| 652 | if (last_space == std::string::npos) { |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 653 | // No space found, just draw as much as we can. |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 654 | sub.resize(text_cols); |
| 655 | next_start += text_cols; |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 656 | } else { |
| 657 | sub.resize(last_space); |
| 658 | next_start += last_space + 1; |
| 659 | } |
| 660 | } |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 661 | offset += DrawTextLine(x, y + offset, sub, false); |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | return offset; |
| 665 | } |
| 666 | |
Jerry Zhang | 0e577ee | 2018-05-07 11:21:10 -0700 | [diff] [blame] | 667 | void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) { |
| 668 | title_lines_ = lines; |
| 669 | } |
| 670 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 671 | // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex |
| 672 | // locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 673 | void ScreenRecoveryUI::draw_screen_locked() { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 674 | if (!show_text) { |
| 675 | draw_background_locked(); |
| 676 | draw_foreground_locked(); |
| 677 | return; |
| 678 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 679 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 680 | gr_color(0, 0, 0, 255); |
| 681 | gr_clear(); |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 682 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 683 | // clang-format off |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 684 | static std::vector<std::string> REGULAR_HELP{ |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 685 | "Use volume up/down and power.", |
| 686 | }; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 687 | static std::vector<std::string> LONG_PRESS_HELP{ |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 688 | "Any button cycles highlight.", |
| 689 | "Long-press activates.", |
| 690 | }; |
| 691 | // clang-format on |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 692 | 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 Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 696 | void ScreenRecoveryUI::draw_menu_and_text_buffer_locked( |
| 697 | const std::vector<std::string>& help_message) { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 698 | int y = margin_height_; |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 699 | if (menu_) { |
Tao Bao | ca6ce2c | 2017-07-13 11:15:27 -0700 | [diff] [blame] | 700 | static constexpr int kMenuIndent = 4; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 701 | int x = margin_width_ + kMenuIndent; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 702 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 703 | SetColor(UIElement::INFO); |
Jerry Zhang | 0e577ee | 2018-05-07 11:21:10 -0700 | [diff] [blame] | 704 | |
| 705 | for (size_t i = 0; i < title_lines_.size(); i++) { |
| 706 | y += DrawTextLine(x, y, title_lines_[i], i == 0); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 707 | } |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 708 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 709 | y += DrawTextLines(x, y, help_message); |
| 710 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 711 | y += menu_->DrawHeader(x, y); |
| 712 | y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress()); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or |
| 716 | // we've displayed the entire text buffer. |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 717 | SetColor(UIElement::LOG); |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 718 | int row = text_row_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 719 | size_t count = 0; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 720 | for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 721 | ty -= char_height_, ++count) { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 722 | DrawTextLine(margin_width_, ty, text_[row], false); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 723 | --row; |
| 724 | if (row < 0) row = text_rows_ - 1; |
| 725 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // Redraw everything on the screen and flip the screen (make it visible). |
| 729 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 730 | void ScreenRecoveryUI::update_screen_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 731 | draw_screen_locked(); |
| 732 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 736 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 737 | void ScreenRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 738 | if (show_text || !pagesIdentical) { |
| 739 | draw_screen_locked(); // Must redraw the whole screen |
| 740 | pagesIdentical = true; |
| 741 | } else { |
| 742 | draw_foreground_locked(); // Draw only the progress bar and overlays |
| 743 | } |
| 744 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 747 | void ScreenRecoveryUI::ProgressThreadLoop() { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 748 | double interval = 1.0 / animation_fps_; |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 749 | while (!progress_thread_stopped_) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 750 | double start = now(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 751 | bool redraw = false; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 752 | { |
| 753 | std::lock_guard<std::mutex> lg(updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 754 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 755 | // update the installation animation, if active |
| 756 | // skip this if we have a text overlay (too expensive to update) |
| 757 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) { |
| 758 | if (!intro_done) { |
| 759 | if (current_frame == intro_frames - 1) { |
| 760 | intro_done = true; |
| 761 | current_frame = 0; |
| 762 | } else { |
| 763 | ++current_frame; |
| 764 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 765 | } else { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 766 | current_frame = (current_frame + 1) % loop_frames; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 769 | redraw = true; |
| 770 | } |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 771 | |
| 772 | // move the progress bar forward on timed intervals, if configured |
| 773 | int duration = progressScopeDuration; |
| 774 | if (progressBarType == DETERMINATE && duration > 0) { |
| 775 | double elapsed = now() - progressScopeTime; |
| 776 | float p = 1.0 * elapsed / duration; |
| 777 | if (p > 1.0) p = 1.0; |
| 778 | if (p > progress) { |
| 779 | progress = p; |
| 780 | redraw = true; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if (redraw) update_progress_locked(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 787 | double end = now(); |
| 788 | // minimum of 20ms delay between frames |
| 789 | double delay = interval - (end - start); |
| 790 | if (delay < 0.02) delay = 0.02; |
| 791 | usleep(static_cast<useconds_t>(delay * 1000000)); |
| 792 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 795 | void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 796 | int result = res_create_display_surface(filename, surface); |
| 797 | if (result < 0) { |
| 798 | LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")"; |
| 799 | } |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 802 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 803 | int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface); |
| 804 | if (result < 0) { |
| 805 | LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")"; |
| 806 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 809 | static char** Alloc2d(size_t rows, size_t cols) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 810 | char** result = new char*[rows]; |
| 811 | for (size_t i = 0; i < rows; ++i) { |
| 812 | result[i] = new char[cols]; |
| 813 | memset(result[i], 0, cols); |
| 814 | } |
| 815 | return result; |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 818 | // Choose the right background string to display during update. |
| 819 | void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 820 | if (security_update) { |
| 821 | LoadLocalizedBitmap("installing_security_text", &installing_text); |
| 822 | } else { |
| 823 | LoadLocalizedBitmap("installing_text", &installing_text); |
| 824 | } |
| 825 | Redraw(); |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 828 | bool ScreenRecoveryUI::InitTextParams() { |
Tao Bao | ba4edb3 | 2018-06-13 11:22:50 -0700 | [diff] [blame] | 829 | // gr_init() would return successfully on font initialization failure. |
| 830 | if (gr_sys_font() == nullptr) { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 831 | return false; |
| 832 | } |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 833 | gr_font_size(gr_sys_font(), &char_width_, &char_height_); |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 834 | text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_; |
| 835 | text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 836 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 839 | bool ScreenRecoveryUI::Init(const std::string& locale) { |
| 840 | RecoveryUI::Init(locale); |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 841 | |
Tao Bao | ba4edb3 | 2018-06-13 11:22:50 -0700 | [diff] [blame] | 842 | if (gr_init() == -1) { |
| 843 | return false; |
| 844 | } |
| 845 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 846 | if (!InitTextParams()) { |
| 847 | return false; |
| 848 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 849 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 850 | // Are we portrait or landscape? |
| 851 | layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; |
| 852 | // Are we the large variant of our base layout? |
| 853 | if (gr_fb_height() > PixelsFromDp(800)) ++layout_; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 854 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 855 | text_ = Alloc2d(text_rows_, text_cols_ + 1); |
| 856 | file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 857 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 858 | text_col_ = text_row_ = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 859 | |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 860 | // Set up the locale info. |
| 861 | SetLocale(locale); |
| 862 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 863 | LoadBitmap("icon_error", &error_icon); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 864 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 865 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 866 | LoadBitmap("progress_fill", &progressBarFill); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 867 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 868 | LoadBitmap("stage_empty", &stageMarkerEmpty); |
| 869 | LoadBitmap("stage_fill", &stageMarkerFill); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 870 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 871 | // Background text for "installing_update" could be "installing update" |
| 872 | // or "installing security update". It will be set after UI init according |
| 873 | // to commands in BCB. |
| 874 | installing_text = nullptr; |
| 875 | LoadLocalizedBitmap("erasing_text", &erasing_text); |
| 876 | LoadLocalizedBitmap("no_command_text", &no_command_text); |
| 877 | LoadLocalizedBitmap("error_text", &error_text); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 878 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 879 | LoadAnimation(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 880 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 881 | // Keep the progress bar updated, even when the process is otherwise busy. |
| 882 | progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this); |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 883 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 884 | return true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 887 | std::string ScreenRecoveryUI::GetLocale() const { |
Jerry Zhang | 2dea53e | 2018-05-02 17:15:03 -0700 | [diff] [blame] | 888 | return locale_; |
| 889 | } |
| 890 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 891 | void ScreenRecoveryUI::LoadAnimation() { |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 892 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()), |
| 893 | closedir); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 894 | dirent* de; |
| 895 | std::vector<std::string> intro_frame_names; |
| 896 | std::vector<std::string> loop_frame_names; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 897 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 898 | while ((de = readdir(dir.get())) != nullptr) { |
| 899 | int value, num_chars; |
| 900 | if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { |
| 901 | intro_frame_names.emplace_back(de->d_name, num_chars); |
| 902 | } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { |
| 903 | loop_frame_names.emplace_back(de->d_name, num_chars); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 904 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 905 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 906 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 907 | intro_frames = intro_frame_names.size(); |
| 908 | loop_frames = loop_frame_names.size(); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 909 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 910 | // It's okay to not have an intro. |
| 911 | if (intro_frames == 0) intro_done = true; |
| 912 | // But you must have an animation. |
| 913 | if (loop_frames == 0) abort(); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 914 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 915 | std::sort(intro_frame_names.begin(), intro_frame_names.end()); |
| 916 | std::sort(loop_frame_names.begin(), loop_frame_names.end()); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 917 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 918 | introFrames = new GRSurface*[intro_frames]; |
| 919 | for (size_t i = 0; i < intro_frames; i++) { |
| 920 | LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]); |
| 921 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 922 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 923 | loopFrames = new GRSurface*[loop_frames]; |
| 924 | for (size_t i = 0; i < loop_frames; i++) { |
| 925 | LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]); |
| 926 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 929 | void ScreenRecoveryUI::SetBackground(Icon icon) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 930 | std::lock_guard<std::mutex> lg(updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 931 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 932 | currentIcon = icon; |
| 933 | update_screen_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 936 | void ScreenRecoveryUI::SetProgressType(ProgressType type) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 937 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 938 | if (progressBarType != type) { |
| 939 | progressBarType = type; |
| 940 | } |
| 941 | progressScopeStart = 0; |
| 942 | progressScopeSize = 0; |
| 943 | progress = 0; |
| 944 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 947 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 948 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 949 | progressBarType = DETERMINATE; |
| 950 | progressScopeStart += progressScopeSize; |
| 951 | progressScopeSize = portion; |
| 952 | progressScopeTime = now(); |
| 953 | progressScopeDuration = seconds; |
| 954 | progress = 0; |
| 955 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 958 | void ScreenRecoveryUI::SetProgress(float fraction) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 959 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 960 | if (fraction < 0.0) fraction = 0.0; |
| 961 | if (fraction > 1.0) fraction = 1.0; |
| 962 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 963 | // Skip updates that aren't visibly different. |
| 964 | int width = gr_get_width(progressBarEmpty); |
| 965 | float scale = width * progressScopeSize; |
| 966 | if ((int)(progress * scale) != (int)(fraction * scale)) { |
| 967 | progress = fraction; |
| 968 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 969 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 970 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 973 | void ScreenRecoveryUI::SetStage(int current, int max) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 974 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 975 | stage = current; |
| 976 | max_stage = max; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 977 | } |
| 978 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 979 | void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 980 | std::string str; |
| 981 | android::base::StringAppendV(&str, fmt, ap); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 982 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 983 | if (copy_to_stdout) { |
| 984 | fputs(str.c_str(), stdout); |
| 985 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 986 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 987 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 988 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 989 | for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { |
| 990 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 991 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 992 | text_col_ = 0; |
| 993 | text_row_ = (text_row_ + 1) % text_rows_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 994 | } |
| 995 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 996 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 997 | text_[text_row_][text_col_] = '\0'; |
| 998 | update_screen_locked(); |
| 999 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1002 | void ScreenRecoveryUI::Print(const char* fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1003 | va_list ap; |
| 1004 | va_start(ap, fmt); |
| 1005 | PrintV(fmt, true, ap); |
| 1006 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1010 | va_list ap; |
| 1011 | va_start(ap, fmt); |
| 1012 | PrintV(fmt, false, ap); |
| 1013 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1016 | void ScreenRecoveryUI::PutChar(char ch) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1017 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1018 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 1019 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 1020 | text_col_ = 0; |
| 1021 | ++text_row_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1022 | } |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1025 | void ScreenRecoveryUI::ClearText() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1026 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1027 | text_col_ = 0; |
| 1028 | text_row_ = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1029 | for (size_t i = 0; i < text_rows_; ++i) { |
| 1030 | memset(text_[i], 0, text_cols_ + 1); |
| 1031 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void ScreenRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1035 | std::vector<off_t> offsets; |
| 1036 | offsets.push_back(ftello(fp)); |
| 1037 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1038 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1039 | struct stat sb; |
| 1040 | fstat(fileno(fp), &sb); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1041 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1042 | bool show_prompt = false; |
| 1043 | while (true) { |
| 1044 | if (show_prompt) { |
| 1045 | PrintOnScreenOnly("--(%d%% of %d bytes)--", |
| 1046 | static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))), |
| 1047 | static_cast<int>(sb.st_size)); |
| 1048 | Redraw(); |
| 1049 | while (show_prompt) { |
| 1050 | show_prompt = false; |
| 1051 | int key = WaitKey(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1052 | if (key == static_cast<int>(KeyError::INTERRUPTED)) return; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1053 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 1054 | return; |
| 1055 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 1056 | if (offsets.size() <= 1) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1057 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1058 | } else { |
| 1059 | offsets.pop_back(); |
| 1060 | fseek(fp, offsets.back(), SEEK_SET); |
| 1061 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1062 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1063 | if (feof(fp)) { |
| 1064 | return; |
| 1065 | } |
| 1066 | offsets.push_back(ftello(fp)); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1067 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1068 | } |
| 1069 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1070 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1071 | |
| 1072 | int ch = getc(fp); |
| 1073 | if (ch == EOF) { |
| 1074 | while (text_row_ < text_rows_ - 1) PutChar('\n'); |
| 1075 | show_prompt = true; |
| 1076 | } else { |
| 1077 | PutChar(ch); |
| 1078 | if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { |
| 1079 | show_prompt = true; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Tao Bao | 1d156b9 | 2018-05-02 12:43:18 -0700 | [diff] [blame] | 1085 | void ScreenRecoveryUI::ShowFile(const std::string& filename) { |
| 1086 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose); |
| 1087 | if (!fp) { |
| 1088 | Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno)); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1089 | return; |
| 1090 | } |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1091 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1092 | char** old_text = text_; |
| 1093 | size_t old_text_col = text_col_; |
| 1094 | size_t old_text_row = text_row_; |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1095 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1096 | // Swap in the alternate screen and clear it. |
| 1097 | text_ = file_viewer_text_; |
| 1098 | ClearText(); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1099 | |
Tao Bao | 1d156b9 | 2018-05-02 12:43:18 -0700 | [diff] [blame] | 1100 | ShowFile(fp.get()); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1101 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1102 | text_ = old_text; |
| 1103 | text_col_ = old_text_col; |
| 1104 | text_row_ = old_text_row; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 1107 | void ScreenRecoveryUI::StartMenu(const std::vector<std::string>& headers, |
| 1108 | const std::vector<std::string>& items, size_t initial_selection) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1109 | std::lock_guard<std::mutex> lg(updateMutex); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 1110 | if (text_rows_ > 0 && text_cols_ > 1) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 1111 | menu_ = std::make_unique<TextMenu>(scrollable_menu_, text_rows_, text_cols_ - 1, headers, items, |
| 1112 | initial_selection, char_height_, *this); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1113 | update_screen_locked(); |
| 1114 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | int ScreenRecoveryUI::SelectMenu(int sel) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1118 | std::lock_guard<std::mutex> lg(updateMutex); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 1119 | if (menu_) { |
| 1120 | int old_sel = menu_->selection(); |
| 1121 | sel = menu_->Select(sel); |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 1122 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 1123 | if (sel != old_sel) { |
| 1124 | update_screen_locked(); |
| 1125 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1126 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1127 | return sel; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | void ScreenRecoveryUI::EndMenu() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1131 | std::lock_guard<std::mutex> lg(updateMutex); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 1132 | if (menu_) { |
| 1133 | menu_.reset(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1134 | update_screen_locked(); |
| 1135 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 1138 | size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers, |
| 1139 | const std::vector<std::string>& items, size_t initial_selection, |
| 1140 | bool menu_only, |
| 1141 | const std::function<int(int, bool)>& key_handler) { |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1142 | // Throw away keys pressed previously, so user doesn't accidentally trigger menu items. |
| 1143 | FlushKeys(); |
| 1144 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1145 | // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the |
| 1146 | // menu. |
| 1147 | if (IsKeyInterrupted()) return static_cast<size_t>(KeyError::INTERRUPTED); |
| 1148 | |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1149 | StartMenu(headers, items, initial_selection); |
| 1150 | |
| 1151 | int selected = initial_selection; |
| 1152 | int chosen_item = -1; |
| 1153 | while (chosen_item < 0) { |
| 1154 | int key = WaitKey(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1155 | if (key == static_cast<int>(KeyError::INTERRUPTED)) { // WaitKey() was interrupted. |
| 1156 | return static_cast<size_t>(KeyError::INTERRUPTED); |
| 1157 | } |
| 1158 | if (key == static_cast<int>(KeyError::TIMED_OUT)) { // WaitKey() timed out. |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1159 | if (WasTextEverVisible()) { |
| 1160 | continue; |
| 1161 | } else { |
| 1162 | LOG(INFO) << "Timed out waiting for key input; rebooting."; |
| 1163 | EndMenu(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1164 | return static_cast<size_t>(KeyError::TIMED_OUT); |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | bool visible = IsTextVisible(); |
| 1169 | int action = key_handler(key, visible); |
| 1170 | if (action < 0) { |
| 1171 | switch (action) { |
| 1172 | case Device::kHighlightUp: |
| 1173 | selected = SelectMenu(--selected); |
| 1174 | break; |
| 1175 | case Device::kHighlightDown: |
| 1176 | selected = SelectMenu(++selected); |
| 1177 | break; |
| 1178 | case Device::kInvokeItem: |
| 1179 | chosen_item = selected; |
| 1180 | break; |
| 1181 | case Device::kNoAction: |
| 1182 | break; |
| 1183 | } |
| 1184 | } else if (!menu_only) { |
| 1185 | chosen_item = action; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | EndMenu(); |
| 1190 | return chosen_item; |
| 1191 | } |
| 1192 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1193 | bool ScreenRecoveryUI::IsTextVisible() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1194 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1195 | int visible = show_text; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1196 | return visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1199 | bool ScreenRecoveryUI::WasTextEverVisible() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1200 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1201 | int ever_visible = show_text_ever; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1202 | return ever_visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1205 | void ScreenRecoveryUI::ShowText(bool visible) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1206 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1207 | show_text = visible; |
| 1208 | if (show_text) show_text_ever = true; |
| 1209 | update_screen_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1210 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1211 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1212 | void ScreenRecoveryUI::Redraw() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1213 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1214 | update_screen_locked(); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1215 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 1216 | |
| 1217 | void ScreenRecoveryUI::KeyLongPress(int) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1218 | // Redraw so that if we're in the menu, the highlight |
| 1219 | // will change color to indicate a successful long press. |
| 1220 | Redraw(); |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 1221 | } |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 1222 | |
| 1223 | void ScreenRecoveryUI::SetLocale(const std::string& new_locale) { |
| 1224 | locale_ = new_locale; |
| 1225 | rtl_locale_ = false; |
| 1226 | |
| 1227 | if (!new_locale.empty()) { |
Tao Bao | 347a659 | 2018-05-08 15:58:29 -0700 | [diff] [blame] | 1228 | size_t separator = new_locale.find('-'); |
| 1229 | // lang has the language prefix prior to the separator, or full string if none exists. |
| 1230 | std::string lang = new_locale.substr(0, separator); |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 1231 | |
| 1232 | // A bit cheesy: keep an explicit list of supported RTL languages. |
| 1233 | if (lang == "ar" || // Arabic |
| 1234 | lang == "fa" || // Persian (Farsi) |
| 1235 | lang == "he" || // Hebrew (new language code) |
| 1236 | lang == "iw" || // Hebrew (old language code) |
| 1237 | lang == "ur") { // Urdu |
| 1238 | rtl_locale_ = true; |
| 1239 | } |
| 1240 | } |
| 1241 | } |