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