blob: 578dd148c17482293e2ebcf5b4e07079b574d7ae [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
Tianjie Xu8f397302018-08-20 13:40:47 -070017#include "recovery_ui/screen_ui.h"
Tao Baoefb49ad2017-01-31 23:03:10 -080018
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>
Doug Zongker211aebc2011-10-28 15:13:10 -070022#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
31
Tao Bao1fe1afe2018-05-01 15:56:05 -070032#include <algorithm>
Tao Bao26ea9592018-05-09 16:32:02 -070033#include <chrono>
Tianjie Xu29d55752017-09-20 17:53:46 -070034#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080035#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070036#include <thread>
Tianjie Xu29d55752017-09-20 17:53:46 -070037#include <unordered_map>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070038#include <vector>
39
Mark Salyzyn30017e72020-05-13 12:39:12 -070040#include <android-base/chrono_utils.h>
Tianjie Xuc21edd42016-08-05 18:00:04 -070041#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070042#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080043#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070044#include <android-base/strings.h>
Tao Baob6918c72015-05-19 17:02:16 -070045
Tao Bao6cd81682018-05-03 21:53:11 -070046#include "minui/minui.h"
47#include "otautil/paths.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070048#include "recovery_ui/device.h"
49#include "recovery_ui/ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070050
fredchiouf2f5aa22022-02-11 16:39:53 +080051enum DirectRenderManager {
52 DRM_INNER,
53 DRM_OUTER,
54};
55
Doug Zongker211aebc2011-10-28 15:13:10 -070056// Return the current time as a double (including fractions of a second).
57static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070058 struct timeval tv;
59 gettimeofday(&tv, nullptr);
60 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070061}
62
Tianjie Xu66dbf632018-10-11 16:54:50 -070063Menu::Menu(size_t initial_selection, const DrawInterface& draw_func)
64 : selection_(initial_selection), draw_funcs_(draw_func) {}
65
66size_t Menu::selection() const {
67 return selection_;
68}
69
70TextMenu::TextMenu(bool scrollable, size_t max_items, size_t max_length,
71 const std::vector<std::string>& headers, const std::vector<std::string>& items,
72 size_t initial_selection, int char_height, const DrawInterface& draw_funcs)
73 : Menu(initial_selection, draw_funcs),
74 scrollable_(scrollable),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070075 max_display_items_(max_items),
76 max_item_length_(max_length),
Tao Baoe02a5b22018-05-02 15:46:11 -070077 text_headers_(headers),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070078 menu_start_(0),
Tianjie Xu66dbf632018-10-11 16:54:50 -070079 char_height_(char_height) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070080 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070081
82 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
Tao Bao1fe1afe2018-05-01 15:56:05 -070083 size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_);
84 for (size_t i = 0; i < items_count; ++i) {
85 text_items_.emplace_back(items[i].substr(0, max_item_length_));
Tao Baoe02a5b22018-05-02 15:46:11 -070086 }
87
88 CHECK(!text_items_.empty());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070089}
90
Tianjie Xu66dbf632018-10-11 16:54:50 -070091const std::vector<std::string>& TextMenu::text_headers() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070092 return text_headers_;
93}
94
Tianjie Xu66dbf632018-10-11 16:54:50 -070095std::string TextMenu::TextItem(size_t index) const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070096 CHECK_LT(index, text_items_.size());
97
98 return text_items_[index];
99}
100
Tianjie Xu66dbf632018-10-11 16:54:50 -0700101size_t TextMenu::MenuStart() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700102 return menu_start_;
103}
104
Tianjie Xu66dbf632018-10-11 16:54:50 -0700105size_t TextMenu::MenuEnd() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700106 return std::min(ItemsCount(), menu_start_ + max_display_items_);
107}
108
Tianjie Xu66dbf632018-10-11 16:54:50 -0700109size_t TextMenu::ItemsCount() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700110 return text_items_.size();
111}
112
Tianjie Xu66dbf632018-10-11 16:54:50 -0700113bool TextMenu::ItemsOverflow(std::string* cur_selection_str) const {
Tao Baoe02a5b22018-05-02 15:46:11 -0700114 if (!scrollable_ || ItemsCount() <= max_display_items_) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700115 return false;
116 }
117
118 *cur_selection_str =
Tao Bao1fe1afe2018-05-01 15:56:05 -0700119 android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700120 return true;
121}
122
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700123// TODO(xunchang) modify the function parameters to button up & down.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700124int TextMenu::Select(int sel) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700125 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
126 int count = ItemsCount();
127
128 // Wraps the selection at boundary if the menu is not scrollable.
129 if (!scrollable_) {
130 if (sel < 0) {
131 selection_ = count - 1;
132 } else if (sel >= count) {
133 selection_ = 0;
134 } else {
135 selection_ = sel;
136 }
137
138 return selection_;
139 }
140
141 if (sel < 0) {
142 selection_ = 0;
143 } else if (sel >= count) {
144 selection_ = count - 1;
145 } else {
146 if (static_cast<size_t>(sel) < menu_start_) {
147 menu_start_--;
148 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
149 menu_start_++;
150 }
151 selection_ = sel;
152 }
153
154 return selection_;
155}
156
Tianjie Xu66dbf632018-10-11 16:54:50 -0700157int TextMenu::DrawHeader(int x, int y) const {
158 int offset = 0;
159
160 draw_funcs_.SetColor(UIElement::HEADER);
161 if (!scrollable()) {
162 offset += draw_funcs_.DrawWrappedTextLines(x, y + offset, text_headers());
163 } else {
164 offset += draw_funcs_.DrawTextLines(x, y + offset, text_headers());
165 // Show the current menu item number in relation to total number if items don't fit on the
166 // screen.
167 std::string cur_selection_str;
168 if (ItemsOverflow(&cur_selection_str)) {
169 offset += draw_funcs_.DrawTextLine(x, y + offset, cur_selection_str, true);
170 }
171 }
172
173 return offset;
174}
175
176int TextMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
177 int offset = 0;
178
179 draw_funcs_.SetColor(UIElement::MENU);
180 // Do not draw the horizontal rule for wear devices.
181 if (!scrollable()) {
182 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
183 }
184 for (size_t i = MenuStart(); i < MenuEnd(); ++i) {
185 bool bold = false;
186 if (i == selection()) {
187 // Draw the highlight bar.
188 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
189
190 int bar_height = char_height_ + 4;
191 draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height);
192
193 // Bold white text for the selected item.
194 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
195 bold = true;
196 }
197 offset += draw_funcs_.DrawTextLine(x, y + offset, TextItem(i), bold);
198
199 draw_funcs_.SetColor(UIElement::MENU);
200 }
201 offset += draw_funcs_.DrawHorizontalRule(y + offset);
202
203 return offset;
204}
205
Tao Baoda409fb2018-10-21 23:36:26 -0700206GraphicMenu::GraphicMenu(const GRSurface* graphic_headers,
207 const std::vector<const GRSurface*>& graphic_items,
Tianjie Xub99e6062018-10-16 15:13:09 -0700208 size_t initial_selection, const DrawInterface& draw_funcs)
Tao Baoda409fb2018-10-21 23:36:26 -0700209 : Menu(initial_selection, draw_funcs) {
210 graphic_headers_ = graphic_headers->Clone();
211 graphic_items_.reserve(graphic_items.size());
212 for (const auto& item : graphic_items) {
213 graphic_items_.emplace_back(item->Clone());
214 }
215}
Tianjie Xu66dbf632018-10-11 16:54:50 -0700216
217int GraphicMenu::Select(int sel) {
218 CHECK_LE(graphic_items_.size(), static_cast<size_t>(std::numeric_limits<int>::max()));
219 int count = graphic_items_.size();
220
221 // Wraps the selection at boundary if the menu is not scrollable.
222 if (sel < 0) {
223 selection_ = count - 1;
224 } else if (sel >= count) {
225 selection_ = 0;
226 } else {
227 selection_ = sel;
228 }
229
230 return selection_;
231}
232
233int GraphicMenu::DrawHeader(int x, int y) const {
Tianjie Xub99e6062018-10-16 15:13:09 -0700234 draw_funcs_.SetColor(UIElement::HEADER);
Tao Baoda409fb2018-10-21 23:36:26 -0700235 draw_funcs_.DrawTextIcon(x, y, graphic_headers_.get());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700236 return graphic_headers_->height;
237}
238
239int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const {
240 int offset = 0;
241
242 draw_funcs_.SetColor(UIElement::MENU);
243 offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4;
244
245 for (size_t i = 0; i < graphic_items_.size(); i++) {
246 auto& item = graphic_items_[i];
247 if (i == selection_) {
248 draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG);
249
250 int bar_height = item->height + 4;
251 draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height);
252
253 // Bold white text for the selected item.
254 draw_funcs_.SetColor(UIElement::MENU_SEL_FG);
255 }
Tao Baoda409fb2018-10-21 23:36:26 -0700256 draw_funcs_.DrawTextIcon(x, y + offset, item.get());
Tianjie Xu66dbf632018-10-11 16:54:50 -0700257 offset += item->height;
258
259 draw_funcs_.SetColor(UIElement::MENU);
260 }
xunchangc7dbc732018-12-20 11:31:18 -0800261 offset += draw_funcs_.DrawHorizontalRule(y + offset);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700262
263 return offset;
264}
265
Tao Baoda409fb2018-10-21 23:36:26 -0700266bool GraphicMenu::Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers,
267 const std::vector<const GRSurface*>& graphic_items) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700268 int offset = 0;
Tianjie Xub99e6062018-10-16 15:13:09 -0700269 if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700270 return false;
271 }
Tianjie Xub99e6062018-10-16 15:13:09 -0700272 offset += graphic_headers->height;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700273
Tianjie Xub99e6062018-10-16 15:13:09 -0700274 for (const auto& item : graphic_items) {
275 if (!ValidateGraphicSurface(max_width, max_height, offset, item)) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700276 return false;
277 }
278 offset += item->height;
279 }
280
281 return true;
282}
283
Tianjie Xub99e6062018-10-16 15:13:09 -0700284bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y,
285 const GRSurface* surface) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700286 if (!surface) {
Tao Baoa00b4492019-01-16 09:29:47 -0800287 fprintf(stderr, "Graphic surface can not be null\n");
Tianjie Xu66dbf632018-10-11 16:54:50 -0700288 return false;
289 }
290
291 if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) {
Tao Baoa00b4492019-01-16 09:29:47 -0800292 fprintf(stderr, "Invalid graphic surface, pixel bytes: %zu, width: %zu row_bytes: %zu\n",
Tianjie Xu66dbf632018-10-11 16:54:50 -0700293 surface->pixel_bytes, surface->width, surface->row_bytes);
294 return false;
295 }
296
Tianjie Xub99e6062018-10-16 15:13:09 -0700297 if (surface->width > max_width || surface->height > max_height - y) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700298 fprintf(stderr,
Tao Baodd789822018-11-26 16:28:07 -0800299 "Graphic surface doesn't fit into the screen. width: %zu, height: %zu, max_width: %zu,"
Tianjie Xu66dbf632018-10-11 16:54:50 -0700300 " max_height: %zu, vertical offset: %d\n",
Tianjie Xub99e6062018-10-16 15:13:09 -0700301 surface->width, surface->height, max_width, max_height, y);
Tianjie Xu66dbf632018-10-11 16:54:50 -0700302 return false;
303 }
304
305 return true;
306}
307
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700308ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
309
Tao Bao0bc88de2018-07-31 14:53:16 -0700310constexpr int kDefaultMarginHeight = 0;
311constexpr int kDefaultMarginWidth = 0;
312constexpr int kDefaultAnimationFps = 30;
313
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700314ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao0bc88de2018-07-31 14:53:16 -0700315 : margin_width_(
316 android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)),
317 margin_height_(
318 android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)),
319 animation_fps_(
320 android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)),
321 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Michael Bestas19630862019-03-23 17:28:22 +0200322 blank_unblank_on_init_(
323 android::base::GetBoolProperty("ro.recovery.ui.blank_unblank_on_init", false)),
Tao Baoda409fb2018-10-21 23:36:26 -0700324 current_icon_(NONE),
325 current_frame_(0),
326 intro_done_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800327 progressBarType(EMPTY),
328 progressScopeStart(0),
329 progressScopeSize(0),
330 progress(0),
331 pagesIdentical(false),
332 text_cols_(0),
333 text_rows_(0),
334 text_(nullptr),
335 text_col_(0),
336 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800337 show_text(false),
338 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700339 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800340 file_viewer_text_(nullptr),
Tao Bao736d59c2017-01-03 10:15:33 -0800341 stage(-1),
342 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800343 locale_(""),
fredchiouf2f5aa22022-02-11 16:39:53 +0800344 rtl_locale_(false),
345 is_graphics_available(false) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700346
Tao Bao26ea9592018-05-09 16:32:02 -0700347ScreenRecoveryUI::~ScreenRecoveryUI() {
348 progress_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -0700349 if (progress_thread_.joinable()) {
350 progress_thread_.join();
351 }
Tao Bao60ac6222018-06-13 14:33:51 -0700352 // No-op if gr_init() (via Init()) was not called or had failed.
353 gr_exit();
Tao Bao26ea9592018-05-09 16:32:02 -0700354}
355
Tao Baoda409fb2018-10-21 23:36:26 -0700356const GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
357 if (current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) {
358 return intro_done_ ? loop_frames_[current_frame_].get() : intro_frames_[current_frame_].get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700359 }
Tao Baoda409fb2018-10-21 23:36:26 -0700360 return error_icon_.get();
Elliott Hughes498cda62016-04-14 16:49:04 -0700361}
362
Tao Baoda409fb2018-10-21 23:36:26 -0700363const GRSurface* ScreenRecoveryUI::GetCurrentText() const {
364 switch (current_icon_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700365 case ERASING:
Tao Baoda409fb2018-10-21 23:36:26 -0700366 return erasing_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700367 case ERROR:
Tao Baoda409fb2018-10-21 23:36:26 -0700368 return error_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700369 case INSTALLING_UPDATE:
Tao Baoda409fb2018-10-21 23:36:26 -0700370 return installing_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700371 case NO_COMMAND:
Tao Baoda409fb2018-10-21 23:36:26 -0700372 return no_command_text_.get();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700373 case NONE:
374 abort();
375 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700376}
377
Mikhail Lappob49767c2017-03-23 21:44:26 +0100378int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao0bc88de2018-07-31 14:53:16 -0700379 return dp * density_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700380}
381
382// Here's the intended layout:
383
Elliott Hughes6d089a92016-07-08 17:23:41 -0700384// | portrait large landscape large
385// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700386// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700387// icon | (200dp)
388// gap | 68dp 68dp 56dp 112dp
389// text | (14sp)
390// gap | 32dp 32dp 26dp 52dp
391// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700392// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700393
Tao Bao3250f722017-06-29 14:32:05 -0700394// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
395// 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 -0700396
Elliott Hughes6d089a92016-07-08 17:23:41 -0700397enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700398enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700399static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tianjie Xu8f397302018-08-20 13:40:47 -0700400 { 32, 68 }, // PORTRAIT
401 { 32, 68 }, // PORTRAIT_LARGE
402 { 26, 56 }, // LANDSCAPE
403 { 52, 112 }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700404};
405
Tao Bao99b2d772017-06-23 22:47:03 -0700406int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Baoda409fb2018-10-21 23:36:26 -0700407 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) -
408 gr_get_height(loop_frames_[0].get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700409}
410
Tao Bao99b2d772017-06-23 22:47:03 -0700411int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700412 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
Tao Baoda409fb2018-10-21 23:36:26 -0700413 gr_get_height(installing_text_.get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700414}
415
Tao Bao99b2d772017-06-23 22:47:03 -0700416int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Baoda409fb2018-10-21 23:36:26 -0700417 int elements_sum = gr_get_height(loop_frames_[0].get()) + PixelsFromDp(kLayouts[layout_][ICON]) +
418 gr_get_height(installing_text_.get()) + PixelsFromDp(kLayouts[layout_][TEXT]) +
419 gr_get_height(progress_bar_fill_.get());
Luke Song92eda4d2017-09-19 10:51:35 -0700420 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
Tao Baoda409fb2018-10-21 23:36:26 -0700421 return ScreenHeight() - bottom_gap - gr_get_height(progress_bar_fill_.get());
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700422}
423
Doug Zongker211aebc2011-10-28 15:13:10 -0700424// Clear the screen and draw the currently selected background icon (if any).
425// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700426void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700427 pagesIdentical = false;
428 gr_color(0, 0, 0, 255);
429 gr_clear();
Tao Baoda409fb2018-10-21 23:36:26 -0700430 if (current_icon_ != NONE) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700431 if (max_stage != -1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700432 int stage_height = gr_get_height(stage_marker_empty_.get());
433 int stage_width = gr_get_width(stage_marker_empty_.get());
434 int x = (ScreenWidth() - max_stage * gr_get_width(stage_marker_empty_.get())) / 2;
Tao Bao0bc88de2018-07-31 14:53:16 -0700435 int y = ScreenHeight() - stage_height - margin_height_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700436 for (int i = 0; i < max_stage; ++i) {
Tao Baoda409fb2018-10-21 23:36:26 -0700437 const auto& stage_surface = (i < stage) ? stage_marker_fill_ : stage_marker_empty_;
438 DrawSurface(stage_surface.get(), 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700439 x += stage_width;
440 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700441 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700442
Tao Baoda409fb2018-10-21 23:36:26 -0700443 const auto& text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700444 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700445 int text_y = GetTextBaseline();
446 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700447 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700448 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700449}
450
Tao Baoea78d862017-06-28 14:52:17 -0700451// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
452// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700453void ScreenRecoveryUI::draw_foreground_locked() {
Tao Baoda409fb2018-10-21 23:36:26 -0700454 if (current_icon_ != NONE) {
455 const auto& frame = GetCurrentFrame();
Tao Bao736d59c2017-01-03 10:15:33 -0800456 int frame_width = gr_get_width(frame);
457 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700458 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800459 int frame_y = GetAnimationBaseline();
zhang sanshan0d81b892019-08-07 16:21:10 +0800460 if (frame_x >= 0 && frame_y >= 0 && (frame_x + frame_width) < ScreenWidth() &&
461 (frame_y + frame_height) < ScreenHeight())
462 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800463 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700464
Tao Bao736d59c2017-01-03 10:15:33 -0800465 if (progressBarType != EMPTY) {
Tao Baoda409fb2018-10-21 23:36:26 -0700466 int width = gr_get_width(progress_bar_empty_.get());
467 int height = gr_get_height(progress_bar_empty_.get());
Doug Zongker211aebc2011-10-28 15:13:10 -0700468
Luke Song92eda4d2017-09-19 10:51:35 -0700469 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800470 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700471
Tao Bao736d59c2017-01-03 10:15:33 -0800472 // Erase behind the progress bar (in case this was a progress-only update)
473 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700474 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700475
Tao Bao736d59c2017-01-03 10:15:33 -0800476 if (progressBarType == DETERMINATE) {
477 float p = progressScopeStart + progress * progressScopeSize;
478 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700479
Tao Bao736d59c2017-01-03 10:15:33 -0800480 if (rtl_locale_) {
481 // Fill the progress bar from right to left.
482 if (pos > 0) {
Tao Baoda409fb2018-10-21 23:36:26 -0700483 DrawSurface(progress_bar_fill_.get(), width - pos, 0, pos, height,
484 progress_x + width - pos, progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700485 }
Tao Bao736d59c2017-01-03 10:15:33 -0800486 if (pos < width - 1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700487 DrawSurface(progress_bar_empty_.get(), 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800488 }
489 } else {
490 // Fill the progress bar from left to right.
491 if (pos > 0) {
Tao Baoda409fb2018-10-21 23:36:26 -0700492 DrawSurface(progress_bar_fill_.get(), 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800493 }
494 if (pos < width - 1) {
Tao Baoda409fb2018-10-21 23:36:26 -0700495 DrawSurface(progress_bar_empty_.get(), pos, 0, width - pos, height, progress_x + pos,
496 progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800497 }
498 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700499 }
Tao Bao736d59c2017-01-03 10:15:33 -0800500 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700501}
502
Tao Bao99b2d772017-06-23 22:47:03 -0700503void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700504 switch (e) {
Tianjie Xu66dbf632018-10-11 16:54:50 -0700505 case UIElement::INFO:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700506 gr_color(249, 194, 0, 255);
507 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700508 case UIElement::HEADER:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700509 gr_color(247, 0, 6, 255);
510 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700511 case UIElement::MENU:
512 case UIElement::MENU_SEL_BG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700513 gr_color(0, 106, 157, 255);
514 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700515 case UIElement::MENU_SEL_BG_ACTIVE:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700516 gr_color(0, 156, 100, 255);
517 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700518 case UIElement::MENU_SEL_FG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700519 gr_color(255, 255, 255, 255);
520 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700521 case UIElement::LOG:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700522 gr_color(196, 196, 196, 255);
523 break;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700524 case UIElement::TEXT_FILL:
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700525 gr_color(0, 0, 0, 160);
526 break;
527 default:
528 gr_color(255, 255, 255, 255);
529 break;
530 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700531}
Doug Zongker211aebc2011-10-28 15:13:10 -0700532
Tianjie Xu29d55752017-09-20 17:53:46 -0700533void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
534 size_t sel) {
535 SetLocale(locales_entries[sel]);
536 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
537 "installing_security_text", "no_command_text" };
Tao Baoda409fb2018-10-21 23:36:26 -0700538 std::unordered_map<std::string, std::unique_ptr<GRSurface>> surfaces;
Tianjie Xu29d55752017-09-20 17:53:46 -0700539 for (const auto& name : text_name) {
Tao Baoda409fb2018-10-21 23:36:26 -0700540 auto text_image = LoadLocalizedBitmap(name);
Tianjie Xu29d55752017-09-20 17:53:46 -0700541 if (!text_image) {
542 Print("Failed to load %s\n", name.c_str());
543 return;
544 }
Tao Baoda409fb2018-10-21 23:36:26 -0700545 surfaces.emplace(name, std::move(text_image));
Tianjie Xu29d55752017-09-20 17:53:46 -0700546 }
547
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700548 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu29d55752017-09-20 17:53:46 -0700549 gr_color(0, 0, 0, 255);
550 gr_clear();
551
Tao Bao0bc88de2018-07-31 14:53:16 -0700552 int text_y = margin_height_;
553 int text_x = margin_width_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700554 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
555 // Write the header and descriptive texts.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700556 SetColor(UIElement::INFO);
Tianjie Xu29d55752017-09-20 17:53:46 -0700557 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700558 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700559 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700560 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700561 // clang-format off
562 std::vector<std::string> instruction = {
563 locale_selection,
564 "Use volume up/down to switch locales and power to exit."
565 };
566 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700567 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
568
569 // Iterate through the text images and display them in order for the current locale.
570 for (const auto& p : surfaces) {
571 text_y += line_spacing;
Tianjie Xu66dbf632018-10-11 16:54:50 -0700572 SetColor(UIElement::LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700573 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700574 gr_color(255, 255, 255, 255);
575 gr_texticon(text_x, text_y, p.second.get());
576 text_y += gr_get_height(p.second.get());
577 }
578 // Update the whole screen.
579 gr_flip();
Tianjie Xu29d55752017-09-20 17:53:46 -0700580}
581
Tao Bao39c49182018-05-07 22:50:33 -0700582void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700583 // Load a list of locales embedded in one of the resource files.
584 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
585 if (locales_entries.empty()) {
586 Print("Failed to load locales from the resource files\n");
587 return;
588 }
Tao Bao39c49182018-05-07 22:50:33 -0700589 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700590 size_t selected = 0;
591 SelectAndShowBackgroundText(locales_entries, selected);
592
593 FlushKeys();
594 while (true) {
595 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -0700596 if (key == static_cast<int>(KeyError::INTERRUPTED)) break;
Tianjie Xu29d55752017-09-20 17:53:46 -0700597 if (key == KEY_POWER || key == KEY_ENTER) {
598 break;
599 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
600 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
601 SelectAndShowBackgroundText(locales_entries, selected);
602 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
603 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
604 SelectAndShowBackgroundText(locales_entries, selected);
605 }
606 }
607
608 SetLocale(saved_locale);
609}
610
Luke Song92eda4d2017-09-19 10:51:35 -0700611int ScreenRecoveryUI::ScreenWidth() const {
612 return gr_fb_width();
613}
614
615int ScreenRecoveryUI::ScreenHeight() const {
616 return gr_fb_height();
617}
618
Tao Bao65815b62018-10-23 10:54:02 -0700619void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx,
Luke Song92eda4d2017-09-19 10:51:35 -0700620 int dy) const {
621 gr_blit(surface, sx, sy, w, h, dx, dy);
622}
623
Tao Baoea78d862017-06-28 14:52:17 -0700624int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700625 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700626 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700627}
628
Luke Songe2bd8762017-06-12 16:08:33 -0700629void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700630 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700631}
632
Luke Song92eda4d2017-09-19 10:51:35 -0700633void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
634 gr_fill(x, y, w, h);
635}
636
Tao Bao65815b62018-10-23 10:54:02 -0700637void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700638 gr_texticon(x, y, surface);
639}
640
Tao Bao93e46ad2018-05-02 14:57:21 -0700641int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
642 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700643 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700644}
645
Tao Bao93e46ad2018-05-02 14:57:21 -0700646int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700647 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700648 for (const auto& line : lines) {
649 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700650 }
Tao Baoea78d862017-06-28 14:52:17 -0700651 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700652}
653
Tao Bao93e46ad2018-05-02 14:57:21 -0700654int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
655 const std::vector<std::string>& lines) const {
Tao Bao452b4872018-05-09 11:52:09 -0700656 // Keep symmetrical margins based on the given offset (i.e. x).
657 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700658 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700659 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700660 size_t next_start = 0;
661 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700662 std::string sub = line.substr(next_start, text_cols + 1);
663 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700664 next_start += sub.size();
665 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700666 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700667 size_t last_space = sub.find_last_of(" \t\n");
668 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700669 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700670 sub.resize(text_cols);
671 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700672 } else {
673 sub.resize(last_space);
674 next_start += last_space + 1;
675 }
676 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700677 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700678 }
679 }
680 return offset;
681}
682
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700683void ScreenRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
684 title_lines_ = lines;
685}
686
Tianjie Xue5032212019-07-23 13:23:29 -0700687std::vector<std::string> ScreenRecoveryUI::GetMenuHelpMessage() const {
688 // clang-format off
689 static std::vector<std::string> REGULAR_HELP{
690 "Use volume up/down and power.",
691 };
692 static std::vector<std::string> LONG_PRESS_HELP{
693 "Any button cycles highlight.",
694 "Long-press activates.",
695 };
696 // clang-format on
697 return HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP;
698}
699
Tao Bao171b4c42017-06-19 23:10:44 -0700700// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
701// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700702void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700703 if (!show_text) {
704 draw_background_locked();
705 draw_foreground_locked();
706 return;
707 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700708
Tao Bao171b4c42017-06-19 23:10:44 -0700709 gr_color(0, 0, 0, 255);
710 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700711
Tianjie Xue5032212019-07-23 13:23:29 -0700712 draw_menu_and_text_buffer_locked(GetMenuHelpMessage());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700713}
714
715// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700716void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
717 const std::vector<std::string>& help_message) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700718 int y = margin_height_;
David Anderson983e2d52019-01-02 11:35:38 -0800719
720 if (fastbootd_logo_ && fastbootd_logo_enabled_) {
721 // Try to get this centered on screen.
722 auto width = gr_get_width(fastbootd_logo_.get());
723 auto height = gr_get_height(fastbootd_logo_.get());
724 auto centered_x = ScreenWidth() / 2 - width / 2;
725 DrawSurface(fastbootd_logo_.get(), 0, 0, width, height, centered_x, y);
726 y += height;
727 }
728
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700729 if (menu_) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700730 int x = margin_width_ + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700731
Tianjie Xu66dbf632018-10-11 16:54:50 -0700732 SetColor(UIElement::INFO);
Jerry Zhang0e577ee2018-05-07 11:21:10 -0700733
734 for (size_t i = 0; i < title_lines_.size(); i++) {
735 y += DrawTextLine(x, y, title_lines_[i], i == 0);
Doug Zongker211aebc2011-10-28 15:13:10 -0700736 }
Tao Bao171b4c42017-06-19 23:10:44 -0700737
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700738 y += DrawTextLines(x, y, help_message);
739
Tianjie Xu66dbf632018-10-11 16:54:50 -0700740 y += menu_->DrawHeader(x, y);
741 y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress());
Tao Bao171b4c42017-06-19 23:10:44 -0700742 }
743
744 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
745 // we've displayed the entire text buffer.
Tianjie Xu66dbf632018-10-11 16:54:50 -0700746 SetColor(UIElement::LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700747 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700748 size_t count = 0;
Tao Bao0bc88de2018-07-31 14:53:16 -0700749 for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700750 ty -= char_height_, ++count) {
Tao Bao0bc88de2018-07-31 14:53:16 -0700751 DrawTextLine(margin_width_, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700752 --row;
753 if (row < 0) row = text_rows_ - 1;
754 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700755}
756
757// Redraw everything on the screen and flip the screen (make it visible).
758// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700759void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700760 draw_screen_locked();
761 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700762}
763
764// Updates only the progress bar, if possible, otherwise redraws the screen.
765// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700766void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700767 if (show_text || !pagesIdentical) {
768 draw_screen_locked(); // Must redraw the whole screen
769 pagesIdentical = true;
770 } else {
771 draw_foreground_locked(); // Draw only the progress bar and overlays
772 }
773 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700774}
775
Elliott Hughes985022a2015-04-13 13:04:32 -0700776void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0bc88de2018-07-31 14:53:16 -0700777 double interval = 1.0 / animation_fps_;
Tao Bao26ea9592018-05-09 16:32:02 -0700778 while (!progress_thread_stopped_) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700779 double start = now();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700780 bool redraw = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700781 {
782 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700783
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700784 // update the installation animation, if active
785 // skip this if we have a text overlay (too expensive to update)
Tao Baoda409fb2018-10-21 23:36:26 -0700786 if ((current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) && !show_text) {
787 if (!intro_done_) {
788 if (current_frame_ == intro_frames_.size() - 1) {
789 intro_done_ = true;
790 current_frame_ = 0;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700791 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700792 ++current_frame_;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700793 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700794 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700795 current_frame_ = (current_frame_ + 1) % loop_frames_.size();
Doug Zongker211aebc2011-10-28 15:13:10 -0700796 }
797
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700798 redraw = true;
799 }
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700800
801 // move the progress bar forward on timed intervals, if configured
802 int duration = progressScopeDuration;
803 if (progressBarType == DETERMINATE && duration > 0) {
804 double elapsed = now() - progressScopeTime;
805 float p = 1.0 * elapsed / duration;
806 if (p > 1.0) p = 1.0;
807 if (p > progress) {
808 progress = p;
809 redraw = true;
810 }
811 }
812
813 if (redraw) update_progress_locked();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700814 }
815
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700816 double end = now();
817 // minimum of 20ms delay between frames
818 double delay = interval - (end - start);
819 if (delay < 0.02) delay = 0.02;
820 usleep(static_cast<useconds_t>(delay * 1000000));
821 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700822}
823
Tao Baoda409fb2018-10-21 23:36:26 -0700824std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadBitmap(const std::string& filename) {
825 GRSurface* surface;
826 if (auto result = res_create_display_surface(filename.c_str(), &surface); result < 0) {
827 LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")";
828 return nullptr;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700829 }
Tao Baoda409fb2018-10-21 23:36:26 -0700830 return std::unique_ptr<GRSurface>(surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800831}
832
Tao Baoda409fb2018-10-21 23:36:26 -0700833std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) {
834 GRSurface* surface;
xunchang9d05c8a2019-04-16 12:07:42 -0700835 auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface);
836 if (result == 0) {
837 return std::unique_ptr<GRSurface>(surface);
Tao Bao736d59c2017-01-03 10:15:33 -0800838 }
xunchang9d05c8a2019-04-16 12:07:42 -0700839 // TODO(xunchang) create a error code enum to refine the retry condition.
840 LOG(WARNING) << "Failed to load bitmap " << filename << " for locale " << locale_ << " (error "
841 << result << "). Falling back to use default locale.";
842
843 result = res_create_localized_alpha_surface(filename.c_str(), DEFAULT_LOCALE, &surface);
844 if (result == 0) {
845 return std::unique_ptr<GRSurface>(surface);
846 }
847
848 LOG(ERROR) << "Failed to load bitmap " << filename << " for locale " << DEFAULT_LOCALE
849 << " (error " << result << ")";
850 return nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700851}
852
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700853static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700854 char** result = new char*[rows];
855 for (size_t i = 0; i < rows; ++i) {
856 result[i] = new char[cols];
857 memset(result[i], 0, cols);
858 }
859 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700860}
861
Tianjie Xu35926c42016-04-28 18:06:26 -0700862// Choose the right background string to display during update.
863void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700864 if (security_update) {
Tao Baoda409fb2018-10-21 23:36:26 -0700865 installing_text_ = LoadLocalizedBitmap("installing_security_text");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700866 } else {
Tao Baoda409fb2018-10-21 23:36:26 -0700867 installing_text_ = LoadLocalizedBitmap("installing_text");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700868 }
869 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700870}
871
Sen Jiangd5304492016-12-09 16:20:49 -0800872bool ScreenRecoveryUI::InitTextParams() {
Tao Baoba4edb32018-06-13 11:22:50 -0700873 // gr_init() would return successfully on font initialization failure.
874 if (gr_sys_font() == nullptr) {
Tao Bao171b4c42017-06-19 23:10:44 -0700875 return false;
876 }
Tao Bao171b4c42017-06-19 23:10:44 -0700877 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao0bc88de2018-07-31 14:53:16 -0700878 text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_;
879 text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700880 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700881}
882
Tianjie Xub99e6062018-10-16 15:13:09 -0700883bool ScreenRecoveryUI::LoadWipeDataMenuText() {
Tianjie Xu1a0a30a2018-10-25 15:22:07 -0700884 // Ignores the errors since the member variables will stay as nullptr.
885 cancel_wipe_data_text_ = LoadLocalizedBitmap("cancel_wipe_data_text");
886 factory_data_reset_text_ = LoadLocalizedBitmap("factory_data_reset_text");
887 try_again_text_ = LoadLocalizedBitmap("try_again_text");
888 wipe_data_confirmation_text_ = LoadLocalizedBitmap("wipe_data_confirmation_text");
889 wipe_data_menu_header_text_ = LoadLocalizedBitmap("wipe_data_menu_header_text");
Tianjie Xub99e6062018-10-16 15:13:09 -0700890 return true;
891}
892
Mark Salyzyn30017e72020-05-13 12:39:12 -0700893static bool InitGraphics() {
894 // Timeout is same as init wait for file default of 5 seconds and is arbitrary
895 const unsigned timeout = 500; // 10ms increments
896 for (auto retry = timeout; retry > 0; --retry) {
897 if (gr_init() == 0) {
898 if (retry < timeout) {
899 // Log message like init wait for file completion log for consistency.
900 LOG(WARNING) << "wait for 'graphics' took " << ((timeout - retry) * 10) << "ms";
901 }
902 return true;
903 }
904 std::this_thread::sleep_for(10ms);
905 }
906 // Log message like init wait for file timeout log for consistency.
907 LOG(ERROR) << "timeout wait for 'graphics' took " << (timeout * 10) << "ms";
908 return false;
909}
910
Tao Bao736d59c2017-01-03 10:15:33 -0800911bool ScreenRecoveryUI::Init(const std::string& locale) {
912 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800913
Mark Salyzyn30017e72020-05-13 12:39:12 -0700914 if (!InitGraphics()) {
Tao Baoba4edb32018-06-13 11:22:50 -0700915 return false;
916 }
fredchiouf2f5aa22022-02-11 16:39:53 +0800917 is_graphics_available = true;
Tao Baoba4edb32018-06-13 11:22:50 -0700918
Tao Bao736d59c2017-01-03 10:15:33 -0800919 if (!InitTextParams()) {
920 return false;
921 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700922
Michael Bestas19630862019-03-23 17:28:22 +0200923 if (blank_unblank_on_init_) {
924 gr_fb_blank(true);
925 gr_fb_blank(false);
926 }
927
Tao Bao736d59c2017-01-03 10:15:33 -0800928 // Are we portrait or landscape?
929 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
930 // Are we the large variant of our base layout?
931 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700932
Tao Bao736d59c2017-01-03 10:15:33 -0800933 text_ = Alloc2d(text_rows_, text_cols_ + 1);
934 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800935
Tao Bao736d59c2017-01-03 10:15:33 -0800936 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700937
Tao Baoefb49ad2017-01-31 23:03:10 -0800938 // Set up the locale info.
939 SetLocale(locale);
940
Tao Baoda409fb2018-10-21 23:36:26 -0700941 error_icon_ = LoadBitmap("icon_error");
Doug Zongker02ec6b82012-08-22 17:26:40 -0700942
Tao Baoda409fb2018-10-21 23:36:26 -0700943 progress_bar_empty_ = LoadBitmap("progress_empty");
944 progress_bar_fill_ = LoadBitmap("progress_fill");
945 stage_marker_empty_ = LoadBitmap("stage_empty");
946 stage_marker_fill_ = LoadBitmap("stage_fill");
Elliott Hughes498cda62016-04-14 16:49:04 -0700947
Tao Baoda409fb2018-10-21 23:36:26 -0700948 erasing_text_ = LoadLocalizedBitmap("erasing_text");
949 no_command_text_ = LoadLocalizedBitmap("no_command_text");
950 error_text_ = LoadLocalizedBitmap("error_text");
Doug Zongker211aebc2011-10-28 15:13:10 -0700951
David Anderson983e2d52019-01-02 11:35:38 -0800952 if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
953 fastbootd_logo_ = LoadBitmap("fastbootd");
954 }
955
Tao Baoda409fb2018-10-21 23:36:26 -0700956 // Background text for "installing_update" could be "installing update" or
957 // "installing security update". It will be set after Init() according to the commands in BCB.
958 installing_text_.reset();
Elliott Hughes498cda62016-04-14 16:49:04 -0700959
Tianjie Xub99e6062018-10-16 15:13:09 -0700960 LoadWipeDataMenuText();
961
Tao Bao736d59c2017-01-03 10:15:33 -0800962 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700963
Tao Bao26ea9592018-05-09 16:32:02 -0700964 // Keep the progress bar updated, even when the process is otherwise busy.
965 progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800966
fredchiouf2f5aa22022-02-11 16:39:53 +0800967 // set the callback for hall sensor event
968 (void)ev_sync_sw_state([this](auto&& a, auto&& b) { return this->SetSwCallback(a, b);});
969
Tao Bao736d59c2017-01-03 10:15:33 -0800970 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700971}
972
Tao Bao551d2c32018-05-09 20:53:13 -0700973std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700974 return locale_;
975}
976
Elliott Hughes498cda62016-04-14 16:49:04 -0700977void ScreenRecoveryUI::LoadAnimation() {
Tao Bao6cd81682018-05-03 21:53:11 -0700978 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
979 closedir);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700980 dirent* de;
981 std::vector<std::string> intro_frame_names;
Joshua Lambert94a83be2019-12-09 20:24:18 +0000982 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700983
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700984 while ((de = readdir(dir.get())) != nullptr) {
985 int value, num_chars;
986 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
987 intro_frame_names.emplace_back(de->d_name, num_chars);
988 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
Joshua Lambert94a83be2019-12-09 20:24:18 +0000989 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700990 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700991 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700992
Tao Baoda409fb2018-10-21 23:36:26 -0700993 size_t intro_frames = intro_frame_names.size();
994 size_t loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700995
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700996 // It's okay to not have an intro.
Tao Baoda409fb2018-10-21 23:36:26 -0700997 if (intro_frames == 0) intro_done_ = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700998 // But you must have an animation.
999 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -07001000
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001001 std::sort(intro_frame_names.begin(), intro_frame_names.end());
1002 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -07001003
Tao Baoda409fb2018-10-21 23:36:26 -07001004 intro_frames_.clear();
1005 intro_frames_.reserve(intro_frames);
1006 for (const auto& frame_name : intro_frame_names) {
1007 intro_frames_.emplace_back(LoadBitmap(frame_name));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001008 }
Elliott Hughes498cda62016-04-14 16:49:04 -07001009
Tao Baoda409fb2018-10-21 23:36:26 -07001010 loop_frames_.clear();
1011 loop_frames_.reserve(loop_frames);
1012 for (const auto& frame_name : loop_frame_names) {
1013 loop_frames_.emplace_back(LoadBitmap(frame_name));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001014 }
Elliott Hughes498cda62016-04-14 16:49:04 -07001015}
1016
Elliott Hughes8de52072015-04-08 20:06:50 -07001017void ScreenRecoveryUI::SetBackground(Icon icon) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001018 std::lock_guard<std::mutex> lg(updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001019
Tao Baoda409fb2018-10-21 23:36:26 -07001020 current_icon_ = icon;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001021 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001022}
1023
Elliott Hughes8de52072015-04-08 20:06:50 -07001024void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001025 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001026 if (progressBarType != type) {
1027 progressBarType = type;
1028 }
1029 progressScopeStart = 0;
1030 progressScopeSize = 0;
1031 progress = 0;
1032 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001033}
1034
Elliott Hughes8de52072015-04-08 20:06:50 -07001035void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001036 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001037 progressBarType = DETERMINATE;
1038 progressScopeStart += progressScopeSize;
1039 progressScopeSize = portion;
1040 progressScopeTime = now();
1041 progressScopeDuration = seconds;
1042 progress = 0;
1043 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001044}
1045
Elliott Hughes8de52072015-04-08 20:06:50 -07001046void ScreenRecoveryUI::SetProgress(float fraction) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001047 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001048 if (fraction < 0.0) fraction = 0.0;
1049 if (fraction > 1.0) fraction = 1.0;
1050 if (progressBarType == DETERMINATE && fraction > progress) {
1051 // Skip updates that aren't visibly different.
Tao Baoda409fb2018-10-21 23:36:26 -07001052 int width = gr_get_width(progress_bar_empty_.get());
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001053 float scale = width * progressScopeSize;
1054 if ((int)(progress * scale) != (int)(fraction * scale)) {
1055 progress = fraction;
1056 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001057 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001058 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001059}
1060
Doug Zongkerc87bab12013-11-25 13:53:25 -08001061void ScreenRecoveryUI::SetStage(int current, int max) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001062 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001063 stage = current;
1064 max_stage = max;
Doug Zongkerc87bab12013-11-25 13:53:25 -08001065}
1066
Tao Baob6918c72015-05-19 17:02:16 -07001067void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001068 std::string str;
1069 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -07001070
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001071 if (copy_to_stdout) {
1072 fputs(str.c_str(), stdout);
1073 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001074
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001075 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001076 if (text_rows_ > 0 && text_cols_ > 0) {
1077 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
1078 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -07001079 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001080 text_col_ = 0;
1081 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001082 }
1083 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001084 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001085 text_[text_row_][text_col_] = '\0';
1086 update_screen_locked();
1087 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001088}
1089
Tao Baob6918c72015-05-19 17:02:16 -07001090void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001091 va_list ap;
1092 va_start(ap, fmt);
1093 PrintV(fmt, true, ap);
1094 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001095}
1096
Tianjie Xu8f397302018-08-20 13:40:47 -07001097void ScreenRecoveryUI::PrintOnScreenOnly(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001098 va_list ap;
1099 va_start(ap, fmt);
1100 PrintV(fmt, false, ap);
1101 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -07001102}
1103
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001104void ScreenRecoveryUI::PutChar(char ch) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001105 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001106 if (ch != '\n') text_[text_row_][text_col_++] = ch;
1107 if (ch == '\n' || text_col_ >= text_cols_) {
1108 text_col_ = 0;
1109 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001110 }
Elliott Hughes8de52072015-04-08 20:06:50 -07001111}
1112
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001113void ScreenRecoveryUI::ClearText() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001114 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001115 text_col_ = 0;
1116 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001117 for (size_t i = 0; i < text_rows_; ++i) {
1118 memset(text_[i], 0, text_cols_ + 1);
1119 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001120}
1121
1122void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001123 std::vector<off_t> offsets;
1124 offsets.push_back(ftello(fp));
1125 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001126
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001127 struct stat sb;
1128 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001129
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001130 bool show_prompt = false;
1131 while (true) {
1132 if (show_prompt) {
1133 PrintOnScreenOnly("--(%d%% of %d bytes)--",
1134 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
1135 static_cast<int>(sb.st_size));
1136 Redraw();
1137 while (show_prompt) {
1138 show_prompt = false;
1139 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001140 if (key == static_cast<int>(KeyError::INTERRUPTED)) return;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001141 if (key == KEY_POWER || key == KEY_ENTER) {
1142 return;
1143 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
1144 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001145 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001146 } else {
1147 offsets.pop_back();
1148 fseek(fp, offsets.back(), SEEK_SET);
1149 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001150 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001151 if (feof(fp)) {
1152 return;
1153 }
1154 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001155 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001156 }
1157 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001158 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001159
1160 int ch = getc(fp);
1161 if (ch == EOF) {
1162 while (text_row_ < text_rows_ - 1) PutChar('\n');
1163 show_prompt = true;
1164 } else {
1165 PutChar(ch);
1166 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
1167 show_prompt = true;
1168 }
1169 }
1170 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -07001171}
1172
Tao Bao1d156b92018-05-02 12:43:18 -07001173void ScreenRecoveryUI::ShowFile(const std::string& filename) {
1174 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
1175 if (!fp) {
1176 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001177 return;
1178 }
Elliott Hughesc0491632015-05-06 12:40:05 -07001179
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001180 char** old_text = text_;
1181 size_t old_text_col = text_col_;
1182 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -07001183
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001184 // Swap in the alternate screen and clear it.
1185 text_ = file_viewer_text_;
1186 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -07001187
Tao Bao1d156b92018-05-02 12:43:18 -07001188 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -07001189
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001190 text_ = old_text;
1191 text_col_ = old_text_col;
1192 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -07001193}
1194
Tao Baoda409fb2018-10-21 23:36:26 -07001195std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(
1196 const GRSurface* graphic_header, const std::vector<const GRSurface*>& graphic_items,
1197 const std::vector<std::string>& text_headers, const std::vector<std::string>& text_items,
1198 size_t initial_selection) const {
Tianjie Xub99e6062018-10-16 15:13:09 -07001199 // horizontal unusable area: margin width + menu indent
1200 size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent;
1201 // vertical unusable area: margin height + title lines + helper message + high light bar.
1202 // It is safe to reserve more space.
1203 size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3);
1204 if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) {
1205 return std::make_unique<GraphicMenu>(graphic_header, graphic_items, initial_selection, *this);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001206 }
Tianjie Xub99e6062018-10-16 15:13:09 -07001207
1208 fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n");
1209
1210 return CreateMenu(text_headers, text_items, initial_selection);
1211}
1212
1213std::unique_ptr<Menu> ScreenRecoveryUI::CreateMenu(const std::vector<std::string>& text_headers,
1214 const std::vector<std::string>& text_items,
1215 size_t initial_selection) const {
1216 if (text_rows_ > 0 && text_cols_ > 1) {
1217 return std::make_unique<TextMenu>(scrollable_menu_, text_rows_, text_cols_ - 1, text_headers,
1218 text_items, initial_selection, char_height_, *this);
1219 }
1220
1221 fprintf(stderr, "Failed to create text menu, text_rows %zu, text_cols %zu.\n", text_rows_,
1222 text_cols_);
1223 return nullptr;
Doug Zongker211aebc2011-10-28 15:13:10 -07001224}
1225
1226int ScreenRecoveryUI::SelectMenu(int sel) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001227 std::lock_guard<std::mutex> lg(updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001228 if (menu_) {
1229 int old_sel = menu_->selection();
1230 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -07001231
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001232 if (sel != old_sel) {
1233 update_screen_locked();
1234 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001235 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001236 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001237}
1238
Tianjie Xub99e6062018-10-16 15:13:09 -07001239size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr<Menu>&& menu, bool menu_only,
Tao Bao1fe1afe2018-05-01 15:56:05 -07001240 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001241 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1242 FlushKeys();
1243
Jerry Zhangb76af932018-05-22 12:08:35 -07001244 // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the
1245 // menu.
1246 if (IsKeyInterrupted()) return static_cast<size_t>(KeyError::INTERRUPTED);
1247
Tianjie Xub99e6062018-10-16 15:13:09 -07001248 CHECK(menu != nullptr);
Tao Bao3aec6962018-04-20 09:24:58 -07001249
Tianjie Xub99e6062018-10-16 15:13:09 -07001250 // Starts and displays the menu
1251 menu_ = std::move(menu);
1252 Redraw();
1253
1254 int selected = menu_->selection();
Tao Bao3aec6962018-04-20 09:24:58 -07001255 int chosen_item = -1;
1256 while (chosen_item < 0) {
1257 int key = WaitKey();
Jerry Zhangb76af932018-05-22 12:08:35 -07001258 if (key == static_cast<int>(KeyError::INTERRUPTED)) { // WaitKey() was interrupted.
1259 return static_cast<size_t>(KeyError::INTERRUPTED);
1260 }
1261 if (key == static_cast<int>(KeyError::TIMED_OUT)) { // WaitKey() timed out.
Tao Bao3aec6962018-04-20 09:24:58 -07001262 if (WasTextEverVisible()) {
1263 continue;
1264 } else {
1265 LOG(INFO) << "Timed out waiting for key input; rebooting.";
Tianjie Xub99e6062018-10-16 15:13:09 -07001266 menu_.reset();
1267 Redraw();
Jerry Zhangb76af932018-05-22 12:08:35 -07001268 return static_cast<size_t>(KeyError::TIMED_OUT);
Tao Bao3aec6962018-04-20 09:24:58 -07001269 }
1270 }
1271
1272 bool visible = IsTextVisible();
1273 int action = key_handler(key, visible);
1274 if (action < 0) {
1275 switch (action) {
1276 case Device::kHighlightUp:
1277 selected = SelectMenu(--selected);
1278 break;
1279 case Device::kHighlightDown:
1280 selected = SelectMenu(++selected);
1281 break;
1282 case Device::kInvokeItem:
1283 chosen_item = selected;
1284 break;
1285 case Device::kNoAction:
1286 break;
Tom Marshallf20b3e32017-08-24 13:50:01 +00001287 case Device::kGoBack:
1288 chosen_item = Device::kGoBack;
1289 break;
1290 case Device::kGoHome:
1291 chosen_item = Device::kGoHome;
1292 break;
Tao Bao3aec6962018-04-20 09:24:58 -07001293 }
1294 } else if (!menu_only) {
1295 chosen_item = action;
1296 }
Tom Marshallf20b3e32017-08-24 13:50:01 +00001297 if (chosen_item == Device::kGoBack || chosen_item == Device::kGoHome) {
1298 break;
1299 }
Tao Bao3aec6962018-04-20 09:24:58 -07001300 }
1301
Tianjie Xub99e6062018-10-16 15:13:09 -07001302 menu_.reset();
1303 Redraw();
1304
Tao Bao3aec6962018-04-20 09:24:58 -07001305 return chosen_item;
1306}
1307
Tianjie Xub99e6062018-10-16 15:13:09 -07001308size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1309 const std::vector<std::string>& items, size_t initial_selection,
1310 bool menu_only,
1311 const std::function<int(int, bool)>& key_handler) {
1312 auto menu = CreateMenu(headers, items, initial_selection);
1313 if (menu == nullptr) {
1314 return initial_selection;
1315 }
1316
Tao Baofa8e02a2019-06-27 09:07:04 -07001317 return ShowMenu(std::move(menu), menu_only, key_handler);
Tianjie Xub99e6062018-10-16 15:13:09 -07001318}
1319
1320size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector<std::string>& backup_headers,
1321 const std::vector<std::string>& backup_items,
1322 const std::function<int(int, bool)>& key_handler) {
Tao Baoda409fb2018-10-21 23:36:26 -07001323 auto wipe_data_menu = CreateMenu(wipe_data_menu_header_text_.get(),
1324 { try_again_text_.get(), factory_data_reset_text_.get() },
1325 backup_headers, backup_items, 0);
Tianjie Xub99e6062018-10-16 15:13:09 -07001326 if (wipe_data_menu == nullptr) {
1327 return 0;
1328 }
1329
1330 return ShowMenu(std::move(wipe_data_menu), true, key_handler);
1331}
1332
Tianjie Xu1a0a30a2018-10-25 15:22:07 -07001333size_t ScreenRecoveryUI::ShowPromptWipeDataConfirmationMenu(
1334 const std::vector<std::string>& backup_headers, const std::vector<std::string>& backup_items,
1335 const std::function<int(int, bool)>& key_handler) {
1336 auto confirmation_menu =
1337 CreateMenu(wipe_data_confirmation_text_.get(),
1338 { cancel_wipe_data_text_.get(), factory_data_reset_text_.get() }, backup_headers,
1339 backup_items, 0);
1340 if (confirmation_menu == nullptr) {
1341 return 0;
1342 }
1343
1344 return ShowMenu(std::move(confirmation_menu), true, key_handler);
1345}
1346
Elliott Hughes8de52072015-04-08 20:06:50 -07001347bool ScreenRecoveryUI::IsTextVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001348 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001349 int visible = show_text;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001350 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001351}
1352
Elliott Hughes8de52072015-04-08 20:06:50 -07001353bool ScreenRecoveryUI::WasTextEverVisible() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001354 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001355 int ever_visible = show_text_ever;
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001356 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001357}
1358
Elliott Hughes8de52072015-04-08 20:06:50 -07001359void ScreenRecoveryUI::ShowText(bool visible) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001360 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001361 show_text = visible;
1362 if (show_text) show_text_ever = true;
1363 update_screen_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -07001364}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001365
Elliott Hughes8de52072015-04-08 20:06:50 -07001366void ScreenRecoveryUI::Redraw() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -07001367 std::lock_guard<std::mutex> lg(updateMutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001368 update_screen_locked();
Doug Zongkerc0441d12013-07-31 11:28:24 -07001369}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001370
1371void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001372 // Redraw so that if we're in the menu, the highlight
1373 // will change color to indicate a successful long press.
1374 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001375}
Tao Baoefb49ad2017-01-31 23:03:10 -08001376
1377void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1378 locale_ = new_locale;
1379 rtl_locale_ = false;
1380
1381 if (!new_locale.empty()) {
Tao Bao347a6592018-05-08 15:58:29 -07001382 size_t separator = new_locale.find('-');
1383 // lang has the language prefix prior to the separator, or full string if none exists.
1384 std::string lang = new_locale.substr(0, separator);
Tao Baoefb49ad2017-01-31 23:03:10 -08001385
1386 // A bit cheesy: keep an explicit list of supported RTL languages.
1387 if (lang == "ar" || // Arabic
1388 lang == "fa" || // Persian (Farsi)
1389 lang == "he" || // Hebrew (new language code)
1390 lang == "iw" || // Hebrew (old language code)
1391 lang == "ur") { // Urdu
1392 rtl_locale_ = true;
1393 }
1394 }
1395}
fredchiouf2f5aa22022-02-11 16:39:53 +08001396
1397int ScreenRecoveryUI::SetSwCallback(int code, int value) {
1398 if (!is_graphics_available) { return -1; }
1399 if (code > SW_MAX) { return -1; }
1400 if (code != SW_LID) { return 0; }
1401
1402 /* detect dual display */
1403 if (!gr_has_multiple_connectors()) { return -1; }
1404
1405 /* turn off all screen */
1406 gr_fb_blank(true, DirectRenderManager::DRM_INNER);
1407 gr_fb_blank(true, DirectRenderManager::DRM_OUTER);
1408 gr_color(0, 0, 0, 255);
1409 gr_clear();
1410
1411 /* turn on the screen */
1412 gr_fb_blank(false, value);
1413 gr_flip();
1414
1415 /* set the retation */
1416 std::string rotation_str;
1417 if (value == DirectRenderManager::DRM_OUTER) {
1418 rotation_str =
1419 android::base::GetProperty("ro.minui.second_rotation", "ROTATION_NONE");
1420 } else {
1421 rotation_str =
1422 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
1423 }
1424
1425 if (rotation_str == "ROTATION_RIGHT") {
1426 gr_rotate(GRRotation::RIGHT);
1427 } else if (rotation_str == "ROTATION_DOWN") {
1428 gr_rotate(GRRotation::DOWN);
1429 } else if (rotation_str == "ROTATION_LEFT") {
1430 gr_rotate(GRRotation::LEFT);
1431 } else { // "ROTATION_NONE" or unknown string
1432 gr_rotate(GRRotation::NONE);
1433 }
1434 Redraw();
1435
1436 return 0;
1437}