blob: 7334b713e66c61473a64d01e0a9f1ed74034300b [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
Mikhail Lappob49767c2017-03-23 21:44:26 +0100263void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) const {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700264 gr_text(gr_sys_font(), x, *y, line, bold);
Prashant Malani7a491222016-03-11 10:00:55 -0800265 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700266}
267
Mikhail Lappob49767c2017-03-23 21:44:26 +0100268void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) const {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700269 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800270 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700271 }
272}
273
274static const char* REGULAR_HELP[] = {
275 "Use volume up/down and power.",
276 NULL
277};
278
279static const char* LONG_PRESS_HELP[] = {
280 "Any button cycles highlight.",
281 "Long-press activates.",
282 NULL
283};
284
Tao Bao8f7547e2017-06-19 23:10:44 -0700285// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
286// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700287void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao8f7547e2017-06-19 23:10:44 -0700288 if (!show_text) {
289 draw_background_locked();
290 draw_foreground_locked();
291 return;
292 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700293
Tao Bao8f7547e2017-06-19 23:10:44 -0700294 gr_color(0, 0, 0, 255);
295 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700296
Tao Bao8f7547e2017-06-19 23:10:44 -0700297 static constexpr int TEXT_INDENT = 4;
Tao Bao855eaff2017-06-20 18:11:21 -0700298 int x = TEXT_INDENT + kMarginWidth;
299 int y = kMarginHeight;
Tao Bao8f7547e2017-06-19 23:10:44 -0700300 if (show_menu) {
301 std::string recovery_fingerprint =
302 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700303
Tao Bao8f7547e2017-06-19 23:10:44 -0700304 SetColor(INFO);
305 DrawTextLine(x, &y, "Android Recovery", true);
306 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
307 DrawTextLine(x, &y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700308 }
Tao Bao8f7547e2017-06-19 23:10:44 -0700309 DrawTextLines(x, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
310
311 SetColor(HEADER);
312 DrawTextLines(x, &y, menu_headers_);
313
314 SetColor(MENU);
315 DrawHorizontalRule(&y);
316 y += 4;
317 for (int i = 0; i < menu_items; ++i) {
318 if (i == menu_sel) {
319 // Draw the highlight bar.
320 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
321 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
322 // Bold white text for the selected item.
323 SetColor(MENU_SEL_FG);
324 gr_text(gr_sys_font(), 4, y, menu_[i], true);
325 SetColor(MENU);
326 } else {
327 gr_text(gr_sys_font(), 4, y, menu_[i], false);
328 }
329 y += char_height_ + 4;
330 }
331 DrawHorizontalRule(&y);
332 }
333
334 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
335 // we've displayed the entire text buffer.
336 SetColor(LOG);
337 int row = (text_top_ + text_rows_ - 1) % text_rows_;
338 size_t count = 0;
Tao Bao855eaff2017-06-20 18:11:21 -0700339 for (int ty = gr_fb_height() - kMarginHeight - char_height_;
Tao Bao8f7547e2017-06-19 23:10:44 -0700340 ty >= y && count < text_rows_; ty -= char_height_, ++count) {
341 gr_text(gr_sys_font(), 0, ty, text_[row], false);
342 --row;
343 if (row < 0) row = text_rows_ - 1;
344 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700345}
346
347// Redraw everything on the screen and flip the screen (make it visible).
348// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700349void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700350 draw_screen_locked();
351 gr_flip();
352}
353
354// Updates only the progress bar, if possible, otherwise redraws the screen.
355// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700356void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700357 if (show_text || !pagesIdentical) {
358 draw_screen_locked(); // Must redraw the whole screen
359 pagesIdentical = true;
360 } else {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700361 draw_foreground_locked(); // Draw only the progress bar and overlays
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 }
363 gr_flip();
364}
365
366// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700367void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
368 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700369 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700370}
371
Elliott Hughes985022a2015-04-13 13:04:32 -0700372void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700373 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700374 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700375 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700376 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700377
Elliott Hughes498cda62016-04-14 16:49:04 -0700378 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700379
380 // update the installation animation, if active
381 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700382 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
383 if (!intro_done) {
384 if (current_frame == intro_frames - 1) {
385 intro_done = true;
386 current_frame = 0;
387 } else {
388 ++current_frame;
389 }
390 } else {
391 current_frame = (current_frame + 1) % loop_frames;
392 }
393
394 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700395 }
396
Doug Zongker211aebc2011-10-28 15:13:10 -0700397 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700398 int duration = progressScopeDuration;
399 if (progressBarType == DETERMINATE && duration > 0) {
400 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700401 float p = 1.0 * elapsed / duration;
402 if (p > 1.0) p = 1.0;
403 if (p > progress) {
404 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700405 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700406 }
407 }
408
Doug Zongker32a0a472011-11-01 11:00:20 -0700409 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700410
Doug Zongker32a0a472011-11-01 11:00:20 -0700411 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700412 double end = now();
413 // minimum of 20ms delay between frames
414 double delay = interval - (end-start);
415 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700416 usleep(static_cast<useconds_t>(delay * 1000000));
Doug Zongker211aebc2011-10-28 15:13:10 -0700417 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700418}
419
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700420void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700421 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700422 if (result < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700423 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
Doug Zongkereac881c2014-03-07 09:21:25 -0800424 }
425}
426
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700427void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800428 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
429 if (result < 0) {
430 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
431 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700432}
433
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700434static char** Alloc2d(size_t rows, size_t cols) {
435 char** result = new char*[rows];
436 for (size_t i = 0; i < rows; ++i) {
437 result[i] = new char[cols];
438 memset(result[i], 0, cols);
439 }
440 return result;
441}
442
Tianjie Xu35926c42016-04-28 18:06:26 -0700443// Choose the right background string to display during update.
444void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
445 if (security_update) {
446 LoadLocalizedBitmap("installing_security_text", &installing_text);
447 } else {
448 LoadLocalizedBitmap("installing_text", &installing_text);
449 }
450 Redraw();
451}
452
Sen Jiangd5304492016-12-09 16:20:49 -0800453bool ScreenRecoveryUI::InitTextParams() {
Tao Bao8f7547e2017-06-19 23:10:44 -0700454 if (gr_init() < 0) {
455 return false;
456 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700457
Tao Bao8f7547e2017-06-19 23:10:44 -0700458 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao855eaff2017-06-20 18:11:21 -0700459 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
460 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao8f7547e2017-06-19 23:10:44 -0700461 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700462}
463
Tao Bao736d59c2017-01-03 10:15:33 -0800464bool ScreenRecoveryUI::Init(const std::string& locale) {
465 RecoveryUI::Init(locale);
466 if (!InitTextParams()) {
467 return false;
468 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700469
Tao Bao736d59c2017-01-03 10:15:33 -0800470 // Are we portrait or landscape?
471 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
472 // Are we the large variant of our base layout?
473 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700474
Tao Bao736d59c2017-01-03 10:15:33 -0800475 text_ = Alloc2d(text_rows_, text_cols_ + 1);
476 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
477 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800478
Tao Bao736d59c2017-01-03 10:15:33 -0800479 text_col_ = text_row_ = 0;
480 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700481
Tao Bao736d59c2017-01-03 10:15:33 -0800482 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700483
Tao Bao736d59c2017-01-03 10:15:33 -0800484 LoadBitmap("progress_empty", &progressBarEmpty);
485 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700486
Tao Bao736d59c2017-01-03 10:15:33 -0800487 LoadBitmap("stage_empty", &stageMarkerEmpty);
488 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700489
Tao Bao736d59c2017-01-03 10:15:33 -0800490 // Background text for "installing_update" could be "installing update"
491 // or "installing security update". It will be set after UI init according
492 // to commands in BCB.
493 installing_text = nullptr;
494 LoadLocalizedBitmap("erasing_text", &erasing_text);
495 LoadLocalizedBitmap("no_command_text", &no_command_text);
496 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700497
Tao Bao736d59c2017-01-03 10:15:33 -0800498 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700499
Tao Bao736d59c2017-01-03 10:15:33 -0800500 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800501
Tao Bao736d59c2017-01-03 10:15:33 -0800502 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700503}
504
Elliott Hughes498cda62016-04-14 16:49:04 -0700505void ScreenRecoveryUI::LoadAnimation() {
Elliott Hughes498cda62016-04-14 16:49:04 -0700506 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
507 dirent* de;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700508 std::vector<std::string> intro_frame_names;
509 std::vector<std::string> loop_frame_names;
510
Elliott Hughes498cda62016-04-14 16:49:04 -0700511 while ((de = readdir(dir.get())) != nullptr) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700512 int value, num_chars;
513 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
514 intro_frame_names.emplace_back(de->d_name, num_chars);
515 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
516 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700517 }
518 }
519
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700520 intro_frames = intro_frame_names.size();
521 loop_frames = loop_frame_names.size();
522
Elliott Hughes498cda62016-04-14 16:49:04 -0700523 // It's okay to not have an intro.
524 if (intro_frames == 0) intro_done = true;
525 // But you must have an animation.
526 if (loop_frames == 0) abort();
527
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700528 std::sort(intro_frame_names.begin(), intro_frame_names.end());
529 std::sort(loop_frame_names.begin(), loop_frame_names.end());
530
Elliott Hughes498cda62016-04-14 16:49:04 -0700531 introFrames = new GRSurface*[intro_frames];
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700532 for (size_t i = 0; i < intro_frames; i++) {
533 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700534 }
535
536 loopFrames = new GRSurface*[loop_frames];
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700537 for (size_t i = 0; i < loop_frames; i++) {
538 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700539 }
540}
541
Elliott Hughes8de52072015-04-08 20:06:50 -0700542void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700543 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700544
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700545 currentIcon = icon;
546 update_screen_locked();
547
Doug Zongker211aebc2011-10-28 15:13:10 -0700548 pthread_mutex_unlock(&updateMutex);
549}
550
Elliott Hughes8de52072015-04-08 20:06:50 -0700551void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700552 pthread_mutex_lock(&updateMutex);
553 if (progressBarType != type) {
554 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700555 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700556 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700557 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700558 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700559 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700560 pthread_mutex_unlock(&updateMutex);
561}
562
Elliott Hughes8de52072015-04-08 20:06:50 -0700563void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700564 pthread_mutex_lock(&updateMutex);
565 progressBarType = DETERMINATE;
566 progressScopeStart += progressScopeSize;
567 progressScopeSize = portion;
568 progressScopeTime = now();
569 progressScopeDuration = seconds;
570 progress = 0;
571 update_progress_locked();
572 pthread_mutex_unlock(&updateMutex);
573}
574
Elliott Hughes8de52072015-04-08 20:06:50 -0700575void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700576 pthread_mutex_lock(&updateMutex);
577 if (fraction < 0.0) fraction = 0.0;
578 if (fraction > 1.0) fraction = 1.0;
579 if (progressBarType == DETERMINATE && fraction > progress) {
580 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800581 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700582 float scale = width * progressScopeSize;
583 if ((int) (progress * scale) != (int) (fraction * scale)) {
584 progress = fraction;
585 update_progress_locked();
586 }
587 }
588 pthread_mutex_unlock(&updateMutex);
589}
590
Doug Zongkerc87bab12013-11-25 13:53:25 -0800591void ScreenRecoveryUI::SetStage(int current, int max) {
592 pthread_mutex_lock(&updateMutex);
593 stage = current;
594 max_stage = max;
595 pthread_mutex_unlock(&updateMutex);
596}
597
Tao Baob6918c72015-05-19 17:02:16 -0700598void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
599 std::string str;
600 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700601
Tao Baob6918c72015-05-19 17:02:16 -0700602 if (copy_to_stdout) {
603 fputs(str.c_str(), stdout);
604 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700605
Doug Zongker211aebc2011-10-28 15:13:10 -0700606 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700607 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700608 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700609 if (*ptr == '\n' || text_col_ >= text_cols_) {
610 text_[text_row_][text_col_] = '\0';
611 text_col_ = 0;
612 text_row_ = (text_row_ + 1) % text_rows_;
613 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700614 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700615 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700616 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700617 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700618 update_screen_locked();
619 }
620 pthread_mutex_unlock(&updateMutex);
621}
622
Tao Baob6918c72015-05-19 17:02:16 -0700623void ScreenRecoveryUI::Print(const char* fmt, ...) {
624 va_list ap;
625 va_start(ap, fmt);
626 PrintV(fmt, true, ap);
627 va_end(ap);
628}
629
630void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
631 va_list ap;
632 va_start(ap, fmt);
633 PrintV(fmt, false, ap);
634 va_end(ap);
635}
636
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700637void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700638 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700639 if (ch != '\n') text_[text_row_][text_col_++] = ch;
640 if (ch == '\n' || text_col_ >= text_cols_) {
641 text_col_ = 0;
642 ++text_row_;
643
644 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700645 }
646 pthread_mutex_unlock(&updateMutex);
647}
648
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700649void ScreenRecoveryUI::ClearText() {
650 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700651 text_col_ = 0;
652 text_row_ = 0;
653 text_top_ = 1;
654 for (size_t i = 0; i < text_rows_; ++i) {
655 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700656 }
657 pthread_mutex_unlock(&updateMutex);
658}
659
660void ScreenRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700661 std::vector<off_t> offsets;
662 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700663 ClearText();
664
665 struct stat sb;
666 fstat(fileno(fp), &sb);
667
668 bool show_prompt = false;
669 while (true) {
670 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700671 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700672 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700673 static_cast<int>(sb.st_size));
674 Redraw();
675 while (show_prompt) {
676 show_prompt = false;
677 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700678 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700679 return;
680 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
681 if (offsets.size() <= 1) {
682 show_prompt = true;
683 } else {
684 offsets.pop_back();
685 fseek(fp, offsets.back(), SEEK_SET);
686 }
687 } else {
688 if (feof(fp)) {
689 return;
690 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700691 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700692 }
693 }
694 ClearText();
695 }
696
697 int ch = getc(fp);
698 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700699 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700700 show_prompt = true;
701 } else {
702 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700703 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700704 show_prompt = true;
705 }
706 }
707 }
708}
709
Elliott Hughes8de52072015-04-08 20:06:50 -0700710void ScreenRecoveryUI::ShowFile(const char* filename) {
711 FILE* fp = fopen_path(filename, "re");
712 if (fp == nullptr) {
713 Print(" Unable to open %s: %s\n", filename, strerror(errno));
714 return;
715 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700716
717 char** old_text = text_;
718 size_t old_text_col = text_col_;
719 size_t old_text_row = text_row_;
720 size_t old_text_top = text_top_;
721
722 // Swap in the alternate screen and clear it.
723 text_ = file_viewer_text_;
724 ClearText();
725
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700726 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700727 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700728
729 text_ = old_text;
730 text_col_ = old_text_col;
731 text_row_ = old_text_row;
732 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700733}
734
Doug Zongker211aebc2011-10-28 15:13:10 -0700735void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
736 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700737 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700738 if (text_rows_ > 0 && text_cols_ > 0) {
739 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700740 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700741 for (; i < text_rows_ && items[i] != nullptr; ++i) {
742 strncpy(menu_[i], items[i], text_cols_ - 1);
743 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700744 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700745 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700746 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700747 menu_sel = initial_selection;
748 update_screen_locked();
749 }
750 pthread_mutex_unlock(&updateMutex);
751}
752
753int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700754 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700755 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700756 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700757 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700758
759 // Wrap at top and bottom.
760 if (menu_sel < 0) menu_sel = menu_items - 1;
761 if (menu_sel >= menu_items) menu_sel = 0;
762
Doug Zongker211aebc2011-10-28 15:13:10 -0700763 sel = menu_sel;
764 if (menu_sel != old_sel) update_screen_locked();
765 }
766 pthread_mutex_unlock(&updateMutex);
767 return sel;
768}
769
770void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700771 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700772 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700773 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700774 update_screen_locked();
775 }
776 pthread_mutex_unlock(&updateMutex);
777}
778
Elliott Hughes8de52072015-04-08 20:06:50 -0700779bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700780 pthread_mutex_lock(&updateMutex);
781 int visible = show_text;
782 pthread_mutex_unlock(&updateMutex);
783 return visible;
784}
785
Elliott Hughes8de52072015-04-08 20:06:50 -0700786bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700787 pthread_mutex_lock(&updateMutex);
788 int ever_visible = show_text_ever;
789 pthread_mutex_unlock(&updateMutex);
790 return ever_visible;
791}
792
Elliott Hughes8de52072015-04-08 20:06:50 -0700793void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700794 pthread_mutex_lock(&updateMutex);
795 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700796 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700797 update_screen_locked();
798 pthread_mutex_unlock(&updateMutex);
799}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700800
Elliott Hughes8de52072015-04-08 20:06:50 -0700801void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700802 pthread_mutex_lock(&updateMutex);
803 update_screen_locked();
804 pthread_mutex_unlock(&updateMutex);
805}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700806
807void ScreenRecoveryUI::KeyLongPress(int) {
808 // Redraw so that if we're in the menu, the highlight
809 // will change color to indicate a successful long press.
810 Redraw();
811}