blob: d3f373eae2a6262b0f1530688c2285606eae1e1c [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 Zongkerdaefc1d2011-10-31 09:34:15 -070045#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070046#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070047
Doug Zongker211aebc2011-10-28 15:13:10 -070048// Return the current time as a double (including fractions of a second).
49static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070050 struct timeval tv;
51 gettimeofday(&tv, nullptr);
52 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070053}
54
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070055Menu::Menu(bool scrollable, size_t max_items, size_t max_length)
56 : scrollable_(scrollable),
57 max_display_items_(max_items),
58 max_item_length_(max_length),
59 text_headers_(nullptr),
60 menu_start_(0),
61 selection_(0) {
62 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
63}
64
65const char* const* Menu::text_headers() const {
66 return text_headers_;
67}
68
69std::string Menu::TextItem(size_t index) const {
70 CHECK_LT(index, text_items_.size());
71
72 return text_items_[index];
73}
74
75size_t Menu::MenuStart() const {
76 return menu_start_;
77}
78
79size_t Menu::MenuEnd() const {
80 return std::min(ItemsCount(), menu_start_ + max_display_items_);
81}
82
83size_t Menu::ItemsCount() const {
84 return text_items_.size();
85}
86
87bool Menu::ItemsOverflow(std::string* cur_selection_str) const {
88 if (!scrollable_ || static_cast<size_t>(ItemsCount()) <= max_display_items_) {
89 return false;
90 }
91
92 *cur_selection_str =
93 android::base::StringPrintf("Current item: %d/%zu", selection_ + 1, ItemsCount());
94 return true;
95}
96
97void Menu::Start(const char* const* headers, const char* const* items, int initial_selection) {
98 text_headers_ = headers;
99
100 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
101 size_t max_items_count = scrollable_ ? std::numeric_limits<int>::max() : max_display_items_;
102 for (size_t i = 0; i < max_items_count && items[i] != nullptr; ++i) {
103 text_items_.emplace_back(items[i], strnlen(items[i], max_item_length_));
104 }
105
106 CHECK(!text_items_.empty());
107 selection_ = initial_selection;
108}
109
110// TODO(xunchang) modify the function parameters to button up & down.
111int Menu::Select(int sel) {
112 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
113 int count = ItemsCount();
114
115 // Wraps the selection at boundary if the menu is not scrollable.
116 if (!scrollable_) {
117 if (sel < 0) {
118 selection_ = count - 1;
119 } else if (sel >= count) {
120 selection_ = 0;
121 } else {
122 selection_ = sel;
123 }
124
125 return selection_;
126 }
127
128 if (sel < 0) {
129 selection_ = 0;
130 } else if (sel >= count) {
131 selection_ = count - 1;
132 } else {
133 if (static_cast<size_t>(sel) < menu_start_) {
134 menu_start_--;
135 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
136 menu_start_++;
137 }
138 selection_ = sel;
139 }
140
141 return selection_;
142}
143
144ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
145
146ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao4521b702017-06-20 18:11:21 -0700147 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
148 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -0700149 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -0700150 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -0700151 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -0800152 progressBarType(EMPTY),
153 progressScopeStart(0),
154 progressScopeSize(0),
155 progress(0),
156 pagesIdentical(false),
157 text_cols_(0),
158 text_rows_(0),
159 text_(nullptr),
160 text_col_(0),
161 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800162 show_text(false),
163 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700164 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800165 file_viewer_text_(nullptr),
166 intro_frames(0),
167 loop_frames(0),
168 current_frame(0),
169 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800170 stage(-1),
171 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800172 locale_(""),
173 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800174 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700175
Tao Bao99b2d772017-06-23 22:47:03 -0700176GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700177 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
178 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
179 }
180 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700181}
182
Tao Bao99b2d772017-06-23 22:47:03 -0700183GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700184 switch (currentIcon) {
185 case ERASING:
186 return erasing_text;
187 case ERROR:
188 return error_text;
189 case INSTALLING_UPDATE:
190 return installing_text;
191 case NO_COMMAND:
192 return no_command_text;
193 case NONE:
194 abort();
195 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700196}
197
Mikhail Lappob49767c2017-03-23 21:44:26 +0100198int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700199 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700200}
201
202// Here's the intended layout:
203
Elliott Hughes6d089a92016-07-08 17:23:41 -0700204// | portrait large landscape large
205// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700206// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700207// icon | (200dp)
208// gap | 68dp 68dp 56dp 112dp
209// text | (14sp)
210// gap | 32dp 32dp 26dp 52dp
211// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700212// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700213
Tao Bao3250f722017-06-29 14:32:05 -0700214// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
215// 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 -0700216
Elliott Hughes6d089a92016-07-08 17:23:41 -0700217enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700218enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700219static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700220 { 32, 68, }, // PORTRAIT
221 { 32, 68, }, // PORTRAIT_LARGE
222 { 26, 56, }, // LANDSCAPE
223 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700224};
225
Tao Bao99b2d772017-06-23 22:47:03 -0700226int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700227 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700228}
229
Tao Bao99b2d772017-06-23 22:47:03 -0700230int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700231 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
232 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700233}
234
Tao Bao99b2d772017-06-23 22:47:03 -0700235int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700236 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
237 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
238 gr_get_height(progressBarFill);
Luke Song92eda4d2017-09-19 10:51:35 -0700239 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
240 return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700241}
242
Doug Zongker211aebc2011-10-28 15:13:10 -0700243// Clear the screen and draw the currently selected background icon (if any).
244// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700245void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700246 pagesIdentical = false;
247 gr_color(0, 0, 0, 255);
248 gr_clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700249 if (currentIcon != NONE) {
250 if (max_stage != -1) {
251 int stage_height = gr_get_height(stageMarkerEmpty);
252 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700253 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
254 int y = ScreenHeight() - stage_height - kMarginHeight;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700255 for (int i = 0; i < max_stage; ++i) {
256 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700257 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700258 x += stage_width;
259 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700260 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700261
262 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700263 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700264 int text_y = GetTextBaseline();
265 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700266 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700267 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700268}
269
Tao Baoea78d862017-06-28 14:52:17 -0700270// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
271// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700272void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800273 if (currentIcon != NONE) {
274 GRSurface* frame = GetCurrentFrame();
275 int frame_width = gr_get_width(frame);
276 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700277 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800278 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700279 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800280 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700281
Tao Bao736d59c2017-01-03 10:15:33 -0800282 if (progressBarType != EMPTY) {
283 int width = gr_get_width(progressBarEmpty);
284 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700285
Luke Song92eda4d2017-09-19 10:51:35 -0700286 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800287 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700288
Tao Bao736d59c2017-01-03 10:15:33 -0800289 // Erase behind the progress bar (in case this was a progress-only update)
290 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700291 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700292
Tao Bao736d59c2017-01-03 10:15:33 -0800293 if (progressBarType == DETERMINATE) {
294 float p = progressScopeStart + progress * progressScopeSize;
295 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700296
Tao Bao736d59c2017-01-03 10:15:33 -0800297 if (rtl_locale_) {
298 // Fill the progress bar from right to left.
299 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700300 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
301 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700302 }
Tao Bao736d59c2017-01-03 10:15:33 -0800303 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700304 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800305 }
306 } else {
307 // Fill the progress bar from left to right.
308 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700309 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800310 }
311 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700312 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800313 }
314 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700315 }
Tao Bao736d59c2017-01-03 10:15:33 -0800316 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700317}
318
Tao Bao99b2d772017-06-23 22:47:03 -0700319void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700320 switch (e) {
321 case INFO:
322 gr_color(249, 194, 0, 255);
323 break;
324 case HEADER:
325 gr_color(247, 0, 6, 255);
326 break;
327 case MENU:
328 case MENU_SEL_BG:
329 gr_color(0, 106, 157, 255);
330 break;
331 case MENU_SEL_BG_ACTIVE:
332 gr_color(0, 156, 100, 255);
333 break;
334 case MENU_SEL_FG:
335 gr_color(255, 255, 255, 255);
336 break;
337 case LOG:
338 gr_color(196, 196, 196, 255);
339 break;
340 case TEXT_FILL:
341 gr_color(0, 0, 0, 160);
342 break;
343 default:
344 gr_color(255, 255, 255, 255);
345 break;
346 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700347}
Doug Zongker211aebc2011-10-28 15:13:10 -0700348
Tianjie Xu29d55752017-09-20 17:53:46 -0700349void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
350 size_t sel) {
351 SetLocale(locales_entries[sel]);
352 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
353 "installing_security_text", "no_command_text" };
354 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
355 for (const auto& name : text_name) {
356 GRSurface* text_image = nullptr;
357 LoadLocalizedBitmap(name.c_str(), &text_image);
358 if (!text_image) {
359 Print("Failed to load %s\n", name.c_str());
360 return;
361 }
362 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
363 }
364
365 pthread_mutex_lock(&updateMutex);
366 gr_color(0, 0, 0, 255);
367 gr_clear();
368
369 int text_y = kMarginHeight;
370 int text_x = kMarginWidth;
371 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
372 // Write the header and descriptive texts.
373 SetColor(INFO);
374 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700375 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700376 std::string locale_selection = android::base::StringPrintf(
377 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700378 // clang-format off
379 std::vector<std::string> instruction = {
380 locale_selection,
381 "Use volume up/down to switch locales and power to exit."
382 };
383 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700384 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
385
386 // Iterate through the text images and display them in order for the current locale.
387 for (const auto& p : surfaces) {
388 text_y += line_spacing;
389 SetColor(LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700390 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700391 gr_color(255, 255, 255, 255);
392 gr_texticon(text_x, text_y, p.second.get());
393 text_y += gr_get_height(p.second.get());
394 }
395 // Update the whole screen.
396 gr_flip();
397 pthread_mutex_unlock(&updateMutex);
398}
399
400void ScreenRecoveryUI::CheckBackgroundTextImages(const std::string& saved_locale) {
401 // Load a list of locales embedded in one of the resource files.
402 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
403 if (locales_entries.empty()) {
404 Print("Failed to load locales from the resource files\n");
405 return;
406 }
407 size_t selected = 0;
408 SelectAndShowBackgroundText(locales_entries, selected);
409
410 FlushKeys();
411 while (true) {
412 int key = WaitKey();
413 if (key == KEY_POWER || key == KEY_ENTER) {
414 break;
415 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
416 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
417 SelectAndShowBackgroundText(locales_entries, selected);
418 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
419 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
420 SelectAndShowBackgroundText(locales_entries, selected);
421 }
422 }
423
424 SetLocale(saved_locale);
425}
426
Luke Song92eda4d2017-09-19 10:51:35 -0700427int ScreenRecoveryUI::ScreenWidth() const {
428 return gr_fb_width();
429}
430
431int ScreenRecoveryUI::ScreenHeight() const {
432 return gr_fb_height();
433}
434
435void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
436 int dy) const {
437 gr_blit(surface, sx, sy, w, h, dx, dy);
438}
439
Tao Baoea78d862017-06-28 14:52:17 -0700440int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700441 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700442 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700443}
444
Luke Songe2bd8762017-06-12 16:08:33 -0700445void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700446 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700447}
448
Luke Song92eda4d2017-09-19 10:51:35 -0700449void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
450 gr_fill(x, y, w, h);
451}
452
453void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
454 gr_texticon(x, y, surface);
455}
456
Tao Bao93e46ad2018-05-02 14:57:21 -0700457int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
458 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700459 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700460}
461
Tao Bao93e46ad2018-05-02 14:57:21 -0700462int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700463 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700464 for (const auto& line : lines) {
465 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700466 }
Tao Baoea78d862017-06-28 14:52:17 -0700467 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700468}
469
Tao Bao93e46ad2018-05-02 14:57:21 -0700470int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
471 const std::vector<std::string>& lines) const {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700472 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700473 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700474 size_t next_start = 0;
475 while (next_start < line.size()) {
476 std::string sub = line.substr(next_start, text_cols_ + 1);
477 if (sub.size() <= text_cols_) {
478 next_start += sub.size();
479 } else {
480 // Line too long and must be wrapped to text_cols_ columns.
481 size_t last_space = sub.find_last_of(" \t\n");
482 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700483 // No space found, just draw as much as we can.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700484 sub.resize(text_cols_);
485 next_start += text_cols_;
486 } else {
487 sub.resize(last_space);
488 next_start += last_space + 1;
489 }
490 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700491 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700492 }
493 }
494 return offset;
495}
496
Tao Bao171b4c42017-06-19 23:10:44 -0700497// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
498// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700499void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700500 if (!show_text) {
501 draw_background_locked();
502 draw_foreground_locked();
503 return;
504 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700505
Tao Bao171b4c42017-06-19 23:10:44 -0700506 gr_color(0, 0, 0, 255);
507 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700508
Tao Bao93e46ad2018-05-02 14:57:21 -0700509 // clang-format off
510 static std::vector<std::string> REGULAR_HELP = {
511 "Use volume up/down and power.",
512 };
513 static std::vector<std::string> LONG_PRESS_HELP = {
514 "Any button cycles highlight.",
515 "Long-press activates.",
516 };
517 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700518 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
519}
520
521// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700522void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
523 const std::vector<std::string>& help_message) {
Tao Bao4521b702017-06-20 18:11:21 -0700524 int y = kMarginHeight;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700525 if (menu_) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700526 static constexpr int kMenuIndent = 4;
527 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700528
Tao Bao171b4c42017-06-19 23:10:44 -0700529 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700530 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700531 std::string recovery_fingerprint =
532 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700533 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700534 y += DrawTextLine(x, y, chunk, false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700535 }
Tao Bao171b4c42017-06-19 23:10:44 -0700536
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700537 y += DrawTextLines(x, y, help_message);
538
Tao Bao93e46ad2018-05-02 14:57:21 -0700539 auto convert_to_vector = [](const char* const* items) -> std::vector<std::string> {
540 if (items == nullptr) return {};
541
542 std::vector<std::string> result;
543 for (size_t i = 0; items[i] != nullptr; ++i) {
544 result.emplace_back(items[i]);
545 }
546 return result;
547 };
548
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700549 // Draw menu header.
Tao Bao171b4c42017-06-19 23:10:44 -0700550 SetColor(HEADER);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700551 if (!menu_->scrollable()) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700552 y += DrawWrappedTextLines(x, y, convert_to_vector(menu_->text_headers()));
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700553 } else {
Tao Bao93e46ad2018-05-02 14:57:21 -0700554 y += DrawTextLines(x, y, convert_to_vector(menu_->text_headers()));
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700555 // Show the current menu item number in relation to total number if items don't fit on the
556 // screen.
557 std::string cur_selection_str;
558 if (menu_->ItemsOverflow(&cur_selection_str)) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700559 y += DrawTextLine(x, y, cur_selection_str, true);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700560 }
561 }
Tao Bao171b4c42017-06-19 23:10:44 -0700562
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700563 // Draw menu items.
Tao Bao171b4c42017-06-19 23:10:44 -0700564 SetColor(MENU);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700565 // Do not draw the horizontal rule for wear devices.
566 if (!menu_->scrollable()) {
567 y += DrawHorizontalRule(y) + 4;
568 }
569 for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) {
570 bool bold = false;
571 if (i == static_cast<size_t>(menu_->selection())) {
Tao Bao171b4c42017-06-19 23:10:44 -0700572 // Draw the highlight bar.
573 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700574
575 int bar_height = char_height_ + 4;
576 DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height);
577
Tao Bao171b4c42017-06-19 23:10:44 -0700578 // Bold white text for the selected item.
579 SetColor(MENU_SEL_FG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700580 bold = true;
Tao Bao171b4c42017-06-19 23:10:44 -0700581 }
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700582
Tao Bao93e46ad2018-05-02 14:57:21 -0700583 y += DrawTextLine(x, y, menu_->TextItem(i), bold);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700584
585 SetColor(MENU);
Tao Bao171b4c42017-06-19 23:10:44 -0700586 }
Tao Baoea78d862017-06-28 14:52:17 -0700587 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700588 }
589
590 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
591 // we've displayed the entire text buffer.
592 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700593 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700594 size_t count = 0;
Luke Song92eda4d2017-09-19 10:51:35 -0700595 for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700596 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700597 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700598 --row;
599 if (row < 0) row = text_rows_ - 1;
600 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700601}
602
603// Redraw everything on the screen and flip the screen (make it visible).
604// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700605void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700606 draw_screen_locked();
607 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700608}
609
610// Updates only the progress bar, if possible, otherwise redraws the screen.
611// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700612void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700613 if (show_text || !pagesIdentical) {
614 draw_screen_locked(); // Must redraw the whole screen
615 pagesIdentical = true;
616 } else {
617 draw_foreground_locked(); // Draw only the progress bar and overlays
618 }
619 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700620}
621
622// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700623void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700624 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
625 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700626}
627
Elliott Hughes985022a2015-04-13 13:04:32 -0700628void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700629 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700630 while (true) {
631 double start = now();
632 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700633
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700634 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700635
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700636 // update the installation animation, if active
637 // skip this if we have a text overlay (too expensive to update)
638 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
639 if (!intro_done) {
640 if (current_frame == intro_frames - 1) {
641 intro_done = true;
642 current_frame = 0;
643 } else {
644 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700645 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700646 } else {
647 current_frame = (current_frame + 1) % loop_frames;
648 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700649
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700650 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700651 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700652
653 // move the progress bar forward on timed intervals, if configured
654 int duration = progressScopeDuration;
655 if (progressBarType == DETERMINATE && duration > 0) {
656 double elapsed = now() - progressScopeTime;
657 float p = 1.0 * elapsed / duration;
658 if (p > 1.0) p = 1.0;
659 if (p > progress) {
660 progress = p;
661 redraw = true;
662 }
663 }
664
665 if (redraw) update_progress_locked();
666
667 pthread_mutex_unlock(&updateMutex);
668 double end = now();
669 // minimum of 20ms delay between frames
670 double delay = interval - (end - start);
671 if (delay < 0.02) delay = 0.02;
672 usleep(static_cast<useconds_t>(delay * 1000000));
673 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700674}
675
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700676void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700677 int result = res_create_display_surface(filename, surface);
678 if (result < 0) {
679 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
680 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800681}
682
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700683void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800684 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
685 if (result < 0) {
686 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
687 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700688}
689
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700690static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700691 char** result = new char*[rows];
692 for (size_t i = 0; i < rows; ++i) {
693 result[i] = new char[cols];
694 memset(result[i], 0, cols);
695 }
696 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700697}
698
Tianjie Xu35926c42016-04-28 18:06:26 -0700699// Choose the right background string to display during update.
700void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700701 if (security_update) {
702 LoadLocalizedBitmap("installing_security_text", &installing_text);
703 } else {
704 LoadLocalizedBitmap("installing_text", &installing_text);
705 }
706 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700707}
708
Sen Jiangd5304492016-12-09 16:20:49 -0800709bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700710 if (gr_init() < 0) {
711 return false;
712 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700713
Tao Bao171b4c42017-06-19 23:10:44 -0700714 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Luke Song92eda4d2017-09-19 10:51:35 -0700715 text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
716 text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700717 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700718}
719
Tao Bao736d59c2017-01-03 10:15:33 -0800720bool ScreenRecoveryUI::Init(const std::string& locale) {
721 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800722
Tao Bao736d59c2017-01-03 10:15:33 -0800723 if (!InitTextParams()) {
724 return false;
725 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700726
Tao Bao736d59c2017-01-03 10:15:33 -0800727 // Are we portrait or landscape?
728 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
729 // Are we the large variant of our base layout?
730 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700731
Tao Bao736d59c2017-01-03 10:15:33 -0800732 text_ = Alloc2d(text_rows_, text_cols_ + 1);
733 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800734
Tao Bao736d59c2017-01-03 10:15:33 -0800735 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700736
Tao Baoefb49ad2017-01-31 23:03:10 -0800737 // Set up the locale info.
738 SetLocale(locale);
739
Tao Bao736d59c2017-01-03 10:15:33 -0800740 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700741
Tao Bao736d59c2017-01-03 10:15:33 -0800742 LoadBitmap("progress_empty", &progressBarEmpty);
743 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700744
Tao Bao736d59c2017-01-03 10:15:33 -0800745 LoadBitmap("stage_empty", &stageMarkerEmpty);
746 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700747
Tao Bao736d59c2017-01-03 10:15:33 -0800748 // Background text for "installing_update" could be "installing update"
749 // or "installing security update". It will be set after UI init according
750 // to commands in BCB.
751 installing_text = nullptr;
752 LoadLocalizedBitmap("erasing_text", &erasing_text);
753 LoadLocalizedBitmap("no_command_text", &no_command_text);
754 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700755
Tao Bao736d59c2017-01-03 10:15:33 -0800756 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700757
Tao Bao736d59c2017-01-03 10:15:33 -0800758 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800759
Tao Bao736d59c2017-01-03 10:15:33 -0800760 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700761}
762
Elliott Hughes498cda62016-04-14 16:49:04 -0700763void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700764 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
765 dirent* de;
766 std::vector<std::string> intro_frame_names;
767 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700768
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700769 while ((de = readdir(dir.get())) != nullptr) {
770 int value, num_chars;
771 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
772 intro_frame_names.emplace_back(de->d_name, num_chars);
773 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
774 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700775 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700776 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700777
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700778 intro_frames = intro_frame_names.size();
779 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700780
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700781 // It's okay to not have an intro.
782 if (intro_frames == 0) intro_done = true;
783 // But you must have an animation.
784 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700785
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700786 std::sort(intro_frame_names.begin(), intro_frame_names.end());
787 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700788
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700789 introFrames = new GRSurface*[intro_frames];
790 for (size_t i = 0; i < intro_frames; i++) {
791 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
792 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700793
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700794 loopFrames = new GRSurface*[loop_frames];
795 for (size_t i = 0; i < loop_frames; i++) {
796 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
797 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700798}
799
Elliott Hughes8de52072015-04-08 20:06:50 -0700800void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700801 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700802
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700803 currentIcon = icon;
804 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700805
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700806 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700807}
808
Elliott Hughes8de52072015-04-08 20:06:50 -0700809void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700810 pthread_mutex_lock(&updateMutex);
811 if (progressBarType != type) {
812 progressBarType = type;
813 }
814 progressScopeStart = 0;
815 progressScopeSize = 0;
816 progress = 0;
817 update_progress_locked();
818 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700819}
820
Elliott Hughes8de52072015-04-08 20:06:50 -0700821void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700822 pthread_mutex_lock(&updateMutex);
823 progressBarType = DETERMINATE;
824 progressScopeStart += progressScopeSize;
825 progressScopeSize = portion;
826 progressScopeTime = now();
827 progressScopeDuration = seconds;
828 progress = 0;
829 update_progress_locked();
830 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700831}
832
Elliott Hughes8de52072015-04-08 20:06:50 -0700833void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700834 pthread_mutex_lock(&updateMutex);
835 if (fraction < 0.0) fraction = 0.0;
836 if (fraction > 1.0) fraction = 1.0;
837 if (progressBarType == DETERMINATE && fraction > progress) {
838 // Skip updates that aren't visibly different.
839 int width = gr_get_width(progressBarEmpty);
840 float scale = width * progressScopeSize;
841 if ((int)(progress * scale) != (int)(fraction * scale)) {
842 progress = fraction;
843 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700844 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700845 }
846 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700847}
848
Doug Zongkerc87bab12013-11-25 13:53:25 -0800849void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700850 pthread_mutex_lock(&updateMutex);
851 stage = current;
852 max_stage = max;
853 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800854}
855
Tao Baob6918c72015-05-19 17:02:16 -0700856void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700857 std::string str;
858 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700859
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700860 if (copy_to_stdout) {
861 fputs(str.c_str(), stdout);
862 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700863
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700864 pthread_mutex_lock(&updateMutex);
865 if (text_rows_ > 0 && text_cols_ > 0) {
866 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
867 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700868 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700869 text_col_ = 0;
870 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700871 }
872 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700873 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700874 text_[text_row_][text_col_] = '\0';
875 update_screen_locked();
876 }
877 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700878}
879
Tao Baob6918c72015-05-19 17:02:16 -0700880void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700881 va_list ap;
882 va_start(ap, fmt);
883 PrintV(fmt, true, ap);
884 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700885}
886
887void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700888 va_list ap;
889 va_start(ap, fmt);
890 PrintV(fmt, false, ap);
891 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700892}
893
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700894void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700895 pthread_mutex_lock(&updateMutex);
896 if (ch != '\n') text_[text_row_][text_col_++] = ch;
897 if (ch == '\n' || text_col_ >= text_cols_) {
898 text_col_ = 0;
899 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700900 }
901 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700902}
903
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700904void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700905 pthread_mutex_lock(&updateMutex);
906 text_col_ = 0;
907 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700908 for (size_t i = 0; i < text_rows_; ++i) {
909 memset(text_[i], 0, text_cols_ + 1);
910 }
911 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700912}
913
914void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700915 std::vector<off_t> offsets;
916 offsets.push_back(ftello(fp));
917 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700918
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700919 struct stat sb;
920 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700921
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700922 bool show_prompt = false;
923 while (true) {
924 if (show_prompt) {
925 PrintOnScreenOnly("--(%d%% of %d bytes)--",
926 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
927 static_cast<int>(sb.st_size));
928 Redraw();
929 while (show_prompt) {
930 show_prompt = false;
931 int key = WaitKey();
932 if (key == KEY_POWER || key == KEY_ENTER) {
933 return;
934 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
935 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700936 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700937 } else {
938 offsets.pop_back();
939 fseek(fp, offsets.back(), SEEK_SET);
940 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700941 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700942 if (feof(fp)) {
943 return;
944 }
945 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700946 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700947 }
948 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700949 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700950
951 int ch = getc(fp);
952 if (ch == EOF) {
953 while (text_row_ < text_rows_ - 1) PutChar('\n');
954 show_prompt = true;
955 } else {
956 PutChar(ch);
957 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
958 show_prompt = true;
959 }
960 }
961 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700962}
963
Tao Bao1d156b92018-05-02 12:43:18 -0700964void ScreenRecoveryUI::ShowFile(const std::string& filename) {
965 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
966 if (!fp) {
967 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700968 return;
969 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700970
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700971 char** old_text = text_;
972 size_t old_text_col = text_col_;
973 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700974
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700975 // Swap in the alternate screen and clear it.
976 text_ = file_viewer_text_;
977 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700978
Tao Bao1d156b92018-05-02 12:43:18 -0700979 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -0700980
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700981 text_ = old_text;
982 text_col_ = old_text_col;
983 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700984}
985
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700986void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700987 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700988 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700989 if (text_rows_ > 0 && text_cols_ > 1) {
990 menu_ = std::make_unique<Menu>(scrollable_menu_, text_rows_, text_cols_ - 1);
991 menu_->Start(headers, items, initial_selection);
992
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700993 update_screen_locked();
994 }
995 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700996}
997
998int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700999 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001000 if (menu_) {
1001 int old_sel = menu_->selection();
1002 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001003
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001004 if (sel != old_sel) {
1005 update_screen_locked();
1006 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001007 }
1008 pthread_mutex_unlock(&updateMutex);
1009 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001010}
1011
1012void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001013 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001014 if (menu_) {
1015 menu_.reset();
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001016 update_screen_locked();
1017 }
1018 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001019}
1020
Tao Bao3aec6962018-04-20 09:24:58 -07001021int ScreenRecoveryUI::ShowMenu(const char* const* headers, const char* const* items,
1022 int initial_selection, bool menu_only,
1023 const std::function<int(int, bool)>& key_handler) {
1024 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1025 FlushKeys();
1026
1027 StartMenu(headers, items, initial_selection);
1028
1029 int selected = initial_selection;
1030 int chosen_item = -1;
1031 while (chosen_item < 0) {
1032 int key = WaitKey();
1033 if (key == -1) { // WaitKey() timed out.
1034 if (WasTextEverVisible()) {
1035 continue;
1036 } else {
1037 LOG(INFO) << "Timed out waiting for key input; rebooting.";
1038 EndMenu();
1039 return -1;
1040 }
1041 }
1042
1043 bool visible = IsTextVisible();
1044 int action = key_handler(key, visible);
1045 if (action < 0) {
1046 switch (action) {
1047 case Device::kHighlightUp:
1048 selected = SelectMenu(--selected);
1049 break;
1050 case Device::kHighlightDown:
1051 selected = SelectMenu(++selected);
1052 break;
1053 case Device::kInvokeItem:
1054 chosen_item = selected;
1055 break;
1056 case Device::kNoAction:
1057 break;
1058 }
1059 } else if (!menu_only) {
1060 chosen_item = action;
1061 }
1062 }
1063
1064 EndMenu();
1065 return chosen_item;
1066}
1067
Elliott Hughes8de52072015-04-08 20:06:50 -07001068bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001069 pthread_mutex_lock(&updateMutex);
1070 int visible = show_text;
1071 pthread_mutex_unlock(&updateMutex);
1072 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001073}
1074
Elliott Hughes8de52072015-04-08 20:06:50 -07001075bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001076 pthread_mutex_lock(&updateMutex);
1077 int ever_visible = show_text_ever;
1078 pthread_mutex_unlock(&updateMutex);
1079 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001080}
1081
Elliott Hughes8de52072015-04-08 20:06:50 -07001082void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001083 pthread_mutex_lock(&updateMutex);
1084 show_text = visible;
1085 if (show_text) show_text_ever = true;
1086 update_screen_locked();
1087 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001088}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001089
Elliott Hughes8de52072015-04-08 20:06:50 -07001090void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001091 pthread_mutex_lock(&updateMutex);
1092 update_screen_locked();
1093 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001094}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001095
1096void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001097 // Redraw so that if we're in the menu, the highlight
1098 // will change color to indicate a successful long press.
1099 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001100}
Tao Baoefb49ad2017-01-31 23:03:10 -08001101
1102void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1103 locale_ = new_locale;
1104 rtl_locale_ = false;
1105
1106 if (!new_locale.empty()) {
1107 size_t underscore = new_locale.find('_');
1108 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
1109 std::string lang = new_locale.substr(0, underscore);
1110
1111 // A bit cheesy: keep an explicit list of supported RTL languages.
1112 if (lang == "ar" || // Arabic
1113 lang == "fa" || // Persian (Farsi)
1114 lang == "he" || // Hebrew (new language code)
1115 lang == "iw" || // Hebrew (old language code)
1116 lang == "ur") { // Urdu
1117 rtl_locale_ = true;
1118 }
1119 }
1120}