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