blob: 465be23fa3b5f8dde5d3ea05a2dc856c52acfa7f [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
108// 162dp
109// icon (200dp)
110// 62dp
111// text (14sp)
112// 24dp
113// progress (2dp)
114// 172dp
115
116// Note that "baseline" is actually the *top* of each icon (because that's how our drawing
117// routines work), so that's the more useful measurement.
118
119int ScreenRecoveryUI::GetAnimationBaseline() {
120 return GetTextBaseline() - PixelsFromDp(68) - gr_get_height(loopFrames[0]);
121}
122
123int ScreenRecoveryUI::GetTextBaseline() {
124 return GetProgressBaseline() - PixelsFromDp(32) - gr_get_height(installing_text);
125}
126
127int ScreenRecoveryUI::GetProgressBaseline() {
128 return gr_fb_height() - PixelsFromDp(is_large_ ? 340 : 194) - gr_get_height(progressBarFill);
129}
130
Doug Zongker211aebc2011-10-28 15:13:10 -0700131// Clear the screen and draw the currently selected background icon (if any).
132// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700133void ScreenRecoveryUI::draw_background_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700134 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700135 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800136 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700137
Elliott Hughes498cda62016-04-14 16:49:04 -0700138 if (currentIcon != NONE) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700139 if (max_stage != -1) {
140 int stage_height = gr_get_height(stageMarkerEmpty);
141 int stage_width = gr_get_width(stageMarkerEmpty);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800142 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700143 int y = gr_fb_height() - stage_height;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800144 for (int i = 0; i < max_stage; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700145 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
146 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
147 x += stage_width;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800148 }
149 }
150
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700151 GRSurface* text_surface = GetCurrentText();
152 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
153 int text_y = GetTextBaseline();
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700154 gr_color(255, 255, 255, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700155 gr_texticon(text_x, text_y, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700156 }
157}
158
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700159// Draws the animation and progress bar (if any) on the screen.
160// Does not flip pages.
Doug Zongker211aebc2011-10-28 15:13:10 -0700161// Should only be called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700162void ScreenRecoveryUI::draw_foreground_locked() {
163 if (currentIcon != NONE) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700164 GRSurface* frame = GetCurrentFrame();
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700165 int frame_width = gr_get_width(frame);
166 int frame_height = gr_get_height(frame);
167 int frame_x = (gr_fb_width() - frame_width) / 2;
168 int frame_y = GetAnimationBaseline();
169 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700170 }
171
172 if (progressBarType != EMPTY) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 int width = gr_get_width(progressBarEmpty);
174 int height = gr_get_height(progressBarEmpty);
175
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700176 int progress_x = (gr_fb_width() - width)/2;
177 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700178
179 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700180 gr_color(0, 0, 0, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700181 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700182
183 if (progressBarType == DETERMINATE) {
184 float p = progressScopeStart + progress * progressScopeSize;
185 int pos = (int) (p * width);
186
Doug Zongker5fa8c232012-09-18 12:37:02 -0700187 if (rtl_locale) {
188 // Fill the progress bar from right to left.
189 if (pos > 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700190 gr_blit(progressBarFill, width-pos, 0, pos, height,
191 progress_x+width-pos, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700192 }
193 if (pos < width-1) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700194 gr_blit(progressBarEmpty, 0, 0, width-pos, height, progress_x, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700195 }
196 } else {
197 // Fill the progress bar from left to right.
198 if (pos > 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700199 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700200 }
201 if (pos < width-1) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700202 gr_blit(progressBarEmpty, pos, 0, width-pos, height,
203 progress_x+pos, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700204 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700205 }
206 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700207 }
208}
209
Doug Zongkerc0441d12013-07-31 11:28:24 -0700210void ScreenRecoveryUI::SetColor(UIElement e) {
211 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700212 case INFO:
213 gr_color(249, 194, 0, 255);
214 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700215 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700216 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700217 break;
218 case MENU:
219 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700220 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700221 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700222 case MENU_SEL_BG_ACTIVE:
223 gr_color(0, 156, 100, 255);
224 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700225 case MENU_SEL_FG:
226 gr_color(255, 255, 255, 255);
227 break;
228 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700229 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700230 break;
231 case TEXT_FILL:
232 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700233 break;
234 default:
235 gr_color(255, 255, 255, 255);
236 break;
237 }
238}
Doug Zongker211aebc2011-10-28 15:13:10 -0700239
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700240void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
241 SetColor(MENU);
242 *y += 4;
243 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700244 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700245}
246
Prashant Malani7a491222016-03-11 10:00:55 -0800247void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
248 gr_text(x, *y, line, bold);
249 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700250}
251
Prashant Malani7a491222016-03-11 10:00:55 -0800252void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700253 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800254 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700255 }
256}
257
258static const char* REGULAR_HELP[] = {
259 "Use volume up/down and power.",
260 NULL
261};
262
263static const char* LONG_PRESS_HELP[] = {
264 "Any button cycles highlight.",
265 "Long-press activates.",
266 NULL
267};
268
Doug Zongker211aebc2011-10-28 15:13:10 -0700269// Redraw everything on the screen. Does not flip pages.
270// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700271void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800272 if (!show_text) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700273 draw_background_locked();
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700274 draw_foreground_locked();
Doug Zongker39cf4172014-03-06 16:16:05 -0800275 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700276 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800277 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700278
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800279 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700280 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700281 char recovery_fingerprint[PROPERTY_VALUE_MAX];
282 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
283
284 SetColor(INFO);
Prashant Malani7a491222016-03-11 10:00:55 -0800285 DrawTextLine(TEXT_INDENT, &y, "Android Recovery", true);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700286 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Prashant Malani7a491222016-03-11 10:00:55 -0800287 DrawTextLine(TEXT_INDENT, &y, chunk.c_str(), false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700288 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700289 DrawTextLines(TEXT_INDENT, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700290
Doug Zongkerc0441d12013-07-31 11:28:24 -0700291 SetColor(HEADER);
Prashant Malani7a491222016-03-11 10:00:55 -0800292 DrawTextLines(TEXT_INDENT, &y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700293
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700294 SetColor(MENU);
295 DrawHorizontalRule(&y);
296 y += 4;
297 for (int i = 0; i < menu_items; ++i) {
298 if (i == menu_sel) {
299 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700300 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800301 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700302 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700303 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700304 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700305 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700306 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700307 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700308 }
Prashant Malani7a491222016-03-11 10:00:55 -0800309 y += char_height_ + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700310 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700311 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700312 }
313
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800314 // display from the bottom up, until we hit the top of the
315 // screen, the bottom of the menu, or we've displayed the
316 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700317 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700318 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700319 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800320 for (int ty = gr_fb_height() - char_height_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700321 ty >= y && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800322 ty -= char_height_, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700323 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800324 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700325 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700326 }
327 }
328}
329
330// Redraw everything on the screen and flip the screen (make it visible).
331// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700332void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700333 draw_screen_locked();
334 gr_flip();
335}
336
337// Updates only the progress bar, if possible, otherwise redraws the screen.
338// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700339void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700340 if (show_text || !pagesIdentical) {
341 draw_screen_locked(); // Must redraw the whole screen
342 pagesIdentical = true;
343 } else {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700344 draw_foreground_locked(); // Draw only the progress bar and overlays
Doug Zongker211aebc2011-10-28 15:13:10 -0700345 }
346 gr_flip();
347}
348
349// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700350void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
351 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700352 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700353}
354
Elliott Hughes985022a2015-04-13 13:04:32 -0700355void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700356 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700357 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700358 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700359 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700360
Elliott Hughes498cda62016-04-14 16:49:04 -0700361 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700362
363 // update the installation animation, if active
364 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700365 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
366 if (!intro_done) {
367 if (current_frame == intro_frames - 1) {
368 intro_done = true;
369 current_frame = 0;
370 } else {
371 ++current_frame;
372 }
373 } else {
374 current_frame = (current_frame + 1) % loop_frames;
375 }
376
377 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700378 }
379
Doug Zongker211aebc2011-10-28 15:13:10 -0700380 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700381 int duration = progressScopeDuration;
382 if (progressBarType == DETERMINATE && duration > 0) {
383 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700384 float p = 1.0 * elapsed / duration;
385 if (p > 1.0) p = 1.0;
386 if (p > progress) {
387 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700388 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700389 }
390 }
391
Doug Zongker32a0a472011-11-01 11:00:20 -0700392 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700393
Doug Zongker32a0a472011-11-01 11:00:20 -0700394 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700395 double end = now();
396 // minimum of 20ms delay between frames
397 double delay = interval - (end-start);
398 if (delay < 0.02) delay = 0.02;
399 usleep((long)(delay * 1000000));
400 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700401}
402
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700403void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700404 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700405 if (result < 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700406 LOGE("couldn't load bitmap %s (error %d)\n", filename, result);
Doug Zongkereac881c2014-03-07 09:21:25 -0800407 }
408}
409
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700410void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700411 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700412 if (result < 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700413 LOGE("couldn't load bitmap %s (error %d)\n", filename, result);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700414 }
415}
416
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700417static char** Alloc2d(size_t rows, size_t cols) {
418 char** result = new char*[rows];
419 for (size_t i = 0; i < rows; ++i) {
420 result[i] = new char[cols];
421 memset(result[i], 0, cols);
422 }
423 return result;
424}
425
Elliott Hughes8de52072015-04-08 20:06:50 -0700426void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700427 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700428
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700429 density_ = static_cast<float>(property_get_int32("ro.sf.lcd_density", 160)) / 160.f;
430 is_large_ = gr_fb_height() > PixelsFromDp(800);
431
Prashant Malani7a491222016-03-11 10:00:55 -0800432 gr_font_size(&char_width_, &char_height_);
433 text_rows_ = gr_fb_height() / char_height_;
434 text_cols_ = gr_fb_width() / char_width_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700435
Elliott Hughesc0491632015-05-06 12:40:05 -0700436 text_ = Alloc2d(text_rows_, text_cols_ + 1);
437 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
438 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800439
Elliott Hughesc0491632015-05-06 12:40:05 -0700440 text_col_ = text_row_ = 0;
441 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700442
Elliott Hughes498cda62016-04-14 16:49:04 -0700443 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700444
Doug Zongker211aebc2011-10-28 15:13:10 -0700445 LoadBitmap("progress_empty", &progressBarEmpty);
446 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700447
Doug Zongkerc87bab12013-11-25 13:53:25 -0800448 LoadBitmap("stage_empty", &stageMarkerEmpty);
449 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700450
Elliott Hughes498cda62016-04-14 16:49:04 -0700451 LoadLocalizedBitmap("installing_text", &installing_text);
452 LoadLocalizedBitmap("erasing_text", &erasing_text);
453 LoadLocalizedBitmap("no_command_text", &no_command_text);
454 LoadLocalizedBitmap("error_text", &error_text);
455
456 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700457
Elliott Hughes985022a2015-04-13 13:04:32 -0700458 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700459
460 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700461}
462
Elliott Hughes498cda62016-04-14 16:49:04 -0700463void ScreenRecoveryUI::LoadAnimation() {
464 // How many frames of intro and loop do we have?
465 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
466 dirent* de;
467 while ((de = readdir(dir.get())) != nullptr) {
468 int value;
469 if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) {
470 intro_frames = value + 1;
471 } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) {
472 loop_frames = value + 1;
473 }
474 }
475
476 // It's okay to not have an intro.
477 if (intro_frames == 0) intro_done = true;
478 // But you must have an animation.
479 if (loop_frames == 0) abort();
480
481 introFrames = new GRSurface*[intro_frames];
482 for (int i = 0; i < intro_frames; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700483 // TODO: remember the names above, so we don't have to hard-code the number of 0s.
484 LoadBitmap(android::base::StringPrintf("intro%05d", i).c_str(), &introFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700485 }
486
487 loopFrames = new GRSurface*[loop_frames];
488 for (int i = 0; i < loop_frames; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700489 LoadBitmap(android::base::StringPrintf("loop%05d", i).c_str(), &loopFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700490 }
491}
492
Doug Zongkera418aa72014-03-17 12:10:02 -0700493void ScreenRecoveryUI::SetLocale(const char* new_locale) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700494 this->locale = new_locale;
495 this->rtl_locale = false;
496
497 if (locale) {
Doug Zongker5fa8c232012-09-18 12:37:02 -0700498 char* lang = strdup(locale);
499 for (char* p = lang; *p; ++p) {
500 if (*p == '_') {
501 *p = '\0';
502 break;
503 }
504 }
505
Elliott Hughes498cda62016-04-14 16:49:04 -0700506 // A bit cheesy: keep an explicit list of supported RTL languages.
Doug Zongker5fa8c232012-09-18 12:37:02 -0700507 if (strcmp(lang, "ar") == 0 || // Arabic
508 strcmp(lang, "fa") == 0 || // Persian (Farsi)
509 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700510 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
511 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700512 rtl_locale = true;
513 }
514 free(lang);
515 }
516}
517
Elliott Hughes8de52072015-04-08 20:06:50 -0700518void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700519 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700520
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700521 currentIcon = icon;
522 update_screen_locked();
523
Doug Zongker211aebc2011-10-28 15:13:10 -0700524 pthread_mutex_unlock(&updateMutex);
525}
526
Elliott Hughes8de52072015-04-08 20:06:50 -0700527void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700528 pthread_mutex_lock(&updateMutex);
529 if (progressBarType != type) {
530 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700531 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700532 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700533 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700534 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700535 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700536 pthread_mutex_unlock(&updateMutex);
537}
538
Elliott Hughes8de52072015-04-08 20:06:50 -0700539void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700540 pthread_mutex_lock(&updateMutex);
541 progressBarType = DETERMINATE;
542 progressScopeStart += progressScopeSize;
543 progressScopeSize = portion;
544 progressScopeTime = now();
545 progressScopeDuration = seconds;
546 progress = 0;
547 update_progress_locked();
548 pthread_mutex_unlock(&updateMutex);
549}
550
Elliott Hughes8de52072015-04-08 20:06:50 -0700551void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700552 pthread_mutex_lock(&updateMutex);
553 if (fraction < 0.0) fraction = 0.0;
554 if (fraction > 1.0) fraction = 1.0;
555 if (progressBarType == DETERMINATE && fraction > progress) {
556 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800557 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700558 float scale = width * progressScopeSize;
559 if ((int) (progress * scale) != (int) (fraction * scale)) {
560 progress = fraction;
561 update_progress_locked();
562 }
563 }
564 pthread_mutex_unlock(&updateMutex);
565}
566
Doug Zongkerc87bab12013-11-25 13:53:25 -0800567void ScreenRecoveryUI::SetStage(int current, int max) {
568 pthread_mutex_lock(&updateMutex);
569 stage = current;
570 max_stage = max;
571 pthread_mutex_unlock(&updateMutex);
572}
573
Tao Baob6918c72015-05-19 17:02:16 -0700574void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
575 std::string str;
576 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700577
Tao Baob6918c72015-05-19 17:02:16 -0700578 if (copy_to_stdout) {
579 fputs(str.c_str(), stdout);
580 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700581
Doug Zongker211aebc2011-10-28 15:13:10 -0700582 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700583 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700584 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700585 if (*ptr == '\n' || text_col_ >= text_cols_) {
586 text_[text_row_][text_col_] = '\0';
587 text_col_ = 0;
588 text_row_ = (text_row_ + 1) % text_rows_;
589 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700590 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700591 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700592 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700593 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700594 update_screen_locked();
595 }
596 pthread_mutex_unlock(&updateMutex);
597}
598
Tao Baob6918c72015-05-19 17:02:16 -0700599void ScreenRecoveryUI::Print(const char* fmt, ...) {
600 va_list ap;
601 va_start(ap, fmt);
602 PrintV(fmt, true, ap);
603 va_end(ap);
604}
605
606void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
607 va_list ap;
608 va_start(ap, fmt);
609 PrintV(fmt, false, ap);
610 va_end(ap);
611}
612
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700613void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700614 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700615 if (ch != '\n') text_[text_row_][text_col_++] = ch;
616 if (ch == '\n' || text_col_ >= text_cols_) {
617 text_col_ = 0;
618 ++text_row_;
619
620 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700621 }
622 pthread_mutex_unlock(&updateMutex);
623}
624
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700625void ScreenRecoveryUI::ClearText() {
626 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700627 text_col_ = 0;
628 text_row_ = 0;
629 text_top_ = 1;
630 for (size_t i = 0; i < text_rows_; ++i) {
631 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700632 }
633 pthread_mutex_unlock(&updateMutex);
634}
635
636void ScreenRecoveryUI::ShowFile(FILE* fp) {
637 std::vector<long> offsets;
638 offsets.push_back(ftell(fp));
639 ClearText();
640
641 struct stat sb;
642 fstat(fileno(fp), &sb);
643
644 bool show_prompt = false;
645 while (true) {
646 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700647 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700648 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
649 static_cast<int>(sb.st_size));
650 Redraw();
651 while (show_prompt) {
652 show_prompt = false;
653 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700654 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700655 return;
656 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
657 if (offsets.size() <= 1) {
658 show_prompt = true;
659 } else {
660 offsets.pop_back();
661 fseek(fp, offsets.back(), SEEK_SET);
662 }
663 } else {
664 if (feof(fp)) {
665 return;
666 }
667 offsets.push_back(ftell(fp));
668 }
669 }
670 ClearText();
671 }
672
673 int ch = getc(fp);
674 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700675 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700676 show_prompt = true;
677 } else {
678 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700679 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700680 show_prompt = true;
681 }
682 }
683 }
684}
685
Elliott Hughes8de52072015-04-08 20:06:50 -0700686void ScreenRecoveryUI::ShowFile(const char* filename) {
687 FILE* fp = fopen_path(filename, "re");
688 if (fp == nullptr) {
689 Print(" Unable to open %s: %s\n", filename, strerror(errno));
690 return;
691 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700692
693 char** old_text = text_;
694 size_t old_text_col = text_col_;
695 size_t old_text_row = text_row_;
696 size_t old_text_top = text_top_;
697
698 // Swap in the alternate screen and clear it.
699 text_ = file_viewer_text_;
700 ClearText();
701
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700702 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700703 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700704
705 text_ = old_text;
706 text_col_ = old_text_col;
707 text_row_ = old_text_row;
708 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700709}
710
Doug Zongker211aebc2011-10-28 15:13:10 -0700711void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
712 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700713 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700714 if (text_rows_ > 0 && text_cols_ > 0) {
715 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700716 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700717 for (; i < text_rows_ && items[i] != nullptr; ++i) {
718 strncpy(menu_[i], items[i], text_cols_ - 1);
719 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700720 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700721 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700722 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700723 menu_sel = initial_selection;
724 update_screen_locked();
725 }
726 pthread_mutex_unlock(&updateMutex);
727}
728
729int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700730 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700731 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700732 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700733 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700734
735 // Wrap at top and bottom.
736 if (menu_sel < 0) menu_sel = menu_items - 1;
737 if (menu_sel >= menu_items) menu_sel = 0;
738
Doug Zongker211aebc2011-10-28 15:13:10 -0700739 sel = menu_sel;
740 if (menu_sel != old_sel) update_screen_locked();
741 }
742 pthread_mutex_unlock(&updateMutex);
743 return sel;
744}
745
746void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700747 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700748 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700749 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700750 update_screen_locked();
751 }
752 pthread_mutex_unlock(&updateMutex);
753}
754
Elliott Hughes8de52072015-04-08 20:06:50 -0700755bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700756 pthread_mutex_lock(&updateMutex);
757 int visible = show_text;
758 pthread_mutex_unlock(&updateMutex);
759 return visible;
760}
761
Elliott Hughes8de52072015-04-08 20:06:50 -0700762bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700763 pthread_mutex_lock(&updateMutex);
764 int ever_visible = show_text_ever;
765 pthread_mutex_unlock(&updateMutex);
766 return ever_visible;
767}
768
Elliott Hughes8de52072015-04-08 20:06:50 -0700769void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700770 pthread_mutex_lock(&updateMutex);
771 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700772 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700773 update_screen_locked();
774 pthread_mutex_unlock(&updateMutex);
775}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700776
Elliott Hughes8de52072015-04-08 20:06:50 -0700777void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700778 pthread_mutex_lock(&updateMutex);
779 update_screen_locked();
780 pthread_mutex_unlock(&updateMutex);
781}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700782
783void ScreenRecoveryUI::KeyLongPress(int) {
784 // Redraw so that if we're in the menu, the highlight
785 // will change color to indicate a successful long press.
786 Redraw();
787}