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