blob: c8fb5aa75520880cf37bd89e07a1f9e95a08e333 [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);
Luke Song92eda4d2017-09-19 10:51:35 -0700152 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
153 return ScreenHeight() - 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();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700162 if (currentIcon != NONE) {
163 if (max_stage != -1) {
164 int stage_height = gr_get_height(stageMarkerEmpty);
165 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700166 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
167 int y = ScreenHeight() - stage_height - kMarginHeight;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700168 for (int i = 0; i < max_stage; ++i) {
169 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700170 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700171 x += stage_width;
172 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700174
175 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700176 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700177 int text_y = GetTextBaseline();
178 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700179 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700180 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700181}
182
Tao Baoea78d862017-06-28 14:52:17 -0700183// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
184// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700185void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800186 if (currentIcon != NONE) {
187 GRSurface* frame = GetCurrentFrame();
188 int frame_width = gr_get_width(frame);
189 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700190 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800191 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700192 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800193 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700194
Tao Bao736d59c2017-01-03 10:15:33 -0800195 if (progressBarType != EMPTY) {
196 int width = gr_get_width(progressBarEmpty);
197 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700198
Luke Song92eda4d2017-09-19 10:51:35 -0700199 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800200 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700201
Tao Bao736d59c2017-01-03 10:15:33 -0800202 // Erase behind the progress bar (in case this was a progress-only update)
203 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700204 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700205
Tao Bao736d59c2017-01-03 10:15:33 -0800206 if (progressBarType == DETERMINATE) {
207 float p = progressScopeStart + progress * progressScopeSize;
208 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700209
Tao Bao736d59c2017-01-03 10:15:33 -0800210 if (rtl_locale_) {
211 // Fill the progress bar from right to left.
212 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700213 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
214 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700215 }
Tao Bao736d59c2017-01-03 10:15:33 -0800216 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700217 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800218 }
219 } else {
220 // Fill the progress bar from left to right.
221 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700222 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800223 }
224 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700225 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800226 }
227 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700228 }
Tao Bao736d59c2017-01-03 10:15:33 -0800229 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700230}
231
Tao Bao99b2d772017-06-23 22:47:03 -0700232void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700233 switch (e) {
234 case INFO:
235 gr_color(249, 194, 0, 255);
236 break;
237 case HEADER:
238 gr_color(247, 0, 6, 255);
239 break;
240 case MENU:
241 case MENU_SEL_BG:
242 gr_color(0, 106, 157, 255);
243 break;
244 case MENU_SEL_BG_ACTIVE:
245 gr_color(0, 156, 100, 255);
246 break;
247 case MENU_SEL_FG:
248 gr_color(255, 255, 255, 255);
249 break;
250 case LOG:
251 gr_color(196, 196, 196, 255);
252 break;
253 case TEXT_FILL:
254 gr_color(0, 0, 0, 160);
255 break;
256 default:
257 gr_color(255, 255, 255, 255);
258 break;
259 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700260}
Doug Zongker211aebc2011-10-28 15:13:10 -0700261
Tianjie Xu29d55752017-09-20 17:53:46 -0700262void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
263 size_t sel) {
264 SetLocale(locales_entries[sel]);
265 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
266 "installing_security_text", "no_command_text" };
267 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
268 for (const auto& name : text_name) {
269 GRSurface* text_image = nullptr;
270 LoadLocalizedBitmap(name.c_str(), &text_image);
271 if (!text_image) {
272 Print("Failed to load %s\n", name.c_str());
273 return;
274 }
275 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
276 }
277
278 pthread_mutex_lock(&updateMutex);
279 gr_color(0, 0, 0, 255);
280 gr_clear();
281
282 int text_y = kMarginHeight;
283 int text_x = kMarginWidth;
284 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
285 // Write the header and descriptive texts.
286 SetColor(INFO);
287 std::string header = "Show background text image";
288 text_y += DrawTextLine(text_x, text_y, header.c_str(), true);
289 std::string locale_selection = android::base::StringPrintf(
290 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel, locales_entries.size());
291 const char* instruction[] = { locale_selection.c_str(),
292 "Use volume up/down to switch locales and power to exit.",
293 nullptr };
294 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
295
296 // Iterate through the text images and display them in order for the current locale.
297 for (const auto& p : surfaces) {
298 text_y += line_spacing;
299 SetColor(LOG);
300 text_y += DrawTextLine(text_x, text_y, p.first.c_str(), false);
301 gr_color(255, 255, 255, 255);
302 gr_texticon(text_x, text_y, p.second.get());
303 text_y += gr_get_height(p.second.get());
304 }
305 // Update the whole screen.
306 gr_flip();
307 pthread_mutex_unlock(&updateMutex);
308}
309
310void ScreenRecoveryUI::CheckBackgroundTextImages(const std::string& saved_locale) {
311 // Load a list of locales embedded in one of the resource files.
312 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
313 if (locales_entries.empty()) {
314 Print("Failed to load locales from the resource files\n");
315 return;
316 }
317 size_t selected = 0;
318 SelectAndShowBackgroundText(locales_entries, selected);
319
320 FlushKeys();
321 while (true) {
322 int key = WaitKey();
323 if (key == KEY_POWER || key == KEY_ENTER) {
324 break;
325 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
326 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
327 SelectAndShowBackgroundText(locales_entries, selected);
328 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
329 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
330 SelectAndShowBackgroundText(locales_entries, selected);
331 }
332 }
333
334 SetLocale(saved_locale);
335}
336
Luke Song92eda4d2017-09-19 10:51:35 -0700337int ScreenRecoveryUI::ScreenWidth() const {
338 return gr_fb_width();
339}
340
341int ScreenRecoveryUI::ScreenHeight() const {
342 return gr_fb_height();
343}
344
345void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
346 int dy) const {
347 gr_blit(surface, sx, sy, w, h, dx, dy);
348}
349
Tao Baoea78d862017-06-28 14:52:17 -0700350int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700351 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700352 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700353}
354
Luke Songe2bd8762017-06-12 16:08:33 -0700355void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700356 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700357}
358
Luke Song92eda4d2017-09-19 10:51:35 -0700359void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
360 gr_fill(x, y, w, h);
361}
362
363void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
364 gr_texticon(x, y, surface);
365}
366
Tao Baoea78d862017-06-28 14:52:17 -0700367int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const {
368 gr_text(gr_sys_font(), x, y, line, bold);
369 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700370}
371
Tao Baoea78d862017-06-28 14:52:17 -0700372int ScreenRecoveryUI::DrawTextLines(int x, int y, const char* const* lines) const {
373 int offset = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700374 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Tao Baoea78d862017-06-28 14:52:17 -0700375 offset += DrawTextLine(x, y + offset, lines[i], false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700376 }
Tao Baoea78d862017-06-28 14:52:17 -0700377 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700378}
379
Tao Bao2bbc6d62017-08-13 23:48:55 -0700380int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* lines) const {
381 int offset = 0;
382 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
383 // The line will be wrapped if it exceeds text_cols_.
384 std::string line(lines[i]);
385 size_t next_start = 0;
386 while (next_start < line.size()) {
387 std::string sub = line.substr(next_start, text_cols_ + 1);
388 if (sub.size() <= text_cols_) {
389 next_start += sub.size();
390 } else {
391 // Line too long and must be wrapped to text_cols_ columns.
392 size_t last_space = sub.find_last_of(" \t\n");
393 if (last_space == std::string::npos) {
394 // No space found, just draw as much as we can
395 sub.resize(text_cols_);
396 next_start += text_cols_;
397 } else {
398 sub.resize(last_space);
399 next_start += last_space + 1;
400 }
401 }
402 offset += DrawTextLine(x, y + offset, sub.c_str(), false);
403 }
404 }
405 return offset;
406}
407
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700408static const char* REGULAR_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700409 "Use volume up/down and power.",
410 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700411};
412
413static const char* LONG_PRESS_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700414 "Any button cycles highlight.",
415 "Long-press activates.",
416 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700417};
418
Tao Bao171b4c42017-06-19 23:10:44 -0700419// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
420// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700421void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700422 if (!show_text) {
423 draw_background_locked();
424 draw_foreground_locked();
425 return;
426 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700427
Tao Bao171b4c42017-06-19 23:10:44 -0700428 gr_color(0, 0, 0, 255);
429 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700430
Tao Bao4521b702017-06-20 18:11:21 -0700431 int y = kMarginHeight;
Tao Bao171b4c42017-06-19 23:10:44 -0700432 if (show_menu) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700433 static constexpr int kMenuIndent = 4;
434 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700435
Tao Bao171b4c42017-06-19 23:10:44 -0700436 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700437 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700438 std::string recovery_fingerprint =
439 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700440 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700441 y += DrawTextLine(x, y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700442 }
Tao Baoea78d862017-06-28 14:52:17 -0700443 y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Tao Bao171b4c42017-06-19 23:10:44 -0700444
445 SetColor(HEADER);
Tao Bao13aa4a92017-08-16 13:25:55 -0700446 // Ignore kMenuIndent, which is not taken into account by text_cols_.
447 y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_);
Tao Bao171b4c42017-06-19 23:10:44 -0700448
449 SetColor(MENU);
Tao Baoea78d862017-06-28 14:52:17 -0700450 y += DrawHorizontalRule(y) + 4;
Tao Bao171b4c42017-06-19 23:10:44 -0700451 for (int i = 0; i < menu_items; ++i) {
452 if (i == menu_sel) {
453 // Draw the highlight bar.
454 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Luke Song92eda4d2017-09-19 10:51:35 -0700455 DrawHighlightBar(0, y - 2, ScreenWidth(), char_height_ + 4);
Tao Bao171b4c42017-06-19 23:10:44 -0700456 // Bold white text for the selected item.
457 SetColor(MENU_SEL_FG);
Tao Baoe15d7a52017-09-07 13:38:51 -0700458 y += DrawTextLine(x, y, menu_[i].c_str(), true);
Tao Bao171b4c42017-06-19 23:10:44 -0700459 SetColor(MENU);
460 } else {
Tao Baoe15d7a52017-09-07 13:38:51 -0700461 y += DrawTextLine(x, y, menu_[i].c_str(), false);
Tao Bao171b4c42017-06-19 23:10:44 -0700462 }
463 }
Tao Baoea78d862017-06-28 14:52:17 -0700464 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700465 }
466
467 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
468 // we've displayed the entire text buffer.
469 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700470 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700471 size_t count = 0;
Luke Song92eda4d2017-09-19 10:51:35 -0700472 for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700473 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700474 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700475 --row;
476 if (row < 0) row = text_rows_ - 1;
477 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700478}
479
480// Redraw everything on the screen and flip the screen (make it visible).
481// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700482void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700483 draw_screen_locked();
484 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700485}
486
487// Updates only the progress bar, if possible, otherwise redraws the screen.
488// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700489void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700490 if (show_text || !pagesIdentical) {
491 draw_screen_locked(); // Must redraw the whole screen
492 pagesIdentical = true;
493 } else {
494 draw_foreground_locked(); // Draw only the progress bar and overlays
495 }
496 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700497}
498
499// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700500void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700501 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
502 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700503}
504
Elliott Hughes985022a2015-04-13 13:04:32 -0700505void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700506 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700507 while (true) {
508 double start = now();
509 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700510
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700511 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700512
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700513 // update the installation animation, if active
514 // skip this if we have a text overlay (too expensive to update)
515 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
516 if (!intro_done) {
517 if (current_frame == intro_frames - 1) {
518 intro_done = true;
519 current_frame = 0;
520 } else {
521 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700522 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700523 } else {
524 current_frame = (current_frame + 1) % loop_frames;
525 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700526
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700527 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700528 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700529
530 // move the progress bar forward on timed intervals, if configured
531 int duration = progressScopeDuration;
532 if (progressBarType == DETERMINATE && duration > 0) {
533 double elapsed = now() - progressScopeTime;
534 float p = 1.0 * elapsed / duration;
535 if (p > 1.0) p = 1.0;
536 if (p > progress) {
537 progress = p;
538 redraw = true;
539 }
540 }
541
542 if (redraw) update_progress_locked();
543
544 pthread_mutex_unlock(&updateMutex);
545 double end = now();
546 // minimum of 20ms delay between frames
547 double delay = interval - (end - start);
548 if (delay < 0.02) delay = 0.02;
549 usleep(static_cast<useconds_t>(delay * 1000000));
550 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700551}
552
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700553void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700554 int result = res_create_display_surface(filename, surface);
555 if (result < 0) {
556 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
557 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800558}
559
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700560void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800561 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
562 if (result < 0) {
563 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
564 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700565}
566
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700567static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700568 char** result = new char*[rows];
569 for (size_t i = 0; i < rows; ++i) {
570 result[i] = new char[cols];
571 memset(result[i], 0, cols);
572 }
573 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700574}
575
Tianjie Xu35926c42016-04-28 18:06:26 -0700576// Choose the right background string to display during update.
577void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700578 if (security_update) {
579 LoadLocalizedBitmap("installing_security_text", &installing_text);
580 } else {
581 LoadLocalizedBitmap("installing_text", &installing_text);
582 }
583 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700584}
585
Sen Jiangd5304492016-12-09 16:20:49 -0800586bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700587 if (gr_init() < 0) {
588 return false;
589 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700590
Tao Bao171b4c42017-06-19 23:10:44 -0700591 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Luke Song92eda4d2017-09-19 10:51:35 -0700592 text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
593 text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700594 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700595}
596
Tao Bao736d59c2017-01-03 10:15:33 -0800597bool ScreenRecoveryUI::Init(const std::string& locale) {
598 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800599
Tao Bao736d59c2017-01-03 10:15:33 -0800600 if (!InitTextParams()) {
601 return false;
602 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700603
Tao Bao736d59c2017-01-03 10:15:33 -0800604 // Are we portrait or landscape?
605 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
606 // Are we the large variant of our base layout?
607 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700608
Tao Bao736d59c2017-01-03 10:15:33 -0800609 text_ = Alloc2d(text_rows_, text_cols_ + 1);
610 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800611
Tao Bao736d59c2017-01-03 10:15:33 -0800612 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700613
Tao Baoefb49ad2017-01-31 23:03:10 -0800614 // Set up the locale info.
615 SetLocale(locale);
616
Tao Bao736d59c2017-01-03 10:15:33 -0800617 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700618
Tao Bao736d59c2017-01-03 10:15:33 -0800619 LoadBitmap("progress_empty", &progressBarEmpty);
620 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700621
Tao Bao736d59c2017-01-03 10:15:33 -0800622 LoadBitmap("stage_empty", &stageMarkerEmpty);
623 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700624
Tao Bao736d59c2017-01-03 10:15:33 -0800625 // Background text for "installing_update" could be "installing update"
626 // or "installing security update". It will be set after UI init according
627 // to commands in BCB.
628 installing_text = nullptr;
629 LoadLocalizedBitmap("erasing_text", &erasing_text);
630 LoadLocalizedBitmap("no_command_text", &no_command_text);
631 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700632
Tao Bao736d59c2017-01-03 10:15:33 -0800633 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700634
Tao Bao736d59c2017-01-03 10:15:33 -0800635 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800636
Tao Bao736d59c2017-01-03 10:15:33 -0800637 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700638}
639
Elliott Hughes498cda62016-04-14 16:49:04 -0700640void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700641 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
642 dirent* de;
643 std::vector<std::string> intro_frame_names;
644 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700645
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700646 while ((de = readdir(dir.get())) != nullptr) {
647 int value, num_chars;
648 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
649 intro_frame_names.emplace_back(de->d_name, num_chars);
650 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
651 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700652 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700653 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700654
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700655 intro_frames = intro_frame_names.size();
656 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700657
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700658 // It's okay to not have an intro.
659 if (intro_frames == 0) intro_done = true;
660 // But you must have an animation.
661 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700662
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700663 std::sort(intro_frame_names.begin(), intro_frame_names.end());
664 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700665
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700666 introFrames = new GRSurface*[intro_frames];
667 for (size_t i = 0; i < intro_frames; i++) {
668 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
669 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700670
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700671 loopFrames = new GRSurface*[loop_frames];
672 for (size_t i = 0; i < loop_frames; i++) {
673 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
674 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700675}
676
Elliott Hughes8de52072015-04-08 20:06:50 -0700677void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700678 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700679
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700680 currentIcon = icon;
681 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700682
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700683 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700684}
685
Elliott Hughes8de52072015-04-08 20:06:50 -0700686void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700687 pthread_mutex_lock(&updateMutex);
688 if (progressBarType != type) {
689 progressBarType = type;
690 }
691 progressScopeStart = 0;
692 progressScopeSize = 0;
693 progress = 0;
694 update_progress_locked();
695 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700696}
697
Elliott Hughes8de52072015-04-08 20:06:50 -0700698void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700699 pthread_mutex_lock(&updateMutex);
700 progressBarType = DETERMINATE;
701 progressScopeStart += progressScopeSize;
702 progressScopeSize = portion;
703 progressScopeTime = now();
704 progressScopeDuration = seconds;
705 progress = 0;
706 update_progress_locked();
707 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700708}
709
Elliott Hughes8de52072015-04-08 20:06:50 -0700710void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700711 pthread_mutex_lock(&updateMutex);
712 if (fraction < 0.0) fraction = 0.0;
713 if (fraction > 1.0) fraction = 1.0;
714 if (progressBarType == DETERMINATE && fraction > progress) {
715 // Skip updates that aren't visibly different.
716 int width = gr_get_width(progressBarEmpty);
717 float scale = width * progressScopeSize;
718 if ((int)(progress * scale) != (int)(fraction * scale)) {
719 progress = fraction;
720 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700721 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700722 }
723 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700724}
725
Doug Zongkerc87bab12013-11-25 13:53:25 -0800726void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700727 pthread_mutex_lock(&updateMutex);
728 stage = current;
729 max_stage = max;
730 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800731}
732
Tao Baob6918c72015-05-19 17:02:16 -0700733void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700734 std::string str;
735 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700736
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700737 if (copy_to_stdout) {
738 fputs(str.c_str(), stdout);
739 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700740
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700741 pthread_mutex_lock(&updateMutex);
742 if (text_rows_ > 0 && text_cols_ > 0) {
743 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
744 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700745 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700746 text_col_ = 0;
747 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700748 }
749 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700750 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700751 text_[text_row_][text_col_] = '\0';
752 update_screen_locked();
753 }
754 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700755}
756
Tao Baob6918c72015-05-19 17:02:16 -0700757void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700758 va_list ap;
759 va_start(ap, fmt);
760 PrintV(fmt, true, ap);
761 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700762}
763
764void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700765 va_list ap;
766 va_start(ap, fmt);
767 PrintV(fmt, false, ap);
768 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700769}
770
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700771void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700772 pthread_mutex_lock(&updateMutex);
773 if (ch != '\n') text_[text_row_][text_col_++] = ch;
774 if (ch == '\n' || text_col_ >= text_cols_) {
775 text_col_ = 0;
776 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700777 }
778 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700779}
780
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700781void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700782 pthread_mutex_lock(&updateMutex);
783 text_col_ = 0;
784 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700785 for (size_t i = 0; i < text_rows_; ++i) {
786 memset(text_[i], 0, text_cols_ + 1);
787 }
788 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700789}
790
791void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700792 std::vector<off_t> offsets;
793 offsets.push_back(ftello(fp));
794 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700795
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700796 struct stat sb;
797 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700798
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700799 bool show_prompt = false;
800 while (true) {
801 if (show_prompt) {
802 PrintOnScreenOnly("--(%d%% of %d bytes)--",
803 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
804 static_cast<int>(sb.st_size));
805 Redraw();
806 while (show_prompt) {
807 show_prompt = false;
808 int key = WaitKey();
809 if (key == KEY_POWER || key == KEY_ENTER) {
810 return;
811 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
812 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700813 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700814 } else {
815 offsets.pop_back();
816 fseek(fp, offsets.back(), SEEK_SET);
817 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700818 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700819 if (feof(fp)) {
820 return;
821 }
822 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700823 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700824 }
825 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700826 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700827
828 int ch = getc(fp);
829 if (ch == EOF) {
830 while (text_row_ < text_rows_ - 1) PutChar('\n');
831 show_prompt = true;
832 } else {
833 PutChar(ch);
834 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
835 show_prompt = true;
836 }
837 }
838 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700839}
840
Elliott Hughes8de52072015-04-08 20:06:50 -0700841void ScreenRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700842 FILE* fp = fopen_path(filename, "re");
843 if (fp == nullptr) {
844 Print(" Unable to open %s: %s\n", filename, strerror(errno));
845 return;
846 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700847
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700848 char** old_text = text_;
849 size_t old_text_col = text_col_;
850 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700851
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700852 // Swap in the alternate screen and clear it.
853 text_ = file_viewer_text_;
854 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700855
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700856 ShowFile(fp);
857 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700858
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700859 text_ = old_text;
860 text_col_ = old_text_col;
861 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700862}
863
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700864void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700865 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700866 pthread_mutex_lock(&updateMutex);
867 if (text_rows_ > 0 && text_cols_ > 0) {
868 menu_headers_ = headers;
Tao Baoe15d7a52017-09-07 13:38:51 -0700869 menu_.clear();
870 for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) {
871 menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1)));
Doug Zongker211aebc2011-10-28 15:13:10 -0700872 }
Tao Baoe15d7a52017-09-07 13:38:51 -0700873 menu_items = static_cast<int>(menu_.size());
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700874 show_menu = true;
875 menu_sel = initial_selection;
876 update_screen_locked();
877 }
878 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700879}
880
881int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700882 pthread_mutex_lock(&updateMutex);
883 if (show_menu) {
884 int old_sel = menu_sel;
885 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700886
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700887 // Wrap at top and bottom.
888 if (menu_sel < 0) menu_sel = menu_items - 1;
889 if (menu_sel >= menu_items) menu_sel = 0;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700890
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700891 sel = menu_sel;
892 if (menu_sel != old_sel) update_screen_locked();
893 }
894 pthread_mutex_unlock(&updateMutex);
895 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700896}
897
898void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700899 pthread_mutex_lock(&updateMutex);
900 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
901 show_menu = false;
902 update_screen_locked();
903 }
904 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700905}
906
Elliott Hughes8de52072015-04-08 20:06:50 -0700907bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700908 pthread_mutex_lock(&updateMutex);
909 int visible = show_text;
910 pthread_mutex_unlock(&updateMutex);
911 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700912}
913
Elliott Hughes8de52072015-04-08 20:06:50 -0700914bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700915 pthread_mutex_lock(&updateMutex);
916 int ever_visible = show_text_ever;
917 pthread_mutex_unlock(&updateMutex);
918 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700919}
920
Elliott Hughes8de52072015-04-08 20:06:50 -0700921void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700922 pthread_mutex_lock(&updateMutex);
923 show_text = visible;
924 if (show_text) show_text_ever = true;
925 update_screen_locked();
926 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700927}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700928
Elliott Hughes8de52072015-04-08 20:06:50 -0700929void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700930 pthread_mutex_lock(&updateMutex);
931 update_screen_locked();
932 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700933}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700934
935void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700936 // Redraw so that if we're in the menu, the highlight
937 // will change color to indicate a successful long press.
938 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -0700939}
Tao Baoefb49ad2017-01-31 23:03:10 -0800940
941void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
942 locale_ = new_locale;
943 rtl_locale_ = false;
944
945 if (!new_locale.empty()) {
946 size_t underscore = new_locale.find('_');
947 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
948 std::string lang = new_locale.substr(0, underscore);
949
950 // A bit cheesy: keep an explicit list of supported RTL languages.
951 if (lang == "ar" || // Arabic
952 lang == "fa" || // Persian (Farsi)
953 lang == "he" || // Hebrew (new language code)
954 lang == "iw" || // Hebrew (old language code)
955 lang == "ur") { // Urdu
956 rtl_locale_ = true;
957 }
958 }
959}