blob: 5d6f17b7dfef8871bc75c5b24551df528c1f957a [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;
149
Doug Zongker02ec6b82012-08-22 17:26:40 -0700150 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700151 GRSurface* frame = GetCurrentFrame();
152 gr_blit(frame, 0, 0, gr_get_width(frame), gr_get_height(frame), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700153 }
154
155 if (progressBarType != EMPTY) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700156 int iconHeight = gr_get_height(loopFrames[0]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700157 int width = gr_get_width(progressBarEmpty);
158 int height = gr_get_height(progressBarEmpty);
159
160 int dx = (gr_fb_width() - width)/2;
161 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
162
163 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700164 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700165 gr_fill(dx, dy, width, height);
166
167 if (progressBarType == DETERMINATE) {
168 float p = progressScopeStart + progress * progressScopeSize;
169 int pos = (int) (p * width);
170
Doug Zongker5fa8c232012-09-18 12:37:02 -0700171 if (rtl_locale) {
172 // Fill the progress bar from right to left.
173 if (pos > 0) {
174 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
175 }
176 if (pos < width-1) {
177 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
178 }
179 } else {
180 // Fill the progress bar from left to right.
181 if (pos > 0) {
182 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
183 }
184 if (pos < width-1) {
185 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
186 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700187 }
188 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700189 }
190}
191
Doug Zongkerc0441d12013-07-31 11:28:24 -0700192void ScreenRecoveryUI::SetColor(UIElement e) {
193 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700194 case INFO:
195 gr_color(249, 194, 0, 255);
196 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700197 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700198 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700199 break;
200 case MENU:
201 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700202 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700203 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700204 case MENU_SEL_BG_ACTIVE:
205 gr_color(0, 156, 100, 255);
206 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700207 case MENU_SEL_FG:
208 gr_color(255, 255, 255, 255);
209 break;
210 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700211 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700212 break;
213 case TEXT_FILL:
214 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700215 break;
216 default:
217 gr_color(255, 255, 255, 255);
218 break;
219 }
220}
Doug Zongker211aebc2011-10-28 15:13:10 -0700221
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700222void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
223 SetColor(MENU);
224 *y += 4;
225 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700226 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700227}
228
Prashant Malani7a491222016-03-11 10:00:55 -0800229void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
230 gr_text(x, *y, line, bold);
231 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700232}
233
Prashant Malani7a491222016-03-11 10:00:55 -0800234void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700235 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800236 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700237 }
238}
239
240static const char* REGULAR_HELP[] = {
241 "Use volume up/down and power.",
242 NULL
243};
244
245static const char* LONG_PRESS_HELP[] = {
246 "Any button cycles highlight.",
247 "Long-press activates.",
248 NULL
249};
250
Doug Zongker211aebc2011-10-28 15:13:10 -0700251// Redraw everything on the screen. Does not flip pages.
252// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700253void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800254 if (!show_text) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700255 draw_background_locked();
Doug Zongker39cf4172014-03-06 16:16:05 -0800256 draw_progress_locked();
257 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700258 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800259 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700260
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800261 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700262 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700263 char recovery_fingerprint[PROPERTY_VALUE_MAX];
264 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
265
266 SetColor(INFO);
Prashant Malani7a491222016-03-11 10:00:55 -0800267 DrawTextLine(TEXT_INDENT, &y, "Android Recovery", true);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700268 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Prashant Malani7a491222016-03-11 10:00:55 -0800269 DrawTextLine(TEXT_INDENT, &y, chunk.c_str(), false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700270 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700271 DrawTextLines(TEXT_INDENT, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700272
Doug Zongkerc0441d12013-07-31 11:28:24 -0700273 SetColor(HEADER);
Prashant Malani7a491222016-03-11 10:00:55 -0800274 DrawTextLines(TEXT_INDENT, &y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700275
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700276 SetColor(MENU);
277 DrawHorizontalRule(&y);
278 y += 4;
279 for (int i = 0; i < menu_items; ++i) {
280 if (i == menu_sel) {
281 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700282 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800283 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700284 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700285 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700286 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700287 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700288 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700289 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700290 }
Prashant Malani7a491222016-03-11 10:00:55 -0800291 y += char_height_ + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700292 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700293 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700294 }
295
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800296 // display from the bottom up, until we hit the top of the
297 // screen, the bottom of the menu, or we've displayed the
298 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700299 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700300 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700301 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800302 for (int ty = gr_fb_height() - char_height_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700303 ty >= y && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800304 ty -= char_height_, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700305 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800306 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700307 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700308 }
309 }
310}
311
312// Redraw everything on the screen and flip the screen (make it visible).
313// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700314void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700315 draw_screen_locked();
316 gr_flip();
317}
318
319// Updates only the progress bar, if possible, otherwise redraws the screen.
320// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700321void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700322 if (show_text || !pagesIdentical) {
323 draw_screen_locked(); // Must redraw the whole screen
324 pagesIdentical = true;
325 } else {
326 draw_progress_locked(); // Draw only the progress bar and overlays
327 }
328 gr_flip();
329}
330
331// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700332void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
333 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700334 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700335}
336
Elliott Hughes985022a2015-04-13 13:04:32 -0700337void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700338 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700339 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700340 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700341 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700342
Elliott Hughes498cda62016-04-14 16:49:04 -0700343 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700344
345 // update the installation animation, if active
346 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700347 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
348 if (!intro_done) {
349 if (current_frame == intro_frames - 1) {
350 intro_done = true;
351 current_frame = 0;
352 } else {
353 ++current_frame;
354 }
355 } else {
356 current_frame = (current_frame + 1) % loop_frames;
357 }
358
359 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700360 }
361
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700363 int duration = progressScopeDuration;
364 if (progressBarType == DETERMINATE && duration > 0) {
365 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700366 float p = 1.0 * elapsed / duration;
367 if (p > 1.0) p = 1.0;
368 if (p > progress) {
369 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700370 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700371 }
372 }
373
Doug Zongker32a0a472011-11-01 11:00:20 -0700374 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700375
Doug Zongker32a0a472011-11-01 11:00:20 -0700376 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 double end = now();
378 // minimum of 20ms delay between frames
379 double delay = interval - (end-start);
380 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700381 usleep(static_cast<useconds_t>(delay * 1000000));
Doug Zongker211aebc2011-10-28 15:13:10 -0700382 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700383}
384
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700385void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700386 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700387 if (result < 0) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700388 LOGE("missing bitmap %s (error %d)\n", filename, result);
Doug Zongkereac881c2014-03-07 09:21:25 -0800389 }
390}
391
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700392void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700393 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700394 if (result < 0) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700395 LOGE("missing bitmap %s (error %d)\n", filename, result);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700396 }
397}
398
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700399static char** Alloc2d(size_t rows, size_t cols) {
400 char** result = new char*[rows];
401 for (size_t i = 0; i < rows; ++i) {
402 result[i] = new char[cols];
403 memset(result[i], 0, cols);
404 }
405 return result;
406}
407
Elliott Hughes8de52072015-04-08 20:06:50 -0700408void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700409 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700410
Prashant Malani7a491222016-03-11 10:00:55 -0800411 gr_font_size(&char_width_, &char_height_);
412 text_rows_ = gr_fb_height() / char_height_;
413 text_cols_ = gr_fb_width() / char_width_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700414
Elliott Hughesc0491632015-05-06 12:40:05 -0700415 text_ = Alloc2d(text_rows_, text_cols_ + 1);
416 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
417 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800418
Elliott Hughesc0491632015-05-06 12:40:05 -0700419 text_col_ = text_row_ = 0;
420 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700421
Elliott Hughes498cda62016-04-14 16:49:04 -0700422 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700423
Doug Zongker211aebc2011-10-28 15:13:10 -0700424 LoadBitmap("progress_empty", &progressBarEmpty);
425 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700426
Doug Zongkerc87bab12013-11-25 13:53:25 -0800427 LoadBitmap("stage_empty", &stageMarkerEmpty);
428 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700429
Elliott Hughes498cda62016-04-14 16:49:04 -0700430 LoadLocalizedBitmap("installing_text", &installing_text);
431 LoadLocalizedBitmap("erasing_text", &erasing_text);
432 LoadLocalizedBitmap("no_command_text", &no_command_text);
433 LoadLocalizedBitmap("error_text", &error_text);
434
435 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700436
Elliott Hughes985022a2015-04-13 13:04:32 -0700437 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700438
439 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700440}
441
Elliott Hughes498cda62016-04-14 16:49:04 -0700442void ScreenRecoveryUI::LoadAnimation() {
443 // How many frames of intro and loop do we have?
444 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
445 dirent* de;
446 while ((de = readdir(dir.get())) != nullptr) {
447 int value;
448 if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) {
449 intro_frames = value + 1;
450 } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) {
451 loop_frames = value + 1;
452 }
453 }
454
455 // It's okay to not have an intro.
456 if (intro_frames == 0) intro_done = true;
457 // But you must have an animation.
458 if (loop_frames == 0) abort();
459
460 introFrames = new GRSurface*[intro_frames];
461 for (int i = 0; i < intro_frames; ++i) {
462 LoadBitmap(android::base::StringPrintf("intro%02d", i).c_str(), &introFrames[i]);
463 }
464
465 loopFrames = new GRSurface*[loop_frames];
466 for (int i = 0; i < loop_frames; ++i) {
467 LoadBitmap(android::base::StringPrintf("loop%02d", i).c_str(), &loopFrames[i]);
468 }
469}
470
Doug Zongkera418aa72014-03-17 12:10:02 -0700471void ScreenRecoveryUI::SetLocale(const char* new_locale) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700472 this->locale = new_locale;
473 this->rtl_locale = false;
474
475 if (locale) {
Doug Zongker5fa8c232012-09-18 12:37:02 -0700476 char* lang = strdup(locale);
477 for (char* p = lang; *p; ++p) {
478 if (*p == '_') {
479 *p = '\0';
480 break;
481 }
482 }
483
Elliott Hughes498cda62016-04-14 16:49:04 -0700484 // A bit cheesy: keep an explicit list of supported RTL languages.
Doug Zongker5fa8c232012-09-18 12:37:02 -0700485 if (strcmp(lang, "ar") == 0 || // Arabic
486 strcmp(lang, "fa") == 0 || // Persian (Farsi)
487 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700488 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
489 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700490 rtl_locale = true;
491 }
492 free(lang);
493 }
494}
495
Elliott Hughes8de52072015-04-08 20:06:50 -0700496void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700497 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700498
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700499 currentIcon = icon;
500 update_screen_locked();
501
Doug Zongker211aebc2011-10-28 15:13:10 -0700502 pthread_mutex_unlock(&updateMutex);
503}
504
Elliott Hughes8de52072015-04-08 20:06:50 -0700505void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700506 pthread_mutex_lock(&updateMutex);
507 if (progressBarType != type) {
508 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700509 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700510 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700511 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700512 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700513 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700514 pthread_mutex_unlock(&updateMutex);
515}
516
Elliott Hughes8de52072015-04-08 20:06:50 -0700517void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700518 pthread_mutex_lock(&updateMutex);
519 progressBarType = DETERMINATE;
520 progressScopeStart += progressScopeSize;
521 progressScopeSize = portion;
522 progressScopeTime = now();
523 progressScopeDuration = seconds;
524 progress = 0;
525 update_progress_locked();
526 pthread_mutex_unlock(&updateMutex);
527}
528
Elliott Hughes8de52072015-04-08 20:06:50 -0700529void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700530 pthread_mutex_lock(&updateMutex);
531 if (fraction < 0.0) fraction = 0.0;
532 if (fraction > 1.0) fraction = 1.0;
533 if (progressBarType == DETERMINATE && fraction > progress) {
534 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800535 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700536 float scale = width * progressScopeSize;
537 if ((int) (progress * scale) != (int) (fraction * scale)) {
538 progress = fraction;
539 update_progress_locked();
540 }
541 }
542 pthread_mutex_unlock(&updateMutex);
543}
544
Doug Zongkerc87bab12013-11-25 13:53:25 -0800545void ScreenRecoveryUI::SetStage(int current, int max) {
546 pthread_mutex_lock(&updateMutex);
547 stage = current;
548 max_stage = max;
549 pthread_mutex_unlock(&updateMutex);
550}
551
Tao Baob6918c72015-05-19 17:02:16 -0700552void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
553 std::string str;
554 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700555
Tao Baob6918c72015-05-19 17:02:16 -0700556 if (copy_to_stdout) {
557 fputs(str.c_str(), stdout);
558 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700559
Doug Zongker211aebc2011-10-28 15:13:10 -0700560 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700561 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700562 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700563 if (*ptr == '\n' || text_col_ >= text_cols_) {
564 text_[text_row_][text_col_] = '\0';
565 text_col_ = 0;
566 text_row_ = (text_row_ + 1) % text_rows_;
567 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700568 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700569 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700570 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700571 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700572 update_screen_locked();
573 }
574 pthread_mutex_unlock(&updateMutex);
575}
576
Tao Baob6918c72015-05-19 17:02:16 -0700577void ScreenRecoveryUI::Print(const char* fmt, ...) {
578 va_list ap;
579 va_start(ap, fmt);
580 PrintV(fmt, true, ap);
581 va_end(ap);
582}
583
584void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
585 va_list ap;
586 va_start(ap, fmt);
587 PrintV(fmt, false, ap);
588 va_end(ap);
589}
590
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700591void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700592 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700593 if (ch != '\n') text_[text_row_][text_col_++] = ch;
594 if (ch == '\n' || text_col_ >= text_cols_) {
595 text_col_ = 0;
596 ++text_row_;
597
598 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700599 }
600 pthread_mutex_unlock(&updateMutex);
601}
602
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700603void ScreenRecoveryUI::ClearText() {
604 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700605 text_col_ = 0;
606 text_row_ = 0;
607 text_top_ = 1;
608 for (size_t i = 0; i < text_rows_; ++i) {
609 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700610 }
611 pthread_mutex_unlock(&updateMutex);
612}
613
614void ScreenRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700615 std::vector<off_t> offsets;
616 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700617 ClearText();
618
619 struct stat sb;
620 fstat(fileno(fp), &sb);
621
622 bool show_prompt = false;
623 while (true) {
624 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700625 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700626 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700627 static_cast<int>(sb.st_size));
628 Redraw();
629 while (show_prompt) {
630 show_prompt = false;
631 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700632 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700633 return;
634 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
635 if (offsets.size() <= 1) {
636 show_prompt = true;
637 } else {
638 offsets.pop_back();
639 fseek(fp, offsets.back(), SEEK_SET);
640 }
641 } else {
642 if (feof(fp)) {
643 return;
644 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700645 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700646 }
647 }
648 ClearText();
649 }
650
651 int ch = getc(fp);
652 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700653 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700654 show_prompt = true;
655 } else {
656 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700657 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700658 show_prompt = true;
659 }
660 }
661 }
662}
663
Elliott Hughes8de52072015-04-08 20:06:50 -0700664void ScreenRecoveryUI::ShowFile(const char* filename) {
665 FILE* fp = fopen_path(filename, "re");
666 if (fp == nullptr) {
667 Print(" Unable to open %s: %s\n", filename, strerror(errno));
668 return;
669 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700670
671 char** old_text = text_;
672 size_t old_text_col = text_col_;
673 size_t old_text_row = text_row_;
674 size_t old_text_top = text_top_;
675
676 // Swap in the alternate screen and clear it.
677 text_ = file_viewer_text_;
678 ClearText();
679
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700680 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700681 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700682
683 text_ = old_text;
684 text_col_ = old_text_col;
685 text_row_ = old_text_row;
686 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700687}
688
Doug Zongker211aebc2011-10-28 15:13:10 -0700689void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
690 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700691 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700692 if (text_rows_ > 0 && text_cols_ > 0) {
693 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700694 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700695 for (; i < text_rows_ && items[i] != nullptr; ++i) {
696 strncpy(menu_[i], items[i], text_cols_ - 1);
697 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700698 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700699 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700700 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700701 menu_sel = initial_selection;
702 update_screen_locked();
703 }
704 pthread_mutex_unlock(&updateMutex);
705}
706
707int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700708 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700709 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700710 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700711 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700712
713 // Wrap at top and bottom.
714 if (menu_sel < 0) menu_sel = menu_items - 1;
715 if (menu_sel >= menu_items) menu_sel = 0;
716
Doug Zongker211aebc2011-10-28 15:13:10 -0700717 sel = menu_sel;
718 if (menu_sel != old_sel) update_screen_locked();
719 }
720 pthread_mutex_unlock(&updateMutex);
721 return sel;
722}
723
724void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700725 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700726 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700727 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700728 update_screen_locked();
729 }
730 pthread_mutex_unlock(&updateMutex);
731}
732
Elliott Hughes8de52072015-04-08 20:06:50 -0700733bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700734 pthread_mutex_lock(&updateMutex);
735 int visible = show_text;
736 pthread_mutex_unlock(&updateMutex);
737 return visible;
738}
739
Elliott Hughes8de52072015-04-08 20:06:50 -0700740bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700741 pthread_mutex_lock(&updateMutex);
742 int ever_visible = show_text_ever;
743 pthread_mutex_unlock(&updateMutex);
744 return ever_visible;
745}
746
Elliott Hughes8de52072015-04-08 20:06:50 -0700747void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700748 pthread_mutex_lock(&updateMutex);
749 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700750 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700751 update_screen_locked();
752 pthread_mutex_unlock(&updateMutex);
753}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700754
Elliott Hughes8de52072015-04-08 20:06:50 -0700755void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700756 pthread_mutex_lock(&updateMutex);
757 update_screen_locked();
758 pthread_mutex_unlock(&updateMutex);
759}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700760
761void ScreenRecoveryUI::KeyLongPress(int) {
762 // Redraw so that if we're in the menu, the highlight
763 // will change color to indicate a successful long press.
764 Redraw();
765}