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