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