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