blob: 5e73d37c49b6ba3328aebf2bd294b2c811e48b5d [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
17#include <errno.h>
18#include <fcntl.h>
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
Elliott Hughes95fc63e2015-04-10 19:12:01 -070031#include <vector>
32
Elliott Hughes8fd86d72015-04-13 14:36:02 -070033#include "base/strings.h"
34#include "cutils/properties.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070035#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070036#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070037#include "minui/minui.h"
38#include "screen_ui.h"
39#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070040
Doug Zongker55a36ac2013-03-04 15:49:02 -080041static int char_width;
42static int char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -070043
Doug Zongker211aebc2011-10-28 15:13:10 -070044// Return the current time as a double (including fractions of a second).
45static double now() {
46 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070047 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070048 return tv.tv_sec + tv.tv_usec / 1000000.0;
49}
50
51ScreenRecoveryUI::ScreenRecoveryUI() :
52 currentIcon(NONE),
53 installingFrame(0),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070054 locale(nullptr),
Doug Zongker5fa8c232012-09-18 12:37:02 -070055 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070056 progressBarType(EMPTY),
57 progressScopeStart(0),
58 progressScopeSize(0),
59 progress(0),
60 pagesIdentical(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070061 text(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070062 text_cols(0),
63 text_rows(0),
64 text_col(0),
65 text_row(0),
66 text_top(0),
67 show_text(false),
68 show_text_ever(false),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070069 menu(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070070 show_menu(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070071 menu_items(0),
72 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070073 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080074 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080075 stage(-1),
76 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080077
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070078 for (int i = 0; i < 5; i++) {
79 backgroundIcon[i] = nullptr;
80 }
81 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070082}
83
Doug Zongker211aebc2011-10-28 15:13:10 -070084// Clear the screen and draw the currently selected background icon (if any).
85// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070086void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070087 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070088 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080089 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070090
91 if (icon) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070092 GRSurface* surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080093 if (icon == INSTALLING_UPDATE || icon == ERASING) {
94 surface = installation[installingFrame];
95 }
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070096 GRSurface* text_surface = backgroundText[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -070097
Doug Zongker211aebc2011-10-28 15:13:10 -070098 int iconWidth = gr_get_width(surface);
99 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700100 int textWidth = gr_get_width(text_surface);
101 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800102 int stageHeight = gr_get_height(stageMarkerEmpty);
103
104 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700105
Doug Zongkereac881c2014-03-07 09:21:25 -0800106 iconX = (gr_fb_width() - iconWidth) / 2;
107 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700108
109 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800110 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700111
Doug Zongker211aebc2011-10-28 15:13:10 -0700112 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800113 if (stageHeight > 0) {
114 int sw = gr_get_width(stageMarkerEmpty);
115 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
116 int y = iconY + iconHeight + 20;
117 for (int i = 0; i < max_stage; ++i) {
118 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
119 0, 0, sw, stageHeight, x, y);
120 x += sw;
121 }
122 }
123
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700124 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700125 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700126 }
127}
128
129// Draw the progress bar (if any) on the screen. Does not flip pages.
130// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700131void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700132 if (currentIcon == ERROR) return;
133
Doug Zongker02ec6b82012-08-22 17:26:40 -0700134 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700135 GRSurface* icon = installation[installingFrame];
Doug Zongkereac881c2014-03-07 09:21:25 -0800136 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700137 }
138
139 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700140 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700141 int width = gr_get_width(progressBarEmpty);
142 int height = gr_get_height(progressBarEmpty);
143
144 int dx = (gr_fb_width() - width)/2;
145 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
146
147 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700148 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700149 gr_fill(dx, dy, width, height);
150
151 if (progressBarType == DETERMINATE) {
152 float p = progressScopeStart + progress * progressScopeSize;
153 int pos = (int) (p * width);
154
Doug Zongker5fa8c232012-09-18 12:37:02 -0700155 if (rtl_locale) {
156 // Fill the progress bar from right to left.
157 if (pos > 0) {
158 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
159 }
160 if (pos < width-1) {
161 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
162 }
163 } else {
164 // Fill the progress bar from left to right.
165 if (pos > 0) {
166 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
167 }
168 if (pos < width-1) {
169 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
170 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700171 }
172 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 }
174}
175
Doug Zongkerc0441d12013-07-31 11:28:24 -0700176void ScreenRecoveryUI::SetColor(UIElement e) {
177 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700178 case INFO:
179 gr_color(249, 194, 0, 255);
180 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700181 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700182 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700183 break;
184 case MENU:
185 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700186 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700187 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700188 case MENU_SEL_BG_ACTIVE:
189 gr_color(0, 156, 100, 255);
190 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700191 case MENU_SEL_FG:
192 gr_color(255, 255, 255, 255);
193 break;
194 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700195 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700196 break;
197 case TEXT_FILL:
198 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700199 break;
200 default:
201 gr_color(255, 255, 255, 255);
202 break;
203 }
204}
Doug Zongker211aebc2011-10-28 15:13:10 -0700205
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700206void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
207 SetColor(MENU);
208 *y += 4;
209 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700210 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700211}
212
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700213void ScreenRecoveryUI::DrawTextLine(int* y, const char* line, bool bold) {
214 gr_text(4, *y, line, bold);
215 *y += char_height + 4;
216}
217
218void ScreenRecoveryUI::DrawTextLines(int* y, const char* const* lines) {
219 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
220 DrawTextLine(y, lines[i], false);
221 }
222}
223
224static const char* REGULAR_HELP[] = {
225 "Use volume up/down and power.",
226 NULL
227};
228
229static const char* LONG_PRESS_HELP[] = {
230 "Any button cycles highlight.",
231 "Long-press activates.",
232 NULL
233};
234
Doug Zongker211aebc2011-10-28 15:13:10 -0700235// Redraw everything on the screen. Does not flip pages.
236// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700237void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800238 if (!show_text) {
239 draw_background_locked(currentIcon);
240 draw_progress_locked();
241 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700242 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800243 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700244
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800245 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700246 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700247 char recovery_fingerprint[PROPERTY_VALUE_MAX];
248 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
249
250 SetColor(INFO);
251 DrawTextLine(&y, "Android Recovery", true);
252 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
253 DrawTextLine(&y, chunk.c_str(), false);
254 }
255 DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
256
Doug Zongkerc0441d12013-07-31 11:28:24 -0700257 SetColor(HEADER);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700258 DrawTextLines(&y, menu_headers);
Doug Zongker211aebc2011-10-28 15:13:10 -0700259
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700260 SetColor(MENU);
261 DrawHorizontalRule(&y);
262 y += 4;
263 for (int i = 0; i < menu_items; ++i) {
264 if (i == menu_sel) {
265 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700266 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700267 gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2);
268 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700269 SetColor(MENU_SEL_FG);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700270 gr_text(4, y, menu[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700271 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700272 } else {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700273 gr_text(4, y, menu[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700274 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700275 y += char_height + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700276 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700277 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700278 }
279
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800280 // display from the bottom up, until we hit the top of the
281 // screen, the bottom of the menu, or we've displayed the
282 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700283 SetColor(LOG);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800284 int row = (text_top+text_rows-1) % text_rows;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700285 size_t count = 0;
286 for (int ty = gr_fb_height() - char_height;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700287 ty >= y && count < text_rows;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800288 ty -= char_height, ++count) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700289 gr_text(0, ty, text[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800290 --row;
291 if (row < 0) row = text_rows-1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700292 }
293 }
294}
295
296// Redraw everything on the screen and flip the screen (make it visible).
297// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700298void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700299 draw_screen_locked();
300 gr_flip();
301}
302
303// Updates only the progress bar, if possible, otherwise redraws the screen.
304// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700305void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700306 if (show_text || !pagesIdentical) {
307 draw_screen_locked(); // Must redraw the whole screen
308 pagesIdentical = true;
309 } else {
310 draw_progress_locked(); // Draw only the progress bar and overlays
311 }
312 gr_flip();
313}
314
315// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700316void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
317 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700318 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700319}
320
Elliott Hughes985022a2015-04-13 13:04:32 -0700321void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700322 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700323 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700324 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700325 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700326
327 int redraw = 0;
328
329 // update the installation animation, if active
330 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700331 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
332 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700333 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700334 redraw = 1;
335 }
336
Doug Zongker211aebc2011-10-28 15:13:10 -0700337 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700338 int duration = progressScopeDuration;
339 if (progressBarType == DETERMINATE && duration > 0) {
340 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700341 float p = 1.0 * elapsed / duration;
342 if (p > 1.0) p = 1.0;
343 if (p > progress) {
344 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700345 redraw = 1;
346 }
347 }
348
Doug Zongker32a0a472011-11-01 11:00:20 -0700349 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700350
Doug Zongker32a0a472011-11-01 11:00:20 -0700351 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700352 double end = now();
353 // minimum of 20ms delay between frames
354 double delay = interval - (end-start);
355 if (delay < 0.02) delay = 0.02;
356 usleep((long)(delay * 1000000));
357 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700358}
359
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700360void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700361 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 if (result < 0) {
363 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
364 }
365}
366
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700367void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, GRSurface*** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700368 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800369 if (result < 0) {
370 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
371 }
372}
373
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700374void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700375 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700376 if (result < 0) {
377 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
378 }
379}
380
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700381static char** Alloc2d(size_t rows, size_t cols) {
382 char** result = new char*[rows];
383 for (size_t i = 0; i < rows; ++i) {
384 result[i] = new char[cols];
385 memset(result[i], 0, cols);
386 }
387 return result;
388}
389
Elliott Hughes8de52072015-04-08 20:06:50 -0700390void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700391 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700392
Doug Zongker55a36ac2013-03-04 15:49:02 -0800393 gr_font_size(&char_width, &char_height);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700394 text_rows = gr_fb_height() / char_height;
395 text_cols = gr_fb_width() / char_width;
396
397 text = Alloc2d(text_rows, text_cols + 1);
398 menu = Alloc2d(text_rows, text_cols + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800399
Doug Zongker211aebc2011-10-28 15:13:10 -0700400 text_col = text_row = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700401 text_top = 1;
402
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700403 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800404 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700405 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700406 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700407 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700408 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
409
Doug Zongker211aebc2011-10-28 15:13:10 -0700410 LoadBitmap("progress_empty", &progressBarEmpty);
411 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800412 LoadBitmap("stage_empty", &stageMarkerEmpty);
413 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700414
Doug Zongker02ec6b82012-08-22 17:26:40 -0700415 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
416 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
417 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
418 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
419
Elliott Hughes985022a2015-04-13 13:04:32 -0700420 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700421
422 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700423}
424
Doug Zongkera418aa72014-03-17 12:10:02 -0700425void ScreenRecoveryUI::SetLocale(const char* new_locale) {
426 if (new_locale) {
427 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700428 char* lang = strdup(locale);
429 for (char* p = lang; *p; ++p) {
430 if (*p == '_') {
431 *p = '\0';
432 break;
433 }
434 }
435
436 // A bit cheesy: keep an explicit list of supported languages
437 // that are RTL.
438 if (strcmp(lang, "ar") == 0 || // Arabic
439 strcmp(lang, "fa") == 0 || // Persian (Farsi)
440 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700441 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
442 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700443 rtl_locale = true;
444 }
445 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700446 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700447 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700448 }
449}
450
Elliott Hughes8de52072015-04-08 20:06:50 -0700451void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700452 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700453
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700454 currentIcon = icon;
455 update_screen_locked();
456
Doug Zongker211aebc2011-10-28 15:13:10 -0700457 pthread_mutex_unlock(&updateMutex);
458}
459
Elliott Hughes8de52072015-04-08 20:06:50 -0700460void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700461 pthread_mutex_lock(&updateMutex);
462 if (progressBarType != type) {
463 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700464 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700465 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700466 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700467 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700468 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700469 pthread_mutex_unlock(&updateMutex);
470}
471
Elliott Hughes8de52072015-04-08 20:06:50 -0700472void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700473 pthread_mutex_lock(&updateMutex);
474 progressBarType = DETERMINATE;
475 progressScopeStart += progressScopeSize;
476 progressScopeSize = portion;
477 progressScopeTime = now();
478 progressScopeDuration = seconds;
479 progress = 0;
480 update_progress_locked();
481 pthread_mutex_unlock(&updateMutex);
482}
483
Elliott Hughes8de52072015-04-08 20:06:50 -0700484void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700485 pthread_mutex_lock(&updateMutex);
486 if (fraction < 0.0) fraction = 0.0;
487 if (fraction > 1.0) fraction = 1.0;
488 if (progressBarType == DETERMINATE && fraction > progress) {
489 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800490 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700491 float scale = width * progressScopeSize;
492 if ((int) (progress * scale) != (int) (fraction * scale)) {
493 progress = fraction;
494 update_progress_locked();
495 }
496 }
497 pthread_mutex_unlock(&updateMutex);
498}
499
Doug Zongkerc87bab12013-11-25 13:53:25 -0800500void ScreenRecoveryUI::SetStage(int current, int max) {
501 pthread_mutex_lock(&updateMutex);
502 stage = current;
503 max_stage = max;
504 pthread_mutex_unlock(&updateMutex);
505}
506
Elliott Hughes8de52072015-04-08 20:06:50 -0700507void ScreenRecoveryUI::Print(const char *fmt, ...) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700508 char buf[256];
509 va_list ap;
510 va_start(ap, fmt);
511 vsnprintf(buf, 256, fmt, ap);
512 va_end(ap);
513
514 fputs(buf, stdout);
515
Doug Zongker211aebc2011-10-28 15:13:10 -0700516 pthread_mutex_lock(&updateMutex);
517 if (text_rows > 0 && text_cols > 0) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700518 for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700519 if (*ptr == '\n' || text_col >= text_cols) {
520 text[text_row][text_col] = '\0';
521 text_col = 0;
522 text_row = (text_row + 1) % text_rows;
523 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
524 }
525 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
526 }
527 text[text_row][text_col] = '\0';
528 update_screen_locked();
529 }
530 pthread_mutex_unlock(&updateMutex);
531}
532
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700533void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700534 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700535 if (ch != '\n') text[text_row][text_col++] = ch;
536 if (ch == '\n' || text_col >= text_cols) {
537 text_col = 0;
538 ++text_row;
Elliott Hughes8de52072015-04-08 20:06:50 -0700539 }
540 pthread_mutex_unlock(&updateMutex);
541}
542
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700543void ScreenRecoveryUI::ClearText() {
544 pthread_mutex_lock(&updateMutex);
545 text_col = 0;
546 text_row = 0;
547 text_top = 1;
548 for (size_t i = 0; i < text_rows; ++i) {
549 memset(text[i], 0, text_cols + 1);
550 }
551 pthread_mutex_unlock(&updateMutex);
552}
553
554void ScreenRecoveryUI::ShowFile(FILE* fp) {
555 std::vector<long> offsets;
556 offsets.push_back(ftell(fp));
557 ClearText();
558
559 struct stat sb;
560 fstat(fileno(fp), &sb);
561
562 bool show_prompt = false;
563 while (true) {
564 if (show_prompt) {
565 Print("--(%d%% of %d bytes)--",
566 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
567 static_cast<int>(sb.st_size));
568 Redraw();
569 while (show_prompt) {
570 show_prompt = false;
571 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700572 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700573 return;
574 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
575 if (offsets.size() <= 1) {
576 show_prompt = true;
577 } else {
578 offsets.pop_back();
579 fseek(fp, offsets.back(), SEEK_SET);
580 }
581 } else {
582 if (feof(fp)) {
583 return;
584 }
585 offsets.push_back(ftell(fp));
586 }
587 }
588 ClearText();
589 }
590
591 int ch = getc(fp);
592 if (ch == EOF) {
593 text_row = text_top = text_rows - 2;
594 show_prompt = true;
595 } else {
596 PutChar(ch);
597 if (text_col == 0 && text_row >= text_rows - 2) {
598 text_top = text_row;
599 show_prompt = true;
600 }
601 }
602 }
603}
604
Elliott Hughes8de52072015-04-08 20:06:50 -0700605void ScreenRecoveryUI::ShowFile(const char* filename) {
606 FILE* fp = fopen_path(filename, "re");
607 if (fp == nullptr) {
608 Print(" Unable to open %s: %s\n", filename, strerror(errno));
609 return;
610 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700611 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700612 fclose(fp);
613}
614
Doug Zongker211aebc2011-10-28 15:13:10 -0700615void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
616 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700617 pthread_mutex_lock(&updateMutex);
618 if (text_rows > 0 && text_cols > 0) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700619 menu_headers = headers;
620 size_t i = 0;
621 for (; i < text_rows && items[i] != nullptr; ++i) {
622 strncpy(menu[i], items[i], text_cols-1);
Doug Zongker211aebc2011-10-28 15:13:10 -0700623 menu[i][text_cols-1] = '\0';
624 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700625 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700626 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700627 menu_sel = initial_selection;
628 update_screen_locked();
629 }
630 pthread_mutex_unlock(&updateMutex);
631}
632
633int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700634 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700635 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700636 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700637 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700638
639 // Wrap at top and bottom.
640 if (menu_sel < 0) menu_sel = menu_items - 1;
641 if (menu_sel >= menu_items) menu_sel = 0;
642
Doug Zongker211aebc2011-10-28 15:13:10 -0700643 sel = menu_sel;
644 if (menu_sel != old_sel) update_screen_locked();
645 }
646 pthread_mutex_unlock(&updateMutex);
647 return sel;
648}
649
650void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700651 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700652 if (show_menu && text_rows > 0 && text_cols > 0) {
653 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700654 update_screen_locked();
655 }
656 pthread_mutex_unlock(&updateMutex);
657}
658
Elliott Hughes8de52072015-04-08 20:06:50 -0700659bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700660 pthread_mutex_lock(&updateMutex);
661 int visible = show_text;
662 pthread_mutex_unlock(&updateMutex);
663 return visible;
664}
665
Elliott Hughes8de52072015-04-08 20:06:50 -0700666bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700667 pthread_mutex_lock(&updateMutex);
668 int ever_visible = show_text_ever;
669 pthread_mutex_unlock(&updateMutex);
670 return ever_visible;
671}
672
Elliott Hughes8de52072015-04-08 20:06:50 -0700673void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700674 pthread_mutex_lock(&updateMutex);
675 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700676 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700677 update_screen_locked();
678 pthread_mutex_unlock(&updateMutex);
679}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700680
Elliott Hughes8de52072015-04-08 20:06:50 -0700681void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700682 pthread_mutex_lock(&updateMutex);
683 update_screen_locked();
684 pthread_mutex_unlock(&updateMutex);
685}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700686
687void ScreenRecoveryUI::KeyLongPress(int) {
688 // Redraw so that if we're in the menu, the highlight
689 // will change color to indicate a successful long press.
690 Redraw();
691}