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