blob: 5c93b6672d6eb80e2bfafe990f1f5e6bd1673514 [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
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 Hughes498cda62016-04-14 16:49:04 -070017#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070018#include <errno.h>
19#include <fcntl.h>
20#include <linux/input.h>
21#include <pthread.h>
22#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
31
Tao Bao736d59c2017-01-03 10:15:33 -080032#include <string>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070033#include <vector>
34
Tianjie Xuc21edd42016-08-05 18:00:04 -070035#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070036#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080037#include <android-base/strings.h>
38#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070039
Doug Zongker211aebc2011-10-28 15:13:10 -070040#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070041#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042#include "minui/minui.h"
43#include "screen_ui.h"
44#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070045
Doug Zongker211aebc2011-10-28 15:13:10 -070046// Return the current time as a double (including fractions of a second).
47static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070048 struct timeval tv;
49 gettimeofday(&tv, nullptr);
50 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070051}
52
Tao Bao736d59c2017-01-03 10:15:33 -080053ScreenRecoveryUI::ScreenRecoveryUI()
Tao Bao4521b702017-06-20 18:11:21 -070054 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
55 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -070056 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao4521b702017-06-20 18:11:21 -070057 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -070058 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -080059 progressBarType(EMPTY),
60 progressScopeStart(0),
61 progressScopeSize(0),
62 progress(0),
63 pagesIdentical(false),
64 text_cols_(0),
65 text_rows_(0),
66 text_(nullptr),
67 text_col_(0),
68 text_row_(0),
69 text_top_(0),
70 show_text(false),
71 show_text_ever(false),
72 menu_(nullptr),
73 show_menu(false),
74 menu_items(0),
75 menu_sel(0),
76 file_viewer_text_(nullptr),
77 intro_frames(0),
78 loop_frames(0),
79 current_frame(0),
80 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -080081 stage(-1),
82 max_stage(-1),
83 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -070084
Tao Bao99b2d772017-06-23 22:47:03 -070085GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070086 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
87 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
88 }
89 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -070090}
91
Tao Bao99b2d772017-06-23 22:47:03 -070092GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070093 switch (currentIcon) {
94 case ERASING:
95 return erasing_text;
96 case ERROR:
97 return error_text;
98 case INSTALLING_UPDATE:
99 return installing_text;
100 case NO_COMMAND:
101 return no_command_text;
102 case NONE:
103 abort();
104 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700105}
106
Mikhail Lappob49767c2017-03-23 21:44:26 +0100107int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700108 return dp * density_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700109}
110
111// Here's the intended layout:
112
Elliott Hughes6d089a92016-07-08 17:23:41 -0700113// | portrait large landscape large
114// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700115// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700116// icon | (200dp)
117// gap | 68dp 68dp 56dp 112dp
118// text | (14sp)
119// gap | 32dp 32dp 26dp 52dp
120// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700121// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700122
Tao Bao3250f722017-06-29 14:32:05 -0700123// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
124// work), so that's the more useful measurement for calling code. We use even top and bottom gaps.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700125
Elliott Hughes6d089a92016-07-08 17:23:41 -0700126enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700127enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700128static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700129 { 32, 68, }, // PORTRAIT
130 { 32, 68, }, // PORTRAIT_LARGE
131 { 26, 56, }, // LANDSCAPE
132 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700133};
134
Tao Bao99b2d772017-06-23 22:47:03 -0700135int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700136 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700137}
138
Tao Bao99b2d772017-06-23 22:47:03 -0700139int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700140 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
141 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700142}
143
Tao Bao99b2d772017-06-23 22:47:03 -0700144int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700145 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
146 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
147 gr_get_height(progressBarFill);
148 int bottom_gap = (gr_fb_height() - elements_sum) / 2;
149 return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700150}
151
Doug Zongker211aebc2011-10-28 15:13:10 -0700152// Clear the screen and draw the currently selected background icon (if any).
153// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700154void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700155 pagesIdentical = false;
156 gr_color(0, 0, 0, 255);
157 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700158
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700159 if (currentIcon != NONE) {
160 if (max_stage != -1) {
161 int stage_height = gr_get_height(stageMarkerEmpty);
162 int stage_width = gr_get_width(stageMarkerEmpty);
163 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
164 int y = gr_fb_height() - stage_height;
165 for (int i = 0; i < max_stage; ++i) {
166 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
167 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
168 x += stage_width;
169 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700170 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700171
172 GRSurface* text_surface = GetCurrentText();
173 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
174 int text_y = GetTextBaseline();
175 gr_color(255, 255, 255, 255);
176 gr_texticon(text_x, text_y, text_surface);
177 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700178}
179
Tao Baoea78d862017-06-28 14:52:17 -0700180// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
181// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700182void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800183 if (currentIcon != NONE) {
184 GRSurface* frame = GetCurrentFrame();
185 int frame_width = gr_get_width(frame);
186 int frame_height = gr_get_height(frame);
187 int frame_x = (gr_fb_width() - frame_width) / 2;
188 int frame_y = GetAnimationBaseline();
189 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
190 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700191
Tao Bao736d59c2017-01-03 10:15:33 -0800192 if (progressBarType != EMPTY) {
193 int width = gr_get_width(progressBarEmpty);
194 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700195
Tao Bao736d59c2017-01-03 10:15:33 -0800196 int progress_x = (gr_fb_width() - width) / 2;
197 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700198
Tao Bao736d59c2017-01-03 10:15:33 -0800199 // Erase behind the progress bar (in case this was a progress-only update)
200 gr_color(0, 0, 0, 255);
201 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700202
Tao Bao736d59c2017-01-03 10:15:33 -0800203 if (progressBarType == DETERMINATE) {
204 float p = progressScopeStart + progress * progressScopeSize;
205 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700206
Tao Bao736d59c2017-01-03 10:15:33 -0800207 if (rtl_locale_) {
208 // Fill the progress bar from right to left.
209 if (pos > 0) {
210 gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
211 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700212 }
Tao Bao736d59c2017-01-03 10:15:33 -0800213 if (pos < width - 1) {
214 gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
215 }
216 } else {
217 // Fill the progress bar from left to right.
218 if (pos > 0) {
219 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
220 }
221 if (pos < width - 1) {
222 gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
223 }
224 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700225 }
Tao Bao736d59c2017-01-03 10:15:33 -0800226 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700227}
228
Tao Bao99b2d772017-06-23 22:47:03 -0700229void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700230 switch (e) {
231 case INFO:
232 gr_color(249, 194, 0, 255);
233 break;
234 case HEADER:
235 gr_color(247, 0, 6, 255);
236 break;
237 case MENU:
238 case MENU_SEL_BG:
239 gr_color(0, 106, 157, 255);
240 break;
241 case MENU_SEL_BG_ACTIVE:
242 gr_color(0, 156, 100, 255);
243 break;
244 case MENU_SEL_FG:
245 gr_color(255, 255, 255, 255);
246 break;
247 case LOG:
248 gr_color(196, 196, 196, 255);
249 break;
250 case TEXT_FILL:
251 gr_color(0, 0, 0, 160);
252 break;
253 default:
254 gr_color(255, 255, 255, 255);
255 break;
256 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700257}
Doug Zongker211aebc2011-10-28 15:13:10 -0700258
Tao Baoea78d862017-06-28 14:52:17 -0700259int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
260 gr_fill(0, y + 4, gr_fb_width(), y + 6);
261 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700262}
263
Luke Songe2bd8762017-06-12 16:08:33 -0700264void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700265 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700266}
267
Tao Baoea78d862017-06-28 14:52:17 -0700268int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const {
269 gr_text(gr_sys_font(), x, y, line, bold);
270 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700271}
272
Tao Baoea78d862017-06-28 14:52:17 -0700273int ScreenRecoveryUI::DrawTextLines(int x, int y, const char* const* lines) const {
274 int offset = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700275 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Tao Baoea78d862017-06-28 14:52:17 -0700276 offset += DrawTextLine(x, y + offset, lines[i], false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700277 }
Tao Baoea78d862017-06-28 14:52:17 -0700278 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700279}
280
Tao Bao2bbc6d62017-08-13 23:48:55 -0700281int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* lines) const {
282 int offset = 0;
283 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
284 // The line will be wrapped if it exceeds text_cols_.
285 std::string line(lines[i]);
286 size_t next_start = 0;
287 while (next_start < line.size()) {
288 std::string sub = line.substr(next_start, text_cols_ + 1);
289 if (sub.size() <= text_cols_) {
290 next_start += sub.size();
291 } else {
292 // Line too long and must be wrapped to text_cols_ columns.
293 size_t last_space = sub.find_last_of(" \t\n");
294 if (last_space == std::string::npos) {
295 // No space found, just draw as much as we can
296 sub.resize(text_cols_);
297 next_start += text_cols_;
298 } else {
299 sub.resize(last_space);
300 next_start += last_space + 1;
301 }
302 }
303 offset += DrawTextLine(x, y + offset, sub.c_str(), false);
304 }
305 }
306 return offset;
307}
308
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700309static const char* REGULAR_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700310 "Use volume up/down and power.",
311 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700312};
313
314static const char* LONG_PRESS_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700315 "Any button cycles highlight.",
316 "Long-press activates.",
317 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700318};
319
Tao Bao171b4c42017-06-19 23:10:44 -0700320// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
321// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700322void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700323 if (!show_text) {
324 draw_background_locked();
325 draw_foreground_locked();
326 return;
327 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700328
Tao Bao171b4c42017-06-19 23:10:44 -0700329 gr_color(0, 0, 0, 255);
330 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700331
Tao Bao4521b702017-06-20 18:11:21 -0700332 int y = kMarginHeight;
Tao Bao171b4c42017-06-19 23:10:44 -0700333 if (show_menu) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700334 static constexpr int kMenuIndent = 4;
335 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700336
Tao Bao171b4c42017-06-19 23:10:44 -0700337 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700338 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700339 std::string recovery_fingerprint =
340 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700341 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700342 y += DrawTextLine(x, y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700343 }
Tao Baoea78d862017-06-28 14:52:17 -0700344 y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Tao Bao171b4c42017-06-19 23:10:44 -0700345
346 SetColor(HEADER);
Tao Bao13aa4a92017-08-16 13:25:55 -0700347 // Ignore kMenuIndent, which is not taken into account by text_cols_.
348 y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_);
Tao Bao171b4c42017-06-19 23:10:44 -0700349
350 SetColor(MENU);
Tao Baoea78d862017-06-28 14:52:17 -0700351 y += DrawHorizontalRule(y) + 4;
Tao Bao171b4c42017-06-19 23:10:44 -0700352 for (int i = 0; i < menu_items; ++i) {
353 if (i == menu_sel) {
354 // Draw the highlight bar.
355 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
356 DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4);
357 // Bold white text for the selected item.
358 SetColor(MENU_SEL_FG);
Tao Baoea78d862017-06-28 14:52:17 -0700359 y += DrawTextLine(x, y, menu_[i], true);
Tao Bao171b4c42017-06-19 23:10:44 -0700360 SetColor(MENU);
361 } else {
Tao Baoea78d862017-06-28 14:52:17 -0700362 y += DrawTextLine(x, y, menu_[i], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700363 }
364 }
Tao Baoea78d862017-06-28 14:52:17 -0700365 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700366 }
367
368 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
369 // we've displayed the entire text buffer.
370 SetColor(LOG);
371 int row = (text_top_ + text_rows_ - 1) % text_rows_;
372 size_t count = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700373 for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
374 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700375 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700376 --row;
377 if (row < 0) row = text_rows_ - 1;
378 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700379}
380
381// Redraw everything on the screen and flip the screen (make it visible).
382// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700383void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700384 draw_screen_locked();
385 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700386}
387
388// Updates only the progress bar, if possible, otherwise redraws the screen.
389// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700390void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700391 if (show_text || !pagesIdentical) {
392 draw_screen_locked(); // Must redraw the whole screen
393 pagesIdentical = true;
394 } else {
395 draw_foreground_locked(); // Draw only the progress bar and overlays
396 }
397 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700398}
399
400// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700401void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700402 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
403 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700404}
405
Elliott Hughes985022a2015-04-13 13:04:32 -0700406void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700407 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700408 while (true) {
409 double start = now();
410 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700411
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700412 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700413
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700414 // update the installation animation, if active
415 // skip this if we have a text overlay (too expensive to update)
416 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
417 if (!intro_done) {
418 if (current_frame == intro_frames - 1) {
419 intro_done = true;
420 current_frame = 0;
421 } else {
422 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700423 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700424 } else {
425 current_frame = (current_frame + 1) % loop_frames;
426 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700427
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700428 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700429 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700430
431 // move the progress bar forward on timed intervals, if configured
432 int duration = progressScopeDuration;
433 if (progressBarType == DETERMINATE && duration > 0) {
434 double elapsed = now() - progressScopeTime;
435 float p = 1.0 * elapsed / duration;
436 if (p > 1.0) p = 1.0;
437 if (p > progress) {
438 progress = p;
439 redraw = true;
440 }
441 }
442
443 if (redraw) update_progress_locked();
444
445 pthread_mutex_unlock(&updateMutex);
446 double end = now();
447 // minimum of 20ms delay between frames
448 double delay = interval - (end - start);
449 if (delay < 0.02) delay = 0.02;
450 usleep(static_cast<useconds_t>(delay * 1000000));
451 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700452}
453
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700454void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700455 int result = res_create_display_surface(filename, surface);
456 if (result < 0) {
457 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
458 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800459}
460
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700461void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800462 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
463 if (result < 0) {
464 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
465 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700466}
467
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700468static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700469 char** result = new char*[rows];
470 for (size_t i = 0; i < rows; ++i) {
471 result[i] = new char[cols];
472 memset(result[i], 0, cols);
473 }
474 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700475}
476
Tianjie Xu35926c42016-04-28 18:06:26 -0700477// Choose the right background string to display during update.
478void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700479 if (security_update) {
480 LoadLocalizedBitmap("installing_security_text", &installing_text);
481 } else {
482 LoadLocalizedBitmap("installing_text", &installing_text);
483 }
484 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700485}
486
Sen Jiangd5304492016-12-09 16:20:49 -0800487bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700488 if (gr_init() < 0) {
489 return false;
490 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700491
Tao Bao171b4c42017-06-19 23:10:44 -0700492 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao4521b702017-06-20 18:11:21 -0700493 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
494 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700495 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700496}
497
Tao Bao736d59c2017-01-03 10:15:33 -0800498bool ScreenRecoveryUI::Init(const std::string& locale) {
499 RecoveryUI::Init(locale);
500 if (!InitTextParams()) {
501 return false;
502 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700503
Tao Bao736d59c2017-01-03 10:15:33 -0800504 // Are we portrait or landscape?
505 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
506 // Are we the large variant of our base layout?
507 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700508
Tao Bao736d59c2017-01-03 10:15:33 -0800509 text_ = Alloc2d(text_rows_, text_cols_ + 1);
510 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
511 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800512
Tao Bao736d59c2017-01-03 10:15:33 -0800513 text_col_ = text_row_ = 0;
514 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700515
Tao Bao736d59c2017-01-03 10:15:33 -0800516 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700517
Tao Bao736d59c2017-01-03 10:15:33 -0800518 LoadBitmap("progress_empty", &progressBarEmpty);
519 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700520
Tao Bao736d59c2017-01-03 10:15:33 -0800521 LoadBitmap("stage_empty", &stageMarkerEmpty);
522 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700523
Tao Bao736d59c2017-01-03 10:15:33 -0800524 // Background text for "installing_update" could be "installing update"
525 // or "installing security update". It will be set after UI init according
526 // to commands in BCB.
527 installing_text = nullptr;
528 LoadLocalizedBitmap("erasing_text", &erasing_text);
529 LoadLocalizedBitmap("no_command_text", &no_command_text);
530 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700531
Tao Bao736d59c2017-01-03 10:15:33 -0800532 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700533
Tao Bao736d59c2017-01-03 10:15:33 -0800534 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800535
Tao Bao736d59c2017-01-03 10:15:33 -0800536 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700537}
538
Elliott Hughes498cda62016-04-14 16:49:04 -0700539void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700540 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
541 dirent* de;
542 std::vector<std::string> intro_frame_names;
543 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700544
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700545 while ((de = readdir(dir.get())) != nullptr) {
546 int value, num_chars;
547 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
548 intro_frame_names.emplace_back(de->d_name, num_chars);
549 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
550 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700551 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700552 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700553
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700554 intro_frames = intro_frame_names.size();
555 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700556
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700557 // It's okay to not have an intro.
558 if (intro_frames == 0) intro_done = true;
559 // But you must have an animation.
560 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700561
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700562 std::sort(intro_frame_names.begin(), intro_frame_names.end());
563 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700564
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700565 introFrames = new GRSurface*[intro_frames];
566 for (size_t i = 0; i < intro_frames; i++) {
567 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
568 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700569
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700570 loopFrames = new GRSurface*[loop_frames];
571 for (size_t i = 0; i < loop_frames; i++) {
572 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
573 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700574}
575
Elliott Hughes8de52072015-04-08 20:06:50 -0700576void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700577 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700578
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700579 currentIcon = icon;
580 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700581
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700582 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700583}
584
Elliott Hughes8de52072015-04-08 20:06:50 -0700585void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700586 pthread_mutex_lock(&updateMutex);
587 if (progressBarType != type) {
588 progressBarType = type;
589 }
590 progressScopeStart = 0;
591 progressScopeSize = 0;
592 progress = 0;
593 update_progress_locked();
594 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700595}
596
Elliott Hughes8de52072015-04-08 20:06:50 -0700597void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700598 pthread_mutex_lock(&updateMutex);
599 progressBarType = DETERMINATE;
600 progressScopeStart += progressScopeSize;
601 progressScopeSize = portion;
602 progressScopeTime = now();
603 progressScopeDuration = seconds;
604 progress = 0;
605 update_progress_locked();
606 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700607}
608
Elliott Hughes8de52072015-04-08 20:06:50 -0700609void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700610 pthread_mutex_lock(&updateMutex);
611 if (fraction < 0.0) fraction = 0.0;
612 if (fraction > 1.0) fraction = 1.0;
613 if (progressBarType == DETERMINATE && fraction > progress) {
614 // Skip updates that aren't visibly different.
615 int width = gr_get_width(progressBarEmpty);
616 float scale = width * progressScopeSize;
617 if ((int)(progress * scale) != (int)(fraction * scale)) {
618 progress = fraction;
619 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700620 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700621 }
622 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700623}
624
Doug Zongkerc87bab12013-11-25 13:53:25 -0800625void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700626 pthread_mutex_lock(&updateMutex);
627 stage = current;
628 max_stage = max;
629 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800630}
631
Tao Baob6918c72015-05-19 17:02:16 -0700632void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700633 std::string str;
634 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700635
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700636 if (copy_to_stdout) {
637 fputs(str.c_str(), stdout);
638 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700639
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700640 pthread_mutex_lock(&updateMutex);
641 if (text_rows_ > 0 && text_cols_ > 0) {
642 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
643 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700644 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700645 text_col_ = 0;
646 text_row_ = (text_row_ + 1) % text_rows_;
647 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
648 }
649 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700650 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700651 text_[text_row_][text_col_] = '\0';
652 update_screen_locked();
653 }
654 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700655}
656
Tao Baob6918c72015-05-19 17:02:16 -0700657void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700658 va_list ap;
659 va_start(ap, fmt);
660 PrintV(fmt, true, ap);
661 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700662}
663
664void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700665 va_list ap;
666 va_start(ap, fmt);
667 PrintV(fmt, false, ap);
668 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700669}
670
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700671void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700672 pthread_mutex_lock(&updateMutex);
673 if (ch != '\n') text_[text_row_][text_col_++] = ch;
674 if (ch == '\n' || text_col_ >= text_cols_) {
675 text_col_ = 0;
676 ++text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700677
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700678 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
679 }
680 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700681}
682
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700683void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700684 pthread_mutex_lock(&updateMutex);
685 text_col_ = 0;
686 text_row_ = 0;
687 text_top_ = 1;
688 for (size_t i = 0; i < text_rows_; ++i) {
689 memset(text_[i], 0, text_cols_ + 1);
690 }
691 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700692}
693
694void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700695 std::vector<off_t> offsets;
696 offsets.push_back(ftello(fp));
697 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700698
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700699 struct stat sb;
700 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700701
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700702 bool show_prompt = false;
703 while (true) {
704 if (show_prompt) {
705 PrintOnScreenOnly("--(%d%% of %d bytes)--",
706 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
707 static_cast<int>(sb.st_size));
708 Redraw();
709 while (show_prompt) {
710 show_prompt = false;
711 int key = WaitKey();
712 if (key == KEY_POWER || key == KEY_ENTER) {
713 return;
714 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
715 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700716 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700717 } else {
718 offsets.pop_back();
719 fseek(fp, offsets.back(), SEEK_SET);
720 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700721 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700722 if (feof(fp)) {
723 return;
724 }
725 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700726 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700727 }
728 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700729 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700730
731 int ch = getc(fp);
732 if (ch == EOF) {
733 while (text_row_ < text_rows_ - 1) PutChar('\n');
734 show_prompt = true;
735 } else {
736 PutChar(ch);
737 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
738 show_prompt = true;
739 }
740 }
741 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700742}
743
Elliott Hughes8de52072015-04-08 20:06:50 -0700744void ScreenRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700745 FILE* fp = fopen_path(filename, "re");
746 if (fp == nullptr) {
747 Print(" Unable to open %s: %s\n", filename, strerror(errno));
748 return;
749 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700750
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700751 char** old_text = text_;
752 size_t old_text_col = text_col_;
753 size_t old_text_row = text_row_;
754 size_t old_text_top = text_top_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700755
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700756 // Swap in the alternate screen and clear it.
757 text_ = file_viewer_text_;
758 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700759
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700760 ShowFile(fp);
761 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700762
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700763 text_ = old_text;
764 text_col_ = old_text_col;
765 text_row_ = old_text_row;
766 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700767}
768
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700769void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700770 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700771 pthread_mutex_lock(&updateMutex);
772 if (text_rows_ > 0 && text_cols_ > 0) {
773 menu_headers_ = headers;
774 size_t i = 0;
775 for (; i < text_rows_ && items[i] != nullptr; ++i) {
776 strncpy(menu_[i], items[i], text_cols_ - 1);
777 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700778 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700779 menu_items = i;
780 show_menu = true;
781 menu_sel = initial_selection;
782 update_screen_locked();
783 }
784 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700785}
786
787int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700788 pthread_mutex_lock(&updateMutex);
789 if (show_menu) {
790 int old_sel = menu_sel;
791 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700792
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700793 // Wrap at top and bottom.
794 if (menu_sel < 0) menu_sel = menu_items - 1;
795 if (menu_sel >= menu_items) menu_sel = 0;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700796
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700797 sel = menu_sel;
798 if (menu_sel != old_sel) update_screen_locked();
799 }
800 pthread_mutex_unlock(&updateMutex);
801 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700802}
803
804void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700805 pthread_mutex_lock(&updateMutex);
806 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
807 show_menu = false;
808 update_screen_locked();
809 }
810 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700811}
812
Elliott Hughes8de52072015-04-08 20:06:50 -0700813bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700814 pthread_mutex_lock(&updateMutex);
815 int visible = show_text;
816 pthread_mutex_unlock(&updateMutex);
817 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700818}
819
Elliott Hughes8de52072015-04-08 20:06:50 -0700820bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700821 pthread_mutex_lock(&updateMutex);
822 int ever_visible = show_text_ever;
823 pthread_mutex_unlock(&updateMutex);
824 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700825}
826
Elliott Hughes8de52072015-04-08 20:06:50 -0700827void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700828 pthread_mutex_lock(&updateMutex);
829 show_text = visible;
830 if (show_text) show_text_ever = true;
831 update_screen_locked();
832 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700833}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700834
Elliott Hughes8de52072015-04-08 20:06:50 -0700835void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700836 pthread_mutex_lock(&updateMutex);
837 update_screen_locked();
838 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700839}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700840
841void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700842 // Redraw so that if we're in the menu, the highlight
843 // will change color to indicate a successful long press.
844 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -0700845}