blob: f1b38781a4f1e2cc92874a004dbc5481f01981b4 [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Baoefb49ad2017-01-31 23:03:10 -080017#include "screen_ui.h"
18
Elliott Hughes498cda62016-04-14 16:49:04 -070019#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070020#include <errno.h>
21#include <fcntl.h>
22#include <linux/input.h>
23#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/types.h>
31#include <time.h>
32#include <unistd.h>
33
Tao Bao1fe1afe2018-05-01 15:56:05 -070034#include <algorithm>
Tianjie Xu29d55752017-09-20 17:53:46 -070035#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080036#include <string>
Tianjie Xu29d55752017-09-20 17:53:46 -070037#include <unordered_map>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070038#include <vector>
39
Tianjie Xuc21edd42016-08-05 18:00:04 -070040#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070041#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070043#include <android-base/strings.h>
Tao Baob6918c72015-05-19 17:02:16 -070044
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070045#include "device.h"
Tao Bao6cd81682018-05-03 21:53:11 -070046#include "minui/minui.h"
47#include "otautil/paths.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070048#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070049
Doug Zongker211aebc2011-10-28 15:13:10 -070050// Return the current time as a double (including fractions of a second).
51static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070052 struct timeval tv;
53 gettimeofday(&tv, nullptr);
54 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070055}
56
Tao Bao1fe1afe2018-05-01 15:56:05 -070057Menu::Menu(bool scrollable, size_t max_items, size_t max_length,
58 const std::vector<std::string>& headers, const std::vector<std::string>& items,
59 size_t initial_selection)
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070060 : scrollable_(scrollable),
61 max_display_items_(max_items),
62 max_item_length_(max_length),
Tao Baoe02a5b22018-05-02 15:46:11 -070063 text_headers_(headers),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070064 menu_start_(0),
Tao Baoe02a5b22018-05-02 15:46:11 -070065 selection_(initial_selection) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070066 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070067
68 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
Tao Bao1fe1afe2018-05-01 15:56:05 -070069 size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_);
70 for (size_t i = 0; i < items_count; ++i) {
71 text_items_.emplace_back(items[i].substr(0, max_item_length_));
Tao Baoe02a5b22018-05-02 15:46:11 -070072 }
73
74 CHECK(!text_items_.empty());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070075}
76
Tao Bao1fe1afe2018-05-01 15:56:05 -070077const std::vector<std::string>& Menu::text_headers() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070078 return text_headers_;
79}
80
81std::string Menu::TextItem(size_t index) const {
82 CHECK_LT(index, text_items_.size());
83
84 return text_items_[index];
85}
86
87size_t Menu::MenuStart() const {
88 return menu_start_;
89}
90
91size_t Menu::MenuEnd() const {
92 return std::min(ItemsCount(), menu_start_ + max_display_items_);
93}
94
95size_t Menu::ItemsCount() const {
96 return text_items_.size();
97}
98
99bool Menu::ItemsOverflow(std::string* cur_selection_str) const {
Tao Baoe02a5b22018-05-02 15:46:11 -0700100 if (!scrollable_ || ItemsCount() <= max_display_items_) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700101 return false;
102 }
103
104 *cur_selection_str =
Tao Bao1fe1afe2018-05-01 15:56:05 -0700105 android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700106 return true;
107}
108
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700109// TODO(xunchang) modify the function parameters to button up & down.
110int Menu::Select(int sel) {
111 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
112 int count = ItemsCount();
113
114 // Wraps the selection at boundary if the menu is not scrollable.
115 if (!scrollable_) {
116 if (sel < 0) {
117 selection_ = count - 1;
118 } else if (sel >= count) {
119 selection_ = 0;
120 } else {
121 selection_ = sel;
122 }
123
124 return selection_;
125 }
126
127 if (sel < 0) {
128 selection_ = 0;
129 } else if (sel >= count) {
130 selection_ = count - 1;
131 } else {
132 if (static_cast<size_t>(sel) < menu_start_) {
133 menu_start_--;
134 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
135 menu_start_++;
136 }
137 selection_ = sel;
138 }
139
140 return selection_;
141}
142
143ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
144
145ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao4521b702017-06-20 18:11:21 -0700146 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
147 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -0700148 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -0700149 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -0700150 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -0800151 progressBarType(EMPTY),
152 progressScopeStart(0),
153 progressScopeSize(0),
154 progress(0),
155 pagesIdentical(false),
156 text_cols_(0),
157 text_rows_(0),
158 text_(nullptr),
159 text_col_(0),
160 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800161 show_text(false),
162 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700163 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800164 file_viewer_text_(nullptr),
165 intro_frames(0),
166 loop_frames(0),
167 current_frame(0),
168 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800169 stage(-1),
170 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800171 locale_(""),
172 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800173 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700174
Tao Bao99b2d772017-06-23 22:47:03 -0700175GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700176 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
177 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
178 }
179 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700180}
181
Tao Bao99b2d772017-06-23 22:47:03 -0700182GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700183 switch (currentIcon) {
184 case ERASING:
185 return erasing_text;
186 case ERROR:
187 return error_text;
188 case INSTALLING_UPDATE:
189 return installing_text;
190 case NO_COMMAND:
191 return no_command_text;
192 case NONE:
193 abort();
194 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700195}
196
Mikhail Lappob49767c2017-03-23 21:44:26 +0100197int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700198 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700199}
200
201// Here's the intended layout:
202
Elliott Hughes6d089a92016-07-08 17:23:41 -0700203// | portrait large landscape large
204// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700205// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700206// icon | (200dp)
207// gap | 68dp 68dp 56dp 112dp
208// text | (14sp)
209// gap | 32dp 32dp 26dp 52dp
210// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700211// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700212
Tao Bao3250f722017-06-29 14:32:05 -0700213// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
214// 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 -0700215
Elliott Hughes6d089a92016-07-08 17:23:41 -0700216enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700217enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700218static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700219 { 32, 68, }, // PORTRAIT
220 { 32, 68, }, // PORTRAIT_LARGE
221 { 26, 56, }, // LANDSCAPE
222 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700223};
224
Tao Bao99b2d772017-06-23 22:47:03 -0700225int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700226 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700227}
228
Tao Bao99b2d772017-06-23 22:47:03 -0700229int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700230 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
231 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700232}
233
Tao Bao99b2d772017-06-23 22:47:03 -0700234int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700235 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
236 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
237 gr_get_height(progressBarFill);
Luke Song92eda4d2017-09-19 10:51:35 -0700238 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
239 return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700240}
241
Doug Zongker211aebc2011-10-28 15:13:10 -0700242// Clear the screen and draw the currently selected background icon (if any).
243// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700244void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700245 pagesIdentical = false;
246 gr_color(0, 0, 0, 255);
247 gr_clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700248 if (currentIcon != NONE) {
249 if (max_stage != -1) {
250 int stage_height = gr_get_height(stageMarkerEmpty);
251 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700252 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
253 int y = ScreenHeight() - stage_height - kMarginHeight;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700254 for (int i = 0; i < max_stage; ++i) {
255 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700256 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700257 x += stage_width;
258 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700259 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700260
261 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700262 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700263 int text_y = GetTextBaseline();
264 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700265 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700266 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700267}
268
Tao Baoea78d862017-06-28 14:52:17 -0700269// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
270// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700271void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800272 if (currentIcon != NONE) {
273 GRSurface* frame = GetCurrentFrame();
274 int frame_width = gr_get_width(frame);
275 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700276 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800277 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700278 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800279 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700280
Tao Bao736d59c2017-01-03 10:15:33 -0800281 if (progressBarType != EMPTY) {
282 int width = gr_get_width(progressBarEmpty);
283 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700284
Luke Song92eda4d2017-09-19 10:51:35 -0700285 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800286 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700287
Tao Bao736d59c2017-01-03 10:15:33 -0800288 // Erase behind the progress bar (in case this was a progress-only update)
289 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700290 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700291
Tao Bao736d59c2017-01-03 10:15:33 -0800292 if (progressBarType == DETERMINATE) {
293 float p = progressScopeStart + progress * progressScopeSize;
294 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700295
Tao Bao736d59c2017-01-03 10:15:33 -0800296 if (rtl_locale_) {
297 // Fill the progress bar from right to left.
298 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700299 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
300 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700301 }
Tao Bao736d59c2017-01-03 10:15:33 -0800302 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700303 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800304 }
305 } else {
306 // Fill the progress bar from left to right.
307 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700308 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800309 }
310 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700311 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800312 }
313 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 }
Tao Bao736d59c2017-01-03 10:15:33 -0800315 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700316}
317
Tao Bao99b2d772017-06-23 22:47:03 -0700318void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700319 switch (e) {
320 case INFO:
321 gr_color(249, 194, 0, 255);
322 break;
323 case HEADER:
324 gr_color(247, 0, 6, 255);
325 break;
326 case MENU:
327 case MENU_SEL_BG:
328 gr_color(0, 106, 157, 255);
329 break;
330 case MENU_SEL_BG_ACTIVE:
331 gr_color(0, 156, 100, 255);
332 break;
333 case MENU_SEL_FG:
334 gr_color(255, 255, 255, 255);
335 break;
336 case LOG:
337 gr_color(196, 196, 196, 255);
338 break;
339 case TEXT_FILL:
340 gr_color(0, 0, 0, 160);
341 break;
342 default:
343 gr_color(255, 255, 255, 255);
344 break;
345 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700346}
Doug Zongker211aebc2011-10-28 15:13:10 -0700347
Tianjie Xu29d55752017-09-20 17:53:46 -0700348void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
349 size_t sel) {
350 SetLocale(locales_entries[sel]);
351 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
352 "installing_security_text", "no_command_text" };
353 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
354 for (const auto& name : text_name) {
355 GRSurface* text_image = nullptr;
356 LoadLocalizedBitmap(name.c_str(), &text_image);
357 if (!text_image) {
358 Print("Failed to load %s\n", name.c_str());
359 return;
360 }
361 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
362 }
363
364 pthread_mutex_lock(&updateMutex);
365 gr_color(0, 0, 0, 255);
366 gr_clear();
367
368 int text_y = kMarginHeight;
369 int text_x = kMarginWidth;
370 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
371 // Write the header and descriptive texts.
372 SetColor(INFO);
373 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700374 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700375 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700376 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700377 // clang-format off
378 std::vector<std::string> instruction = {
379 locale_selection,
380 "Use volume up/down to switch locales and power to exit."
381 };
382 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700383 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
384
385 // Iterate through the text images and display them in order for the current locale.
386 for (const auto& p : surfaces) {
387 text_y += line_spacing;
388 SetColor(LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700389 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700390 gr_color(255, 255, 255, 255);
391 gr_texticon(text_x, text_y, p.second.get());
392 text_y += gr_get_height(p.second.get());
393 }
394 // Update the whole screen.
395 gr_flip();
396 pthread_mutex_unlock(&updateMutex);
397}
398
Tao Bao39c49182018-05-07 22:50:33 -0700399void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700400 // Load a list of locales embedded in one of the resource files.
401 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
402 if (locales_entries.empty()) {
403 Print("Failed to load locales from the resource files\n");
404 return;
405 }
Tao Bao39c49182018-05-07 22:50:33 -0700406 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700407 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 Bao452b4872018-05-09 11:52:09 -0700472 // Keep symmetrical margins based on the given offset (i.e. x).
473 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700474 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700475 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700476 size_t next_start = 0;
477 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700478 std::string sub = line.substr(next_start, text_cols + 1);
479 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700480 next_start += sub.size();
481 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700482 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700483 size_t last_space = sub.find_last_of(" \t\n");
484 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700485 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700486 sub.resize(text_cols);
487 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700488 } else {
489 sub.resize(last_space);
490 next_start += last_space + 1;
491 }
492 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700493 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700494 }
495 }
496 return offset;
497}
498
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700499void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
500 title_lines_ = lines;
501}
502
Tao Bao171b4c42017-06-19 23:10:44 -0700503// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
504// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700505void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700506 if (!show_text) {
507 draw_background_locked();
508 draw_foreground_locked();
509 return;
510 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700511
Tao Bao171b4c42017-06-19 23:10:44 -0700512 gr_color(0, 0, 0, 255);
513 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700514
Tao Bao93e46ad2018-05-02 14:57:21 -0700515 // clang-format off
Tao Bao1fe1afe2018-05-01 15:56:05 -0700516 static std::vector<std::string> REGULAR_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700517 "Use volume up/down and power.",
518 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700519 static std::vector<std::string> LONG_PRESS_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700520 "Any button cycles highlight.",
521 "Long-press activates.",
522 };
523 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700524 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
525}
526
527// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700528void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
529 const std::vector<std::string>& help_message) {
Tao Bao4521b702017-06-20 18:11:21 -0700530 int y = kMarginHeight;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700531 if (menu_) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700532 static constexpr int kMenuIndent = 4;
533 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700534
Tao Bao171b4c42017-06-19 23:10:44 -0700535 SetColor(INFO);
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700536
537 for (size_t i = 0; i < title_lines_.size(); i++) {
538 y += DrawTextLine(x, y, title_lines_[i], i == 0);
Doug Zongker211aebc2011-10-28 15:13:10 -0700539 }
Tao Bao171b4c42017-06-19 23:10:44 -0700540
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700541 y += DrawTextLines(x, y, help_message);
542
543 // Draw menu header.
Tao Bao171b4c42017-06-19 23:10:44 -0700544 SetColor(HEADER);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700545 if (!menu_->scrollable()) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700546 y += DrawWrappedTextLines(x, y, menu_->text_headers());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700547 } else {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700548 y += DrawTextLines(x, y, menu_->text_headers());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700549 // Show the current menu item number in relation to total number if items don't fit on the
550 // screen.
551 std::string cur_selection_str;
552 if (menu_->ItemsOverflow(&cur_selection_str)) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700553 y += DrawTextLine(x, y, cur_selection_str, true);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700554 }
555 }
Tao Bao171b4c42017-06-19 23:10:44 -0700556
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700557 // Draw menu items.
Tao Bao171b4c42017-06-19 23:10:44 -0700558 SetColor(MENU);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700559 // Do not draw the horizontal rule for wear devices.
560 if (!menu_->scrollable()) {
561 y += DrawHorizontalRule(y) + 4;
562 }
563 for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) {
564 bool bold = false;
565 if (i == static_cast<size_t>(menu_->selection())) {
Tao Bao171b4c42017-06-19 23:10:44 -0700566 // Draw the highlight bar.
567 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700568
569 int bar_height = char_height_ + 4;
570 DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height);
571
Tao Bao171b4c42017-06-19 23:10:44 -0700572 // Bold white text for the selected item.
573 SetColor(MENU_SEL_FG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700574 bold = true;
Tao Bao171b4c42017-06-19 23:10:44 -0700575 }
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700576
Tao Bao93e46ad2018-05-02 14:57:21 -0700577 y += DrawTextLine(x, y, menu_->TextItem(i), bold);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700578
579 SetColor(MENU);
Tao Bao171b4c42017-06-19 23:10:44 -0700580 }
Tao Baoea78d862017-06-28 14:52:17 -0700581 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700582 }
583
584 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
585 // we've displayed the entire text buffer.
586 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700587 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700588 size_t count = 0;
Luke Song92eda4d2017-09-19 10:51:35 -0700589 for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700590 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700591 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700592 --row;
593 if (row < 0) row = text_rows_ - 1;
594 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700595}
596
597// Redraw everything on the screen and flip the screen (make it visible).
598// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700599void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700600 draw_screen_locked();
601 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700602}
603
604// Updates only the progress bar, if possible, otherwise redraws the screen.
605// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700606void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700607 if (show_text || !pagesIdentical) {
608 draw_screen_locked(); // Must redraw the whole screen
609 pagesIdentical = true;
610 } else {
611 draw_foreground_locked(); // Draw only the progress bar and overlays
612 }
613 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700614}
615
616// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700617void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700618 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
619 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700620}
621
Elliott Hughes985022a2015-04-13 13:04:32 -0700622void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700623 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700624 while (true) {
625 double start = now();
626 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700627
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700628 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700629
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700630 // update the installation animation, if active
631 // skip this if we have a text overlay (too expensive to update)
632 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
633 if (!intro_done) {
634 if (current_frame == intro_frames - 1) {
635 intro_done = true;
636 current_frame = 0;
637 } else {
638 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700639 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700640 } else {
641 current_frame = (current_frame + 1) % loop_frames;
642 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700643
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700644 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700645 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700646
647 // move the progress bar forward on timed intervals, if configured
648 int duration = progressScopeDuration;
649 if (progressBarType == DETERMINATE && duration > 0) {
650 double elapsed = now() - progressScopeTime;
651 float p = 1.0 * elapsed / duration;
652 if (p > 1.0) p = 1.0;
653 if (p > progress) {
654 progress = p;
655 redraw = true;
656 }
657 }
658
659 if (redraw) update_progress_locked();
660
661 pthread_mutex_unlock(&updateMutex);
662 double end = now();
663 // minimum of 20ms delay between frames
664 double delay = interval - (end - start);
665 if (delay < 0.02) delay = 0.02;
666 usleep(static_cast<useconds_t>(delay * 1000000));
667 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700668}
669
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700670void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700671 int result = res_create_display_surface(filename, surface);
672 if (result < 0) {
673 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
674 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800675}
676
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700677void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800678 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
679 if (result < 0) {
680 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
681 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700682}
683
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700684static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700685 char** result = new char*[rows];
686 for (size_t i = 0; i < rows; ++i) {
687 result[i] = new char[cols];
688 memset(result[i], 0, cols);
689 }
690 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700691}
692
Tianjie Xu35926c42016-04-28 18:06:26 -0700693// Choose the right background string to display during update.
694void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700695 if (security_update) {
696 LoadLocalizedBitmap("installing_security_text", &installing_text);
697 } else {
698 LoadLocalizedBitmap("installing_text", &installing_text);
699 }
700 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700701}
702
Sen Jiangd5304492016-12-09 16:20:49 -0800703bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700704 if (gr_init() < 0) {
705 return false;
706 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700707
Tao Bao171b4c42017-06-19 23:10:44 -0700708 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Luke Song92eda4d2017-09-19 10:51:35 -0700709 text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
710 text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700711 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700712}
713
Tao Bao736d59c2017-01-03 10:15:33 -0800714bool ScreenRecoveryUI::Init(const std::string& locale) {
715 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800716
Tao Bao736d59c2017-01-03 10:15:33 -0800717 if (!InitTextParams()) {
718 return false;
719 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700720
Tao Bao736d59c2017-01-03 10:15:33 -0800721 // Are we portrait or landscape?
722 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
723 // Are we the large variant of our base layout?
724 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700725
Tao Bao736d59c2017-01-03 10:15:33 -0800726 text_ = Alloc2d(text_rows_, text_cols_ + 1);
727 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800728
Tao Bao736d59c2017-01-03 10:15:33 -0800729 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700730
Tao Baoefb49ad2017-01-31 23:03:10 -0800731 // Set up the locale info.
732 SetLocale(locale);
733
Tao Bao736d59c2017-01-03 10:15:33 -0800734 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700735
Tao Bao736d59c2017-01-03 10:15:33 -0800736 LoadBitmap("progress_empty", &progressBarEmpty);
737 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700738
Tao Bao736d59c2017-01-03 10:15:33 -0800739 LoadBitmap("stage_empty", &stageMarkerEmpty);
740 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700741
Tao Bao736d59c2017-01-03 10:15:33 -0800742 // Background text for "installing_update" could be "installing update"
743 // or "installing security update". It will be set after UI init according
744 // to commands in BCB.
745 installing_text = nullptr;
746 LoadLocalizedBitmap("erasing_text", &erasing_text);
747 LoadLocalizedBitmap("no_command_text", &no_command_text);
748 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700749
Tao Bao736d59c2017-01-03 10:15:33 -0800750 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700751
Tao Bao736d59c2017-01-03 10:15:33 -0800752 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800753
Tao Bao736d59c2017-01-03 10:15:33 -0800754 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700755}
756
Tao Bao551d2c32018-05-09 20:53:13 -0700757std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700758 return locale_;
759}
760
Elliott Hughes498cda62016-04-14 16:49:04 -0700761void ScreenRecoveryUI::LoadAnimation() {
Tao Bao6cd81682018-05-03 21:53:11 -0700762 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
763 closedir);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700764 dirent* de;
765 std::vector<std::string> intro_frame_names;
766 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700767
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700768 while ((de = readdir(dir.get())) != nullptr) {
769 int value, num_chars;
770 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
771 intro_frame_names.emplace_back(de->d_name, num_chars);
772 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
773 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700774 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700775 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700776
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700777 intro_frames = intro_frame_names.size();
778 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700779
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700780 // It's okay to not have an intro.
781 if (intro_frames == 0) intro_done = true;
782 // But you must have an animation.
783 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700784
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700785 std::sort(intro_frame_names.begin(), intro_frame_names.end());
786 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700787
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700788 introFrames = new GRSurface*[intro_frames];
789 for (size_t i = 0; i < intro_frames; i++) {
790 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
791 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700792
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700793 loopFrames = new GRSurface*[loop_frames];
794 for (size_t i = 0; i < loop_frames; i++) {
795 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
796 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700797}
798
Elliott Hughes8de52072015-04-08 20:06:50 -0700799void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700800 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700801
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700802 currentIcon = icon;
803 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700804
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700805 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700806}
807
Elliott Hughes8de52072015-04-08 20:06:50 -0700808void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700809 pthread_mutex_lock(&updateMutex);
810 if (progressBarType != type) {
811 progressBarType = type;
812 }
813 progressScopeStart = 0;
814 progressScopeSize = 0;
815 progress = 0;
816 update_progress_locked();
817 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700818}
819
Elliott Hughes8de52072015-04-08 20:06:50 -0700820void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700821 pthread_mutex_lock(&updateMutex);
822 progressBarType = DETERMINATE;
823 progressScopeStart += progressScopeSize;
824 progressScopeSize = portion;
825 progressScopeTime = now();
826 progressScopeDuration = seconds;
827 progress = 0;
828 update_progress_locked();
829 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700830}
831
Elliott Hughes8de52072015-04-08 20:06:50 -0700832void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700833 pthread_mutex_lock(&updateMutex);
834 if (fraction < 0.0) fraction = 0.0;
835 if (fraction > 1.0) fraction = 1.0;
836 if (progressBarType == DETERMINATE && fraction > progress) {
837 // Skip updates that aren't visibly different.
838 int width = gr_get_width(progressBarEmpty);
839 float scale = width * progressScopeSize;
840 if ((int)(progress * scale) != (int)(fraction * scale)) {
841 progress = fraction;
842 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700843 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700844 }
845 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700846}
847
Doug Zongkerc87bab12013-11-25 13:53:25 -0800848void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700849 pthread_mutex_lock(&updateMutex);
850 stage = current;
851 max_stage = max;
852 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800853}
854
Tao Baob6918c72015-05-19 17:02:16 -0700855void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700856 std::string str;
857 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700858
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700859 if (copy_to_stdout) {
860 fputs(str.c_str(), stdout);
861 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700862
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700863 pthread_mutex_lock(&updateMutex);
864 if (text_rows_ > 0 && text_cols_ > 0) {
865 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
866 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700867 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700868 text_col_ = 0;
869 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700870 }
871 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700872 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700873 text_[text_row_][text_col_] = '\0';
874 update_screen_locked();
875 }
876 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700877}
878
Tao Baob6918c72015-05-19 17:02:16 -0700879void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700880 va_list ap;
881 va_start(ap, fmt);
882 PrintV(fmt, true, ap);
883 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700884}
885
886void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700887 va_list ap;
888 va_start(ap, fmt);
889 PrintV(fmt, false, ap);
890 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700891}
892
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700893void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700894 pthread_mutex_lock(&updateMutex);
895 if (ch != '\n') text_[text_row_][text_col_++] = ch;
896 if (ch == '\n' || text_col_ >= text_cols_) {
897 text_col_ = 0;
898 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700899 }
900 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700901}
902
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700903void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700904 pthread_mutex_lock(&updateMutex);
905 text_col_ = 0;
906 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700907 for (size_t i = 0; i < text_rows_; ++i) {
908 memset(text_[i], 0, text_cols_ + 1);
909 }
910 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700911}
912
913void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700914 std::vector<off_t> offsets;
915 offsets.push_back(ftello(fp));
916 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700917
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700918 struct stat sb;
919 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700920
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700921 bool show_prompt = false;
922 while (true) {
923 if (show_prompt) {
924 PrintOnScreenOnly("--(%d%% of %d bytes)--",
925 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
926 static_cast<int>(sb.st_size));
927 Redraw();
928 while (show_prompt) {
929 show_prompt = false;
930 int key = WaitKey();
931 if (key == KEY_POWER || key == KEY_ENTER) {
932 return;
933 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
934 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700935 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700936 } else {
937 offsets.pop_back();
938 fseek(fp, offsets.back(), SEEK_SET);
939 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700940 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700941 if (feof(fp)) {
942 return;
943 }
944 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700945 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700946 }
947 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700948 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700949
950 int ch = getc(fp);
951 if (ch == EOF) {
952 while (text_row_ < text_rows_ - 1) PutChar('\n');
953 show_prompt = true;
954 } else {
955 PutChar(ch);
956 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
957 show_prompt = true;
958 }
959 }
960 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700961}
962
Tao Bao1d156b92018-05-02 12:43:18 -0700963void ScreenRecoveryUI::ShowFile(const std::string& filename) {
964 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
965 if (!fp) {
966 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700967 return;
968 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700969
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700970 char** old_text = text_;
971 size_t old_text_col = text_col_;
972 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700973
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700974 // Swap in the alternate screen and clear it.
975 text_ = file_viewer_text_;
976 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700977
Tao Bao1d156b92018-05-02 12:43:18 -0700978 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -0700979
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700980 text_ = old_text;
981 text_col_ = old_text_col;
982 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700983}
984
Tao Bao1fe1afe2018-05-01 15:56:05 -0700985void ScreenRecoveryUI::StartMenu(const std::vector<std::string>& headers,
986 const std::vector<std::string>& items, size_t initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700987 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700988 if (text_rows_ > 0 && text_cols_ > 1) {
Tao Baoe02a5b22018-05-02 15:46:11 -0700989 menu_ = std::make_unique<Menu>(scrollable_menu_, text_rows_, text_cols_ - 1, headers, items,
990 initial_selection);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700991 update_screen_locked();
992 }
993 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700994}
995
996int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700997 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700998 if (menu_) {
999 int old_sel = menu_->selection();
1000 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001001
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001002 if (sel != old_sel) {
1003 update_screen_locked();
1004 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001005 }
1006 pthread_mutex_unlock(&updateMutex);
1007 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001008}
1009
1010void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001011 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001012 if (menu_) {
1013 menu_.reset();
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001014 update_screen_locked();
1015 }
1016 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001017}
1018
Tao Bao1fe1afe2018-05-01 15:56:05 -07001019size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1020 const std::vector<std::string>& items, size_t initial_selection,
1021 bool menu_only,
1022 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001023 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1024 FlushKeys();
1025
1026 StartMenu(headers, items, initial_selection);
1027
1028 int selected = initial_selection;
1029 int chosen_item = -1;
1030 while (chosen_item < 0) {
1031 int key = WaitKey();
1032 if (key == -1) { // WaitKey() timed out.
1033 if (WasTextEverVisible()) {
1034 continue;
1035 } else {
1036 LOG(INFO) << "Timed out waiting for key input; rebooting.";
1037 EndMenu();
Tao Bao1fe1afe2018-05-01 15:56:05 -07001038 return static_cast<size_t>(-1);
Tao Bao3aec6962018-04-20 09:24:58 -07001039 }
1040 }
1041
1042 bool visible = IsTextVisible();
1043 int action = key_handler(key, visible);
1044 if (action < 0) {
1045 switch (action) {
1046 case Device::kHighlightUp:
1047 selected = SelectMenu(--selected);
1048 break;
1049 case Device::kHighlightDown:
1050 selected = SelectMenu(++selected);
1051 break;
1052 case Device::kInvokeItem:
1053 chosen_item = selected;
1054 break;
1055 case Device::kNoAction:
1056 break;
1057 }
1058 } else if (!menu_only) {
1059 chosen_item = action;
1060 }
1061 }
1062
1063 EndMenu();
1064 return chosen_item;
1065}
1066
Elliott Hughes8de52072015-04-08 20:06:50 -07001067bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001068 pthread_mutex_lock(&updateMutex);
1069 int visible = show_text;
1070 pthread_mutex_unlock(&updateMutex);
1071 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001072}
1073
Elliott Hughes8de52072015-04-08 20:06:50 -07001074bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001075 pthread_mutex_lock(&updateMutex);
1076 int ever_visible = show_text_ever;
1077 pthread_mutex_unlock(&updateMutex);
1078 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001079}
1080
Elliott Hughes8de52072015-04-08 20:06:50 -07001081void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001082 pthread_mutex_lock(&updateMutex);
1083 show_text = visible;
1084 if (show_text) show_text_ever = true;
1085 update_screen_locked();
1086 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001087}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001088
Elliott Hughes8de52072015-04-08 20:06:50 -07001089void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001090 pthread_mutex_lock(&updateMutex);
1091 update_screen_locked();
1092 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001093}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001094
1095void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001096 // Redraw so that if we're in the menu, the highlight
1097 // will change color to indicate a successful long press.
1098 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001099}
Tao Baoefb49ad2017-01-31 23:03:10 -08001100
1101void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1102 locale_ = new_locale;
1103 rtl_locale_ = false;
1104
1105 if (!new_locale.empty()) {
Tao Bao347a6592018-05-08 15:58:29 -07001106 size_t separator = new_locale.find('-');
1107 // lang has the language prefix prior to the separator, or full string if none exists.
1108 std::string lang = new_locale.substr(0, separator);
Tao Baoefb49ad2017-01-31 23:03:10 -08001109
1110 // A bit cheesy: keep an explicit list of supported RTL languages.
1111 if (lang == "ar" || // Arabic
1112 lang == "fa" || // Persian (Farsi)
1113 lang == "he" || // Hebrew (new language code)
1114 lang == "iw" || // Hebrew (old language code)
1115 lang == "ur") { // Urdu
1116 rtl_locale_ = true;
1117 }
1118 }
1119}