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