blob: d65d656bdafe6c91f383ec9e0392cf14ab1c3384 [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
Tao Baoefb49ad2017-01-31 23:03:10 -080017#include "screen_ui.h"
18
Elliott Hughes498cda62016-04-14 16:49:04 -070019#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070020#include <errno.h>
21#include <fcntl.h>
22#include <linux/input.h>
23#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/types.h>
31#include <time.h>
32#include <unistd.h>
33
Tao Bao736d59c2017-01-03 10:15:33 -080034#include <string>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070035#include <vector>
36
Tianjie Xuc21edd42016-08-05 18:00:04 -070037#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070038#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080039#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070040#include <android-base/strings.h>
Tao Baoefb49ad2017-01-31 23:03:10 -080041#include <minui/minui.h>
Tao Baob6918c72015-05-19 17:02:16 -070042
Doug Zongker211aebc2011-10-28 15:13:10 -070043#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070044#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070045#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070046
Doug Zongker211aebc2011-10-28 15:13:10 -070047// Return the current time as a double (including fractions of a second).
48static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070049 struct timeval tv;
50 gettimeofday(&tv, nullptr);
51 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070052}
53
Tao Bao736d59c2017-01-03 10:15:33 -080054ScreenRecoveryUI::ScreenRecoveryUI()
Tao Bao4521b702017-06-20 18:11:21 -070055 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
56 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -070057 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -070058 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -070059 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -080060 progressBarType(EMPTY),
61 progressScopeStart(0),
62 progressScopeSize(0),
63 progress(0),
64 pagesIdentical(false),
65 text_cols_(0),
66 text_rows_(0),
67 text_(nullptr),
68 text_col_(0),
69 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -080070 show_text(false),
71 show_text_ever(false),
Tao Baoe15d7a52017-09-07 13:38:51 -070072 menu_headers_(nullptr),
Tao Bao736d59c2017-01-03 10:15:33 -080073 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),
Tao Baoefb49ad2017-01-31 23:03:10 -080083 locale_(""),
84 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080085 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -070086
Tao Bao99b2d772017-06-23 22:47:03 -070087GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070088 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
89 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
90 }
91 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -070092}
93
Tao Bao99b2d772017-06-23 22:47:03 -070094GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070095 switch (currentIcon) {
96 case ERASING:
97 return erasing_text;
98 case ERROR:
99 return error_text;
100 case INSTALLING_UPDATE:
101 return installing_text;
102 case NO_COMMAND:
103 return no_command_text;
104 case NONE:
105 abort();
106 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700107}
108
Mikhail Lappob49767c2017-03-23 21:44:26 +0100109int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700110 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700111}
112
113// Here's the intended layout:
114
Elliott Hughes6d089a92016-07-08 17:23:41 -0700115// | portrait large landscape large
116// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700117// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700118// icon | (200dp)
119// gap | 68dp 68dp 56dp 112dp
120// text | (14sp)
121// gap | 32dp 32dp 26dp 52dp
122// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700123// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700124
Tao Bao3250f722017-06-29 14:32:05 -0700125// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
126// 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 -0700127
Elliott Hughes6d089a92016-07-08 17:23:41 -0700128enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700129enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700130static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700131 { 32, 68, }, // PORTRAIT
132 { 32, 68, }, // PORTRAIT_LARGE
133 { 26, 56, }, // LANDSCAPE
134 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700135};
136
Tao Bao99b2d772017-06-23 22:47:03 -0700137int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700138 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700139}
140
Tao Bao99b2d772017-06-23 22:47:03 -0700141int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700142 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
143 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700144}
145
Tao Bao99b2d772017-06-23 22:47:03 -0700146int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700147 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
148 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
149 gr_get_height(progressBarFill);
150 int bottom_gap = (gr_fb_height() - elements_sum) / 2;
151 return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700152}
153
Doug Zongker211aebc2011-10-28 15:13:10 -0700154// Clear the screen and draw the currently selected background icon (if any).
155// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700156void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700157 pagesIdentical = false;
158 gr_color(0, 0, 0, 255);
159 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700160
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700161 if (currentIcon != NONE) {
162 if (max_stage != -1) {
163 int stage_height = gr_get_height(stageMarkerEmpty);
164 int stage_width = gr_get_width(stageMarkerEmpty);
165 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
166 int y = gr_fb_height() - stage_height;
167 for (int i = 0; i < max_stage; ++i) {
168 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
169 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
170 x += stage_width;
171 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700172 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700173
174 GRSurface* text_surface = GetCurrentText();
175 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
176 int text_y = GetTextBaseline();
177 gr_color(255, 255, 255, 255);
178 gr_texticon(text_x, text_y, text_surface);
179 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700180}
181
Tao Baoea78d862017-06-28 14:52:17 -0700182// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
183// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700184void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800185 if (currentIcon != NONE) {
186 GRSurface* frame = GetCurrentFrame();
187 int frame_width = gr_get_width(frame);
188 int frame_height = gr_get_height(frame);
189 int frame_x = (gr_fb_width() - frame_width) / 2;
190 int frame_y = GetAnimationBaseline();
191 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
192 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700193
Tao Bao736d59c2017-01-03 10:15:33 -0800194 if (progressBarType != EMPTY) {
195 int width = gr_get_width(progressBarEmpty);
196 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700197
Tao Bao736d59c2017-01-03 10:15:33 -0800198 int progress_x = (gr_fb_width() - width) / 2;
199 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700200
Tao Bao736d59c2017-01-03 10:15:33 -0800201 // Erase behind the progress bar (in case this was a progress-only update)
202 gr_color(0, 0, 0, 255);
203 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700204
Tao Bao736d59c2017-01-03 10:15:33 -0800205 if (progressBarType == DETERMINATE) {
206 float p = progressScopeStart + progress * progressScopeSize;
207 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700208
Tao Bao736d59c2017-01-03 10:15:33 -0800209 if (rtl_locale_) {
210 // Fill the progress bar from right to left.
211 if (pos > 0) {
212 gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
213 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700214 }
Tao Bao736d59c2017-01-03 10:15:33 -0800215 if (pos < width - 1) {
216 gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
217 }
218 } else {
219 // Fill the progress bar from left to right.
220 if (pos > 0) {
221 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
222 }
223 if (pos < width - 1) {
224 gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
225 }
226 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700227 }
Tao Bao736d59c2017-01-03 10:15:33 -0800228 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700229}
230
Tao Bao99b2d772017-06-23 22:47:03 -0700231void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700232 switch (e) {
233 case INFO:
234 gr_color(249, 194, 0, 255);
235 break;
236 case HEADER:
237 gr_color(247, 0, 6, 255);
238 break;
239 case MENU:
240 case MENU_SEL_BG:
241 gr_color(0, 106, 157, 255);
242 break;
243 case MENU_SEL_BG_ACTIVE:
244 gr_color(0, 156, 100, 255);
245 break;
246 case MENU_SEL_FG:
247 gr_color(255, 255, 255, 255);
248 break;
249 case LOG:
250 gr_color(196, 196, 196, 255);
251 break;
252 case TEXT_FILL:
253 gr_color(0, 0, 0, 160);
254 break;
255 default:
256 gr_color(255, 255, 255, 255);
257 break;
258 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700259}
Doug Zongker211aebc2011-10-28 15:13:10 -0700260
Tao Baoea78d862017-06-28 14:52:17 -0700261int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
262 gr_fill(0, y + 4, gr_fb_width(), y + 6);
263 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700264}
265
Luke Songe2bd8762017-06-12 16:08:33 -0700266void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700267 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700268}
269
Tao Baoea78d862017-06-28 14:52:17 -0700270int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const {
271 gr_text(gr_sys_font(), x, y, line, bold);
272 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700273}
274
Tao Baoea78d862017-06-28 14:52:17 -0700275int ScreenRecoveryUI::DrawTextLines(int x, int y, const char* const* lines) const {
276 int offset = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700277 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Tao Baoea78d862017-06-28 14:52:17 -0700278 offset += DrawTextLine(x, y + offset, lines[i], false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700279 }
Tao Baoea78d862017-06-28 14:52:17 -0700280 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700281}
282
Tao Bao2bbc6d62017-08-13 23:48:55 -0700283int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* lines) const {
284 int offset = 0;
285 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
286 // The line will be wrapped if it exceeds text_cols_.
287 std::string line(lines[i]);
288 size_t next_start = 0;
289 while (next_start < line.size()) {
290 std::string sub = line.substr(next_start, text_cols_ + 1);
291 if (sub.size() <= text_cols_) {
292 next_start += sub.size();
293 } else {
294 // Line too long and must be wrapped to text_cols_ columns.
295 size_t last_space = sub.find_last_of(" \t\n");
296 if (last_space == std::string::npos) {
297 // No space found, just draw as much as we can
298 sub.resize(text_cols_);
299 next_start += text_cols_;
300 } else {
301 sub.resize(last_space);
302 next_start += last_space + 1;
303 }
304 }
305 offset += DrawTextLine(x, y + offset, sub.c_str(), false);
306 }
307 }
308 return offset;
309}
310
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700311static const char* REGULAR_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700312 "Use volume up/down and power.",
313 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700314};
315
316static const char* LONG_PRESS_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700317 "Any button cycles highlight.",
318 "Long-press activates.",
319 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700320};
321
Tao Bao171b4c42017-06-19 23:10:44 -0700322// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
323// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700324void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700325 if (!show_text) {
326 draw_background_locked();
327 draw_foreground_locked();
328 return;
329 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700330
Tao Bao171b4c42017-06-19 23:10:44 -0700331 gr_color(0, 0, 0, 255);
332 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700333
Tao Bao4521b702017-06-20 18:11:21 -0700334 int y = kMarginHeight;
Tao Bao171b4c42017-06-19 23:10:44 -0700335 if (show_menu) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700336 static constexpr int kMenuIndent = 4;
337 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700338
Tao Bao171b4c42017-06-19 23:10:44 -0700339 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700340 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700341 std::string recovery_fingerprint =
342 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700343 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700344 y += DrawTextLine(x, y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700345 }
Tao Baoea78d862017-06-28 14:52:17 -0700346 y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Tao Bao171b4c42017-06-19 23:10:44 -0700347
348 SetColor(HEADER);
Tao Bao13aa4a92017-08-16 13:25:55 -0700349 // Ignore kMenuIndent, which is not taken into account by text_cols_.
350 y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_);
Tao Bao171b4c42017-06-19 23:10:44 -0700351
352 SetColor(MENU);
Tao Baoea78d862017-06-28 14:52:17 -0700353 y += DrawHorizontalRule(y) + 4;
Tao Bao171b4c42017-06-19 23:10:44 -0700354 for (int i = 0; i < menu_items; ++i) {
355 if (i == menu_sel) {
356 // Draw the highlight bar.
357 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
358 DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4);
359 // Bold white text for the selected item.
360 SetColor(MENU_SEL_FG);
Tao Baoe15d7a52017-09-07 13:38:51 -0700361 y += DrawTextLine(x, y, menu_[i].c_str(), true);
Tao Bao171b4c42017-06-19 23:10:44 -0700362 SetColor(MENU);
363 } else {
Tao Baoe15d7a52017-09-07 13:38:51 -0700364 y += DrawTextLine(x, y, menu_[i].c_str(), false);
Tao Bao171b4c42017-06-19 23:10:44 -0700365 }
366 }
Tao Baoea78d862017-06-28 14:52:17 -0700367 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700368 }
369
370 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
371 // we've displayed the entire text buffer.
372 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700373 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700374 size_t count = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700375 for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
376 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700377 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700378 --row;
379 if (row < 0) row = text_rows_ - 1;
380 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700381}
382
383// Redraw everything on the screen and flip the screen (make it visible).
384// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700385void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700386 draw_screen_locked();
387 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700388}
389
390// Updates only the progress bar, if possible, otherwise redraws the screen.
391// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700392void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700393 if (show_text || !pagesIdentical) {
394 draw_screen_locked(); // Must redraw the whole screen
395 pagesIdentical = true;
396 } else {
397 draw_foreground_locked(); // Draw only the progress bar and overlays
398 }
399 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700400}
401
402// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700403void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700404 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
405 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700406}
407
Elliott Hughes985022a2015-04-13 13:04:32 -0700408void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700409 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700410 while (true) {
411 double start = now();
412 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700413
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700414 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700415
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700416 // update the installation animation, if active
417 // skip this if we have a text overlay (too expensive to update)
418 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
419 if (!intro_done) {
420 if (current_frame == intro_frames - 1) {
421 intro_done = true;
422 current_frame = 0;
423 } else {
424 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700425 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700426 } else {
427 current_frame = (current_frame + 1) % loop_frames;
428 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700429
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700430 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700431 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700432
433 // move the progress bar forward on timed intervals, if configured
434 int duration = progressScopeDuration;
435 if (progressBarType == DETERMINATE && duration > 0) {
436 double elapsed = now() - progressScopeTime;
437 float p = 1.0 * elapsed / duration;
438 if (p > 1.0) p = 1.0;
439 if (p > progress) {
440 progress = p;
441 redraw = true;
442 }
443 }
444
445 if (redraw) update_progress_locked();
446
447 pthread_mutex_unlock(&updateMutex);
448 double end = now();
449 // minimum of 20ms delay between frames
450 double delay = interval - (end - start);
451 if (delay < 0.02) delay = 0.02;
452 usleep(static_cast<useconds_t>(delay * 1000000));
453 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700454}
455
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700456void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700457 int result = res_create_display_surface(filename, surface);
458 if (result < 0) {
459 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
460 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800461}
462
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700463void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800464 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
465 if (result < 0) {
466 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
467 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700468}
469
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700470static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700471 char** result = new char*[rows];
472 for (size_t i = 0; i < rows; ++i) {
473 result[i] = new char[cols];
474 memset(result[i], 0, cols);
475 }
476 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700477}
478
Tianjie Xu35926c42016-04-28 18:06:26 -0700479// Choose the right background string to display during update.
480void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700481 if (security_update) {
482 LoadLocalizedBitmap("installing_security_text", &installing_text);
483 } else {
484 LoadLocalizedBitmap("installing_text", &installing_text);
485 }
486 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700487}
488
Sen Jiangd5304492016-12-09 16:20:49 -0800489bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700490 if (gr_init() < 0) {
491 return false;
492 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700493
Tao Bao171b4c42017-06-19 23:10:44 -0700494 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao4521b702017-06-20 18:11:21 -0700495 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
496 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700497 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700498}
499
Tao Bao736d59c2017-01-03 10:15:33 -0800500bool ScreenRecoveryUI::Init(const std::string& locale) {
501 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800502
Tao Bao736d59c2017-01-03 10:15:33 -0800503 if (!InitTextParams()) {
504 return false;
505 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700506
Tao Bao736d59c2017-01-03 10:15:33 -0800507 // Are we portrait or landscape?
508 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
509 // Are we the large variant of our base layout?
510 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700511
Tao Bao736d59c2017-01-03 10:15:33 -0800512 text_ = Alloc2d(text_rows_, text_cols_ + 1);
513 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800514
Tao Bao736d59c2017-01-03 10:15:33 -0800515 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700516
Tao Baoefb49ad2017-01-31 23:03:10 -0800517 // Set up the locale info.
518 SetLocale(locale);
519
Tao Bao736d59c2017-01-03 10:15:33 -0800520 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700521
Tao Bao736d59c2017-01-03 10:15:33 -0800522 LoadBitmap("progress_empty", &progressBarEmpty);
523 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700524
Tao Bao736d59c2017-01-03 10:15:33 -0800525 LoadBitmap("stage_empty", &stageMarkerEmpty);
526 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700527
Tao Bao736d59c2017-01-03 10:15:33 -0800528 // Background text for "installing_update" could be "installing update"
529 // or "installing security update". It will be set after UI init according
530 // to commands in BCB.
531 installing_text = nullptr;
532 LoadLocalizedBitmap("erasing_text", &erasing_text);
533 LoadLocalizedBitmap("no_command_text", &no_command_text);
534 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700535
Tao Bao736d59c2017-01-03 10:15:33 -0800536 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700537
Tao Bao736d59c2017-01-03 10:15:33 -0800538 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800539
Tao Bao736d59c2017-01-03 10:15:33 -0800540 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700541}
542
Elliott Hughes498cda62016-04-14 16:49:04 -0700543void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700544 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
545 dirent* de;
546 std::vector<std::string> intro_frame_names;
547 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700548
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700549 while ((de = readdir(dir.get())) != nullptr) {
550 int value, num_chars;
551 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
552 intro_frame_names.emplace_back(de->d_name, num_chars);
553 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
554 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700555 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700556 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700557
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700558 intro_frames = intro_frame_names.size();
559 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700560
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700561 // It's okay to not have an intro.
562 if (intro_frames == 0) intro_done = true;
563 // But you must have an animation.
564 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700565
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700566 std::sort(intro_frame_names.begin(), intro_frame_names.end());
567 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700568
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700569 introFrames = new GRSurface*[intro_frames];
570 for (size_t i = 0; i < intro_frames; i++) {
571 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
572 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700573
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700574 loopFrames = new GRSurface*[loop_frames];
575 for (size_t i = 0; i < loop_frames; i++) {
576 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
577 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700578}
579
Elliott Hughes8de52072015-04-08 20:06:50 -0700580void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700581 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700582
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700583 currentIcon = icon;
584 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700585
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700586 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700587}
588
Elliott Hughes8de52072015-04-08 20:06:50 -0700589void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700590 pthread_mutex_lock(&updateMutex);
591 if (progressBarType != type) {
592 progressBarType = type;
593 }
594 progressScopeStart = 0;
595 progressScopeSize = 0;
596 progress = 0;
597 update_progress_locked();
598 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700599}
600
Elliott Hughes8de52072015-04-08 20:06:50 -0700601void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700602 pthread_mutex_lock(&updateMutex);
603 progressBarType = DETERMINATE;
604 progressScopeStart += progressScopeSize;
605 progressScopeSize = portion;
606 progressScopeTime = now();
607 progressScopeDuration = seconds;
608 progress = 0;
609 update_progress_locked();
610 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700611}
612
Elliott Hughes8de52072015-04-08 20:06:50 -0700613void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700614 pthread_mutex_lock(&updateMutex);
615 if (fraction < 0.0) fraction = 0.0;
616 if (fraction > 1.0) fraction = 1.0;
617 if (progressBarType == DETERMINATE && fraction > progress) {
618 // Skip updates that aren't visibly different.
619 int width = gr_get_width(progressBarEmpty);
620 float scale = width * progressScopeSize;
621 if ((int)(progress * scale) != (int)(fraction * scale)) {
622 progress = fraction;
623 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700624 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700625 }
626 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700627}
628
Doug Zongkerc87bab12013-11-25 13:53:25 -0800629void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700630 pthread_mutex_lock(&updateMutex);
631 stage = current;
632 max_stage = max;
633 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800634}
635
Tao Baob6918c72015-05-19 17:02:16 -0700636void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700637 std::string str;
638 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700639
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700640 if (copy_to_stdout) {
641 fputs(str.c_str(), stdout);
642 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700643
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700644 pthread_mutex_lock(&updateMutex);
645 if (text_rows_ > 0 && text_cols_ > 0) {
646 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
647 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700648 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700649 text_col_ = 0;
650 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700651 }
652 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700653 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700654 text_[text_row_][text_col_] = '\0';
655 update_screen_locked();
656 }
657 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700658}
659
Tao Baob6918c72015-05-19 17:02:16 -0700660void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700661 va_list ap;
662 va_start(ap, fmt);
663 PrintV(fmt, true, ap);
664 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700665}
666
667void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700668 va_list ap;
669 va_start(ap, fmt);
670 PrintV(fmt, false, ap);
671 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700672}
673
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700674void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700675 pthread_mutex_lock(&updateMutex);
676 if (ch != '\n') text_[text_row_][text_col_++] = ch;
677 if (ch == '\n' || text_col_ >= text_cols_) {
678 text_col_ = 0;
679 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700680 }
681 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700682}
683
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700684void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700685 pthread_mutex_lock(&updateMutex);
686 text_col_ = 0;
687 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700688 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_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700754
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700755 // Swap in the alternate screen and clear it.
756 text_ = file_viewer_text_;
757 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700758
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700759 ShowFile(fp);
760 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700761
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700762 text_ = old_text;
763 text_col_ = old_text_col;
764 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700765}
766
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700767void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700768 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700769 pthread_mutex_lock(&updateMutex);
770 if (text_rows_ > 0 && text_cols_ > 0) {
771 menu_headers_ = headers;
Tao Baoe15d7a52017-09-07 13:38:51 -0700772 menu_.clear();
773 for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) {
774 menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1)));
Doug Zongker211aebc2011-10-28 15:13:10 -0700775 }
Tao Baoe15d7a52017-09-07 13:38:51 -0700776 menu_items = static_cast<int>(menu_.size());
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700777 show_menu = true;
778 menu_sel = initial_selection;
779 update_screen_locked();
780 }
781 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700782}
783
784int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700785 pthread_mutex_lock(&updateMutex);
786 if (show_menu) {
787 int old_sel = menu_sel;
788 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700789
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700790 // Wrap at top and bottom.
791 if (menu_sel < 0) menu_sel = menu_items - 1;
792 if (menu_sel >= menu_items) menu_sel = 0;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700793
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700794 sel = menu_sel;
795 if (menu_sel != old_sel) update_screen_locked();
796 }
797 pthread_mutex_unlock(&updateMutex);
798 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700799}
800
801void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700802 pthread_mutex_lock(&updateMutex);
803 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
804 show_menu = false;
805 update_screen_locked();
806 }
807 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700808}
809
Elliott Hughes8de52072015-04-08 20:06:50 -0700810bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700811 pthread_mutex_lock(&updateMutex);
812 int visible = show_text;
813 pthread_mutex_unlock(&updateMutex);
814 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700815}
816
Elliott Hughes8de52072015-04-08 20:06:50 -0700817bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700818 pthread_mutex_lock(&updateMutex);
819 int ever_visible = show_text_ever;
820 pthread_mutex_unlock(&updateMutex);
821 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700822}
823
Elliott Hughes8de52072015-04-08 20:06:50 -0700824void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700825 pthread_mutex_lock(&updateMutex);
826 show_text = visible;
827 if (show_text) show_text_ever = true;
828 update_screen_locked();
829 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700830}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700831
Elliott Hughes8de52072015-04-08 20:06:50 -0700832void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700833 pthread_mutex_lock(&updateMutex);
834 update_screen_locked();
835 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700836}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700837
838void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700839 // Redraw so that if we're in the menu, the highlight
840 // will change color to indicate a successful long press.
841 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -0700842}
Tao Baoefb49ad2017-01-31 23:03:10 -0800843
844void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
845 locale_ = new_locale;
846 rtl_locale_ = false;
847
848 if (!new_locale.empty()) {
849 size_t underscore = new_locale.find('_');
850 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
851 std::string lang = new_locale.substr(0, underscore);
852
853 // A bit cheesy: keep an explicit list of supported RTL languages.
854 if (lang == "ar" || // Arabic
855 lang == "fa" || // Persian (Farsi)
856 lang == "he" || // Hebrew (new language code)
857 lang == "iw" || // Hebrew (old language code)
858 lang == "ur") { // Urdu
859 rtl_locale_ = true;
860 }
861 }
862}