blob: 378a4514af711849f9ec362fa756c455f2a48d31 [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() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070048 struct timeval tv;
49 gettimeofday(&tv, nullptr);
50 return tv.tv_sec + tv.tv_usec / 1000000.0;
Doug Zongker211aebc2011-10-28 15:13:10 -070051}
52
Tao Bao736d59c2017-01-03 10:15:33 -080053ScreenRecoveryUI::ScreenRecoveryUI()
Tao Bao4521b702017-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 Bao171b4c42017-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
Tao Bao99b2d772017-06-23 22:47:03 -070085GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070086 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
87 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
88 }
89 return error_icon;
Elliott Hughes498cda62016-04-14 16:49:04 -070090}
91
Tao Bao99b2d772017-06-23 22:47:03 -070092GRSurface* ScreenRecoveryUI::GetCurrentText() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070093 switch (currentIcon) {
94 case ERASING:
95 return erasing_text;
96 case ERROR:
97 return error_text;
98 case INSTALLING_UPDATE:
99 return installing_text;
100 case NO_COMMAND:
101 return no_command_text;
102 case NONE:
103 abort();
104 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700105}
106
Mikhail Lappob49767c2017-03-23 21:44:26 +0100107int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700108 return dp * density_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700109}
110
111// Here's the intended layout:
112
Elliott Hughes6d089a92016-07-08 17:23:41 -0700113// | portrait large landscape large
114// ---------+-------------------------------------------------
Tao Bao3250f722017-06-29 14:32:05 -0700115// gap |
Elliott Hughes6d089a92016-07-08 17:23:41 -0700116// icon | (200dp)
117// gap | 68dp 68dp 56dp 112dp
118// text | (14sp)
119// gap | 32dp 32dp 26dp 52dp
120// progress | (2dp)
Tao Bao3250f722017-06-29 14:32:05 -0700121// gap |
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700122
Tao Bao3250f722017-06-29 14:32:05 -0700123// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines
124// 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 -0700125
Elliott Hughes6d089a92016-07-08 17:23:41 -0700126enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
Tao Bao3250f722017-06-29 14:32:05 -0700127enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX };
Elliott Hughes6d089a92016-07-08 17:23:41 -0700128static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
Tao Bao3250f722017-06-29 14:32:05 -0700129 { 32, 68, }, // PORTRAIT
130 { 32, 68, }, // PORTRAIT_LARGE
131 { 26, 56, }, // LANDSCAPE
132 { 52, 112, }, // LANDSCAPE_LARGE
Elliott Hughes6d089a92016-07-08 17:23:41 -0700133};
134
Tao Bao99b2d772017-06-23 22:47:03 -0700135int ScreenRecoveryUI::GetAnimationBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700136 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700137}
138
Tao Bao99b2d772017-06-23 22:47:03 -0700139int ScreenRecoveryUI::GetTextBaseline() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700140 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
141 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700142}
143
Tao Bao99b2d772017-06-23 22:47:03 -0700144int ScreenRecoveryUI::GetProgressBaseline() const {
Tao Bao3250f722017-06-29 14:32:05 -0700145 int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
146 gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
147 gr_get_height(progressBarFill);
148 int bottom_gap = (gr_fb_height() - elements_sum) / 2;
149 return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700150}
151
Doug Zongker211aebc2011-10-28 15:13:10 -0700152// Clear the screen and draw the currently selected background icon (if any).
153// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700154void ScreenRecoveryUI::draw_background_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700155 pagesIdentical = false;
156 gr_color(0, 0, 0, 255);
157 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700158
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700159 if (currentIcon != NONE) {
160 if (max_stage != -1) {
161 int stage_height = gr_get_height(stageMarkerEmpty);
162 int stage_width = gr_get_width(stageMarkerEmpty);
163 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
164 int y = gr_fb_height() - stage_height;
165 for (int i = 0; i < max_stage; ++i) {
166 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
167 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
168 x += stage_width;
169 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700170 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700171
172 GRSurface* text_surface = GetCurrentText();
173 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
174 int text_y = GetTextBaseline();
175 gr_color(255, 255, 255, 255);
176 gr_texticon(text_x, text_y, text_surface);
177 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700178}
179
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700180// Draws the animation and progress bar (if any) on the screen.
181// Does not flip pages.
Doug Zongker211aebc2011-10-28 15:13:10 -0700182// Should only be called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700183void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800184 if (currentIcon != NONE) {
185 GRSurface* frame = GetCurrentFrame();
186 int frame_width = gr_get_width(frame);
187 int frame_height = gr_get_height(frame);
188 int frame_x = (gr_fb_width() - frame_width) / 2;
189 int frame_y = GetAnimationBaseline();
190 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
191 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700192
Tao Bao736d59c2017-01-03 10:15:33 -0800193 if (progressBarType != EMPTY) {
194 int width = gr_get_width(progressBarEmpty);
195 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700196
Tao Bao736d59c2017-01-03 10:15:33 -0800197 int progress_x = (gr_fb_width() - width) / 2;
198 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700199
Tao Bao736d59c2017-01-03 10:15:33 -0800200 // Erase behind the progress bar (in case this was a progress-only update)
201 gr_color(0, 0, 0, 255);
202 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700203
Tao Bao736d59c2017-01-03 10:15:33 -0800204 if (progressBarType == DETERMINATE) {
205 float p = progressScopeStart + progress * progressScopeSize;
206 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700207
Tao Bao736d59c2017-01-03 10:15:33 -0800208 if (rtl_locale_) {
209 // Fill the progress bar from right to left.
210 if (pos > 0) {
211 gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
212 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700213 }
Tao Bao736d59c2017-01-03 10:15:33 -0800214 if (pos < width - 1) {
215 gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
216 }
217 } else {
218 // Fill the progress bar from left to right.
219 if (pos > 0) {
220 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
221 }
222 if (pos < width - 1) {
223 gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
224 }
225 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700226 }
Tao Bao736d59c2017-01-03 10:15:33 -0800227 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700228}
229
Tao Bao99b2d772017-06-23 22:47:03 -0700230void ScreenRecoveryUI::SetColor(UIElement e) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700231 switch (e) {
232 case INFO:
233 gr_color(249, 194, 0, 255);
234 break;
235 case HEADER:
236 gr_color(247, 0, 6, 255);
237 break;
238 case MENU:
239 case MENU_SEL_BG:
240 gr_color(0, 106, 157, 255);
241 break;
242 case MENU_SEL_BG_ACTIVE:
243 gr_color(0, 156, 100, 255);
244 break;
245 case MENU_SEL_FG:
246 gr_color(255, 255, 255, 255);
247 break;
248 case LOG:
249 gr_color(196, 196, 196, 255);
250 break;
251 case TEXT_FILL:
252 gr_color(0, 0, 0, 160);
253 break;
254 default:
255 gr_color(255, 255, 255, 255);
256 break;
257 }
Doug Zongkerc0441d12013-07-31 11:28:24 -0700258}
Doug Zongker211aebc2011-10-28 15:13:10 -0700259
Tao Bao99b2d772017-06-23 22:47:03 -0700260void ScreenRecoveryUI::DrawHorizontalRule(int* y) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700261 SetColor(MENU);
262 *y += 4;
263 gr_fill(0, *y, gr_fb_width(), *y + 2);
264 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700265}
266
Luke Songe2bd8762017-06-12 16:08:33 -0700267void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700268 gr_fill(x, y, x + width, y + height);
Luke Songe2bd8762017-06-12 16:08:33 -0700269}
270
Mikhail Lappob49767c2017-03-23 21:44:26 +0100271void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700272 gr_text(gr_sys_font(), x, *y, line, bold);
273 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700274}
275
Mikhail Lappob49767c2017-03-23 21:44:26 +0100276void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700277 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
278 DrawTextLine(x, y, lines[i], false);
279 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700280}
281
282static const char* REGULAR_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700283 "Use volume up/down and power.",
284 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700285};
286
287static const char* LONG_PRESS_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700288 "Any button cycles highlight.",
289 "Long-press activates.",
290 NULL
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700291};
292
Tao Bao171b4c42017-06-19 23:10:44 -0700293// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
294// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700295void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao171b4c42017-06-19 23:10:44 -0700296 if (!show_text) {
297 draw_background_locked();
298 draw_foreground_locked();
299 return;
300 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700301
Tao Bao171b4c42017-06-19 23:10:44 -0700302 gr_color(0, 0, 0, 255);
303 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700304
Tao Bao171b4c42017-06-19 23:10:44 -0700305 static constexpr int TEXT_INDENT = 4;
Tao Bao4521b702017-06-20 18:11:21 -0700306 int x = TEXT_INDENT + kMarginWidth;
307 int y = kMarginHeight;
Tao Bao171b4c42017-06-19 23:10:44 -0700308 if (show_menu) {
309 std::string recovery_fingerprint =
310 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700311
Tao Bao171b4c42017-06-19 23:10:44 -0700312 SetColor(INFO);
313 DrawTextLine(x, &y, "Android Recovery", true);
314 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
315 DrawTextLine(x, &y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700316 }
Tao Bao171b4c42017-06-19 23:10:44 -0700317 DrawTextLines(x, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
318
319 SetColor(HEADER);
320 DrawTextLines(x, &y, menu_headers_);
321
322 SetColor(MENU);
323 DrawHorizontalRule(&y);
324 y += 4;
325 for (int i = 0; i < menu_items; ++i) {
326 if (i == menu_sel) {
327 // Draw the highlight bar.
328 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
329 DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4);
330 // Bold white text for the selected item.
331 SetColor(MENU_SEL_FG);
332 DrawTextLine(x, &y, menu_[i], true);
333 SetColor(MENU);
334 } else {
335 DrawTextLine(x, &y, menu_[i], false);
336 }
337 }
338 DrawHorizontalRule(&y);
339 }
340
341 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
342 // we've displayed the entire text buffer.
343 SetColor(LOG);
344 int row = (text_top_ + text_rows_ - 1) % text_rows_;
345 size_t count = 0;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700346 for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
347 ty -= char_height_, ++count) {
Tao Bao171b4c42017-06-19 23:10:44 -0700348 int temp_y = ty;
349 DrawTextLine(x, &temp_y, text_[row], false);
350 --row;
351 if (row < 0) row = text_rows_ - 1;
352 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700353}
354
355// Redraw everything on the screen and flip the screen (make it visible).
356// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700357void ScreenRecoveryUI::update_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700358 draw_screen_locked();
359 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700360}
361
362// Updates only the progress bar, if possible, otherwise redraws the screen.
363// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700364void ScreenRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700365 if (show_text || !pagesIdentical) {
366 draw_screen_locked(); // Must redraw the whole screen
367 pagesIdentical = true;
368 } else {
369 draw_foreground_locked(); // Draw only the progress bar and overlays
370 }
371 gr_flip();
Doug Zongker211aebc2011-10-28 15:13:10 -0700372}
373
374// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700375void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700376 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
377 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700378}
379
Elliott Hughes985022a2015-04-13 13:04:32 -0700380void ScreenRecoveryUI::ProgressThreadLoop() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700381 double interval = 1.0 / animation_fps;
382 while (true) {
383 double start = now();
384 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700385
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700386 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700387
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700388 // update the installation animation, if active
389 // skip this if we have a text overlay (too expensive to update)
390 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
391 if (!intro_done) {
392 if (current_frame == intro_frames - 1) {
393 intro_done = true;
394 current_frame = 0;
395 } else {
396 ++current_frame;
Doug Zongker211aebc2011-10-28 15:13:10 -0700397 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700398 } else {
399 current_frame = (current_frame + 1) % loop_frames;
400 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700401
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700402 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700403 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700404
405 // move the progress bar forward on timed intervals, if configured
406 int duration = progressScopeDuration;
407 if (progressBarType == DETERMINATE && duration > 0) {
408 double elapsed = now() - progressScopeTime;
409 float p = 1.0 * elapsed / duration;
410 if (p > 1.0) p = 1.0;
411 if (p > progress) {
412 progress = p;
413 redraw = true;
414 }
415 }
416
417 if (redraw) update_progress_locked();
418
419 pthread_mutex_unlock(&updateMutex);
420 double end = now();
421 // minimum of 20ms delay between frames
422 double delay = interval - (end - start);
423 if (delay < 0.02) delay = 0.02;
424 usleep(static_cast<useconds_t>(delay * 1000000));
425 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700426}
427
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700428void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700429 int result = res_create_display_surface(filename, surface);
430 if (result < 0) {
431 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
432 }
Doug Zongkereac881c2014-03-07 09:21:25 -0800433}
434
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700435void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800436 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
437 if (result < 0) {
438 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
439 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700440}
441
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700442static char** Alloc2d(size_t rows, size_t cols) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700443 char** result = new char*[rows];
444 for (size_t i = 0; i < rows; ++i) {
445 result[i] = new char[cols];
446 memset(result[i], 0, cols);
447 }
448 return result;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700449}
450
Tianjie Xu35926c42016-04-28 18:06:26 -0700451// Choose the right background string to display during update.
452void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700453 if (security_update) {
454 LoadLocalizedBitmap("installing_security_text", &installing_text);
455 } else {
456 LoadLocalizedBitmap("installing_text", &installing_text);
457 }
458 Redraw();
Tianjie Xu35926c42016-04-28 18:06:26 -0700459}
460
Sen Jiangd5304492016-12-09 16:20:49 -0800461bool ScreenRecoveryUI::InitTextParams() {
Tao Bao171b4c42017-06-19 23:10:44 -0700462 if (gr_init() < 0) {
463 return false;
464 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700465
Tao Bao171b4c42017-06-19 23:10:44 -0700466 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao4521b702017-06-20 18:11:21 -0700467 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
468 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao171b4c42017-06-19 23:10:44 -0700469 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700470}
471
Tao Bao736d59c2017-01-03 10:15:33 -0800472bool ScreenRecoveryUI::Init(const std::string& locale) {
473 RecoveryUI::Init(locale);
474 if (!InitTextParams()) {
475 return false;
476 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700477
Tao Bao736d59c2017-01-03 10:15:33 -0800478 // Are we portrait or landscape?
479 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
480 // Are we the large variant of our base layout?
481 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700482
Tao Bao736d59c2017-01-03 10:15:33 -0800483 text_ = Alloc2d(text_rows_, text_cols_ + 1);
484 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
485 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800486
Tao Bao736d59c2017-01-03 10:15:33 -0800487 text_col_ = text_row_ = 0;
488 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700489
Tao Bao736d59c2017-01-03 10:15:33 -0800490 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700491
Tao Bao736d59c2017-01-03 10:15:33 -0800492 LoadBitmap("progress_empty", &progressBarEmpty);
493 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700494
Tao Bao736d59c2017-01-03 10:15:33 -0800495 LoadBitmap("stage_empty", &stageMarkerEmpty);
496 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700497
Tao Bao736d59c2017-01-03 10:15:33 -0800498 // Background text for "installing_update" could be "installing update"
499 // or "installing security update". It will be set after UI init according
500 // to commands in BCB.
501 installing_text = nullptr;
502 LoadLocalizedBitmap("erasing_text", &erasing_text);
503 LoadLocalizedBitmap("no_command_text", &no_command_text);
504 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700505
Tao Bao736d59c2017-01-03 10:15:33 -0800506 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700507
Tao Bao736d59c2017-01-03 10:15:33 -0800508 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800509
Tao Bao736d59c2017-01-03 10:15:33 -0800510 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700511}
512
Elliott Hughes498cda62016-04-14 16:49:04 -0700513void ScreenRecoveryUI::LoadAnimation() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700514 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
515 dirent* de;
516 std::vector<std::string> intro_frame_names;
517 std::vector<std::string> loop_frame_names;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700518
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700519 while ((de = readdir(dir.get())) != nullptr) {
520 int value, num_chars;
521 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
522 intro_frame_names.emplace_back(de->d_name, num_chars);
523 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
524 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700525 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700526 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700527
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700528 intro_frames = intro_frame_names.size();
529 loop_frames = loop_frame_names.size();
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700530
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700531 // It's okay to not have an intro.
532 if (intro_frames == 0) intro_done = true;
533 // But you must have an animation.
534 if (loop_frames == 0) abort();
Elliott Hughes498cda62016-04-14 16:49:04 -0700535
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700536 std::sort(intro_frame_names.begin(), intro_frame_names.end());
537 std::sort(loop_frame_names.begin(), loop_frame_names.end());
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700538
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700539 introFrames = new GRSurface*[intro_frames];
540 for (size_t i = 0; i < intro_frames; i++) {
541 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
542 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700543
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700544 loopFrames = new GRSurface*[loop_frames];
545 for (size_t i = 0; i < loop_frames; i++) {
546 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
547 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700548}
549
Elliott Hughes8de52072015-04-08 20:06:50 -0700550void ScreenRecoveryUI::SetBackground(Icon icon) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700551 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700552
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700553 currentIcon = icon;
554 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700555
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700556 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700557}
558
Elliott Hughes8de52072015-04-08 20:06:50 -0700559void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700560 pthread_mutex_lock(&updateMutex);
561 if (progressBarType != type) {
562 progressBarType = type;
563 }
564 progressScopeStart = 0;
565 progressScopeSize = 0;
566 progress = 0;
567 update_progress_locked();
568 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700569}
570
Elliott Hughes8de52072015-04-08 20:06:50 -0700571void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700572 pthread_mutex_lock(&updateMutex);
573 progressBarType = DETERMINATE;
574 progressScopeStart += progressScopeSize;
575 progressScopeSize = portion;
576 progressScopeTime = now();
577 progressScopeDuration = seconds;
578 progress = 0;
579 update_progress_locked();
580 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700581}
582
Elliott Hughes8de52072015-04-08 20:06:50 -0700583void ScreenRecoveryUI::SetProgress(float fraction) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700584 pthread_mutex_lock(&updateMutex);
585 if (fraction < 0.0) fraction = 0.0;
586 if (fraction > 1.0) fraction = 1.0;
587 if (progressBarType == DETERMINATE && fraction > progress) {
588 // Skip updates that aren't visibly different.
589 int width = gr_get_width(progressBarEmpty);
590 float scale = width * progressScopeSize;
591 if ((int)(progress * scale) != (int)(fraction * scale)) {
592 progress = fraction;
593 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700594 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700595 }
596 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700597}
598
Doug Zongkerc87bab12013-11-25 13:53:25 -0800599void ScreenRecoveryUI::SetStage(int current, int max) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700600 pthread_mutex_lock(&updateMutex);
601 stage = current;
602 max_stage = max;
603 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800604}
605
Tao Baob6918c72015-05-19 17:02:16 -0700606void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700607 std::string str;
608 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700609
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700610 if (copy_to_stdout) {
611 fputs(str.c_str(), stdout);
612 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700613
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700614 pthread_mutex_lock(&updateMutex);
615 if (text_rows_ > 0 && text_cols_ > 0) {
616 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
617 if (*ptr == '\n' || text_col_ >= text_cols_) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700618 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700619 text_col_ = 0;
620 text_row_ = (text_row_ + 1) % text_rows_;
621 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
622 }
623 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700624 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700625 text_[text_row_][text_col_] = '\0';
626 update_screen_locked();
627 }
628 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700629}
630
Tao Baob6918c72015-05-19 17:02:16 -0700631void ScreenRecoveryUI::Print(const char* fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700632 va_list ap;
633 va_start(ap, fmt);
634 PrintV(fmt, true, ap);
635 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700636}
637
638void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700639 va_list ap;
640 va_start(ap, fmt);
641 PrintV(fmt, false, ap);
642 va_end(ap);
Tao Baob6918c72015-05-19 17:02:16 -0700643}
644
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700645void ScreenRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700646 pthread_mutex_lock(&updateMutex);
647 if (ch != '\n') text_[text_row_][text_col_++] = ch;
648 if (ch == '\n' || text_col_ >= text_cols_) {
649 text_col_ = 0;
650 ++text_row_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700651
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700652 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
653 }
654 pthread_mutex_unlock(&updateMutex);
Elliott Hughes8de52072015-04-08 20:06:50 -0700655}
656
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700657void ScreenRecoveryUI::ClearText() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700658 pthread_mutex_lock(&updateMutex);
659 text_col_ = 0;
660 text_row_ = 0;
661 text_top_ = 1;
662 for (size_t i = 0; i < text_rows_; ++i) {
663 memset(text_[i], 0, text_cols_ + 1);
664 }
665 pthread_mutex_unlock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700666}
667
668void ScreenRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700669 std::vector<off_t> offsets;
670 offsets.push_back(ftello(fp));
671 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700672
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700673 struct stat sb;
674 fstat(fileno(fp), &sb);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700675
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700676 bool show_prompt = false;
677 while (true) {
678 if (show_prompt) {
679 PrintOnScreenOnly("--(%d%% of %d bytes)--",
680 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
681 static_cast<int>(sb.st_size));
682 Redraw();
683 while (show_prompt) {
684 show_prompt = false;
685 int key = WaitKey();
686 if (key == KEY_POWER || key == KEY_ENTER) {
687 return;
688 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
689 if (offsets.size() <= 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700690 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700691 } else {
692 offsets.pop_back();
693 fseek(fp, offsets.back(), SEEK_SET);
694 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700695 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700696 if (feof(fp)) {
697 return;
698 }
699 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700700 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700701 }
702 ClearText();
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700703 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700704
705 int ch = getc(fp);
706 if (ch == EOF) {
707 while (text_row_ < text_rows_ - 1) PutChar('\n');
708 show_prompt = true;
709 } else {
710 PutChar(ch);
711 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
712 show_prompt = true;
713 }
714 }
715 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700716}
717
Elliott Hughes8de52072015-04-08 20:06:50 -0700718void ScreenRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700719 FILE* fp = fopen_path(filename, "re");
720 if (fp == nullptr) {
721 Print(" Unable to open %s: %s\n", filename, strerror(errno));
722 return;
723 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700724
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700725 char** old_text = text_;
726 size_t old_text_col = text_col_;
727 size_t old_text_row = text_row_;
728 size_t old_text_top = text_top_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700729
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700730 // Swap in the alternate screen and clear it.
731 text_ = file_viewer_text_;
732 ClearText();
Elliott Hughesc0491632015-05-06 12:40:05 -0700733
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700734 ShowFile(fp);
735 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700736
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700737 text_ = old_text;
738 text_col_ = old_text_col;
739 text_row_ = old_text_row;
740 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700741}
742
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700743void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Doug Zongker211aebc2011-10-28 15:13:10 -0700744 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700745 pthread_mutex_lock(&updateMutex);
746 if (text_rows_ > 0 && text_cols_ > 0) {
747 menu_headers_ = headers;
748 size_t i = 0;
749 for (; i < text_rows_ && items[i] != nullptr; ++i) {
750 strncpy(menu_[i], items[i], text_cols_ - 1);
751 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700752 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700753 menu_items = i;
754 show_menu = true;
755 menu_sel = initial_selection;
756 update_screen_locked();
757 }
758 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700759}
760
761int ScreenRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700762 pthread_mutex_lock(&updateMutex);
763 if (show_menu) {
764 int old_sel = menu_sel;
765 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700766
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700767 // Wrap at top and bottom.
768 if (menu_sel < 0) menu_sel = menu_items - 1;
769 if (menu_sel >= menu_items) menu_sel = 0;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700770
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700771 sel = menu_sel;
772 if (menu_sel != old_sel) update_screen_locked();
773 }
774 pthread_mutex_unlock(&updateMutex);
775 return sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700776}
777
778void ScreenRecoveryUI::EndMenu() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700779 pthread_mutex_lock(&updateMutex);
780 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
781 show_menu = false;
782 update_screen_locked();
783 }
784 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700785}
786
Elliott Hughes8de52072015-04-08 20:06:50 -0700787bool ScreenRecoveryUI::IsTextVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700788 pthread_mutex_lock(&updateMutex);
789 int visible = show_text;
790 pthread_mutex_unlock(&updateMutex);
791 return visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700792}
793
Elliott Hughes8de52072015-04-08 20:06:50 -0700794bool ScreenRecoveryUI::WasTextEverVisible() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700795 pthread_mutex_lock(&updateMutex);
796 int ever_visible = show_text_ever;
797 pthread_mutex_unlock(&updateMutex);
798 return ever_visible;
Doug Zongker211aebc2011-10-28 15:13:10 -0700799}
800
Elliott Hughes8de52072015-04-08 20:06:50 -0700801void ScreenRecoveryUI::ShowText(bool visible) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700802 pthread_mutex_lock(&updateMutex);
803 show_text = visible;
804 if (show_text) show_text_ever = true;
805 update_screen_locked();
806 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700807}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700808
Elliott Hughes8de52072015-04-08 20:06:50 -0700809void ScreenRecoveryUI::Redraw() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700810 pthread_mutex_lock(&updateMutex);
811 update_screen_locked();
812 pthread_mutex_unlock(&updateMutex);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700813}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700814
815void ScreenRecoveryUI::KeyLongPress(int) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700816 // Redraw so that if we're in the menu, the highlight
817 // will change color to indicate a successful long press.
818 Redraw();
Elliott Hughes642aaa72015-04-10 12:47:46 -0700819}