blob: 65bcd849422f925c88707dc8858a3e22a95e88cf [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
37static int char_width;
38static int char_height;
39
40// There's only (at most) one of these objects, and global callbacks
41// (for pthread_create, and the input event system) need to find it,
42// so use a global variable.
43static WearRecoveryUI* self = NULL;
44
45// Return the current time as a double (including fractions of a second).
46static double now() {
47 struct timeval tv;
48 gettimeofday(&tv, NULL);
49 return tv.tv_sec + tv.tv_usec / 1000000.0;
50}
51
52WearRecoveryUI::WearRecoveryUI() :
53 progress_bar_height(3),
54 progress_bar_width(200),
55 progress_bar_y(259),
56 outer_height(0),
57 outer_width(0),
58 menu_unusable_rows(0),
59 intro_frames(22),
60 loop_frames(60),
Tao Baob723f4f2015-12-11 15:18:51 -080061 animation_fps(30),
Tao Bao337db142015-08-20 14:52:57 -070062 currentIcon(NONE),
63 intro_done(false),
64 current_frame(0),
Tao Bao337db142015-08-20 14:52:57 -070065 rtl_locale(false),
66 progressBarType(EMPTY),
67 progressScopeStart(0),
68 progressScopeSize(0),
69 progress(0),
70 text_cols(0),
71 text_rows(0),
72 text_col(0),
73 text_row(0),
74 text_top(0),
75 show_text(false),
76 show_text_ever(false),
77 show_menu(false),
78 menu_items(0),
79 menu_sel(0) {
80
81 for (size_t i = 0; i < 5; i++)
82 backgroundIcon[i] = NULL;
83
84 pthread_mutex_init(&updateMutex, NULL);
85 self = this;
86}
87
88// Draw background frame on the screen. Does not flip pages.
89// Should only be called with updateMutex locked.
90void WearRecoveryUI::draw_background_locked(Icon icon)
91{
92 gr_color(0, 0, 0, 255);
93 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
94
95 if (icon) {
96 GRSurface* surface;
97 if (icon == INSTALLING_UPDATE || icon == ERASING) {
98 if (!intro_done) {
99 surface = introFrames[current_frame];
100 } else {
101 surface = loopFrames[current_frame];
102 }
103 }
104 else {
105 surface = backgroundIcon[icon];
106 }
107
108 int width = gr_get_width(surface);
109 int height = gr_get_height(surface);
110
111 int x = (gr_fb_width() - width) / 2;
112 int y = (gr_fb_height() - height) / 2;
113
114 gr_blit(surface, 0, 0, width, height, x, y);
115 }
116}
117
118// Draw the progress bar (if any) on the screen. Does not flip pages.
119// Should only be called with updateMutex locked.
120void WearRecoveryUI::draw_progress_locked()
121{
122 if (currentIcon == ERROR) return;
123 if (progressBarType != DETERMINATE) return;
124
125 int width = progress_bar_width;
126 int height = progress_bar_height;
127 int dx = (gr_fb_width() - width)/2;
128 int dy = progress_bar_y;
129
130 float p = progressScopeStart + progress * progressScopeSize;
131 int pos = (int) (p * width);
132
133 gr_color(0x43, 0x43, 0x43, 0xff);
134 gr_fill(dx, dy, dx + width, dy + height);
135
136 if (pos > 0) {
137 gr_color(0x02, 0xa8, 0xf3, 255);
138 if (rtl_locale) {
139 // Fill the progress bar from right to left.
140 gr_fill(dx + width - pos, dy, dx + width, dy + height);
141 } else {
142 // Fill the progress bar from left to right.
143 gr_fill(dx, dy, dx + pos, dy + height);
144 }
145 }
146}
147
148void WearRecoveryUI::SetColor(UIElement e) {
149 switch (e) {
150 case HEADER:
151 gr_color(247, 0, 6, 255);
152 break;
153 case MENU:
154 case MENU_SEL_BG:
155 gr_color(0, 106, 157, 255);
156 break;
157 case MENU_SEL_FG:
158 gr_color(255, 255, 255, 255);
159 break;
160 case LOG:
161 gr_color(249, 194, 0, 255);
162 break;
163 case TEXT_FILL:
164 gr_color(0, 0, 0, 160);
165 break;
166 default:
167 gr_color(255, 255, 255, 255);
168 break;
169 }
170}
171
172void WearRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
173 gr_text(x, *y, line, bold);
174 *y += char_height + 4;
175}
176
177void WearRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
178 for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
179 DrawTextLine(x, y, lines[i], false);
180 }
181}
182
183static const char* HEADERS[] = {
184 "Swipe up/down to move.",
185 "Swipe left/right to select.",
186 "",
187 NULL
188};
189
190void WearRecoveryUI::draw_screen_locked()
191{
192 draw_background_locked(currentIcon);
193 draw_progress_locked();
194 char cur_selection_str[50];
195
196 if (show_text) {
197 SetColor(TEXT_FILL);
198 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
199
200 int y = outer_height;
201 int x = outer_width;
202 if (show_menu) {
203 char recovery_fingerprint[PROPERTY_VALUE_MAX];
204 property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
205 SetColor(HEADER);
206 DrawTextLine(x + 4, &y, "Android Recovery", true);
207 for (auto& chunk: android::base::Split(recovery_fingerprint, ":")) {
208 DrawTextLine(x +4, &y, chunk.c_str(), false);
209 }
210
211 // This is actually the help strings.
212 DrawTextLines(x + 4, &y, HEADERS);
213 SetColor(HEADER);
214 DrawTextLines(x + 4, &y, menu_headers_);
215
216 // Show the current menu item number in relation to total number if
217 // items don't fit on the screen.
218 if (menu_items > menu_end - menu_start) {
219 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
220 gr_text(x+4, y, cur_selection_str, 1);
221 y += char_height+4;
222 }
223
224 // Menu begins here
225 SetColor(MENU);
226
227 for (int i = menu_start; i < menu_end; ++i) {
228
229 if (i == menu_sel) {
230 // draw the highlight bar
231 SetColor(MENU_SEL_BG);
232 gr_fill(x, y-2, gr_fb_width()-x, y+char_height+2);
233 // white text of selected item
234 SetColor(MENU_SEL_FG);
235 if (menu[i][0]) gr_text(x+4, y, menu[i], 1);
236 SetColor(MENU);
237 } else {
238 if (menu[i][0]) gr_text(x+4, y, menu[i], 0);
239 }
240 y += char_height+4;
241 }
242 SetColor(MENU);
243 y += 4;
244 gr_fill(0, y, gr_fb_width(), y+2);
245 y += 4;
246 }
247
248 SetColor(LOG);
249
250 // display from the bottom up, until we hit the top of the
251 // screen, the bottom of the menu, or we've displayed the
252 // entire text buffer.
253 int ty;
254 int row = (text_top+text_rows-1) % text_rows;
255 size_t count = 0;
256 for (int ty = gr_fb_height() - char_height - outer_height;
257 ty > y+2 && count < text_rows;
258 ty -= char_height, ++count) {
259 gr_text(x+4, ty, text[row], 0);
260 --row;
261 if (row < 0) row = text_rows-1;
262 }
263 }
264}
265
266void WearRecoveryUI::update_screen_locked()
267{
268 draw_screen_locked();
269 gr_flip();
270}
271
272// Keeps the progress bar updated, even when the process is otherwise busy.
273void* WearRecoveryUI::progress_thread(void *cookie) {
274 self->progress_loop();
275 return NULL;
276}
277
278void WearRecoveryUI::progress_loop() {
279 double interval = 1.0 / animation_fps;
280 for (;;) {
281 double start = now();
282 pthread_mutex_lock(&updateMutex);
283 int redraw = 0;
284
285 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING)
286 && !show_text) {
287 if (!intro_done) {
288 if (current_frame == intro_frames - 1) {
289 intro_done = true;
290 current_frame = 0;
291 } else {
292 current_frame++;
293 }
294 } else {
295 current_frame = (current_frame + 1) % loop_frames;
296 }
297 redraw = 1;
298 }
299
300 // move the progress bar forward on timed intervals, if configured
301 int duration = progressScopeDuration;
302 if (progressBarType == DETERMINATE && duration > 0) {
303 double elapsed = now() - progressScopeTime;
304 float p = 1.0 * elapsed / duration;
305 if (p > 1.0) p = 1.0;
306 if (p > progress) {
307 progress = p;
308 redraw = 1;
309 }
310 }
311
312 if (redraw)
313 update_screen_locked();
314
315 pthread_mutex_unlock(&updateMutex);
316 double end = now();
317 // minimum of 20ms delay between frames
318 double delay = interval - (end-start);
319 if (delay < 0.02) delay = 0.02;
320 usleep((long)(delay * 1000000));
321 }
322}
323
324void WearRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
325 int result = res_create_display_surface(filename, surface);
326 if (result < 0) {
327 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
328 }
329}
330
331void WearRecoveryUI::Init()
332{
333 gr_init();
334
335 gr_font_size(&char_width, &char_height);
336
337 text_col = text_row = 0;
338 text_rows = (gr_fb_height()) / char_height;
339 visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height;
340 if (text_rows > kMaxRows) text_rows = kMaxRows;
341 text_top = 1;
342
343 text_cols = (gr_fb_width() - (outer_width * 2)) / char_width;
344 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
345
346 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
347 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
348 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
349 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
350
351 introFrames = (GRSurface**)malloc(intro_frames * sizeof(GRSurface*));
352 for (int i = 0; i < intro_frames; ++i) {
353 char filename[40];
354 sprintf(filename, "intro%02d", i);
355 LoadBitmap(filename, introFrames + i);
356 }
357
358 loopFrames = (GRSurface**)malloc(loop_frames * sizeof(GRSurface*));
359 for (int i = 0; i < loop_frames; ++i) {
360 char filename[40];
361 sprintf(filename, "loop%02d", i);
362 LoadBitmap(filename, loopFrames + i);
363 }
364
365 pthread_create(&progress_t, NULL, progress_thread, NULL);
366 RecoveryUI::Init();
367}
368
Tao Bao337db142015-08-20 14:52:57 -0700369void WearRecoveryUI::SetBackground(Icon icon)
370{
371 pthread_mutex_lock(&updateMutex);
372 currentIcon = icon;
373 update_screen_locked();
374 pthread_mutex_unlock(&updateMutex);
375}
376
377void WearRecoveryUI::SetProgressType(ProgressType type)
378{
379 pthread_mutex_lock(&updateMutex);
380 if (progressBarType != type) {
381 progressBarType = type;
382 }
383 progressScopeStart = 0;
384 progressScopeSize = 0;
385 progress = 0;
386 update_screen_locked();
387 pthread_mutex_unlock(&updateMutex);
388}
389
390void WearRecoveryUI::ShowProgress(float portion, float seconds)
391{
392 pthread_mutex_lock(&updateMutex);
393 progressBarType = DETERMINATE;
394 progressScopeStart += progressScopeSize;
395 progressScopeSize = portion;
396 progressScopeTime = now();
397 progressScopeDuration = seconds;
398 progress = 0;
399 update_screen_locked();
400 pthread_mutex_unlock(&updateMutex);
401}
402
403void WearRecoveryUI::SetProgress(float fraction)
404{
405 pthread_mutex_lock(&updateMutex);
406 if (fraction < 0.0) fraction = 0.0;
407 if (fraction > 1.0) fraction = 1.0;
408 if (progressBarType == DETERMINATE && fraction > progress) {
409 // Skip updates that aren't visibly different.
410 int width = progress_bar_width;
411 float scale = width * progressScopeSize;
412 if ((int) (progress * scale) != (int) (fraction * scale)) {
413 progress = fraction;
414 update_screen_locked();
415 }
416 }
417 pthread_mutex_unlock(&updateMutex);
418}
419
420void WearRecoveryUI::SetStage(int current, int max)
421{
422}
423
424void WearRecoveryUI::Print(const char *fmt, ...)
425{
426 char buf[256];
427 va_list ap;
428 va_start(ap, fmt);
429 vsnprintf(buf, 256, fmt, ap);
430 va_end(ap);
431
432 fputs(buf, stdout);
433
434 // This can get called before ui_init(), so be careful.
435 pthread_mutex_lock(&updateMutex);
436 if (text_rows > 0 && text_cols > 0) {
437 char *ptr;
438 for (ptr = buf; *ptr != '\0'; ++ptr) {
439 if (*ptr == '\n' || text_col >= text_cols) {
440 text[text_row][text_col] = '\0';
441 text_col = 0;
442 text_row = (text_row + 1) % text_rows;
443 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
444 }
445 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
446 }
447 text[text_row][text_col] = '\0';
448 update_screen_locked();
449 }
450 pthread_mutex_unlock(&updateMutex);
451}
452
453void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
454 int initial_selection) {
455 pthread_mutex_lock(&updateMutex);
456 if (text_rows > 0 && text_cols > 0) {
457 menu_headers_ = headers;
458 size_t i = 0;
Tao Bao8e9c6802015-09-02 11:20:30 -0700459 // "i < text_rows" is removed from the loop termination condition,
460 // which is different from the one in ScreenRecoveryUI::StartMenu().
461 // Because WearRecoveryUI supports scrollable menu, it's fine to have
462 // more entries than text_rows. The menu may be truncated otherwise.
463 // Bug: 23752519
464 for (; items[i] != nullptr; i++) {
Tao Bao337db142015-08-20 14:52:57 -0700465 strncpy(menu[i], items[i], text_cols - 1);
466 menu[i][text_cols - 1] = '\0';
467 }
468 menu_items = i;
469 show_menu = 1;
470 menu_sel = initial_selection;
471 menu_start = 0;
472 menu_end = visible_text_rows - 1 - menu_unusable_rows;
473 if (menu_items <= menu_end)
474 menu_end = menu_items;
475 update_screen_locked();
476 }
477 pthread_mutex_unlock(&updateMutex);
478}
479
480int WearRecoveryUI::SelectMenu(int sel) {
481 int old_sel;
482 pthread_mutex_lock(&updateMutex);
483 if (show_menu > 0) {
484 old_sel = menu_sel;
485 menu_sel = sel;
486 if (menu_sel < 0) menu_sel = 0;
487 if (menu_sel >= menu_items) menu_sel = menu_items-1;
488 if (menu_sel < menu_start) {
489 menu_start--;
490 menu_end--;
491 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
492 menu_end++;
493 menu_start++;
494 }
495 sel = menu_sel;
496 if (menu_sel != old_sel) update_screen_locked();
497 }
498 pthread_mutex_unlock(&updateMutex);
499 return sel;
500}
501
502void WearRecoveryUI::EndMenu() {
503 int i;
504 pthread_mutex_lock(&updateMutex);
505 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
506 show_menu = 0;
507 update_screen_locked();
508 }
509 pthread_mutex_unlock(&updateMutex);
510}
511
512bool WearRecoveryUI::IsTextVisible()
513{
514 pthread_mutex_lock(&updateMutex);
515 int visible = show_text;
516 pthread_mutex_unlock(&updateMutex);
517 return visible;
518}
519
520bool WearRecoveryUI::WasTextEverVisible()
521{
522 pthread_mutex_lock(&updateMutex);
523 int ever_visible = show_text_ever;
524 pthread_mutex_unlock(&updateMutex);
525 return ever_visible;
526}
527
528void WearRecoveryUI::ShowText(bool visible)
529{
530 pthread_mutex_lock(&updateMutex);
531 // Don't show text during ota install or factory reset
532 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
533 pthread_mutex_unlock(&updateMutex);
534 return;
535 }
536 show_text = visible;
537 if (show_text) show_text_ever = 1;
538 update_screen_locked();
539 pthread_mutex_unlock(&updateMutex);
540}
541
542void WearRecoveryUI::Redraw()
543{
544 pthread_mutex_lock(&updateMutex);
545 update_screen_locked();
546 pthread_mutex_unlock(&updateMutex);
547}
548
549void WearRecoveryUI::ShowFile(FILE* fp) {
550 std::vector<long> offsets;
551 offsets.push_back(ftell(fp));
552 ClearText();
553
554 struct stat sb;
555 fstat(fileno(fp), &sb);
556
557 bool show_prompt = false;
558 while (true) {
559 if (show_prompt) {
560 Print("--(%d%% of %d bytes)--",
561 static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
562 static_cast<int>(sb.st_size));
563 Redraw();
564 while (show_prompt) {
565 show_prompt = false;
566 int key = WaitKey();
567 if (key == KEY_POWER || key == KEY_ENTER) {
568 return;
569 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
570 if (offsets.size() <= 1) {
571 show_prompt = true;
572 } else {
573 offsets.pop_back();
574 fseek(fp, offsets.back(), SEEK_SET);
575 }
576 } else {
577 if (feof(fp)) {
578 return;
579 }
580 offsets.push_back(ftell(fp));
581 }
582 }
583 ClearText();
584 }
585
586 int ch = getc(fp);
587 if (ch == EOF) {
588 text_row = text_top = text_rows - 2;
589 show_prompt = true;
590 } else {
591 PutChar(ch);
592 if (text_col == 0 && text_row >= text_rows - 2) {
593 text_top = text_row;
594 show_prompt = true;
595 }
596 }
597 }
598}
599
600void WearRecoveryUI::PutChar(char ch) {
601 pthread_mutex_lock(&updateMutex);
602 if (ch != '\n') text[text_row][text_col++] = ch;
603 if (ch == '\n' || text_col >= text_cols) {
604 text_col = 0;
605 ++text_row;
606 }
607 pthread_mutex_unlock(&updateMutex);
608}
609
610void WearRecoveryUI::ShowFile(const char* filename) {
611 FILE* fp = fopen_path(filename, "re");
612 if (fp == nullptr) {
613 Print(" Unable to open %s: %s\n", filename, strerror(errno));
614 return;
615 }
616 ShowFile(fp);
617 fclose(fp);
618}
619
620void WearRecoveryUI::ClearText() {
621 pthread_mutex_lock(&updateMutex);
622 text_col = 0;
623 text_row = 0;
624 text_top = 1;
625 for (size_t i = 0; i < text_rows; ++i) {
626 memset(text[i], 0, text_cols + 1);
627 }
628 pthread_mutex_unlock(&updateMutex);
629}
Prashant Malani0eb41c32016-02-25 18:27:03 -0800630
631void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
632 va_list ap;
633 va_start(ap, fmt);
634 PrintV(fmt, false, ap);
635 va_end(ap);
636}
637
638void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
639 std::string str;
640 android::base::StringAppendV(&str, fmt, ap);
641
642 if (copy_to_stdout) {
643 fputs(str.c_str(), stdout);
644 }
645
646 pthread_mutex_lock(&updateMutex);
647 if (text_rows > 0 && text_cols > 0) {
648 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
649 if (*ptr == '\n' || text_col >= text_cols) {
650 text[text_row][text_col] = '\0';
651 text_col = 0;
652 text_row = (text_row + 1) % text_rows;
653 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
654 }
655 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
656 }
657 text[text_row][text_col] = '\0';
658 update_screen_locked();
659 }
660 pthread_mutex_unlock(&updateMutex);
661}