blob: bc5c5c31f5be17864b8678ce919a21935b354c8d [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
Tianjie Xu29d55752017-09-20 17:53:46 -070034#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080035#include <string>
Tianjie Xu29d55752017-09-20 17:53:46 -070036#include <unordered_map>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070037#include <vector>
38
Tianjie Xuc21edd42016-08-05 18:00:04 -070039#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070040#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080041#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070042#include <android-base/strings.h>
Tao Baoefb49ad2017-01-31 23:03:10 -080043#include <minui/minui.h>
Tao Baob6918c72015-05-19 17:02:16 -070044
Doug Zongker211aebc2011-10-28 15:13:10 -070045#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070046#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070047#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070048
Doug Zongker211aebc2011-10-28 15:13:10 -070049// Return the current time as a double (including fractions of a second).
50static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070051 struct timeval tv;
52 gettimeofday(&tv, nullptr);
53 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070054}
55
Tao Bao736d59c2017-01-03 10:15:33 -080056ScreenRecoveryUI::ScreenRecoveryUI()
Tao Bao4521b702017-06-20 18:11:21 -070057 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
58 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -070059 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -070060 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -070061 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -080062 progressBarType(EMPTY),
63 progressScopeStart(0),
64 progressScopeSize(0),
65 progress(0),
66 pagesIdentical(false),
67 text_cols_(0),
68 text_rows_(0),
69 text_(nullptr),
70 text_col_(0),
71 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -080072 show_text(false),
73 show_text_ever(false),
Tao Baoe15d7a52017-09-07 13:38:51 -070074 menu_headers_(nullptr),
Tao Bao736d59c2017-01-03 10:15:33 -080075 show_menu(false),
76 menu_items(0),
77 menu_sel(0),
78 file_viewer_text_(nullptr),
79 intro_frames(0),
80 loop_frames(0),
81 current_frame(0),
82 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -080083 stage(-1),
84 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -080085 locale_(""),
86 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080087 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -070088
Tao Bao99b2d772017-06-23 22:47:03 -070089GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070090 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
91 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
92 }
93 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -070094}
95
Tao Bao99b2d772017-06-23 22:47:03 -070096GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070097 switch (currentIcon) {
98 case ERASING:
99 return erasing_text;
100 case ERROR:
101 return error_text;
102 case INSTALLING_UPDATE:
103 return installing_text;
104 case NO_COMMAND:
105 return no_command_text;
106 case NONE:
107 abort();
108 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700109}
110
Mikhail Lappob49767c2017-03-23 21:44:26 +0100111int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700112 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700113}
114
115// Here's the intended layout:
116
Elliott Hughes6d089a92016-07-08 17:23:41 -0700117// | portrait large landscape large
118// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700119// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700120// icon | (200dp)
121// gap | 68dp 68dp 56dp 112dp
122// text | (14sp)
123// gap | 32dp 32dp 26dp 52dp
124// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700125// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700126
Tao Bao3250f722017-06-29 14:32:05 -0700127// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
128// 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 -0700129
Elliott Hughes6d089a92016-07-08 17:23:41 -0700130enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700131enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700132static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700133 { 32, 68, }, // PORTRAIT
134 { 32, 68, }, // PORTRAIT_LARGE
135 { 26, 56, }, // LANDSCAPE
136 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700137};
138
Tao Bao99b2d772017-06-23 22:47:03 -0700139int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700140 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700141}
142
Tao Bao99b2d772017-06-23 22:47:03 -0700143int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700144 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
145 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700146}
147
Tao Bao99b2d772017-06-23 22:47:03 -0700148int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700149 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
150 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
151 gr_get_height(progressBarFill);
152 int bottom_gap = (gr_fb_height() - elements_sum) / 2;
153 return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700154}
155
Doug Zongker211aebc2011-10-28 15:13:10 -0700156// Clear the screen and draw the currently selected background icon (if any).
157// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700158void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700159 pagesIdentical = false;
160 gr_color(0, 0, 0, 255);
161 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700162
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700163 if (currentIcon != NONE) {
164 if (max_stage != -1) {
165 int stage_height = gr_get_height(stageMarkerEmpty);
166 int stage_width = gr_get_width(stageMarkerEmpty);
167 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
168 int y = gr_fb_height() - stage_height;
169 for (int i = 0; i < max_stage; ++i) {
170 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
171 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
172 x += stage_width;
173 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700174 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700175
176 GRSurface* text_surface = GetCurrentText();
177 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
178 int text_y = GetTextBaseline();
179 gr_color(255, 255, 255, 255);
180 gr_texticon(text_x, text_y, text_surface);
181 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700182}
183
Tao Baoea78d862017-06-28 14:52:17 -0700184// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
185// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700186void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800187 if (currentIcon != NONE) {
188 GRSurface* frame = GetCurrentFrame();
189 int frame_width = gr_get_width(frame);
190 int frame_height = gr_get_height(frame);
191 int frame_x = (gr_fb_width() - frame_width) / 2;
192 int frame_y = GetAnimationBaseline();
193 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
194 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700195
Tao Bao736d59c2017-01-03 10:15:33 -0800196 if (progressBarType != EMPTY) {
197 int width = gr_get_width(progressBarEmpty);
198 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700199
Tao Bao736d59c2017-01-03 10:15:33 -0800200 int progress_x = (gr_fb_width() - width) / 2;
201 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700202
Tao Bao736d59c2017-01-03 10:15:33 -0800203 // Erase behind the progress bar (in case this was a progress-only update)
204 gr_color(0, 0, 0, 255);
205 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700206
Tao Bao736d59c2017-01-03 10:15:33 -0800207 if (progressBarType == DETERMINATE) {
208 float p = progressScopeStart + progress * progressScopeSize;
209 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700210
Tao Bao736d59c2017-01-03 10:15:33 -0800211 if (rtl_locale_) {
212 // Fill the progress bar from right to left.
213 if (pos > 0) {
214 gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
215 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700216 }
Tao Bao736d59c2017-01-03 10:15:33 -0800217 if (pos < width - 1) {
218 gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
219 }
220 } else {
221 // Fill the progress bar from left to right.
222 if (pos > 0) {
223 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
224 }
225 if (pos < width - 1) {
226 gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
227 }
228 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700229 }
Tao Bao736d59c2017-01-03 10:15:33 -0800230 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700231}
232
Tao Bao99b2d772017-06-23 22:47:03 -0700233void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700234 switch (e) {
235 case INFO:
236 gr_color(249, 194, 0, 255);
237 break;
238 case HEADER:
239 gr_color(247, 0, 6, 255);
240 break;
241 case MENU:
242 case MENU_SEL_BG:
243 gr_color(0, 106, 157, 255);
244 break;
245 case MENU_SEL_BG_ACTIVE:
246 gr_color(0, 156, 100, 255);
247 break;
248 case MENU_SEL_FG:
249 gr_color(255, 255, 255, 255);
250 break;
251 case LOG:
252 gr_color(196, 196, 196, 255);
253 break;
254 case TEXT_FILL:
255 gr_color(0, 0, 0, 160);
256 break;
257 default:
258 gr_color(255, 255, 255, 255);
259 break;
260 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700261}
Doug Zongker211aebc2011-10-28 15:13:10 -0700262
Tianjie Xu29d55752017-09-20 17:53:46 -0700263void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
264 size_t sel) {
265 SetLocale(locales_entries[sel]);
266 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
267 "installing_security_text", "no_command_text" };
268 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
269 for (const auto& name : text_name) {
270 GRSurface* text_image = nullptr;
271 LoadLocalizedBitmap(name.c_str(), &text_image);
272 if (!text_image) {
273 Print("Failed to load %s\n", name.c_str());
274 return;
275 }
276 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
277 }
278
279 pthread_mutex_lock(&updateMutex);
280 gr_color(0, 0, 0, 255);
281 gr_clear();
282
283 int text_y = kMarginHeight;
284 int text_x = kMarginWidth;
285 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
286 // Write the header and descriptive texts.
287 SetColor(INFO);
288 std::string header = "Show background text image";
289 text_y += DrawTextLine(text_x, text_y, header.c_str(), true);
290 std::string locale_selection = android::base::StringPrintf(
291 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel, locales_entries.size());
292 const char* instruction[] = { locale_selection.c_str(),
293 "Use volume up/down to switch locales and power to exit.",
294 nullptr };
295 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
296
297 // Iterate through the text images and display them in order for the current locale.
298 for (const auto& p : surfaces) {
299 text_y += line_spacing;
300 SetColor(LOG);
301 text_y += DrawTextLine(text_x, text_y, p.first.c_str(), false);
302 gr_color(255, 255, 255, 255);
303 gr_texticon(text_x, text_y, p.second.get());
304 text_y += gr_get_height(p.second.get());
305 }
306 // Update the whole screen.
307 gr_flip();
308 pthread_mutex_unlock(&updateMutex);
309}
310
311void ScreenRecoveryUI::CheckBackgroundTextImages(const std::string& saved_locale) {
312 // Load a list of locales embedded in one of the resource files.
313 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
314 if (locales_entries.empty()) {
315 Print("Failed to load locales from the resource files\n");
316 return;
317 }
318 size_t selected = 0;
319 SelectAndShowBackgroundText(locales_entries, selected);
320
321 FlushKeys();
322 while (true) {
323 int key = WaitKey();
324 if (key == KEY_POWER || key == KEY_ENTER) {
325 break;
326 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
327 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
328 SelectAndShowBackgroundText(locales_entries, selected);
329 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
330 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
331 SelectAndShowBackgroundText(locales_entries, selected);
332 }
333 }
334
335 SetLocale(saved_locale);
336}
337
Tao Baoea78d862017-06-28 14:52:17 -0700338int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
339 gr_fill(0, y + 4, gr_fb_width(), y + 6);
340 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700341}
342
Luke Songe2bd8762017-06-12 16:08:33 -0700343void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700344 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700345}
346
Tao Baoea78d862017-06-28 14:52:17 -0700347int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const {
348 gr_text(gr_sys_font(), x, y, line, bold);
349 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700350}
351
Tao Baoea78d862017-06-28 14:52:17 -0700352int ScreenRecoveryUI::DrawTextLines(int x, int y, const char* const* lines) const {
353 int offset = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700354 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Tao Baoea78d862017-06-28 14:52:17 -0700355 offset += DrawTextLine(x, y + offset, lines[i], false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700356 }
Tao Baoea78d862017-06-28 14:52:17 -0700357 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700358}
359
Tao Bao2bbc6d62017-08-13 23:48:55 -0700360int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* lines) const {
361 int offset = 0;
362 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
363 // The line will be wrapped if it exceeds text_cols_.
364 std::string line(lines[i]);
365 size_t next_start = 0;
366 while (next_start < line.size()) {
367 std::string sub = line.substr(next_start, text_cols_ + 1);
368 if (sub.size() <= text_cols_) {
369 next_start += sub.size();
370 } else {
371 // Line too long and must be wrapped to text_cols_ columns.
372 size_t last_space = sub.find_last_of(" \t\n");
373 if (last_space == std::string::npos) {
374 // No space found, just draw as much as we can
375 sub.resize(text_cols_);
376 next_start += text_cols_;
377 } else {
378 sub.resize(last_space);
379 next_start += last_space + 1;
380 }
381 }
382 offset += DrawTextLine(x, y + offset, sub.c_str(), false);
383 }
384 }
385 return offset;
386}
387
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700388static const char* REGULAR_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700389 "Use volume up/down and power.",
390 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700391};
392
393static const char* LONG_PRESS_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700394 "Any button cycles highlight.",
395 "Long-press activates.",
396 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700397};
398
Tao Bao171b4c42017-06-19 23:10:44 -0700399// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
400// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700401void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700402 if (!show_text) {
403 draw_background_locked();
404 draw_foreground_locked();
405 return;
406 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700407
Tao Bao171b4c42017-06-19 23:10:44 -0700408 gr_color(0, 0, 0, 255);
409 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700410
Tao Bao4521b702017-06-20 18:11:21 -0700411 int y = kMarginHeight;
Tao Bao171b4c42017-06-19 23:10:44 -0700412 if (show_menu) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700413 static constexpr int kMenuIndent = 4;
414 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700415
Tao Bao171b4c42017-06-19 23:10:44 -0700416 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700417 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700418 std::string recovery_fingerprint =
419 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700420 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700421 y += DrawTextLine(x, y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700422 }
Tao Baoea78d862017-06-28 14:52:17 -0700423 y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Tao Bao171b4c42017-06-19 23:10:44 -0700424
425 SetColor(HEADER);
Tao Bao13aa4a92017-08-16 13:25:55 -0700426 // Ignore kMenuIndent, which is not taken into account by text_cols_.
427 y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_);
Tao Bao171b4c42017-06-19 23:10:44 -0700428
429 SetColor(MENU);
Tao Baoea78d862017-06-28 14:52:17 -0700430 y += DrawHorizontalRule(y) + 4;
Tao Bao171b4c42017-06-19 23:10:44 -0700431 for (int i = 0; i < menu_items; ++i) {
432 if (i == menu_sel) {
433 // Draw the highlight bar.
434 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
435 DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4);
436 // Bold white text for the selected item.
437 SetColor(MENU_SEL_FG);
Tao Baoe15d7a52017-09-07 13:38:51 -0700438 y += DrawTextLine(x, y, menu_[i].c_str(), true);
Tao Bao171b4c42017-06-19 23:10:44 -0700439 SetColor(MENU);
440 } else {
Tao Baoe15d7a52017-09-07 13:38:51 -0700441 y += DrawTextLine(x, y, menu_[i].c_str(), false);
Tao Bao171b4c42017-06-19 23:10:44 -0700442 }
443 }
Tao Baoea78d862017-06-28 14:52:17 -0700444 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700445 }
446
447 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
448 // we've displayed the entire text buffer.
449 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700450 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700451 size_t count = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700452 for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
453 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700454 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700455 --row;
456 if (row < 0) row = text_rows_ - 1;
457 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700458}
459
460// Redraw everything on the screen and flip the screen (make it visible).
461// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700462void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700463 draw_screen_locked();
464 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700465}
466
467// Updates only the progress bar, if possible, otherwise redraws the screen.
468// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700469void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700470 if (show_text || !pagesIdentical) {
471 draw_screen_locked(); // Must redraw the whole screen
472 pagesIdentical = true;
473 } else {
474 draw_foreground_locked(); // Draw only the progress bar and overlays
475 }
476 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700477}
478
479// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700480void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700481 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
482 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700483}
484
Elliott Hughes985022a2015-04-13 13:04:32 -0700485void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700486 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700487 while (true) {
488 double start = now();
489 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700490
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700491 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700492
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700493 // update the installation animation, if active
494 // skip this if we have a text overlay (too expensive to update)
495 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
496 if (!intro_done) {
497 if (current_frame == intro_frames - 1) {
498 intro_done = true;
499 current_frame = 0;
500 } else {
501 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700502 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700503 } else {
504 current_frame = (current_frame + 1) % loop_frames;
505 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700506
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700507 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700508 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700509
510 // move the progress bar forward on timed intervals, if configured
511 int duration = progressScopeDuration;
512 if (progressBarType == DETERMINATE && duration > 0) {
513 double elapsed = now() - progressScopeTime;
514 float p = 1.0 * elapsed / duration;
515 if (p > 1.0) p = 1.0;
516 if (p > progress) {
517 progress = p;
518 redraw = true;
519 }
520 }
521
522 if (redraw) update_progress_locked();
523
524 pthread_mutex_unlock(&updateMutex);
525 double end = now();
526 // minimum of 20ms delay between frames
527 double delay = interval - (end - start);
528 if (delay < 0.02) delay = 0.02;
529 usleep(static_cast<useconds_t>(delay * 1000000));
530 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700531}
532
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700533void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700534 int result = res_create_display_surface(filename, surface);
535 if (result < 0) {
536 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
537 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800538}
539
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700540void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800541 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
542 if (result < 0) {
543 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
544 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700545}
546
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700547static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700548 char** result = new char*[rows];
549 for (size_t i = 0; i < rows; ++i) {
550 result[i] = new char[cols];
551 memset(result[i], 0, cols);
552 }
553 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700554}
555
Tianjie Xu35926c42016-04-28 18:06:26 -0700556// Choose the right background string to display during update.
557void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700558 if (security_update) {
559 LoadLocalizedBitmap("installing_security_text", &installing_text);
560 } else {
561 LoadLocalizedBitmap("installing_text", &installing_text);
562 }
563 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700564}
565
Sen Jiangd5304492016-12-09 16:20:49 -0800566bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700567 if (gr_init() < 0) {
568 return false;
569 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700570
Tao Bao171b4c42017-06-19 23:10:44 -0700571 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao4521b702017-06-20 18:11:21 -0700572 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
573 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700574 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700575}
576
Tao Bao736d59c2017-01-03 10:15:33 -0800577bool ScreenRecoveryUI::Init(const std::string& locale) {
578 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800579
Tao Bao736d59c2017-01-03 10:15:33 -0800580 if (!InitTextParams()) {
581 return false;
582 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700583
Tao Bao736d59c2017-01-03 10:15:33 -0800584 // Are we portrait or landscape?
585 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
586 // Are we the large variant of our base layout?
587 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700588
Tao Bao736d59c2017-01-03 10:15:33 -0800589 text_ = Alloc2d(text_rows_, text_cols_ + 1);
590 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800591
Tao Bao736d59c2017-01-03 10:15:33 -0800592 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700593
Tao Baoefb49ad2017-01-31 23:03:10 -0800594 // Set up the locale info.
595 SetLocale(locale);
596
Tao Bao736d59c2017-01-03 10:15:33 -0800597 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700598
Tao Bao736d59c2017-01-03 10:15:33 -0800599 LoadBitmap("progress_empty", &progressBarEmpty);
600 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700601
Tao Bao736d59c2017-01-03 10:15:33 -0800602 LoadBitmap("stage_empty", &stageMarkerEmpty);
603 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700604
Tao Bao736d59c2017-01-03 10:15:33 -0800605 // Background text for "installing_update" could be "installing update"
606 // or "installing security update". It will be set after UI init according
607 // to commands in BCB.
608 installing_text = nullptr;
609 LoadLocalizedBitmap("erasing_text", &erasing_text);
610 LoadLocalizedBitmap("no_command_text", &no_command_text);
611 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700612
Tao Bao736d59c2017-01-03 10:15:33 -0800613 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700614
Tao Bao736d59c2017-01-03 10:15:33 -0800615 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800616
Tao Bao736d59c2017-01-03 10:15:33 -0800617 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700618}
619
Elliott Hughes498cda62016-04-14 16:49:04 -0700620void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700621 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
622 dirent* de;
623 std::vector<std::string> intro_frame_names;
624 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700625
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700626 while ((de = readdir(dir.get())) != nullptr) {
627 int value, num_chars;
628 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
629 intro_frame_names.emplace_back(de->d_name, num_chars);
630 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
631 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700632 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700633 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700634
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700635 intro_frames = intro_frame_names.size();
636 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700637
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700638 // It's okay to not have an intro.
639 if (intro_frames == 0) intro_done = true;
640 // But you must have an animation.
641 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700642
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700643 std::sort(intro_frame_names.begin(), intro_frame_names.end());
644 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700645
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700646 introFrames = new GRSurface*[intro_frames];
647 for (size_t i = 0; i < intro_frames; i++) {
648 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
649 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700650
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700651 loopFrames = new GRSurface*[loop_frames];
652 for (size_t i = 0; i < loop_frames; i++) {
653 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
654 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700655}
656
Elliott Hughes8de52072015-04-08 20:06:50 -0700657void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700658 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700659
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700660 currentIcon = icon;
661 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700662
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700663 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700664}
665
Elliott Hughes8de52072015-04-08 20:06:50 -0700666void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700667 pthread_mutex_lock(&updateMutex);
668 if (progressBarType != type) {
669 progressBarType = type;
670 }
671 progressScopeStart = 0;
672 progressScopeSize = 0;
673 progress = 0;
674 update_progress_locked();
675 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700676}
677
Elliott Hughes8de52072015-04-08 20:06:50 -0700678void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700679 pthread_mutex_lock(&updateMutex);
680 progressBarType = DETERMINATE;
681 progressScopeStart += progressScopeSize;
682 progressScopeSize = portion;
683 progressScopeTime = now();
684 progressScopeDuration = seconds;
685 progress = 0;
686 update_progress_locked();
687 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700688}
689
Elliott Hughes8de52072015-04-08 20:06:50 -0700690void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700691 pthread_mutex_lock(&updateMutex);
692 if (fraction < 0.0) fraction = 0.0;
693 if (fraction > 1.0) fraction = 1.0;
694 if (progressBarType == DETERMINATE && fraction > progress) {
695 // Skip updates that aren't visibly different.
696 int width = gr_get_width(progressBarEmpty);
697 float scale = width * progressScopeSize;
698 if ((int)(progress * scale) != (int)(fraction * scale)) {
699 progress = fraction;
700 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700701 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700702 }
703 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700704}
705
Doug Zongkerc87bab12013-11-25 13:53:25 -0800706void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700707 pthread_mutex_lock(&updateMutex);
708 stage = current;
709 max_stage = max;
710 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800711}
712
Tao Baob6918c72015-05-19 17:02:16 -0700713void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700714 std::string str;
715 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700716
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700717 if (copy_to_stdout) {
718 fputs(str.c_str(), stdout);
719 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700720
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700721 pthread_mutex_lock(&updateMutex);
722 if (text_rows_ > 0 && text_cols_ > 0) {
723 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
724 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700725 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700726 text_col_ = 0;
727 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700728 }
729 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700730 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700731 text_[text_row_][text_col_] = '\0';
732 update_screen_locked();
733 }
734 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700735}
736
Tao Baob6918c72015-05-19 17:02:16 -0700737void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700738 va_list ap;
739 va_start(ap, fmt);
740 PrintV(fmt, true, ap);
741 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700742}
743
744void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700745 va_list ap;
746 va_start(ap, fmt);
747 PrintV(fmt, false, ap);
748 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700749}
750
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700751void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700752 pthread_mutex_lock(&updateMutex);
753 if (ch != '\n') text_[text_row_][text_col_++] = ch;
754 if (ch == '\n' || text_col_ >= text_cols_) {
755 text_col_ = 0;
756 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700757 }
758 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700759}
760
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700761void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700762 pthread_mutex_lock(&updateMutex);
763 text_col_ = 0;
764 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700765 for (size_t i = 0; i < text_rows_; ++i) {
766 memset(text_[i], 0, text_cols_ + 1);
767 }
768 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700769}
770
771void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700772 std::vector<off_t> offsets;
773 offsets.push_back(ftello(fp));
774 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700775
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700776 struct stat sb;
777 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700778
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700779 bool show_prompt = false;
780 while (true) {
781 if (show_prompt) {
782 PrintOnScreenOnly("--(%d%% of %d bytes)--",
783 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
784 static_cast<int>(sb.st_size));
785 Redraw();
786 while (show_prompt) {
787 show_prompt = false;
788 int key = WaitKey();
789 if (key == KEY_POWER || key == KEY_ENTER) {
790 return;
791 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
792 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700793 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700794 } else {
795 offsets.pop_back();
796 fseek(fp, offsets.back(), SEEK_SET);
797 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700798 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700799 if (feof(fp)) {
800 return;
801 }
802 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700803 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700804 }
805 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700806 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700807
808 int ch = getc(fp);
809 if (ch == EOF) {
810 while (text_row_ < text_rows_ - 1) PutChar('\n');
811 show_prompt = true;
812 } else {
813 PutChar(ch);
814 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
815 show_prompt = true;
816 }
817 }
818 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700819}
820
Elliott Hughes8de52072015-04-08 20:06:50 -0700821void ScreenRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700822 FILE* fp = fopen_path(filename, "re");
823 if (fp == nullptr) {
824 Print(" Unable to open %s: %s\n", filename, strerror(errno));
825 return;
826 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700827
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700828 char** old_text = text_;
829 size_t old_text_col = text_col_;
830 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700831
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700832 // Swap in the alternate screen and clear it.
833 text_ = file_viewer_text_;
834 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700835
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700836 ShowFile(fp);
837 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700838
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700839 text_ = old_text;
840 text_col_ = old_text_col;
841 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700842}
843
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700844void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700845 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700846 pthread_mutex_lock(&updateMutex);
847 if (text_rows_ > 0 && text_cols_ > 0) {
848 menu_headers_ = headers;
Tao Baoe15d7a52017-09-07 13:38:51 -0700849 menu_.clear();
850 for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) {
851 menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1)));
Doug Zongker211aebc2011-10-28 15:13:10 -0700852 }
Tao Baoe15d7a52017-09-07 13:38:51 -0700853 menu_items = static_cast<int>(menu_.size());
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700854 show_menu = true;
855 menu_sel = initial_selection;
856 update_screen_locked();
857 }
858 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700859}
860
861int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700862 pthread_mutex_lock(&updateMutex);
863 if (show_menu) {
864 int old_sel = menu_sel;
865 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700866
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700867 // Wrap at top and bottom.
868 if (menu_sel < 0) menu_sel = menu_items - 1;
869 if (menu_sel >= menu_items) menu_sel = 0;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700870
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700871 sel = menu_sel;
872 if (menu_sel != old_sel) update_screen_locked();
873 }
874 pthread_mutex_unlock(&updateMutex);
875 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700876}
877
878void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700879 pthread_mutex_lock(&updateMutex);
880 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
881 show_menu = false;
882 update_screen_locked();
883 }
884 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700885}
886
Elliott Hughes8de52072015-04-08 20:06:50 -0700887bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700888 pthread_mutex_lock(&updateMutex);
889 int visible = show_text;
890 pthread_mutex_unlock(&updateMutex);
891 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700892}
893
Elliott Hughes8de52072015-04-08 20:06:50 -0700894bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700895 pthread_mutex_lock(&updateMutex);
896 int ever_visible = show_text_ever;
897 pthread_mutex_unlock(&updateMutex);
898 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700899}
900
Elliott Hughes8de52072015-04-08 20:06:50 -0700901void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700902 pthread_mutex_lock(&updateMutex);
903 show_text = visible;
904 if (show_text) show_text_ever = true;
905 update_screen_locked();
906 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700907}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700908
Elliott Hughes8de52072015-04-08 20:06:50 -0700909void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700910 pthread_mutex_lock(&updateMutex);
911 update_screen_locked();
912 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700913}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700914
915void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700916 // Redraw so that if we're in the menu, the highlight
917 // will change color to indicate a successful long press.
918 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -0700919}
Tao Baoefb49ad2017-01-31 23:03:10 -0800920
921void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
922 locale_ = new_locale;
923 rtl_locale_ = false;
924
925 if (!new_locale.empty()) {
926 size_t underscore = new_locale.find('_');
927 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
928 std::string lang = new_locale.substr(0, underscore);
929
930 // A bit cheesy: keep an explicit list of supported RTL languages.
931 if (lang == "ar" || // Arabic
932 lang == "fa" || // Persian (Farsi)
933 lang == "he" || // Hebrew (new language code)
934 lang == "iw" || // Hebrew (old language code)
935 lang == "ur") { // Urdu
936 rtl_locale_ = true;
937 }
938 }
939}