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