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