blob: e6937755a093a5ba988122588ea3f91e64c6a6eb [file] [log] [blame]
Tao Bao337db142015-08-20 14:52:57 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
Tao Bao337db142015-08-20 14:52:57 -070019#include <stdarg.h>
Tao Bao337db142015-08-20 14:52:57 -070020#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/time.h>
24#include <sys/types.h>
25#include <time.h>
26#include <unistd.h>
27
28#include <vector>
29
30#include "common.h"
31#include "device.h"
Tao Bao337db142015-08-20 14:52:57 -070032#include "wear_ui.h"
Tao Bao337db142015-08-20 14:52:57 -070033#include "cutils/properties.h"
Elliott Hughes4b166f02015-12-04 15:30:20 -080034#include "android-base/strings.h"
Prashant Malani0eb41c32016-02-25 18:27:03 -080035#include "android-base/stringprintf.h"
Tao Bao337db142015-08-20 14:52:57 -070036
Tao Bao337db142015-08-20 14:52:57 -070037// There's only (at most) one of these objects, and global callbacks
38// (for pthread_create, and the input event system) need to find it,
39// so use a global variable.
40static WearRecoveryUI* self = NULL;
41
42// Return the current time as a double (including fractions of a second).
43static double now() {
44 struct timeval tv;
45 gettimeofday(&tv, NULL);
46 return tv.tv_sec + tv.tv_usec / 1000000.0;
47}
48
49WearRecoveryUI::WearRecoveryUI() :
Tao Bao337db142015-08-20 14:52:57 -070050 progress_bar_y(259),
51 outer_height(0),
52 outer_width(0),
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070053 menu_unusable_rows(0) {
54 intro_frames = 22;
55 loop_frames = 60;
56 animation_fps = 30;
Tao Bao337db142015-08-20 14:52:57 -070057
58 for (size_t i = 0; i < 5; i++)
59 backgroundIcon[i] = NULL;
60
Tao Bao337db142015-08-20 14:52:57 -070061 self = this;
62}
63
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070064int WearRecoveryUI::GetProgressBaseline() {
65 return progress_bar_y;
66}
67
Tao Bao337db142015-08-20 14:52:57 -070068// Draw background frame on the screen. Does not flip pages.
69// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070070// TODO merge drawing routines with screen_ui
71void WearRecoveryUI::draw_background_locked()
Tao Bao337db142015-08-20 14:52:57 -070072{
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070073 pagesIdentical = false;
Tao Bao337db142015-08-20 14:52:57 -070074 gr_color(0, 0, 0, 255);
75 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
76
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070077 if (currentIcon != NONE) {
Tao Bao337db142015-08-20 14:52:57 -070078 GRSurface* surface;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070079 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Tao Bao337db142015-08-20 14:52:57 -070080 if (!intro_done) {
81 surface = introFrames[current_frame];
82 } else {
83 surface = loopFrames[current_frame];
84 }
85 }
86 else {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070087 surface = backgroundIcon[currentIcon];
Tao Bao337db142015-08-20 14:52:57 -070088 }
89
90 int width = gr_get_width(surface);
91 int height = gr_get_height(surface);
92
93 int x = (gr_fb_width() - width) / 2;
94 int y = (gr_fb_height() - height) / 2;
95
96 gr_blit(surface, 0, 0, width, height, x, y);
97 }
98}
99
Tao Bao337db142015-08-20 14:52:57 -0700100static const char* HEADERS[] = {
101 "Swipe up/down to move.",
102 "Swipe left/right to select.",
103 "",
104 NULL
105};
106
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700107// TODO merge drawing routines with screen_ui
Tao Bao337db142015-08-20 14:52:57 -0700108void WearRecoveryUI::draw_screen_locked()
109{
Tao Bao337db142015-08-20 14:52:57 -0700110 char cur_selection_str[50];
111
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700112 draw_background_locked();
113 if (!show_text) {
114 draw_foreground_locked();
115 } else {
Tao Bao337db142015-08-20 14:52:57 -0700116 SetColor(TEXT_FILL);
117 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
118
119 int y = outer_height;
120 int x = outer_width;
121 if (show_menu) {
122 char recovery_fingerprint[PROPERTY_VALUE_MAX];
123 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
124 SetColor(HEADER);
125 DrawTextLine(x + 4, &y, "Android Recovery", true);
126 for (auto& chunk: android::base::Split(recovery_fingerprint, ":")) {
127 DrawTextLine(x +4, &y, chunk.c_str(), false);
128 }
129
130 // This is actually the help strings.
131 DrawTextLines(x + 4, &y, HEADERS);
132 SetColor(HEADER);
133 DrawTextLines(x + 4, &y, menu_headers_);
134
135 // Show the current menu item number in relation to total number if
136 // items don't fit on the screen.
137 if (menu_items > menu_end - menu_start) {
138 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700139 gr_text(gr_sys_font(), x+4, y, cur_selection_str, 1);
Prashant Malani7a491222016-03-11 10:00:55 -0800140 y += char_height_+4;
Tao Bao337db142015-08-20 14:52:57 -0700141 }
142
143 // Menu begins here
144 SetColor(MENU);
145
146 for (int i = menu_start; i < menu_end; ++i) {
147
148 if (i == menu_sel) {
149 // draw the highlight bar
150 SetColor(MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800151 gr_fill(x, y-2, gr_fb_width()-x, y+char_height_+2);
Tao Bao337db142015-08-20 14:52:57 -0700152 // white text of selected item
153 SetColor(MENU_SEL_FG);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700154 if (menu_[i][0]) {
155 gr_text(gr_sys_font(), x + 4, y, menu_[i], 1);
156 }
Tao Bao337db142015-08-20 14:52:57 -0700157 SetColor(MENU);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700158 } else if (menu_[i][0]) {
159 gr_text(gr_sys_font(), x + 4, y, menu_[i], 0);
Tao Bao337db142015-08-20 14:52:57 -0700160 }
Prashant Malani7a491222016-03-11 10:00:55 -0800161 y += char_height_+4;
Tao Bao337db142015-08-20 14:52:57 -0700162 }
163 SetColor(MENU);
164 y += 4;
165 gr_fill(0, y, gr_fb_width(), y+2);
166 y += 4;
167 }
168
169 SetColor(LOG);
170
171 // display from the bottom up, until we hit the top of the
172 // screen, the bottom of the menu, or we've displayed the
173 // entire text buffer.
174 int ty;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700175 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Tao Bao337db142015-08-20 14:52:57 -0700176 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800177 for (int ty = gr_fb_height() - char_height_ - outer_height;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700178 ty > y + 2 && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800179 ty -= char_height_, ++count) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700180 gr_text(gr_sys_font(), x+4, ty, text_[row], 0);
Tao Bao337db142015-08-20 14:52:57 -0700181 --row;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700182 if (row < 0) row = text_rows_ - 1;
Tao Bao337db142015-08-20 14:52:57 -0700183 }
184 }
185}
186
Tao Bao337db142015-08-20 14:52:57 -0700187// Keeps the progress bar updated, even when the process is otherwise busy.
188void* WearRecoveryUI::progress_thread(void *cookie) {
189 self->progress_loop();
190 return NULL;
191}
192
193void WearRecoveryUI::progress_loop() {
194 double interval = 1.0 / animation_fps;
195 for (;;) {
196 double start = now();
197 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700198 bool redraw = false;
Tao Bao337db142015-08-20 14:52:57 -0700199
200 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING)
201 && !show_text) {
202 if (!intro_done) {
Alain Vongsouvanh5b001102016-06-07 10:05:29 -0700203 if (current_frame >= intro_frames - 1) {
Tao Bao337db142015-08-20 14:52:57 -0700204 intro_done = true;
205 current_frame = 0;
206 } else {
207 current_frame++;
208 }
209 } else {
210 current_frame = (current_frame + 1) % loop_frames;
211 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700212 redraw = true;
Tao Bao337db142015-08-20 14:52:57 -0700213 }
214
215 // move the progress bar forward on timed intervals, if configured
216 int duration = progressScopeDuration;
217 if (progressBarType == DETERMINATE && duration > 0) {
218 double elapsed = now() - progressScopeTime;
219 float p = 1.0 * elapsed / duration;
220 if (p > 1.0) p = 1.0;
221 if (p > progress) {
222 progress = p;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700223 redraw = true;
Tao Bao337db142015-08-20 14:52:57 -0700224 }
225 }
226
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700227 if (redraw) update_screen_locked();
Tao Bao337db142015-08-20 14:52:57 -0700228
229 pthread_mutex_unlock(&updateMutex);
230 double end = now();
231 // minimum of 20ms delay between frames
232 double delay = interval - (end-start);
233 if (delay < 0.02) delay = 0.02;
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700234 usleep(static_cast<useconds_t>(delay * 1000000));
Tao Bao337db142015-08-20 14:52:57 -0700235 }
236}
237
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700238void WearRecoveryUI::InitTextParams() {
239 ScreenRecoveryUI::InitTextParams();
Tao Bao337db142015-08-20 14:52:57 -0700240
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700241 text_cols_ = (gr_fb_width() - (outer_width * 2)) / char_width_;
Tao Bao337db142015-08-20 14:52:57 -0700242
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700243 if (text_rows_ > kMaxRows) text_rows_ = kMaxRows;
244 if (text_cols_ > kMaxCols) text_cols_ = kMaxCols;
245
Prashant Malani7a491222016-03-11 10:00:55 -0800246 visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height_;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700247}
Tao Bao337db142015-08-20 14:52:57 -0700248
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700249void WearRecoveryUI::Init() {
250 ScreenRecoveryUI::Init();
Tao Bao337db142015-08-20 14:52:57 -0700251
252 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
253 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
254 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
255 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
256
Tao Bao337db142015-08-20 14:52:57 -0700257 pthread_create(&progress_t, NULL, progress_thread, NULL);
Tao Bao337db142015-08-20 14:52:57 -0700258
Prashant Malanif7f9e502016-03-10 03:40:20 +0000259}
260
Tao Bao337db142015-08-20 14:52:57 -0700261void WearRecoveryUI::SetStage(int current, int max)
262{
263}
264
265void WearRecoveryUI::Print(const char *fmt, ...)
266{
267 char buf[256];
268 va_list ap;
269 va_start(ap, fmt);
270 vsnprintf(buf, 256, fmt, ap);
271 va_end(ap);
272
273 fputs(buf, stdout);
274
275 // This can get called before ui_init(), so be careful.
276 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700277 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Bao337db142015-08-20 14:52:57 -0700278 char *ptr;
279 for (ptr = buf; *ptr != '\0'; ++ptr) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700280 if (*ptr == '\n' || text_col_ >= text_cols_) {
281 text_[text_row_][text_col_] = '\0';
282 text_col_ = 0;
283 text_row_ = (text_row_ + 1) % text_rows_;
284 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Tao Bao337db142015-08-20 14:52:57 -0700285 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700286 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700287 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700288 text_[text_row_][text_col_] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700289 update_screen_locked();
290 }
291 pthread_mutex_unlock(&updateMutex);
292}
293
294void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700295 int initial_selection) {
Tao Bao337db142015-08-20 14:52:57 -0700296 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700297 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Bao337db142015-08-20 14:52:57 -0700298 menu_headers_ = headers;
299 size_t i = 0;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700300 // "i < text_rows_" is removed from the loop termination condition,
Tao Bao8e9c6802015-09-02 11:20:30 -0700301 // which is different from the one in ScreenRecoveryUI::StartMenu().
302 // Because WearRecoveryUI supports scrollable menu, it's fine to have
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700303 // more entries than text_rows_. The menu may be truncated otherwise.
Tao Bao8e9c6802015-09-02 11:20:30 -0700304 // Bug: 23752519
305 for (; items[i] != nullptr; i++) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700306 strncpy(menu_[i], items[i], text_cols_ - 1);
307 menu_[i][text_cols_ - 1] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700308 }
309 menu_items = i;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700310 show_menu = true;
Tao Bao337db142015-08-20 14:52:57 -0700311 menu_sel = initial_selection;
312 menu_start = 0;
313 menu_end = visible_text_rows - 1 - menu_unusable_rows;
314 if (menu_items <= menu_end)
315 menu_end = menu_items;
316 update_screen_locked();
317 }
318 pthread_mutex_unlock(&updateMutex);
319}
320
321int WearRecoveryUI::SelectMenu(int sel) {
322 int old_sel;
323 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700324 if (show_menu) {
Tao Bao337db142015-08-20 14:52:57 -0700325 old_sel = menu_sel;
326 menu_sel = sel;
327 if (menu_sel < 0) menu_sel = 0;
328 if (menu_sel >= menu_items) menu_sel = menu_items-1;
329 if (menu_sel < menu_start) {
330 menu_start--;
331 menu_end--;
332 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
333 menu_end++;
334 menu_start++;
335 }
336 sel = menu_sel;
337 if (menu_sel != old_sel) update_screen_locked();
338 }
339 pthread_mutex_unlock(&updateMutex);
340 return sel;
341}
342
Tao Bao337db142015-08-20 14:52:57 -0700343void WearRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700344 std::vector<off_t> offsets;
345 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700346 ClearText();
347
348 struct stat sb;
349 fstat(fileno(fp), &sb);
350
351 bool show_prompt = false;
352 while (true) {
353 if (show_prompt) {
354 Print("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700355 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Tao Bao337db142015-08-20 14:52:57 -0700356 static_cast<int>(sb.st_size));
357 Redraw();
358 while (show_prompt) {
359 show_prompt = false;
360 int key = WaitKey();
361 if (key == KEY_POWER || key == KEY_ENTER) {
362 return;
363 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
364 if (offsets.size() <= 1) {
365 show_prompt = true;
366 } else {
367 offsets.pop_back();
368 fseek(fp, offsets.back(), SEEK_SET);
369 }
370 } else {
371 if (feof(fp)) {
372 return;
373 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700374 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700375 }
376 }
377 ClearText();
378 }
379
380 int ch = getc(fp);
381 if (ch == EOF) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700382 text_row_ = text_top_ = text_rows_ - 2;
Tao Bao337db142015-08-20 14:52:57 -0700383 show_prompt = true;
384 } else {
385 PutChar(ch);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700386 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
387 text_top_ = text_row_;
Tao Bao337db142015-08-20 14:52:57 -0700388 show_prompt = true;
389 }
390 }
391 }
392}
393
394void WearRecoveryUI::PutChar(char ch) {
395 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700396 if (ch != '\n') text_[text_row_][text_col_++] = ch;
397 if (ch == '\n' || text_col_ >= text_cols_) {
398 text_col_ = 0;
399 ++text_row_;
Tao Bao337db142015-08-20 14:52:57 -0700400 }
401 pthread_mutex_unlock(&updateMutex);
402}
403
404void WearRecoveryUI::ShowFile(const char* filename) {
405 FILE* fp = fopen_path(filename, "re");
406 if (fp == nullptr) {
407 Print(" Unable to open %s: %s\n", filename, strerror(errno));
408 return;
409 }
410 ShowFile(fp);
411 fclose(fp);
412}
413
414void WearRecoveryUI::ClearText() {
415 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700416 text_col_ = 0;
417 text_row_ = 0;
418 text_top_ = 1;
419 for (size_t i = 0; i < text_rows_; ++i) {
420 memset(text_[i], 0, text_cols_ + 1);
Tao Bao337db142015-08-20 14:52:57 -0700421 }
422 pthread_mutex_unlock(&updateMutex);
423}
Prashant Malani0eb41c32016-02-25 18:27:03 -0800424
425void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
426 va_list ap;
427 va_start(ap, fmt);
428 PrintV(fmt, false, ap);
429 va_end(ap);
430}
431
432void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
433 std::string str;
434 android::base::StringAppendV(&str, fmt, ap);
435
436 if (copy_to_stdout) {
437 fputs(str.c_str(), stdout);
438 }
439
440 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700441 if (text_rows_ > 0 && text_cols_ > 0) {
Prashant Malani0eb41c32016-02-25 18:27:03 -0800442 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700443 if (*ptr == '\n' || text_col_ >= text_cols_) {
444 text_[text_row_][text_col_] = '\0';
445 text_col_ = 0;
446 text_row_ = (text_row_ + 1) % text_rows_;
447 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800448 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700449 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800450 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700451 text_[text_row_][text_col_] = '\0';
Prashant Malani0eb41c32016-02-25 18:27:03 -0800452 update_screen_locked();
453 }
454 pthread_mutex_unlock(&updateMutex);
455}