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