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 | |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 17 | #include "recovery_ui/screen_ui.h" |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | 30017e7 | 2020-05-13 12:39:12 -0700 | [diff] [blame] | 40 | #include <android-base/chrono_utils.h> |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 44 | #include <android-base/strings.h> |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 45 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 46 | #include "minui/minui.h" |
| 47 | #include "otautil/paths.h" |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 48 | #include "recovery_ui/device.h" |
| 49 | #include "recovery_ui/ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 50 | |
fredchiou | f2f5aa2 | 2022-02-11 16:39:53 +0800 | [diff] [blame] | 51 | enum DirectRenderManager { |
| 52 | DRM_INNER, |
| 53 | DRM_OUTER, |
| 54 | }; |
| 55 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 56 | // Return the current time as a double (including fractions of a second). |
| 57 | static double now() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 58 | struct timeval tv; |
| 59 | gettimeofday(&tv, nullptr); |
| 60 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 63 | Menu::Menu(size_t initial_selection, const DrawInterface& draw_func) |
| 64 | : selection_(initial_selection), draw_funcs_(draw_func) {} |
| 65 | |
| 66 | size_t Menu::selection() const { |
| 67 | return selection_; |
| 68 | } |
| 69 | |
| 70 | TextMenu::TextMenu(bool scrollable, size_t max_items, size_t max_length, |
| 71 | const std::vector<std::string>& headers, const std::vector<std::string>& items, |
| 72 | size_t initial_selection, int char_height, const DrawInterface& draw_funcs) |
| 73 | : Menu(initial_selection, draw_funcs), |
| 74 | scrollable_(scrollable), |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 75 | max_display_items_(max_items), |
| 76 | max_item_length_(max_length), |
Tao Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 77 | text_headers_(headers), |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 78 | menu_start_(0), |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 79 | char_height_(char_height) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 80 | 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] | 81 | |
| 82 | // 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] | 83 | size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_); |
| 84 | for (size_t i = 0; i < items_count; ++i) { |
| 85 | text_items_.emplace_back(items[i].substr(0, max_item_length_)); |
Tao Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | CHECK(!text_items_.empty()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 91 | const std::vector<std::string>& TextMenu::text_headers() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 92 | return text_headers_; |
| 93 | } |
| 94 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 95 | std::string TextMenu::TextItem(size_t index) const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 96 | CHECK_LT(index, text_items_.size()); |
| 97 | |
| 98 | return text_items_[index]; |
| 99 | } |
| 100 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 101 | size_t TextMenu::MenuStart() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 102 | return menu_start_; |
| 103 | } |
| 104 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 105 | size_t TextMenu::MenuEnd() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 106 | return std::min(ItemsCount(), menu_start_ + max_display_items_); |
| 107 | } |
| 108 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 109 | size_t TextMenu::ItemsCount() const { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 110 | return text_items_.size(); |
| 111 | } |
| 112 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 113 | bool TextMenu::ItemsOverflow(std::string* cur_selection_str) const { |
Tao Bao | e02a5b2 | 2018-05-02 15:46:11 -0700 | [diff] [blame] | 114 | if (!scrollable_ || ItemsCount() <= max_display_items_) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
| 118 | *cur_selection_str = |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 119 | android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 123 | // TODO(xunchang) modify the function parameters to button up & down. |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 124 | int TextMenu::Select(int sel) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 125 | CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max())); |
| 126 | int count = ItemsCount(); |
| 127 | |
| 128 | // Wraps the selection at boundary if the menu is not scrollable. |
| 129 | if (!scrollable_) { |
| 130 | if (sel < 0) { |
| 131 | selection_ = count - 1; |
| 132 | } else if (sel >= count) { |
| 133 | selection_ = 0; |
| 134 | } else { |
| 135 | selection_ = sel; |
| 136 | } |
| 137 | |
| 138 | return selection_; |
| 139 | } |
| 140 | |
| 141 | if (sel < 0) { |
| 142 | selection_ = 0; |
| 143 | } else if (sel >= count) { |
| 144 | selection_ = count - 1; |
| 145 | } else { |
| 146 | if (static_cast<size_t>(sel) < menu_start_) { |
| 147 | menu_start_--; |
| 148 | } else if (static_cast<size_t>(sel) >= MenuEnd()) { |
| 149 | menu_start_++; |
| 150 | } |
| 151 | selection_ = sel; |
| 152 | } |
| 153 | |
| 154 | return selection_; |
| 155 | } |
| 156 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 157 | int TextMenu::DrawHeader(int x, int y) const { |
| 158 | int offset = 0; |
| 159 | |
| 160 | draw_funcs_.SetColor(UIElement::HEADER); |
| 161 | if (!scrollable()) { |
| 162 | offset += draw_funcs_.DrawWrappedTextLines(x, y + offset, text_headers()); |
| 163 | } else { |
| 164 | offset += draw_funcs_.DrawTextLines(x, y + offset, text_headers()); |
| 165 | // Show the current menu item number in relation to total number if items don't fit on the |
| 166 | // screen. |
| 167 | std::string cur_selection_str; |
| 168 | if (ItemsOverflow(&cur_selection_str)) { |
| 169 | offset += draw_funcs_.DrawTextLine(x, y + offset, cur_selection_str, true); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return offset; |
| 174 | } |
| 175 | |
| 176 | int TextMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { |
| 177 | int offset = 0; |
| 178 | |
| 179 | draw_funcs_.SetColor(UIElement::MENU); |
| 180 | // Do not draw the horizontal rule for wear devices. |
| 181 | if (!scrollable()) { |
| 182 | offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; |
| 183 | } |
| 184 | for (size_t i = MenuStart(); i < MenuEnd(); ++i) { |
| 185 | bool bold = false; |
| 186 | if (i == selection()) { |
| 187 | // Draw the highlight bar. |
| 188 | draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); |
| 189 | |
| 190 | int bar_height = char_height_ + 4; |
| 191 | draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); |
| 192 | |
| 193 | // Bold white text for the selected item. |
| 194 | draw_funcs_.SetColor(UIElement::MENU_SEL_FG); |
| 195 | bold = true; |
| 196 | } |
| 197 | offset += draw_funcs_.DrawTextLine(x, y + offset, TextItem(i), bold); |
| 198 | |
| 199 | draw_funcs_.SetColor(UIElement::MENU); |
| 200 | } |
| 201 | offset += draw_funcs_.DrawHorizontalRule(y + offset); |
| 202 | |
| 203 | return offset; |
| 204 | } |
| 205 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 206 | GraphicMenu::GraphicMenu(const GRSurface* graphic_headers, |
| 207 | const std::vector<const GRSurface*>& graphic_items, |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 208 | size_t initial_selection, const DrawInterface& draw_funcs) |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 209 | : Menu(initial_selection, draw_funcs) { |
| 210 | graphic_headers_ = graphic_headers->Clone(); |
| 211 | graphic_items_.reserve(graphic_items.size()); |
| 212 | for (const auto& item : graphic_items) { |
| 213 | graphic_items_.emplace_back(item->Clone()); |
| 214 | } |
| 215 | } |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 216 | |
| 217 | int GraphicMenu::Select(int sel) { |
| 218 | CHECK_LE(graphic_items_.size(), static_cast<size_t>(std::numeric_limits<int>::max())); |
| 219 | int count = graphic_items_.size(); |
| 220 | |
| 221 | // Wraps the selection at boundary if the menu is not scrollable. |
| 222 | if (sel < 0) { |
| 223 | selection_ = count - 1; |
| 224 | } else if (sel >= count) { |
| 225 | selection_ = 0; |
| 226 | } else { |
| 227 | selection_ = sel; |
| 228 | } |
| 229 | |
| 230 | return selection_; |
| 231 | } |
| 232 | |
| 233 | int GraphicMenu::DrawHeader(int x, int y) const { |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 234 | draw_funcs_.SetColor(UIElement::HEADER); |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 235 | draw_funcs_.DrawTextIcon(x, y, graphic_headers_.get()); |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 236 | return graphic_headers_->height; |
| 237 | } |
| 238 | |
| 239 | int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { |
| 240 | int offset = 0; |
| 241 | |
| 242 | draw_funcs_.SetColor(UIElement::MENU); |
| 243 | offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; |
| 244 | |
| 245 | for (size_t i = 0; i < graphic_items_.size(); i++) { |
| 246 | auto& item = graphic_items_[i]; |
| 247 | if (i == selection_) { |
| 248 | draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); |
| 249 | |
| 250 | int bar_height = item->height + 4; |
| 251 | draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); |
| 252 | |
| 253 | // Bold white text for the selected item. |
| 254 | draw_funcs_.SetColor(UIElement::MENU_SEL_FG); |
| 255 | } |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 256 | draw_funcs_.DrawTextIcon(x, y + offset, item.get()); |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 257 | offset += item->height; |
| 258 | |
| 259 | draw_funcs_.SetColor(UIElement::MENU); |
| 260 | } |
xunchang | c7dbc73 | 2018-12-20 11:31:18 -0800 | [diff] [blame] | 261 | offset += draw_funcs_.DrawHorizontalRule(y + offset); |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 262 | |
| 263 | return offset; |
| 264 | } |
| 265 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 266 | bool GraphicMenu::Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers, |
| 267 | const std::vector<const GRSurface*>& graphic_items) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 268 | int offset = 0; |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 269 | if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 272 | offset += graphic_headers->height; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 273 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 274 | for (const auto& item : graphic_items) { |
| 275 | if (!ValidateGraphicSurface(max_width, max_height, offset, item)) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | offset += item->height; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 284 | bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y, |
| 285 | const GRSurface* surface) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 286 | if (!surface) { |
Tao Bao | a00b449 | 2019-01-16 09:29:47 -0800 | [diff] [blame] | 287 | fprintf(stderr, "Graphic surface can not be null\n"); |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) { |
Tao Bao | a00b449 | 2019-01-16 09:29:47 -0800 | [diff] [blame] | 292 | fprintf(stderr, "Invalid graphic surface, pixel bytes: %zu, width: %zu row_bytes: %zu\n", |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 293 | surface->pixel_bytes, surface->width, surface->row_bytes); |
| 294 | return false; |
| 295 | } |
| 296 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 297 | if (surface->width > max_width || surface->height > max_height - y) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 298 | fprintf(stderr, |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 299 | "Graphic surface doesn't fit into the screen. width: %zu, height: %zu, max_width: %zu," |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 300 | " max_height: %zu, vertical offset: %d\n", |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 301 | surface->width, surface->height, max_width, max_height, y); |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 308 | ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} |
| 309 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 310 | constexpr int kDefaultMarginHeight = 0; |
| 311 | constexpr int kDefaultMarginWidth = 0; |
| 312 | constexpr int kDefaultAnimationFps = 30; |
| 313 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 314 | ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 315 | : margin_width_( |
| 316 | android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)), |
| 317 | margin_height_( |
| 318 | android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)), |
| 319 | animation_fps_( |
| 320 | android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)), |
| 321 | density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f), |
Michael Bestas | 1963086 | 2019-03-23 17:28:22 +0200 | [diff] [blame] | 322 | blank_unblank_on_init_( |
| 323 | android::base::GetBoolProperty("ro.recovery.ui.blank_unblank_on_init", false)), |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 324 | current_icon_(NONE), |
| 325 | current_frame_(0), |
| 326 | intro_done_(false), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 327 | progressBarType(EMPTY), |
| 328 | progressScopeStart(0), |
| 329 | progressScopeSize(0), |
| 330 | progress(0), |
| 331 | pagesIdentical(false), |
| 332 | text_cols_(0), |
| 333 | text_rows_(0), |
| 334 | text_(nullptr), |
| 335 | text_col_(0), |
| 336 | text_row_(0), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 337 | show_text(false), |
| 338 | show_text_ever(false), |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 339 | scrollable_menu_(scrollable_menu), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 340 | file_viewer_text_(nullptr), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 341 | stage(-1), |
| 342 | max_stage(-1), |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 343 | locale_(""), |
fredchiou | f2f5aa2 | 2022-02-11 16:39:53 +0800 | [diff] [blame] | 344 | rtl_locale_(false), |
| 345 | is_graphics_available(false) {} |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 346 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 347 | ScreenRecoveryUI::~ScreenRecoveryUI() { |
| 348 | progress_thread_stopped_ = true; |
Tao Bao | 94371fd | 2018-06-06 07:38:54 -0700 | [diff] [blame] | 349 | if (progress_thread_.joinable()) { |
| 350 | progress_thread_.join(); |
| 351 | } |
Tao Bao | 60ac622 | 2018-06-13 14:33:51 -0700 | [diff] [blame] | 352 | // No-op if gr_init() (via Init()) was not called or had failed. |
| 353 | gr_exit(); |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 356 | const GRSurface* ScreenRecoveryUI::GetCurrentFrame() const { |
| 357 | if (current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) { |
| 358 | return intro_done_ ? loop_frames_[current_frame_].get() : intro_frames_[current_frame_].get(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 359 | } |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 360 | return error_icon_.get(); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 363 | const GRSurface* ScreenRecoveryUI::GetCurrentText() const { |
| 364 | switch (current_icon_) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 365 | case ERASING: |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 366 | return erasing_text_.get(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 367 | case ERROR: |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 368 | return error_text_.get(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 369 | case INSTALLING_UPDATE: |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 370 | return installing_text_.get(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 371 | case NO_COMMAND: |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 372 | return no_command_text_.get(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 373 | case NONE: |
| 374 | abort(); |
| 375 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 378 | int ScreenRecoveryUI::PixelsFromDp(int dp) const { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 379 | return dp * density_; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Here's the intended layout: |
| 383 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 384 | // | portrait large landscape large |
| 385 | // ---------+------------------------------------------------- |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 386 | // gap | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 387 | // icon | (200dp) |
| 388 | // gap | 68dp 68dp 56dp 112dp |
| 389 | // text | (14sp) |
| 390 | // gap | 32dp 32dp 26dp 52dp |
| 391 | // progress | (2dp) |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 392 | // gap | |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 393 | |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 394 | // Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines |
| 395 | // 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] | 396 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 397 | 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] | 398 | enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX }; |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 399 | static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 400 | { 32, 68 }, // PORTRAIT |
| 401 | { 32, 68 }, // PORTRAIT_LARGE |
| 402 | { 26, 56 }, // LANDSCAPE |
| 403 | { 52, 112 }, // LANDSCAPE_LARGE |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 406 | int ScreenRecoveryUI::GetAnimationBaseline() const { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 407 | return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - |
| 408 | gr_get_height(loop_frames_[0].get()); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 411 | int ScreenRecoveryUI::GetTextBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 412 | return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 413 | gr_get_height(installing_text_.get()); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 416 | int ScreenRecoveryUI::GetProgressBaseline() const { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 417 | int elements_sum = gr_get_height(loop_frames_[0].get()) + PixelsFromDp(kLayouts[layout_][ICON]) + |
| 418 | gr_get_height(installing_text_.get()) + PixelsFromDp(kLayouts[layout_][TEXT]) + |
| 419 | gr_get_height(progress_bar_fill_.get()); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 420 | int bottom_gap = (ScreenHeight() - elements_sum) / 2; |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 421 | return ScreenHeight() - bottom_gap - gr_get_height(progress_bar_fill_.get()); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 424 | // Clear the screen and draw the currently selected background icon (if any). |
| 425 | // Should only be called with updateMutex locked. |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 426 | void ScreenRecoveryUI::draw_background_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 427 | pagesIdentical = false; |
| 428 | gr_color(0, 0, 0, 255); |
| 429 | gr_clear(); |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 430 | if (current_icon_ != NONE) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 431 | if (max_stage != -1) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 432 | int stage_height = gr_get_height(stage_marker_empty_.get()); |
| 433 | int stage_width = gr_get_width(stage_marker_empty_.get()); |
| 434 | int x = (ScreenWidth() - max_stage * gr_get_width(stage_marker_empty_.get())) / 2; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 435 | int y = ScreenHeight() - stage_height - margin_height_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 436 | for (int i = 0; i < max_stage; ++i) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 437 | const auto& stage_surface = (i < stage) ? stage_marker_fill_ : stage_marker_empty_; |
| 438 | DrawSurface(stage_surface.get(), 0, 0, stage_width, stage_height, x, y); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 439 | x += stage_width; |
| 440 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 441 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 442 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 443 | const auto& text_surface = GetCurrentText(); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 444 | int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 445 | int text_y = GetTextBaseline(); |
| 446 | gr_color(255, 255, 255, 255); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 447 | DrawTextIcon(text_x, text_y, text_surface); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 448 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 451 | // Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be |
| 452 | // called with updateMutex locked. |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 453 | void ScreenRecoveryUI::draw_foreground_locked() { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 454 | if (current_icon_ != NONE) { |
| 455 | const auto& frame = GetCurrentFrame(); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 456 | int frame_width = gr_get_width(frame); |
| 457 | int frame_height = gr_get_height(frame); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 458 | int frame_x = (ScreenWidth() - frame_width) / 2; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 459 | int frame_y = GetAnimationBaseline(); |
zhang sanshan | 0d81b89 | 2019-08-07 16:21:10 +0800 | [diff] [blame] | 460 | if (frame_x >= 0 && frame_y >= 0 && (frame_x + frame_width) < ScreenWidth() && |
| 461 | (frame_y + frame_height) < ScreenHeight()) |
| 462 | DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 463 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 464 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 465 | if (progressBarType != EMPTY) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 466 | int width = gr_get_width(progress_bar_empty_.get()); |
| 467 | int height = gr_get_height(progress_bar_empty_.get()); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 468 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 469 | int progress_x = (ScreenWidth() - width) / 2; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 470 | int progress_y = GetProgressBaseline(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 471 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 472 | // Erase behind the progress bar (in case this was a progress-only update) |
| 473 | gr_color(0, 0, 0, 255); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 474 | DrawFill(progress_x, progress_y, width, height); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 475 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 476 | if (progressBarType == DETERMINATE) { |
| 477 | float p = progressScopeStart + progress * progressScopeSize; |
| 478 | int pos = static_cast<int>(p * width); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 479 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 480 | if (rtl_locale_) { |
| 481 | // Fill the progress bar from right to left. |
| 482 | if (pos > 0) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 483 | DrawSurface(progress_bar_fill_.get(), width - pos, 0, pos, height, |
| 484 | progress_x + width - pos, progress_y); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 485 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 486 | if (pos < width - 1) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 487 | DrawSurface(progress_bar_empty_.get(), 0, 0, width - pos, height, progress_x, progress_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 488 | } |
| 489 | } else { |
| 490 | // Fill the progress bar from left to right. |
| 491 | if (pos > 0) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 492 | DrawSurface(progress_bar_fill_.get(), 0, 0, pos, height, progress_x, progress_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 493 | } |
| 494 | if (pos < width - 1) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 495 | DrawSurface(progress_bar_empty_.get(), pos, 0, width - pos, height, progress_x + pos, |
| 496 | progress_y); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 499 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 500 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 503 | void ScreenRecoveryUI::SetColor(UIElement e) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 504 | switch (e) { |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 505 | case UIElement::INFO: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 506 | gr_color(249, 194, 0, 255); |
| 507 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 508 | case UIElement::HEADER: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 509 | gr_color(247, 0, 6, 255); |
| 510 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 511 | case UIElement::MENU: |
| 512 | case UIElement::MENU_SEL_BG: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 513 | gr_color(0, 106, 157, 255); |
| 514 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 515 | case UIElement::MENU_SEL_BG_ACTIVE: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 516 | gr_color(0, 156, 100, 255); |
| 517 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 518 | case UIElement::MENU_SEL_FG: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 519 | gr_color(255, 255, 255, 255); |
| 520 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 521 | case UIElement::LOG: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 522 | gr_color(196, 196, 196, 255); |
| 523 | break; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 524 | case UIElement::TEXT_FILL: |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 525 | gr_color(0, 0, 0, 160); |
| 526 | break; |
| 527 | default: |
| 528 | gr_color(255, 255, 255, 255); |
| 529 | break; |
| 530 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 531 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 532 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 533 | void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries, |
| 534 | size_t sel) { |
| 535 | SetLocale(locales_entries[sel]); |
| 536 | std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text", |
| 537 | "installing_security_text", "no_command_text" }; |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 538 | std::unordered_map<std::string, std::unique_ptr<GRSurface>> surfaces; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 539 | for (const auto& name : text_name) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 540 | auto text_image = LoadLocalizedBitmap(name); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 541 | if (!text_image) { |
| 542 | Print("Failed to load %s\n", name.c_str()); |
| 543 | return; |
| 544 | } |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 545 | surfaces.emplace(name, std::move(text_image)); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 548 | std::lock_guard<std::mutex> lg(updateMutex); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 549 | gr_color(0, 0, 0, 255); |
| 550 | gr_clear(); |
| 551 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 552 | int text_y = margin_height_; |
| 553 | int text_x = margin_width_; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 554 | int line_spacing = gr_sys_font()->char_height; // Put some extra space between images. |
| 555 | // Write the header and descriptive texts. |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 556 | SetColor(UIElement::INFO); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 557 | std::string header = "Show background text image"; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 558 | text_y += DrawTextLine(text_x, text_y, header, true); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 559 | std::string locale_selection = android::base::StringPrintf( |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 560 | "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] | 561 | // clang-format off |
| 562 | std::vector<std::string> instruction = { |
| 563 | locale_selection, |
| 564 | "Use volume up/down to switch locales and power to exit." |
| 565 | }; |
| 566 | // clang-format on |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 567 | text_y += DrawWrappedTextLines(text_x, text_y, instruction); |
| 568 | |
| 569 | // Iterate through the text images and display them in order for the current locale. |
| 570 | for (const auto& p : surfaces) { |
| 571 | text_y += line_spacing; |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 572 | SetColor(UIElement::LOG); |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 573 | text_y += DrawTextLine(text_x, text_y, p.first, false); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 574 | gr_color(255, 255, 255, 255); |
| 575 | gr_texticon(text_x, text_y, p.second.get()); |
| 576 | text_y += gr_get_height(p.second.get()); |
| 577 | } |
| 578 | // Update the whole screen. |
| 579 | gr_flip(); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 582 | void ScreenRecoveryUI::CheckBackgroundTextImages() { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 583 | // Load a list of locales embedded in one of the resource files. |
| 584 | std::vector<std::string> locales_entries = get_locales_in_png("installing_text"); |
| 585 | if (locales_entries.empty()) { |
| 586 | Print("Failed to load locales from the resource files\n"); |
| 587 | return; |
| 588 | } |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 589 | std::string saved_locale = locale_; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 590 | size_t selected = 0; |
| 591 | SelectAndShowBackgroundText(locales_entries, selected); |
| 592 | |
| 593 | FlushKeys(); |
| 594 | while (true) { |
| 595 | int key = WaitKey(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 596 | if (key == static_cast<int>(KeyError::INTERRUPTED)) break; |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 597 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 598 | break; |
| 599 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 600 | selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1; |
| 601 | SelectAndShowBackgroundText(locales_entries, selected); |
| 602 | } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) { |
| 603 | selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1; |
| 604 | SelectAndShowBackgroundText(locales_entries, selected); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | SetLocale(saved_locale); |
| 609 | } |
| 610 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 611 | int ScreenRecoveryUI::ScreenWidth() const { |
| 612 | return gr_fb_width(); |
| 613 | } |
| 614 | |
| 615 | int ScreenRecoveryUI::ScreenHeight() const { |
| 616 | return gr_fb_height(); |
| 617 | } |
| 618 | |
Tao Bao | 65815b6 | 2018-10-23 10:54:02 -0700 | [diff] [blame] | 619 | void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 620 | int dy) const { |
| 621 | gr_blit(surface, sx, sy, w, h, dx, dy); |
| 622 | } |
| 623 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 624 | int ScreenRecoveryUI::DrawHorizontalRule(int y) const { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 625 | gr_fill(0, y + 4, ScreenWidth(), y + 6); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 626 | return 8; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 629 | void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 630 | gr_fill(x, y, x + width, y + height); |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 633 | void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const { |
| 634 | gr_fill(x, y, w, h); |
| 635 | } |
| 636 | |
Tao Bao | 65815b6 | 2018-10-23 10:54:02 -0700 | [diff] [blame] | 637 | void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 638 | gr_texticon(x, y, surface); |
| 639 | } |
| 640 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 641 | int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const { |
| 642 | gr_text(gr_sys_font(), x, y, line.c_str(), bold); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 643 | return char_height_ + 4; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 646 | 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] | 647 | int offset = 0; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 648 | for (const auto& line : lines) { |
| 649 | offset += DrawTextLine(x, y + offset, line, false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 650 | } |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 651 | return offset; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 654 | int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, |
| 655 | const std::vector<std::string>& lines) const { |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 656 | // Keep symmetrical margins based on the given offset (i.e. x). |
| 657 | size_t text_cols = (ScreenWidth() - x * 2) / char_width_; |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 658 | int offset = 0; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 659 | for (const auto& line : lines) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 660 | size_t next_start = 0; |
| 661 | while (next_start < line.size()) { |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 662 | std::string sub = line.substr(next_start, text_cols + 1); |
| 663 | if (sub.size() <= text_cols) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 664 | next_start += sub.size(); |
| 665 | } else { |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 666 | // Line too long and must be wrapped to text_cols columns. |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 667 | size_t last_space = sub.find_last_of(" \t\n"); |
| 668 | if (last_space == std::string::npos) { |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 669 | // No space found, just draw as much as we can. |
Tao Bao | 452b487 | 2018-05-09 11:52:09 -0700 | [diff] [blame] | 670 | sub.resize(text_cols); |
| 671 | next_start += text_cols; |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 672 | } else { |
| 673 | sub.resize(last_space); |
| 674 | next_start += last_space + 1; |
| 675 | } |
| 676 | } |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 677 | offset += DrawTextLine(x, y + offset, sub, false); |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | return offset; |
| 681 | } |
| 682 | |
Jerry Zhang | 0e577ee | 2018-05-07 11:21:10 -0700 | [diff] [blame] | 683 | void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) { |
| 684 | title_lines_ = lines; |
| 685 | } |
| 686 | |
Tianjie Xu | e503221 | 2019-07-23 13:23:29 -0700 | [diff] [blame] | 687 | std::vector<std::string> ScreenRecoveryUI::GetMenuHelpMessage() const { |
| 688 | // clang-format off |
| 689 | static std::vector<std::string> REGULAR_HELP{ |
| 690 | "Use volume up/down and power.", |
| 691 | }; |
| 692 | static std::vector<std::string> LONG_PRESS_HELP{ |
| 693 | "Any button cycles highlight.", |
| 694 | "Long-press activates.", |
| 695 | }; |
| 696 | // clang-format on |
| 697 | return HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP; |
| 698 | } |
| 699 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 700 | // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex |
| 701 | // locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 702 | void ScreenRecoveryUI::draw_screen_locked() { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 703 | if (!show_text) { |
| 704 | draw_background_locked(); |
| 705 | draw_foreground_locked(); |
| 706 | return; |
| 707 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 708 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 709 | gr_color(0, 0, 0, 255); |
| 710 | gr_clear(); |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 711 | |
Tianjie Xu | e503221 | 2019-07-23 13:23:29 -0700 | [diff] [blame] | 712 | draw_menu_and_text_buffer_locked(GetMenuHelpMessage()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // 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] | 716 | void ScreenRecoveryUI::draw_menu_and_text_buffer_locked( |
| 717 | const std::vector<std::string>& help_message) { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 718 | int y = margin_height_; |
David Anderson | 983e2d5 | 2019-01-02 11:35:38 -0800 | [diff] [blame] | 719 | |
| 720 | if (fastbootd_logo_ && fastbootd_logo_enabled_) { |
| 721 | // Try to get this centered on screen. |
| 722 | auto width = gr_get_width(fastbootd_logo_.get()); |
| 723 | auto height = gr_get_height(fastbootd_logo_.get()); |
| 724 | auto centered_x = ScreenWidth() / 2 - width / 2; |
| 725 | DrawSurface(fastbootd_logo_.get(), 0, 0, width, height, centered_x, y); |
| 726 | y += height; |
| 727 | } |
| 728 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 729 | if (menu_) { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 730 | int x = margin_width_ + kMenuIndent; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 731 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 732 | SetColor(UIElement::INFO); |
Jerry Zhang | 0e577ee | 2018-05-07 11:21:10 -0700 | [diff] [blame] | 733 | |
| 734 | for (size_t i = 0; i < title_lines_.size(); i++) { |
| 735 | y += DrawTextLine(x, y, title_lines_[i], i == 0); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 736 | } |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 737 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 738 | y += DrawTextLines(x, y, help_message); |
| 739 | |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 740 | y += menu_->DrawHeader(x, y); |
| 741 | y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress()); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or |
| 745 | // we've displayed the entire text buffer. |
Tianjie Xu | 66dbf63 | 2018-10-11 16:54:50 -0700 | [diff] [blame] | 746 | SetColor(UIElement::LOG); |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 747 | int row = text_row_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 748 | size_t count = 0; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 749 | 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] | 750 | ty -= char_height_, ++count) { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 751 | DrawTextLine(margin_width_, ty, text_[row], false); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 752 | --row; |
| 753 | if (row < 0) row = text_rows_ - 1; |
| 754 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // Redraw everything on the screen and flip the screen (make it visible). |
| 758 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 759 | void ScreenRecoveryUI::update_screen_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 760 | draw_screen_locked(); |
| 761 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 765 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 766 | void ScreenRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 767 | if (show_text || !pagesIdentical) { |
| 768 | draw_screen_locked(); // Must redraw the whole screen |
| 769 | pagesIdentical = true; |
| 770 | } else { |
| 771 | draw_foreground_locked(); // Draw only the progress bar and overlays |
| 772 | } |
| 773 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 776 | void ScreenRecoveryUI::ProgressThreadLoop() { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 777 | double interval = 1.0 / animation_fps_; |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 778 | while (!progress_thread_stopped_) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 779 | double start = now(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 780 | bool redraw = false; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 781 | { |
| 782 | std::lock_guard<std::mutex> lg(updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 783 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 784 | // update the installation animation, if active |
| 785 | // skip this if we have a text overlay (too expensive to update) |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 786 | if ((current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) && !show_text) { |
| 787 | if (!intro_done_) { |
| 788 | if (current_frame_ == intro_frames_.size() - 1) { |
| 789 | intro_done_ = true; |
| 790 | current_frame_ = 0; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 791 | } else { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 792 | ++current_frame_; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 793 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 794 | } else { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 795 | current_frame_ = (current_frame_ + 1) % loop_frames_.size(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 798 | redraw = true; |
| 799 | } |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 800 | |
| 801 | // move the progress bar forward on timed intervals, if configured |
| 802 | int duration = progressScopeDuration; |
| 803 | if (progressBarType == DETERMINATE && duration > 0) { |
| 804 | double elapsed = now() - progressScopeTime; |
| 805 | float p = 1.0 * elapsed / duration; |
| 806 | if (p > 1.0) p = 1.0; |
| 807 | if (p > progress) { |
| 808 | progress = p; |
| 809 | redraw = true; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | if (redraw) update_progress_locked(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 816 | double end = now(); |
| 817 | // minimum of 20ms delay between frames |
| 818 | double delay = interval - (end - start); |
| 819 | if (delay < 0.02) delay = 0.02; |
| 820 | usleep(static_cast<useconds_t>(delay * 1000000)); |
| 821 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 824 | std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadBitmap(const std::string& filename) { |
| 825 | GRSurface* surface; |
| 826 | if (auto result = res_create_display_surface(filename.c_str(), &surface); result < 0) { |
| 827 | LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")"; |
| 828 | return nullptr; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 829 | } |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 830 | return std::unique_ptr<GRSurface>(surface); |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 833 | std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) { |
| 834 | GRSurface* surface; |
xunchang | 9d05c8a | 2019-04-16 12:07:42 -0700 | [diff] [blame] | 835 | auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface); |
| 836 | if (result == 0) { |
| 837 | return std::unique_ptr<GRSurface>(surface); |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 838 | } |
xunchang | 9d05c8a | 2019-04-16 12:07:42 -0700 | [diff] [blame] | 839 | // TODO(xunchang) create a error code enum to refine the retry condition. |
| 840 | LOG(WARNING) << "Failed to load bitmap " << filename << " for locale " << locale_ << " (error " |
| 841 | << result << "). Falling back to use default locale."; |
| 842 | |
| 843 | result = res_create_localized_alpha_surface(filename.c_str(), DEFAULT_LOCALE, &surface); |
| 844 | if (result == 0) { |
| 845 | return std::unique_ptr<GRSurface>(surface); |
| 846 | } |
| 847 | |
| 848 | LOG(ERROR) << "Failed to load bitmap " << filename << " for locale " << DEFAULT_LOCALE |
| 849 | << " (error " << result << ")"; |
| 850 | return nullptr; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 853 | static char** Alloc2d(size_t rows, size_t cols) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 854 | char** result = new char*[rows]; |
| 855 | for (size_t i = 0; i < rows; ++i) { |
| 856 | result[i] = new char[cols]; |
| 857 | memset(result[i], 0, cols); |
| 858 | } |
| 859 | return result; |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 862 | // Choose the right background string to display during update. |
| 863 | void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 864 | if (security_update) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 865 | installing_text_ = LoadLocalizedBitmap("installing_security_text"); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 866 | } else { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 867 | installing_text_ = LoadLocalizedBitmap("installing_text"); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 868 | } |
| 869 | Redraw(); |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 872 | bool ScreenRecoveryUI::InitTextParams() { |
Tao Bao | ba4edb3 | 2018-06-13 11:22:50 -0700 | [diff] [blame] | 873 | // gr_init() would return successfully on font initialization failure. |
| 874 | if (gr_sys_font() == nullptr) { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 875 | return false; |
| 876 | } |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 877 | gr_font_size(gr_sys_font(), &char_width_, &char_height_); |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 878 | text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_; |
| 879 | text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 880 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 883 | bool ScreenRecoveryUI::LoadWipeDataMenuText() { |
Tianjie Xu | 1a0a30a | 2018-10-25 15:22:07 -0700 | [diff] [blame] | 884 | // Ignores the errors since the member variables will stay as nullptr. |
| 885 | cancel_wipe_data_text_ = LoadLocalizedBitmap("cancel_wipe_data_text"); |
| 886 | factory_data_reset_text_ = LoadLocalizedBitmap("factory_data_reset_text"); |
| 887 | try_again_text_ = LoadLocalizedBitmap("try_again_text"); |
| 888 | wipe_data_confirmation_text_ = LoadLocalizedBitmap("wipe_data_confirmation_text"); |
| 889 | wipe_data_menu_header_text_ = LoadLocalizedBitmap("wipe_data_menu_header_text"); |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 890 | return true; |
| 891 | } |
| 892 | |
Mark Salyzyn | 30017e7 | 2020-05-13 12:39:12 -0700 | [diff] [blame] | 893 | static bool InitGraphics() { |
| 894 | // Timeout is same as init wait for file default of 5 seconds and is arbitrary |
| 895 | const unsigned timeout = 500; // 10ms increments |
| 896 | for (auto retry = timeout; retry > 0; --retry) { |
| 897 | if (gr_init() == 0) { |
| 898 | if (retry < timeout) { |
| 899 | // Log message like init wait for file completion log for consistency. |
| 900 | LOG(WARNING) << "wait for 'graphics' took " << ((timeout - retry) * 10) << "ms"; |
| 901 | } |
| 902 | return true; |
| 903 | } |
| 904 | std::this_thread::sleep_for(10ms); |
| 905 | } |
| 906 | // Log message like init wait for file timeout log for consistency. |
| 907 | LOG(ERROR) << "timeout wait for 'graphics' took " << (timeout * 10) << "ms"; |
| 908 | return false; |
| 909 | } |
| 910 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 911 | bool ScreenRecoveryUI::Init(const std::string& locale) { |
| 912 | RecoveryUI::Init(locale); |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 913 | |
Mark Salyzyn | 30017e7 | 2020-05-13 12:39:12 -0700 | [diff] [blame] | 914 | if (!InitGraphics()) { |
Tao Bao | ba4edb3 | 2018-06-13 11:22:50 -0700 | [diff] [blame] | 915 | return false; |
| 916 | } |
fredchiou | f2f5aa2 | 2022-02-11 16:39:53 +0800 | [diff] [blame] | 917 | is_graphics_available = true; |
Tao Bao | ba4edb3 | 2018-06-13 11:22:50 -0700 | [diff] [blame] | 918 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 919 | if (!InitTextParams()) { |
| 920 | return false; |
| 921 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 922 | |
Michael Bestas | 1963086 | 2019-03-23 17:28:22 +0200 | [diff] [blame] | 923 | if (blank_unblank_on_init_) { |
| 924 | gr_fb_blank(true); |
| 925 | gr_fb_blank(false); |
| 926 | } |
| 927 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 928 | // Are we portrait or landscape? |
| 929 | layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; |
| 930 | // Are we the large variant of our base layout? |
| 931 | if (gr_fb_height() > PixelsFromDp(800)) ++layout_; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 932 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 933 | text_ = Alloc2d(text_rows_, text_cols_ + 1); |
| 934 | file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 935 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 936 | text_col_ = text_row_ = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 937 | |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 938 | // Set up the locale info. |
| 939 | SetLocale(locale); |
| 940 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 941 | error_icon_ = LoadBitmap("icon_error"); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 942 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 943 | progress_bar_empty_ = LoadBitmap("progress_empty"); |
| 944 | progress_bar_fill_ = LoadBitmap("progress_fill"); |
| 945 | stage_marker_empty_ = LoadBitmap("stage_empty"); |
| 946 | stage_marker_fill_ = LoadBitmap("stage_fill"); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 947 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 948 | erasing_text_ = LoadLocalizedBitmap("erasing_text"); |
| 949 | no_command_text_ = LoadLocalizedBitmap("no_command_text"); |
| 950 | error_text_ = LoadLocalizedBitmap("error_text"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 951 | |
Alessandro Astone | 957d98e | 2020-02-26 17:25:54 +0100 | [diff] [blame^] | 952 | if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) || |
| 953 | android::base::GetBoolProperty("ro.fastbootd.available", false)) { |
David Anderson | 983e2d5 | 2019-01-02 11:35:38 -0800 | [diff] [blame] | 954 | fastbootd_logo_ = LoadBitmap("fastbootd"); |
| 955 | } |
| 956 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 957 | // Background text for "installing_update" could be "installing update" or |
| 958 | // "installing security update". It will be set after Init() according to the commands in BCB. |
| 959 | installing_text_.reset(); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 960 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 961 | LoadWipeDataMenuText(); |
| 962 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 963 | LoadAnimation(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 964 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 965 | // Keep the progress bar updated, even when the process is otherwise busy. |
| 966 | progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this); |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 967 | |
fredchiou | f2f5aa2 | 2022-02-11 16:39:53 +0800 | [diff] [blame] | 968 | // set the callback for hall sensor event |
| 969 | (void)ev_sync_sw_state([this](auto&& a, auto&& b) { return this->SetSwCallback(a, b);}); |
| 970 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 971 | return true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 974 | std::string ScreenRecoveryUI::GetLocale() const { |
Jerry Zhang | 2dea53e | 2018-05-02 17:15:03 -0700 | [diff] [blame] | 975 | return locale_; |
| 976 | } |
| 977 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 978 | void ScreenRecoveryUI::LoadAnimation() { |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 979 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()), |
| 980 | closedir); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 981 | dirent* de; |
| 982 | std::vector<std::string> intro_frame_names; |
Joshua Lambert | 94a83be | 2019-12-09 20:24:18 +0000 | [diff] [blame] | 983 | std::vector<std::string> loop_frame_names; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 984 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 985 | while ((de = readdir(dir.get())) != nullptr) { |
| 986 | int value, num_chars; |
| 987 | if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { |
| 988 | intro_frame_names.emplace_back(de->d_name, num_chars); |
| 989 | } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { |
Joshua Lambert | 94a83be | 2019-12-09 20:24:18 +0000 | [diff] [blame] | 990 | loop_frame_names.emplace_back(de->d_name, num_chars); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 991 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 992 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 993 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 994 | size_t intro_frames = intro_frame_names.size(); |
| 995 | size_t loop_frames = loop_frame_names.size(); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 996 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 997 | // It's okay to not have an intro. |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 998 | if (intro_frames == 0) intro_done_ = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 999 | // But you must have an animation. |
| 1000 | if (loop_frames == 0) abort(); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 1001 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1002 | std::sort(intro_frame_names.begin(), intro_frame_names.end()); |
| 1003 | std::sort(loop_frame_names.begin(), loop_frame_names.end()); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 1004 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 1005 | intro_frames_.clear(); |
| 1006 | intro_frames_.reserve(intro_frames); |
| 1007 | for (const auto& frame_name : intro_frame_names) { |
| 1008 | intro_frames_.emplace_back(LoadBitmap(frame_name)); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1009 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 1010 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 1011 | loop_frames_.clear(); |
| 1012 | loop_frames_.reserve(loop_frames); |
| 1013 | for (const auto& frame_name : loop_frame_names) { |
| 1014 | loop_frames_.emplace_back(LoadBitmap(frame_name)); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1015 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1018 | void ScreenRecoveryUI::SetBackground(Icon icon) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1019 | std::lock_guard<std::mutex> lg(updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1020 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 1021 | current_icon_ = icon; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1022 | update_screen_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1025 | void ScreenRecoveryUI::SetProgressType(ProgressType type) { |
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 | if (progressBarType != type) { |
| 1028 | progressBarType = type; |
| 1029 | } |
| 1030 | progressScopeStart = 0; |
| 1031 | progressScopeSize = 0; |
| 1032 | progress = 0; |
| 1033 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1036 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1037 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1038 | progressBarType = DETERMINATE; |
| 1039 | progressScopeStart += progressScopeSize; |
| 1040 | progressScopeSize = portion; |
| 1041 | progressScopeTime = now(); |
| 1042 | progressScopeDuration = seconds; |
| 1043 | progress = 0; |
| 1044 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1047 | void ScreenRecoveryUI::SetProgress(float fraction) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1048 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1049 | if (fraction < 0.0) fraction = 0.0; |
| 1050 | if (fraction > 1.0) fraction = 1.0; |
| 1051 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 1052 | // Skip updates that aren't visibly different. |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 1053 | int width = gr_get_width(progress_bar_empty_.get()); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1054 | float scale = width * progressScopeSize; |
| 1055 | if ((int)(progress * scale) != (int)(fraction * scale)) { |
| 1056 | progress = fraction; |
| 1057 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1058 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1059 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1062 | void ScreenRecoveryUI::SetStage(int current, int max) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1063 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1064 | stage = current; |
| 1065 | max_stage = max; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1068 | 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] | 1069 | std::string str; |
| 1070 | android::base::StringAppendV(&str, fmt, ap); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1071 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1072 | if (copy_to_stdout) { |
| 1073 | fputs(str.c_str(), stdout); |
| 1074 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1075 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1076 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1077 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 1078 | for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { |
| 1079 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1080 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1081 | text_col_ = 0; |
| 1082 | text_row_ = (text_row_ + 1) % text_rows_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1083 | } |
| 1084 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1085 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1086 | text_[text_row_][text_col_] = '\0'; |
| 1087 | update_screen_locked(); |
| 1088 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1091 | void ScreenRecoveryUI::Print(const char* fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1092 | va_list ap; |
| 1093 | va_start(ap, fmt); |
| 1094 | PrintV(fmt, true, ap); |
| 1095 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 1098 | void ScreenRecoveryUI::PrintOnScreenOnly(const char* fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1099 | va_list ap; |
| 1100 | va_start(ap, fmt); |
| 1101 | PrintV(fmt, false, ap); |
| 1102 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1105 | void ScreenRecoveryUI::PutChar(char ch) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1106 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1107 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 1108 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 1109 | text_col_ = 0; |
| 1110 | ++text_row_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1111 | } |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1114 | void ScreenRecoveryUI::ClearText() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1115 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1116 | text_col_ = 0; |
| 1117 | text_row_ = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1118 | for (size_t i = 0; i < text_rows_; ++i) { |
| 1119 | memset(text_[i], 0, text_cols_ + 1); |
| 1120 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void ScreenRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1124 | std::vector<off_t> offsets; |
| 1125 | offsets.push_back(ftello(fp)); |
| 1126 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1127 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1128 | struct stat sb; |
| 1129 | fstat(fileno(fp), &sb); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1130 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1131 | bool show_prompt = false; |
| 1132 | while (true) { |
| 1133 | if (show_prompt) { |
| 1134 | PrintOnScreenOnly("--(%d%% of %d bytes)--", |
| 1135 | static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))), |
| 1136 | static_cast<int>(sb.st_size)); |
| 1137 | Redraw(); |
| 1138 | while (show_prompt) { |
| 1139 | show_prompt = false; |
| 1140 | int key = WaitKey(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1141 | if (key == static_cast<int>(KeyError::INTERRUPTED)) return; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1142 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 1143 | return; |
| 1144 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 1145 | if (offsets.size() <= 1) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1146 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1147 | } else { |
| 1148 | offsets.pop_back(); |
| 1149 | fseek(fp, offsets.back(), SEEK_SET); |
| 1150 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1151 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1152 | if (feof(fp)) { |
| 1153 | return; |
| 1154 | } |
| 1155 | offsets.push_back(ftello(fp)); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1156 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1157 | } |
| 1158 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1159 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1160 | |
| 1161 | int ch = getc(fp); |
| 1162 | if (ch == EOF) { |
| 1163 | while (text_row_ < text_rows_ - 1) PutChar('\n'); |
| 1164 | show_prompt = true; |
| 1165 | } else { |
| 1166 | PutChar(ch); |
| 1167 | if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { |
| 1168 | show_prompt = true; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Tao Bao | 1d156b9 | 2018-05-02 12:43:18 -0700 | [diff] [blame] | 1174 | void ScreenRecoveryUI::ShowFile(const std::string& filename) { |
| 1175 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose); |
| 1176 | if (!fp) { |
| 1177 | Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno)); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1178 | return; |
| 1179 | } |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1180 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1181 | char** old_text = text_; |
| 1182 | size_t old_text_col = text_col_; |
| 1183 | size_t old_text_row = text_row_; |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1184 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1185 | // Swap in the alternate screen and clear it. |
| 1186 | text_ = file_viewer_text_; |
| 1187 | ClearText(); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1188 | |
Tao Bao | 1d156b9 | 2018-05-02 12:43:18 -0700 | [diff] [blame] | 1189 | ShowFile(fp.get()); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1190 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1191 | text_ = old_text; |
| 1192 | text_col_ = old_text_col; |
| 1193 | text_row_ = old_text_row; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 1196 | std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu( |
| 1197 | const GRSurface* graphic_header, const std::vector<const GRSurface*>& graphic_items, |
| 1198 | const std::vector<std::string>& text_headers, const std::vector<std::string>& text_items, |
| 1199 | size_t initial_selection) const { |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1200 | // horizontal unusable area: margin width + menu indent |
| 1201 | size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent; |
| 1202 | // vertical unusable area: margin height + title lines + helper message + high light bar. |
| 1203 | // It is safe to reserve more space. |
| 1204 | size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3); |
| 1205 | if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) { |
| 1206 | return std::make_unique<GraphicMenu>(graphic_header, graphic_items, initial_selection, *this); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1207 | } |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1208 | |
| 1209 | fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n"); |
| 1210 | |
| 1211 | return CreateMenu(text_headers, text_items, initial_selection); |
| 1212 | } |
| 1213 | |
| 1214 | std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(const std::vector<std::string>& text_headers, |
| 1215 | const std::vector<std::string>& text_items, |
| 1216 | size_t initial_selection) const { |
| 1217 | if (text_rows_ > 0 && text_cols_ > 1) { |
| 1218 | return std::make_unique<TextMenu>(scrollable_menu_, text_rows_, text_cols_ - 1, text_headers, |
| 1219 | text_items, initial_selection, char_height_, *this); |
| 1220 | } |
| 1221 | |
| 1222 | fprintf(stderr, "Failed to create text menu, text_rows %zu, text_cols %zu.\n", text_rows_, |
| 1223 | text_cols_); |
| 1224 | return nullptr; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | int ScreenRecoveryUI::SelectMenu(int sel) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1228 | std::lock_guard<std::mutex> lg(updateMutex); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 1229 | if (menu_) { |
| 1230 | int old_sel = menu_->selection(); |
| 1231 | sel = menu_->Select(sel); |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 1232 | |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 1233 | if (sel != old_sel) { |
| 1234 | update_screen_locked(); |
| 1235 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1236 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1237 | return sel; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1240 | size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr<Menu>&& menu, bool menu_only, |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 1241 | const std::function<int(int, bool)>& key_handler) { |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1242 | // Throw away keys pressed previously, so user doesn't accidentally trigger menu items. |
| 1243 | FlushKeys(); |
| 1244 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1245 | // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the |
| 1246 | // menu. |
| 1247 | if (IsKeyInterrupted()) return static_cast<size_t>(KeyError::INTERRUPTED); |
| 1248 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1249 | CHECK(menu != nullptr); |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1250 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1251 | // Starts and displays the menu |
| 1252 | menu_ = std::move(menu); |
| 1253 | Redraw(); |
| 1254 | |
| 1255 | int selected = menu_->selection(); |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1256 | int chosen_item = -1; |
| 1257 | while (chosen_item < 0) { |
| 1258 | int key = WaitKey(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1259 | if (key == static_cast<int>(KeyError::INTERRUPTED)) { // WaitKey() was interrupted. |
| 1260 | return static_cast<size_t>(KeyError::INTERRUPTED); |
| 1261 | } |
| 1262 | if (key == static_cast<int>(KeyError::TIMED_OUT)) { // WaitKey() timed out. |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1263 | if (WasTextEverVisible()) { |
| 1264 | continue; |
| 1265 | } else { |
| 1266 | LOG(INFO) << "Timed out waiting for key input; rebooting."; |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1267 | menu_.reset(); |
| 1268 | Redraw(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 1269 | return static_cast<size_t>(KeyError::TIMED_OUT); |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | bool visible = IsTextVisible(); |
| 1274 | int action = key_handler(key, visible); |
| 1275 | if (action < 0) { |
| 1276 | switch (action) { |
| 1277 | case Device::kHighlightUp: |
| 1278 | selected = SelectMenu(--selected); |
| 1279 | break; |
| 1280 | case Device::kHighlightDown: |
| 1281 | selected = SelectMenu(++selected); |
| 1282 | break; |
| 1283 | case Device::kInvokeItem: |
| 1284 | chosen_item = selected; |
| 1285 | break; |
| 1286 | case Device::kNoAction: |
| 1287 | break; |
Tom Marshall | f20b3e3 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 1288 | case Device::kGoBack: |
| 1289 | chosen_item = Device::kGoBack; |
| 1290 | break; |
| 1291 | case Device::kGoHome: |
| 1292 | chosen_item = Device::kGoHome; |
| 1293 | break; |
Tom Marshall | 9f2f328 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 1294 | case Device::kDoSideload: |
| 1295 | chosen_item = Device::kDoSideload; |
| 1296 | break; |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1297 | } |
| 1298 | } else if (!menu_only) { |
| 1299 | chosen_item = action; |
| 1300 | } |
Tom Marshall | 9f2f328 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 1301 | |
| 1302 | if (chosen_item == Device::kGoBack || chosen_item == Device::kGoHome || |
| 1303 | chosen_item == Device::kDoSideload) { |
Tom Marshall | f20b3e3 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 1304 | break; |
| 1305 | } |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1308 | menu_.reset(); |
| 1309 | Redraw(); |
| 1310 | |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 1311 | return chosen_item; |
| 1312 | } |
| 1313 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1314 | size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers, |
| 1315 | const std::vector<std::string>& items, size_t initial_selection, |
| 1316 | bool menu_only, |
| 1317 | const std::function<int(int, bool)>& key_handler) { |
| 1318 | auto menu = CreateMenu(headers, items, initial_selection); |
| 1319 | if (menu == nullptr) { |
| 1320 | return initial_selection; |
| 1321 | } |
| 1322 | |
Tao Bao | fa8e02a | 2019-06-27 09:07:04 -0700 | [diff] [blame] | 1323 | return ShowMenu(std::move(menu), menu_only, key_handler); |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector<std::string>& backup_headers, |
| 1327 | const std::vector<std::string>& backup_items, |
| 1328 | const std::function<int(int, bool)>& key_handler) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 1329 | auto wipe_data_menu = CreateMenu(wipe_data_menu_header_text_.get(), |
| 1330 | { try_again_text_.get(), factory_data_reset_text_.get() }, |
| 1331 | backup_headers, backup_items, 0); |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 1332 | if (wipe_data_menu == nullptr) { |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | return ShowMenu(std::move(wipe_data_menu), true, key_handler); |
| 1337 | } |
| 1338 | |
Tianjie Xu | 1a0a30a | 2018-10-25 15:22:07 -0700 | [diff] [blame] | 1339 | size_t ScreenRecoveryUI::ShowPromptWipeDataConfirmationMenu( |
| 1340 | const std::vector<std::string>& backup_headers, const std::vector<std::string>& backup_items, |
| 1341 | const std::function<int(int, bool)>& key_handler) { |
| 1342 | auto confirmation_menu = |
| 1343 | CreateMenu(wipe_data_confirmation_text_.get(), |
| 1344 | { cancel_wipe_data_text_.get(), factory_data_reset_text_.get() }, backup_headers, |
| 1345 | backup_items, 0); |
| 1346 | if (confirmation_menu == nullptr) { |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | return ShowMenu(std::move(confirmation_menu), true, key_handler); |
| 1351 | } |
| 1352 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1353 | bool ScreenRecoveryUI::IsTextVisible() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1354 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1355 | int visible = show_text; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1356 | return visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1359 | bool ScreenRecoveryUI::WasTextEverVisible() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1360 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1361 | int ever_visible = show_text_ever; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1362 | return ever_visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1365 | void ScreenRecoveryUI::ShowText(bool visible) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1366 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1367 | show_text = visible; |
| 1368 | if (show_text) show_text_ever = true; |
| 1369 | update_screen_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1370 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1371 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1372 | void ScreenRecoveryUI::Redraw() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 1373 | std::lock_guard<std::mutex> lg(updateMutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1374 | update_screen_locked(); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1375 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 1376 | |
| 1377 | void ScreenRecoveryUI::KeyLongPress(int) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1378 | // Redraw so that if we're in the menu, the highlight |
| 1379 | // will change color to indicate a successful long press. |
| 1380 | Redraw(); |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 1381 | } |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 1382 | |
| 1383 | void ScreenRecoveryUI::SetLocale(const std::string& new_locale) { |
| 1384 | locale_ = new_locale; |
| 1385 | rtl_locale_ = false; |
| 1386 | |
| 1387 | if (!new_locale.empty()) { |
Tao Bao | 347a659 | 2018-05-08 15:58:29 -0700 | [diff] [blame] | 1388 | size_t separator = new_locale.find('-'); |
| 1389 | // lang has the language prefix prior to the separator, or full string if none exists. |
| 1390 | std::string lang = new_locale.substr(0, separator); |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 1391 | |
| 1392 | // A bit cheesy: keep an explicit list of supported RTL languages. |
| 1393 | if (lang == "ar" || // Arabic |
| 1394 | lang == "fa" || // Persian (Farsi) |
| 1395 | lang == "he" || // Hebrew (new language code) |
| 1396 | lang == "iw" || // Hebrew (old language code) |
| 1397 | lang == "ur") { // Urdu |
| 1398 | rtl_locale_ = true; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
fredchiou | f2f5aa2 | 2022-02-11 16:39:53 +0800 | [diff] [blame] | 1402 | |
| 1403 | int ScreenRecoveryUI::SetSwCallback(int code, int value) { |
| 1404 | if (!is_graphics_available) { return -1; } |
| 1405 | if (code > SW_MAX) { return -1; } |
| 1406 | if (code != SW_LID) { return 0; } |
| 1407 | |
| 1408 | /* detect dual display */ |
| 1409 | if (!gr_has_multiple_connectors()) { return -1; } |
| 1410 | |
| 1411 | /* turn off all screen */ |
| 1412 | gr_fb_blank(true, DirectRenderManager::DRM_INNER); |
| 1413 | gr_fb_blank(true, DirectRenderManager::DRM_OUTER); |
| 1414 | gr_color(0, 0, 0, 255); |
| 1415 | gr_clear(); |
| 1416 | |
| 1417 | /* turn on the screen */ |
| 1418 | gr_fb_blank(false, value); |
| 1419 | gr_flip(); |
| 1420 | |
| 1421 | /* set the retation */ |
| 1422 | std::string rotation_str; |
| 1423 | if (value == DirectRenderManager::DRM_OUTER) { |
| 1424 | rotation_str = |
| 1425 | android::base::GetProperty("ro.minui.second_rotation", "ROTATION_NONE"); |
| 1426 | } else { |
| 1427 | rotation_str = |
| 1428 | android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE"); |
| 1429 | } |
| 1430 | |
| 1431 | if (rotation_str == "ROTATION_RIGHT") { |
| 1432 | gr_rotate(GRRotation::RIGHT); |
| 1433 | } else if (rotation_str == "ROTATION_DOWN") { |
| 1434 | gr_rotate(GRRotation::DOWN); |
| 1435 | } else if (rotation_str == "ROTATION_LEFT") { |
| 1436 | gr_rotate(GRRotation::LEFT); |
| 1437 | } else { // "ROTATION_NONE" or unknown string |
| 1438 | gr_rotate(GRRotation::NONE); |
| 1439 | } |
| 1440 | Redraw(); |
| 1441 | |
| 1442 | return 0; |
| 1443 | } |