blob: ff95915144bd367b93d3376db0816c0053fe225e [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 Hughesc0491632015-05-06 12:40:05 -070061 text_cols_(0),
62 text_rows_(0),
63 text_(nullptr),
64 text_col_(0),
65 text_row_(0),
66 text_top_(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070067 show_text(false),
68 show_text_ever(false),
Elliott Hughesc0491632015-05-06 12:40:05 -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),
Elliott Hughesc0491632015-05-06 12:40:05 -070073 file_viewer_text_(nullptr),
Doug Zongker32a0a472011-11-01 11:00:20 -070074 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080075 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080076 stage(-1),
77 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080078
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070079 for (int i = 0; i < 5; i++) {
80 backgroundIcon[i] = nullptr;
81 }
82 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070083}
84
Doug Zongker211aebc2011-10-28 15:13:10 -070085// Clear the screen and draw the currently selected background icon (if any).
86// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070087void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070088 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070089 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080090 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070091
92 if (icon) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070093 GRSurface* surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080094 if (icon == INSTALLING_UPDATE || icon == ERASING) {
95 surface = installation[installingFrame];
96 }
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070097 GRSurface* text_surface = backgroundText[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -070098
Doug Zongker211aebc2011-10-28 15:13:10 -070099 int iconWidth = gr_get_width(surface);
100 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700101 int textWidth = gr_get_width(text_surface);
102 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800103 int stageHeight = gr_get_height(stageMarkerEmpty);
104
105 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700106
Doug Zongkereac881c2014-03-07 09:21:25 -0800107 iconX = (gr_fb_width() - iconWidth) / 2;
108 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700109
110 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800111 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700112
Doug Zongker211aebc2011-10-28 15:13:10 -0700113 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800114 if (stageHeight > 0) {
115 int sw = gr_get_width(stageMarkerEmpty);
116 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
117 int y = iconY + iconHeight + 20;
118 for (int i = 0; i < max_stage; ++i) {
119 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
120 0, 0, sw, stageHeight, x, y);
121 x += sw;
122 }
123 }
124
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700125 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700126 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700127 }
128}
129
130// Draw the progress bar (if any) on the screen. Does not flip pages.
131// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700132void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700133 if (currentIcon == ERROR) return;
134
Doug Zongker02ec6b82012-08-22 17:26:40 -0700135 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700136 GRSurface* icon = installation[installingFrame];
Doug Zongkereac881c2014-03-07 09:21:25 -0800137 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700138 }
139
140 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700141 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700142 int width = gr_get_width(progressBarEmpty);
143 int height = gr_get_height(progressBarEmpty);
144
145 int dx = (gr_fb_width() - width)/2;
146 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
147
148 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700149 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700150 gr_fill(dx, dy, width, height);
151
152 if (progressBarType == DETERMINATE) {
153 float p = progressScopeStart + progress * progressScopeSize;
154 int pos = (int) (p * width);
155
Doug Zongker5fa8c232012-09-18 12:37:02 -0700156 if (rtl_locale) {
157 // Fill the progress bar from right to left.
158 if (pos > 0) {
159 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
160 }
161 if (pos < width-1) {
162 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
163 }
164 } else {
165 // Fill the progress bar from left to right.
166 if (pos > 0) {
167 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
168 }
169 if (pos < width-1) {
170 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
171 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700172 }
173 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700174 }
175}
176
Doug Zongkerc0441d12013-07-31 11:28:24 -0700177void ScreenRecoveryUI::SetColor(UIElement e) {
178 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700179 case INFO:
180 gr_color(249, 194, 0, 255);
181 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700182 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700183 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700184 break;
185 case MENU:
186 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700187 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700188 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700189 case MENU_SEL_BG_ACTIVE:
190 gr_color(0, 156, 100, 255);
191 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700192 case MENU_SEL_FG:
193 gr_color(255, 255, 255, 255);
194 break;
195 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700196 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700197 break;
198 case TEXT_FILL:
199 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700200 break;
201 default:
202 gr_color(255, 255, 255, 255);
203 break;
204 }
205}
Doug Zongker211aebc2011-10-28 15:13:10 -0700206
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700207void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
208 SetColor(MENU);
209 *y += 4;
210 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700211 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700212}
213
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700214void ScreenRecoveryUI::DrawTextLine(int* y, const char* line, bool bold) {
215 gr_text(4, *y, line, bold);
216 *y += char_height + 4;
217}
218
219void ScreenRecoveryUI::DrawTextLines(int* y, const char* const* lines) {
220 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
221 DrawTextLine(y, lines[i], false);
222 }
223}
224
225static const char* REGULAR_HELP[] = {
226 "Use volume up/down and power.",
227 NULL
228};
229
230static const char* LONG_PRESS_HELP[] = {
231 "Any button cycles highlight.",
232 "Long-press activates.",
233 NULL
234};
235
Doug Zongker211aebc2011-10-28 15:13:10 -0700236// Redraw everything on the screen. Does not flip pages.
237// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700238void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800239 if (!show_text) {
240 draw_background_locked(currentIcon);
241 draw_progress_locked();
242 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700243 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800244 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700245
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800246 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700247 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700248 char recovery_fingerprint[PROPERTY_VALUE_MAX];
249 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
250
251 SetColor(INFO);
252 DrawTextLine(&y, "Android Recovery", true);
253 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
254 DrawTextLine(&y, chunk.c_str(), false);
255 }
256 DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
257
Doug Zongkerc0441d12013-07-31 11:28:24 -0700258 SetColor(HEADER);
Elliott Hughesc0491632015-05-06 12:40:05 -0700259 DrawTextLines(&y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700260
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700261 SetColor(MENU);
262 DrawHorizontalRule(&y);
263 y += 4;
264 for (int i = 0; i < menu_items; ++i) {
265 if (i == menu_sel) {
266 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700267 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700268 gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2);
269 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700270 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700271 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700272 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700273 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700274 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700275 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700276 y += char_height + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700277 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700278 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700279 }
280
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800281 // display from the bottom up, until we hit the top of the
282 // screen, the bottom of the menu, or we've displayed the
283 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700284 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700285 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700286 size_t count = 0;
287 for (int ty = gr_fb_height() - char_height;
Elliott Hughesc0491632015-05-06 12:40:05 -0700288 ty >= y && count < text_rows_;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800289 ty -= char_height, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700290 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800291 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700292 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700293 }
294 }
295}
296
297// Redraw everything on the screen and flip the screen (make it visible).
298// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700299void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700300 draw_screen_locked();
301 gr_flip();
302}
303
304// Updates only the progress bar, if possible, otherwise redraws the screen.
305// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700306void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700307 if (show_text || !pagesIdentical) {
308 draw_screen_locked(); // Must redraw the whole screen
309 pagesIdentical = true;
310 } else {
311 draw_progress_locked(); // Draw only the progress bar and overlays
312 }
313 gr_flip();
314}
315
316// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700317void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
318 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700319 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700320}
321
Elliott Hughes985022a2015-04-13 13:04:32 -0700322void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700323 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700324 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700325 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700326 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700327
328 int redraw = 0;
329
330 // update the installation animation, if active
331 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700332 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
333 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700334 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700335 redraw = 1;
336 }
337
Doug Zongker211aebc2011-10-28 15:13:10 -0700338 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700339 int duration = progressScopeDuration;
340 if (progressBarType == DETERMINATE && duration > 0) {
341 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700342 float p = 1.0 * elapsed / duration;
343 if (p > 1.0) p = 1.0;
344 if (p > progress) {
345 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700346 redraw = 1;
347 }
348 }
349
Doug Zongker32a0a472011-11-01 11:00:20 -0700350 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700351
Doug Zongker32a0a472011-11-01 11:00:20 -0700352 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 double end = now();
354 // minimum of 20ms delay between frames
355 double delay = interval - (end-start);
356 if (delay < 0.02) delay = 0.02;
357 usleep((long)(delay * 1000000));
358 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700359}
360
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700361void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700362 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700363 if (result < 0) {
364 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
365 }
366}
367
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700368void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, GRSurface*** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700369 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800370 if (result < 0) {
371 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
372 }
373}
374
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700375void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700376 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700377 if (result < 0) {
378 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
379 }
380}
381
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700382static char** Alloc2d(size_t rows, size_t cols) {
383 char** result = new char*[rows];
384 for (size_t i = 0; i < rows; ++i) {
385 result[i] = new char[cols];
386 memset(result[i], 0, cols);
387 }
388 return result;
389}
390
Elliott Hughes8de52072015-04-08 20:06:50 -0700391void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700392 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700393
Doug Zongker55a36ac2013-03-04 15:49:02 -0800394 gr_font_size(&char_width, &char_height);
Elliott Hughesc0491632015-05-06 12:40:05 -0700395 text_rows_ = gr_fb_height() / char_height;
396 text_cols_ = gr_fb_width() / char_width;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700397
Elliott Hughesc0491632015-05-06 12:40:05 -0700398 text_ = Alloc2d(text_rows_, text_cols_ + 1);
399 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
400 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800401
Elliott Hughesc0491632015-05-06 12:40:05 -0700402 text_col_ = text_row_ = 0;
403 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700404
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700405 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800406 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700407 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700408 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700409 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700410 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
411
Doug Zongker211aebc2011-10-28 15:13:10 -0700412 LoadBitmap("progress_empty", &progressBarEmpty);
413 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800414 LoadBitmap("stage_empty", &stageMarkerEmpty);
415 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700416
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
418 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
419 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
420 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
421
Elliott Hughes985022a2015-04-13 13:04:32 -0700422 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700423
424 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700425}
426
Doug Zongkera418aa72014-03-17 12:10:02 -0700427void ScreenRecoveryUI::SetLocale(const char* new_locale) {
428 if (new_locale) {
429 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700430 char* lang = strdup(locale);
431 for (char* p = lang; *p; ++p) {
432 if (*p == '_') {
433 *p = '\0';
434 break;
435 }
436 }
437
438 // A bit cheesy: keep an explicit list of supported languages
439 // that are RTL.
440 if (strcmp(lang, "ar") == 0 || // Arabic
441 strcmp(lang, "fa") == 0 || // Persian (Farsi)
442 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700443 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
444 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700445 rtl_locale = true;
446 }
447 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700448 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700449 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700450 }
451}
452
Elliott Hughes8de52072015-04-08 20:06:50 -0700453void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700454 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700455
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700456 currentIcon = icon;
457 update_screen_locked();
458
Doug Zongker211aebc2011-10-28 15:13:10 -0700459 pthread_mutex_unlock(&updateMutex);
460}
461
Elliott Hughes8de52072015-04-08 20:06:50 -0700462void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 pthread_mutex_lock(&updateMutex);
464 if (progressBarType != type) {
465 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700466 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700467 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700468 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700469 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700470 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700471 pthread_mutex_unlock(&updateMutex);
472}
473
Elliott Hughes8de52072015-04-08 20:06:50 -0700474void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700475 pthread_mutex_lock(&updateMutex);
476 progressBarType = DETERMINATE;
477 progressScopeStart += progressScopeSize;
478 progressScopeSize = portion;
479 progressScopeTime = now();
480 progressScopeDuration = seconds;
481 progress = 0;
482 update_progress_locked();
483 pthread_mutex_unlock(&updateMutex);
484}
485
Elliott Hughes8de52072015-04-08 20:06:50 -0700486void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700487 pthread_mutex_lock(&updateMutex);
488 if (fraction < 0.0) fraction = 0.0;
489 if (fraction > 1.0) fraction = 1.0;
490 if (progressBarType == DETERMINATE && fraction > progress) {
491 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800492 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700493 float scale = width * progressScopeSize;
494 if ((int) (progress * scale) != (int) (fraction * scale)) {
495 progress = fraction;
496 update_progress_locked();
497 }
498 }
499 pthread_mutex_unlock(&updateMutex);
500}
501
Doug Zongkerc87bab12013-11-25 13:53:25 -0800502void ScreenRecoveryUI::SetStage(int current, int max) {
503 pthread_mutex_lock(&updateMutex);
504 stage = current;
505 max_stage = max;
506 pthread_mutex_unlock(&updateMutex);
507}
508
Elliott Hughes8de52072015-04-08 20:06:50 -0700509void ScreenRecoveryUI::Print(const char *fmt, ...) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700510 char buf[256];
511 va_list ap;
512 va_start(ap, fmt);
513 vsnprintf(buf, 256, fmt, ap);
514 va_end(ap);
515
516 fputs(buf, stdout);
517
Doug Zongker211aebc2011-10-28 15:13:10 -0700518 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700519 if (text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700520 for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700521 if (*ptr == '\n' || text_col_ >= text_cols_) {
522 text_[text_row_][text_col_] = '\0';
523 text_col_ = 0;
524 text_row_ = (text_row_ + 1) % text_rows_;
525 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700526 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700527 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700528 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700529 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700530 update_screen_locked();
531 }
532 pthread_mutex_unlock(&updateMutex);
533}
534
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700535void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700536 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700537 if (ch != '\n') text_[text_row_][text_col_++] = ch;
538 if (ch == '\n' || text_col_ >= text_cols_) {
539 text_col_ = 0;
540 ++text_row_;
541
542 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700543 }
544 pthread_mutex_unlock(&updateMutex);
545}
546
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700547void ScreenRecoveryUI::ClearText() {
548 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700549 text_col_ = 0;
550 text_row_ = 0;
551 text_top_ = 1;
552 for (size_t i = 0; i < text_rows_; ++i) {
553 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700554 }
555 pthread_mutex_unlock(&updateMutex);
556}
557
558void ScreenRecoveryUI::ShowFile(FILE* fp) {
559 std::vector<long> offsets;
560 offsets.push_back(ftell(fp));
561 ClearText();
562
563 struct stat sb;
564 fstat(fileno(fp), &sb);
565
566 bool show_prompt = false;
567 while (true) {
568 if (show_prompt) {
569 Print("--(%d%% of %d bytes)--",
570 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
571 static_cast<int>(sb.st_size));
572 Redraw();
573 while (show_prompt) {
574 show_prompt = false;
575 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700576 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700577 return;
578 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
579 if (offsets.size() <= 1) {
580 show_prompt = true;
581 } else {
582 offsets.pop_back();
583 fseek(fp, offsets.back(), SEEK_SET);
584 }
585 } else {
586 if (feof(fp)) {
587 return;
588 }
589 offsets.push_back(ftell(fp));
590 }
591 }
592 ClearText();
593 }
594
595 int ch = getc(fp);
596 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700597 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700598 show_prompt = true;
599 } else {
600 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700601 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700602 show_prompt = true;
603 }
604 }
605 }
606}
607
Elliott Hughes8de52072015-04-08 20:06:50 -0700608void ScreenRecoveryUI::ShowFile(const char* filename) {
609 FILE* fp = fopen_path(filename, "re");
610 if (fp == nullptr) {
611 Print(" Unable to open %s: %s\n", filename, strerror(errno));
612 return;
613 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700614
615 char** old_text = text_;
616 size_t old_text_col = text_col_;
617 size_t old_text_row = text_row_;
618 size_t old_text_top = text_top_;
619
620 // Swap in the alternate screen and clear it.
621 text_ = file_viewer_text_;
622 ClearText();
623
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700624 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700625 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700626
627 text_ = old_text;
628 text_col_ = old_text_col;
629 text_row_ = old_text_row;
630 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700631}
632
Doug Zongker211aebc2011-10-28 15:13:10 -0700633void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
634 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700635 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700636 if (text_rows_ > 0 && text_cols_ > 0) {
637 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700638 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700639 for (; i < text_rows_ && items[i] != nullptr; ++i) {
640 strncpy(menu_[i], items[i], text_cols_ - 1);
641 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700642 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700643 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700644 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700645 menu_sel = initial_selection;
646 update_screen_locked();
647 }
648 pthread_mutex_unlock(&updateMutex);
649}
650
651int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700652 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700653 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700654 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700655 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700656
657 // Wrap at top and bottom.
658 if (menu_sel < 0) menu_sel = menu_items - 1;
659 if (menu_sel >= menu_items) menu_sel = 0;
660
Doug Zongker211aebc2011-10-28 15:13:10 -0700661 sel = menu_sel;
662 if (menu_sel != old_sel) update_screen_locked();
663 }
664 pthread_mutex_unlock(&updateMutex);
665 return sel;
666}
667
668void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700669 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700670 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700671 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700672 update_screen_locked();
673 }
674 pthread_mutex_unlock(&updateMutex);
675}
676
Elliott Hughes8de52072015-04-08 20:06:50 -0700677bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700678 pthread_mutex_lock(&updateMutex);
679 int visible = show_text;
680 pthread_mutex_unlock(&updateMutex);
681 return visible;
682}
683
Elliott Hughes8de52072015-04-08 20:06:50 -0700684bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700685 pthread_mutex_lock(&updateMutex);
686 int ever_visible = show_text_ever;
687 pthread_mutex_unlock(&updateMutex);
688 return ever_visible;
689}
690
Elliott Hughes8de52072015-04-08 20:06:50 -0700691void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700692 pthread_mutex_lock(&updateMutex);
693 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700694 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700695 update_screen_locked();
696 pthread_mutex_unlock(&updateMutex);
697}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700698
Elliott Hughes8de52072015-04-08 20:06:50 -0700699void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700700 pthread_mutex_lock(&updateMutex);
701 update_screen_locked();
702 pthread_mutex_unlock(&updateMutex);
703}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700704
705void ScreenRecoveryUI::KeyLongPress(int) {
706 // Redraw so that if we're in the menu, the highlight
707 // will change color to indicate a successful long press.
708 Redraw();
709}