blob: cfe36e186010ade28cbcd5f1a99a38a13d29ffed [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
Elliott Hughes95fc63e2015-04-10 19:12:01 -070032#include <vector>
33
Elliott Hughes4b166f02015-12-04 15:30:20 -080034#include <android-base/strings.h>
35#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070036#include <cutils/properties.h>
37
Doug Zongker211aebc2011-10-28 15:13:10 -070038#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070039#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070040#include "minui/minui.h"
41#include "screen_ui.h"
42#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070043
Prashant Malani7a491222016-03-11 10:00:55 -080044#define TEXT_INDENT 4
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
53ScreenRecoveryUI::ScreenRecoveryUI() :
Prashant Malanif7f9e502016-03-10 03:40:20 +000054 currentIcon(NONE),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070055 locale(nullptr),
Elliott Hughes498cda62016-04-14 16:49:04 -070056 intro_done(false),
57 current_frame(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070058 progressBarType(EMPTY),
59 progressScopeStart(0),
60 progressScopeSize(0),
61 progress(0),
62 pagesIdentical(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070063 text_cols_(0),
64 text_rows_(0),
65 text_(nullptr),
66 text_col_(0),
67 text_row_(0),
68 text_top_(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070069 show_text(false),
70 show_text_ever(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070071 menu_(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070072 show_menu(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070073 menu_items(0),
74 menu_sel(0),
Elliott Hughesc0491632015-05-06 12:40:05 -070075 file_viewer_text_(nullptr),
Elliott Hughes498cda62016-04-14 16:49:04 -070076 intro_frames(0),
77 loop_frames(0),
78 animation_fps(30), // TODO: there's currently no way to infer this.
Doug Zongkerc87bab12013-11-25 13:53:25 -080079 stage(-1),
Prashant Malani0ba21cf2016-03-10 14:51:25 -080080 max_stage(-1),
81 rtl_locale(false) {
yetta_wu5b468fc2013-06-25 15:03:11 +080082
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070083 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070084}
85
Elliott Hughes498cda62016-04-14 16:49:04 -070086GRSurface* ScreenRecoveryUI::GetCurrentFrame() {
87 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
88 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
89 }
90 return error_icon;
91}
92
93GRSurface* ScreenRecoveryUI::GetCurrentText() {
94 switch (currentIcon) {
95 case ERASING: return erasing_text;
96 case ERROR: return error_text;
97 case INSTALLING_UPDATE: return installing_text;
98 case NO_COMMAND: return no_command_text;
99 case NONE: abort();
100 }
101}
102
Doug Zongker211aebc2011-10-28 15:13:10 -0700103// Clear the screen and draw the currently selected background icon (if any).
104// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700105void ScreenRecoveryUI::draw_background_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700106 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700107 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800108 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700109
Elliott Hughes498cda62016-04-14 16:49:04 -0700110 if (currentIcon != NONE) {
111 GRSurface* surface = GetCurrentFrame();
112 GRSurface* text_surface = GetCurrentText();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700113
Doug Zongker211aebc2011-10-28 15:13:10 -0700114 int iconWidth = gr_get_width(surface);
115 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700116 int textWidth = gr_get_width(text_surface);
117 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800118 int stageHeight = gr_get_height(stageMarkerEmpty);
119
120 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700121
Doug Zongkereac881c2014-03-07 09:21:25 -0800122 iconX = (gr_fb_width() - iconWidth) / 2;
123 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700124
125 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800126 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700127
Doug Zongker211aebc2011-10-28 15:13:10 -0700128 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800129 if (stageHeight > 0) {
130 int sw = gr_get_width(stageMarkerEmpty);
131 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
132 int y = iconY + iconHeight + 20;
133 for (int i = 0; i < max_stage; ++i) {
134 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
135 0, 0, sw, stageHeight, x, y);
136 x += sw;
137 }
138 }
139
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700140 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700141 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700142 }
143}
144
145// Draw the progress bar (if any) on the screen. Does not flip pages.
146// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700147void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700148 if (currentIcon == ERROR) return;
Elliott Hughes498cda62016-04-14 16:49:04 -0700149 if (progressBarType != DETERMINATE) return;
Doug Zongker69f4b672012-04-26 14:37:53 -0700150
Doug Zongker02ec6b82012-08-22 17:26:40 -0700151 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700152 GRSurface* frame = GetCurrentFrame();
153 gr_blit(frame, 0, 0, gr_get_width(frame), gr_get_height(frame), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700154 }
155
156 if (progressBarType != EMPTY) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700157 int iconHeight = gr_get_height(loopFrames[0]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700158 int width = gr_get_width(progressBarEmpty);
159 int height = gr_get_height(progressBarEmpty);
160
161 int dx = (gr_fb_width() - width)/2;
162 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
163
164 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700165 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700166 gr_fill(dx, dy, width, height);
167
168 if (progressBarType == DETERMINATE) {
169 float p = progressScopeStart + progress * progressScopeSize;
170 int pos = (int) (p * width);
171
Doug Zongker5fa8c232012-09-18 12:37:02 -0700172 if (rtl_locale) {
173 // Fill the progress bar from right to left.
174 if (pos > 0) {
175 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
176 }
177 if (pos < width-1) {
178 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
179 }
180 } else {
181 // Fill the progress bar from left to right.
182 if (pos > 0) {
183 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
184 }
185 if (pos < width-1) {
186 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
187 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700188 }
189 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700190 }
191}
192
Doug Zongkerc0441d12013-07-31 11:28:24 -0700193void ScreenRecoveryUI::SetColor(UIElement e) {
194 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700195 case INFO:
196 gr_color(249, 194, 0, 255);
197 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700198 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700199 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700200 break;
201 case MENU:
202 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700203 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700204 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700205 case MENU_SEL_BG_ACTIVE:
206 gr_color(0, 156, 100, 255);
207 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700208 case MENU_SEL_FG:
209 gr_color(255, 255, 255, 255);
210 break;
211 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700212 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700213 break;
214 case TEXT_FILL:
215 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700216 break;
217 default:
218 gr_color(255, 255, 255, 255);
219 break;
220 }
221}
Doug Zongker211aebc2011-10-28 15:13:10 -0700222
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700223void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
224 SetColor(MENU);
225 *y += 4;
226 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700227 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700228}
229
Prashant Malani7a491222016-03-11 10:00:55 -0800230void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
231 gr_text(x, *y, line, bold);
232 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700233}
234
Prashant Malani7a491222016-03-11 10:00:55 -0800235void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700236 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800237 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700238 }
239}
240
241static const char* REGULAR_HELP[] = {
242 "Use volume up/down and power.",
243 NULL
244};
245
246static const char* LONG_PRESS_HELP[] = {
247 "Any button cycles highlight.",
248 "Long-press activates.",
249 NULL
250};
251
Doug Zongker211aebc2011-10-28 15:13:10 -0700252// Redraw everything on the screen. Does not flip pages.
253// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700254void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800255 if (!show_text) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700256 draw_background_locked();
Doug Zongker39cf4172014-03-06 16:16:05 -0800257 draw_progress_locked();
258 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700259 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800260 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700261
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800262 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700263 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700264 char recovery_fingerprint[PROPERTY_VALUE_MAX];
265 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
266
267 SetColor(INFO);
Prashant Malani7a491222016-03-11 10:00:55 -0800268 DrawTextLine(TEXT_INDENT, &y, "Android Recovery", true);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700269 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Prashant Malani7a491222016-03-11 10:00:55 -0800270 DrawTextLine(TEXT_INDENT, &y, chunk.c_str(), false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700271 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700272 DrawTextLines(TEXT_INDENT, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700273
Doug Zongkerc0441d12013-07-31 11:28:24 -0700274 SetColor(HEADER);
Prashant Malani7a491222016-03-11 10:00:55 -0800275 DrawTextLines(TEXT_INDENT, &y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700276
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700277 SetColor(MENU);
278 DrawHorizontalRule(&y);
279 y += 4;
280 for (int i = 0; i < menu_items; ++i) {
281 if (i == menu_sel) {
282 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700283 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800284 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700285 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700286 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700287 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700288 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700289 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700290 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700291 }
Prashant Malani7a491222016-03-11 10:00:55 -0800292 y += char_height_ + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700293 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700294 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700295 }
296
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800297 // display from the bottom up, until we hit the top of the
298 // screen, the bottom of the menu, or we've displayed the
299 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700300 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700301 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700302 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800303 for (int ty = gr_fb_height() - char_height_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700304 ty >= y && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800305 ty -= char_height_, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700306 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800307 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700308 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700309 }
310 }
311}
312
313// Redraw everything on the screen and flip the screen (make it visible).
314// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700315void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700316 draw_screen_locked();
317 gr_flip();
318}
319
320// Updates only the progress bar, if possible, otherwise redraws the screen.
321// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700322void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700323 if (show_text || !pagesIdentical) {
324 draw_screen_locked(); // Must redraw the whole screen
325 pagesIdentical = true;
326 } else {
327 draw_progress_locked(); // Draw only the progress bar and overlays
328 }
329 gr_flip();
330}
331
332// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700333void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
334 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700335 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700336}
337
Elliott Hughes985022a2015-04-13 13:04:32 -0700338void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700339 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700340 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700341 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700342 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700343
Elliott Hughes498cda62016-04-14 16:49:04 -0700344 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700345
346 // update the installation animation, if active
347 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700348 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
349 if (!intro_done) {
350 if (current_frame == intro_frames - 1) {
351 intro_done = true;
352 current_frame = 0;
353 } else {
354 ++current_frame;
355 }
356 } else {
357 current_frame = (current_frame + 1) % loop_frames;
358 }
359
360 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700361 }
362
Doug Zongker211aebc2011-10-28 15:13:10 -0700363 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700364 int duration = progressScopeDuration;
365 if (progressBarType == DETERMINATE && duration > 0) {
366 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700367 float p = 1.0 * elapsed / duration;
368 if (p > 1.0) p = 1.0;
369 if (p > progress) {
370 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700371 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700372 }
373 }
374
Doug Zongker32a0a472011-11-01 11:00:20 -0700375 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700376
Doug Zongker32a0a472011-11-01 11:00:20 -0700377 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700378 double end = now();
379 // minimum of 20ms delay between frames
380 double delay = interval - (end-start);
381 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700382 usleep(static_cast<useconds_t>(delay * 1000000));
Doug Zongker211aebc2011-10-28 15:13:10 -0700383 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700384}
385
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700386void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700387 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700388 if (result < 0) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700389 LOGE("missing bitmap %s (error %d)\n", filename, result);
Doug Zongkereac881c2014-03-07 09:21:25 -0800390 }
391}
392
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700393void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700394 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700395 if (result < 0) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700396 LOGE("missing bitmap %s (error %d)\n", filename, result);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700397 }
398}
399
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700400static char** Alloc2d(size_t rows, size_t cols) {
401 char** result = new char*[rows];
402 for (size_t i = 0; i < rows; ++i) {
403 result[i] = new char[cols];
404 memset(result[i], 0, cols);
405 }
406 return result;
407}
408
Elliott Hughes8de52072015-04-08 20:06:50 -0700409void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700410 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700411
Prashant Malani7a491222016-03-11 10:00:55 -0800412 gr_font_size(&char_width_, &char_height_);
413 text_rows_ = gr_fb_height() / char_height_;
414 text_cols_ = gr_fb_width() / char_width_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700415
Elliott Hughesc0491632015-05-06 12:40:05 -0700416 text_ = Alloc2d(text_rows_, text_cols_ + 1);
417 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
418 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800419
Elliott Hughesc0491632015-05-06 12:40:05 -0700420 text_col_ = text_row_ = 0;
421 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700422
Elliott Hughes498cda62016-04-14 16:49:04 -0700423 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700424
Doug Zongker211aebc2011-10-28 15:13:10 -0700425 LoadBitmap("progress_empty", &progressBarEmpty);
426 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700427
Doug Zongkerc87bab12013-11-25 13:53:25 -0800428 LoadBitmap("stage_empty", &stageMarkerEmpty);
429 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700430
Elliott Hughes498cda62016-04-14 16:49:04 -0700431 LoadLocalizedBitmap("installing_text", &installing_text);
432 LoadLocalizedBitmap("erasing_text", &erasing_text);
433 LoadLocalizedBitmap("no_command_text", &no_command_text);
434 LoadLocalizedBitmap("error_text", &error_text);
435
436 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700437
Elliott Hughes985022a2015-04-13 13:04:32 -0700438 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700439
440 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700441}
442
Elliott Hughes498cda62016-04-14 16:49:04 -0700443void ScreenRecoveryUI::LoadAnimation() {
444 // How many frames of intro and loop do we have?
445 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
446 dirent* de;
447 while ((de = readdir(dir.get())) != nullptr) {
448 int value;
449 if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) {
450 intro_frames = value + 1;
451 } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) {
452 loop_frames = value + 1;
453 }
454 }
455
456 // It's okay to not have an intro.
457 if (intro_frames == 0) intro_done = true;
458 // But you must have an animation.
459 if (loop_frames == 0) abort();
460
461 introFrames = new GRSurface*[intro_frames];
462 for (int i = 0; i < intro_frames; ++i) {
463 LoadBitmap(android::base::StringPrintf("intro%02d", i).c_str(), &introFrames[i]);
464 }
465
466 loopFrames = new GRSurface*[loop_frames];
467 for (int i = 0; i < loop_frames; ++i) {
468 LoadBitmap(android::base::StringPrintf("loop%02d", i).c_str(), &loopFrames[i]);
469 }
470}
471
Doug Zongkera418aa72014-03-17 12:10:02 -0700472void ScreenRecoveryUI::SetLocale(const char* new_locale) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700473 this->locale = new_locale;
474 this->rtl_locale = false;
475
476 if (locale) {
Doug Zongker5fa8c232012-09-18 12:37:02 -0700477 char* lang = strdup(locale);
478 for (char* p = lang; *p; ++p) {
479 if (*p == '_') {
480 *p = '\0';
481 break;
482 }
483 }
484
Elliott Hughes498cda62016-04-14 16:49:04 -0700485 // A bit cheesy: keep an explicit list of supported RTL languages.
Doug Zongker5fa8c232012-09-18 12:37:02 -0700486 if (strcmp(lang, "ar") == 0 || // Arabic
487 strcmp(lang, "fa") == 0 || // Persian (Farsi)
488 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700489 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
490 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700491 rtl_locale = true;
492 }
493 free(lang);
494 }
495}
496
Elliott Hughes8de52072015-04-08 20:06:50 -0700497void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700498 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700499
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700500 currentIcon = icon;
501 update_screen_locked();
502
Doug Zongker211aebc2011-10-28 15:13:10 -0700503 pthread_mutex_unlock(&updateMutex);
504}
505
Elliott Hughes8de52072015-04-08 20:06:50 -0700506void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700507 pthread_mutex_lock(&updateMutex);
508 if (progressBarType != type) {
509 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700510 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700511 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700512 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700513 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700514 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700515 pthread_mutex_unlock(&updateMutex);
516}
517
Elliott Hughes8de52072015-04-08 20:06:50 -0700518void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700519 pthread_mutex_lock(&updateMutex);
520 progressBarType = DETERMINATE;
521 progressScopeStart += progressScopeSize;
522 progressScopeSize = portion;
523 progressScopeTime = now();
524 progressScopeDuration = seconds;
525 progress = 0;
526 update_progress_locked();
527 pthread_mutex_unlock(&updateMutex);
528}
529
Elliott Hughes8de52072015-04-08 20:06:50 -0700530void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700531 pthread_mutex_lock(&updateMutex);
532 if (fraction < 0.0) fraction = 0.0;
533 if (fraction > 1.0) fraction = 1.0;
534 if (progressBarType == DETERMINATE && fraction > progress) {
535 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800536 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700537 float scale = width * progressScopeSize;
538 if ((int) (progress * scale) != (int) (fraction * scale)) {
539 progress = fraction;
540 update_progress_locked();
541 }
542 }
543 pthread_mutex_unlock(&updateMutex);
544}
545
Doug Zongkerc87bab12013-11-25 13:53:25 -0800546void ScreenRecoveryUI::SetStage(int current, int max) {
547 pthread_mutex_lock(&updateMutex);
548 stage = current;
549 max_stage = max;
550 pthread_mutex_unlock(&updateMutex);
551}
552
Tao Baob6918c72015-05-19 17:02:16 -0700553void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
554 std::string str;
555 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700556
Tao Baob6918c72015-05-19 17:02:16 -0700557 if (copy_to_stdout) {
558 fputs(str.c_str(), stdout);
559 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700560
Doug Zongker211aebc2011-10-28 15:13:10 -0700561 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700562 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700563 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700564 if (*ptr == '\n' || text_col_ >= text_cols_) {
565 text_[text_row_][text_col_] = '\0';
566 text_col_ = 0;
567 text_row_ = (text_row_ + 1) % text_rows_;
568 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700569 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700570 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700571 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700572 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700573 update_screen_locked();
574 }
575 pthread_mutex_unlock(&updateMutex);
576}
577
Tao Baob6918c72015-05-19 17:02:16 -0700578void ScreenRecoveryUI::Print(const char* fmt, ...) {
579 va_list ap;
580 va_start(ap, fmt);
581 PrintV(fmt, true, ap);
582 va_end(ap);
583}
584
585void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
586 va_list ap;
587 va_start(ap, fmt);
588 PrintV(fmt, false, ap);
589 va_end(ap);
590}
591
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700592void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700593 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700594 if (ch != '\n') text_[text_row_][text_col_++] = ch;
595 if (ch == '\n' || text_col_ >= text_cols_) {
596 text_col_ = 0;
597 ++text_row_;
598
599 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700600 }
601 pthread_mutex_unlock(&updateMutex);
602}
603
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700604void ScreenRecoveryUI::ClearText() {
605 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700606 text_col_ = 0;
607 text_row_ = 0;
608 text_top_ = 1;
609 for (size_t i = 0; i < text_rows_; ++i) {
610 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700611 }
612 pthread_mutex_unlock(&updateMutex);
613}
614
615void ScreenRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700616 std::vector<off_t> offsets;
617 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700618 ClearText();
619
620 struct stat sb;
621 fstat(fileno(fp), &sb);
622
623 bool show_prompt = false;
624 while (true) {
625 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700626 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700627 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700628 static_cast<int>(sb.st_size));
629 Redraw();
630 while (show_prompt) {
631 show_prompt = false;
632 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700633 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700634 return;
635 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
636 if (offsets.size() <= 1) {
637 show_prompt = true;
638 } else {
639 offsets.pop_back();
640 fseek(fp, offsets.back(), SEEK_SET);
641 }
642 } else {
643 if (feof(fp)) {
644 return;
645 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700646 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700647 }
648 }
649 ClearText();
650 }
651
652 int ch = getc(fp);
653 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700654 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700655 show_prompt = true;
656 } else {
657 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700658 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700659 show_prompt = true;
660 }
661 }
662 }
663}
664
Elliott Hughes8de52072015-04-08 20:06:50 -0700665void ScreenRecoveryUI::ShowFile(const char* filename) {
666 FILE* fp = fopen_path(filename, "re");
667 if (fp == nullptr) {
668 Print(" Unable to open %s: %s\n", filename, strerror(errno));
669 return;
670 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700671
672 char** old_text = text_;
673 size_t old_text_col = text_col_;
674 size_t old_text_row = text_row_;
675 size_t old_text_top = text_top_;
676
677 // Swap in the alternate screen and clear it.
678 text_ = file_viewer_text_;
679 ClearText();
680
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700681 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700682 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700683
684 text_ = old_text;
685 text_col_ = old_text_col;
686 text_row_ = old_text_row;
687 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700688}
689
Doug Zongker211aebc2011-10-28 15:13:10 -0700690void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
691 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700692 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700693 if (text_rows_ > 0 && text_cols_ > 0) {
694 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700695 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700696 for (; i < text_rows_ && items[i] != nullptr; ++i) {
697 strncpy(menu_[i], items[i], text_cols_ - 1);
698 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700699 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700700 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700701 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700702 menu_sel = initial_selection;
703 update_screen_locked();
704 }
705 pthread_mutex_unlock(&updateMutex);
706}
707
708int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700709 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700710 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700711 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700712 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700713
714 // Wrap at top and bottom.
715 if (menu_sel < 0) menu_sel = menu_items - 1;
716 if (menu_sel >= menu_items) menu_sel = 0;
717
Doug Zongker211aebc2011-10-28 15:13:10 -0700718 sel = menu_sel;
719 if (menu_sel != old_sel) update_screen_locked();
720 }
721 pthread_mutex_unlock(&updateMutex);
722 return sel;
723}
724
725void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700726 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700727 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700728 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700729 update_screen_locked();
730 }
731 pthread_mutex_unlock(&updateMutex);
732}
733
Elliott Hughes8de52072015-04-08 20:06:50 -0700734bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700735 pthread_mutex_lock(&updateMutex);
736 int visible = show_text;
737 pthread_mutex_unlock(&updateMutex);
738 return visible;
739}
740
Elliott Hughes8de52072015-04-08 20:06:50 -0700741bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700742 pthread_mutex_lock(&updateMutex);
743 int ever_visible = show_text_ever;
744 pthread_mutex_unlock(&updateMutex);
745 return ever_visible;
746}
747
Elliott Hughes8de52072015-04-08 20:06:50 -0700748void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700749 pthread_mutex_lock(&updateMutex);
750 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700751 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700752 update_screen_locked();
753 pthread_mutex_unlock(&updateMutex);
754}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700755
Elliott Hughes8de52072015-04-08 20:06:50 -0700756void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700757 pthread_mutex_lock(&updateMutex);
758 update_screen_locked();
759 pthread_mutex_unlock(&updateMutex);
760}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700761
762void ScreenRecoveryUI::KeyLongPress(int) {
763 // Redraw so that if we're in the menu, the highlight
764 // will change color to indicate a successful long press.
765 Redraw();
766}