blob: a7d9c9f4b02901cadd466c35d260257d59deb07f [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
Elliott Hughes498cda62016-04-14 16:49:04 -070017#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070018#include <errno.h>
19#include <fcntl.h>
20#include <linux/input.h>
21#include <pthread.h>
22#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 Bao736d59c2017-01-03 10:15:33 -080032#include <string>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070033#include <vector>
34
Tianjie Xuc21edd42016-08-05 18:00:04 -070035#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070036#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080037#include <android-base/strings.h>
38#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070039
Doug Zongker211aebc2011-10-28 15:13:10 -070040#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070041#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042#include "minui/minui.h"
43#include "screen_ui.h"
44#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070045
Doug Zongker211aebc2011-10-28 15:13:10 -070046// Return the current time as a double (including fractions of a second).
47static double now() {
48 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070049 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070050 return tv.tv_sec + tv.tv_usec / 1000000.0;
51}
52
Tao Bao736d59c2017-01-03 10:15:33 -080053ScreenRecoveryUI::ScreenRecoveryUI()
Tao Bao855eaff2017-06-20 18:11:21 -070054 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
55 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
56 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao8f7547e2017-06-19 23:10:44 -070057 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -080058 progressBarType(EMPTY),
59 progressScopeStart(0),
60 progressScopeSize(0),
61 progress(0),
62 pagesIdentical(false),
63 text_cols_(0),
64 text_rows_(0),
65 text_(nullptr),
66 text_col_(0),
67 text_row_(0),
68 text_top_(0),
69 show_text(false),
70 show_text_ever(false),
71 menu_(nullptr),
72 show_menu(false),
73 menu_items(0),
74 menu_sel(0),
75 file_viewer_text_(nullptr),
76 intro_frames(0),
77 loop_frames(0),
78 current_frame(0),
79 intro_done(false),
80 animation_fps(30), // TODO: there's currently no way to infer this.
81 stage(-1),
82 max_stage(-1),
83 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -070084
Elliott Hughes498cda62016-04-14 16:49:04 -070085GRSurface* ScreenRecoveryUI::GetCurrentFrame() {
86 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
87 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
88 }
89 return error_icon;
90}
91
92GRSurface* ScreenRecoveryUI::GetCurrentText() {
93 switch (currentIcon) {
94 case ERASING: return erasing_text;
95 case ERROR: return error_text;
96 case INSTALLING_UPDATE: return installing_text;
97 case NO_COMMAND: return no_command_text;
98 case NONE: abort();
99 }
100}
101
Mikhail Lappob49767c2017-03-23 21:44:26 +0100102int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700103 return dp * density_;
104}
105
106// Here's the intended layout:
107
Elliott Hughes6d089a92016-07-08 17:23:41 -0700108// | portrait large landscape large
109// ---------+-------------------------------------------------
Tao Baof95e6862017-06-29 14:32:05 -0700110// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700111// icon | (200dp)
112// gap | 68dp 68dp 56dp 112dp
113// text | (14sp)
114// gap | 32dp 32dp 26dp 52dp
115// progress | (2dp)
Tao Baof95e6862017-06-29 14:32:05 -0700116// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700117
Tao Baof95e6862017-06-29 14:32:05 -0700118// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
119// 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 -0700120
Elliott Hughes6d089a92016-07-08 17:23:41 -0700121enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Baof95e6862017-06-29 14:32:05 -0700122enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700123static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Baof95e6862017-06-29 14:32:05 -0700124 { 32, 68, }, // PORTRAIT
125 { 32, 68, }, // PORTRAIT_LARGE
126 { 26, 56, }, // LANDSCAPE
127 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700128};
129
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700130int ScreenRecoveryUI::GetAnimationBaseline() {
Elliott Hughes6d089a92016-07-08 17:23:41 -0700131 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) -
132 gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700133}
134
135int ScreenRecoveryUI::GetTextBaseline() {
Elliott Hughes6d089a92016-07-08 17:23:41 -0700136 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
137 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700138}
139
140int ScreenRecoveryUI::GetProgressBaseline() {
Tao Baof95e6862017-06-29 14:32:05 -0700141 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
142 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
143 gr_get_height(progressBarFill);
144 int bottom_gap = (gr_fb_height() - elements_sum) / 2;
145 return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700146}
147
Doug Zongker211aebc2011-10-28 15:13:10 -0700148// Clear the screen and draw the currently selected background icon (if any).
149// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700150void ScreenRecoveryUI::draw_background_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700151 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700152 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800153 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700154
Elliott Hughes498cda62016-04-14 16:49:04 -0700155 if (currentIcon != NONE) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700156 if (max_stage != -1) {
157 int stage_height = gr_get_height(stageMarkerEmpty);
158 int stage_width = gr_get_width(stageMarkerEmpty);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800159 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700160 int y = gr_fb_height() - stage_height;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800161 for (int i = 0; i < max_stage; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700162 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
163 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
164 x += stage_width;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800165 }
166 }
167
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700168 GRSurface* text_surface = GetCurrentText();
169 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
170 int text_y = GetTextBaseline();
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700171 gr_color(255, 255, 255, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700172 gr_texticon(text_x, text_y, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 }
174}
175
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700176// Draws the animation and progress bar (if any) on the screen.
177// Does not flip pages.
Doug Zongker211aebc2011-10-28 15:13:10 -0700178// Should only be called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700179void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800180 if (currentIcon != NONE) {
181 GRSurface* frame = GetCurrentFrame();
182 int frame_width = gr_get_width(frame);
183 int frame_height = gr_get_height(frame);
184 int frame_x = (gr_fb_width() - frame_width) / 2;
185 int frame_y = GetAnimationBaseline();
186 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
187 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700188
Tao Bao736d59c2017-01-03 10:15:33 -0800189 if (progressBarType != EMPTY) {
190 int width = gr_get_width(progressBarEmpty);
191 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700192
Tao Bao736d59c2017-01-03 10:15:33 -0800193 int progress_x = (gr_fb_width() - width) / 2;
194 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700195
Tao Bao736d59c2017-01-03 10:15:33 -0800196 // Erase behind the progress bar (in case this was a progress-only update)
197 gr_color(0, 0, 0, 255);
198 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700199
Tao Bao736d59c2017-01-03 10:15:33 -0800200 if (progressBarType == DETERMINATE) {
201 float p = progressScopeStart + progress * progressScopeSize;
202 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700203
Tao Bao736d59c2017-01-03 10:15:33 -0800204 if (rtl_locale_) {
205 // Fill the progress bar from right to left.
206 if (pos > 0) {
207 gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
208 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700209 }
Tao Bao736d59c2017-01-03 10:15:33 -0800210 if (pos < width - 1) {
211 gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
212 }
213 } else {
214 // Fill the progress bar from left to right.
215 if (pos > 0) {
216 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
217 }
218 if (pos < width - 1) {
219 gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
220 }
221 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700222 }
Tao Bao736d59c2017-01-03 10:15:33 -0800223 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700224}
225
Doug Zongkerc0441d12013-07-31 11:28:24 -0700226void ScreenRecoveryUI::SetColor(UIElement e) {
227 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700228 case INFO:
229 gr_color(249, 194, 0, 255);
230 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700231 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700232 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700233 break;
234 case MENU:
235 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700236 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700237 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700238 case MENU_SEL_BG_ACTIVE:
239 gr_color(0, 156, 100, 255);
240 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700241 case MENU_SEL_FG:
242 gr_color(255, 255, 255, 255);
243 break;
244 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700245 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700246 break;
247 case TEXT_FILL:
248 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700249 break;
250 default:
251 gr_color(255, 255, 255, 255);
252 break;
253 }
254}
Doug Zongker211aebc2011-10-28 15:13:10 -0700255
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700256void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
257 SetColor(MENU);
258 *y += 4;
259 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700260 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700261}
262
Luke Songedc6b522017-06-12 16:08:33 -0700263void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
264 gr_fill(x, y, x + width, y + height);
265}
266
Mikhail Lappob49767c2017-03-23 21:44:26 +0100267void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) const {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700268 gr_text(gr_sys_font(), x, *y, line, bold);
Prashant Malani7a491222016-03-11 10:00:55 -0800269 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700270}
271
Mikhail Lappob49767c2017-03-23 21:44:26 +0100272void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) const {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700273 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800274 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700275 }
276}
277
278static const char* REGULAR_HELP[] = {
279 "Use volume up/down and power.",
280 NULL
281};
282
283static const char* LONG_PRESS_HELP[] = {
284 "Any button cycles highlight.",
285 "Long-press activates.",
286 NULL
287};
288
Tao Bao8f7547e2017-06-19 23:10:44 -0700289// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
290// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700291void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao8f7547e2017-06-19 23:10:44 -0700292 if (!show_text) {
293 draw_background_locked();
294 draw_foreground_locked();
295 return;
296 }
Tao Bao8f7547e2017-06-19 23:10:44 -0700297 gr_color(0, 0, 0, 255);
298 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700299
Tao Bao8f7547e2017-06-19 23:10:44 -0700300 static constexpr int TEXT_INDENT = 4;
Tao Bao855eaff2017-06-20 18:11:21 -0700301 int x = TEXT_INDENT + kMarginWidth;
302 int y = kMarginHeight;
Tao Bao8f7547e2017-06-19 23:10:44 -0700303 if (show_menu) {
304 std::string recovery_fingerprint =
305 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700306
Tao Bao8f7547e2017-06-19 23:10:44 -0700307 SetColor(INFO);
308 DrawTextLine(x, &y, "Android Recovery", true);
309 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
310 DrawTextLine(x, &y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700311 }
Tao Bao8f7547e2017-06-19 23:10:44 -0700312 DrawTextLines(x, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
313
314 SetColor(HEADER);
315 DrawTextLines(x, &y, menu_headers_);
316
317 SetColor(MENU);
318 DrawHorizontalRule(&y);
319 y += 4;
320 for (int i = 0; i < menu_items; ++i) {
321 if (i == menu_sel) {
322 // Draw the highlight bar.
323 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Luke Songedc6b522017-06-12 16:08:33 -0700324 DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4);
Tao Bao8f7547e2017-06-19 23:10:44 -0700325 // Bold white text for the selected item.
326 SetColor(MENU_SEL_FG);
Luke Songedc6b522017-06-12 16:08:33 -0700327 DrawTextLine(x, &y, menu_[i], true);
Tao Bao8f7547e2017-06-19 23:10:44 -0700328 SetColor(MENU);
329 } else {
Luke Songedc6b522017-06-12 16:08:33 -0700330 DrawTextLine(x, &y, menu_[i], false);
Tao Bao8f7547e2017-06-19 23:10:44 -0700331 }
Tao Bao8f7547e2017-06-19 23:10:44 -0700332 }
333 DrawHorizontalRule(&y);
334 }
335
336 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
337 // we've displayed the entire text buffer.
338 SetColor(LOG);
339 int row = (text_top_ + text_rows_ - 1) % text_rows_;
340 size_t count = 0;
Luke Songc3928882017-06-23 14:33:46 -0700341 for (int ty = gr_fb_height() - kMarginHeight - char_height_;
Tao Bao8f7547e2017-06-19 23:10:44 -0700342 ty >= y && count < text_rows_; ty -= char_height_, ++count) {
Luke Songedc6b522017-06-12 16:08:33 -0700343 int temp_y = ty;
344 DrawTextLine(x, &temp_y, text_[row], false);
Tao Bao8f7547e2017-06-19 23:10:44 -0700345 --row;
346 if (row < 0) row = text_rows_ - 1;
347 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700348}
349
350// Redraw everything on the screen and flip the screen (make it visible).
351// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700352void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 draw_screen_locked();
354 gr_flip();
355}
356
357// Updates only the progress bar, if possible, otherwise redraws the screen.
358// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700359void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700360 if (show_text || !pagesIdentical) {
361 draw_screen_locked(); // Must redraw the whole screen
362 pagesIdentical = true;
363 } else {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700364 draw_foreground_locked(); // Draw only the progress bar and overlays
Doug Zongker211aebc2011-10-28 15:13:10 -0700365 }
366 gr_flip();
367}
368
369// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700370void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
371 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700372 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700373}
374
Elliott Hughes985022a2015-04-13 13:04:32 -0700375void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700376 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700377 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700378 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700379 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700380
Elliott Hughes498cda62016-04-14 16:49:04 -0700381 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700382
383 // update the installation animation, if active
384 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700385 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
386 if (!intro_done) {
387 if (current_frame == intro_frames - 1) {
388 intro_done = true;
389 current_frame = 0;
390 } else {
391 ++current_frame;
392 }
393 } else {
394 current_frame = (current_frame + 1) % loop_frames;
395 }
396
397 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700398 }
399
Doug Zongker211aebc2011-10-28 15:13:10 -0700400 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700401 int duration = progressScopeDuration;
402 if (progressBarType == DETERMINATE && duration > 0) {
403 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700404 float p = 1.0 * elapsed / duration;
405 if (p > 1.0) p = 1.0;
406 if (p > progress) {
407 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700408 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700409 }
410 }
411
Doug Zongker32a0a472011-11-01 11:00:20 -0700412 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700413
Doug Zongker32a0a472011-11-01 11:00:20 -0700414 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700415 double end = now();
416 // minimum of 20ms delay between frames
417 double delay = interval - (end-start);
418 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700419 usleep(static_cast<useconds_t>(delay * 1000000));
Doug Zongker211aebc2011-10-28 15:13:10 -0700420 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700421}
422
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700423void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700424 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700425 if (result < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700426 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
Doug Zongkereac881c2014-03-07 09:21:25 -0800427 }
428}
429
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700430void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800431 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
432 if (result < 0) {
433 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
434 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700435}
436
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700437static char** Alloc2d(size_t rows, size_t cols) {
438 char** result = new char*[rows];
439 for (size_t i = 0; i < rows; ++i) {
440 result[i] = new char[cols];
441 memset(result[i], 0, cols);
442 }
443 return result;
444}
445
Tianjie Xu35926c42016-04-28 18:06:26 -0700446// Choose the right background string to display during update.
447void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
448 if (security_update) {
449 LoadLocalizedBitmap("installing_security_text", &installing_text);
450 } else {
451 LoadLocalizedBitmap("installing_text", &installing_text);
452 }
453 Redraw();
454}
455
Sen Jiangd5304492016-12-09 16:20:49 -0800456bool ScreenRecoveryUI::InitTextParams() {
Tao Bao8f7547e2017-06-19 23:10:44 -0700457 if (gr_init() < 0) {
458 return false;
459 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700460
Tao Bao8f7547e2017-06-19 23:10:44 -0700461 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao855eaff2017-06-20 18:11:21 -0700462 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
463 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao8f7547e2017-06-19 23:10:44 -0700464 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700465}
466
Tao Bao736d59c2017-01-03 10:15:33 -0800467bool ScreenRecoveryUI::Init(const std::string& locale) {
468 RecoveryUI::Init(locale);
469 if (!InitTextParams()) {
470 return false;
471 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700472
Tao Bao736d59c2017-01-03 10:15:33 -0800473 // Are we portrait or landscape?
474 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
475 // Are we the large variant of our base layout?
476 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700477
Tao Bao736d59c2017-01-03 10:15:33 -0800478 text_ = Alloc2d(text_rows_, text_cols_ + 1);
479 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
480 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800481
Tao Bao736d59c2017-01-03 10:15:33 -0800482 text_col_ = text_row_ = 0;
483 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700484
Tao Bao736d59c2017-01-03 10:15:33 -0800485 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700486
Tao Bao736d59c2017-01-03 10:15:33 -0800487 LoadBitmap("progress_empty", &progressBarEmpty);
488 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700489
Tao Bao736d59c2017-01-03 10:15:33 -0800490 LoadBitmap("stage_empty", &stageMarkerEmpty);
491 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700492
Tao Bao736d59c2017-01-03 10:15:33 -0800493 // Background text for "installing_update" could be "installing update"
494 // or "installing security update". It will be set after UI init according
495 // to commands in BCB.
496 installing_text = nullptr;
497 LoadLocalizedBitmap("erasing_text", &erasing_text);
498 LoadLocalizedBitmap("no_command_text", &no_command_text);
499 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700500
Tao Bao736d59c2017-01-03 10:15:33 -0800501 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700502
Tao Bao736d59c2017-01-03 10:15:33 -0800503 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800504
Tao Bao736d59c2017-01-03 10:15:33 -0800505 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700506}
507
Elliott Hughes498cda62016-04-14 16:49:04 -0700508void ScreenRecoveryUI::LoadAnimation() {
Elliott Hughes498cda62016-04-14 16:49:04 -0700509 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
510 dirent* de;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700511 std::vector<std::string> intro_frame_names;
512 std::vector<std::string> loop_frame_names;
513
Elliott Hughes498cda62016-04-14 16:49:04 -0700514 while ((de = readdir(dir.get())) != nullptr) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700515 int value, num_chars;
516 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
517 intro_frame_names.emplace_back(de->d_name, num_chars);
518 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
519 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700520 }
521 }
522
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700523 intro_frames = intro_frame_names.size();
524 loop_frames = loop_frame_names.size();
525
Elliott Hughes498cda62016-04-14 16:49:04 -0700526 // It's okay to not have an intro.
527 if (intro_frames == 0) intro_done = true;
528 // But you must have an animation.
529 if (loop_frames == 0) abort();
530
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700531 std::sort(intro_frame_names.begin(), intro_frame_names.end());
532 std::sort(loop_frame_names.begin(), loop_frame_names.end());
533
Elliott Hughes498cda62016-04-14 16:49:04 -0700534 introFrames = new GRSurface*[intro_frames];
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700535 for (size_t i = 0; i < intro_frames; i++) {
536 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700537 }
538
539 loopFrames = new GRSurface*[loop_frames];
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700540 for (size_t i = 0; i < loop_frames; i++) {
541 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700542 }
543}
544
Elliott Hughes8de52072015-04-08 20:06:50 -0700545void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700546 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700547
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700548 currentIcon = icon;
549 update_screen_locked();
550
Doug Zongker211aebc2011-10-28 15:13:10 -0700551 pthread_mutex_unlock(&updateMutex);
552}
553
Elliott Hughes8de52072015-04-08 20:06:50 -0700554void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700555 pthread_mutex_lock(&updateMutex);
556 if (progressBarType != type) {
557 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700558 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700559 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700560 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700561 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700562 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700563 pthread_mutex_unlock(&updateMutex);
564}
565
Elliott Hughes8de52072015-04-08 20:06:50 -0700566void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700567 pthread_mutex_lock(&updateMutex);
568 progressBarType = DETERMINATE;
569 progressScopeStart += progressScopeSize;
570 progressScopeSize = portion;
571 progressScopeTime = now();
572 progressScopeDuration = seconds;
573 progress = 0;
574 update_progress_locked();
575 pthread_mutex_unlock(&updateMutex);
576}
577
Elliott Hughes8de52072015-04-08 20:06:50 -0700578void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700579 pthread_mutex_lock(&updateMutex);
580 if (fraction < 0.0) fraction = 0.0;
581 if (fraction > 1.0) fraction = 1.0;
582 if (progressBarType == DETERMINATE && fraction > progress) {
583 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800584 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700585 float scale = width * progressScopeSize;
586 if ((int) (progress * scale) != (int) (fraction * scale)) {
587 progress = fraction;
588 update_progress_locked();
589 }
590 }
591 pthread_mutex_unlock(&updateMutex);
592}
593
Doug Zongkerc87bab12013-11-25 13:53:25 -0800594void ScreenRecoveryUI::SetStage(int current, int max) {
595 pthread_mutex_lock(&updateMutex);
596 stage = current;
597 max_stage = max;
598 pthread_mutex_unlock(&updateMutex);
599}
600
Tao Baob6918c72015-05-19 17:02:16 -0700601void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
602 std::string str;
603 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700604
Tao Baob6918c72015-05-19 17:02:16 -0700605 if (copy_to_stdout) {
606 fputs(str.c_str(), stdout);
607 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700608
Doug Zongker211aebc2011-10-28 15:13:10 -0700609 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700610 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700611 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700612 if (*ptr == '\n' || text_col_ >= text_cols_) {
613 text_[text_row_][text_col_] = '\0';
614 text_col_ = 0;
615 text_row_ = (text_row_ + 1) % text_rows_;
616 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700617 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700618 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700619 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700620 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700621 update_screen_locked();
622 }
623 pthread_mutex_unlock(&updateMutex);
624}
625
Tao Baob6918c72015-05-19 17:02:16 -0700626void ScreenRecoveryUI::Print(const char* fmt, ...) {
627 va_list ap;
628 va_start(ap, fmt);
629 PrintV(fmt, true, ap);
630 va_end(ap);
631}
632
633void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
634 va_list ap;
635 va_start(ap, fmt);
636 PrintV(fmt, false, ap);
637 va_end(ap);
638}
639
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700640void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700641 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700642 if (ch != '\n') text_[text_row_][text_col_++] = ch;
643 if (ch == '\n' || text_col_ >= text_cols_) {
644 text_col_ = 0;
645 ++text_row_;
646
647 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700648 }
649 pthread_mutex_unlock(&updateMutex);
650}
651
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700652void ScreenRecoveryUI::ClearText() {
653 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700654 text_col_ = 0;
655 text_row_ = 0;
656 text_top_ = 1;
657 for (size_t i = 0; i < text_rows_; ++i) {
658 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700659 }
660 pthread_mutex_unlock(&updateMutex);
661}
662
663void ScreenRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700664 std::vector<off_t> offsets;
665 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700666 ClearText();
667
668 struct stat sb;
669 fstat(fileno(fp), &sb);
670
671 bool show_prompt = false;
672 while (true) {
673 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700674 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700675 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700676 static_cast<int>(sb.st_size));
677 Redraw();
678 while (show_prompt) {
679 show_prompt = false;
680 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700681 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700682 return;
683 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
684 if (offsets.size() <= 1) {
685 show_prompt = true;
686 } else {
687 offsets.pop_back();
688 fseek(fp, offsets.back(), SEEK_SET);
689 }
690 } else {
691 if (feof(fp)) {
692 return;
693 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700694 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700695 }
696 }
697 ClearText();
698 }
699
700 int ch = getc(fp);
701 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700702 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700703 show_prompt = true;
704 } else {
705 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700706 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700707 show_prompt = true;
708 }
709 }
710 }
711}
712
Elliott Hughes8de52072015-04-08 20:06:50 -0700713void ScreenRecoveryUI::ShowFile(const char* filename) {
714 FILE* fp = fopen_path(filename, "re");
715 if (fp == nullptr) {
716 Print(" Unable to open %s: %s\n", filename, strerror(errno));
717 return;
718 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700719
720 char** old_text = text_;
721 size_t old_text_col = text_col_;
722 size_t old_text_row = text_row_;
723 size_t old_text_top = text_top_;
724
725 // Swap in the alternate screen and clear it.
726 text_ = file_viewer_text_;
727 ClearText();
728
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700729 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700730 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700731
732 text_ = old_text;
733 text_col_ = old_text_col;
734 text_row_ = old_text_row;
735 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700736}
737
Doug Zongker211aebc2011-10-28 15:13:10 -0700738void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
739 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700740 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700741 if (text_rows_ > 0 && text_cols_ > 0) {
742 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700743 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700744 for (; i < text_rows_ && items[i] != nullptr; ++i) {
745 strncpy(menu_[i], items[i], text_cols_ - 1);
746 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700747 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700748 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700749 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700750 menu_sel = initial_selection;
751 update_screen_locked();
752 }
753 pthread_mutex_unlock(&updateMutex);
754}
755
756int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700757 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700758 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700759 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700760 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700761
762 // Wrap at top and bottom.
763 if (menu_sel < 0) menu_sel = menu_items - 1;
764 if (menu_sel >= menu_items) menu_sel = 0;
765
Doug Zongker211aebc2011-10-28 15:13:10 -0700766 sel = menu_sel;
767 if (menu_sel != old_sel) update_screen_locked();
768 }
769 pthread_mutex_unlock(&updateMutex);
770 return sel;
771}
772
773void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700774 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700775 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700776 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700777 update_screen_locked();
778 }
779 pthread_mutex_unlock(&updateMutex);
780}
781
Elliott Hughes8de52072015-04-08 20:06:50 -0700782bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700783 pthread_mutex_lock(&updateMutex);
784 int visible = show_text;
785 pthread_mutex_unlock(&updateMutex);
786 return visible;
787}
788
Elliott Hughes8de52072015-04-08 20:06:50 -0700789bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700790 pthread_mutex_lock(&updateMutex);
791 int ever_visible = show_text_ever;
792 pthread_mutex_unlock(&updateMutex);
793 return ever_visible;
794}
795
Elliott Hughes8de52072015-04-08 20:06:50 -0700796void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700797 pthread_mutex_lock(&updateMutex);
798 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700799 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700800 update_screen_locked();
801 pthread_mutex_unlock(&updateMutex);
802}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700803
Elliott Hughes8de52072015-04-08 20:06:50 -0700804void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700805 pthread_mutex_lock(&updateMutex);
806 update_screen_locked();
807 pthread_mutex_unlock(&updateMutex);
808}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700809
810void ScreenRecoveryUI::KeyLongPress(int) {
811 // Redraw so that if we're in the menu, the highlight
812 // will change color to indicate a successful long press.
813 Redraw();
814}