blob: 23fc9015414fd2d1eeebf96400d2bef32b2c0f6d [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 Hughes4b166f02015-12-04 15:30:20 -080033#include <android-base/strings.h>
34#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070035#include <cutils/properties.h>
36
Doug Zongker211aebc2011-10-28 15:13:10 -070037#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070038#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070039#include "minui/minui.h"
40#include "screen_ui.h"
41#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070042
Doug Zongker55a36ac2013-03-04 15:49:02 -080043static int char_width;
44static int char_height;
Doug Zongker211aebc2011-10-28 15:13:10 -070045
Doug Zongker211aebc2011-10-28 15:13:10 -070046// Return the current time as a double (including fractions of a second).
47static double now() {
48 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070049 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070050 return tv.tv_sec + tv.tv_usec / 1000000.0;
51}
52
53ScreenRecoveryUI::ScreenRecoveryUI() :
54 currentIcon(NONE),
55 installingFrame(0),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070056 locale(nullptr),
Doug Zongker5fa8c232012-09-18 12:37:02 -070057 rtl_locale(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070058 progressBarType(EMPTY),
59 progressScopeStart(0),
60 progressScopeSize(0),
61 progress(0),
62 pagesIdentical(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070063 text_cols_(0),
64 text_rows_(0),
65 text_(nullptr),
66 text_col_(0),
67 text_row_(0),
68 text_top_(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070069 show_text(false),
70 show_text_ever(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070071 menu_(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070072 show_menu(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070073 menu_items(0),
74 menu_sel(0),
Elliott Hughesc0491632015-05-06 12:40:05 -070075 file_viewer_text_(nullptr),
Doug Zongker32a0a472011-11-01 11:00:20 -070076 animation_fps(20),
Doug Zongkereac881c2014-03-07 09:21:25 -080077 installing_frames(-1),
Doug Zongkerc87bab12013-11-25 13:53:25 -080078 stage(-1),
79 max_stage(-1) {
yetta_wu5b468fc2013-06-25 15:03:11 +080080
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070081 for (int i = 0; i < 5; i++) {
82 backgroundIcon[i] = nullptr;
83 }
84 pthread_mutex_init(&updateMutex, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070085}
86
Doug Zongker211aebc2011-10-28 15:13:10 -070087// Clear the screen and draw the currently selected background icon (if any).
88// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -070089void ScreenRecoveryUI::draw_background_locked(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -070090 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -070091 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -080092 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -070093
94 if (icon) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070095 GRSurface* surface = backgroundIcon[icon];
Doug Zongkereac881c2014-03-07 09:21:25 -080096 if (icon == INSTALLING_UPDATE || icon == ERASING) {
97 surface = installation[installingFrame];
98 }
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070099 GRSurface* text_surface = backgroundText[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700100
Doug Zongker211aebc2011-10-28 15:13:10 -0700101 int iconWidth = gr_get_width(surface);
102 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700103 int textWidth = gr_get_width(text_surface);
104 int textHeight = gr_get_height(text_surface);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800105 int stageHeight = gr_get_height(stageMarkerEmpty);
106
107 int sh = (max_stage >= 0) ? stageHeight : 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700108
Doug Zongkereac881c2014-03-07 09:21:25 -0800109 iconX = (gr_fb_width() - iconWidth) / 2;
110 iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700111
112 int textX = (gr_fb_width() - textWidth) / 2;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800113 int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700114
Doug Zongker211aebc2011-10-28 15:13:10 -0700115 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800116 if (stageHeight > 0) {
117 int sw = gr_get_width(stageMarkerEmpty);
118 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
119 int y = iconY + iconHeight + 20;
120 for (int i = 0; i < max_stage; ++i) {
121 gr_blit((i < stage) ? stageMarkerFill : stageMarkerEmpty,
122 0, 0, sw, stageHeight, x, y);
123 x += sw;
124 }
125 }
126
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700127 gr_color(255, 255, 255, 255);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700128 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700129 }
130}
131
132// Draw the progress bar (if any) on the screen. Does not flip pages.
133// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700134void ScreenRecoveryUI::draw_progress_locked() {
Doug Zongker69f4b672012-04-26 14:37:53 -0700135 if (currentIcon == ERROR) return;
136
Doug Zongker02ec6b82012-08-22 17:26:40 -0700137 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700138 GRSurface* icon = installation[installingFrame];
Doug Zongkereac881c2014-03-07 09:21:25 -0800139 gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY);
Doug Zongker211aebc2011-10-28 15:13:10 -0700140 }
141
142 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700143 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700144 int width = gr_get_width(progressBarEmpty);
145 int height = gr_get_height(progressBarEmpty);
146
147 int dx = (gr_fb_width() - width)/2;
148 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
149
150 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700151 gr_color(0, 0, 0, 255);
Doug Zongker211aebc2011-10-28 15:13:10 -0700152 gr_fill(dx, dy, width, height);
153
154 if (progressBarType == DETERMINATE) {
155 float p = progressScopeStart + progress * progressScopeSize;
156 int pos = (int) (p * width);
157
Doug Zongker5fa8c232012-09-18 12:37:02 -0700158 if (rtl_locale) {
159 // Fill the progress bar from right to left.
160 if (pos > 0) {
161 gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
162 }
163 if (pos < width-1) {
164 gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
165 }
166 } else {
167 // Fill the progress bar from left to right.
168 if (pos > 0) {
169 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
170 }
171 if (pos < width-1) {
172 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
173 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700174 }
175 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700176 }
177}
178
Doug Zongkerc0441d12013-07-31 11:28:24 -0700179void ScreenRecoveryUI::SetColor(UIElement e) {
180 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700181 case INFO:
182 gr_color(249, 194, 0, 255);
183 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700184 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700185 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700186 break;
187 case MENU:
188 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700189 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700190 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700191 case MENU_SEL_BG_ACTIVE:
192 gr_color(0, 156, 100, 255);
193 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700194 case MENU_SEL_FG:
195 gr_color(255, 255, 255, 255);
196 break;
197 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700198 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700199 break;
200 case TEXT_FILL:
201 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700202 break;
203 default:
204 gr_color(255, 255, 255, 255);
205 break;
206 }
207}
Doug Zongker211aebc2011-10-28 15:13:10 -0700208
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700209void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
210 SetColor(MENU);
211 *y += 4;
212 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700213 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700214}
215
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700216void ScreenRecoveryUI::DrawTextLine(int* y, const char* line, bool bold) {
217 gr_text(4, *y, line, bold);
218 *y += char_height + 4;
219}
220
221void ScreenRecoveryUI::DrawTextLines(int* y, const char* const* lines) {
222 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
223 DrawTextLine(y, lines[i], false);
224 }
225}
226
227static const char* REGULAR_HELP[] = {
228 "Use volume up/down and power.",
229 NULL
230};
231
232static const char* LONG_PRESS_HELP[] = {
233 "Any button cycles highlight.",
234 "Long-press activates.",
235 NULL
236};
237
Doug Zongker211aebc2011-10-28 15:13:10 -0700238// Redraw everything on the screen. Does not flip pages.
239// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700240void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800241 if (!show_text) {
242 draw_background_locked(currentIcon);
243 draw_progress_locked();
244 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700245 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800246 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700247
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800248 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700249 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700250 char recovery_fingerprint[PROPERTY_VALUE_MAX];
251 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
252
253 SetColor(INFO);
254 DrawTextLine(&y, "Android Recovery", true);
255 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
256 DrawTextLine(&y, chunk.c_str(), false);
257 }
258 DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
259
Doug Zongkerc0441d12013-07-31 11:28:24 -0700260 SetColor(HEADER);
Elliott Hughesc0491632015-05-06 12:40:05 -0700261 DrawTextLines(&y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700262
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700263 SetColor(MENU);
264 DrawHorizontalRule(&y);
265 y += 4;
266 for (int i = 0; i < menu_items; ++i) {
267 if (i == menu_sel) {
268 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700269 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700270 gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2);
271 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700272 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700273 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700274 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700275 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700276 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700277 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700278 y += char_height + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700279 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700280 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700281 }
282
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800283 // display from the bottom up, until we hit the top of the
284 // screen, the bottom of the menu, or we've displayed the
285 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700286 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700287 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700288 size_t count = 0;
289 for (int ty = gr_fb_height() - char_height;
Elliott Hughesc0491632015-05-06 12:40:05 -0700290 ty >= y && count < text_rows_;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800291 ty -= char_height, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700292 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800293 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700294 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700295 }
296 }
297}
298
299// Redraw everything on the screen and flip the screen (make it visible).
300// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700301void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700302 draw_screen_locked();
303 gr_flip();
304}
305
306// Updates only the progress bar, if possible, otherwise redraws the screen.
307// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700308void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700309 if (show_text || !pagesIdentical) {
310 draw_screen_locked(); // Must redraw the whole screen
311 pagesIdentical = true;
312 } else {
313 draw_progress_locked(); // Draw only the progress bar and overlays
314 }
315 gr_flip();
316}
317
318// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700319void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
320 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700321 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700322}
323
Elliott Hughes985022a2015-04-13 13:04:32 -0700324void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700325 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700326 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700327 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700328 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700329
330 int redraw = 0;
331
332 // update the installation animation, if active
333 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700334 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
335 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700336 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700337 redraw = 1;
338 }
339
Doug Zongker211aebc2011-10-28 15:13:10 -0700340 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700341 int duration = progressScopeDuration;
342 if (progressBarType == DETERMINATE && duration > 0) {
343 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700344 float p = 1.0 * elapsed / duration;
345 if (p > 1.0) p = 1.0;
346 if (p > progress) {
347 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700348 redraw = 1;
349 }
350 }
351
Doug Zongker32a0a472011-11-01 11:00:20 -0700352 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700353
Doug Zongker32a0a472011-11-01 11:00:20 -0700354 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700355 double end = now();
356 // minimum of 20ms delay between frames
357 double delay = interval - (end-start);
358 if (delay < 0.02) delay = 0.02;
359 usleep((long)(delay * 1000000));
360 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700361}
362
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700363void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700364 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700365 if (result < 0) {
366 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
367 }
368}
369
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700370void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, GRSurface*** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700371 int result = res_create_multi_display_surface(filename, frames, surface);
Doug Zongkereac881c2014-03-07 09:21:25 -0800372 if (result < 0) {
373 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
374 }
375}
376
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700377void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700378 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700379 if (result < 0) {
380 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
381 }
382}
383
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700384static char** Alloc2d(size_t rows, size_t cols) {
385 char** result = new char*[rows];
386 for (size_t i = 0; i < rows; ++i) {
387 result[i] = new char[cols];
388 memset(result[i], 0, cols);
389 }
390 return result;
391}
392
Elliott Hughes8de52072015-04-08 20:06:50 -0700393void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700394 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700395
Doug Zongker55a36ac2013-03-04 15:49:02 -0800396 gr_font_size(&char_width, &char_height);
Elliott Hughesc0491632015-05-06 12:40:05 -0700397 text_rows_ = gr_fb_height() / char_height;
398 text_cols_ = gr_fb_width() / char_width;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700399
Elliott Hughesc0491632015-05-06 12:40:05 -0700400 text_ = Alloc2d(text_rows_, text_cols_ + 1);
401 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
402 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800403
Elliott Hughesc0491632015-05-06 12:40:05 -0700404 text_col_ = text_row_ = 0;
405 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700406
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700407 backgroundIcon[NONE] = nullptr;
Doug Zongkereac881c2014-03-07 09:21:25 -0800408 LoadBitmapArray("icon_installing", &installing_frames, &installation);
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700409 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700410 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700411 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700412 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
413
Doug Zongker211aebc2011-10-28 15:13:10 -0700414 LoadBitmap("progress_empty", &progressBarEmpty);
415 LoadBitmap("progress_fill", &progressBarFill);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800416 LoadBitmap("stage_empty", &stageMarkerEmpty);
417 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700418
Doug Zongker02ec6b82012-08-22 17:26:40 -0700419 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
420 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
421 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
422 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
423
Elliott Hughes985022a2015-04-13 13:04:32 -0700424 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700425
426 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700427}
428
Doug Zongkera418aa72014-03-17 12:10:02 -0700429void ScreenRecoveryUI::SetLocale(const char* new_locale) {
430 if (new_locale) {
431 this->locale = new_locale;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700432 char* lang = strdup(locale);
433 for (char* p = lang; *p; ++p) {
434 if (*p == '_') {
435 *p = '\0';
436 break;
437 }
438 }
439
440 // A bit cheesy: keep an explicit list of supported languages
441 // that are RTL.
442 if (strcmp(lang, "ar") == 0 || // Arabic
443 strcmp(lang, "fa") == 0 || // Persian (Farsi)
444 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700445 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
446 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700447 rtl_locale = true;
448 }
449 free(lang);
Doug Zongkera418aa72014-03-17 12:10:02 -0700450 } else {
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700451 new_locale = nullptr;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700452 }
453}
454
Elliott Hughes8de52072015-04-08 20:06:50 -0700455void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700456 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700457
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700458 currentIcon = icon;
459 update_screen_locked();
460
Doug Zongker211aebc2011-10-28 15:13:10 -0700461 pthread_mutex_unlock(&updateMutex);
462}
463
Elliott Hughes8de52072015-04-08 20:06:50 -0700464void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700465 pthread_mutex_lock(&updateMutex);
466 if (progressBarType != type) {
467 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700468 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700469 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700470 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700471 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700472 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700473 pthread_mutex_unlock(&updateMutex);
474}
475
Elliott Hughes8de52072015-04-08 20:06:50 -0700476void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700477 pthread_mutex_lock(&updateMutex);
478 progressBarType = DETERMINATE;
479 progressScopeStart += progressScopeSize;
480 progressScopeSize = portion;
481 progressScopeTime = now();
482 progressScopeDuration = seconds;
483 progress = 0;
484 update_progress_locked();
485 pthread_mutex_unlock(&updateMutex);
486}
487
Elliott Hughes8de52072015-04-08 20:06:50 -0700488void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700489 pthread_mutex_lock(&updateMutex);
490 if (fraction < 0.0) fraction = 0.0;
491 if (fraction > 1.0) fraction = 1.0;
492 if (progressBarType == DETERMINATE && fraction > progress) {
493 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800494 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700495 float scale = width * progressScopeSize;
496 if ((int) (progress * scale) != (int) (fraction * scale)) {
497 progress = fraction;
498 update_progress_locked();
499 }
500 }
501 pthread_mutex_unlock(&updateMutex);
502}
503
Doug Zongkerc87bab12013-11-25 13:53:25 -0800504void ScreenRecoveryUI::SetStage(int current, int max) {
505 pthread_mutex_lock(&updateMutex);
506 stage = current;
507 max_stage = max;
508 pthread_mutex_unlock(&updateMutex);
509}
510
Tao Baob6918c72015-05-19 17:02:16 -0700511void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
512 std::string str;
513 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700514
Tao Baob6918c72015-05-19 17:02:16 -0700515 if (copy_to_stdout) {
516 fputs(str.c_str(), stdout);
517 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700518
Doug Zongker211aebc2011-10-28 15:13:10 -0700519 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700520 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700521 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700522 if (*ptr == '\n' || text_col_ >= text_cols_) {
523 text_[text_row_][text_col_] = '\0';
524 text_col_ = 0;
525 text_row_ = (text_row_ + 1) % text_rows_;
526 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700527 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700528 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700529 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700530 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700531 update_screen_locked();
532 }
533 pthread_mutex_unlock(&updateMutex);
534}
535
Tao Baob6918c72015-05-19 17:02:16 -0700536void ScreenRecoveryUI::Print(const char* fmt, ...) {
537 va_list ap;
538 va_start(ap, fmt);
539 PrintV(fmt, true, ap);
540 va_end(ap);
541}
542
543void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
544 va_list ap;
545 va_start(ap, fmt);
546 PrintV(fmt, false, ap);
547 va_end(ap);
548}
549
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700550void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700551 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700552 if (ch != '\n') text_[text_row_][text_col_++] = ch;
553 if (ch == '\n' || text_col_ >= text_cols_) {
554 text_col_ = 0;
555 ++text_row_;
556
557 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700558 }
559 pthread_mutex_unlock(&updateMutex);
560}
561
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700562void ScreenRecoveryUI::ClearText() {
563 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700564 text_col_ = 0;
565 text_row_ = 0;
566 text_top_ = 1;
567 for (size_t i = 0; i < text_rows_; ++i) {
568 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700569 }
570 pthread_mutex_unlock(&updateMutex);
571}
572
573void ScreenRecoveryUI::ShowFile(FILE* fp) {
574 std::vector<long> offsets;
575 offsets.push_back(ftell(fp));
576 ClearText();
577
578 struct stat sb;
579 fstat(fileno(fp), &sb);
580
581 bool show_prompt = false;
582 while (true) {
583 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700584 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700585 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
586 static_cast<int>(sb.st_size));
587 Redraw();
588 while (show_prompt) {
589 show_prompt = false;
590 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700591 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700592 return;
593 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
594 if (offsets.size() <= 1) {
595 show_prompt = true;
596 } else {
597 offsets.pop_back();
598 fseek(fp, offsets.back(), SEEK_SET);
599 }
600 } else {
601 if (feof(fp)) {
602 return;
603 }
604 offsets.push_back(ftell(fp));
605 }
606 }
607 ClearText();
608 }
609
610 int ch = getc(fp);
611 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700612 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700613 show_prompt = true;
614 } else {
615 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700616 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700617 show_prompt = true;
618 }
619 }
620 }
621}
622
Elliott Hughes8de52072015-04-08 20:06:50 -0700623void ScreenRecoveryUI::ShowFile(const char* filename) {
624 FILE* fp = fopen_path(filename, "re");
625 if (fp == nullptr) {
626 Print(" Unable to open %s: %s\n", filename, strerror(errno));
627 return;
628 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700629
630 char** old_text = text_;
631 size_t old_text_col = text_col_;
632 size_t old_text_row = text_row_;
633 size_t old_text_top = text_top_;
634
635 // Swap in the alternate screen and clear it.
636 text_ = file_viewer_text_;
637 ClearText();
638
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700639 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700640 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700641
642 text_ = old_text;
643 text_col_ = old_text_col;
644 text_row_ = old_text_row;
645 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700646}
647
Doug Zongker211aebc2011-10-28 15:13:10 -0700648void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
649 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700650 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700651 if (text_rows_ > 0 && text_cols_ > 0) {
652 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700653 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700654 for (; i < text_rows_ && items[i] != nullptr; ++i) {
655 strncpy(menu_[i], items[i], text_cols_ - 1);
656 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700657 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700658 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700659 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700660 menu_sel = initial_selection;
661 update_screen_locked();
662 }
663 pthread_mutex_unlock(&updateMutex);
664}
665
666int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700667 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700668 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700669 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700670 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700671
672 // Wrap at top and bottom.
673 if (menu_sel < 0) menu_sel = menu_items - 1;
674 if (menu_sel >= menu_items) menu_sel = 0;
675
Doug Zongker211aebc2011-10-28 15:13:10 -0700676 sel = menu_sel;
677 if (menu_sel != old_sel) update_screen_locked();
678 }
679 pthread_mutex_unlock(&updateMutex);
680 return sel;
681}
682
683void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700684 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700685 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700686 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700687 update_screen_locked();
688 }
689 pthread_mutex_unlock(&updateMutex);
690}
691
Elliott Hughes8de52072015-04-08 20:06:50 -0700692bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700693 pthread_mutex_lock(&updateMutex);
694 int visible = show_text;
695 pthread_mutex_unlock(&updateMutex);
696 return visible;
697}
698
Elliott Hughes8de52072015-04-08 20:06:50 -0700699bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700700 pthread_mutex_lock(&updateMutex);
701 int ever_visible = show_text_ever;
702 pthread_mutex_unlock(&updateMutex);
703 return ever_visible;
704}
705
Elliott Hughes8de52072015-04-08 20:06:50 -0700706void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700707 pthread_mutex_lock(&updateMutex);
708 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700709 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700710 update_screen_locked();
711 pthread_mutex_unlock(&updateMutex);
712}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700713
Elliott Hughes8de52072015-04-08 20:06:50 -0700714void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700715 pthread_mutex_lock(&updateMutex);
716 update_screen_locked();
717 pthread_mutex_unlock(&updateMutex);
718}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700719
720void ScreenRecoveryUI::KeyLongPress(int) {
721 // Redraw so that if we're in the menu, the highlight
722 // will change color to indicate a successful long press.
723 Redraw();
724}