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