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