blob: e699538c710dd8bb022ba65f9841209bd35d912b [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040040extern "C" {
41#include "minuitwrp/minui.h"
42int twgr_text(int x, int y, const char *s);
Dees_Troy32c8eb82012-09-11 15:28:06 -040043#include "gui/gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040044}
Dees_Troy32c8eb82012-09-11 15:28:06 -040045#include "data.hpp"
Doug Zongker211aebc2011-10-28 15:13:10 -070046
Doug Zongker55a36ac2013-03-04 15:49:02 -080047static int char_width;
48static int char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -070049
Doug Zongker211aebc2011-10-28 15:13:10 -070050// Return the current time as a double (including fractions of a second).
51static double now() {
52 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070053 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070054 return tv.tv_sec + tv.tv_usec / 1000000.0;
55}
56
57ScreenRecoveryUI::ScreenRecoveryUI() :
58 currentIcon(NONE),
59 installingFrame(0),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070060 locale(nullptr),
Doug Zongker5fa8c232012-09-18 12:37:02 -070061 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070062 progressBarType(EMPTY),
63 progressScopeStart(0),
64 progressScopeSize(0),
65 progress(0),
66 pagesIdentical(false),
Elliott Hughesdf52e1e2015-05-06 12:40:05 -070067 text_cols_(0),
68 text_rows_(0),
69 text_(nullptr),
70 text_col_(0),
71 text_row_(0),
72 text_top_(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070073 show_text(false),
74 show_text_ever(false),
Elliott Hughesdf52e1e2015-05-06 12:40:05 -070075 menu_(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070076 show_menu(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070077 menu_items(0),
78 menu_sel(0),
Elliott Hughesdf52e1e2015-05-06 12:40:05 -070079 file_viewer_text_(nullptr),
Doug Zongker32a0a472011-11-01 11:00:20 -070080 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080081 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080082 stage(-1),
83 max_stage(-1) {
yetta_wu2f6877a2013-06-25 15:03:11 +080084
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070085 for (int i = 0; i < 5; i++) {
86 backgroundIcon[i] = nullptr;
87 }
88 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070089}
90
Doug Zongker211aebc2011-10-28 15:13:10 -070091// Clear the screen and draw the currently selected background icon (if any).
92// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070093void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070094 pagesIdentical = false;
95 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080096 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070097
98 if (icon) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070099 GRSurface* surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -0800100 if (icon == INSTALLING_UPDATE || icon == ERASING) {
101 surface = installation[installingFrame];
102 }
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700103 GRSurface* text_surface = backgroundText[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700104
Doug Zongker211aebc2011-10-28 15:13:10 -0700105 int iconWidth = gr_get_width(surface);
106 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700107 int textWidth = gr_get_width(text_surface);
108 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800109 int stageHeight = gr_get_height(stageMarkerEmpty);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110
Doug Zongkerc87bab12013-11-25 13:53:25 -0800111 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700112
Doug Zongkereac881c2014-03-07 09:21:25 -0800113 iconX = (gr_fb_width() - iconWidth) / 2;
114 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700115
116 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800117 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700118
Doug Zongker211aebc2011-10-28 15:13:10 -0700119 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800120 if (stageHeight > 0) {
121 int sw = gr_get_width(stageMarkerEmpty);
122 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
123 int y = iconY + iconHeight + 20;
124 for (int i = 0; i < max_stage; ++i) {
125 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
126 0, 0, sw, stageHeight, x, y);
127 x += sw;
128 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700129 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700130
131 gr_color(255, 255, 255, 255);
132 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700133 }
134}
135
136// Draw the progress bar (if any) on the screen. Does not flip pages.
137// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700138void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700139 if (currentIcon == ERROR) return;
140
Doug Zongker02ec6b82012-08-22 17:26:40 -0700141 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700142 GRSurface* icon = installation[installingFrame];
Doug Zongkereac881c2014-03-07 09:21:25 -0800143 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700144 }
145
146 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700147 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700148 int width = gr_get_width(progressBarEmpty);
149 int height = gr_get_height(progressBarEmpty);
150
151 int dx = (gr_fb_width() - width)/2;
152 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
153
154 // Erase behind the progress bar (in case this was a progress-only update)
155 gr_color(0, 0, 0, 255);
156 gr_fill(dx, dy, width, height);
157
158 if (progressBarType == DETERMINATE) {
159 float p = progressScopeStart + progress * progressScopeSize;
160 int pos = (int) (p * width);
161
Doug Zongker5fa8c232012-09-18 12:37:02 -0700162 if (rtl_locale) {
163 // Fill the progress bar from right to left.
164 if (pos > 0) {
165 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
166 }
167 if (pos < width-1) {
168 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
169 }
170 } else {
171 // Fill the progress bar from left to right.
172 if (pos > 0) {
173 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
174 }
175 if (pos < width-1) {
176 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
177 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700178 }
179 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700180 }
181}
182
Doug Zongkerc0441d12013-07-31 11:28:24 -0700183void ScreenRecoveryUI::SetColor(UIElement e) {
184 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700185 case INFO:
186 gr_color(249, 194, 0, 255);
187 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700188 case HEADER:
189 gr_color(247, 0, 6, 255);
190 break;
191 case MENU:
192 case MENU_SEL_BG:
193 gr_color(0, 106, 157, 255);
194 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700195 case MENU_SEL_BG_ACTIVE:
196 gr_color(0, 156, 100, 255);
197 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700198 case MENU_SEL_FG:
199 gr_color(255, 255, 255, 255);
200 break;
201 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700202 gr_color(196, 196, 196, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700203 break;
204 case TEXT_FILL:
205 gr_color(0, 0, 0, 160);
206 break;
207 default:
208 gr_color(255, 255, 255, 255);
209 break;
210 }
211}
Doug Zongker211aebc2011-10-28 15:13:10 -0700212
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700213void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
214 SetColor(MENU);
215 *y += 4;
216 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700217 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700218}
219
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700220void ScreenRecoveryUI::DrawTextLine(int* y, const char* line, bool bold) {
221 gr_text(4, *y, line, bold);
222 *y += char_height + 4;
223}
224
225void ScreenRecoveryUI::DrawTextLines(int* y, const char* const* lines) {
226 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
227 DrawTextLine(y, lines[i], false);
228 }
229}
230
231static const char* REGULAR_HELP[] = {
232 "Use volume up/down and power.",
233 NULL
234};
235
236static const char* LONG_PRESS_HELP[] = {
237 "Any button cycles highlight.",
238 "Long-press activates.",
239 NULL
240};
241
Doug Zongker211aebc2011-10-28 15:13:10 -0700242// Redraw everything on the screen. Does not flip pages.
243// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700244void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800245 if (!show_text) {
246 draw_background_locked(currentIcon);
247 draw_progress_locked();
248 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700249 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800250 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700251
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800252 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700253 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700254 char recovery_fingerprint[PROPERTY_VALUE_MAX];
255 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
256
257 SetColor(INFO);
258 DrawTextLine(&y, "Android Recovery", true);
259 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
260 DrawTextLine(&y, chunk.c_str(), false);
261 }
262 DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
263
Doug Zongkerc0441d12013-07-31 11:28:24 -0700264 SetColor(HEADER);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700265 DrawTextLines(&y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700266
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700267 SetColor(MENU);
268 DrawHorizontalRule(&y);
269 y += 4;
270 for (int i = 0; i < menu_items; ++i) {
271 if (i == menu_sel) {
272 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700273 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700274 gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2);
275 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700276 SetColor(MENU_SEL_FG);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700277 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700278 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700279 } else {
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700280 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700281 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700282 y += char_height + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700283 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700284 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700285 }
286
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800287 // display from the bottom up, until we hit the top of the
288 // screen, the bottom of the menu, or we've displayed the
289 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700290 SetColor(LOG);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700291 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700292 size_t count = 0;
293 for (int ty = gr_fb_height() - char_height;
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700294 ty >= y && count < text_rows_;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800295 ty -= char_height, ++count) {
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700296 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800297 --row;
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700298 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700299 }
300 }
301}
302
303// Redraw everything on the screen and flip the screen (make it visible).
304// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700305void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700306 draw_screen_locked();
307 gr_flip();
308}
309
310// Updates only the progress bar, if possible, otherwise redraws the screen.
311// Should only be called with updateMutex locked.
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500312
Elliott Hughes8de52072015-04-08 20:06:50 -0700313void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 if (show_text || !pagesIdentical) {
315 draw_screen_locked(); // Must redraw the whole screen
316 pagesIdentical = true;
317 } else {
318 draw_progress_locked(); // Draw only the progress bar and overlays
319 }
320 gr_flip();
321}
322
323// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700324void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
325 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700326 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700327}
328
Elliott Hughes985022a2015-04-13 13:04:32 -0700329void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700330 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700331 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700332 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700333 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700334
335 int redraw = 0;
336
337 // update the installation animation, if active
338 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700339 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
340 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700341 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700342 redraw = 1;
343 }
344
Doug Zongker211aebc2011-10-28 15:13:10 -0700345 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700346 int duration = progressScopeDuration;
347 if (progressBarType == DETERMINATE && duration > 0) {
348 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700349 float p = 1.0 * elapsed / duration;
350 if (p > 1.0) p = 1.0;
351 if (p > progress) {
352 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700353 redraw = 1;
354 }
355 }
356
Doug Zongker32a0a472011-11-01 11:00:20 -0700357 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700358
Doug Zongker32a0a472011-11-01 11:00:20 -0700359 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700360 double end = now();
361 // minimum of 20ms delay between frames
362 double delay = interval - (end-start);
363 if (delay < 0.02) delay = 0.02;
364 usleep((long)(delay * 1000000));
365 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700366}
367
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700368void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700369 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700370 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::LoadBitmapArray(const char* filename, int* frames, GRSurface*** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700376 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 if (result < 0) {
378 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
379 }
380}
381
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700382void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700383 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700384 if (result < 0) {
385 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
386 }
387}
388
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700389static char** Alloc2d(size_t rows, size_t cols) {
390 char** result = new char*[rows];
391 for (size_t i = 0; i < rows; ++i) {
392 result[i] = new char[cols];
393 memset(result[i], 0, cols);
394 }
395 return result;
396}
397
Elliott Hughes8de52072015-04-08 20:06:50 -0700398void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700399 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700400
Doug Zongker55a36ac2013-03-04 15:49:02 -0800401 gr_font_size(&char_width, &char_height);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700402 text_rows_ = gr_fb_height() / char_height;
403 text_cols_ = gr_fb_width() / char_width;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800404
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700405 text_ = Alloc2d(text_rows_, text_cols_ + 1);
406 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
407 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker211aebc2011-10-28 15:13:10 -0700408
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700409 text_col_ = text_row_ = 0;
410 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700411
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700412 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800413 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700414 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700415 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700416 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
418
Doug Zongker211aebc2011-10-28 15:13:10 -0700419 LoadBitmap("progress_empty", &progressBarEmpty);
420 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800421 LoadBitmap("stage_empty", &stageMarkerEmpty);
422 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700423
Doug Zongker02ec6b82012-08-22 17:26:40 -0700424 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
425 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
426 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
427 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
428
Elliott Hughes985022a2015-04-13 13:04:32 -0700429 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700430
431 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700432}
433
Doug Zongkera418aa72014-03-17 12:10:02 -0700434void ScreenRecoveryUI::SetLocale(const char* new_locale) {
435 if (new_locale) {
436 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700437 char* lang = strdup(locale);
438 for (char* p = lang; *p; ++p) {
439 if (*p == '_') {
440 *p = '\0';
441 break;
442 }
443 }
444
445 // A bit cheesy: keep an explicit list of supported languages
446 // that are RTL.
447 if (strcmp(lang, "ar") == 0 || // Arabic
448 strcmp(lang, "fa") == 0 || // Persian (Farsi)
449 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700450 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
451 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700452 rtl_locale = true;
453 }
454 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700455 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700456 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700457 }
458}
459
Elliott Hughes8de52072015-04-08 20:06:50 -0700460void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700461 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700462
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 currentIcon = icon;
464 update_screen_locked();
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700465
Doug Zongker211aebc2011-10-28 15:13:10 -0700466 pthread_mutex_unlock(&updateMutex);
467}
468
Elliott Hughes8de52072015-04-08 20:06:50 -0700469void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700470 pthread_mutex_lock(&updateMutex);
471 if (progressBarType != type) {
472 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700473 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700474 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700475 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700476 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700477 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700478 pthread_mutex_unlock(&updateMutex);
479}
480
Elliott Hughes8de52072015-04-08 20:06:50 -0700481void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700482 pthread_mutex_lock(&updateMutex);
483 progressBarType = DETERMINATE;
484 progressScopeStart += progressScopeSize;
485 progressScopeSize = portion;
486 progressScopeTime = now();
487 progressScopeDuration = seconds;
488 progress = 0;
489 update_progress_locked();
490 pthread_mutex_unlock(&updateMutex);
491}
492
Elliott Hughes8de52072015-04-08 20:06:50 -0700493void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700494 pthread_mutex_lock(&updateMutex);
495 if (fraction < 0.0) fraction = 0.0;
496 if (fraction > 1.0) fraction = 1.0;
497 if (progressBarType == DETERMINATE && fraction > progress) {
498 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800499 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700500 float scale = width * progressScopeSize;
501 if ((int) (progress * scale) != (int) (fraction * scale)) {
502 progress = fraction;
503 update_progress_locked();
504 }
505 }
506 pthread_mutex_unlock(&updateMutex);
507}
508
Doug Zongkerc87bab12013-11-25 13:53:25 -0800509void ScreenRecoveryUI::SetStage(int current, int max) {
510 pthread_mutex_lock(&updateMutex);
511 stage = current;
512 max_stage = max;
513 pthread_mutex_unlock(&updateMutex);
514}
515
Elliott Hughes8de52072015-04-08 20:06:50 -0700516void ScreenRecoveryUI::Print(const char *fmt, ...) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700517 char buf[256];
518 va_list ap;
519 va_start(ap, fmt);
520 vsnprintf(buf, 256, fmt, ap);
521 va_end(ap);
522
Dees_Troy32c8eb82012-09-11 15:28:06 -0400523 gui_print("%s", buf);
524 return;
525
Doug Zongker211aebc2011-10-28 15:13:10 -0700526 fputs(buf, stdout);
527
Doug Zongker211aebc2011-10-28 15:13:10 -0700528 pthread_mutex_lock(&updateMutex);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700529 if (text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700530 for (const char* ptr = buf; *ptr != '\0'; ++ptr) {
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700531 if (*ptr == '\n' || text_col_ >= text_cols_) {
532 text_[text_row_][text_col_] = '\0';
533 text_col_ = 0;
534 text_row_ = (text_row_ + 1) % text_rows_;
535 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700536 }
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700537 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700538 }
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700539 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700540 update_screen_locked();
541 }
542 pthread_mutex_unlock(&updateMutex);
543}
544
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700545void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700546 pthread_mutex_lock(&updateMutex);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700547 if (ch != '\n') text_[text_row_][text_col_++] = ch;
548 if (ch == '\n' || text_col_ >= text_cols_) {
549 text_col_ = 0;
550 ++text_row_;
551
552 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700553 }
554 pthread_mutex_unlock(&updateMutex);
555}
556
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700557void ScreenRecoveryUI::ClearText() {
558 pthread_mutex_lock(&updateMutex);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700559 text_col_ = 0;
560 text_row_ = 0;
561 text_top_ = 1;
562 for (size_t i = 0; i < text_rows_; ++i) {
563 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700564 }
565 pthread_mutex_unlock(&updateMutex);
566}
567
568void ScreenRecoveryUI::ShowFile(FILE* fp) {
569 std::vector<long> offsets;
570 offsets.push_back(ftell(fp));
571 ClearText();
572
573 struct stat sb;
574 fstat(fileno(fp), &sb);
575
576 bool show_prompt = false;
577 while (true) {
578 if (show_prompt) {
579 Print("--(%d%% of %d bytes)--",
580 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
581 static_cast<int>(sb.st_size));
582 Redraw();
583 while (show_prompt) {
584 show_prompt = false;
585 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700586 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700587 return;
588 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
589 if (offsets.size() <= 1) {
590 show_prompt = true;
591 } else {
592 offsets.pop_back();
593 fseek(fp, offsets.back(), SEEK_SET);
594 }
595 } else {
596 if (feof(fp)) {
597 return;
598 }
599 offsets.push_back(ftell(fp));
600 }
601 }
602 ClearText();
603 }
604
605 int ch = getc(fp);
606 if (ch == EOF) {
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700607 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700608 show_prompt = true;
609 } else {
610 PutChar(ch);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700611 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700612 show_prompt = true;
613 }
614 }
615 }
616}
617
Elliott Hughes8de52072015-04-08 20:06:50 -0700618void ScreenRecoveryUI::ShowFile(const char* filename) {
619 FILE* fp = fopen_path(filename, "re");
620 if (fp == nullptr) {
621 Print(" Unable to open %s: %s\n", filename, strerror(errno));
622 return;
623 }
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700624
625 char** old_text = text_;
626 size_t old_text_col = text_col_;
627 size_t old_text_row = text_row_;
628 size_t old_text_top = text_top_;
629
630 // Swap in the alternate screen and clear it.
631 text_ = file_viewer_text_;
632 ClearText();
633
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700634 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700635 fclose(fp);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700636
637 text_ = old_text;
638 text_col_ = old_text_col;
639 text_row_ = old_text_row;
640 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700641}
642
Doug Zongker211aebc2011-10-28 15:13:10 -0700643void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
644 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700645 pthread_mutex_lock(&updateMutex);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700646 if (text_rows_ > 0 && text_cols_ > 0) {
647 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700648 size_t i = 0;
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700649 for (; i < text_rows_ && items[i] != nullptr; ++i) {
650 strncpy(menu_[i], items[i], text_cols_ - 1);
651 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700652 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700653 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700654 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700655 menu_sel = initial_selection;
656 update_screen_locked();
657 }
658 pthread_mutex_unlock(&updateMutex);
659}
660
661int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700662 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700663 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700664 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700665 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700666
667 // Wrap at top and bottom.
668 if (menu_sel < 0) menu_sel = menu_items - 1;
669 if (menu_sel >= menu_items) menu_sel = 0;
670
Doug Zongker211aebc2011-10-28 15:13:10 -0700671 sel = menu_sel;
672 if (menu_sel != old_sel) update_screen_locked();
673 }
674 pthread_mutex_unlock(&updateMutex);
675 return sel;
676}
677
678void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700679 pthread_mutex_lock(&updateMutex);
Elliott Hughesdf52e1e2015-05-06 12:40:05 -0700680 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700681 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700682 update_screen_locked();
683 }
684 pthread_mutex_unlock(&updateMutex);
685}
686
Elliott Hughes8de52072015-04-08 20:06:50 -0700687bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700688 pthread_mutex_lock(&updateMutex);
689 int visible = show_text;
690 pthread_mutex_unlock(&updateMutex);
691 return visible;
692}
693
Elliott Hughes8de52072015-04-08 20:06:50 -0700694bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700695 pthread_mutex_lock(&updateMutex);
696 int ever_visible = show_text_ever;
697 pthread_mutex_unlock(&updateMutex);
698 return ever_visible;
699}
700
Elliott Hughes8de52072015-04-08 20:06:50 -0700701void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700702 pthread_mutex_lock(&updateMutex);
703 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700704 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700705 update_screen_locked();
706 pthread_mutex_unlock(&updateMutex);
707}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700708
Elliott Hughes8de52072015-04-08 20:06:50 -0700709void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700710 pthread_mutex_lock(&updateMutex);
711 update_screen_locked();
712 pthread_mutex_unlock(&updateMutex);
713}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700714
715void ScreenRecoveryUI::KeyLongPress(int) {
716 // Redraw so that if we're in the menu, the highlight
717 // will change color to indicate a successful long press.
718 Redraw();
719}