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