blob: 56ca48ea883f82aa0324dbaaf50d50ec95673917 [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
Tao Baoe02a5b22018-05-02 15:46:11 -070055Menu::Menu(bool scrollable, size_t max_items, size_t max_length, const char* const* headers,
56 const char* const* items, int initial_selection)
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070057 : scrollable_(scrollable),
58 max_display_items_(max_items),
59 max_item_length_(max_length),
Tao Baoe02a5b22018-05-02 15:46:11 -070060 text_headers_(headers),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070061 menu_start_(0),
Tao Baoe02a5b22018-05-02 15:46:11 -070062 selection_(initial_selection) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070063 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070064
65 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
66 size_t max_items_count = scrollable_ ? std::numeric_limits<int>::max() : max_display_items_;
67 for (size_t i = 0; i < max_items_count && items[i] != nullptr; ++i) {
68 text_items_.emplace_back(items[i], strnlen(items[i], max_item_length_));
69 }
70
71 CHECK(!text_items_.empty());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070072}
73
74const char* const* Menu::text_headers() const {
75 return text_headers_;
76}
77
78std::string Menu::TextItem(size_t index) const {
79 CHECK_LT(index, text_items_.size());
80
81 return text_items_[index];
82}
83
84size_t Menu::MenuStart() const {
85 return menu_start_;
86}
87
88size_t Menu::MenuEnd() const {
89 return std::min(ItemsCount(), menu_start_ + max_display_items_);
90}
91
92size_t Menu::ItemsCount() const {
93 return text_items_.size();
94}
95
96bool Menu::ItemsOverflow(std::string* cur_selection_str) const {
Tao Baoe02a5b22018-05-02 15:46:11 -070097 if (!scrollable_ || ItemsCount() <= max_display_items_) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070098 return false;
99 }
100
101 *cur_selection_str =
102 android::base::StringPrintf("Current item: %d/%zu", selection_ + 1, ItemsCount());
103 return true;
104}
105
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700106// TODO(xunchang) modify the function parameters to button up & down.
107int Menu::Select(int sel) {
108 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
109 int count = ItemsCount();
110
111 // Wraps the selection at boundary if the menu is not scrollable.
112 if (!scrollable_) {
113 if (sel < 0) {
114 selection_ = count - 1;
115 } else if (sel >= count) {
116 selection_ = 0;
117 } else {
118 selection_ = sel;
119 }
120
121 return selection_;
122 }
123
124 if (sel < 0) {
125 selection_ = 0;
126 } else if (sel >= count) {
127 selection_ = count - 1;
128 } else {
129 if (static_cast<size_t>(sel) < menu_start_) {
130 menu_start_--;
131 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
132 menu_start_++;
133 }
134 selection_ = sel;
135 }
136
137 return selection_;
138}
139
140ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
141
142ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao4521b702017-06-20 18:11:21 -0700143 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
144 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -0700145 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -0700146 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -0700147 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -0800148 progressBarType(EMPTY),
149 progressScopeStart(0),
150 progressScopeSize(0),
151 progress(0),
152 pagesIdentical(false),
153 text_cols_(0),
154 text_rows_(0),
155 text_(nullptr),
156 text_col_(0),
157 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800158 show_text(false),
159 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700160 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800161 file_viewer_text_(nullptr),
162 intro_frames(0),
163 loop_frames(0),
164 current_frame(0),
165 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800166 stage(-1),
167 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800168 locale_(""),
169 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800170 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700171
Tao Bao99b2d772017-06-23 22:47:03 -0700172GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700173 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
174 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
175 }
176 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700177}
178
Tao Bao99b2d772017-06-23 22:47:03 -0700179GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700180 switch (currentIcon) {
181 case ERASING:
182 return erasing_text;
183 case ERROR:
184 return error_text;
185 case INSTALLING_UPDATE:
186 return installing_text;
187 case NO_COMMAND:
188 return no_command_text;
189 case NONE:
190 abort();
191 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700192}
193
Mikhail Lappob49767c2017-03-23 21:44:26 +0100194int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700195 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700196}
197
198// Here's the intended layout:
199
Elliott Hughes6d089a92016-07-08 17:23:41 -0700200// | portrait large landscape large
201// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700202// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700203// icon | (200dp)
204// gap | 68dp 68dp 56dp 112dp
205// text | (14sp)
206// gap | 32dp 32dp 26dp 52dp
207// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700208// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700209
Tao Bao3250f722017-06-29 14:32:05 -0700210// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
211// 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 -0700212
Elliott Hughes6d089a92016-07-08 17:23:41 -0700213enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700214enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700215static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700216 { 32, 68, }, // PORTRAIT
217 { 32, 68, }, // PORTRAIT_LARGE
218 { 26, 56, }, // LANDSCAPE
219 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700220};
221
Tao Bao99b2d772017-06-23 22:47:03 -0700222int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700223 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700224}
225
Tao Bao99b2d772017-06-23 22:47:03 -0700226int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700227 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
228 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700229}
230
Tao Bao99b2d772017-06-23 22:47:03 -0700231int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700232 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
233 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
234 gr_get_height(progressBarFill);
Luke Song92eda4d2017-09-19 10:51:35 -0700235 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
236 return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700237}
238
Doug Zongker211aebc2011-10-28 15:13:10 -0700239// Clear the screen and draw the currently selected background icon (if any).
240// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700241void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700242 pagesIdentical = false;
243 gr_color(0, 0, 0, 255);
244 gr_clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700245 if (currentIcon != NONE) {
246 if (max_stage != -1) {
247 int stage_height = gr_get_height(stageMarkerEmpty);
248 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700249 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
250 int y = ScreenHeight() - stage_height - kMarginHeight;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700251 for (int i = 0; i < max_stage; ++i) {
252 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700253 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700254 x += stage_width;
255 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700256 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700257
258 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700259 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700260 int text_y = GetTextBaseline();
261 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700262 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700263 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700264}
265
Tao Baoea78d862017-06-28 14:52:17 -0700266// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
267// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700268void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800269 if (currentIcon != NONE) {
270 GRSurface* frame = GetCurrentFrame();
271 int frame_width = gr_get_width(frame);
272 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700273 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800274 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700275 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800276 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700277
Tao Bao736d59c2017-01-03 10:15:33 -0800278 if (progressBarType != EMPTY) {
279 int width = gr_get_width(progressBarEmpty);
280 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700281
Luke Song92eda4d2017-09-19 10:51:35 -0700282 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800283 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700284
Tao Bao736d59c2017-01-03 10:15:33 -0800285 // Erase behind the progress bar (in case this was a progress-only update)
286 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700287 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700288
Tao Bao736d59c2017-01-03 10:15:33 -0800289 if (progressBarType == DETERMINATE) {
290 float p = progressScopeStart + progress * progressScopeSize;
291 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700292
Tao Bao736d59c2017-01-03 10:15:33 -0800293 if (rtl_locale_) {
294 // Fill the progress bar from right to left.
295 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700296 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
297 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700298 }
Tao Bao736d59c2017-01-03 10:15:33 -0800299 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700300 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800301 }
302 } else {
303 // Fill the progress bar from left to right.
304 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700305 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800306 }
307 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700308 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800309 }
310 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700311 }
Tao Bao736d59c2017-01-03 10:15:33 -0800312 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700313}
314
Tao Bao99b2d772017-06-23 22:47:03 -0700315void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700316 switch (e) {
317 case INFO:
318 gr_color(249, 194, 0, 255);
319 break;
320 case HEADER:
321 gr_color(247, 0, 6, 255);
322 break;
323 case MENU:
324 case MENU_SEL_BG:
325 gr_color(0, 106, 157, 255);
326 break;
327 case MENU_SEL_BG_ACTIVE:
328 gr_color(0, 156, 100, 255);
329 break;
330 case MENU_SEL_FG:
331 gr_color(255, 255, 255, 255);
332 break;
333 case LOG:
334 gr_color(196, 196, 196, 255);
335 break;
336 case TEXT_FILL:
337 gr_color(0, 0, 0, 160);
338 break;
339 default:
340 gr_color(255, 255, 255, 255);
341 break;
342 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700343}
Doug Zongker211aebc2011-10-28 15:13:10 -0700344
Tianjie Xu29d55752017-09-20 17:53:46 -0700345void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
346 size_t sel) {
347 SetLocale(locales_entries[sel]);
348 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
349 "installing_security_text", "no_command_text" };
350 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
351 for (const auto& name : text_name) {
352 GRSurface* text_image = nullptr;
353 LoadLocalizedBitmap(name.c_str(), &text_image);
354 if (!text_image) {
355 Print("Failed to load %s\n", name.c_str());
356 return;
357 }
358 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
359 }
360
361 pthread_mutex_lock(&updateMutex);
362 gr_color(0, 0, 0, 255);
363 gr_clear();
364
365 int text_y = kMarginHeight;
366 int text_x = kMarginWidth;
367 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
368 // Write the header and descriptive texts.
369 SetColor(INFO);
370 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700371 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700372 std::string locale_selection = android::base::StringPrintf(
373 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700374 // clang-format off
375 std::vector<std::string> instruction = {
376 locale_selection,
377 "Use volume up/down to switch locales and power to exit."
378 };
379 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700380 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
381
382 // Iterate through the text images and display them in order for the current locale.
383 for (const auto& p : surfaces) {
384 text_y += line_spacing;
385 SetColor(LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700386 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700387 gr_color(255, 255, 255, 255);
388 gr_texticon(text_x, text_y, p.second.get());
389 text_y += gr_get_height(p.second.get());
390 }
391 // Update the whole screen.
392 gr_flip();
393 pthread_mutex_unlock(&updateMutex);
394}
395
396void ScreenRecoveryUI::CheckBackgroundTextImages(const std::string& saved_locale) {
397 // Load a list of locales embedded in one of the resource files.
398 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
399 if (locales_entries.empty()) {
400 Print("Failed to load locales from the resource files\n");
401 return;
402 }
403 size_t selected = 0;
404 SelectAndShowBackgroundText(locales_entries, selected);
405
406 FlushKeys();
407 while (true) {
408 int key = WaitKey();
409 if (key == KEY_POWER || key == KEY_ENTER) {
410 break;
411 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
412 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
413 SelectAndShowBackgroundText(locales_entries, selected);
414 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
415 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
416 SelectAndShowBackgroundText(locales_entries, selected);
417 }
418 }
419
420 SetLocale(saved_locale);
421}
422
Luke Song92eda4d2017-09-19 10:51:35 -0700423int ScreenRecoveryUI::ScreenWidth() const {
424 return gr_fb_width();
425}
426
427int ScreenRecoveryUI::ScreenHeight() const {
428 return gr_fb_height();
429}
430
431void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
432 int dy) const {
433 gr_blit(surface, sx, sy, w, h, dx, dy);
434}
435
Tao Baoea78d862017-06-28 14:52:17 -0700436int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700437 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700438 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700439}
440
Luke Songe2bd8762017-06-12 16:08:33 -0700441void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700442 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700443}
444
Luke Song92eda4d2017-09-19 10:51:35 -0700445void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
446 gr_fill(x, y, w, h);
447}
448
449void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
450 gr_texticon(x, y, surface);
451}
452
Tao Bao93e46ad2018-05-02 14:57:21 -0700453int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
454 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700455 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700456}
457
Tao Bao93e46ad2018-05-02 14:57:21 -0700458int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700459 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700460 for (const auto& line : lines) {
461 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700462 }
Tao Baoea78d862017-06-28 14:52:17 -0700463 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700464}
465
Tao Bao93e46ad2018-05-02 14:57:21 -0700466int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
467 const std::vector<std::string>& lines) const {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700468 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700469 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700470 size_t next_start = 0;
471 while (next_start < line.size()) {
472 std::string sub = line.substr(next_start, text_cols_ + 1);
473 if (sub.size() <= text_cols_) {
474 next_start += sub.size();
475 } else {
476 // Line too long and must be wrapped to text_cols_ columns.
477 size_t last_space = sub.find_last_of(" \t\n");
478 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700479 // No space found, just draw as much as we can.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700480 sub.resize(text_cols_);
481 next_start += text_cols_;
482 } else {
483 sub.resize(last_space);
484 next_start += last_space + 1;
485 }
486 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700487 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700488 }
489 }
490 return offset;
491}
492
Tao Bao171b4c42017-06-19 23:10:44 -0700493// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
494// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700495void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700496 if (!show_text) {
497 draw_background_locked();
498 draw_foreground_locked();
499 return;
500 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700501
Tao Bao171b4c42017-06-19 23:10:44 -0700502 gr_color(0, 0, 0, 255);
503 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700504
Tao Bao93e46ad2018-05-02 14:57:21 -0700505 // clang-format off
506 static std::vector<std::string> REGULAR_HELP = {
507 "Use volume up/down and power.",
508 };
509 static std::vector<std::string> LONG_PRESS_HELP = {
510 "Any button cycles highlight.",
511 "Long-press activates.",
512 };
513 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700514 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
515}
516
517// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700518void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
519 const std::vector<std::string>& help_message) {
Tao Bao4521b702017-06-20 18:11:21 -0700520 int y = kMarginHeight;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700521 if (menu_) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700522 static constexpr int kMenuIndent = 4;
523 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700524
Tao Bao171b4c42017-06-19 23:10:44 -0700525 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700526 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700527 std::string recovery_fingerprint =
528 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700529 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700530 y += DrawTextLine(x, y, chunk, false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700531 }
Tao Bao171b4c42017-06-19 23:10:44 -0700532
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700533 y += DrawTextLines(x, y, help_message);
534
Tao Bao93e46ad2018-05-02 14:57:21 -0700535 auto convert_to_vector = [](const char* const* items) -> std::vector<std::string> {
536 if (items == nullptr) return {};
537
538 std::vector<std::string> result;
539 for (size_t i = 0; items[i] != nullptr; ++i) {
540 result.emplace_back(items[i]);
541 }
542 return result;
543 };
544
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700545 // Draw menu header.
Tao Bao171b4c42017-06-19 23:10:44 -0700546 SetColor(HEADER);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700547 if (!menu_->scrollable()) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700548 y += DrawWrappedTextLines(x, y, convert_to_vector(menu_->text_headers()));
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700549 } else {
Tao Bao93e46ad2018-05-02 14:57:21 -0700550 y += DrawTextLines(x, y, convert_to_vector(menu_->text_headers()));
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700551 // Show the current menu item number in relation to total number if items don't fit on the
552 // screen.
553 std::string cur_selection_str;
554 if (menu_->ItemsOverflow(&cur_selection_str)) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700555 y += DrawTextLine(x, y, cur_selection_str, true);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700556 }
557 }
Tao Bao171b4c42017-06-19 23:10:44 -0700558
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700559 // Draw menu items.
Tao Bao171b4c42017-06-19 23:10:44 -0700560 SetColor(MENU);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700561 // Do not draw the horizontal rule for wear devices.
562 if (!menu_->scrollable()) {
563 y += DrawHorizontalRule(y) + 4;
564 }
565 for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) {
566 bool bold = false;
567 if (i == static_cast<size_t>(menu_->selection())) {
Tao Bao171b4c42017-06-19 23:10:44 -0700568 // Draw the highlight bar.
569 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700570
571 int bar_height = char_height_ + 4;
572 DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height);
573
Tao Bao171b4c42017-06-19 23:10:44 -0700574 // Bold white text for the selected item.
575 SetColor(MENU_SEL_FG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700576 bold = true;
Tao Bao171b4c42017-06-19 23:10:44 -0700577 }
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700578
Tao Bao93e46ad2018-05-02 14:57:21 -0700579 y += DrawTextLine(x, y, menu_->TextItem(i), bold);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700580
581 SetColor(MENU);
Tao Bao171b4c42017-06-19 23:10:44 -0700582 }
Tao Baoea78d862017-06-28 14:52:17 -0700583 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700584 }
585
586 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
587 // we've displayed the entire text buffer.
588 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700589 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700590 size_t count = 0;
Luke Song92eda4d2017-09-19 10:51:35 -0700591 for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700592 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700593 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700594 --row;
595 if (row < 0) row = text_rows_ - 1;
596 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700597}
598
599// Redraw everything on the screen and flip the screen (make it visible).
600// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700601void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700602 draw_screen_locked();
603 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700604}
605
606// Updates only the progress bar, if possible, otherwise redraws the screen.
607// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700608void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700609 if (show_text || !pagesIdentical) {
610 draw_screen_locked(); // Must redraw the whole screen
611 pagesIdentical = true;
612 } else {
613 draw_foreground_locked(); // Draw only the progress bar and overlays
614 }
615 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700616}
617
618// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700619void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700620 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
621 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700622}
623
Elliott Hughes985022a2015-04-13 13:04:32 -0700624void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700625 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700626 while (true) {
627 double start = now();
628 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700629
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700630 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700631
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700632 // update the installation animation, if active
633 // skip this if we have a text overlay (too expensive to update)
634 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
635 if (!intro_done) {
636 if (current_frame == intro_frames - 1) {
637 intro_done = true;
638 current_frame = 0;
639 } else {
640 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700641 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700642 } else {
643 current_frame = (current_frame + 1) % loop_frames;
644 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700645
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700646 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700647 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700648
649 // move the progress bar forward on timed intervals, if configured
650 int duration = progressScopeDuration;
651 if (progressBarType == DETERMINATE && duration > 0) {
652 double elapsed = now() - progressScopeTime;
653 float p = 1.0 * elapsed / duration;
654 if (p > 1.0) p = 1.0;
655 if (p > progress) {
656 progress = p;
657 redraw = true;
658 }
659 }
660
661 if (redraw) update_progress_locked();
662
663 pthread_mutex_unlock(&updateMutex);
664 double end = now();
665 // minimum of 20ms delay between frames
666 double delay = interval - (end - start);
667 if (delay < 0.02) delay = 0.02;
668 usleep(static_cast<useconds_t>(delay * 1000000));
669 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700670}
671
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700672void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700673 int result = res_create_display_surface(filename, surface);
674 if (result < 0) {
675 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
676 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800677}
678
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700679void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800680 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
681 if (result < 0) {
682 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
683 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700684}
685
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700686static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700687 char** result = new char*[rows];
688 for (size_t i = 0; i < rows; ++i) {
689 result[i] = new char[cols];
690 memset(result[i], 0, cols);
691 }
692 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700693}
694
Tianjie Xu35926c42016-04-28 18:06:26 -0700695// Choose the right background string to display during update.
696void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700697 if (security_update) {
698 LoadLocalizedBitmap("installing_security_text", &installing_text);
699 } else {
700 LoadLocalizedBitmap("installing_text", &installing_text);
701 }
702 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700703}
704
Sen Jiangd5304492016-12-09 16:20:49 -0800705bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700706 if (gr_init() < 0) {
707 return false;
708 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700709
Tao Bao171b4c42017-06-19 23:10:44 -0700710 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Luke Song92eda4d2017-09-19 10:51:35 -0700711 text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
712 text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700713 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700714}
715
Tao Bao736d59c2017-01-03 10:15:33 -0800716bool ScreenRecoveryUI::Init(const std::string& locale) {
717 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800718
Tao Bao736d59c2017-01-03 10:15:33 -0800719 if (!InitTextParams()) {
720 return false;
721 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700722
Tao Bao736d59c2017-01-03 10:15:33 -0800723 // Are we portrait or landscape?
724 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
725 // Are we the large variant of our base layout?
726 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700727
Tao Bao736d59c2017-01-03 10:15:33 -0800728 text_ = Alloc2d(text_rows_, text_cols_ + 1);
729 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800730
Tao Bao736d59c2017-01-03 10:15:33 -0800731 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700732
Tao Baoefb49ad2017-01-31 23:03:10 -0800733 // Set up the locale info.
734 SetLocale(locale);
735
Tao Bao736d59c2017-01-03 10:15:33 -0800736 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700737
Tao Bao736d59c2017-01-03 10:15:33 -0800738 LoadBitmap("progress_empty", &progressBarEmpty);
739 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700740
Tao Bao736d59c2017-01-03 10:15:33 -0800741 LoadBitmap("stage_empty", &stageMarkerEmpty);
742 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700743
Tao Bao736d59c2017-01-03 10:15:33 -0800744 // Background text for "installing_update" could be "installing update"
745 // or "installing security update". It will be set after UI init according
746 // to commands in BCB.
747 installing_text = nullptr;
748 LoadLocalizedBitmap("erasing_text", &erasing_text);
749 LoadLocalizedBitmap("no_command_text", &no_command_text);
750 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700751
Tao Bao736d59c2017-01-03 10:15:33 -0800752 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700753
Tao Bao736d59c2017-01-03 10:15:33 -0800754 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800755
Tao Bao736d59c2017-01-03 10:15:33 -0800756 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700757}
758
Elliott Hughes498cda62016-04-14 16:49:04 -0700759void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700760 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
761 dirent* de;
762 std::vector<std::string> intro_frame_names;
763 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700764
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700765 while ((de = readdir(dir.get())) != nullptr) {
766 int value, num_chars;
767 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
768 intro_frame_names.emplace_back(de->d_name, num_chars);
769 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
770 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700771 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700772 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700773
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700774 intro_frames = intro_frame_names.size();
775 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700776
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700777 // It's okay to not have an intro.
778 if (intro_frames == 0) intro_done = true;
779 // But you must have an animation.
780 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700781
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700782 std::sort(intro_frame_names.begin(), intro_frame_names.end());
783 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700784
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700785 introFrames = new GRSurface*[intro_frames];
786 for (size_t i = 0; i < intro_frames; i++) {
787 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
788 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700789
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700790 loopFrames = new GRSurface*[loop_frames];
791 for (size_t i = 0; i < loop_frames; i++) {
792 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
793 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700794}
795
Elliott Hughes8de52072015-04-08 20:06:50 -0700796void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700797 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700798
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700799 currentIcon = icon;
800 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700801
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700802 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700803}
804
Elliott Hughes8de52072015-04-08 20:06:50 -0700805void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700806 pthread_mutex_lock(&updateMutex);
807 if (progressBarType != type) {
808 progressBarType = type;
809 }
810 progressScopeStart = 0;
811 progressScopeSize = 0;
812 progress = 0;
813 update_progress_locked();
814 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700815}
816
Elliott Hughes8de52072015-04-08 20:06:50 -0700817void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700818 pthread_mutex_lock(&updateMutex);
819 progressBarType = DETERMINATE;
820 progressScopeStart += progressScopeSize;
821 progressScopeSize = portion;
822 progressScopeTime = now();
823 progressScopeDuration = seconds;
824 progress = 0;
825 update_progress_locked();
826 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700827}
828
Elliott Hughes8de52072015-04-08 20:06:50 -0700829void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700830 pthread_mutex_lock(&updateMutex);
831 if (fraction < 0.0) fraction = 0.0;
832 if (fraction > 1.0) fraction = 1.0;
833 if (progressBarType == DETERMINATE && fraction > progress) {
834 // Skip updates that aren't visibly different.
835 int width = gr_get_width(progressBarEmpty);
836 float scale = width * progressScopeSize;
837 if ((int)(progress * scale) != (int)(fraction * scale)) {
838 progress = fraction;
839 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700840 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700841 }
842 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700843}
844
Doug Zongkerc87bab12013-11-25 13:53:25 -0800845void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700846 pthread_mutex_lock(&updateMutex);
847 stage = current;
848 max_stage = max;
849 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800850}
851
Tao Baob6918c72015-05-19 17:02:16 -0700852void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700853 std::string str;
854 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700855
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700856 if (copy_to_stdout) {
857 fputs(str.c_str(), stdout);
858 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700859
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700860 pthread_mutex_lock(&updateMutex);
861 if (text_rows_ > 0 && text_cols_ > 0) {
862 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
863 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700864 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700865 text_col_ = 0;
866 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700867 }
868 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700869 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700870 text_[text_row_][text_col_] = '\0';
871 update_screen_locked();
872 }
873 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700874}
875
Tao Baob6918c72015-05-19 17:02:16 -0700876void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700877 va_list ap;
878 va_start(ap, fmt);
879 PrintV(fmt, true, ap);
880 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700881}
882
883void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700884 va_list ap;
885 va_start(ap, fmt);
886 PrintV(fmt, false, ap);
887 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700888}
889
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700890void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700891 pthread_mutex_lock(&updateMutex);
892 if (ch != '\n') text_[text_row_][text_col_++] = ch;
893 if (ch == '\n' || text_col_ >= text_cols_) {
894 text_col_ = 0;
895 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700896 }
897 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700898}
899
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700900void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700901 pthread_mutex_lock(&updateMutex);
902 text_col_ = 0;
903 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700904 for (size_t i = 0; i < text_rows_; ++i) {
905 memset(text_[i], 0, text_cols_ + 1);
906 }
907 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700908}
909
910void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700911 std::vector<off_t> offsets;
912 offsets.push_back(ftello(fp));
913 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700914
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700915 struct stat sb;
916 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700917
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700918 bool show_prompt = false;
919 while (true) {
920 if (show_prompt) {
921 PrintOnScreenOnly("--(%d%% of %d bytes)--",
922 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
923 static_cast<int>(sb.st_size));
924 Redraw();
925 while (show_prompt) {
926 show_prompt = false;
927 int key = WaitKey();
928 if (key == KEY_POWER || key == KEY_ENTER) {
929 return;
930 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
931 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700932 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700933 } else {
934 offsets.pop_back();
935 fseek(fp, offsets.back(), SEEK_SET);
936 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700937 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700938 if (feof(fp)) {
939 return;
940 }
941 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700942 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700943 }
944 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700945 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700946
947 int ch = getc(fp);
948 if (ch == EOF) {
949 while (text_row_ < text_rows_ - 1) PutChar('\n');
950 show_prompt = true;
951 } else {
952 PutChar(ch);
953 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
954 show_prompt = true;
955 }
956 }
957 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700958}
959
Tao Bao1d156b92018-05-02 12:43:18 -0700960void ScreenRecoveryUI::ShowFile(const std::string& filename) {
961 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
962 if (!fp) {
963 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700964 return;
965 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700966
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700967 char** old_text = text_;
968 size_t old_text_col = text_col_;
969 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700970
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700971 // Swap in the alternate screen and clear it.
972 text_ = file_viewer_text_;
973 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700974
Tao Bao1d156b92018-05-02 12:43:18 -0700975 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -0700976
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700977 text_ = old_text;
978 text_col_ = old_text_col;
979 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700980}
981
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700982void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700983 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700984 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700985 if (text_rows_ > 0 && text_cols_ > 1) {
Tao Baoe02a5b22018-05-02 15:46:11 -0700986 menu_ = std::make_unique<Menu>(scrollable_menu_, text_rows_, text_cols_ - 1, headers, items,
987 initial_selection);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700988 update_screen_locked();
989 }
990 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700991}
992
993int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700994 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700995 if (menu_) {
996 int old_sel = menu_->selection();
997 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -0700998
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700999 if (sel != old_sel) {
1000 update_screen_locked();
1001 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001002 }
1003 pthread_mutex_unlock(&updateMutex);
1004 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001005}
1006
1007void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001008 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001009 if (menu_) {
1010 menu_.reset();
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001011 update_screen_locked();
1012 }
1013 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001014}
1015
Tao Bao3aec6962018-04-20 09:24:58 -07001016int ScreenRecoveryUI::ShowMenu(const char* const* headers, const char* const* items,
1017 int initial_selection, bool menu_only,
1018 const std::function<int(int, bool)>& key_handler) {
1019 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1020 FlushKeys();
1021
1022 StartMenu(headers, items, initial_selection);
1023
1024 int selected = initial_selection;
1025 int chosen_item = -1;
1026 while (chosen_item < 0) {
1027 int key = WaitKey();
1028 if (key == -1) { // WaitKey() timed out.
1029 if (WasTextEverVisible()) {
1030 continue;
1031 } else {
1032 LOG(INFO) << "Timed out waiting for key input; rebooting.";
1033 EndMenu();
1034 return -1;
1035 }
1036 }
1037
1038 bool visible = IsTextVisible();
1039 int action = key_handler(key, visible);
1040 if (action < 0) {
1041 switch (action) {
1042 case Device::kHighlightUp:
1043 selected = SelectMenu(--selected);
1044 break;
1045 case Device::kHighlightDown:
1046 selected = SelectMenu(++selected);
1047 break;
1048 case Device::kInvokeItem:
1049 chosen_item = selected;
1050 break;
1051 case Device::kNoAction:
1052 break;
1053 }
1054 } else if (!menu_only) {
1055 chosen_item = action;
1056 }
1057 }
1058
1059 EndMenu();
1060 return chosen_item;
1061}
1062
Elliott Hughes8de52072015-04-08 20:06:50 -07001063bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001064 pthread_mutex_lock(&updateMutex);
1065 int visible = show_text;
1066 pthread_mutex_unlock(&updateMutex);
1067 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001068}
1069
Elliott Hughes8de52072015-04-08 20:06:50 -07001070bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001071 pthread_mutex_lock(&updateMutex);
1072 int ever_visible = show_text_ever;
1073 pthread_mutex_unlock(&updateMutex);
1074 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001075}
1076
Elliott Hughes8de52072015-04-08 20:06:50 -07001077void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001078 pthread_mutex_lock(&updateMutex);
1079 show_text = visible;
1080 if (show_text) show_text_ever = true;
1081 update_screen_locked();
1082 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001083}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001084
Elliott Hughes8de52072015-04-08 20:06:50 -07001085void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001086 pthread_mutex_lock(&updateMutex);
1087 update_screen_locked();
1088 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001089}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001090
1091void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001092 // Redraw so that if we're in the menu, the highlight
1093 // will change color to indicate a successful long press.
1094 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001095}
Tao Baoefb49ad2017-01-31 23:03:10 -08001096
1097void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1098 locale_ = new_locale;
1099 rtl_locale_ = false;
1100
1101 if (!new_locale.empty()) {
1102 size_t underscore = new_locale.find('_');
1103 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
1104 std::string lang = new_locale.substr(0, underscore);
1105
1106 // A bit cheesy: keep an explicit list of supported RTL languages.
1107 if (lang == "ar" || // Arabic
1108 lang == "fa" || // Persian (Farsi)
1109 lang == "he" || // Hebrew (new language code)
1110 lang == "iw" || // Hebrew (old language code)
1111 lang == "ur") { // Urdu
1112 rtl_locale_ = true;
1113 }
1114 }
1115}