blob: f5dadf7f005fe0ee24b4da01938a36786d7817da [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Baoefb49ad2017-01-31 23:03:10 -080017#include "screen_ui.h"
18
Elliott Hughes498cda62016-04-14 16:49:04 -070019#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070020#include <errno.h>
21#include <fcntl.h>
22#include <linux/input.h>
23#include <pthread.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/types.h>
31#include <time.h>
32#include <unistd.h>
33
Tao Bao1fe1afe2018-05-01 15:56:05 -070034#include <algorithm>
Tianjie Xu29d55752017-09-20 17:53:46 -070035#include <memory>
Tao Bao736d59c2017-01-03 10:15:33 -080036#include <string>
Tianjie Xu29d55752017-09-20 17:53:46 -070037#include <unordered_map>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070038#include <vector>
39
Tianjie Xuc21edd42016-08-05 18:00:04 -070040#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070041#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/stringprintf.h>
Tao Baocb5524c2017-09-08 21:25:32 -070043#include <android-base/strings.h>
Tao Baoefb49ad2017-01-31 23:03:10 -080044#include <minui/minui.h>
Tao Baob6918c72015-05-19 17:02:16 -070045
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070046#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070047#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070048
Doug Zongker211aebc2011-10-28 15:13:10 -070049// Return the current time as a double (including fractions of a second).
50static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070051 struct timeval tv;
52 gettimeofday(&tv, nullptr);
53 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070054}
55
Tao Bao1fe1afe2018-05-01 15:56:05 -070056Menu::Menu(bool scrollable, size_t max_items, size_t max_length,
57 const std::vector<std::string>& headers, const std::vector<std::string>& items,
58 size_t initial_selection)
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070059 : scrollable_(scrollable),
60 max_display_items_(max_items),
61 max_item_length_(max_length),
Tao Baoe02a5b22018-05-02 15:46:11 -070062 text_headers_(headers),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070063 menu_start_(0),
Tao Baoe02a5b22018-05-02 15:46:11 -070064 selection_(initial_selection) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070065 CHECK_LE(max_items, static_cast<size_t>(std::numeric_limits<int>::max()));
Tao Baoe02a5b22018-05-02 15:46:11 -070066
67 // It's fine to have more entries than text_rows_ if scrollable menu is supported.
Tao Bao1fe1afe2018-05-01 15:56:05 -070068 size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_);
69 for (size_t i = 0; i < items_count; ++i) {
70 text_items_.emplace_back(items[i].substr(0, max_item_length_));
Tao Baoe02a5b22018-05-02 15:46:11 -070071 }
72
73 CHECK(!text_items_.empty());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070074}
75
Tao Bao1fe1afe2018-05-01 15:56:05 -070076const std::vector<std::string>& Menu::text_headers() const {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070077 return text_headers_;
78}
79
80std::string Menu::TextItem(size_t index) const {
81 CHECK_LT(index, text_items_.size());
82
83 return text_items_[index];
84}
85
86size_t Menu::MenuStart() const {
87 return menu_start_;
88}
89
90size_t Menu::MenuEnd() const {
91 return std::min(ItemsCount(), menu_start_ + max_display_items_);
92}
93
94size_t Menu::ItemsCount() const {
95 return text_items_.size();
96}
97
98bool Menu::ItemsOverflow(std::string* cur_selection_str) const {
Tao Baoe02a5b22018-05-02 15:46:11 -070099 if (!scrollable_ || ItemsCount() <= max_display_items_) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700100 return false;
101 }
102
103 *cur_selection_str =
Tao Bao1fe1afe2018-05-01 15:56:05 -0700104 android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700105 return true;
106}
107
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700108// TODO(xunchang) modify the function parameters to button up & down.
109int Menu::Select(int sel) {
110 CHECK_LE(ItemsCount(), static_cast<size_t>(std::numeric_limits<int>::max()));
111 int count = ItemsCount();
112
113 // Wraps the selection at boundary if the menu is not scrollable.
114 if (!scrollable_) {
115 if (sel < 0) {
116 selection_ = count - 1;
117 } else if (sel >= count) {
118 selection_ = 0;
119 } else {
120 selection_ = sel;
121 }
122
123 return selection_;
124 }
125
126 if (sel < 0) {
127 selection_ = 0;
128 } else if (sel >= count) {
129 selection_ = count - 1;
130 } else {
131 if (static_cast<size_t>(sel) < menu_start_) {
132 menu_start_--;
133 } else if (static_cast<size_t>(sel) >= MenuEnd()) {
134 menu_start_++;
135 }
136 selection_ = sel;
137 }
138
139 return selection_;
140}
141
142ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {}
143
144ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu)
Tao Bao4521b702017-06-20 18:11:21 -0700145 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
146 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
Tao Bao0470cee2017-08-02 17:11:04 -0700147 kAnimationFps(RECOVERY_UI_ANIMATION_FPS),
Tao Bao75779652017-09-10 11:28:32 -0700148 kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao171b4c42017-06-19 23:10:44 -0700149 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -0800150 progressBarType(EMPTY),
151 progressScopeStart(0),
152 progressScopeSize(0),
153 progress(0),
154 pagesIdentical(false),
155 text_cols_(0),
156 text_rows_(0),
157 text_(nullptr),
158 text_col_(0),
159 text_row_(0),
Tao Bao736d59c2017-01-03 10:15:33 -0800160 show_text(false),
161 show_text_ever(false),
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700162 scrollable_menu_(scrollable_menu),
Tao Bao736d59c2017-01-03 10:15:33 -0800163 file_viewer_text_(nullptr),
164 intro_frames(0),
165 loop_frames(0),
166 current_frame(0),
167 intro_done(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800168 stage(-1),
169 max_stage(-1),
Tao Baoefb49ad2017-01-31 23:03:10 -0800170 locale_(""),
171 rtl_locale_(false),
Tao Bao736d59c2017-01-03 10:15:33 -0800172 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -0700173
Tao Bao99b2d772017-06-23 22:47:03 -0700174GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700175 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
176 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
177 }
178 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -0700179}
180
Tao Bao99b2d772017-06-23 22:47:03 -0700181GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700182 switch (currentIcon) {
183 case ERASING:
184 return erasing_text;
185 case ERROR:
186 return error_text;
187 case INSTALLING_UPDATE:
188 return installing_text;
189 case NO_COMMAND:
190 return no_command_text;
191 case NONE:
192 abort();
193 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700194}
195
Mikhail Lappob49767c2017-03-23 21:44:26 +0100196int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao75779652017-09-10 11:28:32 -0700197 return dp * kDensity;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700198}
199
200// Here's the intended layout:
201
Elliott Hughes6d089a92016-07-08 17:23:41 -0700202// | portrait large landscape large
203// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700204// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700205// icon | (200dp)
206// gap | 68dp 68dp 56dp 112dp
207// text | (14sp)
208// gap | 32dp 32dp 26dp 52dp
209// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700210// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700211
Tao Bao3250f722017-06-29 14:32:05 -0700212// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
213// 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 -0700214
Elliott Hughes6d089a92016-07-08 17:23:41 -0700215enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700216enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700217static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700218 { 32, 68, }, // PORTRAIT
219 { 32, 68, }, // PORTRAIT_LARGE
220 { 26, 56, }, // LANDSCAPE
221 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700222};
223
Tao Bao99b2d772017-06-23 22:47:03 -0700224int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700225 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700226}
227
Tao Bao99b2d772017-06-23 22:47:03 -0700228int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700229 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
230 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700231}
232
Tao Bao99b2d772017-06-23 22:47:03 -0700233int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700234 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
235 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
236 gr_get_height(progressBarFill);
Luke Song92eda4d2017-09-19 10:51:35 -0700237 int bottom_gap = (ScreenHeight() - elements_sum) / 2;
238 return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700239}
240
Doug Zongker211aebc2011-10-28 15:13:10 -0700241// Clear the screen and draw the currently selected background icon (if any).
242// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700243void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700244 pagesIdentical = false;
245 gr_color(0, 0, 0, 255);
246 gr_clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700247 if (currentIcon != NONE) {
248 if (max_stage != -1) {
249 int stage_height = gr_get_height(stageMarkerEmpty);
250 int stage_width = gr_get_width(stageMarkerEmpty);
Luke Song92eda4d2017-09-19 10:51:35 -0700251 int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
252 int y = ScreenHeight() - stage_height - kMarginHeight;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700253 for (int i = 0; i < max_stage; ++i) {
254 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
Luke Song92eda4d2017-09-19 10:51:35 -0700255 DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700256 x += stage_width;
257 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700258 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700259
260 GRSurface* text_surface = GetCurrentText();
Luke Song92eda4d2017-09-19 10:51:35 -0700261 int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700262 int text_y = GetTextBaseline();
263 gr_color(255, 255, 255, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700264 DrawTextIcon(text_x, text_y, text_surface);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700265 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700266}
267
Tao Baoea78d862017-06-28 14:52:17 -0700268// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be
269// called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700270void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800271 if (currentIcon != NONE) {
272 GRSurface* frame = GetCurrentFrame();
273 int frame_width = gr_get_width(frame);
274 int frame_height = gr_get_height(frame);
Luke Song92eda4d2017-09-19 10:51:35 -0700275 int frame_x = (ScreenWidth() - frame_width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800276 int frame_y = GetAnimationBaseline();
Luke Song92eda4d2017-09-19 10:51:35 -0700277 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800278 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700279
Tao Bao736d59c2017-01-03 10:15:33 -0800280 if (progressBarType != EMPTY) {
281 int width = gr_get_width(progressBarEmpty);
282 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700283
Luke Song92eda4d2017-09-19 10:51:35 -0700284 int progress_x = (ScreenWidth() - width) / 2;
Tao Bao736d59c2017-01-03 10:15:33 -0800285 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700286
Tao Bao736d59c2017-01-03 10:15:33 -0800287 // Erase behind the progress bar (in case this was a progress-only update)
288 gr_color(0, 0, 0, 255);
Luke Song92eda4d2017-09-19 10:51:35 -0700289 DrawFill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700290
Tao Bao736d59c2017-01-03 10:15:33 -0800291 if (progressBarType == DETERMINATE) {
292 float p = progressScopeStart + progress * progressScopeSize;
293 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700294
Tao Bao736d59c2017-01-03 10:15:33 -0800295 if (rtl_locale_) {
296 // Fill the progress bar from right to left.
297 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700298 DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
299 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700300 }
Tao Bao736d59c2017-01-03 10:15:33 -0800301 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700302 DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800303 }
304 } else {
305 // Fill the progress bar from left to right.
306 if (pos > 0) {
Luke Song92eda4d2017-09-19 10:51:35 -0700307 DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800308 }
309 if (pos < width - 1) {
Luke Song92eda4d2017-09-19 10:51:35 -0700310 DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
Tao Bao736d59c2017-01-03 10:15:33 -0800311 }
312 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700313 }
Tao Bao736d59c2017-01-03 10:15:33 -0800314 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700315}
316
Tao Bao99b2d772017-06-23 22:47:03 -0700317void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700318 switch (e) {
319 case INFO:
320 gr_color(249, 194, 0, 255);
321 break;
322 case HEADER:
323 gr_color(247, 0, 6, 255);
324 break;
325 case MENU:
326 case MENU_SEL_BG:
327 gr_color(0, 106, 157, 255);
328 break;
329 case MENU_SEL_BG_ACTIVE:
330 gr_color(0, 156, 100, 255);
331 break;
332 case MENU_SEL_FG:
333 gr_color(255, 255, 255, 255);
334 break;
335 case LOG:
336 gr_color(196, 196, 196, 255);
337 break;
338 case TEXT_FILL:
339 gr_color(0, 0, 0, 160);
340 break;
341 default:
342 gr_color(255, 255, 255, 255);
343 break;
344 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700345}
Doug Zongker211aebc2011-10-28 15:13:10 -0700346
Tianjie Xu29d55752017-09-20 17:53:46 -0700347void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries,
348 size_t sel) {
349 SetLocale(locales_entries[sel]);
350 std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text",
351 "installing_security_text", "no_command_text" };
352 std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces;
353 for (const auto& name : text_name) {
354 GRSurface* text_image = nullptr;
355 LoadLocalizedBitmap(name.c_str(), &text_image);
356 if (!text_image) {
357 Print("Failed to load %s\n", name.c_str());
358 return;
359 }
360 surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free));
361 }
362
363 pthread_mutex_lock(&updateMutex);
364 gr_color(0, 0, 0, 255);
365 gr_clear();
366
367 int text_y = kMarginHeight;
368 int text_x = kMarginWidth;
369 int line_spacing = gr_sys_font()->char_height; // Put some extra space between images.
370 // Write the header and descriptive texts.
371 SetColor(INFO);
372 std::string header = "Show background text image";
Tao Bao93e46ad2018-05-02 14:57:21 -0700373 text_y += DrawTextLine(text_x, text_y, header, true);
Tianjie Xu29d55752017-09-20 17:53:46 -0700374 std::string locale_selection = android::base::StringPrintf(
Tao Bao39c49182018-05-07 22:50:33 -0700375 "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size());
Tao Bao93e46ad2018-05-02 14:57:21 -0700376 // clang-format off
377 std::vector<std::string> instruction = {
378 locale_selection,
379 "Use volume up/down to switch locales and power to exit."
380 };
381 // clang-format on
Tianjie Xu29d55752017-09-20 17:53:46 -0700382 text_y += DrawWrappedTextLines(text_x, text_y, instruction);
383
384 // Iterate through the text images and display them in order for the current locale.
385 for (const auto& p : surfaces) {
386 text_y += line_spacing;
387 SetColor(LOG);
Tao Bao93e46ad2018-05-02 14:57:21 -0700388 text_y += DrawTextLine(text_x, text_y, p.first, false);
Tianjie Xu29d55752017-09-20 17:53:46 -0700389 gr_color(255, 255, 255, 255);
390 gr_texticon(text_x, text_y, p.second.get());
391 text_y += gr_get_height(p.second.get());
392 }
393 // Update the whole screen.
394 gr_flip();
395 pthread_mutex_unlock(&updateMutex);
396}
397
Tao Bao39c49182018-05-07 22:50:33 -0700398void ScreenRecoveryUI::CheckBackgroundTextImages() {
Tianjie Xu29d55752017-09-20 17:53:46 -0700399 // Load a list of locales embedded in one of the resource files.
400 std::vector<std::string> locales_entries = get_locales_in_png("installing_text");
401 if (locales_entries.empty()) {
402 Print("Failed to load locales from the resource files\n");
403 return;
404 }
Tao Bao39c49182018-05-07 22:50:33 -0700405 std::string saved_locale = locale_;
Tianjie Xu29d55752017-09-20 17:53:46 -0700406 size_t selected = 0;
407 SelectAndShowBackgroundText(locales_entries, selected);
408
409 FlushKeys();
410 while (true) {
411 int key = WaitKey();
412 if (key == KEY_POWER || key == KEY_ENTER) {
413 break;
414 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
415 selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1;
416 SelectAndShowBackgroundText(locales_entries, selected);
417 } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) {
418 selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1;
419 SelectAndShowBackgroundText(locales_entries, selected);
420 }
421 }
422
423 SetLocale(saved_locale);
424}
425
Luke Song92eda4d2017-09-19 10:51:35 -0700426int ScreenRecoveryUI::ScreenWidth() const {
427 return gr_fb_width();
428}
429
430int ScreenRecoveryUI::ScreenHeight() const {
431 return gr_fb_height();
432}
433
434void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
435 int dy) const {
436 gr_blit(surface, sx, sy, w, h, dx, dy);
437}
438
Tao Baoea78d862017-06-28 14:52:17 -0700439int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
Luke Song92eda4d2017-09-19 10:51:35 -0700440 gr_fill(0, y + 4, ScreenWidth(), y + 6);
Tao Baoea78d862017-06-28 14:52:17 -0700441 return 8;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700442}
443
Luke Songe2bd8762017-06-12 16:08:33 -0700444void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700445 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700446}
447
Luke Song92eda4d2017-09-19 10:51:35 -0700448void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
449 gr_fill(x, y, w, h);
450}
451
452void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
453 gr_texticon(x, y, surface);
454}
455
Tao Bao93e46ad2018-05-02 14:57:21 -0700456int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const {
457 gr_text(gr_sys_font(), x, y, line.c_str(), bold);
Tao Baoea78d862017-06-28 14:52:17 -0700458 return char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700459}
460
Tao Bao93e46ad2018-05-02 14:57:21 -0700461int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector<std::string>& lines) const {
Tao Baoea78d862017-06-28 14:52:17 -0700462 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700463 for (const auto& line : lines) {
464 offset += DrawTextLine(x, y + offset, line, false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700465 }
Tao Baoea78d862017-06-28 14:52:17 -0700466 return offset;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700467}
468
Tao Bao93e46ad2018-05-02 14:57:21 -0700469int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y,
470 const std::vector<std::string>& lines) const {
Tao Bao452b4872018-05-09 11:52:09 -0700471 // Keep symmetrical margins based on the given offset (i.e. x).
472 size_t text_cols = (ScreenWidth() - x * 2) / char_width_;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700473 int offset = 0;
Tao Bao93e46ad2018-05-02 14:57:21 -0700474 for (const auto& line : lines) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700475 size_t next_start = 0;
476 while (next_start < line.size()) {
Tao Bao452b4872018-05-09 11:52:09 -0700477 std::string sub = line.substr(next_start, text_cols + 1);
478 if (sub.size() <= text_cols) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700479 next_start += sub.size();
480 } else {
Tao Bao452b4872018-05-09 11:52:09 -0700481 // Line too long and must be wrapped to text_cols columns.
Tao Bao2bbc6d62017-08-13 23:48:55 -0700482 size_t last_space = sub.find_last_of(" \t\n");
483 if (last_space == std::string::npos) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700484 // No space found, just draw as much as we can.
Tao Bao452b4872018-05-09 11:52:09 -0700485 sub.resize(text_cols);
486 next_start += text_cols;
Tao Bao2bbc6d62017-08-13 23:48:55 -0700487 } else {
488 sub.resize(last_space);
489 next_start += last_space + 1;
490 }
491 }
Tao Bao93e46ad2018-05-02 14:57:21 -0700492 offset += DrawTextLine(x, y + offset, sub, false);
Tao Bao2bbc6d62017-08-13 23:48:55 -0700493 }
494 }
495 return offset;
496}
497
Tao Bao171b4c42017-06-19 23:10:44 -0700498// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
499// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700500void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700501 if (!show_text) {
502 draw_background_locked();
503 draw_foreground_locked();
504 return;
505 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700506
Tao Bao171b4c42017-06-19 23:10:44 -0700507 gr_color(0, 0, 0, 255);
508 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700509
Tao Bao93e46ad2018-05-02 14:57:21 -0700510 // clang-format off
Tao Bao1fe1afe2018-05-01 15:56:05 -0700511 static std::vector<std::string> REGULAR_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700512 "Use volume up/down and power.",
513 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700514 static std::vector<std::string> LONG_PRESS_HELP{
Tao Bao93e46ad2018-05-02 14:57:21 -0700515 "Any button cycles highlight.",
516 "Long-press activates.",
517 };
518 // clang-format on
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700519 draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
520}
521
522// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked.
Tao Bao93e46ad2018-05-02 14:57:21 -0700523void ScreenRecoveryUI::draw_menu_and_text_buffer_locked(
524 const std::vector<std::string>& help_message) {
Tao Bao4521b702017-06-20 18:11:21 -0700525 int y = kMarginHeight;
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700526 if (menu_) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700527 static constexpr int kMenuIndent = 4;
528 int x = kMarginWidth + kMenuIndent;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700529
Tao Bao171b4c42017-06-19 23:10:44 -0700530 SetColor(INFO);
Tao Baoea78d862017-06-28 14:52:17 -0700531 y += DrawTextLine(x, y, "Android Recovery", true);
Tao Baoca6ce2c2017-07-13 11:15:27 -0700532 std::string recovery_fingerprint =
533 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao171b4c42017-06-19 23:10:44 -0700534 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700535 y += DrawTextLine(x, y, chunk, false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700536 }
Tao Bao171b4c42017-06-19 23:10:44 -0700537
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700538 y += DrawTextLines(x, y, help_message);
539
540 // Draw menu header.
Tao Bao171b4c42017-06-19 23:10:44 -0700541 SetColor(HEADER);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700542 if (!menu_->scrollable()) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700543 y += DrawWrappedTextLines(x, y, menu_->text_headers());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700544 } else {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700545 y += DrawTextLines(x, y, menu_->text_headers());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700546 // Show the current menu item number in relation to total number if items don't fit on the
547 // screen.
548 std::string cur_selection_str;
549 if (menu_->ItemsOverflow(&cur_selection_str)) {
Tao Bao93e46ad2018-05-02 14:57:21 -0700550 y += DrawTextLine(x, y, cur_selection_str, true);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700551 }
552 }
Tao Bao171b4c42017-06-19 23:10:44 -0700553
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700554 // Draw menu items.
Tao Bao171b4c42017-06-19 23:10:44 -0700555 SetColor(MENU);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700556 // Do not draw the horizontal rule for wear devices.
557 if (!menu_->scrollable()) {
558 y += DrawHorizontalRule(y) + 4;
559 }
560 for (size_t i = menu_->MenuStart(); i < menu_->MenuEnd(); ++i) {
561 bool bold = false;
562 if (i == static_cast<size_t>(menu_->selection())) {
Tao Bao171b4c42017-06-19 23:10:44 -0700563 // Draw the highlight bar.
564 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700565
566 int bar_height = char_height_ + 4;
567 DrawHighlightBar(0, y - 2, ScreenWidth(), bar_height);
568
Tao Bao171b4c42017-06-19 23:10:44 -0700569 // Bold white text for the selected item.
570 SetColor(MENU_SEL_FG);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700571 bold = true;
Tao Bao171b4c42017-06-19 23:10:44 -0700572 }
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700573
Tao Bao93e46ad2018-05-02 14:57:21 -0700574 y += DrawTextLine(x, y, menu_->TextItem(i), bold);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700575
576 SetColor(MENU);
Tao Bao171b4c42017-06-19 23:10:44 -0700577 }
Tao Baoea78d862017-06-28 14:52:17 -0700578 y += DrawHorizontalRule(y);
Tao Bao171b4c42017-06-19 23:10:44 -0700579 }
580
581 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
582 // we've displayed the entire text buffer.
583 SetColor(LOG);
Tao Baocb5524c2017-09-08 21:25:32 -0700584 int row = text_row_;
Tao Bao171b4c42017-06-19 23:10:44 -0700585 size_t count = 0;
Luke Song92eda4d2017-09-19 10:51:35 -0700586 for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700587 ty -= char_height_, ++count) {
Tao Baoca6ce2c2017-07-13 11:15:27 -0700588 DrawTextLine(kMarginWidth, ty, text_[row], false);
Tao Bao171b4c42017-06-19 23:10:44 -0700589 --row;
590 if (row < 0) row = text_rows_ - 1;
591 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700592}
593
594// Redraw everything on the screen and flip the screen (make it visible).
595// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700596void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700597 draw_screen_locked();
598 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700599}
600
601// Updates only the progress bar, if possible, otherwise redraws the screen.
602// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700603void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700604 if (show_text || !pagesIdentical) {
605 draw_screen_locked(); // Must redraw the whole screen
606 pagesIdentical = true;
607 } else {
608 draw_foreground_locked(); // Draw only the progress bar and overlays
609 }
610 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700611}
612
613// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700614void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700615 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
616 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700617}
618
Elliott Hughes985022a2015-04-13 13:04:32 -0700619void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao0470cee2017-08-02 17:11:04 -0700620 double interval = 1.0 / kAnimationFps;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700621 while (true) {
622 double start = now();
623 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700624
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700625 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700626
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700627 // update the installation animation, if active
628 // skip this if we have a text overlay (too expensive to update)
629 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
630 if (!intro_done) {
631 if (current_frame == intro_frames - 1) {
632 intro_done = true;
633 current_frame = 0;
634 } else {
635 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700636 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700637 } else {
638 current_frame = (current_frame + 1) % loop_frames;
639 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700640
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700641 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700642 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700643
644 // move the progress bar forward on timed intervals, if configured
645 int duration = progressScopeDuration;
646 if (progressBarType == DETERMINATE && duration > 0) {
647 double elapsed = now() - progressScopeTime;
648 float p = 1.0 * elapsed / duration;
649 if (p > 1.0) p = 1.0;
650 if (p > progress) {
651 progress = p;
652 redraw = true;
653 }
654 }
655
656 if (redraw) update_progress_locked();
657
658 pthread_mutex_unlock(&updateMutex);
659 double end = now();
660 // minimum of 20ms delay between frames
661 double delay = interval - (end - start);
662 if (delay < 0.02) delay = 0.02;
663 usleep(static_cast<useconds_t>(delay * 1000000));
664 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700665}
666
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700667void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700668 int result = res_create_display_surface(filename, surface);
669 if (result < 0) {
670 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
671 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800672}
673
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700674void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800675 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
676 if (result < 0) {
677 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
678 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700679}
680
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700681static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700682 char** result = new char*[rows];
683 for (size_t i = 0; i < rows; ++i) {
684 result[i] = new char[cols];
685 memset(result[i], 0, cols);
686 }
687 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700688}
689
Tianjie Xu35926c42016-04-28 18:06:26 -0700690// Choose the right background string to display during update.
691void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700692 if (security_update) {
693 LoadLocalizedBitmap("installing_security_text", &installing_text);
694 } else {
695 LoadLocalizedBitmap("installing_text", &installing_text);
696 }
697 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700698}
699
Sen Jiangd5304492016-12-09 16:20:49 -0800700bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700701 if (gr_init() < 0) {
702 return false;
703 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700704
Tao Bao171b4c42017-06-19 23:10:44 -0700705 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Luke Song92eda4d2017-09-19 10:51:35 -0700706 text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
707 text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700708 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700709}
710
Tao Bao736d59c2017-01-03 10:15:33 -0800711bool ScreenRecoveryUI::Init(const std::string& locale) {
712 RecoveryUI::Init(locale);
Tao Baoefb49ad2017-01-31 23:03:10 -0800713
Tao Bao736d59c2017-01-03 10:15:33 -0800714 if (!InitTextParams()) {
715 return false;
716 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700717
Tao Bao736d59c2017-01-03 10:15:33 -0800718 // Are we portrait or landscape?
719 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
720 // Are we the large variant of our base layout?
721 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700722
Tao Bao736d59c2017-01-03 10:15:33 -0800723 text_ = Alloc2d(text_rows_, text_cols_ + 1);
724 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800725
Tao Bao736d59c2017-01-03 10:15:33 -0800726 text_col_ = text_row_ = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700727
Tao Baoefb49ad2017-01-31 23:03:10 -0800728 // Set up the locale info.
729 SetLocale(locale);
730
Tao Bao736d59c2017-01-03 10:15:33 -0800731 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700732
Tao Bao736d59c2017-01-03 10:15:33 -0800733 LoadBitmap("progress_empty", &progressBarEmpty);
734 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700735
Tao Bao736d59c2017-01-03 10:15:33 -0800736 LoadBitmap("stage_empty", &stageMarkerEmpty);
737 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700738
Tao Bao736d59c2017-01-03 10:15:33 -0800739 // Background text for "installing_update" could be "installing update"
740 // or "installing security update". It will be set after UI init according
741 // to commands in BCB.
742 installing_text = nullptr;
743 LoadLocalizedBitmap("erasing_text", &erasing_text);
744 LoadLocalizedBitmap("no_command_text", &no_command_text);
745 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700746
Tao Bao736d59c2017-01-03 10:15:33 -0800747 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700748
Tao Bao736d59c2017-01-03 10:15:33 -0800749 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800750
Tao Bao736d59c2017-01-03 10:15:33 -0800751 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700752}
753
Tao Bao551d2c32018-05-09 20:53:13 -0700754std::string ScreenRecoveryUI::GetLocale() const {
Jerry Zhang2dea53e2018-05-02 17:15:03 -0700755 return locale_;
756}
757
Elliott Hughes498cda62016-04-14 16:49:04 -0700758void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700759 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
760 dirent* de;
761 std::vector<std::string> intro_frame_names;
762 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700763
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700764 while ((de = readdir(dir.get())) != nullptr) {
765 int value, num_chars;
766 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
767 intro_frame_names.emplace_back(de->d_name, num_chars);
768 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
769 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700770 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700771 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700772
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700773 intro_frames = intro_frame_names.size();
774 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700775
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700776 // It's okay to not have an intro.
777 if (intro_frames == 0) intro_done = true;
778 // But you must have an animation.
779 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700780
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700781 std::sort(intro_frame_names.begin(), intro_frame_names.end());
782 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700783
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700784 introFrames = new GRSurface*[intro_frames];
785 for (size_t i = 0; i < intro_frames; i++) {
786 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
787 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700788
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700789 loopFrames = new GRSurface*[loop_frames];
790 for (size_t i = 0; i < loop_frames; i++) {
791 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
792 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700793}
794
Elliott Hughes8de52072015-04-08 20:06:50 -0700795void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700796 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700797
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700798 currentIcon = icon;
799 update_screen_locked();
Doug Zongker52eeea42012-09-04 14:28:25 -0700800
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700801 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700802}
803
Elliott Hughes8de52072015-04-08 20:06:50 -0700804void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700805 pthread_mutex_lock(&updateMutex);
806 if (progressBarType != type) {
807 progressBarType = type;
808 }
809 progressScopeStart = 0;
810 progressScopeSize = 0;
811 progress = 0;
812 update_progress_locked();
813 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700814}
815
Elliott Hughes8de52072015-04-08 20:06:50 -0700816void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700817 pthread_mutex_lock(&updateMutex);
818 progressBarType = DETERMINATE;
819 progressScopeStart += progressScopeSize;
820 progressScopeSize = portion;
821 progressScopeTime = now();
822 progressScopeDuration = seconds;
823 progress = 0;
824 update_progress_locked();
825 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700826}
827
Elliott Hughes8de52072015-04-08 20:06:50 -0700828void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700829 pthread_mutex_lock(&updateMutex);
830 if (fraction < 0.0) fraction = 0.0;
831 if (fraction > 1.0) fraction = 1.0;
832 if (progressBarType == DETERMINATE && fraction > progress) {
833 // Skip updates that aren't visibly different.
834 int width = gr_get_width(progressBarEmpty);
835 float scale = width * progressScopeSize;
836 if ((int)(progress * scale) != (int)(fraction * scale)) {
837 progress = fraction;
838 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700839 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700840 }
841 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700842}
843
Doug Zongkerc87bab12013-11-25 13:53:25 -0800844void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700845 pthread_mutex_lock(&updateMutex);
846 stage = current;
847 max_stage = max;
848 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800849}
850
Tao Baob6918c72015-05-19 17:02:16 -0700851void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700852 std::string str;
853 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700854
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700855 if (copy_to_stdout) {
856 fputs(str.c_str(), stdout);
857 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700858
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700859 pthread_mutex_lock(&updateMutex);
860 if (text_rows_ > 0 && text_cols_ > 0) {
861 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
862 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700863 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700864 text_col_ = 0;
865 text_row_ = (text_row_ + 1) % text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700866 }
867 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700868 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700869 text_[text_row_][text_col_] = '\0';
870 update_screen_locked();
871 }
872 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700873}
874
Tao Baob6918c72015-05-19 17:02:16 -0700875void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700876 va_list ap;
877 va_start(ap, fmt);
878 PrintV(fmt, true, ap);
879 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700880}
881
882void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700883 va_list ap;
884 va_start(ap, fmt);
885 PrintV(fmt, false, ap);
886 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700887}
888
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700889void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700890 pthread_mutex_lock(&updateMutex);
891 if (ch != '\n') text_[text_row_][text_col_++] = ch;
892 if (ch == '\n' || text_col_ >= text_cols_) {
893 text_col_ = 0;
894 ++text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700895 }
896 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700897}
898
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700899void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700900 pthread_mutex_lock(&updateMutex);
901 text_col_ = 0;
902 text_row_ = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700903 for (size_t i = 0; i < text_rows_; ++i) {
904 memset(text_[i], 0, text_cols_ + 1);
905 }
906 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700907}
908
909void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700910 std::vector<off_t> offsets;
911 offsets.push_back(ftello(fp));
912 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700913
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700914 struct stat sb;
915 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700916
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700917 bool show_prompt = false;
918 while (true) {
919 if (show_prompt) {
920 PrintOnScreenOnly("--(%d%% of %d bytes)--",
921 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
922 static_cast<int>(sb.st_size));
923 Redraw();
924 while (show_prompt) {
925 show_prompt = false;
926 int key = WaitKey();
927 if (key == KEY_POWER || key == KEY_ENTER) {
928 return;
929 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
930 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700931 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700932 } else {
933 offsets.pop_back();
934 fseek(fp, offsets.back(), SEEK_SET);
935 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700936 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700937 if (feof(fp)) {
938 return;
939 }
940 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700941 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700942 }
943 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700944 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700945
946 int ch = getc(fp);
947 if (ch == EOF) {
948 while (text_row_ < text_rows_ - 1) PutChar('\n');
949 show_prompt = true;
950 } else {
951 PutChar(ch);
952 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
953 show_prompt = true;
954 }
955 }
956 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700957}
958
Tao Bao1d156b92018-05-02 12:43:18 -0700959void ScreenRecoveryUI::ShowFile(const std::string& filename) {
960 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "re"), fclose);
961 if (!fp) {
962 Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno));
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700963 return;
964 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700965
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700966 char** old_text = text_;
967 size_t old_text_col = text_col_;
968 size_t old_text_row = text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700969
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700970 // Swap in the alternate screen and clear it.
971 text_ = file_viewer_text_;
972 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700973
Tao Bao1d156b92018-05-02 12:43:18 -0700974 ShowFile(fp.get());
Elliott Hughesc0491632015-05-06 12:40:05 -0700975
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700976 text_ = old_text;
977 text_col_ = old_text_col;
978 text_row_ = old_text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700979}
980
Tao Bao1fe1afe2018-05-01 15:56:05 -0700981void ScreenRecoveryUI::StartMenu(const std::vector<std::string>& headers,
982 const std::vector<std::string>& items, size_t initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700983 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700984 if (text_rows_ > 0 && text_cols_ > 1) {
Tao Baoe02a5b22018-05-02 15:46:11 -0700985 menu_ = std::make_unique<Menu>(scrollable_menu_, text_rows_, text_cols_ - 1, headers, items,
986 initial_selection);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700987 update_screen_locked();
988 }
989 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700990}
991
992int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700993 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700994 if (menu_) {
995 int old_sel = menu_->selection();
996 sel = menu_->Select(sel);
Elliott Hughesfc06f872015-03-23 13:45:31 -0700997
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700998 if (sel != old_sel) {
999 update_screen_locked();
1000 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001001 }
1002 pthread_mutex_unlock(&updateMutex);
1003 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -07001004}
1005
1006void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001007 pthread_mutex_lock(&updateMutex);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001008 if (menu_) {
1009 menu_.reset();
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001010 update_screen_locked();
1011 }
1012 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001013}
1014
Tao Bao1fe1afe2018-05-01 15:56:05 -07001015size_t ScreenRecoveryUI::ShowMenu(const std::vector<std::string>& headers,
1016 const std::vector<std::string>& items, size_t initial_selection,
1017 bool menu_only,
1018 const std::function<int(int, bool)>& key_handler) {
Tao Bao3aec6962018-04-20 09:24:58 -07001019 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
1020 FlushKeys();
1021
1022 StartMenu(headers, items, initial_selection);
1023
1024 int selected = initial_selection;
1025 int chosen_item = -1;
1026 while (chosen_item < 0) {
1027 int key = WaitKey();
1028 if (key == -1) { // WaitKey() timed out.
1029 if (WasTextEverVisible()) {
1030 continue;
1031 } else {
1032 LOG(INFO) << "Timed out waiting for key input; rebooting.";
1033 EndMenu();
Tao Bao1fe1afe2018-05-01 15:56:05 -07001034 return static_cast<size_t>(-1);
Tao Bao3aec6962018-04-20 09:24:58 -07001035 }
1036 }
1037
1038 bool visible = IsTextVisible();
1039 int action = key_handler(key, visible);
1040 if (action < 0) {
1041 switch (action) {
1042 case Device::kHighlightUp:
1043 selected = SelectMenu(--selected);
1044 break;
1045 case Device::kHighlightDown:
1046 selected = SelectMenu(++selected);
1047 break;
1048 case Device::kInvokeItem:
1049 chosen_item = selected;
1050 break;
1051 case Device::kNoAction:
1052 break;
1053 }
1054 } else if (!menu_only) {
1055 chosen_item = action;
1056 }
1057 }
1058
1059 EndMenu();
1060 return chosen_item;
1061}
1062
Elliott Hughes8de52072015-04-08 20:06:50 -07001063bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001064 pthread_mutex_lock(&updateMutex);
1065 int visible = show_text;
1066 pthread_mutex_unlock(&updateMutex);
1067 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001068}
1069
Elliott Hughes8de52072015-04-08 20:06:50 -07001070bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001071 pthread_mutex_lock(&updateMutex);
1072 int ever_visible = show_text_ever;
1073 pthread_mutex_unlock(&updateMutex);
1074 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -07001075}
1076
Elliott Hughes8de52072015-04-08 20:06:50 -07001077void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001078 pthread_mutex_lock(&updateMutex);
1079 show_text = visible;
1080 if (show_text) show_text_ever = true;
1081 update_screen_locked();
1082 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -07001083}
Doug Zongkerc0441d12013-07-31 11:28:24 -07001084
Elliott Hughes8de52072015-04-08 20:06:50 -07001085void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001086 pthread_mutex_lock(&updateMutex);
1087 update_screen_locked();
1088 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001089}
Elliott Hughes642aaa72015-04-10 12:47:46 -07001090
1091void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -07001092 // Redraw so that if we're in the menu, the highlight
1093 // will change color to indicate a successful long press.
1094 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -07001095}
Tao Baoefb49ad2017-01-31 23:03:10 -08001096
1097void ScreenRecoveryUI::SetLocale(const std::string& new_locale) {
1098 locale_ = new_locale;
1099 rtl_locale_ = false;
1100
1101 if (!new_locale.empty()) {
1102 size_t underscore = new_locale.find('_');
1103 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
1104 std::string lang = new_locale.substr(0, underscore);
1105
1106 // A bit cheesy: keep an explicit list of supported RTL languages.
1107 if (lang == "ar" || // Arabic
1108 lang == "fa" || // Persian (Farsi)
1109 lang == "he" || // Hebrew (new language code)
1110 lang == "iw" || // Hebrew (old language code)
1111 lang == "ur") { // Urdu
1112 rtl_locale_ = true;
1113 }
1114 }
1115}