blob: 85f789f3fafbb4d4f0e93a9c1000a275860f2a82 [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes498cda62016-04-14 16:49:04 -070017#include <dirent.h>
Doug Zongker211aebc2011-10-28 15:13:10 -070018#include <errno.h>
19#include <fcntl.h>
20#include <linux/input.h>
21#include <pthread.h>
22#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
31
Elliott Hughes95fc63e2015-04-10 19:12:01 -070032#include <vector>
33
Elliott Hughes4b166f02015-12-04 15:30:20 -080034#include <android-base/strings.h>
35#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070036#include <cutils/properties.h>
37
Doug Zongker211aebc2011-10-28 15:13:10 -070038#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070039#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070040#include "minui/minui.h"
41#include "screen_ui.h"
42#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070043
Prashant Malani7a491222016-03-11 10:00:55 -080044#define TEXT_INDENT 4
Doug Zongker211aebc2011-10-28 15:13:10 -070045
Doug Zongker211aebc2011-10-28 15:13:10 -070046// Return the current time as a double (including fractions of a second).
47static double now() {
48 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070049 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070050 return tv.tv_sec + tv.tv_usec / 1000000.0;
51}
52
53ScreenRecoveryUI::ScreenRecoveryUI() :
Prashant Malanif7f9e502016-03-10 03:40:20 +000054 currentIcon(NONE),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070055 locale(nullptr),
Elliott Hughes498cda62016-04-14 16:49:04 -070056 intro_done(false),
57 current_frame(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070058 progressBarType(EMPTY),
59 progressScopeStart(0),
60 progressScopeSize(0),
61 progress(0),
62 pagesIdentical(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070063 text_cols_(0),
64 text_rows_(0),
65 text_(nullptr),
66 text_col_(0),
67 text_row_(0),
68 text_top_(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070069 show_text(false),
70 show_text_ever(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070071 menu_(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070072 show_menu(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070073 menu_items(0),
74 menu_sel(0),
Elliott Hughesc0491632015-05-06 12:40:05 -070075 file_viewer_text_(nullptr),
Elliott Hughes498cda62016-04-14 16:49:04 -070076 intro_frames(0),
77 loop_frames(0),
78 animation_fps(30), // TODO: there's currently no way to infer this.
Doug Zongkerc87bab12013-11-25 13:53:25 -080079 stage(-1),
Prashant Malani0ba21cf2016-03-10 14:51:25 -080080 max_stage(-1),
Elliott Hughesfaf36e02016-04-20 17:22:16 -070081 updateMutex(PTHREAD_MUTEX_INITIALIZER),
Prashant Malani0ba21cf2016-03-10 14:51:25 -080082 rtl_locale(false) {
Doug Zongker211aebc2011-10-28 15:13:10 -070083}
84
Elliott Hughes498cda62016-04-14 16:49:04 -070085GRSurface* ScreenRecoveryUI::GetCurrentFrame() {
86 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
87 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
88 }
89 return error_icon;
90}
91
92GRSurface* ScreenRecoveryUI::GetCurrentText() {
93 switch (currentIcon) {
94 case ERASING: return erasing_text;
95 case ERROR: return error_text;
96 case INSTALLING_UPDATE: return installing_text;
97 case NO_COMMAND: return no_command_text;
98 case NONE: abort();
99 }
100}
101
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700102int ScreenRecoveryUI::PixelsFromDp(int dp) {
103 return dp * density_;
104}
105
106// Here's the intended layout:
107
Elliott Hughesa3691042016-04-27 17:40:11 -0700108// | regular large
109// ---------+--------------------
110// | 220dp 366dp
111// icon | (200dp) (200dp)
112// | 68dp 68dp
113// text | (14sp) (14sp)
114// | 32dp 32dp
115// progress | (2dp) (2dp)
116// | 194dp 340dp
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700117
118// Note that "baseline" is actually the *top* of each icon (because that's how our drawing
Elliott Hughesa3691042016-04-27 17:40:11 -0700119// routines work), so that's the more useful measurement for calling code.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700120
121int ScreenRecoveryUI::GetAnimationBaseline() {
122 return GetTextBaseline() - PixelsFromDp(68) - gr_get_height(loopFrames[0]);
123}
124
125int ScreenRecoveryUI::GetTextBaseline() {
126 return GetProgressBaseline() - PixelsFromDp(32) - gr_get_height(installing_text);
127}
128
129int ScreenRecoveryUI::GetProgressBaseline() {
130 return gr_fb_height() - PixelsFromDp(is_large_ ? 340 : 194) - gr_get_height(progressBarFill);
131}
132
Doug Zongker211aebc2011-10-28 15:13:10 -0700133// Clear the screen and draw the currently selected background icon (if any).
134// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700135void ScreenRecoveryUI::draw_background_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700136 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700137 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800138 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700139
Elliott Hughes498cda62016-04-14 16:49:04 -0700140 if (currentIcon != NONE) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700141 if (max_stage != -1) {
142 int stage_height = gr_get_height(stageMarkerEmpty);
143 int stage_width = gr_get_width(stageMarkerEmpty);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800144 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700145 int y = gr_fb_height() - stage_height;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800146 for (int i = 0; i < max_stage; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700147 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
148 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
149 x += stage_width;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800150 }
151 }
152
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700153 GRSurface* text_surface = GetCurrentText();
154 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
155 int text_y = GetTextBaseline();
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700156 gr_color(255, 255, 255, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700157 gr_texticon(text_x, text_y, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700158 }
159}
160
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700161// Draws the animation and progress bar (if any) on the screen.
162// Does not flip pages.
Doug Zongker211aebc2011-10-28 15:13:10 -0700163// Should only be called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700164void ScreenRecoveryUI::draw_foreground_locked() {
165 if (currentIcon != NONE) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700166 GRSurface* frame = GetCurrentFrame();
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700167 int frame_width = gr_get_width(frame);
168 int frame_height = gr_get_height(frame);
169 int frame_x = (gr_fb_width() - frame_width) / 2;
170 int frame_y = GetAnimationBaseline();
171 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700172 }
173
174 if (progressBarType != EMPTY) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700175 int width = gr_get_width(progressBarEmpty);
176 int height = gr_get_height(progressBarEmpty);
177
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700178 int progress_x = (gr_fb_width() - width)/2;
179 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700180
181 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700182 gr_color(0, 0, 0, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700183 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700184
185 if (progressBarType == DETERMINATE) {
186 float p = progressScopeStart + progress * progressScopeSize;
187 int pos = (int) (p * width);
188
Doug Zongker5fa8c232012-09-18 12:37:02 -0700189 if (rtl_locale) {
190 // Fill the progress bar from right to left.
191 if (pos > 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700192 gr_blit(progressBarFill, width-pos, 0, pos, height,
193 progress_x+width-pos, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700194 }
195 if (pos < width-1) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700196 gr_blit(progressBarEmpty, 0, 0, width-pos, height, progress_x, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700197 }
198 } else {
199 // Fill the progress bar from left to right.
200 if (pos > 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700201 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700202 }
203 if (pos < width-1) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700204 gr_blit(progressBarEmpty, pos, 0, width-pos, height,
205 progress_x+pos, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700206 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700207 }
208 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700209 }
210}
211
Doug Zongkerc0441d12013-07-31 11:28:24 -0700212void ScreenRecoveryUI::SetColor(UIElement e) {
213 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700214 case INFO:
215 gr_color(249, 194, 0, 255);
216 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700217 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700218 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700219 break;
220 case MENU:
221 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700222 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700223 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700224 case MENU_SEL_BG_ACTIVE:
225 gr_color(0, 156, 100, 255);
226 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700227 case MENU_SEL_FG:
228 gr_color(255, 255, 255, 255);
229 break;
230 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700231 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700232 break;
233 case TEXT_FILL:
234 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700235 break;
236 default:
237 gr_color(255, 255, 255, 255);
238 break;
239 }
240}
Doug Zongker211aebc2011-10-28 15:13:10 -0700241
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700242void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
243 SetColor(MENU);
244 *y += 4;
245 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700246 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700247}
248
Prashant Malani7a491222016-03-11 10:00:55 -0800249void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
250 gr_text(x, *y, line, bold);
251 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700252}
253
Prashant Malani7a491222016-03-11 10:00:55 -0800254void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700255 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800256 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700257 }
258}
259
260static const char* REGULAR_HELP[] = {
261 "Use volume up/down and power.",
262 NULL
263};
264
265static const char* LONG_PRESS_HELP[] = {
266 "Any button cycles highlight.",
267 "Long-press activates.",
268 NULL
269};
270
Doug Zongker211aebc2011-10-28 15:13:10 -0700271// Redraw everything on the screen. Does not flip pages.
272// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700273void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800274 if (!show_text) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700275 draw_background_locked();
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700276 draw_foreground_locked();
Doug Zongker39cf4172014-03-06 16:16:05 -0800277 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700278 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800279 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700280
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800281 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700282 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700283 char recovery_fingerprint[PROPERTY_VALUE_MAX];
284 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
285
286 SetColor(INFO);
Prashant Malani7a491222016-03-11 10:00:55 -0800287 DrawTextLine(TEXT_INDENT, &y, "Android Recovery", true);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700288 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Prashant Malani7a491222016-03-11 10:00:55 -0800289 DrawTextLine(TEXT_INDENT, &y, chunk.c_str(), false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700290 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700291 DrawTextLines(TEXT_INDENT, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700292
Doug Zongkerc0441d12013-07-31 11:28:24 -0700293 SetColor(HEADER);
Prashant Malani7a491222016-03-11 10:00:55 -0800294 DrawTextLines(TEXT_INDENT, &y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700295
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700296 SetColor(MENU);
297 DrawHorizontalRule(&y);
298 y += 4;
299 for (int i = 0; i < menu_items; ++i) {
300 if (i == menu_sel) {
301 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700302 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800303 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700304 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700305 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700306 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700307 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700308 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700309 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700310 }
Prashant Malani7a491222016-03-11 10:00:55 -0800311 y += char_height_ + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700312 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700313 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 }
315
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800316 // display from the bottom up, until we hit the top of the
317 // screen, the bottom of the menu, or we've displayed the
318 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700319 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700320 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700321 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800322 for (int ty = gr_fb_height() - char_height_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700323 ty >= y && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800324 ty -= char_height_, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700325 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800326 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700327 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700328 }
329 }
330}
331
332// Redraw everything on the screen and flip the screen (make it visible).
333// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700334void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700335 draw_screen_locked();
336 gr_flip();
337}
338
339// Updates only the progress bar, if possible, otherwise redraws the screen.
340// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700341void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700342 if (show_text || !pagesIdentical) {
343 draw_screen_locked(); // Must redraw the whole screen
344 pagesIdentical = true;
345 } else {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700346 draw_foreground_locked(); // Draw only the progress bar and overlays
Doug Zongker211aebc2011-10-28 15:13:10 -0700347 }
348 gr_flip();
349}
350
351// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700352void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
353 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700354 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700355}
356
Elliott Hughes985022a2015-04-13 13:04:32 -0700357void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700358 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700359 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700360 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700361 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700362
Elliott Hughes498cda62016-04-14 16:49:04 -0700363 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700364
365 // update the installation animation, if active
366 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700367 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
368 if (!intro_done) {
369 if (current_frame == intro_frames - 1) {
370 intro_done = true;
371 current_frame = 0;
372 } else {
373 ++current_frame;
374 }
375 } else {
376 current_frame = (current_frame + 1) % loop_frames;
377 }
378
379 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700380 }
381
Doug Zongker211aebc2011-10-28 15:13:10 -0700382 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700383 int duration = progressScopeDuration;
384 if (progressBarType == DETERMINATE && duration > 0) {
385 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700386 float p = 1.0 * elapsed / duration;
387 if (p > 1.0) p = 1.0;
388 if (p > progress) {
389 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700390 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700391 }
392 }
393
Doug Zongker32a0a472011-11-01 11:00:20 -0700394 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700395
Doug Zongker32a0a472011-11-01 11:00:20 -0700396 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700397 double end = now();
398 // minimum of 20ms delay between frames
399 double delay = interval - (end-start);
400 if (delay < 0.02) delay = 0.02;
401 usleep((long)(delay * 1000000));
402 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700403}
404
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700405void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700406 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700407 if (result < 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700408 LOGE("couldn't load bitmap %s (error %d)\n", filename, result);
Doug Zongkereac881c2014-03-07 09:21:25 -0800409 }
410}
411
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700412void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700413 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700414 if (result < 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700415 LOGE("couldn't load bitmap %s (error %d)\n", filename, result);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700416 }
417}
418
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700419static char** Alloc2d(size_t rows, size_t cols) {
420 char** result = new char*[rows];
421 for (size_t i = 0; i < rows; ++i) {
422 result[i] = new char[cols];
423 memset(result[i], 0, cols);
424 }
425 return result;
426}
427
Tianjie Xu35926c42016-04-28 18:06:26 -0700428// Choose the right background string to display during update.
429void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
430 if (security_update) {
431 LoadLocalizedBitmap("installing_security_text", &installing_text);
432 } else {
433 LoadLocalizedBitmap("installing_text", &installing_text);
434 }
435 Redraw();
436}
437
Elliott Hughes8de52072015-04-08 20:06:50 -0700438void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700439 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700440
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700441 density_ = static_cast<float>(property_get_int32("ro.sf.lcd_density", 160)) / 160.f;
442 is_large_ = gr_fb_height() > PixelsFromDp(800);
443
Prashant Malani7a491222016-03-11 10:00:55 -0800444 gr_font_size(&char_width_, &char_height_);
445 text_rows_ = gr_fb_height() / char_height_;
446 text_cols_ = gr_fb_width() / char_width_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700447
Elliott Hughesc0491632015-05-06 12:40:05 -0700448 text_ = Alloc2d(text_rows_, text_cols_ + 1);
449 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
450 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800451
Elliott Hughesc0491632015-05-06 12:40:05 -0700452 text_col_ = text_row_ = 0;
453 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700454
Elliott Hughes498cda62016-04-14 16:49:04 -0700455 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700456
Doug Zongker211aebc2011-10-28 15:13:10 -0700457 LoadBitmap("progress_empty", &progressBarEmpty);
458 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700459
Doug Zongkerc87bab12013-11-25 13:53:25 -0800460 LoadBitmap("stage_empty", &stageMarkerEmpty);
461 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700462
Tianjie Xu35926c42016-04-28 18:06:26 -0700463 // Background text for "installing_update" could be "installing update"
464 // or "installing security update". It will be set after UI init according
465 // to commands in BCB.
466 installing_text = nullptr;
Elliott Hughes498cda62016-04-14 16:49:04 -0700467 LoadLocalizedBitmap("erasing_text", &erasing_text);
468 LoadLocalizedBitmap("no_command_text", &no_command_text);
469 LoadLocalizedBitmap("error_text", &error_text);
470
471 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700472
Elliott Hughes985022a2015-04-13 13:04:32 -0700473 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700474
475 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700476}
477
Elliott Hughes498cda62016-04-14 16:49:04 -0700478void ScreenRecoveryUI::LoadAnimation() {
479 // How many frames of intro and loop do we have?
480 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
481 dirent* de;
482 while ((de = readdir(dir.get())) != nullptr) {
483 int value;
484 if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) {
485 intro_frames = value + 1;
486 } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) {
487 loop_frames = value + 1;
488 }
489 }
490
491 // It's okay to not have an intro.
492 if (intro_frames == 0) intro_done = true;
493 // But you must have an animation.
494 if (loop_frames == 0) abort();
495
496 introFrames = new GRSurface*[intro_frames];
497 for (int i = 0; i < intro_frames; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700498 // TODO: remember the names above, so we don't have to hard-code the number of 0s.
499 LoadBitmap(android::base::StringPrintf("intro%05d", i).c_str(), &introFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700500 }
501
502 loopFrames = new GRSurface*[loop_frames];
503 for (int i = 0; i < loop_frames; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700504 LoadBitmap(android::base::StringPrintf("loop%05d", i).c_str(), &loopFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700505 }
506}
507
Doug Zongkera418aa72014-03-17 12:10:02 -0700508void ScreenRecoveryUI::SetLocale(const char* new_locale) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700509 this->locale = new_locale;
510 this->rtl_locale = false;
511
512 if (locale) {
Doug Zongker5fa8c232012-09-18 12:37:02 -0700513 char* lang = strdup(locale);
514 for (char* p = lang; *p; ++p) {
515 if (*p == '_') {
516 *p = '\0';
517 break;
518 }
519 }
520
Elliott Hughes498cda62016-04-14 16:49:04 -0700521 // A bit cheesy: keep an explicit list of supported RTL languages.
Doug Zongker5fa8c232012-09-18 12:37:02 -0700522 if (strcmp(lang, "ar") == 0 || // Arabic
523 strcmp(lang, "fa") == 0 || // Persian (Farsi)
524 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700525 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
526 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700527 rtl_locale = true;
528 }
529 free(lang);
530 }
531}
532
Elliott Hughes8de52072015-04-08 20:06:50 -0700533void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700534 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700535
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700536 currentIcon = icon;
537 update_screen_locked();
538
Doug Zongker211aebc2011-10-28 15:13:10 -0700539 pthread_mutex_unlock(&updateMutex);
540}
541
Elliott Hughes8de52072015-04-08 20:06:50 -0700542void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700543 pthread_mutex_lock(&updateMutex);
544 if (progressBarType != type) {
545 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700546 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700547 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700548 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700549 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700550 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700551 pthread_mutex_unlock(&updateMutex);
552}
553
Elliott Hughes8de52072015-04-08 20:06:50 -0700554void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700555 pthread_mutex_lock(&updateMutex);
556 progressBarType = DETERMINATE;
557 progressScopeStart += progressScopeSize;
558 progressScopeSize = portion;
559 progressScopeTime = now();
560 progressScopeDuration = seconds;
561 progress = 0;
562 update_progress_locked();
563 pthread_mutex_unlock(&updateMutex);
564}
565
Elliott Hughes8de52072015-04-08 20:06:50 -0700566void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700567 pthread_mutex_lock(&updateMutex);
568 if (fraction < 0.0) fraction = 0.0;
569 if (fraction > 1.0) fraction = 1.0;
570 if (progressBarType == DETERMINATE && fraction > progress) {
571 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800572 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700573 float scale = width * progressScopeSize;
574 if ((int) (progress * scale) != (int) (fraction * scale)) {
575 progress = fraction;
576 update_progress_locked();
577 }
578 }
579 pthread_mutex_unlock(&updateMutex);
580}
581
Doug Zongkerc87bab12013-11-25 13:53:25 -0800582void ScreenRecoveryUI::SetStage(int current, int max) {
583 pthread_mutex_lock(&updateMutex);
584 stage = current;
585 max_stage = max;
586 pthread_mutex_unlock(&updateMutex);
587}
588
Tao Baob6918c72015-05-19 17:02:16 -0700589void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
590 std::string str;
591 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700592
Tao Baob6918c72015-05-19 17:02:16 -0700593 if (copy_to_stdout) {
594 fputs(str.c_str(), stdout);
595 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700596
Doug Zongker211aebc2011-10-28 15:13:10 -0700597 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700598 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700599 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700600 if (*ptr == '\n' || text_col_ >= text_cols_) {
601 text_[text_row_][text_col_] = '\0';
602 text_col_ = 0;
603 text_row_ = (text_row_ + 1) % text_rows_;
604 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700605 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700606 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700607 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700608 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700609 update_screen_locked();
610 }
611 pthread_mutex_unlock(&updateMutex);
612}
613
Tao Baob6918c72015-05-19 17:02:16 -0700614void ScreenRecoveryUI::Print(const char* fmt, ...) {
615 va_list ap;
616 va_start(ap, fmt);
617 PrintV(fmt, true, ap);
618 va_end(ap);
619}
620
621void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
622 va_list ap;
623 va_start(ap, fmt);
624 PrintV(fmt, false, ap);
625 va_end(ap);
626}
627
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700628void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700629 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700630 if (ch != '\n') text_[text_row_][text_col_++] = ch;
631 if (ch == '\n' || text_col_ >= text_cols_) {
632 text_col_ = 0;
633 ++text_row_;
634
635 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700636 }
637 pthread_mutex_unlock(&updateMutex);
638}
639
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700640void ScreenRecoveryUI::ClearText() {
641 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700642 text_col_ = 0;
643 text_row_ = 0;
644 text_top_ = 1;
645 for (size_t i = 0; i < text_rows_; ++i) {
646 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700647 }
648 pthread_mutex_unlock(&updateMutex);
649}
650
651void ScreenRecoveryUI::ShowFile(FILE* fp) {
652 std::vector<long> offsets;
653 offsets.push_back(ftell(fp));
654 ClearText();
655
656 struct stat sb;
657 fstat(fileno(fp), &sb);
658
659 bool show_prompt = false;
660 while (true) {
661 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700662 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700663 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
664 static_cast<int>(sb.st_size));
665 Redraw();
666 while (show_prompt) {
667 show_prompt = false;
668 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700669 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700670 return;
671 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
672 if (offsets.size() <= 1) {
673 show_prompt = true;
674 } else {
675 offsets.pop_back();
676 fseek(fp, offsets.back(), SEEK_SET);
677 }
678 } else {
679 if (feof(fp)) {
680 return;
681 }
682 offsets.push_back(ftell(fp));
683 }
684 }
685 ClearText();
686 }
687
688 int ch = getc(fp);
689 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700690 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700691 show_prompt = true;
692 } else {
693 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700694 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700695 show_prompt = true;
696 }
697 }
698 }
699}
700
Elliott Hughes8de52072015-04-08 20:06:50 -0700701void ScreenRecoveryUI::ShowFile(const char* filename) {
702 FILE* fp = fopen_path(filename, "re");
703 if (fp == nullptr) {
704 Print(" Unable to open %s: %s\n", filename, strerror(errno));
705 return;
706 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700707
708 char** old_text = text_;
709 size_t old_text_col = text_col_;
710 size_t old_text_row = text_row_;
711 size_t old_text_top = text_top_;
712
713 // Swap in the alternate screen and clear it.
714 text_ = file_viewer_text_;
715 ClearText();
716
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700717 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700718 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700719
720 text_ = old_text;
721 text_col_ = old_text_col;
722 text_row_ = old_text_row;
723 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700724}
725
Doug Zongker211aebc2011-10-28 15:13:10 -0700726void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
727 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700728 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700729 if (text_rows_ > 0 && text_cols_ > 0) {
730 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700731 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700732 for (; i < text_rows_ && items[i] != nullptr; ++i) {
733 strncpy(menu_[i], items[i], text_cols_ - 1);
734 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700735 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700736 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700737 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700738 menu_sel = initial_selection;
739 update_screen_locked();
740 }
741 pthread_mutex_unlock(&updateMutex);
742}
743
744int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700745 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700746 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700747 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700748 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700749
750 // Wrap at top and bottom.
751 if (menu_sel < 0) menu_sel = menu_items - 1;
752 if (menu_sel >= menu_items) menu_sel = 0;
753
Doug Zongker211aebc2011-10-28 15:13:10 -0700754 sel = menu_sel;
755 if (menu_sel != old_sel) update_screen_locked();
756 }
757 pthread_mutex_unlock(&updateMutex);
758 return sel;
759}
760
761void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700762 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700763 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700764 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700765 update_screen_locked();
766 }
767 pthread_mutex_unlock(&updateMutex);
768}
769
Elliott Hughes8de52072015-04-08 20:06:50 -0700770bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700771 pthread_mutex_lock(&updateMutex);
772 int visible = show_text;
773 pthread_mutex_unlock(&updateMutex);
774 return visible;
775}
776
Elliott Hughes8de52072015-04-08 20:06:50 -0700777bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700778 pthread_mutex_lock(&updateMutex);
779 int ever_visible = show_text_ever;
780 pthread_mutex_unlock(&updateMutex);
781 return ever_visible;
782}
783
Elliott Hughes8de52072015-04-08 20:06:50 -0700784void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700785 pthread_mutex_lock(&updateMutex);
786 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700787 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700788 update_screen_locked();
789 pthread_mutex_unlock(&updateMutex);
790}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700791
Elliott Hughes8de52072015-04-08 20:06:50 -0700792void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700793 pthread_mutex_lock(&updateMutex);
794 update_screen_locked();
795 pthread_mutex_unlock(&updateMutex);
796}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700797
798void ScreenRecoveryUI::KeyLongPress(int) {
799 // Redraw so that if we're in the menu, the highlight
800 // will change color to indicate a successful long press.
801 Redraw();
802}