blob: 79b5466a88b883215e8beddb403beffb570e584f [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
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070034#include <android-base/logging.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080035#include <android-base/strings.h>
36#include <android-base/stringprintf.h>
Tao Baob6918c72015-05-19 17:02:16 -070037#include <cutils/properties.h>
38
Doug Zongker211aebc2011-10-28 15:13:10 -070039#include "common.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070040#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070041#include "minui/minui.h"
42#include "screen_ui.h"
43#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070044
Prashant Malani7a491222016-03-11 10:00:55 -080045#define TEXT_INDENT 4
Doug Zongker211aebc2011-10-28 15:13:10 -070046
Doug Zongker211aebc2011-10-28 15:13:10 -070047// Return the current time as a double (including fractions of a second).
48static double now() {
49 struct timeval tv;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070050 gettimeofday(&tv, nullptr);
Doug Zongker211aebc2011-10-28 15:13:10 -070051 return tv.tv_sec + tv.tv_usec / 1000000.0;
52}
53
54ScreenRecoveryUI::ScreenRecoveryUI() :
Prashant Malanif7f9e502016-03-10 03:40:20 +000055 currentIcon(NONE),
Elliott Hughesaa0d6af2015-04-08 12:42:50 -070056 locale(nullptr),
Elliott Hughes498cda62016-04-14 16:49:04 -070057 intro_done(false),
58 current_frame(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070059 progressBarType(EMPTY),
60 progressScopeStart(0),
61 progressScopeSize(0),
62 progress(0),
63 pagesIdentical(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070064 text_cols_(0),
65 text_rows_(0),
66 text_(nullptr),
67 text_col_(0),
68 text_row_(0),
69 text_top_(0),
Doug Zongker211aebc2011-10-28 15:13:10 -070070 show_text(false),
71 show_text_ever(false),
Elliott Hughesc0491632015-05-06 12:40:05 -070072 menu_(nullptr),
Doug Zongker211aebc2011-10-28 15:13:10 -070073 show_menu(false),
Doug Zongker211aebc2011-10-28 15:13:10 -070074 menu_items(0),
75 menu_sel(0),
Elliott Hughesc0491632015-05-06 12:40:05 -070076 file_viewer_text_(nullptr),
Elliott Hughes498cda62016-04-14 16:49:04 -070077 intro_frames(0),
78 loop_frames(0),
79 animation_fps(30), // TODO: there's currently no way to infer this.
Doug Zongkerc87bab12013-11-25 13:53:25 -080080 stage(-1),
Prashant Malani0ba21cf2016-03-10 14:51:25 -080081 max_stage(-1),
Elliott Hughesfaf36e02016-04-20 17:22:16 -070082 updateMutex(PTHREAD_MUTEX_INITIALIZER),
Prashant Malani0ba21cf2016-03-10 14:51:25 -080083 rtl_locale(false) {
Doug Zongker211aebc2011-10-28 15:13:10 -070084}
85
Elliott Hughes498cda62016-04-14 16:49:04 -070086GRSurface* ScreenRecoveryUI::GetCurrentFrame() {
87 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
88 return intro_done ? loopFrames[current_frame] : introFrames[current_frame];
89 }
90 return error_icon;
91}
92
93GRSurface* ScreenRecoveryUI::GetCurrentText() {
94 switch (currentIcon) {
95 case ERASING: return erasing_text;
96 case ERROR: return error_text;
97 case INSTALLING_UPDATE: return installing_text;
98 case NO_COMMAND: return no_command_text;
99 case NONE: abort();
100 }
101}
102
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700103int ScreenRecoveryUI::PixelsFromDp(int dp) {
104 return dp * density_;
105}
106
107// Here's the intended layout:
108
Elliott Hughesa3691042016-04-27 17:40:11 -0700109// | regular large
110// ---------+--------------------
111// | 220dp 366dp
112// icon | (200dp) (200dp)
113// | 68dp 68dp
114// text | (14sp) (14sp)
115// | 32dp 32dp
116// progress | (2dp) (2dp)
117// | 194dp 340dp
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700118
119// Note that "baseline" is actually the *top* of each icon (because that's how our drawing
Elliott Hughesa3691042016-04-27 17:40:11 -0700120// routines work), so that's the more useful measurement for calling code.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700121
122int ScreenRecoveryUI::GetAnimationBaseline() {
123 return GetTextBaseline() - PixelsFromDp(68) - gr_get_height(loopFrames[0]);
124}
125
126int ScreenRecoveryUI::GetTextBaseline() {
127 return GetProgressBaseline() - PixelsFromDp(32) - gr_get_height(installing_text);
128}
129
130int ScreenRecoveryUI::GetProgressBaseline() {
131 return gr_fb_height() - PixelsFromDp(is_large_ ? 340 : 194) - gr_get_height(progressBarFill);
132}
133
Doug Zongker211aebc2011-10-28 15:13:10 -0700134// Clear the screen and draw the currently selected background icon (if any).
135// Should only be called with updateMutex locked.
Elliott Hughes498cda62016-04-14 16:49:04 -0700136void ScreenRecoveryUI::draw_background_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700137 pagesIdentical = false;
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700138 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800139 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700140
Elliott Hughes498cda62016-04-14 16:49:04 -0700141 if (currentIcon != NONE) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700142 if (max_stage != -1) {
143 int stage_height = gr_get_height(stageMarkerEmpty);
144 int stage_width = gr_get_width(stageMarkerEmpty);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800145 int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700146 int y = gr_fb_height() - stage_height;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800147 for (int i = 0; i < max_stage; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700148 GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
149 gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
150 x += stage_width;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800151 }
152 }
153
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700154 GRSurface* text_surface = GetCurrentText();
155 int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
156 int text_y = GetTextBaseline();
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700157 gr_color(255, 255, 255, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700158 gr_texticon(text_x, text_y, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700159 }
160}
161
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700162// Draws the animation and progress bar (if any) on the screen.
163// Does not flip pages.
Doug Zongker211aebc2011-10-28 15:13:10 -0700164// Should only be called with updateMutex locked.
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700165void ScreenRecoveryUI::draw_foreground_locked() {
166 if (currentIcon != NONE) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700167 GRSurface* frame = GetCurrentFrame();
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700168 int frame_width = gr_get_width(frame);
169 int frame_height = gr_get_height(frame);
170 int frame_x = (gr_fb_width() - frame_width) / 2;
171 int frame_y = GetAnimationBaseline();
172 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 }
174
175 if (progressBarType != EMPTY) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700176 int width = gr_get_width(progressBarEmpty);
177 int height = gr_get_height(progressBarEmpty);
178
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700179 int progress_x = (gr_fb_width() - width)/2;
180 int progress_y = GetProgressBaseline();
Doug Zongker211aebc2011-10-28 15:13:10 -0700181
182 // Erase behind the progress bar (in case this was a progress-only update)
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700183 gr_color(0, 0, 0, 255);
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700184 gr_fill(progress_x, progress_y, width, height);
Doug Zongker211aebc2011-10-28 15:13:10 -0700185
186 if (progressBarType == DETERMINATE) {
187 float p = progressScopeStart + progress * progressScopeSize;
188 int pos = (int) (p * width);
189
Doug Zongker5fa8c232012-09-18 12:37:02 -0700190 if (rtl_locale) {
191 // Fill the progress bar from right to left.
192 if (pos > 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700193 gr_blit(progressBarFill, width-pos, 0, pos, height,
194 progress_x+width-pos, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700195 }
196 if (pos < width-1) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700197 gr_blit(progressBarEmpty, 0, 0, width-pos, height, progress_x, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700198 }
199 } else {
200 // Fill the progress bar from left to right.
201 if (pos > 0) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700202 gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700203 }
204 if (pos < width-1) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700205 gr_blit(progressBarEmpty, pos, 0, width-pos, height,
206 progress_x+pos, progress_y);
Doug Zongker5fa8c232012-09-18 12:37:02 -0700207 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700208 }
209 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700210 }
211}
212
Doug Zongkerc0441d12013-07-31 11:28:24 -0700213void ScreenRecoveryUI::SetColor(UIElement e) {
214 switch (e) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700215 case INFO:
216 gr_color(249, 194, 0, 255);
217 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700218 case HEADER:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700219 gr_color(247, 0, 6, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700220 break;
221 case MENU:
222 case MENU_SEL_BG:
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700223 gr_color(0, 106, 157, 255);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700224 break;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700225 case MENU_SEL_BG_ACTIVE:
226 gr_color(0, 156, 100, 255);
227 break;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700228 case MENU_SEL_FG:
229 gr_color(255, 255, 255, 255);
230 break;
231 case LOG:
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700232 gr_color(196, 196, 196, 255);
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700233 break;
234 case TEXT_FILL:
235 gr_color(0, 0, 0, 160);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700236 break;
237 default:
238 gr_color(255, 255, 255, 255);
239 break;
240 }
241}
Doug Zongker211aebc2011-10-28 15:13:10 -0700242
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700243void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
244 SetColor(MENU);
245 *y += 4;
246 gr_fill(0, *y, gr_fb_width(), *y + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700247 *y += 4;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700248}
249
Prashant Malani7a491222016-03-11 10:00:55 -0800250void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
251 gr_text(x, *y, line, bold);
252 *y += char_height_ + 4;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700253}
254
Prashant Malani7a491222016-03-11 10:00:55 -0800255void ScreenRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700256 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
Prashant Malani7a491222016-03-11 10:00:55 -0800257 DrawTextLine(x, y, lines[i], false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700258 }
259}
260
261static const char* REGULAR_HELP[] = {
262 "Use volume up/down and power.",
263 NULL
264};
265
266static const char* LONG_PRESS_HELP[] = {
267 "Any button cycles highlight.",
268 "Long-press activates.",
269 NULL
270};
271
Doug Zongker211aebc2011-10-28 15:13:10 -0700272// Redraw everything on the screen. Does not flip pages.
273// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700274void ScreenRecoveryUI::draw_screen_locked() {
Doug Zongker39cf4172014-03-06 16:16:05 -0800275 if (!show_text) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700276 draw_background_locked();
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700277 draw_foreground_locked();
Doug Zongker39cf4172014-03-06 16:16:05 -0800278 } else {
Doug Zongker5b5f6c22014-06-03 10:50:13 -0700279 gr_color(0, 0, 0, 255);
Doug Zongker39cf4172014-03-06 16:16:05 -0800280 gr_clear();
Doug Zongker211aebc2011-10-28 15:13:10 -0700281
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800282 int y = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700283 if (show_menu) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700284 char recovery_fingerprint[PROPERTY_VALUE_MAX];
285 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
286
287 SetColor(INFO);
Prashant Malani7a491222016-03-11 10:00:55 -0800288 DrawTextLine(TEXT_INDENT, &y, "Android Recovery", true);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700289 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Prashant Malani7a491222016-03-11 10:00:55 -0800290 DrawTextLine(TEXT_INDENT, &y, chunk.c_str(), false);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700291 }
Elliott Hughes498cda62016-04-14 16:49:04 -0700292 DrawTextLines(TEXT_INDENT, &y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700293
Doug Zongkerc0441d12013-07-31 11:28:24 -0700294 SetColor(HEADER);
Prashant Malani7a491222016-03-11 10:00:55 -0800295 DrawTextLines(TEXT_INDENT, &y, menu_headers_);
Doug Zongker211aebc2011-10-28 15:13:10 -0700296
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700297 SetColor(MENU);
298 DrawHorizontalRule(&y);
299 y += 4;
300 for (int i = 0; i < menu_items; ++i) {
301 if (i == menu_sel) {
302 // Draw the highlight bar.
Elliott Hughes642aaa72015-04-10 12:47:46 -0700303 SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800304 gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700305 // Bold white text for the selected item.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700306 SetColor(MENU_SEL_FG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700307 gr_text(4, y, menu_[i], true);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700308 SetColor(MENU);
Doug Zongker211aebc2011-10-28 15:13:10 -0700309 } else {
Elliott Hughesc0491632015-05-06 12:40:05 -0700310 gr_text(4, y, menu_[i], false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700311 }
Prashant Malani7a491222016-03-11 10:00:55 -0800312 y += char_height_ + 4;
Doug Zongker211aebc2011-10-28 15:13:10 -0700313 }
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700314 DrawHorizontalRule(&y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700315 }
316
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800317 // display from the bottom up, until we hit the top of the
318 // screen, the bottom of the menu, or we've displayed the
319 // entire text buffer.
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700320 SetColor(LOG);
Elliott Hughesc0491632015-05-06 12:40:05 -0700321 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700322 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800323 for (int ty = gr_fb_height() - char_height_;
Elliott Hughesc0491632015-05-06 12:40:05 -0700324 ty >= y && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800325 ty -= char_height_, ++count) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700326 gr_text(0, ty, text_[row], false);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800327 --row;
Elliott Hughesc0491632015-05-06 12:40:05 -0700328 if (row < 0) row = text_rows_ - 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700329 }
330 }
331}
332
333// Redraw everything on the screen and flip the screen (make it visible).
334// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700335void ScreenRecoveryUI::update_screen_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700336 draw_screen_locked();
337 gr_flip();
338}
339
340// Updates only the progress bar, if possible, otherwise redraws the screen.
341// Should only be called with updateMutex locked.
Elliott Hughes8de52072015-04-08 20:06:50 -0700342void ScreenRecoveryUI::update_progress_locked() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700343 if (show_text || !pagesIdentical) {
344 draw_screen_locked(); // Must redraw the whole screen
345 pagesIdentical = true;
346 } else {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700347 draw_foreground_locked(); // Draw only the progress bar and overlays
Doug Zongker211aebc2011-10-28 15:13:10 -0700348 }
349 gr_flip();
350}
351
352// Keeps the progress bar updated, even when the process is otherwise busy.
Elliott Hughes985022a2015-04-13 13:04:32 -0700353void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) {
354 reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop();
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700355 return nullptr;
Doug Zongker32a0a472011-11-01 11:00:20 -0700356}
357
Elliott Hughes985022a2015-04-13 13:04:32 -0700358void ScreenRecoveryUI::ProgressThreadLoop() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700359 double interval = 1.0 / animation_fps;
Elliott Hughes985022a2015-04-13 13:04:32 -0700360 while (true) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700361 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700362 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700363
Elliott Hughes498cda62016-04-14 16:49:04 -0700364 bool redraw = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700365
366 // update the installation animation, if active
367 // skip this if we have a text overlay (too expensive to update)
Elliott Hughes498cda62016-04-14 16:49:04 -0700368 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) {
369 if (!intro_done) {
370 if (current_frame == intro_frames - 1) {
371 intro_done = true;
372 current_frame = 0;
373 } else {
374 ++current_frame;
375 }
376 } else {
377 current_frame = (current_frame + 1) % loop_frames;
378 }
379
380 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700381 }
382
Doug Zongker211aebc2011-10-28 15:13:10 -0700383 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700384 int duration = progressScopeDuration;
385 if (progressBarType == DETERMINATE && duration > 0) {
386 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700387 float p = 1.0 * elapsed / duration;
388 if (p > 1.0) p = 1.0;
389 if (p > progress) {
390 progress = p;
Elliott Hughes498cda62016-04-14 16:49:04 -0700391 redraw = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700392 }
393 }
394
Doug Zongker32a0a472011-11-01 11:00:20 -0700395 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700396
Doug Zongker32a0a472011-11-01 11:00:20 -0700397 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700398 double end = now();
399 // minimum of 20ms delay between frames
400 double delay = interval - (end-start);
401 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700402 usleep(static_cast<useconds_t>(delay * 1000000));
Doug Zongker211aebc2011-10-28 15:13:10 -0700403 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700404}
405
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700406void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700407 int result = res_create_display_surface(filename, surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700408 if (result < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700409 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
Doug Zongkereac881c2014-03-07 09:21:25 -0800410 }
411}
412
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700413void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700414 int result = res_create_localized_alpha_surface(filename, locale, surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700415 if (result < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700416 LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")";
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417 }
418}
419
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700420static char** Alloc2d(size_t rows, size_t cols) {
421 char** result = new char*[rows];
422 for (size_t i = 0; i < rows; ++i) {
423 result[i] = new char[cols];
424 memset(result[i], 0, cols);
425 }
426 return result;
427}
428
Tianjie Xu35926c42016-04-28 18:06:26 -0700429// Choose the right background string to display during update.
430void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) {
431 if (security_update) {
432 LoadLocalizedBitmap("installing_security_text", &installing_text);
433 } else {
434 LoadLocalizedBitmap("installing_text", &installing_text);
435 }
436 Redraw();
437}
438
Elliott Hughes8de52072015-04-08 20:06:50 -0700439void ScreenRecoveryUI::Init() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700440 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700441
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700442 density_ = static_cast<float>(property_get_int32("ro.sf.lcd_density", 160)) / 160.f;
443 is_large_ = gr_fb_height() > PixelsFromDp(800);
444
Prashant Malani7a491222016-03-11 10:00:55 -0800445 gr_font_size(&char_width_, &char_height_);
446 text_rows_ = gr_fb_height() / char_height_;
447 text_cols_ = gr_fb_width() / char_width_;
Elliott Hughesaa0d6af2015-04-08 12:42:50 -0700448
Elliott Hughesc0491632015-05-06 12:40:05 -0700449 text_ = Alloc2d(text_rows_, text_cols_ + 1);
450 file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1);
451 menu_ = Alloc2d(text_rows_, text_cols_ + 1);
Doug Zongker55a36ac2013-03-04 15:49:02 -0800452
Elliott Hughesc0491632015-05-06 12:40:05 -0700453 text_col_ = text_row_ = 0;
454 text_top_ = 1;
Doug Zongker211aebc2011-10-28 15:13:10 -0700455
Elliott Hughes498cda62016-04-14 16:49:04 -0700456 LoadBitmap("icon_error", &error_icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700457
Doug Zongker211aebc2011-10-28 15:13:10 -0700458 LoadBitmap("progress_empty", &progressBarEmpty);
459 LoadBitmap("progress_fill", &progressBarFill);
Elliott Hughes498cda62016-04-14 16:49:04 -0700460
Doug Zongkerc87bab12013-11-25 13:53:25 -0800461 LoadBitmap("stage_empty", &stageMarkerEmpty);
462 LoadBitmap("stage_fill", &stageMarkerFill);
Doug Zongker211aebc2011-10-28 15:13:10 -0700463
Tianjie Xu35926c42016-04-28 18:06:26 -0700464 // Background text for "installing_update" could be "installing update"
465 // or "installing security update". It will be set after UI init according
466 // to commands in BCB.
467 installing_text = nullptr;
Elliott Hughes498cda62016-04-14 16:49:04 -0700468 LoadLocalizedBitmap("erasing_text", &erasing_text);
469 LoadLocalizedBitmap("no_command_text", &no_command_text);
470 LoadLocalizedBitmap("error_text", &error_text);
471
472 LoadAnimation();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700473
Elliott Hughes985022a2015-04-13 13:04:32 -0700474 pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this);
Doug Zongker32a0a472011-11-01 11:00:20 -0700475
476 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700477}
478
Elliott Hughes498cda62016-04-14 16:49:04 -0700479void ScreenRecoveryUI::LoadAnimation() {
480 // How many frames of intro and loop do we have?
481 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir);
482 dirent* de;
483 while ((de = readdir(dir.get())) != nullptr) {
484 int value;
485 if (sscanf(de->d_name, "intro%d", &value) == 1 && intro_frames < (value + 1)) {
486 intro_frames = value + 1;
487 } else if (sscanf(de->d_name, "loop%d", &value) == 1 && loop_frames < (value + 1)) {
488 loop_frames = value + 1;
489 }
490 }
491
492 // It's okay to not have an intro.
493 if (intro_frames == 0) intro_done = true;
494 // But you must have an animation.
495 if (loop_frames == 0) abort();
496
497 introFrames = new GRSurface*[intro_frames];
498 for (int i = 0; i < intro_frames; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700499 // TODO: remember the names above, so we don't have to hard-code the number of 0s.
500 LoadBitmap(android::base::StringPrintf("intro%05d", i).c_str(), &introFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700501 }
502
503 loopFrames = new GRSurface*[loop_frames];
504 for (int i = 0; i < loop_frames; ++i) {
Elliott Hughesfaf36e02016-04-20 17:22:16 -0700505 LoadBitmap(android::base::StringPrintf("loop%05d", i).c_str(), &loopFrames[i]);
Elliott Hughes498cda62016-04-14 16:49:04 -0700506 }
507}
508
Doug Zongkera418aa72014-03-17 12:10:02 -0700509void ScreenRecoveryUI::SetLocale(const char* new_locale) {
Elliott Hughes498cda62016-04-14 16:49:04 -0700510 this->locale = new_locale;
511 this->rtl_locale = false;
512
513 if (locale) {
Doug Zongker5fa8c232012-09-18 12:37:02 -0700514 char* lang = strdup(locale);
515 for (char* p = lang; *p; ++p) {
516 if (*p == '_') {
517 *p = '\0';
518 break;
519 }
520 }
521
Elliott Hughes498cda62016-04-14 16:49:04 -0700522 // A bit cheesy: keep an explicit list of supported RTL languages.
Doug Zongker5fa8c232012-09-18 12:37:02 -0700523 if (strcmp(lang, "ar") == 0 || // Arabic
524 strcmp(lang, "fa") == 0 || // Persian (Farsi)
525 strcmp(lang, "he") == 0 || // Hebrew (new language code)
Doug Zongkerb66cb692012-09-18 14:52:18 -0700526 strcmp(lang, "iw") == 0 || // Hebrew (old language code)
527 strcmp(lang, "ur") == 0) { // Urdu
Doug Zongker5fa8c232012-09-18 12:37:02 -0700528 rtl_locale = true;
529 }
530 free(lang);
531 }
532}
533
Elliott Hughes8de52072015-04-08 20:06:50 -0700534void ScreenRecoveryUI::SetBackground(Icon icon) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700535 pthread_mutex_lock(&updateMutex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700536
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700537 currentIcon = icon;
538 update_screen_locked();
539
Doug Zongker211aebc2011-10-28 15:13:10 -0700540 pthread_mutex_unlock(&updateMutex);
541}
542
Elliott Hughes8de52072015-04-08 20:06:50 -0700543void ScreenRecoveryUI::SetProgressType(ProgressType type) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700544 pthread_mutex_lock(&updateMutex);
545 if (progressBarType != type) {
546 progressBarType = type;
Doug Zongker211aebc2011-10-28 15:13:10 -0700547 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700548 progressScopeStart = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700549 progressScopeSize = 0;
Doug Zongker69f4b672012-04-26 14:37:53 -0700550 progress = 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700551 update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700552 pthread_mutex_unlock(&updateMutex);
553}
554
Elliott Hughes8de52072015-04-08 20:06:50 -0700555void ScreenRecoveryUI::ShowProgress(float portion, float seconds) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700556 pthread_mutex_lock(&updateMutex);
557 progressBarType = DETERMINATE;
558 progressScopeStart += progressScopeSize;
559 progressScopeSize = portion;
560 progressScopeTime = now();
561 progressScopeDuration = seconds;
562 progress = 0;
563 update_progress_locked();
564 pthread_mutex_unlock(&updateMutex);
565}
566
Elliott Hughes8de52072015-04-08 20:06:50 -0700567void ScreenRecoveryUI::SetProgress(float fraction) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700568 pthread_mutex_lock(&updateMutex);
569 if (fraction < 0.0) fraction = 0.0;
570 if (fraction > 1.0) fraction = 1.0;
571 if (progressBarType == DETERMINATE && fraction > progress) {
572 // Skip updates that aren't visibly different.
Doug Zongkereac881c2014-03-07 09:21:25 -0800573 int width = gr_get_width(progressBarEmpty);
Doug Zongker211aebc2011-10-28 15:13:10 -0700574 float scale = width * progressScopeSize;
575 if ((int) (progress * scale) != (int) (fraction * scale)) {
576 progress = fraction;
577 update_progress_locked();
578 }
579 }
580 pthread_mutex_unlock(&updateMutex);
581}
582
Doug Zongkerc87bab12013-11-25 13:53:25 -0800583void ScreenRecoveryUI::SetStage(int current, int max) {
584 pthread_mutex_lock(&updateMutex);
585 stage = current;
586 max_stage = max;
587 pthread_mutex_unlock(&updateMutex);
588}
589
Tao Baob6918c72015-05-19 17:02:16 -0700590void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
591 std::string str;
592 android::base::StringAppendV(&str, fmt, ap);
Doug Zongker211aebc2011-10-28 15:13:10 -0700593
Tao Baob6918c72015-05-19 17:02:16 -0700594 if (copy_to_stdout) {
595 fputs(str.c_str(), stdout);
596 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700597
Doug Zongker211aebc2011-10-28 15:13:10 -0700598 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700599 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Baob6918c72015-05-19 17:02:16 -0700600 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700601 if (*ptr == '\n' || text_col_ >= text_cols_) {
602 text_[text_row_][text_col_] = '\0';
603 text_col_ = 0;
604 text_row_ = (text_row_ + 1) % text_rows_;
605 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Doug Zongker211aebc2011-10-28 15:13:10 -0700606 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700607 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Doug Zongker211aebc2011-10-28 15:13:10 -0700608 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700609 text_[text_row_][text_col_] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700610 update_screen_locked();
611 }
612 pthread_mutex_unlock(&updateMutex);
613}
614
Tao Baob6918c72015-05-19 17:02:16 -0700615void ScreenRecoveryUI::Print(const char* fmt, ...) {
616 va_list ap;
617 va_start(ap, fmt);
618 PrintV(fmt, true, ap);
619 va_end(ap);
620}
621
622void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
623 va_list ap;
624 va_start(ap, fmt);
625 PrintV(fmt, false, ap);
626 va_end(ap);
627}
628
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700629void ScreenRecoveryUI::PutChar(char ch) {
Elliott Hughes8de52072015-04-08 20:06:50 -0700630 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700631 if (ch != '\n') text_[text_row_][text_col_++] = ch;
632 if (ch == '\n' || text_col_ >= text_cols_) {
633 text_col_ = 0;
634 ++text_row_;
635
636 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Elliott Hughes8de52072015-04-08 20:06:50 -0700637 }
638 pthread_mutex_unlock(&updateMutex);
639}
640
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700641void ScreenRecoveryUI::ClearText() {
642 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700643 text_col_ = 0;
644 text_row_ = 0;
645 text_top_ = 1;
646 for (size_t i = 0; i < text_rows_; ++i) {
647 memset(text_[i], 0, text_cols_ + 1);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700648 }
649 pthread_mutex_unlock(&updateMutex);
650}
651
652void ScreenRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700653 std::vector<off_t> offsets;
654 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700655 ClearText();
656
657 struct stat sb;
658 fstat(fileno(fp), &sb);
659
660 bool show_prompt = false;
661 while (true) {
662 if (show_prompt) {
Tao Bao9a7fd802015-09-10 13:33:58 -0700663 PrintOnScreenOnly("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700664 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700665 static_cast<int>(sb.st_size));
666 Redraw();
667 while (show_prompt) {
668 show_prompt = false;
669 int key = WaitKey();
Elliott Hughes300ed082015-04-13 10:47:59 -0700670 if (key == KEY_POWER || key == KEY_ENTER) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700671 return;
672 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
673 if (offsets.size() <= 1) {
674 show_prompt = true;
675 } else {
676 offsets.pop_back();
677 fseek(fp, offsets.back(), SEEK_SET);
678 }
679 } else {
680 if (feof(fp)) {
681 return;
682 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700683 offsets.push_back(ftello(fp));
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700684 }
685 }
686 ClearText();
687 }
688
689 int ch = getc(fp);
690 if (ch == EOF) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700691 while (text_row_ < text_rows_ - 1) PutChar('\n');
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700692 show_prompt = true;
693 } else {
694 PutChar(ch);
Elliott Hughesc0491632015-05-06 12:40:05 -0700695 if (text_col_ == 0 && text_row_ >= text_rows_ - 1) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700696 show_prompt = true;
697 }
698 }
699 }
700}
701
Elliott Hughes8de52072015-04-08 20:06:50 -0700702void ScreenRecoveryUI::ShowFile(const char* filename) {
703 FILE* fp = fopen_path(filename, "re");
704 if (fp == nullptr) {
705 Print(" Unable to open %s: %s\n", filename, strerror(errno));
706 return;
707 }
Elliott Hughesc0491632015-05-06 12:40:05 -0700708
709 char** old_text = text_;
710 size_t old_text_col = text_col_;
711 size_t old_text_row = text_row_;
712 size_t old_text_top = text_top_;
713
714 // Swap in the alternate screen and clear it.
715 text_ = file_viewer_text_;
716 ClearText();
717
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700718 ShowFile(fp);
Elliott Hughes8de52072015-04-08 20:06:50 -0700719 fclose(fp);
Elliott Hughesc0491632015-05-06 12:40:05 -0700720
721 text_ = old_text;
722 text_col_ = old_text_col;
723 text_row_ = old_text_row;
724 text_top_ = old_text_top;
Elliott Hughes8de52072015-04-08 20:06:50 -0700725}
726
Doug Zongker211aebc2011-10-28 15:13:10 -0700727void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
728 int initial_selection) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700729 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700730 if (text_rows_ > 0 && text_cols_ > 0) {
731 menu_headers_ = headers;
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700732 size_t i = 0;
Elliott Hughesc0491632015-05-06 12:40:05 -0700733 for (; i < text_rows_ && items[i] != nullptr; ++i) {
734 strncpy(menu_[i], items[i], text_cols_ - 1);
735 menu_[i][text_cols_ - 1] = '\0';
Doug Zongker211aebc2011-10-28 15:13:10 -0700736 }
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700737 menu_items = i;
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700738 show_menu = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700739 menu_sel = initial_selection;
740 update_screen_locked();
741 }
742 pthread_mutex_unlock(&updateMutex);
743}
744
745int ScreenRecoveryUI::SelectMenu(int sel) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700746 pthread_mutex_lock(&updateMutex);
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700747 if (show_menu) {
Elliott Hughes01a4d082015-03-24 15:21:48 -0700748 int old_sel = menu_sel;
Doug Zongker211aebc2011-10-28 15:13:10 -0700749 menu_sel = sel;
Elliott Hughesfc06f872015-03-23 13:45:31 -0700750
751 // Wrap at top and bottom.
752 if (menu_sel < 0) menu_sel = menu_items - 1;
753 if (menu_sel >= menu_items) menu_sel = 0;
754
Doug Zongker211aebc2011-10-28 15:13:10 -0700755 sel = menu_sel;
756 if (menu_sel != old_sel) update_screen_locked();
757 }
758 pthread_mutex_unlock(&updateMutex);
759 return sel;
760}
761
762void ScreenRecoveryUI::EndMenu() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700763 pthread_mutex_lock(&updateMutex);
Elliott Hughesc0491632015-05-06 12:40:05 -0700764 if (show_menu && text_rows_ > 0 && text_cols_ > 0) {
Elliott Hughes95fc63e2015-04-10 19:12:01 -0700765 show_menu = false;
Doug Zongker211aebc2011-10-28 15:13:10 -0700766 update_screen_locked();
767 }
768 pthread_mutex_unlock(&updateMutex);
769}
770
Elliott Hughes8de52072015-04-08 20:06:50 -0700771bool ScreenRecoveryUI::IsTextVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700772 pthread_mutex_lock(&updateMutex);
773 int visible = show_text;
774 pthread_mutex_unlock(&updateMutex);
775 return visible;
776}
777
Elliott Hughes8de52072015-04-08 20:06:50 -0700778bool ScreenRecoveryUI::WasTextEverVisible() {
Doug Zongker211aebc2011-10-28 15:13:10 -0700779 pthread_mutex_lock(&updateMutex);
780 int ever_visible = show_text_ever;
781 pthread_mutex_unlock(&updateMutex);
782 return ever_visible;
783}
784
Elliott Hughes8de52072015-04-08 20:06:50 -0700785void ScreenRecoveryUI::ShowText(bool visible) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700786 pthread_mutex_lock(&updateMutex);
787 show_text = visible;
Elliott Hughes8de52072015-04-08 20:06:50 -0700788 if (show_text) show_text_ever = true;
Doug Zongker211aebc2011-10-28 15:13:10 -0700789 update_screen_locked();
790 pthread_mutex_unlock(&updateMutex);
791}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700792
Elliott Hughes8de52072015-04-08 20:06:50 -0700793void ScreenRecoveryUI::Redraw() {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700794 pthread_mutex_lock(&updateMutex);
795 update_screen_locked();
796 pthread_mutex_unlock(&updateMutex);
797}
Elliott Hughes642aaa72015-04-10 12:47:46 -0700798
799void ScreenRecoveryUI::KeyLongPress(int) {
800 // Redraw so that if we're in the menu, the highlight
801 // will change color to indicate a successful long press.
802 Redraw();
803}