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