blob: 4ca96a9b2262ff04cb33b92ddf9b144b4e53864e [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
Tao Bao736d59c2017-01-03 10:15:33 -080032#include <string>
Elliott Hughes95fc63e2015-04-10 19:12:01 -070033#include <vector>
34
Tianjie Xuc21edd42016-08-05 18:00:04 -070035#include <android-base/logging.h>
Elliott Hughescb220402016-09-23 15:30:55 -070036#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080037#include <android-base/strings.h>
38#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070039
Doug Zongker211aebc2011-10-28 15:13:10 -070040#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070041#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042#include "minui/minui.h"
43#include "screen_ui.h"
44#include "ui.h"
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
Tao Bao736d59c2017-01-03 10:15:33 -080053ScreenRecoveryUI::ScreenRecoveryUI()
Tao Bao855eaff2017-06-20 18:11:21 -070054 : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH),
55 kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT),
56 density_(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f),
Tao Bao8f7547e2017-06-19 23:10:44 -070057 currentIcon(NONE),
Tao Bao736d59c2017-01-03 10:15:33 -080058 progressBarType(EMPTY),
59 progressScopeStart(0),
60 progressScopeSize(0),
61 progress(0),
62 pagesIdentical(false),
63 text_cols_(0),
64 text_rows_(0),
65 text_(nullptr),
66 text_col_(0),
67 text_row_(0),
68 text_top_(0),
69 show_text(false),
70 show_text_ever(false),
71 menu_(nullptr),
72 show_menu(false),
73 menu_items(0),
74 menu_sel(0),
75 file_viewer_text_(nullptr),
76 intro_frames(0),
77 loop_frames(0),
78 current_frame(0),
79 intro_done(false),
80 animation_fps(30), // TODO: there's currently no way to infer this.
81 stage(-1),
82 max_stage(-1),
83 updateMutex(PTHREAD_MUTEX_INITIALIZER) {}
Doug Zongker211aebc2011-10-28 15:13:10 -070084
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
Mikhail Lappob49767c2017-03-23 21:44:26 +0100102int ScreenRecoveryUI::PixelsFromDp(int dp) const {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700103 return dp * density_;
104}
105
106// Here's the intended layout:
107
Elliott Hughes6d089a92016-07-08 17:23:41 -0700108// | portrait large landscape large
109// ---------+-------------------------------------------------
110// gap | 220dp 366dp 142dp 284dp
111// icon | (200dp)
112// gap | 68dp 68dp 56dp 112dp
113// text | (14sp)
114// gap | 32dp 32dp 26dp 52dp
115// progress | (2dp)
116// gap | 194dp 340dp 131dp 262dp
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
Elliott Hughes6d089a92016-07-08 17:23:41 -0700121enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX };
122enum Dimension { PROGRESS = 0, TEXT = 1, ICON = 2, DIMENSION_MAX };
123static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = {
124 { 194, 32, 68, }, // PORTRAIT
125 { 340, 32, 68, }, // PORTRAIT_LARGE
126 { 131, 26, 56, }, // LANDSCAPE
127 { 262, 52, 112, }, // LANDSCAPE_LARGE
128};
129
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700130int ScreenRecoveryUI::GetAnimationBaseline() {
Elliott Hughes6d089a92016-07-08 17:23:41 -0700131 return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) -
132 gr_get_height(loopFrames[0]);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700133}
134
135int ScreenRecoveryUI::GetTextBaseline() {
Elliott Hughes6d089a92016-07-08 17:23:41 -0700136 return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) -
137 gr_get_height(installing_text);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700138}
139
140int ScreenRecoveryUI::GetProgressBaseline() {
Elliott Hughes6d089a92016-07-08 17:23:41 -0700141 return gr_fb_height() - PixelsFromDp(kLayouts[layout_][PROGRESS]) -
142 gr_get_height(progressBarFill);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700143}
144
Doug Zongker211aebc2011-10-28 15:13:10 -0700145// Clear the screen and draw the currently selected background icon (if any).
146// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700147void ScreenRecoveryUI::draw_background_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700148 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700149 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800150 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700151
Elliott Hughes498cda62016-04-14 16:49:04 -0700152 if (currentIcon != NONE) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700153 if (max_stage != -1) {
154 int stage_height = gr_get_height(stageMarkerEmpty);
155 int stage_width = gr_get_width(stageMarkerEmpty);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800156 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700157 int y = gr_fb_height() - stage_height;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800158 for (int i = 0; i < max_stage; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700159 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
160 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
161 x += stage_width;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800162 }
163 }
164
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700165 GRSurface* text_surface = GetCurrentText();
166 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
167 int text_y = GetTextBaseline();
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700168 gr_color(255, 255, 255, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700169 gr_texticon(text_x, text_y, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700170 }
171}
172
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700173// Draws the animation and progress bar (if any) on the screen.
174// Does not flip pages.
Doug Zongker211aebc2011-10-28 15:13:10 -0700175// Should only be called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700176void ScreenRecoveryUI::draw_foreground_locked() {
Tao Bao736d59c2017-01-03 10:15:33 -0800177 if (currentIcon != NONE) {
178 GRSurface* frame = GetCurrentFrame();
179 int frame_width = gr_get_width(frame);
180 int frame_height = gr_get_height(frame);
181 int frame_x = (gr_fb_width() - frame_width) / 2;
182 int frame_y = GetAnimationBaseline();
183 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
184 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700185
Tao Bao736d59c2017-01-03 10:15:33 -0800186 if (progressBarType != EMPTY) {
187 int width = gr_get_width(progressBarEmpty);
188 int height = gr_get_height(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700189
Tao Bao736d59c2017-01-03 10:15:33 -0800190 int progress_x = (gr_fb_width() - width) / 2;
191 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700192
Tao Bao736d59c2017-01-03 10:15:33 -0800193 // Erase behind the progress bar (in case this was a progress-only update)
194 gr_color(0, 0, 0, 255);
195 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700196
Tao Bao736d59c2017-01-03 10:15:33 -0800197 if (progressBarType == DETERMINATE) {
198 float p = progressScopeStart + progress * progressScopeSize;
199 int pos = static_cast<int>(p * width);
Doug Zongker211aebc2011-10-28 15:13:10 -0700200
Tao Bao736d59c2017-01-03 10:15:33 -0800201 if (rtl_locale_) {
202 // Fill the progress bar from right to left.
203 if (pos > 0) {
204 gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
205 progress_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700206 }
Tao Bao736d59c2017-01-03 10:15:33 -0800207 if (pos < width - 1) {
208 gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
209 }
210 } else {
211 // Fill the progress bar from left to right.
212 if (pos > 0) {
213 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
214 }
215 if (pos < width - 1) {
216 gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
217 }
218 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700219 }
Tao Bao736d59c2017-01-03 10:15:33 -0800220 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700221}
222
Doug Zongkerc0441d12013-07-31 11:28:24 -0700223void ScreenRecoveryUI::SetColor(UIElement e) {
224 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700225 case INFO:
226 gr_color(249, 194, 0, 255);
227 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700229 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700230 break;
231 case MENU:
232 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700233 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700234 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700235 case MENU_SEL_BG_ACTIVE:
236 gr_color(0, 156, 100, 255);
237 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700238 case MENU_SEL_FG:
239 gr_color(255, 255, 255, 255);
240 break;
241 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700242 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700243 break;
244 case TEXT_FILL:
245 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700246 break;
247 default:
248 gr_color(255, 255, 255, 255);
249 break;
250 }
251}
Doug Zongker211aebc2011-10-28 15:13:10 -0700252
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700253void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
254 SetColor(MENU);
255 *y += 4;
256 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700257 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700258}
259
Mikhail Lappob49767c2017-03-23 21:44:26 +0100260void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) const {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700261 gr_text(gr_sys_font(), x, *y, line, bold);
Prashant Malani7a491222016-03-11 10:00:55 -0800262 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700263}
264
Mikhail Lappob49767c2017-03-23 21:44:26 +0100265void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) const {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700266 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800267 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700268 }
269}
270
271static const char* REGULAR_HELP[] = {
272 "Use volume up/down and power.",
273 NULL
274};
275
276static const char* LONG_PRESS_HELP[] = {
277 "Any button cycles highlight.",
278 "Long-press activates.",
279 NULL
280};
281
Tao Bao8f7547e2017-06-19 23:10:44 -0700282// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex
283// locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700284void ScreenRecoveryUI::draw_screen_locked() {
Tao Bao8f7547e2017-06-19 23:10:44 -0700285 if (!show_text) {
286 draw_background_locked();
287 draw_foreground_locked();
288 return;
289 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700290
Tao Bao8f7547e2017-06-19 23:10:44 -0700291 gr_color(0, 0, 0, 255);
292 gr_clear();
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700293
Tao Bao8f7547e2017-06-19 23:10:44 -0700294 static constexpr int TEXT_INDENT = 4;
Tao Bao855eaff2017-06-20 18:11:21 -0700295 int x = TEXT_INDENT + kMarginWidth;
296 int y = kMarginHeight;
Tao Bao8f7547e2017-06-19 23:10:44 -0700297 if (show_menu) {
298 std::string recovery_fingerprint =
299 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700300
Tao Bao8f7547e2017-06-19 23:10:44 -0700301 SetColor(INFO);
302 DrawTextLine(x, &y, "Android Recovery", true);
303 for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
304 DrawTextLine(x, &y, chunk.c_str(), false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700305 }
Tao Bao8f7547e2017-06-19 23:10:44 -0700306 DrawTextLines(x, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
307
308 SetColor(HEADER);
309 DrawTextLines(x, &y, menu_headers_);
310
311 SetColor(MENU);
312 DrawHorizontalRule(&y);
313 y += 4;
314 for (int i = 0; i < menu_items; ++i) {
315 if (i == menu_sel) {
316 // Draw the highlight bar.
317 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
318 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
319 // Bold white text for the selected item.
320 SetColor(MENU_SEL_FG);
321 gr_text(gr_sys_font(), 4, y, menu_[i], true);
322 SetColor(MENU);
323 } else {
324 gr_text(gr_sys_font(), 4, y, menu_[i], false);
325 }
326 y += char_height_ + 4;
327 }
328 DrawHorizontalRule(&y);
329 }
330
331 // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or
332 // we've displayed the entire text buffer.
333 SetColor(LOG);
334 int row = (text_top_ + text_rows_ - 1) % text_rows_;
335 size_t count = 0;
Tao Bao855eaff2017-06-20 18:11:21 -0700336 for (int ty = gr_fb_height() - kMarginHeight - char_height_;
Tao Bao8f7547e2017-06-19 23:10:44 -0700337 ty >= y && count < text_rows_; ty -= char_height_, ++count) {
338 gr_text(gr_sys_font(), 0, ty, text_[row], false);
339 --row;
340 if (row < 0) row = text_rows_ - 1;
341 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700342}
343
344// Redraw everything on the screen and flip the screen (make it visible).
345// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700346void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700347 draw_screen_locked();
348 gr_flip();
349}
350
351// Updates only the progress bar, if possible, otherwise redraws the screen.
352// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700353void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700354 if (show_text || !pagesIdentical) {
355 draw_screen_locked(); // Must redraw the whole screen
356 pagesIdentical = true;
357 } else {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700358 draw_foreground_locked(); // Draw only the progress bar and overlays
Doug Zongker211aebc2011-10-28 15:13:10 -0700359 }
360 gr_flip();
361}
362
363// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700364void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
365 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700366 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700367}
368
Elliott Hughes985022a2015-04-13 13:04:32 -0700369void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700370 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700371 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700372 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700373 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700374
Elliott Hughes498cda62016-04-14 16:49:04 -0700375 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700376
377 // update the installation animation, if active
378 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700379 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
380 if (!intro_done) {
381 if (current_frame == intro_frames - 1) {
382 intro_done = true;
383 current_frame = 0;
384 } else {
385 ++current_frame;
386 }
387 } else {
388 current_frame = (current_frame + 1) % loop_frames;
389 }
390
391 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700392 }
393
Doug Zongker211aebc2011-10-28 15:13:10 -0700394 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700395 int duration = progressScopeDuration;
396 if (progressBarType == DETERMINATE && duration > 0) {
397 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700398 float p = 1.0 * elapsed / duration;
399 if (p > 1.0) p = 1.0;
400 if (p > progress) {
401 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700402 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700403 }
404 }
405
Doug Zongker32a0a472011-11-01 11:00:20 -0700406 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700407
Doug Zongker32a0a472011-11-01 11:00:20 -0700408 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700409 double end = now();
410 // minimum of 20ms delay between frames
411 double delay = interval - (end-start);
412 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700413 usleep(static_cast<useconds_t>(delay * 1000000));
Doug Zongker211aebc2011-10-28 15:13:10 -0700414 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700415}
416
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700417void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700418 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700419 if (result < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700420 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
Doug Zongkereac881c2014-03-07 09:21:25 -0800421 }
422}
423
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700424void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Tao Bao736d59c2017-01-03 10:15:33 -0800425 int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface);
426 if (result < 0) {
427 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
428 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700429}
430
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700431static char** Alloc2d(size_t rows, size_t cols) {
432 char** result = new char*[rows];
433 for (size_t i = 0; i < rows; ++i) {
434 result[i] = new char[cols];
435 memset(result[i], 0, cols);
436 }
437 return result;
438}
439
Tianjie Xu35926c42016-04-28 18:06:26 -0700440// Choose the right background string to display during update.
441void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
442 if (security_update) {
443 LoadLocalizedBitmap("installing_security_text", &installing_text);
444 } else {
445 LoadLocalizedBitmap("installing_text", &installing_text);
446 }
447 Redraw();
448}
449
Sen Jiangd5304492016-12-09 16:20:49 -0800450bool ScreenRecoveryUI::InitTextParams() {
Tao Bao8f7547e2017-06-19 23:10:44 -0700451 if (gr_init() < 0) {
452 return false;
453 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700454
Tao Bao8f7547e2017-06-19 23:10:44 -0700455 gr_font_size(gr_sys_font(), &char_width_, &char_height_);
Tao Bao855eaff2017-06-20 18:11:21 -0700456 text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
457 text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
Tao Bao8f7547e2017-06-19 23:10:44 -0700458 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700459}
460
Tao Bao736d59c2017-01-03 10:15:33 -0800461bool ScreenRecoveryUI::Init(const std::string& locale) {
462 RecoveryUI::Init(locale);
463 if (!InitTextParams()) {
464 return false;
465 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700466
Tao Bao736d59c2017-01-03 10:15:33 -0800467 // Are we portrait or landscape?
468 layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT;
469 // Are we the large variant of our base layout?
470 if (gr_fb_height() > PixelsFromDp(800)) ++layout_;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700471
Tao Bao736d59c2017-01-03 10:15:33 -0800472 text_ = Alloc2d(text_rows_, text_cols_ + 1);
473 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
474 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800475
Tao Bao736d59c2017-01-03 10:15:33 -0800476 text_col_ = text_row_ = 0;
477 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700478
Tao Bao736d59c2017-01-03 10:15:33 -0800479 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700480
Tao Bao736d59c2017-01-03 10:15:33 -0800481 LoadBitmap("progress_empty", &progressBarEmpty);
482 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700483
Tao Bao736d59c2017-01-03 10:15:33 -0800484 LoadBitmap("stage_empty", &stageMarkerEmpty);
485 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700486
Tao Bao736d59c2017-01-03 10:15:33 -0800487 // Background text for "installing_update" could be "installing update"
488 // or "installing security update". It will be set after UI init according
489 // to commands in BCB.
490 installing_text = nullptr;
491 LoadLocalizedBitmap("erasing_text", &erasing_text);
492 LoadLocalizedBitmap("no_command_text", &no_command_text);
493 LoadLocalizedBitmap("error_text", &error_text);
Elliott Hughes498cda62016-04-14 16:49:04 -0700494
Tao Bao736d59c2017-01-03 10:15:33 -0800495 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700496
Tao Bao736d59c2017-01-03 10:15:33 -0800497 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Sen Jiangd5304492016-12-09 16:20:49 -0800498
Tao Bao736d59c2017-01-03 10:15:33 -0800499 return true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700500}
501
Elliott Hughes498cda62016-04-14 16:49:04 -0700502void ScreenRecoveryUI::LoadAnimation() {
Elliott Hughes498cda62016-04-14 16:49:04 -0700503 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
504 dirent* de;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700505 std::vector<std::string> intro_frame_names;
506 std::vector<std::string> loop_frame_names;
507
Elliott Hughes498cda62016-04-14 16:49:04 -0700508 while ((de = readdir(dir.get())) != nullptr) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700509 int value, num_chars;
510 if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) {
511 intro_frame_names.emplace_back(de->d_name, num_chars);
512 } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) {
513 loop_frame_names.emplace_back(de->d_name, num_chars);
Elliott Hughes498cda62016-04-14 16:49:04 -0700514 }
515 }
516
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700517 intro_frames = intro_frame_names.size();
518 loop_frames = loop_frame_names.size();
519
Elliott Hughes498cda62016-04-14 16:49:04 -0700520 // It's okay to not have an intro.
521 if (intro_frames == 0) intro_done = true;
522 // But you must have an animation.
523 if (loop_frames == 0) abort();
524
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700525 std::sort(intro_frame_names.begin(), intro_frame_names.end());
526 std::sort(loop_frame_names.begin(), loop_frame_names.end());
527
Elliott Hughes498cda62016-04-14 16:49:04 -0700528 introFrames = new GRSurface*[intro_frames];
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700529 for (size_t i = 0; i < intro_frames; i++) {
530 LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700531 }
532
533 loopFrames = new GRSurface*[loop_frames];
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700534 for (size_t i = 0; i < loop_frames; i++) {
535 LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700536 }
537}
538
Elliott Hughes8de52072015-04-08 20:06:50 -0700539void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700540 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700541
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700542 currentIcon = icon;
543 update_screen_locked();
544
Doug Zongker211aebc2011-10-28 15:13:10 -0700545 pthread_mutex_unlock(&updateMutex);
546}
547
Elliott Hughes8de52072015-04-08 20:06:50 -0700548void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700549 pthread_mutex_lock(&updateMutex);
550 if (progressBarType != type) {
551 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700552 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700553 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700554 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700555 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700556 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700557 pthread_mutex_unlock(&updateMutex);
558}
559
Elliott Hughes8de52072015-04-08 20:06:50 -0700560void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700561 pthread_mutex_lock(&updateMutex);
562 progressBarType = DETERMINATE;
563 progressScopeStart += progressScopeSize;
564 progressScopeSize = portion;
565 progressScopeTime = now();
566 progressScopeDuration = seconds;
567 progress = 0;
568 update_progress_locked();
569 pthread_mutex_unlock(&updateMutex);
570}
571
Elliott Hughes8de52072015-04-08 20:06:50 -0700572void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700573 pthread_mutex_lock(&updateMutex);
574 if (fraction < 0.0) fraction = 0.0;
575 if (fraction > 1.0) fraction = 1.0;
576 if (progressBarType == DETERMINATE && fraction > progress) {
577 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800578 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700579 float scale = width * progressScopeSize;
580 if ((int) (progress * scale) != (int) (fraction * scale)) {
581 progress = fraction;
582 update_progress_locked();
583 }
584 }
585 pthread_mutex_unlock(&updateMutex);
586}
587
Doug Zongkerc87bab12013-11-25 13:53:25 -0800588void ScreenRecoveryUI::SetStage(int current, int max) {
589 pthread_mutex_lock(&updateMutex);
590 stage = current;
591 max_stage = max;
592 pthread_mutex_unlock(&updateMutex);
593}
594
Tao Baob6918c72015-05-19 17:02:16 -0700595void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
596 std::string str;
597 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700598
Tao Baob6918c72015-05-19 17:02:16 -0700599 if (copy_to_stdout) {
600 fputs(str.c_str(), stdout);
601 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700602
Doug Zongker211aebc2011-10-28 15:13:10 -0700603 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700604 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700605 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700606 if (*ptr == '\n' || text_col_ >= text_cols_) {
607 text_[text_row_][text_col_] = '\0';
608 text_col_ = 0;
609 text_row_ = (text_row_ + 1) % text_rows_;
610 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700611 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700612 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700613 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700614 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700615 update_screen_locked();
616 }
617 pthread_mutex_unlock(&updateMutex);
618}
619
Tao Baob6918c72015-05-19 17:02:16 -0700620void ScreenRecoveryUI::Print(const char* fmt, ...) {
621 va_list ap;
622 va_start(ap, fmt);
623 PrintV(fmt, true, ap);
624 va_end(ap);
625}
626
627void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
628 va_list ap;
629 va_start(ap, fmt);
630 PrintV(fmt, false, ap);
631 va_end(ap);
632}
633
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700634void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700635 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700636 if (ch != '\n') text_[text_row_][text_col_++] = ch;
637 if (ch == '\n' || text_col_ >= text_cols_) {
638 text_col_ = 0;
639 ++text_row_;
640
641 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700642 }
643 pthread_mutex_unlock(&updateMutex);
644}
645
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700646void ScreenRecoveryUI::ClearText() {
647 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700648 text_col_ = 0;
649 text_row_ = 0;
650 text_top_ = 1;
651 for (size_t i = 0; i < text_rows_; ++i) {
652 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700653 }
654 pthread_mutex_unlock(&updateMutex);
655}
656
657void ScreenRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700658 std::vector<off_t> offsets;
659 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700660 ClearText();
661
662 struct stat sb;
663 fstat(fileno(fp), &sb);
664
665 bool show_prompt = false;
666 while (true) {
667 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700668 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700669 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700670 static_cast<int>(sb.st_size));
671 Redraw();
672 while (show_prompt) {
673 show_prompt = false;
674 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700675 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700676 return;
677 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
678 if (offsets.size() <= 1) {
679 show_prompt = true;
680 } else {
681 offsets.pop_back();
682 fseek(fp, offsets.back(), SEEK_SET);
683 }
684 } else {
685 if (feof(fp)) {
686 return;
687 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700688 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700689 }
690 }
691 ClearText();
692 }
693
694 int ch = getc(fp);
695 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700696 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700697 show_prompt = true;
698 } else {
699 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700700 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700701 show_prompt = true;
702 }
703 }
704 }
705}
706
Elliott Hughes8de52072015-04-08 20:06:50 -0700707void ScreenRecoveryUI::ShowFile(const char* filename) {
708 FILE* fp = fopen_path(filename, "re");
709 if (fp == nullptr) {
710 Print(" Unable to open %s: %s\n", filename, strerror(errno));
711 return;
712 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700713
714 char** old_text = text_;
715 size_t old_text_col = text_col_;
716 size_t old_text_row = text_row_;
717 size_t old_text_top = text_top_;
718
719 // Swap in the alternate screen and clear it.
720 text_ = file_viewer_text_;
721 ClearText();
722
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700723 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700724 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700725
726 text_ = old_text;
727 text_col_ = old_text_col;
728 text_row_ = old_text_row;
729 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700730}
731
Doug Zongker211aebc2011-10-28 15:13:10 -0700732void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
733 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700734 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700735 if (text_rows_ > 0 && text_cols_ > 0) {
736 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700737 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700738 for (; i < text_rows_ && items[i] != nullptr; ++i) {
739 strncpy(menu_[i], items[i], text_cols_ - 1);
740 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700741 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700742 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700743 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700744 menu_sel = initial_selection;
745 update_screen_locked();
746 }
747 pthread_mutex_unlock(&updateMutex);
748}
749
750int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700751 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700752 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700753 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700754 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700755
756 // Wrap at top and bottom.
757 if (menu_sel < 0) menu_sel = menu_items - 1;
758 if (menu_sel >= menu_items) menu_sel = 0;
759
Doug Zongker211aebc2011-10-28 15:13:10 -0700760 sel = menu_sel;
761 if (menu_sel != old_sel) update_screen_locked();
762 }
763 pthread_mutex_unlock(&updateMutex);
764 return sel;
765}
766
767void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700768 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700769 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700770 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700771 update_screen_locked();
772 }
773 pthread_mutex_unlock(&updateMutex);
774}
775
Elliott Hughes8de52072015-04-08 20:06:50 -0700776bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700777 pthread_mutex_lock(&updateMutex);
778 int visible = show_text;
779 pthread_mutex_unlock(&updateMutex);
780 return visible;
781}
782
Elliott Hughes8de52072015-04-08 20:06:50 -0700783bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700784 pthread_mutex_lock(&updateMutex);
785 int ever_visible = show_text_ever;
786 pthread_mutex_unlock(&updateMutex);
787 return ever_visible;
788}
789
Elliott Hughes8de52072015-04-08 20:06:50 -0700790void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700791 pthread_mutex_lock(&updateMutex);
792 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700793 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700794 update_screen_locked();
795 pthread_mutex_unlock(&updateMutex);
796}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700797
Elliott Hughes8de52072015-04-08 20:06:50 -0700798void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700799 pthread_mutex_lock(&updateMutex);
800 update_screen_locked();
801 pthread_mutex_unlock(&updateMutex);
802}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700803
804void ScreenRecoveryUI::KeyLongPress(int) {
805 // Redraw so that if we're in the menu, the highlight
806 // will change color to indicate a successful long press.
807 Redraw();
808}