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