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