blob: 0ee0ddcec424904da2645fca825e16fd844a73f8 [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>
Tao Bao26ea9592018-05-09 16:32:02 -070035#include <chrono>
Tianjie Xu29d55752017-09-20 17:53:46 -070036#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080037#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070038#include <thread>
Tianjie Xu29d55752017-09-20 17:53:46 -070039#include <unordered_map>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070040#include <vector>
41
Tianjie Xuc21edd42016-08-05 18:00:04 -070042#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070043#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080044#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070045#include <android-base/strings.h>
Tao Baob6918c72015-05-19 17:02:16 -070046
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070047#include "device.h"
Tao Bao6cd81682018-05-03 21:53:11 -070048#include "minui/minui.h"
49#include "otautil/paths.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070050#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070051
Doug Zongker211aebc2011-10-28 15:13:10 -070052// Return the current time as a double (including fractions of a second).
53static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070054 struct timeval tv;
55 gettimeofday(&tv, nullptr);
56 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070057}
58
Tao Bao1fe1afe2018-05-01 15:56:05 -070059Menu::Menu(bool scrollable, size_t max_items, size_t max_length,
60 const std::vector<std::string>& headers, const std::vector<std::string>& items,
61 size_t initial_selection)
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070062 : scrollable_(scrollable),
63 max_display_items_(max_items),
64 max_item_length_(max_length),
Tao Baoe02a5b22018-05-02 15:46:11 -070065 text_headers_(headers),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070066 menu_start_(0),
Tao Baoe02a5b22018-05-02 15:46:11 -070067 selection_(initial_selection) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070068 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070069
70 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
Tao Bao1fe1afe2018-05-01 15:56:05 -070071 size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_);
72 for (size_t i = 0; i < items_count; ++i) {
73 text_items_.emplace_back(items[i].substr(0, max_item_length_));
Tao Baoe02a5b22018-05-02 15:46:11 -070074 }
75
76 CHECK(!text_items_.empty());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070077}
78
Tao Bao1fe1afe2018-05-01 15:56:05 -070079const std::vector<std::string>& Menu::text_headers() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070080 return text_headers_;
81}
82
83std::string Menu::TextItem(size_t index) const {
84 CHECK_LT(index, text_items_.size());
85
86 return text_items_[index];
87}
88
89size_t Menu::MenuStart() const {
90 return menu_start_;
91}
92
93size_t Menu::MenuEnd() const {
94 return std::min(ItemsCount(), menu_start_ + max_display_items_);
95}
96
97size_t Menu::ItemsCount() const {
98 return text_items_.size();
99}
100
101bool Menu::ItemsOverflow(std::string* cur_selection_str) const {
Tao Baoe02a5b22018-05-02 15:46:11 -0700102 if (!scrollable_ || ItemsCount() <= max_display_items_) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700103 return false;
104 }
105
106 *cur_selection_str =
Tao Bao1fe1afe2018-05-01 15:56:05 -0700107 android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700108 return true;
109}
110
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700111// TODO(xunchang) modify the function parameters to button up & down.
112int Menu::Select(int sel) {
113 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
114 int count = ItemsCount();
115
116 // Wraps the selection at boundary if the menu is not scrollable.
117 if (!scrollable_) {
118 if (sel < 0) {
119 selection_ = count - 1;
120 } else if (sel >= count) {
121 selection_ = 0;
122 } else {
123 selection_ = sel;
124 }
125
126 return selection_;
127 }
128
129 if (sel < 0) {
130 selection_ = 0;
131 } else if (sel >= count) {
132 selection_ = count - 1;
133 } else {
134 if (static_cast<size_t>(sel) < menu_start_) {
135 menu_start_--;
136 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
137 menu_start_++;
138 }
139 selection_ = sel;
140 }
141
142 return selection_;
143}
144
145ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
146
147ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao4521b702017-06-20 18:11:21 -0700148 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
149 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -0700150 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -0700151 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -0700152 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -0800153 progressBarType(EMPTY),
154 progressScopeStart(0),
155 progressScopeSize(0),
156 progress(0),
157 pagesIdentical(false),
158 text_cols_(0),
159 text_rows_(0),
160 text_(nullptr),
161 text_col_(0),
162 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800163 show_text(false),
164 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700165 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800166 file_viewer_text_(nullptr),
167 intro_frames(0),
168 loop_frames(0),
169 current_frame(0),
170 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800171 stage(-1),
172 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800173 locale_(""),
174 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800175 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700176
Tao Bao26ea9592018-05-09 16:32:02 -0700177ScreenRecoveryUI::~ScreenRecoveryUI() {
178 progress_thread_stopped_ = true;
179 progress_thread_.join();
180}
181
Tao Bao99b2d772017-06-23 22:47:03 -0700182GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700183 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
184 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
185 }
186 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700187}
188
Tao Bao99b2d772017-06-23 22:47:03 -0700189GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700190 switch (currentIcon) {
191 case ERASING:
192 return erasing_text;
193 case ERROR:
194 return error_text;
195 case INSTALLING_UPDATE:
196 return installing_text;
197 case NO_COMMAND:
198 return no_command_text;
199 case NONE:
200 abort();
201 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700202}
203
Mikhail Lappob49767c2017-03-23 21:44:26 +0100204int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700205 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700206}
207
208// Here's the intended layout:
209
Elliott Hughes6d089a92016-07-08 17:23:41 -0700210// | portrait large landscape large
211// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700212// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700213// icon | (200dp)
214// gap | 68dp 68dp 56dp 112dp
215// text | (14sp)
216// gap | 32dp 32dp 26dp 52dp
217// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700218// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700219
Tao Bao3250f722017-06-29 14:32:05 -0700220// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
221// 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 -0700222
Elliott Hughes6d089a92016-07-08 17:23:41 -0700223enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700224enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700225static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700226 { 32, 68, }, // PORTRAIT
227 { 32, 68, }, // PORTRAIT_LARGE
228 { 26, 56, }, // LANDSCAPE
229 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700230};
231
Tao Bao99b2d772017-06-23 22:47:03 -0700232int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700233 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700234}
235
Tao Bao99b2d772017-06-23 22:47:03 -0700236int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700237 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
238 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700239}
240
Tao Bao99b2d772017-06-23 22:47:03 -0700241int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700242 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
243 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
244 gr_get_height(progressBarFill);
Luke Song92eda4d2017-09-19 10:51:35 -0700245 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
246 return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700247}
248
Doug Zongker211aebc2011-10-28 15:13:10 -0700249// Clear the screen and draw the currently selected background icon (if any).
250// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700251void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700252 pagesIdentical = false;
253 gr_color(0, 0, 0, 255);
254 gr_clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700255 if (currentIcon != NONE) {
256 if (max_stage != -1) {
257 int stage_height = gr_get_height(stageMarkerEmpty);
258 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700259 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
260 int y = ScreenHeight() - stage_height - kMarginHeight;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700261 for (int i = 0; i < max_stage; ++i) {
262 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700263 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700264 x += stage_width;
265 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700266 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700267
268 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700269 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700270 int text_y = GetTextBaseline();
271 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700272 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700273 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700274}
275
Tao Baoea78d862017-06-28 14:52:17 -0700276// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
277// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700278void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800279 if (currentIcon != NONE) {
280 GRSurface* frame = GetCurrentFrame();
281 int frame_width = gr_get_width(frame);
282 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700283 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800284 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700285 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800286 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700287
Tao Bao736d59c2017-01-03 10:15:33 -0800288 if (progressBarType != EMPTY) {
289 int width = gr_get_width(progressBarEmpty);
290 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700291
Luke Song92eda4d2017-09-19 10:51:35 -0700292 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800293 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700294
Tao Bao736d59c2017-01-03 10:15:33 -0800295 // Erase behind the progress bar (in case this was a progress-only update)
296 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700297 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700298
Tao Bao736d59c2017-01-03 10:15:33 -0800299 if (progressBarType == DETERMINATE) {
300 float p = progressScopeStart + progress * progressScopeSize;
301 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700302
Tao Bao736d59c2017-01-03 10:15:33 -0800303 if (rtl_locale_) {
304 // Fill the progress bar from right to left.
305 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700306 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
307 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700308 }
Tao Bao736d59c2017-01-03 10:15:33 -0800309 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700310 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800311 }
312 } else {
313 // Fill the progress bar from left to right.
314 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700315 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800316 }
317 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700318 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800319 }
320 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700321 }
Tao Bao736d59c2017-01-03 10:15:33 -0800322 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700323}
324
Tao Bao99b2d772017-06-23 22:47:03 -0700325void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700326 switch (e) {
327 case INFO:
328 gr_color(249, 194, 0, 255);
329 break;
330 case HEADER:
331 gr_color(247, 0, 6, 255);
332 break;
333 case MENU:
334 case MENU_SEL_BG:
335 gr_color(0, 106, 157, 255);
336 break;
337 case MENU_SEL_BG_ACTIVE:
338 gr_color(0, 156, 100, 255);
339 break;
340 case MENU_SEL_FG:
341 gr_color(255, 255, 255, 255);
342 break;
343 case LOG:
344 gr_color(196, 196, 196, 255);
345 break;
346 case TEXT_FILL:
347 gr_color(0, 0, 0, 160);
348 break;
349 default:
350 gr_color(255, 255, 255, 255);
351 break;
352 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700353}
Doug Zongker211aebc2011-10-28 15:13:10 -0700354
Tianjie Xu29d55752017-09-20 17:53:46 -0700355void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
356 size_t sel) {
357 SetLocale(locales_entries[sel]);
358 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
359 "installing_security_text", "no_command_text" };
360 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
361 for (const auto& name : text_name) {
362 GRSurface* text_image = nullptr;
363 LoadLocalizedBitmap(name.c_str(), &text_image);
364 if (!text_image) {
365 Print("Failed to load %s\n", name.c_str());
366 return;
367 }
368 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
369 }
370
371 pthread_mutex_lock(&updateMutex);
372 gr_color(0, 0, 0, 255);
373 gr_clear();
374
375 int text_y = kMarginHeight;
376 int text_x = kMarginWidth;
377 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
378 // Write the header and descriptive texts.
379 SetColor(INFO);
380 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700381 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700382 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700383 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700384 // clang-format off
385 std::vector<std::string> instruction = {
386 locale_selection,
387 "Use volume up/down to switch locales and power to exit."
388 };
389 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700390 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
391
392 // Iterate through the text images and display them in order for the current locale.
393 for (const auto& p : surfaces) {
394 text_y += line_spacing;
395 SetColor(LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700396 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700397 gr_color(255, 255, 255, 255);
398 gr_texticon(text_x, text_y, p.second.get());
399 text_y += gr_get_height(p.second.get());
400 }
401 // Update the whole screen.
402 gr_flip();
403 pthread_mutex_unlock(&updateMutex);
404}
405
Tao Bao39c49182018-05-07 22:50:33 -0700406void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700407 // Load a list of locales embedded in one of the resource files.
408 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
409 if (locales_entries.empty()) {
410 Print("Failed to load locales from the resource files\n");
411 return;
412 }
Tao Bao39c49182018-05-07 22:50:33 -0700413 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700414 size_t selected = 0;
415 SelectAndShowBackgroundText(locales_entries, selected);
416
417 FlushKeys();
418 while (true) {
419 int key = WaitKey();
420 if (key == KEY_POWER || key == KEY_ENTER) {
421 break;
422 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
423 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
424 SelectAndShowBackgroundText(locales_entries, selected);
425 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
426 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
427 SelectAndShowBackgroundText(locales_entries, selected);
428 }
429 }
430
431 SetLocale(saved_locale);
432}
433
Luke Song92eda4d2017-09-19 10:51:35 -0700434int ScreenRecoveryUI::ScreenWidth() const {
435 return gr_fb_width();
436}
437
438int ScreenRecoveryUI::ScreenHeight() const {
439 return gr_fb_height();
440}
441
442void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
443 int dy) const {
444 gr_blit(surface, sx, sy, w, h, dx, dy);
445}
446
Tao Baoea78d862017-06-28 14:52:17 -0700447int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700448 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700449 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700450}
451
Luke Songe2bd8762017-06-12 16:08:33 -0700452void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700453 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700454}
455
Luke Song92eda4d2017-09-19 10:51:35 -0700456void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
457 gr_fill(x, y, w, h);
458}
459
460void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
461 gr_texticon(x, y, surface);
462}
463
Tao Bao93e46ad2018-05-02 14:57:21 -0700464int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
465 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700466 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700467}
468
Tao Bao93e46ad2018-05-02 14:57:21 -0700469int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700470 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700471 for (const auto& line : lines) {
472 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700473 }
Tao Baoea78d862017-06-28 14:52:17 -0700474 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700475}
476
Tao Bao93e46ad2018-05-02 14:57:21 -0700477int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
478 const std::vector<std::string>& lines) const {
Tao Bao452b4872018-05-09 11:52:09 -0700479 // Keep symmetrical margins based on the given offset (i.e. x).
480 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700481 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700482 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700483 size_t next_start = 0;
484 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700485 std::string sub = line.substr(next_start, text_cols + 1);
486 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700487 next_start += sub.size();
488 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700489 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700490 size_t last_space = sub.find_last_of(" \t\n");
491 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700492 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700493 sub.resize(text_cols);
494 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700495 } else {
496 sub.resize(last_space);
497 next_start += last_space + 1;
498 }
499 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700500 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700501 }
502 }
503 return offset;
504}
505
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700506void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
507 title_lines_ = lines;
508}
509
Tao Bao171b4c42017-06-19 23:10:44 -0700510// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
511// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700512void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700513 if (!show_text) {
514 draw_background_locked();
515 draw_foreground_locked();
516 return;
517 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700518
Tao Bao171b4c42017-06-19 23:10:44 -0700519 gr_color(0, 0, 0, 255);
520 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700521
Tao Bao93e46ad2018-05-02 14:57:21 -0700522 // clang-format off
Tao Bao1fe1afe2018-05-01 15:56:05 -0700523 static std::vector<std::string> REGULAR_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700524 "Use volume up/down and power.",
525 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700526 static std::vector<std::string> LONG_PRESS_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700527 "Any button cycles highlight.",
528 "Long-press activates.",
529 };
530 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700531 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
532}
533
534// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700535void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
536 const std::vector<std::string>& help_message) {
Tao Bao4521b702017-06-20 18:11:21 -0700537 int y = kMarginHeight;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700538 if (menu_) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700539 static constexpr int kMenuIndent = 4;
540 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700541
Tao Bao171b4c42017-06-19 23:10:44 -0700542 SetColor(INFO);
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700543
544 for (size_t i = 0; i < title_lines_.size(); i++) {
545 y += DrawTextLine(x, y, title_lines_[i], i == 0);
Doug Zongker211aebc2011-10-28 15:13:10 -0700546 }
Tao Bao171b4c42017-06-19 23:10:44 -0700547
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700548 y += DrawTextLines(x, y, help_message);
549
550 // Draw menu header.
Tao Bao171b4c42017-06-19 23:10:44 -0700551 SetColor(HEADER);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700552 if (!menu_->scrollable()) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700553 y += DrawWrappedTextLines(x, y, menu_->text_headers());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700554 } else {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700555 y += DrawTextLines(x, y, menu_->text_headers());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700556 // Show the current menu item number in relation to total number if items don't fit on the
557 // screen.
558 std::string cur_selection_str;
559 if (menu_->ItemsOverflow(&cur_selection_str)) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700560 y += DrawTextLine(x, y, cur_selection_str, true);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700561 }
562 }
Tao Bao171b4c42017-06-19 23:10:44 -0700563
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700564 // Draw menu items.
Tao Bao171b4c42017-06-19 23:10:44 -0700565 SetColor(MENU);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700566 // Do not draw the horizontal rule for wear devices.
567 if (!menu_->scrollable()) {
568 y += DrawHorizontalRule(y) + 4;
569 }
570 for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) {
571 bool bold = false;
572 if (i == static_cast<size_t>(menu_->selection())) {
Tao Bao171b4c42017-06-19 23:10:44 -0700573 // Draw the highlight bar.
574 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700575
576 int bar_height = char_height_ + 4;
577 DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height);
578
Tao Bao171b4c42017-06-19 23:10:44 -0700579 // Bold white text for the selected item.
580 SetColor(MENU_SEL_FG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700581 bold = true;
Tao Bao171b4c42017-06-19 23:10:44 -0700582 }
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700583
Tao Bao93e46ad2018-05-02 14:57:21 -0700584 y += DrawTextLine(x, y, menu_->TextItem(i), bold);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700585
586 SetColor(MENU);
Tao Bao171b4c42017-06-19 23:10:44 -0700587 }
Tao Baoea78d862017-06-28 14:52:17 -0700588 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700589 }
590
591 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
592 // we've displayed the entire text buffer.
593 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700594 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700595 size_t count = 0;
Luke Song92eda4d2017-09-19 10:51:35 -0700596 for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700597 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700598 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700599 --row;
600 if (row < 0) row = text_rows_ - 1;
601 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700602}
603
604// Redraw everything on the screen and flip the screen (make it visible).
605// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700606void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700607 draw_screen_locked();
608 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700609}
610
611// Updates only the progress bar, if possible, otherwise redraws the screen.
612// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700613void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700614 if (show_text || !pagesIdentical) {
615 draw_screen_locked(); // Must redraw the whole screen
616 pagesIdentical = true;
617 } else {
618 draw_foreground_locked(); // Draw only the progress bar and overlays
619 }
620 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700621}
622
Elliott Hughes985022a2015-04-13 13:04:32 -0700623void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700624 double interval = 1.0 / kAnimationFps;
Tao Bao26ea9592018-05-09 16:32:02 -0700625 while (!progress_thread_stopped_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700626 double start = now();
627 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700628
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700629 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700630
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700631 // update the installation animation, if active
632 // skip this if we have a text overlay (too expensive to update)
633 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
634 if (!intro_done) {
635 if (current_frame == intro_frames - 1) {
636 intro_done = true;
637 current_frame = 0;
638 } else {
639 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700640 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700641 } else {
642 current_frame = (current_frame + 1) % loop_frames;
643 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700644
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700645 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700646 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700647
648 // move the progress bar forward on timed intervals, if configured
649 int duration = progressScopeDuration;
650 if (progressBarType == DETERMINATE && duration > 0) {
651 double elapsed = now() - progressScopeTime;
652 float p = 1.0 * elapsed / duration;
653 if (p > 1.0) p = 1.0;
654 if (p > progress) {
655 progress = p;
656 redraw = true;
657 }
658 }
659
660 if (redraw) update_progress_locked();
661
662 pthread_mutex_unlock(&updateMutex);
663 double end = now();
664 // minimum of 20ms delay between frames
665 double delay = interval - (end - start);
666 if (delay < 0.02) delay = 0.02;
667 usleep(static_cast<useconds_t>(delay * 1000000));
668 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700669}
670
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700671void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700672 int result = res_create_display_surface(filename, surface);
673 if (result < 0) {
674 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
675 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800676}
677
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700678void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800679 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
680 if (result < 0) {
681 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
682 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700683}
684
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700685static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700686 char** result = new char*[rows];
687 for (size_t i = 0; i < rows; ++i) {
688 result[i] = new char[cols];
689 memset(result[i], 0, cols);
690 }
691 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700692}
693
Tianjie Xu35926c42016-04-28 18:06:26 -0700694// Choose the right background string to display during update.
695void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700696 if (security_update) {
697 LoadLocalizedBitmap("installing_security_text", &installing_text);
698 } else {
699 LoadLocalizedBitmap("installing_text", &installing_text);
700 }
701 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700702}
703
Sen Jiangd5304492016-12-09 16:20:49 -0800704bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700705 if (gr_init() < 0) {
706 return false;
707 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700708
Tao Bao171b4c42017-06-19 23:10:44 -0700709 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Luke Song92eda4d2017-09-19 10:51:35 -0700710 text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
711 text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700712 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700713}
714
Tao Bao736d59c2017-01-03 10:15:33 -0800715bool ScreenRecoveryUI::Init(const std::string& locale) {
716 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800717
Tao Bao736d59c2017-01-03 10:15:33 -0800718 if (!InitTextParams()) {
719 return false;
720 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700721
Tao Bao736d59c2017-01-03 10:15:33 -0800722 // Are we portrait or landscape?
723 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
724 // Are we the large variant of our base layout?
725 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700726
Tao Bao736d59c2017-01-03 10:15:33 -0800727 text_ = Alloc2d(text_rows_, text_cols_ + 1);
728 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800729
Tao Bao736d59c2017-01-03 10:15:33 -0800730 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700731
Tao Baoefb49ad2017-01-31 23:03:10 -0800732 // Set up the locale info.
733 SetLocale(locale);
734
Tao Bao736d59c2017-01-03 10:15:33 -0800735 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700736
Tao Bao736d59c2017-01-03 10:15:33 -0800737 LoadBitmap("progress_empty", &progressBarEmpty);
738 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700739
Tao Bao736d59c2017-01-03 10:15:33 -0800740 LoadBitmap("stage_empty", &stageMarkerEmpty);
741 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700742
Tao Bao736d59c2017-01-03 10:15:33 -0800743 // Background text for "installing_update" could be "installing update"
744 // or "installing security update". It will be set after UI init according
745 // to commands in BCB.
746 installing_text = nullptr;
747 LoadLocalizedBitmap("erasing_text", &erasing_text);
748 LoadLocalizedBitmap("no_command_text", &no_command_text);
749 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700750
Tao Bao736d59c2017-01-03 10:15:33 -0800751 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700752
Tao Bao26ea9592018-05-09 16:32:02 -0700753 // Keep the progress bar updated, even when the process is otherwise busy.
754 progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, 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
Tao Bao551d2c32018-05-09 20:53:13 -0700759std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700760 return locale_;
761}
762
Elliott Hughes498cda62016-04-14 16:49:04 -0700763void ScreenRecoveryUI::LoadAnimation() {
Tao Bao6cd81682018-05-03 21:53:11 -0700764 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
765 closedir);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700766 dirent* de;
767 std::vector<std::string> intro_frame_names;
768 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700769
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700770 while ((de = readdir(dir.get())) != nullptr) {
771 int value, num_chars;
772 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
773 intro_frame_names.emplace_back(de->d_name, num_chars);
774 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
775 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700776 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700777 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700778
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700779 intro_frames = intro_frame_names.size();
780 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700781
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700782 // It's okay to not have an intro.
783 if (intro_frames == 0) intro_done = true;
784 // But you must have an animation.
785 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700786
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700787 std::sort(intro_frame_names.begin(), intro_frame_names.end());
788 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700789
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700790 introFrames = new GRSurface*[intro_frames];
791 for (size_t i = 0; i < intro_frames; i++) {
792 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
793 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700794
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700795 loopFrames = new GRSurface*[loop_frames];
796 for (size_t i = 0; i < loop_frames; i++) {
797 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
798 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700799}
800
Elliott Hughes8de52072015-04-08 20:06:50 -0700801void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700802 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700803
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700804 currentIcon = icon;
805 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700806
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700807 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700808}
809
Elliott Hughes8de52072015-04-08 20:06:50 -0700810void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700811 pthread_mutex_lock(&updateMutex);
812 if (progressBarType != type) {
813 progressBarType = type;
814 }
815 progressScopeStart = 0;
816 progressScopeSize = 0;
817 progress = 0;
818 update_progress_locked();
819 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700820}
821
Elliott Hughes8de52072015-04-08 20:06:50 -0700822void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700823 pthread_mutex_lock(&updateMutex);
824 progressBarType = DETERMINATE;
825 progressScopeStart += progressScopeSize;
826 progressScopeSize = portion;
827 progressScopeTime = now();
828 progressScopeDuration = seconds;
829 progress = 0;
830 update_progress_locked();
831 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700832}
833
Elliott Hughes8de52072015-04-08 20:06:50 -0700834void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700835 pthread_mutex_lock(&updateMutex);
836 if (fraction < 0.0) fraction = 0.0;
837 if (fraction > 1.0) fraction = 1.0;
838 if (progressBarType == DETERMINATE && fraction > progress) {
839 // Skip updates that aren't visibly different.
840 int width = gr_get_width(progressBarEmpty);
841 float scale = width * progressScopeSize;
842 if ((int)(progress * scale) != (int)(fraction * scale)) {
843 progress = fraction;
844 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700845 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700846 }
847 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700848}
849
Doug Zongkerc87bab12013-11-25 13:53:25 -0800850void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700851 pthread_mutex_lock(&updateMutex);
852 stage = current;
853 max_stage = max;
854 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800855}
856
Tao Baob6918c72015-05-19 17:02:16 -0700857void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700858 std::string str;
859 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700860
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700861 if (copy_to_stdout) {
862 fputs(str.c_str(), stdout);
863 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700864
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700865 pthread_mutex_lock(&updateMutex);
866 if (text_rows_ > 0 && text_cols_ > 0) {
867 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
868 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700869 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700870 text_col_ = 0;
871 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700872 }
873 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700874 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700875 text_[text_row_][text_col_] = '\0';
876 update_screen_locked();
877 }
878 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700879}
880
Tao Baob6918c72015-05-19 17:02:16 -0700881void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700882 va_list ap;
883 va_start(ap, fmt);
884 PrintV(fmt, true, ap);
885 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700886}
887
888void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700889 va_list ap;
890 va_start(ap, fmt);
891 PrintV(fmt, false, ap);
892 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700893}
894
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700895void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700896 pthread_mutex_lock(&updateMutex);
897 if (ch != '\n') text_[text_row_][text_col_++] = ch;
898 if (ch == '\n' || text_col_ >= text_cols_) {
899 text_col_ = 0;
900 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700901 }
902 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700903}
904
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700905void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700906 pthread_mutex_lock(&updateMutex);
907 text_col_ = 0;
908 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700909 for (size_t i = 0; i < text_rows_; ++i) {
910 memset(text_[i], 0, text_cols_ + 1);
911 }
912 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700913}
914
915void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700916 std::vector<off_t> offsets;
917 offsets.push_back(ftello(fp));
918 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700919
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700920 struct stat sb;
921 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700922
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700923 bool show_prompt = false;
924 while (true) {
925 if (show_prompt) {
926 PrintOnScreenOnly("--(%d%% of %d bytes)--",
927 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
928 static_cast<int>(sb.st_size));
929 Redraw();
930 while (show_prompt) {
931 show_prompt = false;
932 int key = WaitKey();
933 if (key == KEY_POWER || key == KEY_ENTER) {
934 return;
935 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
936 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700937 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700938 } else {
939 offsets.pop_back();
940 fseek(fp, offsets.back(), SEEK_SET);
941 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700942 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700943 if (feof(fp)) {
944 return;
945 }
946 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700947 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700948 }
949 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700950 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700951
952 int ch = getc(fp);
953 if (ch == EOF) {
954 while (text_row_ < text_rows_ - 1) PutChar('\n');
955 show_prompt = true;
956 } else {
957 PutChar(ch);
958 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
959 show_prompt = true;
960 }
961 }
962 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700963}
964
Tao Bao1d156b92018-05-02 12:43:18 -0700965void ScreenRecoveryUI::ShowFile(const std::string& filename) {
966 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
967 if (!fp) {
968 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700969 return;
970 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700971
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700972 char** old_text = text_;
973 size_t old_text_col = text_col_;
974 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700975
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700976 // Swap in the alternate screen and clear it.
977 text_ = file_viewer_text_;
978 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700979
Tao Bao1d156b92018-05-02 12:43:18 -0700980 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -0700981
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700982 text_ = old_text;
983 text_col_ = old_text_col;
984 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700985}
986
Tao Bao1fe1afe2018-05-01 15:56:05 -0700987void ScreenRecoveryUI::StartMenu(const std::vector<std::string>& headers,
988 const std::vector<std::string>& items, size_t initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700989 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700990 if (text_rows_ > 0 && text_cols_ > 1) {
Tao Baoe02a5b22018-05-02 15:46:11 -0700991 menu_ = std::make_unique<Menu>(scrollable_menu_, text_rows_, text_cols_ - 1, headers, items,
992 initial_selection);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700993 update_screen_locked();
994 }
995 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700996}
997
998int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700999 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001000 if (menu_) {
1001 int old_sel = menu_->selection();
1002 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001003
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001004 if (sel != old_sel) {
1005 update_screen_locked();
1006 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001007 }
1008 pthread_mutex_unlock(&updateMutex);
1009 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001010}
1011
1012void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001013 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001014 if (menu_) {
1015 menu_.reset();
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001016 update_screen_locked();
1017 }
1018 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001019}
1020
Tao Bao1fe1afe2018-05-01 15:56:05 -07001021size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1022 const std::vector<std::string>& items, size_t initial_selection,
1023 bool menu_only,
1024 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001025 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1026 FlushKeys();
1027
1028 StartMenu(headers, items, initial_selection);
1029
1030 int selected = initial_selection;
1031 int chosen_item = -1;
1032 while (chosen_item < 0) {
1033 int key = WaitKey();
1034 if (key == -1) { // WaitKey() timed out.
1035 if (WasTextEverVisible()) {
1036 continue;
1037 } else {
1038 LOG(INFO) << "Timed out waiting for key input; rebooting.";
1039 EndMenu();
Tao Bao1fe1afe2018-05-01 15:56:05 -07001040 return static_cast<size_t>(-1);
Tao Bao3aec6962018-04-20 09:24:58 -07001041 }
1042 }
1043
1044 bool visible = IsTextVisible();
1045 int action = key_handler(key, visible);
1046 if (action < 0) {
1047 switch (action) {
1048 case Device::kHighlightUp:
1049 selected = SelectMenu(--selected);
1050 break;
1051 case Device::kHighlightDown:
1052 selected = SelectMenu(++selected);
1053 break;
1054 case Device::kInvokeItem:
1055 chosen_item = selected;
1056 break;
1057 case Device::kNoAction:
1058 break;
1059 }
1060 } else if (!menu_only) {
1061 chosen_item = action;
1062 }
1063 }
1064
1065 EndMenu();
1066 return chosen_item;
1067}
1068
Elliott Hughes8de52072015-04-08 20:06:50 -07001069bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001070 pthread_mutex_lock(&updateMutex);
1071 int visible = show_text;
1072 pthread_mutex_unlock(&updateMutex);
1073 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001074}
1075
Elliott Hughes8de52072015-04-08 20:06:50 -07001076bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001077 pthread_mutex_lock(&updateMutex);
1078 int ever_visible = show_text_ever;
1079 pthread_mutex_unlock(&updateMutex);
1080 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001081}
1082
Elliott Hughes8de52072015-04-08 20:06:50 -07001083void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001084 pthread_mutex_lock(&updateMutex);
1085 show_text = visible;
1086 if (show_text) show_text_ever = true;
1087 update_screen_locked();
1088 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001089}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001090
Elliott Hughes8de52072015-04-08 20:06:50 -07001091void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001092 pthread_mutex_lock(&updateMutex);
1093 update_screen_locked();
1094 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001095}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001096
1097void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001098 // Redraw so that if we're in the menu, the highlight
1099 // will change color to indicate a successful long press.
1100 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001101}
Tao Baoefb49ad2017-01-31 23:03:10 -08001102
1103void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1104 locale_ = new_locale;
1105 rtl_locale_ = false;
1106
1107 if (!new_locale.empty()) {
Tao Bao347a6592018-05-08 15:58:29 -07001108 size_t separator = new_locale.find('-');
1109 // lang has the language prefix prior to the separator, or full string if none exists.
1110 std::string lang = new_locale.substr(0, separator);
Tao Baoefb49ad2017-01-31 23:03:10 -08001111
1112 // A bit cheesy: keep an explicit list of supported RTL languages.
1113 if (lang == "ar" || // Arabic
1114 lang == "fa" || // Persian (Farsi)
1115 lang == "he" || // Hebrew (new language code)
1116 lang == "iw" || // Hebrew (old language code)
1117 lang == "ur") { // Urdu
1118 rtl_locale_ = true;
1119 }
1120 }
1121}